forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuestionnaireResponseService.php
680 lines (634 loc) · 25.3 KB
/
QuestionnaireResponseService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
<?php
/**
* Service for handling Questionnaires
*
* @package OpenEMR
* @link https://www.open-emr.org
* @author Jerry Padgett <[email protected]>
* @copyright Copyright (c) 2022 Jerry Padgett <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use Exception;
use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Events\Services\ServiceSaveEvent;
use OpenEMR\FHIR\Config\ServerConfig;
use OpenEMR\FHIR\R4\FHIRDomainResource\FHIRQuestionnaire;
use OpenEMR\FHIR\R4\FHIRDomainResource\FHIRQuestionnaireResponse;
use OpenEMR\FHIR\R4\FHIRElement\FHIRCanonical;
use OpenEMR\FHIR\R4\FHIRElement\FHIRId;
use OpenEMR\FHIR\R4\FHIRElement\FHIRNarrative;
use OpenEMR\FHIR\R4\FHIRElement\FHIRNarrativeStatus;
use OpenEMR\FHIR\R4\FHIRElement\FHIRReference;
use OpenEMR\FHIR\R4\FHIRElement\FHIRString;
use OpenEMR\Services\Search\FhirSearchWhereClauseBuilder;
use OpenEMR\Validators\ProcessingResult;
class QuestionnaireResponseService extends BaseService
{
use QuestionnaireTraits;
public const TABLE_NAME = 'questionnaire_response';
private $repeatingForms;
private $formDisplayNames;
private $questionnaireService;
/**
* @throws Exception
*/
public function __construct($questionnaire = null)
{
parent::__construct(self::TABLE_NAME);
$this->questionnaireService = new QuestionnaireService();
if (!empty($questionnaire)) {
$this->setQuestionnaireForms($questionnaire);
}
}
/**
* @param $questionnaire
* @return void
* @throws Exception
*/
public function setQuestionnaireForms($questionnaire): void
{
$forms = $this->questionnaireService->createQuestionnaireFormDictionary($questionnaire);
$this->setFormDisplayNames($forms['form_display']['labels'] ?? []);
$this->setRepeatingForms($forms['repeating_forms']['labels'] ?? []);
}
/**
* @return mixed
*/
public function getRepeatingForms()
{
return $this->repeatingForms;
}
/**
* @param mixed $repeatingForms
*/
public function setRepeatingForms($repeatingForms): void
{
$this->repeatingForms = $repeatingForms;
}
/**
* @return mixed
*/
public function getFormDisplayNames()
{
return $this->formDisplayNames;
}
/**
* @param mixed $formDisplayNames
*/
public function setFormDisplayNames($formDisplayNames): void
{
$this->formDisplayNames = $formDisplayNames;
}
/**
* @param $source
* @param $delimiter
* @return string
*/
public function buildQuestionnaireResponseFlex($source, $delimiter = '|'): string
{
$html = <<<head
<html>
<body>
<form class='form'>
<div class='d-flex d-lg-flex d-md-flex d-sm-flex flex-wrap'>
head;
$title = true;
foreach ($source as $k => $value) {
$v = explode($delimiter, $k);
$last = count($v ?? []) - 1;
$item = $v[$last];
$margin_count = max(substr_count($k, 'item') - 1, 0);
$margin = attr($margin_count * 2 . 'rem');
if ($item === 'text') {
if ($title) {
$html .= "<h4>" . text($value) . "</h4>";
$title = false;
} else {
$html .= "<div class='w-100 m-0 p-0'><h5 class='my-1' style='margin-left: $margin;'>" . text($value) . "</h5></div>";
}
}
if ($item === 'question') {
$html .= "<div class='form-group my-0'><label class='my-0 font-weight-bold' style='margin-left: $margin;'>" . text($value) . ":</label>";
}
if ($item === 'answer') {
if (is_array($value ?? null)) {
if ($value['unit'] ?? null) {
$value = $value['value'] . ' ' . $value['unit'];
} else {
$v = '';
foreach ($value as $a) {
$v .= $a . ' ';
}
$value = trim($v);
}
}
$html .= "<span class='my-0 ml-1'>" . text($value) . "</span></div>";
}
}
$html .= <<<foot
</div>
</form>
</body>
</html>
foot;
return $html;
}
/**
* @param $response
* @return array
*/
public function parseQuestionnaireResponseForms($response): array
{
$o = $this->parse($response);
$fieldNames = [];
$data = [];
$instanceCount = 0;
$repeatingForms = array_fill_keys($this->repeatingForms, true);
$parseItems = function ($parent) use (&$parseItems, &$fieldNames, &$data, &$instanceCount, $repeatingForms) {
foreach ($parent->getItem() as $item) {
$answers = $item->getAnswer();
$fieldText = $this->getText($item);
if (empty($answers)) {
$parseItems($item);
} else {
foreach ($answers as $answer) {
$fn = $this->getFieldName($item);
$fieldName = $fieldText; //use "$this->getFieldName($item)" if linkId path is wanted
$value = $this->getTypedValue($answer);
$formName = $this->getInstrumentName($parent, $item);
$repeatInstrument = '';
if ($repeatingForms[$formName]) {
$repeatInstrument = $formName;
}
$fieldNames[$fieldName] = true;
$fieldData = &$data[$repeatInstrument][$fieldName];
$fieldData[] = $value;
$instanceCount = max($instanceCount, count($fieldData));
}
}
}
};
$parseItems($o);
// save to memory
$out = fopen('php://memory', 'r+');
$fieldNames = array_keys($fieldNames);
fputcsv($out, array_merge(
[
'response_id',
'repeat_instrument',
'repeat_instance'
],
$fieldNames
));
foreach ($data as $repeatInstrument => $instancesByFieldName) {
for ($instance = 1; $instance <= $instanceCount; $instance++) {
$row = [
'placeholder',
];
if (!$repeatInstrument) {
$row[] = 'Top Form';
$row[] = '';
} else {
$row[] = $repeatInstrument;
$row[] = $instance;
}
$rowHasValues = false;
foreach ($fieldNames as $fieldName) {
$value = @$instancesByFieldName[$fieldName][$instance - 1];
if (is_array($value ?? null)) {
if ($value['unit'] ?? null) {
$value = $value['value'] . ' ' . $value['unit'];
}
}
$row[] = $value;
if ($value !== null) {
$rowHasValues = true;
}
}
if ($rowHasValues) {
fputcsv($out, $row);
}
if (!$repeatInstrument) {
break; // No reason to continue iteration
}
}
}
$question_results = [];
//rewind($out);
//$question_results['csv_dictionary'] = stream_get_contents($out);
rewind($out);
$keys = fgetcsv($out, 1000);
while (($d = fgetcsv($out, 5000)) !== false) {
$question_results['form_repeats'][$d[1]] = $d[2];
$question_results['form_groups'][$d[1]][$d[2]] = array_combine($keys, $d);
$question_results['form_dictionary'][] = array_combine($keys, $d);
}
fclose($out);
return $question_results;
}
public function getUuidFields(): array
{
return ['questionnaire_response_uuid', 'encounter_uuid', 'puuid'];
}
public function search($search, $isAndCondition = true)
{
$sqlSelectIds = "SELECT DISTINCT qr.questionnaire_response_uuid ";
$sqlSelectData = " SELECT qr.*
,fe.encounter_uuid
,pd.puuid ";
$sql = "FROM (
SELECT
uuid AS questionnaire_response_uuid
,id AS id
,response_id
,questionnaire_foreign_id
,questionnaire_id
,questionnaire_name
,patient_id
,encounter
,audit_user_id
,creator_user_id
,create_time
,last_updated
,version
,status
,questionnaire
,questionnaire_response
,form_response
,form_score
,tscore
,error
FROM questionnaire_response
) qr "
. " LEFT JOIN form_questionnaire_assessments fqa ON fqa.response_id = qr.response_id " // TODO: @adunsulag is this field indexed?
. " LEFT JOIN (SELECT uuid AS questionnaire_uuid,id AS q_repo_id FROM questionnaire_repository) q_repo ON qr.questionnaire_foreign_id = q_repo.q_repo_id "
. " LEFT JOIN forms f ON fqa.id = f.form_id AND f.formdir='questionnaire_assessments' "
. " LEFT JOIN (
SELECT
uuid AS encounter_uuid
,encounter
FROM form_encounter
) fe ON f.encounter = fe.encounter "
. " LEFT JOIN (
SELECT
uuid AS puuid
,pid
FROM patient_data
) pd ON qr.patient_id = pd.pid "
// we only grab users that are actual Practitioners with a valid NPI number
. " LEFT JOIN users ON qr.creator_user_id = users.id AND users.username IS NOT NULL and users.npi IS NOT NULL AND users.npi != ''" ;
$whereUuidClause = FhirSearchWhereClauseBuilder::build($search, $isAndCondition);
$sqlUuids = $sqlSelectIds . " " . $sql . " " . $whereUuidClause->getFragment();
$uuidResults = QueryUtils::fetchTableColumn($sqlUuids, 'questionnaire_response_uuid', $whereUuidClause->getBoundValues());
if (!empty($uuidResults)) {
// now we are going to run through this again and grab all of our data w only the uuid search as our filter
// this makes sure we grab the entire patient record and associated data
$whereClause = " WHERE qr.questionnaire_response_uuid IN (" . implode(",", array_map(function ($uuid) {
return "?";
}, $uuidResults)) . ") ORDER BY qr.create_time DESC ";
$statementResults = QueryUtils::sqlStatementThrowException($sqlSelectData . $sql . $whereClause, $uuidResults);
$processingResult = new ProcessingResult();
foreach ($statementResults as $record) {
$processingResult->addData($this->createResultRecordFromDatabaseResult($record));
}
return $processingResult;
} else {
return new ProcessingResult();
}
}
/**
* @param $response
* @param $pid
* @param null $encounter
* @param null $qr_id
* @param null $qr_record_id
* @param null $q
* @param null $q_id
* @param null $form_response
* @param bool $add_report
* @param array $scores
* @return array|false|int|mixed
* @throws Exception
*/
public function saveQuestionnaireResponse(
$response,
$pid,
$encounter = null,
$qr_id = null,
$qr_record_id = null,
$q = null,
$q_id = null,
$form_response = null,
$add_report = false,
$scores = []
) {
$q_content = null;
$q_title = null;
$q_record_id = null;
$update_flag = false;
if (is_string($q)) {
$q = json_decode($q, true);
$is_json = json_last_error() === JSON_ERROR_NONE;
if (!$is_json) {
throw new Exception(xlt("Questionnaire json is invalid"));
}
}
// questionnaire. If isJason let's not reformat and use passed in json
if (is_string($q)) {
$q_content = $q;
$q = json_decode($q, true);
$is_json = json_last_error() === JSON_ERROR_NONE;
if (!$is_json) {
throw new Exception(xlt("Questionnaire json is invalid"));
}
$fhirQuestionnaireOb = new FHIRQuestionnaire($q);
} elseif (is_array($q)) {
$fhirQuestionnaireOb = new FHIRQuestionnaire($q);
$q_content = $this->jsonSerialize($q);
} else {
throw new Exception(xlt("Questionnaire argument is invalid"));
}
// response
if (is_string($response)) {
$response = json_decode($response, true);
$is_json = json_last_error() === JSON_ERROR_NONE;
if (!$is_json) {
throw new Exception(xlt("Questionnaire json is invalid"));
}
$fhirResponseOb = new FHIRQuestionnaireResponse($response);
} elseif (is_array($response)) {
$fhirResponseOb = new FHIRQuestionnaireResponse($response);
} else {
throw new Exception(xlt("Questionnaire response is invalid format"));
}
$version = 1;
if (!empty($fhirQuestionnaireOb)) {
$q_id = $q_id ?: $this->getValue($fhirQuestionnaireOb->id);
$q_title = $this->getValue($fhirQuestionnaireOb->title);
$q_name = $this->getValue($fhirQuestionnaireOb->name);
if (empty($q_title)) {
$q_title = $q_name;
}
$q_record_id = $this->questionnaireService->getQuestionnaireIdAndVersion($q_title, $q_id)['id'] ?? null;
}
if ($add_report) {
$response_array = $this->fhirObjectToArray($fhirResponseOb);
$answers = $this->flattenQuestionnaireResponse($response_array, '|', '');
$html = $this->buildQuestionnaireResponseHtml($answers, '|');
$report = new FHIRNarrative();
$report->setStatus(new FHIRNarrativeStatus(['value' => 'generated']));
$report->setDiv($html);
$fhirResponseOb->setText($report);
}
if (!empty($qr_id)) {
$update_flag = true;
} else {
$qr_id = $this->getValue($fhirResponseOb->id);
}
if (empty($qr_id)) {
$qr_uuid = (new UuidRegistry(['table_name' => 'questionnaire_response']))->createUuid();
// unique id for this set of answers
$qr_id = UuidRegistry::uuidToString($qr_uuid);
$update_flag = false;
} else {
$update_flag = true;
}
if ($update_flag) {
$id = $this->getQuestionnaireResourceIdAndVersion(null, $qr_id, null);
if (empty($id)) {
$update_flag = false;
}
}
$fhirResponseOb->setId(new FHIRId($qr_id));
$serverConfig = new ServerConfig();
$fhirResponseOb->setQuestionnaire(new FHIRCanonical($serverConfig->getFhirUrl() . '/Questionnaire/' . $q_id));
if (is_numeric($encounter)) {
$encounter_uuid = $this::getUuidById($encounter, 'form_encounter', 'encounter');
if (!empty($encounter_uuid)) {
$encounter = UuidRegistry::uuidToString($encounter_uuid) ?: $encounter;
} else {
$encounter = 0;
}
}
if (!empty($encounter)) {
$encRef = new FHIRReference();
$encRef->setReference(new FHIRString('fhir/Encounter/' . $encounter));
$fhirResponseOb->setEncounter($encRef);
}
$r_status = $fhirResponseOb->getStatus();
// todo add author and other meta
$r_content = $this->jsonSerialize($fhirResponseOb);
$dataValues = [
'uuid' => $qr_uuid
,'response_id' => $qr_id
,'questionnaire_foreign_id' => $q_record_id
,'questionnaire_id' => $q_id
,'questionnaire_name' => $q_title
// if its created by a patient we won't have an authUserID
,'audit_user_id' => $update_flag ? ($_SESSION['authUserID'] ?? null) : null
,'creator_user_id' => $_SESSION['authUserID'] ?? null
,'version' => $update_flag ? (int)$id['version'] + 1 : 1
,'last_updated' => date("Y-m-d H:i:s")
,'patient_id' => $pid
,'encounter' => $encounter
,'status' => $r_status ?: 'in-progress'
,'questionnaire' => $q_content
,'questionnaire_response' => $r_content
,'form_response' => $form_response
,'form_score' => null
,'tscore' => null
,'error' => null
,'id' => $id['id'] ?? null
,'isNew' => !$update_flag
];
$sql_insert = "INSERT INTO `questionnaire_response` (`uuid`, `response_id`, `questionnaire_foreign_id`, `questionnaire_id`, `questionnaire_name`, `audit_user_id`, `creator_user_id`, `create_time`, `last_updated`, `patient_id`,`encounter`, `version`, `status`, `questionnaire`, `questionnaire_response`, `form_response`, `form_score`, `tscore`, `error`) VALUES (?, ?, ?, ?, ?, NULL, ?, current_timestamp(), current_timestamp(), ?, ?, ?, ?, ?, ?, ?, NULL, NULL, NULL)";
$sql_update = "UPDATE `questionnaire_response` SET `audit_user_id` = ?,`version` = ?, `last_updated` = ?, `status` = ?, `questionnaire_response` = ?, `form_response` = ?, `form_score` = ?, `tscore`= ?, `error` = ? WHERE `id` = ?";
$preSaveEvent = new ServiceSaveEvent($this, $dataValues);
$updatedPreSaveEvent = $this->getEventDispatcher()->dispatch($preSaveEvent, ServiceSaveEvent::EVENT_PRE_SAVE);
if (!$updatedPreSaveEvent instanceof ServiceSaveEvent) {
$this->getLogger()->error(self::class . "->saveQuestionnaireResponse() failed ot receive valid class for " . ServiceSaveEvent::class);
}
$dataValues = $updatedPreSaveEvent->getSaveData();
if ($update_flag) {
$bind = array(
$dataValues['audit_user_id']
,$dataValues['version']
,$dataValues['last_updated']
,$dataValues['status']
,$dataValues['questionnaire_response']
,$dataValues['form_response']
,$dataValues['form_score']
,$dataValues['tscore']
,$dataValues['error']
,$dataValues['id']
);
$result = sqlQuery($sql_update, $bind);
$id = $id['id'];
} else {
$bind = array(
$dataValues['uuid'],
$dataValues['response_id'],
$dataValues['questionnaire_foreign_id'],
$dataValues['questionnaire_id'],
$dataValues['questionnaire_name'],
$dataValues['creator_user_id'],
$dataValues['patient_id'],
$dataValues['encounter'],
$dataValues['version'],
$dataValues['status'],
$dataValues['questionnaire'],
$dataValues['questionnaire_response'],
$dataValues['form_response']
);
$id = sqlInsert($sql_insert, $bind) ?: 0;
$dataValues['id'] = $id;
}
$postSaveEvent = new ServiceSaveEvent($this, $dataValues);
$updatedPostSaveEvent = $this->getEventDispatcher()->dispatch($postSaveEvent, ServiceSaveEvent::EVENT_POST_SAVE);
if (!$updatedPostSaveEvent instanceof ServiceSaveEvent) {
$this->getLogger()->error(self::class . "->saveQuestionnaireResponse() failed to receive valid class for " . ServiceSaveEvent::class);
}
return ['id' => $id, 'response_id' => $qr_id, 'new' => !$update_flag];
}
/**
* @param $items
* @param string $delimiter
* @param string $prepend
* @return array
*/
public function flattenQuestionnaireResponse($items, string $delimiter = '.', string $prepend = ''): array
{
$flatArray = [];
if (empty($items)) {
return [];
}
foreach ($items as $key => $value) {
if (is_array($value) && $value !== []) {
if ($key === 'answer') {
$flatArray[] = [$prepend . $key => $this->setAnswer($value, true)];
continue;
}
$flatArray[] = $this->flattenQuestionnaireResponse($value, $delimiter, $prepend . $key . $delimiter);
} else {
if ($key === 'text' && isset($items['answer'])) {
$key = 'question';
}
$flatArray[] = [$prepend . $key => $value];
}
}
if (count($flatArray ?? []) === 0) {
return [];
}
return array_merge_recursive([], ...$flatArray);
}
/**
* @param $source array - flattened
* @param $delimiter
* @return string
*/
public function buildQuestionnaireResponseHtml($source, $delimiter = '|'): string
{
$html = <<<head
<div style="display: flex;flex-direction: column;flex-basis: 100%;">
<form>
head;
$title = true;
foreach ($source as $k => $value) {
$v = explode($delimiter, $k);
$last = count($v ?? []) - 1;
$item = $v[$last];
$margin_count = max(substr_count($k, 'item') - 1, 0);
$margin = attr($margin_count * 1.5 . 'rem');
if ($item === 'text') {
if ($title) {
$html .= "<h4>" . text($value) . "</h4>\n";
$title = false;
} else {
$html .= "<div style='width:100%;margin:0 0;padding:0 0;'>\n<h5 style='margin:0.25rem auto 0.25rem $margin;'>" . text($value) . "</h5></div>\n";
}
}
if ($item === 'question') {
$html .= "<div style='margin: 0 auto 0;'>\n<label style='margin: 0 auto 0 $margin;'><strong>" . text($value) . ":</strong></label>\n";
}
if ($item === 'answer') {
if (is_array($value ?? null)) {
if ($value['unit'] ?? null) {
$value = $value['value'] . ' ' . $value['unit'];
} else {
$v = '';
foreach ($value as $a) {
$v .= $a . ' ';
}
$value = trim($v);
}
}
$html .= "<span style='margin: 0 auto 0 0.5rem;'>" . text($value) . "</span></div>\n";
}
}
$html .= <<<foot
</form>
</div>
foot;
return $html;
}
/**
* @param $name
* @param null $q_id
* @param null $uuid
* @return array
*/
public function getQuestionnaireResourceIdAndVersion($name, $q_id = null, $uuid = null): array
{
$sql = "Select `id`, `uuid`, response_id, `version` From `questionnaire_response` Where ((`questionnaire_name` IS NOT NULL And `questionnaire_name` = ?) Or (`response_id` IS NOT NULL And `response_id` = ?))";
$bind = array($name, $q_id);
if (!empty($uuid)) {
$sql = "Select `id`, `uuid`, response_id, `version` From `questionnaire_response` Where `uuid` = ?";
$bind = array($uuid);
}
$response = sqlQuery($sql, $bind) ?: [];
if (is_array($response) && !empty($response['uuid'] ?? null)) {
$response['uuid'] = UuidRegistry::uuidToString($response['uuid']);
}
return $response;
}
/**
* @param $id
* @param $uuid
* @return array
*/
public function fetchQuestionnaireResponseById($id, $qr_id, $uuid = null): array
{
$id = $id ?: 0;
if (!empty($uuid)) {
$sql = "Select * From `questionnaire_response` Where `uuid` = ?";
$bind = array($uuid);
} else {
$sql = "Select * From `questionnaire_response` Where (`id` = ?) Or (`response_id` IS NOT NULL And `response_id` = ?)";
$bind = array($id, $qr_id);
}
$response = sqlQuery($sql, $bind) ?: [];
if (is_array($response) && !empty($response['uuid'])) {
$response['uuid'] = UuidRegistry::uuidToString($response['uuid']);
}
return $response;
}
/**
* @param $pid
* @param $id
* @param $name
* @param $q_id
* @return array
*/
public function fetchQuestionnaireResponse($record_id = null, $qr_id = null): array
{
$sql = "Select * From `questionnaire_response` Where `id` = ? Or `response_id` = ?";
$resource = sqlQuery($sql, array($record_id, $qr_id));
return $resource ?: [];
}
public function fetchQuestionnaireResponseByResponseId($qr_id)
{
return $this->fetchQuestionnaireResponse(null, $qr_id);
}
}