Bump phpmailer/phpmailer from 6.1.5 to 6.1.6 (#3567)
[openemr.git] / library / forms.inc
blob57ecda29d60c8441b20353734bbfb7711f3f674f
1 <?php
3 use OpenEMR\Common\Acl\AclMain;
5 $GLOBALS['form_exit_url'] = "javascript:parent.closeTab(window.name, false)";
7 function getFormByEncounter(
8     $attendant_id,
9     $encounter,
10     $cols = "form_id, form_name",
11     $name = "",
12     $orderby = "FIND_IN_SET(formdir,'vitals') DESC, date DESC"
13 ) {
15     global $attendant_type;
16     $arraySqlBind = array();
17     $sql = "select " . escape_sql_column_name(process_cols_escape($cols), array('forms')) . " from forms where encounter = ? and deleted = 0 ";
18     array_push($arraySqlBind, $encounter);
19     if (!empty($name)) {
20         $sql .= "and form_name=? ";
21         array_push($arraySqlBind, $name);
22     }
24     if ($attendant_type == 'pid') {
25         $sql .= " and pid=? and therapy_group_id IS NULL ";
26     } else {
27         $sql .= " and therapy_group_id = ? and pid IS NULL ";
28     }
30     array_push($arraySqlBind, $attendant_id);
32     // Default $orderby puts vitals first in the list, and newpatient last:
33     $sql .= "ORDER BY $orderby";
35     $res = sqlStatement($sql, $arraySqlBind);
37     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
38         $all[$iter] = $row;
39     }
41     return $all;
44 function addForm(
45     $encounter,
46     $form_name,
47     $form_id,
48     $formdir,
49     $pid,
50     $authorized = "0",
51     $date = "NOW()",
52     $user = "",
53     $group = "",
54     $therapy_group = 'not_given'
55 ) {
57     global $attendant_type;
58     if (!$user) {
59         $user = $_SESSION['authUser'];
60     }
62     if (!$group) {
63         $group = $_SESSION['authProvider'];
64     }
66     if ($therapy_group == 'not_given') {
67         $therapy_group = $attendant_type == 'pid' ? null : $_SESSION['therapy_group'];
68     }
70     //print_r($_SESSION['therapy_group']);die;
71         $arraySqlBind = array();
72     $sql = "insert into forms (date, encounter, form_name, form_id, pid, " .
73         "user, groupname, authorized, formdir, therapy_group_id) values (";
74     if ($date == "NOW()") {
75         $sql .= "$date";
76     } else {
77         $sql .= "?";
78                 array_push($arraySqlBind, $date);
79     }
81     $sql .= ", ?, ?, ?, ?, ?, ?, ?, ?, ?)";
82         array_push($arraySqlBind, $encounter, $form_name, $form_id, $pid, $user, $group, $authorized, $formdir, $therapy_group);
83     return sqlInsert($sql, $arraySqlBind);
86 function authorizeForm($id, $authorized = "1")
88     sqlQuery("UPDATE forms SET authorized = ? WHERE id = ? AND deleted = 0", array($authorized, $id));
91 function getEncounters($pid, $dateStart = '', $dateEnd = '', $encounterRuleType = '')
93     $arraySqlBind = array();
95     if ($encounterRuleType) {
96         // Only collect certain type of encounters (list_options item from the rule_enc_types list that is mapped via enc_category_map table)
97         $from = "form_encounter LEFT JOIN enc_category_map ON (form_encounter.pc_catid = enc_category_map.main_cat_id)";
98         $where = "enc_category_map.rule_enc_id = ? and ";
99         array_push($arraySqlBind, $encounterRuleType);
100     } else {
101         // Collect all encounters
102         $from = "form_encounter";
103     }
105     if ($dateStart && $dateEnd) {
106         $where .= "form_encounter.pid = ? and form_encounter.date >= ? and form_encounter.date <= ?";
107         array_push($arraySqlBind, $pid, $dateStart, $dateEnd);
108     } elseif ($dateStart && !$dateEnd) {
109         $where .= "form_encounter.pid = ? and form_encounter.date >= ?";
110         array_push($arraySqlBind, $pid, $dateStart);
111     } elseif (!$dateStart && $dateEnd) {
112         $where .= "form_encounter.pid = ? and form_encounter.date <= ?";
113         array_push($arraySqlBind, $pid, $dateEnd);
114     } else {
115         $where .= "form_encounter.pid = ?";
116         array_push($arraySqlBind, $pid);
117     }
119     //Not table escaping $from since this is hard-coded above and can include more than just a table name
120     $res = sqlStatement("SELECT distinct encounter FROM " . $from . " WHERE " . $where . " ORDER by date desc", $arraySqlBind);
122     for ($iter = 0; $row = sqlFetchArray($res); $iter++) {
123         $all[$iter] = $row;
124     }
126     return $all;
129 function getEncounterDateByEncounter($encounter)
131     global $attendant_type;
132     $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
133     // $sql = "select date from forms where encounter='$encounter' order by date";
134     $sql = "SELECT date FROM " . escape_table_name($table) . " WHERE encounter = ? ORDER BY date";
135     return sqlQuery($sql, array($encounter));
138 function getProviderIdOfEncounter($encounter)
140         global $attendant_type;
141         $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
142         $sql = "SELECT provider_id FROM " . escape_table_name($table) . " WHERE encounter=? ORDER BY date";
143         $res = sqlQuery($sql, array($encounter));
144         return $res['provider_id'];
147 function getFormNameByFormdirAndFormid($formdir, $form_id)
149     return sqlQuery("SELECT form_name FROM forms WHERE formdir = ? AND form_id = ? AND deleted = 0", array($formdir, $form_id));
152 function getFormIdByFormdirAndFormid($formdir, $form_id)
154     $result = sqlQuery("select id from forms where formdir = ? and form_id = ? and deleted = 0 ", array( $formdir, $form_id ));
155     return $result['id'];
158 function getFormNameByFormdir($formdir)
160     return sqlQuery("SELECT form_name FROM forms WHERE formdir = ? AND deleted = 0", array($formdir));
163 function getDocumentsByEncounter($patientID = null, $encounterID = null)
165     $allDocuments = null;
166     $currentEncounter = ( $encounterID ) ? $encounterID : $_SESSION['encounter'];
167     $currentPatient = ( $patientID ) ? $patientID : $_SESSION['pid'];
169     if ($currentPatient != "" && $currentEncounter != "") {
170         $sql = "SELECT d.id, d.type, d.url, d.docdate, d.list_id, c.name,d.encounter_id FROM documents AS d, categories_to_documents AS cd,
171                         categories AS c WHERE d.foreign_id = ? AND d.encounter_id=? AND cd.document_id = d.id AND c.id = cd.category_id ORDER BY d.docdate DESC, d.id DESC";
172         $res = sqlStatement($sql, array($currentPatient,$currentEncounter));
174         while ($row = sqlFetchArray($res)) {
175             $allDocuments[] = $row;
176         }
177     }
179     return $allDocuments;
182 function hasFormPermission($formDir)
184     // get the aco spec from registry table
185     $formRow = sqlQuery("SELECT aco_spec FROM registry WHERE directory = ?", array($formDir));
186     $permission = explode('|', $formRow['aco_spec']);
187     return AclMain::aclCheckCore($permission[0], $permission[1]);