Assorted minor corrections per code review.
[openemr.git] / interface / orders / qoe.inc.php
blob3a8e593cca0d62bbd297468dbf7b7f83fc68948d
1 <?php
2 /**
3 * Functions to support questions at order entry that are specific to order type.
5 * Copyright (C) 2012 Rod Roark <rod@sunsetsystems.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>.
18 * @package OpenEMR
19 * @author Rod Roark <rod@sunsetsystems.com>
22 /**
23 * Generate HTML for the QOE form suitable for insertion into a <div>.
24 * This HTML may contain single quotes but not unescaped double quotes.
26 * @param integer $ptid Value matching a procedure_type_id in the procedure_types table.
27 * @param integer $orderid Procedure order ID, if there is an existing order.
28 * @param integer $dbseq Procedure order item sequence number, if there is an existing procedure.
29 * @param string $formseq Zero-relative occurrence number in the form.
30 * @return string The generated HTML.
32 function generate_qoe_html($ptid=0, $orderid=0, $dbseq=0, $formseq=0) {
33 global $rootdir, $qoe_init_javascript;
35 $s = "";
36 $qoe_init_javascript = '';
37 $prefix = 'ans' . $formseq . '_';
39 if (empty($ptid)) return $s;
41 $s .= "<table>";
43 // Get all the questions for the given procedure order type.
44 $qres = sqlStatement("SELECT " .
45 "q.question_code, q.question_text, q.options, q.required, q.maxsize, " .
46 "q.fldtype, q.tips " .
47 "FROM procedure_type AS t " .
48 "JOIN procedure_questions AS q ON q.lab_id = t.lab_id " .
49 "AND q.procedure_code = t.procedure_code AND q.activity = 1 " .
50 "WHERE t.procedure_type_id = ? " .
51 "ORDER BY q.seq, q.question_text", array($ptid));
53 while ($qrow = sqlFetchArray($qres)) {
54 $options = trim($qrow['options']);
55 $qfieldid = $prefix . attr(trim($qrow['question_code']));
56 $fldtype = $qrow['fldtype'];
57 $maxsize = 0 + $qrow['maxsize'];
59 // Get answer value(s) to this question, if any.
60 $answers = array();
61 if ($orderid && $dbseq > 0) {
62 $ares = sqlStatement("SELECT answer FROM procedure_answers WHERE " .
63 "procedure_order_id = ? AND procedure_order_seq = ? AND question_code = ? " .
64 "ORDER BY answer_seq", array($orderid, $dbseq, $qrow['question_code']));
65 while ($arow = sqlFetchArray($ares)) {
66 $answers[] = $arow['answer'];
70 $s .= "<tr>";
71 $s .= "<td width='1%' valign='top' nowrap";
72 if ($qrow['required']) $s .= " style='color:#880000'"; // TBD: move to stylesheet
73 $s .= ">" . attr($qrow['question_text']) . "</td>";
74 $s .= "<td valign='top'>";
76 if ($fldtype == 'T') {
77 // Text Field.
78 $s .= "<input type='text' name='$qfieldid'";
79 if ($maxsize) $s .= " maxlength='$maxsize'";
80 if (!empty($answers)) $s .= " value='" . attr($answers[0]) . "'";
81 $s .= " />";
82 $s .= "&nbsp;" . text($qrow['tips']);
85 else if ($fldtype == 'N') {
86 // Numeric text Field.
87 // TBD: Add some JavaScript validation for this.
88 $s .= "<input type='text' name='$qfieldid' maxlength='8'";
89 if (!empty($answers)) $s .= " value='" . attr($answers[0]) . "'";
90 $s .= " />";
91 $s .= "&nbsp;" . text($qrow['tips']);
94 else if ($fldtype == 'D') {
95 // Date Field.
96 $s .= "<input type='text' size='10' name='$qfieldid' id='$qfieldid'";
97 if (!empty($answers)) $s .= " value='" . attr($answers[0]) . "'";
98 $s .= " onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />";
99 $s .= "<img src='$rootdir/pic/show_calendar.gif' align='absbottom' width='24' height='22'" .
100 " id='img_$qfieldid' border='0' alt='[?]' style='cursor:pointer'" .
101 " title='" . htmlspecialchars( xl('Click here to choose a date'), ENT_QUOTES) . "' />";
102 $qoe_init_javascript .= " Calendar.setup({inputField:'$qfieldid', ifFormat:'%Y-%m-%d', button:'img_$qfieldid'});";
105 else if ($fldtype == 'G') {
106 // Gestational age in weeks and days.
107 $currweeks = -1;
108 $currdays = -1;
109 if (!empty($answers)) {
110 $currweeks = intval($answers[0] / 7);
111 $currdays = $answers[0] % 7;
113 $s .= "<select name='G1_$qfieldid'>";
114 $s .= "<option value=''></option>";
115 for ($i = 5; $i <= 21; ++$i) {
116 $s .= "<option value='$i'";
117 if ($i == $currweeks) $s .= " selected";
118 $s .= ">$i</option>";
120 $s .= "</select>";
121 $s .= " " . xlt('weeks') . " &nbsp;";
122 $s .= "<select name='G2_$qfieldid'>";
123 $s .= "<option value=''></option>";
124 for ($i = 0; $i <= 6; ++$i) {
125 $s .= "<option value='$i'";
126 if ($i == $currdays) $s .= " selected";
127 $s .= ">$i</option>";
129 $s .= "</select>";
130 $s .= " " . xlt('days');
133 // Possible alternative code instead of radio buttons and checkboxes.
134 // Might use this for cases where the list of choices is large.
135 /*****************************************************************
136 else {
137 // Single- or multi-select list.
138 $multiple = false;
139 if (substr($options, 0, 2) == '+;') {
140 $multiple = true;
141 $options = substr($options, 2);
143 $s .= "<select name='$qfieldid'";
144 if ($multiple) $s .= " multiple";
145 $s .= ">";
146 $a = explode(';', $qrow['options']);
147 foreach ($a as $aval) {
148 list($desc, $code) = explode(':', $aval);
149 if (empty($code)) $code = $desc;
150 $s .= "<option value='" . attr($code) . "'";
151 if (in_array($code, $answers)) $s .= " selected";
152 $s .= ">" . text($desc) . "</option>";
154 $s .= "</select>";
156 *****************************************************************/
158 else if ($fldtype == 'M') {
159 // List of checkboxes.
160 $a = explode(';', $qrow['options']);
161 $i = 0;
162 foreach ($a as $aval) {
163 list($desc, $code) = explode(':', $aval);
164 if (empty($code)) $code = $desc;
165 if ($i) $s .= "<br />";
166 $s .= "<input type='checkbox' name='$qfieldid[$i]' value='" . attr($code) . "'";
167 if (in_array($code, $answers)) $s .= " checked";
168 $s .= " />" . text($desc);
169 ++$i;
173 else {
174 // Radio buttons.
175 $a = explode(';', $qrow['options']);
176 $i = 0;
177 foreach ($a as $aval) {
178 list($desc, $code) = explode(':', $aval);
179 if (empty($code)) $code = $desc;
180 if ($i) $s .= "<br />";
181 $s .= "<input type='radio' name='$qfieldid' value='" . attr($code) . "'";
182 if (in_array($code, $answers)) $s .= " checked";
183 $s .= " />" . text($desc);
184 ++$i;
188 $s .= '</td>';
189 $s .= '</tr>';
192 $s .= '</table>';
193 return $s;