From db1bcfb1df1b63de800142edafd182bf5b8c47a1 Mon Sep 17 00:00:00 2001 From: azammitdcarf Date: Fri, 27 Mar 2009 03:32:03 +0000 Subject: [PATCH] Added quota row functionality (ability to assign a quota to remove records by row from a sample) --- admin/index.php | 1 + admin/quotarow.php | 261 +++++++++++++++++++++++++++++++ database/quexs.sql | 21 +++ functions/functions.operator.php | 67 +++++++- functions/functions.xhtml.php | 10 +- 5 files changed, 351 insertions(+), 9 deletions(-) create mode 100755 admin/quotarow.php diff --git a/admin/index.php b/admin/index.php index 8eff4a55..52ab1155 100644 --- a/admin/index.php +++ b/admin/index.php @@ -55,6 +55,7 @@ print "
  • " . T_("Import a print "
  • " . T_("Assign samples to questionnaires") . "
  • "; print "
  • " . T_("Set values in questionnaire to pre fill") . "
  • "; print "
  • " . T_("Quota management") . "
  • "; +print "
  • " . T_("Quota row management") . "
  • "; print "
  • " . T_("Add operators to the system") . "
  • "; print "
  • " . T_("Assign operators to questionnaires") . "
  • "; print "
  • " . T_("Modify operator skills") . "
  • "; diff --git a/admin/quotarow.php b/admin/quotarow.php new file mode 100755 index 00000000..7f62c318 --- /dev/null +++ b/admin/quotarow.php @@ -0,0 +1,261 @@ + + * @copyright Deakin University 2007,2008,2009 + * @package queXS + * @subpackage admin + * @link http://www.deakin.edu.au/dcarf/ queXS was writen for DCARF - Deakin Computer Assisted Research Facility + * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License (GPL) Version 2 + * + * + */ + +/** + * Configuration file + */ +include("../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"); + +/** + * Limesurvey functions + */ +include("../functions/functions.limesurvey.php"); + +/** + * Operator functions + */ +include("../functions/functions.operator.php"); + + +global $db; + + +if (isset($_GET['questionnaire_id']) && isset($_GET['sgqa']) && isset($_GET['value']) && isset($_GET['completions']) && isset($_GET['sample_import_id']) && isset($_GET['comparison']) && isset($_GET['exclude_var']) && isset($_GET['exclude_val'])) +{ + //need to add quota + + $questionnaire_id = bigintval($_GET['questionnaire_id']); + $sample_import_id = bigintval($_GET['sample_import_id']); + $value = $db->quote($_GET['value']); + $completions = $db->quote($_GET['completions']); + $sgqa = $db->quote($_GET['sgqa']); + $comparison = $db->quote($_GET['comparison']); + $exclude_val = $db->quote($_GET['exclude_val']); + $exclude_var = $db->quote($_GET['exclude_var']); + + $sql = "INSERT INTO questionnaire_sample_quota_row(questionnaire_id, sample_import_id, lime_sgqa,value,completions,comparison,exclude_var,exclude_val) + VALUES ($questionnaire_id, $sample_import_id, $sgqa, $value, $completions, $comparison, $exclude_var, $exclude_val)"; + + $db->Execute($sql); + + //Make sure to calculate on the spot + update_quotas($questionnaire_id); +} + +if (isset($_GET['questionnaire_id']) && isset($_GET['questionnaire_sample_quota_row_id'])) +{ + //need to remove quota + + $questionnaire_id = bigintval($_GET['questionnaire_id']); + $questionnaire_sample_quota_row_id = bigintval($_GET['questionnaire_sample_quota_row_id']); + + $sql = "DELETE FROM questionnaire_sample_quota_row + WHERE questionnaire_sample_quota_row_id = '$questionnaire_sample_quota_row_id'"; + + $db->Execute($sql); + +} + +$questionnaire_id = false; +if (isset($_GET['questionnaire_id'])) $questionnaire_id = bigintval($_GET['questionnaire_id']); + +xhtml_head(T_("Quota row management"),true,false,array("../js/window.js")); +print "

    " . T_("Select a questionnaire from the list below") . "

    "; + +$sql = "SELECT questionnaire_id as value,description, CASE WHEN questionnaire_id = '$questionnaire_id' THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM questionnaire"; +display_chooser($db->GetAll($sql),"questionnaire","questionnaire_id"); + + +if ($questionnaire_id != false) +{ + $sample_import_id = false; + if (isset($_GET['sample_import_id'])) $sample_import_id = bigintval($_GET['sample_import_id']); + + print "

    " . T_("Select a sample from the list below") . "

    "; + + $sql = "SELECT s.sample_import_id as value,s.description, CASE WHEN s.sample_import_id = '$sample_import_id' THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM sample_import as s, questionnaire_sample as q + WHERE q.questionnaire_id = $questionnaire_id + AND q.sample_import_id = s.sample_import_id"; + + display_chooser($db->GetAll($sql),"sample","sample_import_id",true,"questionnaire_id=$questionnaire_id"); + + if ($sample_import_id != false) + { + print "

    " . T_("Current row quotas (click to delete)") . "

    "; + + $sql = "SELECT questionnaire_sample_quota_row_id,lime_sgqa,value,completions,quota_reached,lime_sid,comparison,exclude_var,exclude_val + FROM questionnaire_sample_quota_row as qsq, questionnaire as q + WHERE qsq.questionnaire_id = '$questionnaire_id' + AND qsq.sample_import_id = '$sample_import_id' + AND q.questionnaire_id = '$questionnaire_id'"; + + $r = $db->GetAll($sql); + + if (empty($r)) + { + print "

    " . T_("Currently no row quotas") . "

    "; + } + else + { + foreach($r as $v) + { + print "
    " . T_("Stop calling") . " " . $v['exclude_var'] . " " . T_("like") . " " . $v['exclude_val'] . " " . T_("rows from this sample when:") . " {$v['lime_sgqa']} {$v['comparison']} {$v['value']} " . T_("for") . ": {$v['completions']} " . T_("completions") ." - "; + + if ($v['quota_reached'] == 1) + print T_("Row quota reached"); + else + print T_("Row quota not yet reached"); + + print " - " . T_("Current completions: ") . limesurvey_quota_completions($v['lime_sgqa'],$v['lime_sid'],$questionnaire_id,$sample_import_id,$v['value'],$v['comparison']); + + print "
    "; + + } + } + + + print "

    " . T_("Select a question for the row quota") . "

    "; + + $sql = "SELECT lime_sid + FROM questionnaire + WHERE questionnaire_id = '$questionnaire_id'"; + + $r = $db->GetRow($sql); + + $lime_sid = $r['lime_sid']; + + $sgqa = false; + if (isset($_GET['sgqa'])) $sgqa = $_GET['sgqa']; + + $sql = "SELECT CONCAT( q.sid, 'X', q.gid, 'X', q.qid, IFNULL( a.code, '' ) ) AS value, CONCAT(q.question, ': ', IFNULL(a.answer,'')) as description, CASE WHEN CONCAT( q.sid, 'X', q.gid, 'X', q.qid, IFNULL( a.code, '' ) ) = '$sgqa' THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM `" . LIME_PREFIX . "questions` AS q + LEFT JOIN `" . LIME_PREFIX . "answers` AS a ON ( a.qid = q.qid ) + WHERE q.sid = '$lime_sid'"; + + + display_chooser($ldb->GetAll($sql),"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id"); + + if ($sgqa != false) + { + $sample_var = false; + if (isset($_GET['sample_var'])) + $sample_var = $_GET['sample_var']; + + print "

    " . T_("Select the sample variable to exclude") . "

    "; + + $sql = "SELECT sv.var as value, sv.var as description, CASE WHEN sv.var LIKE '$sample_var' THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM sample_var AS sv, sample AS s + WHERE s.import_id = $sample_import_id + AND s.sample_id = sv.sample_id + GROUP BY sv.var"; + + display_chooser($db->GetAll($sql),"sample_var","sample_var",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id&sgqa=$sgqa"); + + if ($sample_var != false) + { + print "

    " . T_("Enter the details for creating the row quota:") . "

    "; + print "

    " . T_("Pre defined values for this question:") . "

    "; + + $qid = explode("X", $sgqa); + $qid = $qid[2]; + + $sql = "SELECT l.code,l.title + FROM `" . LIME_PREFIX . "labels` as l, `" . LIME_PREFIX . "questions` as q + WHERE q.qid = '$qid' + AND l.lid = q.lid"; + + $rs = $ldb->GetAll($sql); + + if (!isset($rs) || empty($rs)) + print "

    " . T_("No labels defined for this question") ."

    "; + else + xhtml_table($rs,array('code','title'),array(T_("Code value"), T_("Description"))); + + + ?> +
    +

    +
    +
    +
    + + GetAll($sql),"exclude_val","exclude_val",false,false,false,false); + flush(); + ?> +
    + + + + + "/>

    +
    + diff --git a/database/quexs.sql b/database/quexs.sql index f8a8759b..1b152414 100644 --- a/database/quexs.sql +++ b/database/quexs.sql @@ -518,6 +518,27 @@ CREATE TABLE IF NOT EXISTS `questionnaire_sample_quota` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; +-- +-- Table structure for table `questionnaire_sample_quota_row` +-- + +CREATE TABLE IF NOT EXISTS `questionnaire_sample_quota_row` ( + `questionnaire_sample_quota_row_id` bigint(20) NOT NULL auto_increment, + `questionnaire_id` bigint(20) NOT NULL, + `sample_import_id` bigint(20) NOT NULL, + `lime_sgqa` varchar(255) collate utf8_unicode_ci NOT NULL, + `value` varchar(2048) collate utf8_unicode_ci NOT NULL, + `comparison` varchar(15) collate utf8_unicode_ci NOT NULL default 'LIKE', + `completions` int(11) NOT NULL, + `exclude_var` char(128) collate utf8_unicode_ci NOT NULL, + `exclude_val` varchar(256) collate utf8_unicode_ci NOT NULL, + `quota_reached` tinyint(1) NOT NULL default '0', + PRIMARY KEY (`questionnaire_sample_quota_row_id`), + KEY `questionnaire_id` (`questionnaire_id`), + KEY `sample_import_id` (`sample_import_id`), + KEY `exclude_var` (`exclude_var`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + -- -------------------------------------------------------- diff --git a/functions/functions.operator.php b/functions/functions.operator.php index 364381f0..b2a2e114 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -302,6 +302,7 @@ function get_case_id($operator_id, $create = false) AND ((apn.appointment_id IS NOT NULL) or qs.call_attempt_max = 0 or ((SELECT count(*) FROM call_attempt WHERE case_id = c.case_id) < qs.call_attempt_max)) AND ((apn.appointment_id IS NOT NULL) or qs.call_max = 0 or ((SELECT count(*) FROM `call` WHERE case_id = c.case_id) < qs.call_max)) AND (SELECT count(*) FROM `questionnaire_sample_quota` WHERE questionnaire_id = c.questionnaire_id AND sample_import_id = s.import_id AND quota_reached = 1) = 0 + AND (SELECT count(*) FROM `questionnaire_sample_quota_row` as qsqr, sample_var as sv WHERE qsqr.questionnaire_id = c.questionnaire_id AND qsqr.sample_import_id = s.import_id AND qsqr.quota_reached = 1 and sv.sample_id = s.sample_id AND sv.var = qsqr.exclude_var AND qsqr.exclude_val LIKE sv.val) = 0 ORDER BY apn.start DESC, a.start ASC LIMIT 1"; @@ -336,6 +337,7 @@ function get_case_id($operator_id, $create = false) AND !(q.restrict_work_shifts = 1 AND sh.shift_id IS NULL) AND !(si.call_restrict = 1 AND cr.day_of_week IS NULL) AND (SELECT count(*) FROM `questionnaire_sample_quota` WHERE questionnaire_id = qs.questionnaire_id AND sample_import_id = s.import_id AND quota_reached = 1) = 0 + AND (SELECT count(*) FROM `questionnaire_sample_quota_row` as qsqr, sample_var as sv WHERE qsqr.questionnaire_id = qs.questionnaire_id AND qsqr.sample_import_id = s.import_id AND qsqr.quota_reached = 1 AND sv.sample_id = s.sample_id AND sv.var = qsqr.exclude_var AND qsqr.exclude_val LIKE sv.val) = 0 ORDER BY rand() * qs.random_select, s.sample_id LIMIT 1"; @@ -959,13 +961,11 @@ function missed_appointment($call_attempt_id) } /** - * Check if any quotas apply to this questionnaire - * and if so, update them - * - * @param int $questionnaire_id The questionnaire id + * Update the quota table (sample wide) * + * @param int $questionnaire_id The questionnaire ID to update */ -function update_quotas($questionnaire_id) +function update_quota($questionnaire_id) { global $db; @@ -999,6 +999,63 @@ function update_quotas($questionnaire_id) } return false; + +} + + +/** + * Update the row quota table + * + * @param int $questionnaire_id The questionnaire ID to update + */ +function update_row_quota($questionnaire_id) +{ + global $db; + + $sql = "SELECT questionnaire_sample_quota_row_id,q.questionnaire_id,sample_import_id,lime_sgqa,value,comparison,completions,quota_reached,q.lime_sid + FROM questionnaire_sample_quota_row as qsq, questionnaire as q + WHERE qsq.questionnaire_id = '$questionnaire_id' + AND q.questionnaire_id = '$questionnaire_id'"; + + $rs = $db->GetAll($sql); + + if (isset($rs) && !empty($rs)) + { + //include limesurvey functions + include_once(dirname(__FILE__).'/functions.limesurvey.php'); + + //update all row quotas for this questionnaire + foreach($rs as $r) + { + $completions = limesurvey_quota_completions($r['lime_sgqa'],$r['lime_sid'],$r['questionnaire_id'],$r['sample_import_id'],$r['value'],$r['comparison']); + + if ($completions >= $r['completions']) + { + //set row quota to reached + $sql = "UPDATE questionnaire_sample_quota_row + SET quota_reached = '1' + WHERE questionnaire_sample_quota_row_id = {$r['questionnaire_sample_quota_row_id']}"; + + $db->Execute($sql); + } + } + } + + return false; + +} + +/** + * Check if any quotas apply to this questionnaire + * and if so, update them + * + * @param int $questionnaire_id The questionnaire id + * + */ +function update_quotas($questionnaire_id) +{ + update_quota($questionnaire_id); + update_row_quota($questionnaire_id); } diff --git a/functions/functions.xhtml.php b/functions/functions.xhtml.php index 946a5984..b8d1d385 100644 --- a/functions/functions.xhtml.php +++ b/functions/functions.xhtml.php @@ -135,11 +135,13 @@ function xhtml_table($content,$fields,$head = false,$class = "tclass",$highlight * @param bool $useblank Add a blank element to the start of the list * @param string|bool $pass Anything to pass along in the return string (remember to separate with &) * @param bool $js Whether to use JS or not + * @param bool $indiv Whether to display in a div or not * */ -function display_chooser($elements, $selectid, $var, $useblank = true, $pass = false, $js = true) +function display_chooser($elements, $selectid, $var, $useblank = true, $pass = false, $js = true, $indiv = true) { - print "
    "; if ($useblank) @@ -166,8 +168,8 @@ function display_chooser($elements, $selectid, $var, $useblank = true, $pass = f if (isset($e['selected'])) print $e['selected']; print ">".$e['description'].""; } - print "
    "; - + print ""; + if ($indiv) print ""; }