forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormService.php
82 lines (68 loc) · 2.86 KB
/
FormService.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
<?php
/**
* FormService refactored getFormByEncounter
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author <Unknown> Authorship was not listed in encounter.inc.php
* @author Stephen Nielson <[email protected]>
* @copyright Copyright (c) 2021 Stephen Nielson <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use OpenEMR\Common\Database\QueryUtils;
use OpenEMR\Common\Forms\BaseForm;
class FormService
{
public function getFormByEncounter(
$attendant_id,
$encounter,
$cols = "form_id, form_name",
$name = "",
$orderby = "FIND_IN_SET(formdir,'vitals') DESC, date DESC"
) {
global $attendant_type;
$arraySqlBind = array();
$sql = "select " . escape_sql_column_name(process_cols_escape($cols), array('forms')) . " from forms where encounter = ? and deleted = 0 ";
array_push($arraySqlBind, $encounter);
if (!empty($name)) {
$sql .= "and form_name=? ";
array_push($arraySqlBind, $name);
}
if ($attendant_type == 'pid') {
$sql .= " and pid=? and therapy_group_id IS NULL ";
} else {
$sql .= " and therapy_group_id = ? and pid IS NULL ";
}
array_push($arraySqlBind, $attendant_id);
// Default $orderby puts vitals first in the list, and newpatient last:
$sql .= "ORDER BY $orderby";
$res = sqlStatement($sql, $arraySqlBind);
$all = [];
for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
$all[$iter] = $row;
}
// TODO: @adunsulag fire off a module filter event here letting us modify / restrict / add data to the form list.
return $all;
}
public function saveEncounterForm(BaseForm $form): BaseForm
{
// first we insert the form
$data = $form->getFormTableDataForSave();
$columns = implode(",", array_keys($data));
$bind = array_values($data);
$bindings = str_repeat("?,", count($data) - 1) . "?";
$sql = "INSERT INTO " . \escape_table_name($form->getFormTableName()) . " (" . $columns . ") VALUES (" . $bindings . ")";
$insert = QueryUtils::sqlInsert($sql, $bind);
$form->setFormId($insert);
$encounterFormData = $form->getEncounterFormDataForSave();
$encounterFormColumns = implode(",", array_keys($encounterFormData));
$encounterFormBind = array_values($encounterFormData);
$encounterFormBindings = str_repeat("?,", count($encounterFormData) - 1) . "?";
$encounterFormSql = "INSERT INTO forms (" . $encounterFormColumns
. ") VALUES (" . $encounterFormBindings . ")";
$id = QueryUtils::sqlInsert($encounterFormSql, $encounterFormBind);
$form->setId($id);
return $form;
}
}