From ffcc9c3b4e8bacb7230a84d5caddfe0962119f1a Mon Sep 17 00:00:00 2001 From: Adam Zammit Date: Thu, 4 Jul 2013 14:54:12 +1000 Subject: [PATCH] Added case status and assignment feature which lists cases when they are available and allows the assignment to operators (requires database change) --- CHANGELOG | 10 ++ admin/casestatus.php | 218 +++++++++++++++++++++++++++++++ admin/index.php | 1 + functions/functions.operator.php | 57 +++++++- 4 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 admin/casestatus.php diff --git a/CHANGELOG b/CHANGELOG index cf7b48d9..70997011 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,13 @@ +CREATE TABLE IF NOT EXISTS `case_queue` ( + `case_queue_id` bigint(20) NOT NULL AUTO_INCREMENT, + `case_id` bigint(20) NOT NULL, + `operator_id` bigint(20) NOT NULL, + `sortorder` int(11) NOT NULL, + PRIMARY KEY (`case_queue_id`), + UNIQUE KEY `case_id` (`case_id`), + KEY `operator_id` (`operator_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + CREATE TABLE IF NOT EXISTS `sample_import_var_restrict` ( `sample_import_id` bigint(20) NOT NULL, `var` char(128) collate utf8_unicode_ci NOT NULL, diff --git a/admin/casestatus.php b/admin/casestatus.php new file mode 100644 index 00000000..15443513 --- /dev/null +++ b/admin/casestatus.php @@ -0,0 +1,218 @@ + + * @copyright Australian Consortium for Social and Political Research Incorporated (ACSPRI) 2013 + * @package queXS + * @subpackage admin + * @link http://www.acspri.org.au/ queXS was writen for ACSPRI + * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License (GPL) Version 2 + * + */ + +/** + * Configuration file + */ +include_once(dirname(__FILE__).'/../config.inc.php'); + +/** + * Database file + */ +include ("../db.inc.php"); + +/** + * XHTML functions + */ +include ("../functions/functions.xhtml.php"); + +/** + * Display functions + */ +include("../functions/functions.display.php"); + +/** + * Input functions + */ +include("../functions/functions.input.php"); + +/** + * Generate the case status report + * + * @param mixed $questionnaire_id The quesitonnaire, if specified + * @param string $sample_id The sample, if speified + * @param mixed $outcome_id THe outcome id, if specified + * + * @return false if empty otherwise true if table drawn + * @author Adam Zammit + * @since 2012-10-02 + */ +function case_status_report($questionnaire_id = false, $sample_id = false, $outcome_id = false) +{ + global $db; + + $q = ""; + if ($questionnaire_id !== false) + $q = "AND c.questionnaire_id = $questionnaire_id"; + + $s = ""; + if ($sample_id !== false) + $s = "AND s.import_id = '$sample_id'"; + + $o = ""; + if ($outcome_id !== false) + $o = "AND c.current_outcome_id = $outcome_id"; + + + + + $sql = "SELECT CONCAT('', c.case_id, '') as case_id, + o.description as outcomes, + si.description as samples, + CASE WHEN ca.end IS NULL THEN '" . T_("Now") . "' + WHEN TIME_TO_SEC(TIMEDIFF(ca.end,CONVERT_TZ(DATE_SUB(NOW(), INTERVAL co.default_delay_minutes MINUTE),'System','UTC'))) < 0 THEN '" . T_("Now") . "' + ELSE ROUND(TIME_TO_SEC(TIMEDIFF(ca.end,CONVERT_TZ(DATE_SUB(NOW(), INTERVAL co.default_delay_minutes MINUTE),'System','UTC'))) / 60) + END AS availableinmin, + CASE WHEN oq.operator_id IS NULL THEN CONCAT('". T_("Not assigned, select to assign") ." ','') + ELSE CONCAT('". T_("Assigned to") . ": ', oq.firstName, ' " . T_("Order") . ":', cq.sortorder , ' (". T_("Click to unassign") .")') + END AS assignedoperator + FROM `case` as c + JOIN questionnaire as q ON (q.questionnaire_id = c.questionnaire_id and q.enabled = 1) + JOIN outcome as o ON (o.outcome_id = c.current_outcome_id AND o.outcome_type_id = 1) + JOIN sample as s ON (s.sample_id = c.sample_id $s) + JOIN sample_import as si ON (s.import_id = si.sample_import_id) + LEFT JOIN `call` as ca ON (ca.call_id = c.last_call_id) + LEFT JOIN outcome as co ON (co.outcome_id = ca.outcome_id) + LEFT JOIN case_queue as cq ON (cq.case_id = c.case_id) + LEFT JOIN operator as oq ON (cq.operator_id = oq.operator_id) + WHERE c.current_operator_id IS NULL $q $o + ORDER BY availableinmin ASC"; + +// print $sql; + + print ("
"); + + xhtml_table($db->GetAll($sql),array('case_id','outcomes','samples','availableinmin','assignedoperator'),array(T_("Case id"),T_("Outcome"),T_("Sample"),T_("Case available in x minutes"),T_("Assigned to operator"))); + + $sql = "SELECT operator_id as value,CONCAT(firstName,' ', lastName) as description, '' selected + FROM operator + WHERE enabled = 1"; + + $rs3 = $db->GetAll($sql); + + print ""; + display_chooser($rs3, "operator_id", "operator_id",true,false,false); + + print (""); + print ("
"); + + return true; +} + +if (isset($_POST['operator_id']) && !empty($_POST['operator_id'])) +{ + $operator_id = intval($_POST['operator_id']); + + $db->StartTrans(); + + $sql = "SELECT MAX(sortorder) + FROM case_queue + WHERE operator_id = '$operator_id'"; + + $sortorder = $db->GetOne($sql); + + foreach($_POST as $key => $val) + { + $sortorder++; + + if (substr($key,0,1) == "c") + { + $sql = "INSERT INTO case_queue (case_id,operator_id,sortorder) + VALUES ('" . bigintval($val) . "', '$operator_id', '$sortorder')"; + + $db->Execute($sql); + } + } + + $db->CompleteTrans(); +} + +if (isset($_GET['unassign'])) +{ + $case_queue_id = bigintval($_GET['unassign']); + + $db->StartTrans(); + + $sql = "SELECT operator_id + FROM case_queue + WHERE case_queue_id = '$case_queue_id'"; + + $operator_id = $db->GetOne($sql); + + $sql = "DELETE FROM case_queue + WHERE case_queue_id = '$case_queue_id'"; + + $db->Execute($sql); + + $sql = "SELECT case_queue_id + FROM case_queue + WHERE operator_id = '$operator_id' + ORDER BY sortorder ASC"; + + $rs = $db->GetAll($sql); + + $sortorder = 1; + foreach($rs as $r) + { + $sql = "UPDATE case_queue + SET sortorder = '$sortorder' + WHERE case_queue_id = '{$r['case_queue_id']}'"; + + $db->Execute($sql); + + $sortorder++; + } + + + $db->CompleteTrans(); + +} + +xhtml_head(T_("Case status and assignment"),true,array("../css/table.css"),array("../js/window.js")); + +print "

" . T_("List cases by questionnaire and sample with the ability to assign them to be called next in a queue by a particular operator. If you assign cases to an operator, it will override the normal scheduling process and call them as soon as the operator is available.") . "

"; + +$questionnaire_id = false; +if (isset($_GET['questionnaire_id'])) $questionnaire_id = bigintval($_GET['questionnaire_id']); +$sample_import_id = false; +if (isset($_GET['sample_import_id']) && !empty($_GET['sample_import_id'])) $sample_import_id = bigintval($_GET['sample_import_id']); +$outcome_id = false; + +print ""; +display_questionnaire_chooser($questionnaire_id); +print ""; +display_sample_chooser($questionnaire_id,$sample_import_id); + +if ($questionnaire_id) + case_status_report($questionnaire_id,$sample_import_id,$outcome_id); + + +xhtml_foot(); + +?> diff --git a/admin/index.php b/admin/index.php index d7de5d1d..b1bb7a63 100644 --- a/admin/index.php +++ b/admin/index.php @@ -93,6 +93,7 @@ print ""; print "
  • " . T_("System settings") . "

    "; diff --git a/functions/functions.operator.php b/functions/functions.operator.php index 207e3b71..8b54ddcf 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -348,6 +348,16 @@ function get_case_id($operator_id, $create = false) $rnc = $db->GetRow($sql); + $sql = "SELECT cq.case_id, cq.case_queue_id + FROM case_queue as cq, `case` as c + WHERE cq.operator_id = '$operator_id' + AND cq.case_id = c.case_id + AND c.current_operator_id IS NULL + ORDER BY cq.sortorder ASC + LIMIT 1"; + + $sq = $db->GetRow($sql); + if (isset($rnc['next_case_id']) && !empty($rnc['next_case_id'])) { $case_id = $rnc['next_case_id']; @@ -374,7 +384,52 @@ function get_case_id($operator_id, $create = false) $db->Execute($sql); } - } + } + else if (isset($sq['case_id']) && !empty($sq['case_id'])) + { + $case_id = $sq['case_id']; + $case_queue_id = $sq['case_queue_id']; + + $sql = "UPDATE `case` + SET current_operator_id = '$operator_id' + WHERE current_operator_id IS NULL + AND case_id = '$case_id'"; + + $db->Execute($sql); + + //should fail transaction if already assigned to another case + if ($db->Affected_Rows() != 1) + { + $db->FailTrans(); + } + else + { + //remove case from queue and update sortorder + $sql = "DELETE FROM case_queue + WHERE case_queue_id = '$case_queue_id'"; + + $db->Execute($sql); + + $sql = "SELECT case_queue_id + FROM case_queue + WHERE operator_id = '$operator_id' + ORDER BY sortorder ASC"; + + $rs = $db->GetAll($sql); + + $sortorder = 1; + foreach($rs as $r) + { + $sql = "UPDATE case_queue + SET sortorder = '$sortorder' + WHERE case_queue_id = '{$r['case_queue_id']}'"; + + $db->Execute($sql); + + $sortorder++; + } + } + } else if ($create) { $systemsort = get_setting('systemsort');