diff --git a/CHANGELOG b/CHANGELOG index 21de19e3..c94c63cb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,6 +7,46 @@ CREATE TABLE `questionnaire_sample_timeslot` ( PRIMARY KEY ( `questionnaire_id` , `availability_group_id` , `sample_import_id` ) ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci; +CREATE TABLE `qsqr_question` ( + `qsqr_question_id` bigint(20) NOT NULL AUTO_INCREMENT, + `questionnaire_sample_quota_row_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, + `description` text COLLATE utf8_unicode_ci NOT NULL, + PRIMARY KEY (`qsqr_question_id`), + KEY `questionnaire_sample_quota_row_id` (`questionnaire_sample_quota_row_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; + +CREATE TABLE `qsqr_sample` ( + `qsqr_sample_id` bigint(20) NOT NULL AUTO_INCREMENT, + `questionnaire_sample_quota_row_id` bigint(20) NOT NULL, + `exclude_var` char(128) COLLATE utf8_unicode_ci NOT NULL, + `exclude_val` varchar(256) COLLATE utf8_unicode_ci NOT NULL, + `comparison` varchar(15) COLLATE utf8_unicode_ci NOT NULL, + `description` text COLLATE utf8_unicode_ci NOT NULL, + PRIMARY KEY (`qsqr_sample_id`), + KEY `questionnaire_sample_quota_row_id` (`questionnaire_sample_quota_row_id`), + KEY `exclude_var` (`exclude_var`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; + + +INSERT INTO qsqr_question (questionnaire_sample_quota_row_id,lime_sgqa,value,comparison) +SELECT questionnaire_sample_quota_row_id, lime_sgqa, value, comparison +FROM questionnaire_sample_quota_row +WHERE lime_sgqa != -1 AND lime_sgqa != -2; + +INSERT INTO qsqr_sample (questionnaire_sample_quota_row_id,exclude_var,exclude_val,comparison) +SELECT questionnaire_sample_quota_row_id, exclude_var,exclude_val, 'LIKE' +FROM questionnaire_sample_quota_row; + +ALTER TABLE `questionnaire_sample_quota_row` + DROP `lime_sgqa`, + DROP `value`, + DROP `comparison`, + DROP `exclude_var`, + DROP `exclude_val`; + queXS 1.11.1 - Changes since 1.11.0 Fixed Bug: Quota priority not able to override sort of last call made @@ -47,7 +87,6 @@ PRIMARY KEY ( `questionnaire_id` , `availability_group_id` ) INSERT INTO `setting` (`setting_id`,`field`,`value`) VALUES (NULL , 'DEFAULT_TIME_ZONE', 's:18:"Australia/Victoria";'); - queXS 1.10.4 - Changes since 1.10.3 Fixed bug: Quota priority could get below 0 diff --git a/admin/questionnairelist.php b/admin/questionnairelist.php index d9c37803..14e8ab7f 100644 --- a/admin/questionnairelist.php +++ b/admin/questionnairelist.php @@ -158,6 +158,22 @@ if (isset($_POST['questionnaire_id']) && isset($_POST['submit'])) $db->Execute($sql); + $sql = "DELETE FROM qsqr_question + WHERE questionnaire_sample_quota_row_id IN ( + SELECT questionnaire_sample_quota_row_id + FROM questionnaire_sample_quota_row + WHERE questionnaire_id = $questionnaire_id)"; + + $db->Execute($sql); + + $sql = "DELETE FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id IN ( + SELECT questionnaire_sample_quota_row_id + FROM questionnaire_sample_quota_row + WHERE questionnaire_id = $questionnaire_id)"; + + $db->Execute($sql); + $sql = "DELETE FROM `questionnaire_sample_quota_row` WHERE questionnaire_id = $questionnaire_id"; diff --git a/admin/quotareport.php b/admin/quotareport.php index 3004ccc3..d4318c87 100644 --- a/admin/quotareport.php +++ b/admin/quotareport.php @@ -178,12 +178,12 @@ if ($questionnaire_id) $report[] = array("strata" => T_("Total sample"), "quota" => $drawn + $remain, "sample" => $drawn + $remain, "sampleused" => $drawn, "sampleremain" => $remain, "completions" => $completions, "perc" => ROUND(($completions / ($drawn + $remain)) * 100,2)); //a. (Standard quota) Monitor outcomes of questions in completed questionnaires, and exclude selected sample records when completion limit is reached - //b. (Replicate quota) Exclude selected sample records (where lime_sgqa == -1) - $sql = "SELECT questionnaire_sample_quota_row_id,lime_sgqa,value,completions,quota_reached,lime_sid,comparison,exclude_var,exclude_val,qsq.description,current_completions, priority, autoprioritise - FROM questionnaire_sample_quota_row as qsq, questionnaire as q - WHERE qsq.questionnaire_id = '$questionnaire_id' + //b. (Replicate quota) Exclude selected sample records (where no qsqr_question rows) + $sql = "SELECT qsq.questionnaire_sample_quota_row_id,completions,quota_reached,lime_sid,qsq.description,current_completions, priority, autoprioritise + 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'"; + AND q.questionnaire_id = '$questionnaire_id'"; $r = $db->GetAll($sql); @@ -197,22 +197,29 @@ if ($questionnaire_id) $qsqr = $v['questionnaire_sample_quota_row_id']; - if ($v['lime_sgqa'] == -1) - { - $v['completions'] = ""; - $perc = ""; - } - else - { - $perc = ($v['completions'] <= 0 ? 0 : ROUND(($completions / ($v['completions'])) * 100,2)); - } + $perc = ($v['completions'] <= 0 ? 0 : ROUND(($completions / ($v['completions'])) * 100,2)); + //We need to calc Sample size, Sample drawn, Sample remain $sql = "SELECT (c.sample_id is not null) as type, count(*) as count FROM sample as s - JOIN questionnaire_sample as qs ON (qs.questionnaire_id = '$questionnaire_id' and qs.sample_import_id = s.import_id) - JOIN sample_var as sv ON (sv.sample_id = s.sample_id AND sv.var LIKE '{$v['exclude_var']}' AND sv.val LIKE '{$v['exclude_val']}') - LEFT JOIN `case` as c ON (c.questionnaire_id = qs.questionnaire_id and c.sample_id = s.sample_id) + JOIN questionnaire_sample as qs ON (qs.questionnaire_id = '$questionnaire_id' and qs.sample_import_id = s.import_id) "; + + $sql2 = "SELECT exclude_val,exclude_var,comparison + FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id = {$v['questionnaire_sample_quota_row_id']}"; + + $rev = $db->GetAll($sql2); + + //reduce sample by every item in the qsqr_sample table + $x = 1; + foreach($rev as $ev) + { + $sql .= " JOIN sample_var as sv$x ON (sv$x.sample_id = s.sample_id AND sv$x.var LIKE '{$ev['exclude_var']}' AND sv$x.val {$ev['comparison']} '{$ev['exclude_val']}') "; + $x++; + } + + $sql .= " LEFT JOIN `case` as c ON (c.questionnaire_id = qs.questionnaire_id and c.sample_id = s.sample_id) WHERE s.import_id = '$sample_import_id' GROUP BY (c.sample_id is not null)"; @@ -227,7 +234,7 @@ if ($questionnaire_id) if ($r['type'] == 0) $remain = $r['count']; } - if ($completions < $v['completions'] || $v['lime_sgqa'] == -1) //if completions less than the quota, allow for closing/opening + if ($completions < $v['completions']) //if completions less than the quota, allow for closing/opening { if ($v['quota_reached'] == 1) $status = "" . T_("closed") . ""; @@ -242,7 +249,7 @@ if ($questionnaire_id) $status = T_("open"); } - $report[] = array("strata" => "" . $v['description'] . "", "status" => $status, "quota" => $v['completions'], "sample" => $drawn + $remain, "sampleused" => $drawn, "sampleremain" => $remain, "completions" => $completions, "perc" => $perc, "priority" => "", "autoprioritise" => ""); + $report[] = array("strata" => "" . $v['description'] . "", "status" => $status, "quota" => $v['completions'], "sample" => $drawn + $remain, "sampleused" => $drawn, "sampleremain" => $remain, "completions" => $completions, "perc" => $perc, "priority" => "", "autoprioritise" => ""); } //c. (Questionnaire quota) Monitor outcomes of questions in completed questionnaires, and abort interview when completion limit is reached diff --git a/admin/quotarow.php b/admin/quotarow.php index b4f3fced..552eb7b4 100755 --- a/admin/quotarow.php +++ b/admin/quotarow.php @@ -69,61 +69,217 @@ 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'])) + +if (isset($_POST['submitdelete'])) +{ + foreach($_POST as $key => $val) + { + if (substr($key,0,7) == "select_") + { + $tmp = bigintval(substr($key,7)); + open_row_quota($tmp); + } + } +} + +if (isset($_POST['submitexport'])) +{ + $csv = array(); + foreach($_POST as $key => $val) + { + if (substr($key,0,7) == "select_") + { + $tmp = bigintval(substr($key,7)); + + $sql = "SELECT description,completions,autoprioritise + FROM questionnaire_sample_quota_row + WHERE questionnaire_sample_quota_row_id = $tmp"; + + $rs = $db->GetRow($sql); + + $sql = "SELECT lime_sgqa,comparison,value + FROM qsqr_question + WHERE questionnaire_sample_quota_row_id = $tmp"; + + $q2 = $db->GetAll($sql); + + $sql = "SELECT exclude_var as samplerecord,comparison,exclude_val as value + FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id = $tmp"; + + $q3 = $db->GetAll($sql); + + $ta = array($rs['description'],$rs['completions'],$rs['autoprioritise']); + + //just search where col 1 looks like 333X2X2 and assume it is a question + + foreach($q2 as $qr) + foreach($qr as $qe => $val) + $ta[] = $val; + + foreach($q3 as $qr) + foreach($qr as $qe => $val) + $ta[] = $val; + + $csv[] = $ta; + } + } + if (!empty($csv)) + { + $fn = T_("Quota") .".csv"; + + header("Content-Type: text/csv"); + header("Content-Disposition: attachment; filename=$fn"); + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past + header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); + Header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); + header("Pragma: no-cache"); // HTTP/1.0 + + foreach($csv as $cr) + { + for ($i = 0; $i < count($cr); $i++) + { + echo str_replace(","," ",$cr[$i]); + if ($i < (count($cr) - 1)) + echo ","; + } + echo "\n"; + } + die(); + } +} + +if (isset($_GET['delete'])) +{ + $qsqri = bigintval($_GET['qsqri']); + + if (isset($_GET['qsqrqi'])) + { + $qsqrqi = bigintval($_GET['qsqrqi']); + $sql = "DELETE FROM qsqr_question + WHERE qsqr_question_id = $qsqrqi"; + $db->Execute($sql); + + //Make sure to calculate on the spot + update_single_row_quota($qsqri); + } + if (isset($_GET['qsqrsi'])) + { + $qsqrsi = bigintval($_GET['qsqrsi']); + $sql = "DELETE FROM qsqr_sample + WHERE qsqr_sample_id = $qsqrsi"; + $db->Execute($sql); + + //Make sure to calculate on the spot + update_single_row_quota($qsqri); + } +} + + +if (isset($_POST['add_quota'])) { //need to add quota - $value = -1; - $comparison = -1; - $completions = -1; - $sgqa = -1; + $completions = intval($_POST['completions']); $autoprioritise = 0; - - if (isset($_GET['autoprioritise'])) $autoprioritise = 1; - - $priority = intval($_GET['priority']); + if (isset($_POST['autoprioritise'])) $autoprioritise = 1; + $priority = intval($_POST['priority']); $questionnaire_id = bigintval($_GET['questionnaire_id']); - $sample_import_id = bigintval($_GET['sample_import_id']); - if ($_GET['sgqa'] != -1) - { - if ($_GET['sgqa'] != -2) - { - $comparison = $db->quote($_GET['comparison']); - $value = $db->quote($_GET['value']); - $sgqa = $db->quote($_GET['sgqa']); - } - else - { - $sgqa = -2; - } - $completions = $db->quote($_GET['completions']); - } - $exclude_val = $db->quote($_GET['exclude_val']); - $exclude_var = $db->quote($_GET['exclude_var']); - $description = $db->quote($_GET['description']); + $sample_import_id = bigintval($_GET['sample_import_id']); + $description = $db->quote($_POST['description']); - $sql = "INSERT INTO questionnaire_sample_quota_row(questionnaire_id, sample_import_id, lime_sgqa,value,completions,comparison,exclude_var,exclude_val,description, priority, autoprioritise) - VALUES ($questionnaire_id, $sample_import_id, $sgqa, $value, $completions, $comparison, $exclude_var, $exclude_val, $description, $priority, $autoprioritise)"; + $sql = "INSERT INTO questionnaire_sample_quota_row(questionnaire_id, sample_import_id, completions, description, priority, autoprioritise) + VALUES ($questionnaire_id, $sample_import_id, $completions, $description, $priority, $autoprioritise)"; + + $db->Execute($sql); + + $qq = $db->Insert_ID(); + + //Make sure to calculate on the spot + update_single_row_quota($qq); +} + +if (isset($_POST['edit_quota'])) +{ + $completions = intval($_POST['completions']); + $autoprioritise = 0; + if (isset($_POST['autoprioritise'])) $autoprioritise = 1; + $priority = intval($_POST['priority']); + $description = $db->quote($_POST['description']); + $qsqri = bigintval($_POST['qsqri']); + + $sql = "UPDATE questionnaire_sample_quota_row + SET completions = $completions, + autoprioritise = $autoprioritise, + priority = $priority, + description = $description + WHERE questionnaire_sample_quota_row_id = $qsqri"; $db->Execute($sql); + $_GET['qsqri'] = $qsqri; + $_GET['edit'] = "edit"; + //Make sure to calculate on the spot - update_quotas($questionnaire_id); + update_single_row_quota($qsqri); } -if (isset($_GET['questionnaire_id']) && isset($_GET['questionnaire_sample_quota_row_id'])) + +$qsqri = false; +$qsqrid = false; +if (isset($_GET['qsqri']) & isset($_GET['edit'])) { - //need to remove quota + $qsqri = bigintval($_GET['qsqri']); - $questionnaire_id = bigintval($_GET['questionnaire_id']); - $questionnaire_sample_quota_row_id = bigintval($_GET['questionnaire_sample_quota_row_id']); + $sql = "SELECT questionnaire_id,sample_import_id,description,autoprioritise,priority,completions + FROM questionnaire_sample_quota_row + WHERE questionnaire_sample_quota_row_id = $qsqri"; - open_row_quota($questionnaire_sample_quota_row_id); + $rs = $db->GetRow($sql); + + $_GET['questionnaire_id'] = $rs['questionnaire_id']; + $_GET['sample_import_id'] = $rs['sample_import_id']; + $qsqrid = $rs['description']; + $qsqrich = ""; + if ($rs['autoprioritise'] == 1) + $qsqrich = "checked=\"checked\""; + $qsqric = $rs['completions']; + $qsqrip = $rs['priority']; + + if (isset($_POST['adds'])) + { + $comparison = $db->qstr($_POST['comparisons']); + $exvar = $db->qstr(substr($_POST['sample_var'],12,strpos($_POST['sample_var'],'&')-12)); + $exval = $db->qstr($_POST['exclude_val']); + //add ssample + $sql = "INSERT INTO qsqr_sample (questionnaire_sample_quota_row_id,exclude_var,exclude_val,comparison) + VALUES ($qsqri,$exvar,$exval,$comparison)"; + + $db->Execute($sql); + + //Make sure to calculate on the spot + update_single_row_quota($qsqri); + } + + if (isset($_POST['addq'])) + { + $comparison = $db->qstr($_POST['comparison']); + $value = $db->qstr($_POST['value']); + $sgqa = $db->qstr(substr($_POST['sgqa'],6,strpos($_POST['sgqa'],'&')-6)); + //add ssample + $sql = "INSERT INTO qsqr_question (questionnaire_sample_quota_row_id,lime_sgqa,value,comparison) + VALUES ($qsqri,$sgqa,$value,$comparison)"; + + $db->Execute($sql); + + //Make sure to calculate on the spot + update_single_row_quota($qsqri); + } } $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")); +xhtml_head(T_("Quota row management"),true,array("../css/table.css"),array("../js/window.js")); print "
" . T_("Currently no row quotas") . "
"; - } - else - { - foreach($r as $v) - { - if ($v['lime_sgqa'] == -1) - print "" . T_("No labels defined for this question") ."
"; - else - xhtml_table($rs,array('code','title'),array(T_("Code value"), T_("Description"))); - - - ?> - + " . TQ_("Delete") . "') as qdelete + FROM qsqr_question + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + + $rs = $db->GetAll($sql); + + if (empty($rs)) + { + print "" . T_("POSSIBLE ERROR: Row quota reached for this question") . " - " . $r['lime_sgqa']; + print "
" . T_("POSSIBLE ERROR: Row quota reached for this quota") . " - " . $r['description']; } } }