diff --git a/admin/quota.php b/admin/quota.php index b30fc85b..b4effab3 100755 --- a/admin/quota.php +++ b/admin/quota.php @@ -1,228 +1,260 @@ - - * @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'])) -{ - //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']); - - $sql = "INSERT INTO questionnaire_sample_quota(questionnaire_id, sample_import_id, lime_sgqa,value,completions,comparison) - VALUES ($questionnaire_id, $sample_import_id, $sgqa, $value, $completions, $comparison)"; - - $db->Execute($sql); - - //Make sure to calculate on the spot - update_quotas($questionnaire_id); -} - -if (isset($_GET['questionnaire_id']) && isset($_GET['questionnaire_sample_quota_id'])) -{ - //need to remove quota - - $questionnaire_id = bigintval($_GET['questionnaire_id']); - $questionnaire_sample_quota_id = bigintval($_GET['questionnaire_sample_quota_id']); - - $sql = "DELETE FROM questionnaire_sample_quota - WHERE questionnaire_sample_quota_id = '$questionnaire_sample_quota_id'"; - - $db->Execute($sql); - -} - -$questionnaire_id = false; -if (isset($_GET['questionnaire_id'])) $questionnaire_id = bigintval($_GET['questionnaire_id']); - -xhtml_head(T_("Quota 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 - WHERE enabled = 1"; -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 quotas (click to delete)") . "

"; - - $sql = "SELECT questionnaire_sample_quota_id,lime_sgqa,value,completions,quota_reached,lime_sid,comparison - FROM questionnaire_sample_quota 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 quotas") . "

"; - } - else - { - foreach($r as $v) - { - print "
" . T_("Stop calling this sample when:") . " {$v['lime_sgqa']} {$v['comparison']} {$v['value']} " . T_("for") . ": {$v['completions']} " . T_("completions") ." - "; - - if ($v['quota_reached'] == 1) - print T_("Quota reached"); - else - print T_("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 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( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) as value, CASE WHEN lq.parent_qid = 0 THEN lq.question ELSE CONCAT(lq2.question, ': ', lq.question) END as description, CASE WHEN CONCAT( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) = '$sgqa' THEN 'selected=\'selected\'' ELSE '' END AS selected - FROM `" . LIME_PREFIX . "questions` AS lq - LEFT JOIN `" . LIME_PREFIX . "questions` AS lq2 ON ( lq2.qid = lq.parent_qid ) - JOIN `" . LIME_PREFIX . "groups` as g ON (g.gid = lq.gid) - WHERE lq.sid = '$lime_sid' - ORDER BY g.group_order ASC, lq.question_order ASC"; - - - display_chooser($db->GetAll($sql),"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id"); - - if ($sgqa != false) - { - print "

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

"; - print "

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

"; - - $qid = explode("X", $sgqa); - $qid = $qid[2]; - - $sql = "SELECT l.code,l.answer as title - FROM `" . LIME_PREFIX . "answers` as l - WHERE l.qid = '$qid'"; - - $rs = $db->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"))); - - - ?> -
-

-
-
-
- - - - "/>

-
- + + * @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'])) +{ + //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']); + + $sql = "INSERT INTO questionnaire_sample_quota(questionnaire_id, sample_import_id, lime_sgqa,value,completions,comparison) + VALUES ($questionnaire_id, $sample_import_id, $sgqa, $value, $completions, $comparison)"; + + $db->Execute($sql); + + //Make sure to calculate on the spot + update_quotas($questionnaire_id); +} + +if (isset($_GET['questionnaire_id']) && isset($_GET['questionnaire_sample_quota_id'])) +{ + //need to remove quota + + $questionnaire_id = bigintval($_GET['questionnaire_id']); + $questionnaire_sample_quota_id = bigintval($_GET['questionnaire_sample_quota_id']); + + $sql = "DELETE FROM questionnaire_sample_quota + WHERE questionnaire_sample_quota_id = '$questionnaire_sample_quota_id'"; + + $db->Execute($sql); + +} + +$questionnaire_id = false; +if (isset($_GET['questionnaire_id'])) $questionnaire_id = bigintval($_GET['questionnaire_id']); + +xhtml_head(T_("Quota management"),true,array("../include/bootstrap-3.3.2/css/bootstrap.min.css","../css/custom.css"),array("../js/window.js")); +print "

" . T_("Questionnaire") . ": 

"; + +$sql = "SELECT questionnaire_id as value,description, CASE WHEN questionnaire_id = '$questionnaire_id' THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM questionnaire + WHERE enabled = 1"; +display_chooser($db->GetAll($sql),"questionnaire","questionnaire_id", true,false,true,true,false,true,"form-inline pull-left "); + +if ($questionnaire_id != false) +{ + $sample_import_id = false; + if (isset($_GET['sample_import_id'])) $sample_import_id = bigintval($_GET['sample_import_id']); + + + + $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"; + $s = $db->GetAll($sql); + if (!empty($s)){ + + print "

   " . T_("Sample") . ":  

"; + display_chooser($db->GetAll($sql),"sample","sample_import_id",true,"questionnaire_id=$questionnaire_id",true,true,false,true,"pull-left"); + + } else { + print "
" . T_("No samples assigned to this questionnaire.") . "
"; + + } + + print "
"; + + if ($sample_import_id != false) + { + print "

" . T_("Current quotas") . ":

";//(click to delete) + + $sql = "SELECT questionnaire_sample_quota_id,lime_sgqa,value,completions,quota_reached,lime_sid,comparison + FROM questionnaire_sample_quota 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 quotas") . "

"; + } + else + { + foreach($r as $v) + { + print "
" . T_("Stop calling this sample when:") . " {$v['lime_sgqa']} {$v['comparison']} {$v['value']} " . T_("for") . ": {$v['completions']} " . T_("completions") ." - "; + + if ($v['quota_reached'] == 1) + print T_("Quota reached"); + else + print T_("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 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( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) as value, CASE WHEN lq.parent_qid = 0 THEN lq.question ELSE CONCAT(lq2.question, ': ', lq.question) END as description, CASE WHEN CONCAT( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) = '$sgqa' THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM `" . LIME_PREFIX . "questions` AS lq + LEFT JOIN `" . LIME_PREFIX . "questions` AS lq2 ON ( lq2.qid = lq.parent_qid ) + JOIN `" . LIME_PREFIX . "groups` as g ON (g.gid = lq.gid) + WHERE lq.sid = '$lime_sid' + ORDER BY lq.parent_qid ASC, lq.question_order ASC";//, lq.parent_qid ASC lq.qid ASC, + + + display_chooser($db->GetAll($sql),"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id",true,true,false,true,"form-group"); + + print "
"; + + if ($sgqa != false) + { + + print "

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

"; + + ?> +
+ +

+

+ +

+

+ +

+

+ + + + + +

" class="btn btn-primary fa"/>

+
+ "; + + print "

" . T_("Code values for this question") . ":

"; + + $qid = explode("X", $sgqa); + $qid = $qid[2]; + + $sql = "SELECT CONCAT(' ', l.code , '')as code,l.answer as title + FROM `" . LIME_PREFIX . "answers` as l + WHERE l.qid = '$qid'"; + + $rs = $db->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"))); + + + print "
"; + + } + } +} + +xhtml_foot(); + +?> \ No newline at end of file diff --git a/admin/quotareport.php b/admin/quotareport.php index 3004ccc3..2ffc876e 100644 --- a/admin/quotareport.php +++ b/admin/quotareport.php @@ -1,293 +1,298 @@ - - * @copyright Australian Consortium for Social and Political Research Incorporated (ACSPRI) 2009 - * @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"); - -/** - * Operator functions - */ -include("../functions/functions.operator.php"); - -/** - * Limesurvey functions - */ -include("../functions/functions.limesurvey.php"); - - -if (isset($_POST['submit'])) -{ - $questionnaire_id = bigintval($_POST['questionnaire_id']); - - $db->StartTrans(); - - $sql = "UPDATE questionnaire_sample_quota_row - SET autoprioritise = 0 - WHERE questionnaire_id = '$questionnaire_id'"; - - $db->Execute($sql); - - foreach($_POST as $key => $val) - { - $qsqr = bigintval(substr($key,1)); - if (substr($key,0,1) == 'a') - { - $sql = "UPDATE questionnaire_sample_quota_row - SET autoprioritise = 1 - WHERE questionnaire_sample_quota_row_id = $qsqr"; - - $db->Execute($sql); - } - else if (substr($key,0,1) == 'p') - { - $val = intval($val); - - $sql = "UPDATE questionnaire_sample_quota_row - SET priority = '$val' - WHERE questionnaire_sample_quota_row_id = $qsqr"; - - $db->Execute($sql); - } - } - update_quota_priorities($questionnaire_id); - - $db->CompleteTrans(); -} - -xhtml_head(T_("Quota report"),true,array("../css/table.css"),array("../js/window.js")); - -print "

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

"; -$questionnaire_id = false; -if (isset($_GET['questionnaire_id'])) $questionnaire_id = bigintval($_GET['questionnaire_id']); -display_questionnaire_chooser($questionnaire_id); - -if ($questionnaire_id) -{ - print "

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

"; - $sample_import_id = false; - if (isset($_GET['sample_import_id'])) $sample_import_id = bigintval($_GET['sample_import_id']); - display_sample_chooser($questionnaire_id,$sample_import_id); - - if ($sample_import_id) - { - if (isset($_GET['rowquota'])) - { - $qsq = bigintval($_GET['rowquota']); - $qr = 0; - if (isset($_GET['close'])) $qr = 1; - $sql = "UPDATE questionnaire_sample_quota_row - SET quota_reached = $qr - WHERE questionnaire_sample_quota_row_id = '$qsq'"; - $db->Execute($sql); - if ($qr == 1) - close_row_quota($qsq); - else - open_row_quota($qsq,false); - } - - //Display report of quotas - $report = array(); - - //Rows to display: Strata Status Quota Sample Sample Used Sample Remaining Completes % Complete - - // Firstly, for the entire sample - - //We need to calc Sample size, Sample drawn, Sample remain, Completions, %complete - $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) - 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)"; - - $rs = $db->GetAll($sql); - - //type == 1 is drawn from sample, type == 0 is remains in sample - $drawn = 0; - $remain = 0; - - foreach ($rs as $r) - { - if ($r['type'] == 1) $drawn = $r['count']; - if ($r['type'] == 0) $remain = $r['count']; - } - - $sql = "SELECT count(*) as count - FROM `case` as c, sample as s - WHERE c.current_outcome_id = 10 - AND s.import_id = '$sample_import_id' - AND s.sample_id = c.sample_id - AND c.questionnaire_id = '$questionnaire_id'"; - - $rs = $db->GetRow($sql); - - $completions = $rs['count']; - - $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' - AND qsq.sample_import_id = '$sample_import_id' - AND q.questionnaire_id = '$questionnaire_id'"; - - $r = $db->GetAll($sql); - - foreach ($r as $v) - { - $completions = $v['current_completions']; - $priority = $v['priority']; - $autoprioritise = $v['autoprioritise']; - $checked = ""; - if ($autoprioritise) $checked = "checked='checked'"; - $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)); - } - - //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) - WHERE s.import_id = '$sample_import_id' - GROUP BY (c.sample_id is not null)"; - - $rs = $db->GetAll($sql); - //type == 1 is drawn from sample, type == 0 is remains in sample - $drawn = 0; - $remain = 0; - - foreach ($rs as $r) - { - if ($r['type'] == 1) $drawn = $r['count']; - 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 ($v['quota_reached'] == 1) - $status = "" . T_("closed") . ""; - else - $status = "" . T_("open") . ""; - } - else - { - if ($v['quota_reached'] == 1) - $status = T_("closed"); - else - $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" => ""); - } - - //c. (Questionnaire quota) Monitor outcomes of questions in completed questionnaires, and abort interview when completion limit is reached - $sql = "SELECT * - FROM " . LIME_PREFIX . "quota as qu, questionnaire as q - WHERE qu.sid = q.lime_sid - AND qu.active = 1 - AND q.questionnaire_id = '$questionnaire_id'"; - - $rs = $db->GetAll($sql); - - //for each limesurvey quota - foreach($rs as $r) - { - //limesurvey quotas for this question - $quotas = (get_limesurvey_quota_info($r['id'])); - $sqlq = array(); - - foreach ($quotas as $q) - $sqlq[] = "s." . $q['fieldname'] . " = '" . $q['value'] . "'"; - - $sql = "SELECT COUNT(id) as count - FROM ".LIME_PREFIX."survey_{$r['sid']} as s - JOIN `case` as c ON (c.questionnaire_id = '$questionnaire_id') - JOIN `sample` as sam ON (c.sample_id = sam.sample_id AND sam.import_id = '$sample_import_id') - WHERE ".implode(' AND ',$sqlq)." "." - AND submitdate IS NOT NULL - AND s.token = c.token"; - - $rs = $db->GetRow($sql); - - $completions = $rs['count']; - $perc = ROUND(($completions / $r['qlimit']) * 100,2); - - $report[] = array("strata" => "" . $r['name'] . "", "quota" => $r['qlimit'], "completions" => $completions, "perc" => $perc); - } - - print "
"; - xhtml_table($report,array("strata","status","quota","sample","sampleused","sampleremain","completions","perc","priority","autoprioritise"),array(T_("Strata"),T_("Status"),T_("Quota"),T_("Sample"),T_("Sample Used"),T_("Sample Remaining"),T_("Completions"),T_("% Complete"),T_("Set priority"),T_("Auto prioritise")),"tclass",false,false); - print "

"; - } - -} - -xhtml_foot(); - - -?> + + * @copyright Australian Consortium for Social and Political Research Incorporated (ACSPRI) 2009 + * @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"); + +/** + * Operator functions + */ +include("../functions/functions.operator.php"); + +/** + * Limesurvey functions + */ +include("../functions/functions.limesurvey.php"); + + +if (isset($_POST['submit'])) +{ + $questionnaire_id = bigintval($_POST['questionnaire_id']); + + $db->StartTrans(); + + $sql = "UPDATE questionnaire_sample_quota_row + SET autoprioritise = 0 + WHERE questionnaire_id = '$questionnaire_id'"; + + $db->Execute($sql); + + foreach($_POST as $key => $val) + { + $qsqr = bigintval(substr($key,1)); + if (substr($key,0,1) == 'a') + { + $sql = "UPDATE questionnaire_sample_quota_row + SET autoprioritise = 1 + WHERE questionnaire_sample_quota_row_id = $qsqr"; + + $db->Execute($sql); + } + else if (substr($key,0,1) == 'p') + { + $val = intval($val); + + $sql = "UPDATE questionnaire_sample_quota_row + SET priority = '$val' + WHERE questionnaire_sample_quota_row_id = $qsqr"; + + $db->Execute($sql); + } + } + update_quota_priorities($questionnaire_id); + + $db->CompleteTrans(); +} + +xhtml_head(T_("Quota report"),true,array("../include/bootstrap-3.3.2/css/bootstrap.min.css","../css/custom.css"),array("../js/window.js")); + +print "

" . T_("Select a questionnaire") . ": 

"; + +$questionnaire_id = false; +if (isset($_GET['questionnaire_id'])) $questionnaire_id = bigintval($_GET['questionnaire_id']); +display_questionnaire_chooser($questionnaire_id,false,"form-inline form-group", "form-control"); + +if ($questionnaire_id) +{ + print "

" . T_("Select a sample") . ": 

"; + $sample_import_id = false; + if (isset($_GET['sample_import_id'])) $sample_import_id = bigintval($_GET['sample_import_id']); + display_sample_chooser($questionnaire_id,$sample_import_id,false,"form-inline form-group clearfix", "form-control"); + + if ($sample_import_id) + { + if (isset($_GET['rowquota'])) + { + $qsq = bigintval($_GET['rowquota']); + $qr = 0; + if (isset($_GET['close'])) $qr = 1; + $sql = "UPDATE questionnaire_sample_quota_row + SET quota_reached = $qr + WHERE questionnaire_sample_quota_row_id = '$qsq'"; + $db->Execute($sql); + if ($qr == 1) + close_row_quota($qsq); + else + open_row_quota($qsq,false); + } + + //Display report of quotas + $report = array(); + + //Rows to display: Strata Status Quota Sample Sample Used Sample Remaining Completes % Complete + + // Firstly, for the entire sample + + //We need to calc Sample size, Sample drawn, Sample remain, Completions, %complete + $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) + 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)"; + + $rs = $db->GetAll($sql); + + //type == 1 is drawn from sample, type == 0 is remains in sample + $drawn = 0; + $remain = 0; + + foreach ($rs as $r) + { + if ($r['type'] == 1) $drawn = $r['count']; + if ($r['type'] == 0) $remain = $r['count']; + } + + $sql = "SELECT count(*) as count + FROM `case` as c, sample as s + WHERE c.current_outcome_id = 10 + AND s.import_id = '$sample_import_id' + AND s.sample_id = c.sample_id + AND c.questionnaire_id = '$questionnaire_id'"; + + $rs = $db->GetRow($sql); + + $completions = $rs['count']; + + $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' + AND qsq.sample_import_id = '$sample_import_id' + AND q.questionnaire_id = '$questionnaire_id'"; + + $r = $db->GetAll($sql); + + foreach ($r as $v) + { + $completions = $v['current_completions']; + $priority = $v['priority']; + $autoprioritise = $v['autoprioritise']; + $checked = ""; + if ($autoprioritise) $checked = "checked='checked'"; + $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)); + } + + //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) + WHERE s.import_id = '$sample_import_id' + GROUP BY (c.sample_id is not null)"; + + $rs = $db->GetAll($sql); + //type == 1 is drawn from sample, type == 0 is remains in sample + $drawn = 0; + $remain = 0; + + foreach ($rs as $r) + { + if ($r['type'] == 1) $drawn = $r['count']; + 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 ($v['quota_reached'] == 1) + $status = "" . T_("closed") . ""; + else + $status = "" . T_("open") . ""; + } + else + { + if ($v['quota_reached'] == 1) + $status = T_("closed"); + else + $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" => ""); + } + + //c. (Questionnaire quota) Monitor outcomes of questions in completed questionnaires, and abort interview when completion limit is reached + $sql = "SELECT * + FROM " . LIME_PREFIX . "quota as qu, questionnaire as q + WHERE qu.sid = q.lime_sid + AND qu.active = 1 + AND q.questionnaire_id = '$questionnaire_id'"; + + $rs = $db->GetAll($sql); + + //for each limesurvey quota + foreach($rs as $r) + { + //limesurvey quotas for this question + $quotas = (get_limesurvey_quota_info($r['id'])); + $sqlq = array(); + + foreach ($quotas as $q) + $sqlq[] = "s." . $q['fieldname'] . " = '" . $q['value'] . "'"; + + $sql = "SELECT COUNT(id) as count + FROM ".LIME_PREFIX."survey_{$r['sid']} as s + JOIN `case` as c ON (c.questionnaire_id = '$questionnaire_id') + JOIN `sample` as sam ON (c.sample_id = sam.sample_id AND sam.import_id = '$sample_import_id') + WHERE ".implode(' AND ',$sqlq)." "." + AND submitdate IS NOT NULL + AND s.token = c.token"; + + $rs = $db->GetRow($sql); + + $completions = $rs['count']; + $perc = ROUND(($completions / $r['qlimit']) * 100,2); + + $report[] = array("strata" => "" . $r['name'] . "", "quota" => $r['qlimit'], "completions" => $completions, "perc" => $perc); + } + + print "
"; + xhtml_table($report,array("strata","status","quota","sample","sampleused","sampleremain","completions","perc","priority","autoprioritise"),array(T_("Strata"),T_("Status"),T_("Quota"),T_("Sample"),T_("Sample Used"),T_("Sample Remaining"),T_("Completions"),T_("% Complete"),T_("Set priority"),T_("Auto prioritise")),"tclass",false,false); + + if ($report[0]("strata") != T_("Total sample")) + print "
+ "; + + print "
"; + } + +} + +xhtml_foot(); + +?> \ No newline at end of file diff --git a/admin/quotarow.php b/admin/quotarow.php index b4f3fced..3dfce00f 100755 --- a/admin/quotarow.php +++ b/admin/quotarow.php @@ -1,344 +1,381 @@ - - * @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 - $value = -1; - $comparison = -1; - $completions = -1; - $sgqa = -1; - $autoprioritise = 0; - - if (isset($_GET['autoprioritise'])) $autoprioritise = 1; - - $priority = intval($_GET['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']); - - $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)"; - - $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']); - - open_row_quota($questionnaire_sample_quota_row_id); -} - -$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 - WHERE enabled = 1"; -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) - { - if (isset($_POST['copy_sample_import_id'])) - { - copy_row_quota($questionnaire_id,$sample_import_id,bigintval($_POST['copy_sample_import_id'])); - print "

" . T_("Copied quotas") . "

"; - } - - if (isset($_POST['copy_sample_import_id_with_adjustment'])) - { - copy_row_quota_with_adjusting($questionnaire_id,$sample_import_id,bigintval($_POST['copy_sample_import_id_with_adjustment'])); - print "

" . T_("Copied quotas") . "

"; - } - - 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,current_completions - 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) - { - if ($v['lime_sgqa'] == -1) - print "
" . T_("Replicate: Where") . " " . $v['exclude_var'] . " " . T_("like") . " " . $v['exclude_val'] . " - "; - else if ($v['lime_sgqa'] == -2) - print "
" . T_("Sample only. Stop calling where") . " " . $v['exclude_var'] . " " . T_("like") . " " . $v['exclude_val'] . " " . T_("rows from this sample when:") . " {$v['completions']} " . T_("completions") . " - "; - - else - 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 (Closed)"); - else - print T_("Row quota not yet reached (Open)"); - - if ($v['lime_sgqa'] != -1) - print " - " . T_("Current completions: ") . $v['current_completions'] . ":" . limesurvey_quota_completions($v['lime_sgqa'],$v['lime_sid'],$questionnaire_id,$sample_import_id,$v['value'],$v['comparison']); - - print "
"; - - } - - - $sql = "SELECT s.sample_import_id as value,s.description, '' 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 - AND s.sample_import_id != '$sample_import_id'"; - - $ss = $db->GetAll($sql); - - if (!empty($ss)) - { - print "

" . T_("Copy quotas for this sample to (No error/duplicate checking): "); - display_chooser($ss,"copy_sample_import_id","copy_sample_import_id",false,false,false,false); - print "

"; - - print "

" . T_("Copy quotas for this sample to (No error/duplicate checking) with adjusting: "); - display_chooser($ss,"copy_sample_import_id_with_adjustment","copy_sample_import_id_with_adjustment",false,false,false,false); - 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( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) as value, CASE WHEN lq.parent_qid = 0 THEN lq.question ELSE CONCAT(lq2.question, ': ', lq.question) END as description, CASE WHEN CONCAT( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) = '$sgqa' THEN 'selected=\'selected\'' ELSE '' END AS selected - FROM `" . LIME_PREFIX . "questions` AS lq - LEFT JOIN `" . LIME_PREFIX . "questions` AS lq2 ON ( lq2.qid = lq.parent_qid ) - JOIN `" . LIME_PREFIX . "groups` as g ON (g.gid = lq.gid) - WHERE lq.sid = '$lime_sid' - ORDER BY g.group_order ASC, lq.question_order ASC"; - - $rs = $db->GetAll($sql); - - $selected = ""; - if ($sgqa == -1) $selected = "selected='selected'"; - array_unshift($rs,array("value" => -1, "description" => T_("No question (Replicate)"), "selected" => $selected)); - - $selected = ""; - if ($sgqa == -2) $selected = "selected='selected'"; - array_unshift($rs,array("value" => -2, "description" => T_("Sample only quota"), "selected" => $selected)); - - display_chooser($rs,"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:") . "

"; - - $rs = ""; - - if ($sgqa != -2 && $sgqa != -1 && !empty($sgqa)) - { - $qid = explode("X", $sgqa); - $qid = $qid[2]; - - $sql = "SELECT l.code,l.answer as title - FROM `" . LIME_PREFIX . "answers` as l - WHERE l.qid = '$qid'"; - - $rs = $db->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(); - ?> -
- - - - - "/>

-
- + + * @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 + $value = -1; + $comparison = -1; + $completions = -1; + $sgqa = -1; + $autoprioritise = 0; + + if (isset($_GET['autoprioritise'])) $autoprioritise = 1; + + $priority = intval($_GET['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']); + + $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)"; + + $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']); + + open_row_quota($questionnaire_sample_quota_row_id); +} + +$questionnaire_id = false; +if (isset($_GET['questionnaire_id'])) $questionnaire_id = bigintval($_GET['questionnaire_id']); + +xhtml_head(T_("Quota row management"),true,array("../include/bootstrap-3.3.2/css/bootstrap.min.css","../css/custom.css"),array("../js/window.js")); +print "

" . T_("Questionnaire") . ": 

"; + +$sql = "SELECT questionnaire_id as value,description, CASE WHEN questionnaire_id = '$questionnaire_id' THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM questionnaire + WHERE enabled = 1"; +display_chooser($db->GetAll($sql),"questionnaire","questionnaire_id", true,false,true,true,false,true,"form-inline pull-left "); + + +if ($questionnaire_id != false) +{ + $sample_import_id = false; + if (isset($_GET['sample_import_id'])) $sample_import_id = bigintval($_GET['sample_import_id']); + + + + $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"; + $s = $db->GetAll($sql); + if (!empty($s)){ + print "

   " . T_("Sample") . ": 

"; + display_chooser($s,"sample","sample_import_id",true,"questionnaire_id=$questionnaire_id",true,true,false,true,"pull-left"); + } else { + print "
" . T_("No samples assigned to this questionnaire.") . "
"; + } + print "
"; + + if ($sample_import_id != false) + { + if (isset($_POST['copy_sample_import_id'])) + { + copy_row_quota($questionnaire_id,$sample_import_id,bigintval($_POST['copy_sample_import_id'])); + print "

" . T_("Copied quotas") . ":

"; + } + + if (isset($_POST['copy_sample_import_id_with_adjustment'])) + { + copy_row_quota_with_adjusting($questionnaire_id,$sample_import_id,bigintval($_POST['copy_sample_import_id_with_adjustment'])); + print "

" . T_("Copied quotas with adjustment") . ":

"; + } + + 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,current_completions + 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) + { + if ($v['lime_sgqa'] == -1) + print "
" . T_("Replicate: Where") . " " . $v['exclude_var'] . " " . T_("like") . " " . $v['exclude_val'] . " - "; + else if ($v['lime_sgqa'] == -2) + print "
" . T_("Sample only. Stop calling where") . " " . $v['exclude_var'] . " " . T_("like") . " " . $v['exclude_val'] . " " . T_("rows from this sample when:") . " {$v['completions']} " . T_("completions") . " - "; + + else + 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 (Closed)"); + else + print T_("Row quota not yet reached (Open)"); + + if ($v['lime_sgqa'] != -1) + print " - " . T_("Current completions: ") . $v['current_completions'] . ":" . limesurvey_quota_completions($v['lime_sgqa'],$v['lime_sid'],$questionnaire_id,$sample_import_id,$v['value'],$v['comparison']); + + print "
"; + + } + + $sql = "SELECT s.sample_import_id as value,s.description, '' 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 + AND s.sample_import_id != '$sample_import_id'"; + + $ss = $db->GetAll($sql); + + if (!empty($ss)) + { + print "
+

" . T_("Copy quotas for this sample to (No error/duplicate checking): "); + display_chooser($ss,"copy_sample_import_id","copy_sample_import_id",false,false,false,false); + print "

"; + + print "
+

" . T_("Copy quotas for this sample to (No error/duplicate checking) with adjusting: "); + display_chooser($ss,"copy_sample_import_id_with_adjustment","copy_sample_import_id_with_adjustment",false,false,false,false); + 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( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) as value, CASE WHEN lq.parent_qid = 0 THEN lq.question ELSE CONCAT(lq2.question, ': ', lq.question) END as description, CASE WHEN CONCAT( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) = '$sgqa' THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM `" . LIME_PREFIX . "questions` AS lq + LEFT JOIN `" . LIME_PREFIX . "questions` AS lq2 ON ( lq2.qid = lq.parent_qid ) + JOIN `" . LIME_PREFIX . "groups` as g ON (g.gid = lq.gid) + WHERE lq.sid = '$lime_sid' + ORDER BY lq.parent_qid ASC, lq.question_order ASC"; + + $rs = $db->GetAll($sql); + + $selected = ""; + if ($sgqa == -1) $selected = "selected='selected'"; + array_unshift($rs,array("value" => -1, "description" => T_("No question (Replicate)"), "selected" => $selected)); + + $selected = ""; + if ($sgqa == -2) $selected = "selected='selected'"; + array_unshift($rs,array("value" => -2, "description" => T_("Sample only quota"), "selected" => $selected)); + + display_chooser($rs,"sgqa","sgqa",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id"); + + print "
"; + + 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",true,true,false,true,"pull-left"); + + print "
"; + + if ($sample_var != false) + { + + print "

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

"; + + ?> +
+ +

+

+ +

+

+ +

+

+ + +

+

+ +

+

+ + + + + + +

+

+ + + + + + + +

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

+ + + + + + " class="btn btn-primary fa"/> +
+ "; + + print "

" . T_("Code values for this question") . ":

"; + + $rs = ""; + + if ($sgqa != -2 && $sgqa != -1 && !empty($sgqa)) + { + $qid = explode("X", $sgqa); + $qid = $qid[2]; + + $sql = "SELECT CONCAT(' ', l.code , '')as code, l.answer as title + FROM `" . LIME_PREFIX . "answers` as l + WHERE l.qid = '$qid'"; + + $rs = $db->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"))); + + print "
"; + } + } + } +} + +xhtml_foot(); + +?> \ No newline at end of file