From 582636fa48573b5d6da48ab7740e389a9941dbf3 Mon Sep 17 00:00:00 2001 From: sunsetsystems Date: Sun, 14 Jan 2007 19:54:26 +0000 Subject: [PATCH] new spreadsheet-like form with support for templates --- contrib/forms/treatment_protocols/info.txt | 1 + contrib/forms/treatment_protocols/new.php | 545 +++++++++++++++++++++++++++ contrib/forms/treatment_protocols/report.php | 47 +++ contrib/forms/treatment_protocols/table.sql | 18 + contrib/forms/treatment_protocols/view.php | 545 +++++++++++++++++++++++++++ 5 files changed, 1156 insertions(+) create mode 100644 contrib/forms/treatment_protocols/info.txt create mode 100644 contrib/forms/treatment_protocols/new.php create mode 100644 contrib/forms/treatment_protocols/report.php create mode 100644 contrib/forms/treatment_protocols/table.sql create mode 100644 contrib/forms/treatment_protocols/view.php diff --git a/contrib/forms/treatment_protocols/info.txt b/contrib/forms/treatment_protocols/info.txt new file mode 100644 index 000000000..ac9686b31 --- /dev/null +++ b/contrib/forms/treatment_protocols/info.txt @@ -0,0 +1 @@ +Treatment Protocols diff --git a/contrib/forms/treatment_protocols/new.php b/contrib/forms/treatment_protocols/new.php new file mode 100644 index 000000000..e4e9246b3 --- /dev/null +++ b/contrib/forms/treatment_protocols/new.php @@ -0,0 +1,545 @@ + +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. + +include_once("../../globals.php"); +include_once("$srcdir/api.inc"); +include_once("$srcdir/forms.inc"); + +// encode a string from a form field for database writing. +function form2db($fldval) { + $fldval = trim($fldval); + if (!get_magic_quotes_gpc()) $fldval = addslashes($fldval); + return $fldval; +} + +// encode a plain string for database writing. +function real2db($fldval) { + return addslashes($fldval); +} + +// Get the actual string from a form field. +function form2real($fldval) { + $fldval = trim($fldval); + if (get_magic_quotes_gpc()) $fldval = stripslashes($fldval); + return $fldval; +} + +// encode a plain string for html display. +function real2form($fldval) { + return htmlspecialchars($fldval, ENT_QUOTES); +} + +// Putting an error message in here will result in a javascript alert. +$alertmsg = ''; + +// If we are invoked as a popup (not in an encounter): +$popup = $_GET['popup']; + +// The form ID is passed to us when an existing encounter form is loaded. +$formid = $_GET['id']; + +// $tempid is the currently selected template, if any. +$tempid = $_POST['form_template'] + 0; + +// This is the start date to be saved with the spreadsheet. +$start_date = ''; + +$form_completed = '0'; + +if (!$popup && !$encounter) { // $encounter comes from globals.php + die("Internal error: we do not seem to be in an encounter!"); +} + +// Get the name of the template selected by the dropdown, if any; +// or if we are loading a form then it comes from that. +$template_name = ''; +if ($tempid) { + $trow = sqlQuery("SELECT value FROM form_treatment_protocols WHERE " . + "id = $tempid AND rownbr = -1 AND colnbr = -1"); + $template_name = $trow['value']; +} +else if ($formid) { + $trow = sqlQuery("SELECT value FROM form_treatment_protocols WHERE " . + "id = $formid AND rownbr = -1 AND colnbr = -1"); + list($form_completed, $start_date, $template_name) = explode('|', $trow['value'], 3); +} + +if (!$start_date) $start_date = form2real($_POST['form_start_date']); + +// Used rows and columns are those beyond which there are only unused cells. +$num_used_rows = 0; +$num_used_cols = 0; + +// If we are saving... +// +if ($_POST['bn_save_form'] || $_POST['bn_save_template']) { + + // The form data determines how many rows and columns are now used. + $cells = $_POST['cell']; + for ($i = 0; $i < count($cells); ++$i) { + $row = $cells[$i]; + for ($j = 0; $j < count($row); ++$j) { + if (substr($row[$j], 0, 1)) { + if ($i >= $num_used_rows) $num_used_rows = $i + 1; + if ($j >= $num_used_cols) $num_used_cols = $j + 1; + } + } + } + + if ($_POST['bn_save_form']) { + $form_completed = $_POST['form_completed'] ? '1' : '0'; + + // If updating an existing form... + if ($formid) { + sqlStatement("UPDATE form_treatment_protocols SET " . + "value = '$form_completed|$start_date|$template_name' " . + "WHERE id = '$formid' AND rownbr = -1 AND colnbr = -1"); + sqlStatement("DELETE FROM form_treatment_protocols WHERE " . + "id = '$formid' AND rownbr >= 0 AND colnbr >= 0"); + } + // If adding a new form... + else { + sqlStatement("LOCK TABLES form_treatment_protocols WRITE"); + $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM form_treatment_protocols"); + $formid = $tmprow['maxid'] + 1; + if ($formid <= 0) $formid = 1; + sqlInsert("INSERT INTO form_treatment_protocols ( " . + "id, rownbr, colnbr, datatype, value " . + ") VALUES ( " . + "$formid, -1, -1, 0, " . + "'$form_completed|$start_date|$template_name' " . + ")"); + sqlStatement("UNLOCK TABLES"); + addForm($encounter, "Treatment Protocols", $formid, "treatment_protocols", + $pid, $userauthorized); + } + $saveid = $formid; + } + else { // saving a template + // The rule is, we can update the original name, or insert a new name + // which must not match any existing template name. + $new_template_name = form2real($_POST['form_new_template_name']); + if ($new_template_name != $template_name) { + $trow = sqlQuery("SELECT id FROM form_treatment_protocols WHERE " . + "id < 0 AND rownbr = -1 AND colnbr = -1 AND value = '" . + real2db($new_template_name) . "'"); + if ($trow['id']) { + $alertmsg = "Template \"" . real2form($new_template_name) . + "\" already exists!"; + } + else { + $tempid = 0; // to force insert of new template + $template_name = $new_template_name; + } + } + if (!$alertmsg) { + // If updating an existing template... + if ($tempid) { + sqlStatement("DELETE FROM form_treatment_protocols WHERE " . + "id = '$tempid' AND rownbr >= 0 AND colnbr >= 0"); + } + // If adding a new template... + else { + sqlStatement("LOCK TABLES form_treatment_protocols WRITE"); + $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM form_treatment_protocols"); + $tempid = $tmprow['minid'] - 1; + if ($tempid >= 0) $tempid = -1; + sqlInsert("INSERT INTO form_treatment_protocols ( " . + "id, rownbr, colnbr, datatype, value " . + ") VALUES ( " . + "$tempid, -1, -1, 0, " . + "'" . real2db($template_name) . "' " . + ")"); + sqlStatement("UNLOCK TABLES"); + } + $saveid = $tempid; + } + } + + if (!$alertmsg) { + // Finally, save the table cells. + for ($i = 0; $i < $num_used_rows; ++$i) { + for ($j = 0; $j < $num_used_cols; ++$j) { + $tmp = $cells[$i][$j]; + $celltype = substr($tmp, 0, 1) + 0; + $cellvalue = form2db(substr($tmp, 1)); + if ($celltype) { + sqlInsert("INSERT INTO form_treatment_protocols ( " . + "id, rownbr, colnbr, datatype, value " . + ") VALUES ( " . + "$saveid, $i, $j, $celltype, '$cellvalue' )"); + } + } + } + } +} +else if ($_POST['bn_delete_template'] && $tempid) { + sqlStatement("DELETE FROM form_treatment_protocols WHERE " . + "id = '$tempid'"); + $tempid = 0; + $template_name = ''; +} + +if ($_POST['bn_save_form'] && !$alertmsg && !$popup) { + formHeader("Redirecting...."); + formJump(); + formFooter(); + exit; +} + +// If we get here then we are displaying a spreadsheet, either a template or +// an encounter form. + +// Get the array of template names. +$tres = sqlStatement("SELECT id, value FROM form_treatment_protocols WHERE " . + "id < 0 AND rownbr = -1 AND colnbr = -1 ORDER BY value"); + +$dres = false; + +# If we are reloading a form, get it. +if ($formid) { + $dres = sqlStatement("SELECT * FROM form_treatment_protocols WHERE " . + "id = '$formid' ORDER BY rownbr, colnbr"); + $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " . + "FROM form_treatment_protocols WHERE id = '$formid'"); + $num_used_rows = $tmprow['rowmax'] + 1; + $num_used_cols = $tmprow['colmax'] + 1; +} +# Otherwise if we are editing a template, get it. +else if ($tempid) { + $dres = sqlStatement("SELECT * FROM form_treatment_protocols WHERE " . + "id = '$tempid' ORDER BY rownbr, colnbr"); + $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " . + "FROM form_treatment_protocols WHERE id = '$tempid'"); + $num_used_rows = $tmprow['rowmax'] + 1; + $num_used_cols = $tmprow['colmax'] + 1; +} + +// Virtual rows and columns are those available when in Edit Structure mode, +// and include some additional ones beyond those used. This allows quite a +// lot of stuff to be entered before having to save the template. +$num_virtual_rows = $num_used_rows ? $num_used_rows + 5 : 10; +$num_virtual_cols = $num_used_cols ? $num_used_cols + 5 : 10; +?> + + + + + + + + + + + + + + + topmargin="0" rightmargin="0" leftmargin="0" + bottommargin="0" marginwidth="0" marginheight="0"> +
+
+ + + + + +
+ : + /> + + [?] + +   + + +   + + + +   + /> + + +
+ + +\n"; + for ($j = 0; $j < $num_virtual_cols; ++$j) { + + // Match up with the database for cell type and value. + $celltype = '0'; + $cellvalue = ''; + if ($dres) { + while ($drow && $drow['rownbr'] < $i) + $drow = sqlFetchArray($dres); + while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j) + $drow = sqlFetchArray($dres); + if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) { + $celltype = $drow['datatype']; + $cellvalue = real2form($drow['value']); + $cellstatic = addslashes($drow['value']); + } + } + + echo " \n"; + } + echo " \n"; +} +?> +
= $num_used_rows || $j >= $num_used_cols) + echo " style='display:none'"; + echo ">"; + // echo "
"; + echo "["; + echo $typeprompts[$celltype]; + echo "]"; + echo ""; + if ($celltype == '1') { + // So we don't have to write a PHP version of genStatic(): + echo ""; + } + else if ($celltype == '2') { + echo ""; + } + else if ($celltype == '3') { + echo ""; + } + // echo "
"; + echo "
+ +

+ + +  + +  + +  + + +  + +

+ +
+
+ + + diff --git a/contrib/forms/treatment_protocols/report.php b/contrib/forms/treatment_protocols/report.php new file mode 100644 index 000000000..ca6f405c3 --- /dev/null +++ b/contrib/forms/treatment_protocols/report.php @@ -0,0 +1,47 @@ + +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. + +include_once("../../globals.php"); +include_once($GLOBALS["srcdir"] . "/api.inc"); + +function treatment_protocols_report($pid, $encounter, $cols, $id) { + /**** + $cols = 1; // force always 1 column + $count = 0; + $data = sqlQuery("SELECT * " . + "FROM form_treatment_protocols WHERE " . + "id = '$id'"); + if ($data) { + print "\n\n"; + foreach($data as $key => $value) { + if ($key == "id" || $key == "pid" || $key == "user" || $key == "groupname" || + $key == "authorized" || $key == "activity" || $key == "date" || + $value == "" || $value == "0" || $value == "0.00") { + continue; + } + + if ($key == 'followup_required') { + switch ($value) { + case '1': $value = 'Yes'; break; + case '2': $value = 'Pending investigation'; break; + } + } + + $key=ucwords(str_replace("_"," ",$key)); + print "\n"; + $count++; + if ($count == $cols) { + $count = 0; + print "\n\n"; + } + } + print "\n
$key: $value  
\n"; + } + ****/ +} +?> diff --git a/contrib/forms/treatment_protocols/table.sql b/contrib/forms/treatment_protocols/table.sql new file mode 100644 index 000000000..1c8ffde96 --- /dev/null +++ b/contrib/forms/treatment_protocols/table.sql @@ -0,0 +1,18 @@ +# id is the form ID. Negative form IDs are used for the templates. +# (rownbr, colnbr) start at (0, 0) for cell data. (-1, -1) is used for the +# template name. +# +# datatype is one of: +# 0 = unused cell +# 1 = static text +# 2 = checkbox +# 3 = text input +# +CREATE TABLE IF NOT EXISTS form_treatment_protocols ( + id int NOT NULL, + rownbr int NOT NULL DEFAULT 0, + colnbr int NOT NULL DEFAULT 0, + datatype tinyint NOT NULL DEFAULT 0, + value varchar(255) DEFAULT NULL, + PRIMARY KEY (id, rownbr, colnbr) +) TYPE=MyISAM; diff --git a/contrib/forms/treatment_protocols/view.php b/contrib/forms/treatment_protocols/view.php new file mode 100644 index 000000000..e4e9246b3 --- /dev/null +++ b/contrib/forms/treatment_protocols/view.php @@ -0,0 +1,545 @@ + +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. + +include_once("../../globals.php"); +include_once("$srcdir/api.inc"); +include_once("$srcdir/forms.inc"); + +// encode a string from a form field for database writing. +function form2db($fldval) { + $fldval = trim($fldval); + if (!get_magic_quotes_gpc()) $fldval = addslashes($fldval); + return $fldval; +} + +// encode a plain string for database writing. +function real2db($fldval) { + return addslashes($fldval); +} + +// Get the actual string from a form field. +function form2real($fldval) { + $fldval = trim($fldval); + if (get_magic_quotes_gpc()) $fldval = stripslashes($fldval); + return $fldval; +} + +// encode a plain string for html display. +function real2form($fldval) { + return htmlspecialchars($fldval, ENT_QUOTES); +} + +// Putting an error message in here will result in a javascript alert. +$alertmsg = ''; + +// If we are invoked as a popup (not in an encounter): +$popup = $_GET['popup']; + +// The form ID is passed to us when an existing encounter form is loaded. +$formid = $_GET['id']; + +// $tempid is the currently selected template, if any. +$tempid = $_POST['form_template'] + 0; + +// This is the start date to be saved with the spreadsheet. +$start_date = ''; + +$form_completed = '0'; + +if (!$popup && !$encounter) { // $encounter comes from globals.php + die("Internal error: we do not seem to be in an encounter!"); +} + +// Get the name of the template selected by the dropdown, if any; +// or if we are loading a form then it comes from that. +$template_name = ''; +if ($tempid) { + $trow = sqlQuery("SELECT value FROM form_treatment_protocols WHERE " . + "id = $tempid AND rownbr = -1 AND colnbr = -1"); + $template_name = $trow['value']; +} +else if ($formid) { + $trow = sqlQuery("SELECT value FROM form_treatment_protocols WHERE " . + "id = $formid AND rownbr = -1 AND colnbr = -1"); + list($form_completed, $start_date, $template_name) = explode('|', $trow['value'], 3); +} + +if (!$start_date) $start_date = form2real($_POST['form_start_date']); + +// Used rows and columns are those beyond which there are only unused cells. +$num_used_rows = 0; +$num_used_cols = 0; + +// If we are saving... +// +if ($_POST['bn_save_form'] || $_POST['bn_save_template']) { + + // The form data determines how many rows and columns are now used. + $cells = $_POST['cell']; + for ($i = 0; $i < count($cells); ++$i) { + $row = $cells[$i]; + for ($j = 0; $j < count($row); ++$j) { + if (substr($row[$j], 0, 1)) { + if ($i >= $num_used_rows) $num_used_rows = $i + 1; + if ($j >= $num_used_cols) $num_used_cols = $j + 1; + } + } + } + + if ($_POST['bn_save_form']) { + $form_completed = $_POST['form_completed'] ? '1' : '0'; + + // If updating an existing form... + if ($formid) { + sqlStatement("UPDATE form_treatment_protocols SET " . + "value = '$form_completed|$start_date|$template_name' " . + "WHERE id = '$formid' AND rownbr = -1 AND colnbr = -1"); + sqlStatement("DELETE FROM form_treatment_protocols WHERE " . + "id = '$formid' AND rownbr >= 0 AND colnbr >= 0"); + } + // If adding a new form... + else { + sqlStatement("LOCK TABLES form_treatment_protocols WRITE"); + $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM form_treatment_protocols"); + $formid = $tmprow['maxid'] + 1; + if ($formid <= 0) $formid = 1; + sqlInsert("INSERT INTO form_treatment_protocols ( " . + "id, rownbr, colnbr, datatype, value " . + ") VALUES ( " . + "$formid, -1, -1, 0, " . + "'$form_completed|$start_date|$template_name' " . + ")"); + sqlStatement("UNLOCK TABLES"); + addForm($encounter, "Treatment Protocols", $formid, "treatment_protocols", + $pid, $userauthorized); + } + $saveid = $formid; + } + else { // saving a template + // The rule is, we can update the original name, or insert a new name + // which must not match any existing template name. + $new_template_name = form2real($_POST['form_new_template_name']); + if ($new_template_name != $template_name) { + $trow = sqlQuery("SELECT id FROM form_treatment_protocols WHERE " . + "id < 0 AND rownbr = -1 AND colnbr = -1 AND value = '" . + real2db($new_template_name) . "'"); + if ($trow['id']) { + $alertmsg = "Template \"" . real2form($new_template_name) . + "\" already exists!"; + } + else { + $tempid = 0; // to force insert of new template + $template_name = $new_template_name; + } + } + if (!$alertmsg) { + // If updating an existing template... + if ($tempid) { + sqlStatement("DELETE FROM form_treatment_protocols WHERE " . + "id = '$tempid' AND rownbr >= 0 AND colnbr >= 0"); + } + // If adding a new template... + else { + sqlStatement("LOCK TABLES form_treatment_protocols WRITE"); + $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM form_treatment_protocols"); + $tempid = $tmprow['minid'] - 1; + if ($tempid >= 0) $tempid = -1; + sqlInsert("INSERT INTO form_treatment_protocols ( " . + "id, rownbr, colnbr, datatype, value " . + ") VALUES ( " . + "$tempid, -1, -1, 0, " . + "'" . real2db($template_name) . "' " . + ")"); + sqlStatement("UNLOCK TABLES"); + } + $saveid = $tempid; + } + } + + if (!$alertmsg) { + // Finally, save the table cells. + for ($i = 0; $i < $num_used_rows; ++$i) { + for ($j = 0; $j < $num_used_cols; ++$j) { + $tmp = $cells[$i][$j]; + $celltype = substr($tmp, 0, 1) + 0; + $cellvalue = form2db(substr($tmp, 1)); + if ($celltype) { + sqlInsert("INSERT INTO form_treatment_protocols ( " . + "id, rownbr, colnbr, datatype, value " . + ") VALUES ( " . + "$saveid, $i, $j, $celltype, '$cellvalue' )"); + } + } + } + } +} +else if ($_POST['bn_delete_template'] && $tempid) { + sqlStatement("DELETE FROM form_treatment_protocols WHERE " . + "id = '$tempid'"); + $tempid = 0; + $template_name = ''; +} + +if ($_POST['bn_save_form'] && !$alertmsg && !$popup) { + formHeader("Redirecting...."); + formJump(); + formFooter(); + exit; +} + +// If we get here then we are displaying a spreadsheet, either a template or +// an encounter form. + +// Get the array of template names. +$tres = sqlStatement("SELECT id, value FROM form_treatment_protocols WHERE " . + "id < 0 AND rownbr = -1 AND colnbr = -1 ORDER BY value"); + +$dres = false; + +# If we are reloading a form, get it. +if ($formid) { + $dres = sqlStatement("SELECT * FROM form_treatment_protocols WHERE " . + "id = '$formid' ORDER BY rownbr, colnbr"); + $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " . + "FROM form_treatment_protocols WHERE id = '$formid'"); + $num_used_rows = $tmprow['rowmax'] + 1; + $num_used_cols = $tmprow['colmax'] + 1; +} +# Otherwise if we are editing a template, get it. +else if ($tempid) { + $dres = sqlStatement("SELECT * FROM form_treatment_protocols WHERE " . + "id = '$tempid' ORDER BY rownbr, colnbr"); + $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " . + "FROM form_treatment_protocols WHERE id = '$tempid'"); + $num_used_rows = $tmprow['rowmax'] + 1; + $num_used_cols = $tmprow['colmax'] + 1; +} + +// Virtual rows and columns are those available when in Edit Structure mode, +// and include some additional ones beyond those used. This allows quite a +// lot of stuff to be entered before having to save the template. +$num_virtual_rows = $num_used_rows ? $num_used_rows + 5 : 10; +$num_virtual_cols = $num_used_cols ? $num_used_cols + 5 : 10; +?> + + + + + + + + + + + + + + + topmargin="0" rightmargin="0" leftmargin="0" + bottommargin="0" marginwidth="0" marginheight="0"> +
+
+ + + + + +
+ : + /> + + [?] + +   + + +   + + + +   + /> + + +
+ + +\n"; + for ($j = 0; $j < $num_virtual_cols; ++$j) { + + // Match up with the database for cell type and value. + $celltype = '0'; + $cellvalue = ''; + if ($dres) { + while ($drow && $drow['rownbr'] < $i) + $drow = sqlFetchArray($dres); + while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j) + $drow = sqlFetchArray($dres); + if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) { + $celltype = $drow['datatype']; + $cellvalue = real2form($drow['value']); + $cellstatic = addslashes($drow['value']); + } + } + + echo " \n"; + } + echo " \n"; +} +?> +
= $num_used_rows || $j >= $num_used_cols) + echo " style='display:none'"; + echo ">"; + // echo "
"; + echo "["; + echo $typeprompts[$celltype]; + echo "]"; + echo ""; + if ($celltype == '1') { + // So we don't have to write a PHP version of genStatic(): + echo ""; + } + else if ($celltype == '2') { + echo ""; + } + else if ($celltype == '3') { + echo ""; + } + // echo "
"; + echo "
+ +

+ + +  + +  + +  + + +  + +

+ +
+
+ + + -- 2.11.4.GIT