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 0fcd1e42..d1fecc46 100644 --- a/admin/questionnairelist.php +++ b/admin/questionnairelist.php @@ -1,570 +1,954 @@ - - * @copyright Australian Consortium for Social and Political Research Incorporated (ACSPRI) 2011 - * @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 ("../config.inc.php"); - -/** - * Database file - */ -include ("../db.inc.php"); - -/** - * XHTML functions - */ -include ("../functions/functions.xhtml.php"); - -/** - * Input functions - */ -include("../functions/functions.input.php"); - -/** - * CKEditor - */ -include("../include/ckeditor/ckeditor.php"); - -global $db; - -$css = array( -"../include/bootstrap-3.3.2/css/bootstrap.min.css", -"../include/bootstrap-3.3.2/css/bootstrap-theme.min.css", -"../include/font-awesome-4.3.0/css/font-awesome.css", -"../include/bootstrap-toggle/css/bootstrap-toggle.min.css", -"../css/custom.css" - ); -$js_head = array( -"../js/jquery-2.1.3.min.js", -"../include/bootstrap-3.3.2/js/bootstrap.min.js" - ); -$js_foot = array( -"../js/new.js", -"../include/bootstrap-toggle/js/bootstrap-toggle.min.js", -"../js/bootstrap-confirmation.js", -"../js/custom.js" - ); - -if (isset($_POST['questionnaire_id']) && isset($_POST['submit'])) -{ - //Delete the questionnaire - - $questionnaire_id = intval($_POST['questionnaire_id']); - - $db->StartTrans(); - - $sql = "DELETE FROM `appointment` - WHERE case_id IN - (SELECT case_id - FROM `case` - WHERE questionnaire_id = $questionnaire_id)"; - - $db->Execute($sql); - - $sql = "DELETE FROM `call` - WHERE case_id IN - (SELECT case_id - FROM `case` - WHERE questionnaire_id = $questionnaire_id)"; - - $db->Execute($sql); - - - $sql = "DELETE FROM `call_attempt` - WHERE case_id IN - (SELECT case_id - FROM `case` - WHERE questionnaire_id = $questionnaire_id)"; - - $db->Execute($sql); - - - $sql = "DELETE FROM `case_availability` - WHERE case_id IN - (SELECT case_id - FROM `case` - WHERE questionnaire_id = $questionnaire_id)"; - - $db->Execute($sql); - - $sql = "DELETE FROM `case_note` - WHERE case_id IN - (SELECT case_id - FROM `case` - WHERE questionnaire_id = $questionnaire_id)"; - - $db->Execute($sql); - - $sql = "DELETE FROM `contact_phone` - WHERE case_id IN - (SELECT case_id - FROM `case` - WHERE questionnaire_id = $questionnaire_id)"; - - $db->Execute($sql); - - $sql = "DELETE FROM `respondent` - WHERE case_id IN - (SELECT case_id - FROM `case` - WHERE questionnaire_id = $questionnaire_id)"; - - $db->Execute($sql); - - $sql = "DELETE FROM `client_questionnaire` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `operator_questionnaire` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `questionnaire_availability` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `questionnaire_prefill` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `questionnaire_sample` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `questionnaire_sample_exclude_priority` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `questionnaire_sample_quota` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `questionnaire_sample_quota_row` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `questionnaire_sample_quota_row_exclude` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `shift_report` - WHERE shift_id IN - (SELECT shift_id - FROM `shift` - WHERE questionnaire_id = $questionnaire_id)"; - - $db->Execute($sql); - - $sql = "DELETE FROM `shift` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `case` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $sql = "DELETE FROM `questionnaire` - WHERE questionnaire_id = $questionnaire_id"; - - $db->Execute($sql); - - $db->CompleteTrans(); -} - -if (isset($_GET['disable'])) -{ - $questionnaire_id = intval($_GET['disable']); - - $sql = "UPDATE questionnaire - SET enabled = 0 - WHERE questionnaire_id = '$questionnaire_id'"; - - $db->Execute($sql); -} - -if (isset($_GET['enable'])) -{ - $questionnaire_id = intval($_GET['enable']); - - $sql = "UPDATE questionnaire - SET enabled = 1 - WHERE questionnaire_id = '$questionnaire_id'"; - - $db->Execute($sql); -} - -if (isset($_POST['update']) && isset($_GET['modify'])) -{ - $questionnaire_id = intval($_GET['modify']); - - $ras =0; - $rws = 0; - $rs = 0; - $respsc = 0; - $referral = 0; - if (isset($_POST['ras'])) $ras = 1; - if (isset($_POST['rws'])) $rws = 1; - if (isset($_POST['respsc'])) $respsc = 1; - if (isset($_POST['referral'])) $referral = 1; - - $name = $db->qstr(html_entity_decode($_POST['description'],ENT_QUOTES,'UTF-8')); - if (isset($_POST['rs_intro'])) - { - $rs = 1; - $rs_intro = $db->qstr(html_entity_decode($_POST['rs_intro'],ENT_QUOTES,'UTF-8')); - $rs_project_intro = $db->qstr(html_entity_decode($_POST['rs_project_intro'],ENT_QUOTES,'UTF-8')); - $rs_callback = $db->qstr(html_entity_decode($_POST['rs_callback'],ENT_QUOTES,'UTF-8')); - $rs_answeringmachine = $db->qstr(html_entity_decode($_POST['rs_answeringmachine'],ENT_QUOTES,'UTF-8')); - } - $info = $db->qstr(html_entity_decode($_POST['info'],ENT_QUOTES,'UTF-8')); - $rs_project_end = $db->qstr(html_entity_decode($_POST['rs_project_end'],ENT_QUOTES,'UTF-8')); - - $sql = "UPDATE questionnaire - SET description = $name, info = $info, rs_project_end = $rs_project_end, restrict_appointments_shifts = '$ras', restrict_work_shifts = '$rws', self_complete = $respsc, referral = $referral - WHERE questionnaire_id = '$questionnaire_id'"; - - $db->Execute($sql); - - if ($rs) - { - $sql = "UPDATE questionnaire - SET rs_intro = $rs_intro, rs_project_intro = $rs_project_intro, rs_callback = $rs_callback, rs_answeringmachine = $rs_answeringmachine - WHERE questionnaire_id = '$questionnaire_id'"; - $db->Execute($sql); - } - - if ($respsc == 1) - { - $lime_mode = $db->qstr($_POST['lime_mode'],get_magic_quotes_gpc()); - $lime_template = $db->qstr($_POST['lime_template'],get_magic_quotes_gpc()); - $lime_endurl = $db->qstr($_POST['lime_endurl'],get_magic_quotes_gpc()); - - $sql = "UPDATE questionnaire - SET lime_mode = $lime_mode, lime_template = $lime_template, lime_endurl = $lime_endurl - WHERE questionnaire_id = $questionnaire_id"; - $db->Execute($sql); - } -} - -if (isset($_GET['modify'])) -{ - $questionnaire_id = intval($_GET['modify']); - - $sql = "SELECT * - FROM questionnaire - WHERE questionnaire_id = $questionnaire_id"; - $rs = $db->GetRow($sql); - - $referral = $testing = $rws = $ras = $rsc = "checked=\"checked\""; - $rscd = ""; - - $aio = $qbq = $gat = ""; - if ($rs['lime_mode'] == "survey") $aio = "selected=\"selected\""; - if ($rs['lime_mode'] == "question") $qbq = "selected=\"selected\""; - if ($rs['lime_mode'] == "group") $gat = "selected=\"selected\""; - - if ($rs['restrict_appointments_shifts'] != 1) $ras = ""; - if ($rs['restrict_work_shifts'] != 1) $rws = ""; - if ($rs['testing'] != 1) $testing = ""; - if ($rs['referral'] != 1) $referral = ""; - if ($rs['self_complete'] == 0) - { - $rsc = ""; - $rscd = "style='display:none;'"; - } - - xhtml_head(T_("Modify Questionnaire "),true,$css,$js_head, false, false, false, "  " . "$rs[description]" . ""); - - $CKEditor = new CKEditor(); - $CKEditor->basePath = "../include/ckeditor/"; - - $ckeditorConfig = array("toolbar" => array(array("tokens","-","Source"), - array("Cut","Copy","Paste","PasteText","PasteFromWord","-","Print","SpellChecker"), - array("Undo","Redo","-","Find","Replace","-","SelectAll","RemoveFormat"), - array('Link','Unlink','Anchor'), - array('Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'), - array('About'), - "/", - array("Bold","Italic","Underline","Strike","-","Subscript","Superscript"), - array("NumberedList","BulletedList","-","Outdent","Indent","Blockquote"), - array('JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'), - array('BidiLtr', 'BidiRtl'), - array('Styles','Format','Font','FontSize'), - array('TextColor','BGColor')), - "extraPlugins" => "tokens"); -?> -
-
-
-
 " . T_("Edit instrument in Limesurvey") . " "; ?>
-
- -
-
- -
"/>
-
-
- -
data-toggle="toggle" data-on="" data-off="" data-width="80"/>
-
-
-
data-toggle="toggle" data-on="" data-off="" data-width="80"/>
-
-
- -
" data-off="" data-onstyle="danger" data-width="80"/>
-
-
- -
data-toggle="toggle" data-on="" data-off="" data-width="80"/>
-
-
- -
onchange="if(this.checked==true) {show(this,'limesc'); $('#url').attr('required','required');} else{ hide(this,'limesc'); $('#url').removeAttr('required');}" data-toggle="toggle" data-on="" data-off="" data-width="80"/>
-
-
> -
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- - -
-
- " style="margin-top: -5px;"> -

-
-
- editor("rs_intro",$rs['rs_intro'],$ckeditorConfig);?> -
-
- -
-
- " style="margin-top: -5px;"> -

-
-
- editor("rs_project_intro",$rs['rs_project_intro'],$ckeditorConfig);?> -
-
- -
-
- " style="margin-top: -5px;"> -

-
-
- editor("rs_callback",$rs['rs_callback'],$ckeditorConfig);?> -
-
- -
-
- " style="margin-top: -5px;"> -

-
-
- editor("rs_answeringmachine",$rs['rs_answeringmachine'],$ckeditorConfig);?> -
-
- - " . T_("Edit respondent selection instrument in Limesurvey") . ""; } -?> - -
-
- " style="margin-top: -5px;"> -

-
-
- editor("rs_project_end",$rs['rs_project_end'],$ckeditorConfig);?> -
-
- -
-
- " style="margin-top: -5px;"> -

-
-
- editor("info",$rs['info'],$ckeditorConfig);?> -
-
- - - "/> - -
-GetRow($sql); - - xhtml_head(T_("Delete Questionnaire"),true,$css,$js_head, false, false, false, " " . "$rs[description]" . ""); - - print "

" . T_("Any collected data and the limesurvey instrument will NOT be deleted") . "

"; - print "

" . T_("The questionnaire will be deleted from queXS including call history, cases, case notes, respondent details, appointments and the links between operators, clients and the questionnaire") . "

"; - print "

" . T_("Please confirm you wish to delete the questionnaire") . "

"; - - print "
"; - print "

   " . T_("Go back") . ""; - print "

"; - print "
"; -} -else -{ - xhtml_head(T_("Questionnaire management"),true,$css,$js_head, false, false, false, "Questionnaire list"); - echo "
-  " . T_("Go back") . " -  " . T_("Create a new questionnaire") . " -
"; - print "
"; // add timeslots, callattempts, quotas? - - $sql = "SELECT - CONCAT(' ',questionnaire_id,' ') as qid, - CONCAT('

',description,'

') as description, - CASE WHEN enabled = 0 THEN - CONCAT(' " . TQ_("Disabled") . " ') - ELSE - CONCAT(' " . TQ_("Enabled") . " ') - END as status, - CASE WHEN enabled = 0 THEN - CONCAT('  ') - ELSE - CONCAT('  ') - END as enabledisable, - CONCAT('') as modify, - CONCAT('') as inlime, - CASE WHEN enabled = 0 THEN - CONCAT('') - ELSE - CONCAT('') - END as shifts, - CASE WHEN enabled = 0 THEN - CONCAT('') - ELSE - CONCAT('') - END as prefill, - CASE WHEN enabled = 1 THEN - CONCAT('') - ELSE - CONCAT('') - END as deletee, - CASE WHEN enabled = 0 THEN - CONCAT('') - ELSE - CONCAT('') - END as outcomes, - CONCAT('') as calls, - CASE WHEN enabled = 0 THEN - CONCAT('') - ELSE - CONCAT('') - END as dataout, - CASE WHEN enabled = 0 THEN - CONCAT('') - ELSE - CONCAT('') - END as assample, - CASE WHEN enabled = 0 THEN - CONCAT('') - ELSE - CONCAT('') - END as casestatus - FROM questionnaire"; - $rs = $db->GetAll($sql); - - $columns = array("qid","description","status","enabledisable","outcomes","calls","casestatus","shifts","assample","dataout","modify","inlime","prefill","deletee"); - xhtml_table($rs,$columns,false,"table-hover table-condensed "); - -print "
"; -} -xhtml_foot($js_foot); -?> - \ No newline at end of file +<<<<<<< TREE + + * @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_id']) && 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_var_id = $db->quote($_GET['exclude_var_id']); + $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_id,exclude_var,exclude_val,description, priority, autoprioritise) + VALUES ($questionnaire_id, $sample_import_id, $sgqa, $value, $completions, $comparison, $exclude_var_id, $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","../include/bootstrap-toggle/css/bootstrap-toggle.min.css","../css/custom.css"),array("../js/jquery-2.1.3.min.js","../include/bootstrap-toggle/js/bootstrap-toggle.min.js","../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 CASE WHEN lq2.question_order IS NULL THEN lq.question_order ELSE lq2.question_order + (lq.question_order / 1000) END 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_id = false; + if (isset($_GET['sample_var_id'])) + $sample_var_id = $_GET['sample_var_id']; + + print "

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

"; + + $sql = "SELECT sivr.var_id as value, sivr.var as description, + CASE WHEN sivr.var_id = '$sample_var_id' THEN 'selected=\'selected\'' ELSE '' END AS selected + FROM `sample_import_var_restrict` as sivr + WHERE sivr.sample_import_id = '$sample_import_id'"; + + $rsv = $db->GetAll($sql); + + $sample_var = $rsv[0]['description']; + + display_chooser($rsv,"sample_var_id","sample_var_id",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id&sgqa=$sgqa",true,true,false,true,"pull-left"); + + print "
"; + + if ($sample_var_id != false) + { + + print "

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

"; + + ?> +
+ +

+

+ +

+

+ +

+ " data-off="" data-offstyle="warning"/>

+ + +

+

+ +

+

+ + + + + + +

+

+ + + + + + + +

+ 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(); + +?>======= + + * @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($_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 + $completions = intval($_POST['completions']); + $autoprioritise = 0; + if (isset($_POST['autoprioritise'])) $autoprioritise = 1; + $priority = intval($_POST['priority']); + $questionnaire_id = bigintval($_GET['questionnaire_id']); + $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, 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_single_row_quota($qsqri); +} + + +$qsqri = false; +$qsqrid = false; +if (isset($_GET['qsqri']) & isset($_GET['edit'])) +{ + $qsqri = bigintval($_GET['qsqri']); + + $sql = "SELECT questionnaire_id,sample_import_id,description,autoprioritise,priority,completions + FROM questionnaire_sample_quota_row + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + $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,array("../css/table.css"),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['import_quota'])) + { + if (isset($_FILES['file']['tmp_name'])) + { + $handle = fopen($_FILES['file']['tmp_name'], "r"); + while (($data = fgetcsv($handle)) !== FALSE) + { + if (count($data) > 2) + { + //one quota record per row + $completions = intval($data[1]); + $autoprioritise = 0; + if ($data[2] != 0) $autoprioritise = 1; + $description = $db->quote($data[0]); + + $sql = "INSERT INTO questionnaire_sample_quota_row(questionnaire_id, sample_import_id, completions, description, priority, autoprioritise) + VALUES ($questionnaire_id, $sample_import_id, $completions, $description, 50, $autoprioritise)"; + + $db->Execute($sql); + + $qq = $db->Insert_ID(); + + if (count($data) > 5) //also some other records + { + //check if records exist (come in triplets + for ($i = 1; isset($data[$i * 3]) && !empty($data[$i*3]); $i++) + { + if (preg_match("/\d+X\d+X.+/",$data[$i*3])) + { + //is a limesurvey question + $comparison = $db->qstr($data[($i*3) + 1]); + $value = $db->qstr($data[($i*3) + 2]); + $sgqa = $db->qstr($data[$i*3]); + + $sql = "INSERT INTO qsqr_question (questionnaire_sample_quota_row_id,lime_sgqa,value,comparison) + VALUES ($qq,$sgqa,$value,$comparison)"; + + $db->Execute($sql); + } + else + { + //is a sample variable + $comparison = $db->qstr($data[($i*3) + 1]); + $value = $db->qstr($data[($i*3) + 2]); + $var = $db->qstr($data[$i*3]); + + $sql = "INSERT INTO qsqr_sample (questionnaire_sample_quota_row_id,exclude_var,exclude_val,comparison) + VALUES ($qq,$var,$value,$comparison)"; + + $db->Execute($sql); + } + } + } + + //Make sure to calculate on the spot + update_single_row_quota($qq); + } + } + fclose($handle); + } + } + + + + if ($qsqri != false) + { + print "

" . T_("Quota") . ": $qsqrid

"; + print "

" . T_("Go back") . "

"; + + ?> +
" method="post"> +

+
+
+ />
+
+ + "/>

+
+ " . TQ_("Delete") . "') as qdelete + FROM qsqr_question + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + + $rs = $db->GetAll($sql); + + if (empty($rs)) + { + print "

" . T_("All completed responses that match the sample criteria below will be counted towards the quota") . "

"; + } + else + { + print "

" . T_("Only completed responses that have answered the following will be counted") . "

"; + xhtml_table($rs,array('lime_sgqa','comparison','value','qdelete'),array(T_("Question"),T_("Comparison"),T_("Value"),T_("Delete"))); + } + + //add questionnaire references if any (refer to sample only or count completions based on responses to questions) + + $sql = "SELECT lime_sid + FROM questionnaire + WHERE questionnaire_id = $questionnaire_id"; + + $lime_sid = $db->GetOne($sql); + + $ssgqa = "''"; + if (isset($_GET['sgqa'])) + $ssgqa = $db->qstr($_GET['sgqa']); + + //select question + $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 $ssgqa LIKE CONCAT( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) 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); + + if (!empty($rs)) + { + print "
"; + print "

" . T_("Add restriction based on answered questions") . "

"; + print ""; + display_chooser($rs,"sgqa","sgqa",false,"edit=edit&qsqri=$qsqri",true,false); + ?> +

+
+ "/> +
+ " . TQ_("Delete") . "') as sdelete + FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + $rs = $db->GetAll($sql); + + if (empty($rs)) + { + print "

" . T_("All sample records will be excluded") . "

"; + } + else + { + print "

" . T_("Completed responses that have the following sample details will be counted towards the quota and excluded when the quota is reached") . "

"; + xhtml_table($rs,array('exclude_var','comparison','exclude_val','sdelete'),array(T_("Sample record"),T_("Comparison"),T_("Value"),T_("Delete"))); + } + + + $ssample_var = "''"; + if (isset($_GET['sample_var'])) + $ssample_var = $db->qstr($_GET['sample_var']); + + //add sample references (records from sample to exclude when quota reached) + $sql = "SELECT sv.var as value, sv.var as description, CASE WHEN sv.var LIKE $ssample_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"; + + $rs = $db->GetAll($sql); + + if (!empty($rs)) + { + if ($ssample_var == "''") + $ssample_var = "'" . $rs[0]['value']. "'"; + + print "

" . T_("Add restriction based on sample records") . "

"; + print "
"; + print ""; + display_chooser($rs,"sample_var","sample_var",false,"edit=edit&qsqri=$qsqri",true,false); + ?> +

+ + GetAll($sql),"exclude_val","exclude_val",false,false,false,false); + flush(); + ?> +
"/> +
+ " . T_("Current row quotas (click to edit)") . ""; + + + $sql = "SELECT questionnaire_sample_quota_row_id,qsq.description, + CONCAT('', qsq.description, '') as qedit, + CONCAT('') as qselect, + qsq.completions,qsq.quota_reached,qsq.current_completions, + CASE WHEN qsq.autoprioritise = 1 THEN '" . TQ_("Yes") . "' ELSE '" . TQ_("No") . "' END AS ap, qsq.priority, + CASE WHEN qsq.quota_reached = 1 THEN '" . TQ_("closed") . "' ELSE '" . TQ_("open") . "' END AS status + 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); + + print "
"; + if (empty($r)) + { + print "

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

"; + } + else + { + xhtml_table($r,array('qedit','completions','current_completions','status','priority','ap','qselect'),array(T_("Description"),T_("Quota"),T_("Completions"),T_("Status"),T_("Priority"),T_("Auto prioritise"),T_("Select"))); + print ""; + print ""; + + //select sample + + } + print "
"; + print "

" . T_("Add row quota") . "

"; + ?> +
" method="post"> +

+
+
+
+
+ "/>

+
+ " . T_("Import row quota") . ""; + ?> +
" method="post"> +

+

+

"/>

+
+ +>>>>>>> MERGE-SOURCE diff --git a/admin/quotareport.php b/admin/quotareport.php index 450d8bbb..036a9da0 100644 --- a/admin/quotareport.php +++ b/admin/quotareport.php @@ -1,302 +1,307 @@ - - * @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 - - - //We need to calc Sample size, Sample drawn, Sample remain, Completions, %complete - //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_id,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)); - } - - if($v['exclude_var_id'] > 0) $excl = "sv.var_id = '{$v['exclude_var_id']}'"; else $excl = "sv.var LIKE '{$v['exclude_var']}'"; - - //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.val LIKE '{$v['exclude_val']}' AND $excl ) - 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); - } - - - // At the end - > 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)); - - 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 (count($report) > 1) - print "
- "; - - print "
"; - } - -} - -xhtml_foot(); - -?> \ No newline at end of file + + * @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 + + + + //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 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'"; + + $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']; + + + $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) "; + + $sql2 = "SELECT exclude_val,exclude_var,exclude_var_id,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_id = '{$ev['exclude_var_id']}' 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)"; + + $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']) //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); + } + + + // At the end - > 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)); + + 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 (count($report) > 1) + print "
+ "; + + print "
"; + } + +} + +xhtml_foot(); + +?> diff --git a/admin/quotarow.php b/admin/quotarow.php index bdc83378..0b6393c0 100755 --- a/admin/quotarow.php +++ b/admin/quotarow.php @@ -1,386 +1,575 @@ - - * @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_id']) && 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_var_id = $db->quote($_GET['exclude_var_id']); - $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_id,exclude_var,exclude_val,description, priority, autoprioritise) - VALUES ($questionnaire_id, $sample_import_id, $sgqa, $value, $completions, $comparison, $exclude_var_id, $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","../include/bootstrap-toggle/css/bootstrap-toggle.min.css","../css/custom.css"),array("../js/jquery-2.1.3.min.js","../include/bootstrap-toggle/js/bootstrap-toggle.min.js","../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 CASE WHEN lq2.question_order IS NULL THEN lq.question_order ELSE lq2.question_order + (lq.question_order / 1000) END 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_id = false; - if (isset($_GET['sample_var_id'])) - $sample_var_id = $_GET['sample_var_id']; - - print "

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

"; - - $sql = "SELECT sivr.var_id as value, sivr.var as description, - CASE WHEN sivr.var_id = '$sample_var_id' THEN 'selected=\'selected\'' ELSE '' END AS selected - FROM `sample_import_var_restrict` as sivr - WHERE sivr.sample_import_id = '$sample_import_id'"; - - $rsv = $db->GetAll($sql); - - $sample_var = $rsv[0]['description']; - - display_chooser($rsv,"sample_var_id","sample_var_id",true,"questionnaire_id=$questionnaire_id&sample_import_id=$sample_import_id&sgqa=$sgqa",true,true,false,true,"pull-left"); - - print "
"; - - if ($sample_var_id != false) - { - - print "

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

"; - - ?> -
- -

-

- -

-

- -

- " data-off="" data-offstyle="warning"/>

- - -

-

- -

-

- - - - - - -

-

- - - - - - - -

- 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 + + * @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($_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'])) +/* 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_id']) && isset($_GET['exclude_var']) && isset($_GET['exclude_val'])) */ +{ + //need to add quota + $completions = intval($_POST['completions']); + $autoprioritise = 0; + if (isset($_POST['autoprioritise'])) $autoprioritise = 1; + $priority = intval($_POST['priority']); + $questionnaire_id = bigintval($_GET['questionnaire_id']); + $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, 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_single_row_quota($qsqri); +} + + +$qsqri = false; +$qsqrid = false; +if (isset($_GET['qsqri']) & isset($_GET['edit'])) +{ + $qsqri = bigintval($_GET['qsqri']); + + $sql = "SELECT questionnaire_id,sample_import_id,description,autoprioritise,priority,completions + FROM questionnaire_sample_quota_row + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + $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,array("../include/bootstrap-3.3.2/css/bootstrap.min.css","../include/bootstrap-toggle/css/bootstrap-toggle.min.css","../css/custom.css"),array("../js/jquery-2.1.3.min.js","../include/bootstrap-toggle/js/bootstrap-toggle.min.js","../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']); + + 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"; + $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['import_quota'])) + { + if (isset($_FILES['file']['tmp_name'])) + { + $handle = fopen($_FILES['file']['tmp_name'], "r"); + while (($data = fgetcsv($handle)) !== FALSE) + { + if (count($data) > 2) + { + //one quota record per row + $completions = intval($data[1]); + $autoprioritise = 0; + if ($data[2] != 0) $autoprioritise = 1; + $description = $db->quote($data[0]); + + $sql = "INSERT INTO questionnaire_sample_quota_row(questionnaire_id, sample_import_id, completions, description, priority, autoprioritise) + VALUES ($questionnaire_id, $sample_import_id, $completions, $description, 50, $autoprioritise)"; + + $db->Execute($sql); + + $qq = $db->Insert_ID(); + + if (count($data) > 5) //also some other records + { + //check if records exist (come in triplets + for ($i = 1; isset($data[$i * 3]) && !empty($data[$i*3]); $i++) + { + if (preg_match("/\d+X\d+X.+/",$data[$i*3])) + { + //is a limesurvey question + $comparison = $db->qstr($data[($i*3) + 1]); + $value = $db->qstr($data[($i*3) + 2]); + $sgqa = $db->qstr($data[$i*3]); + + $sql = "INSERT INTO qsqr_question (questionnaire_sample_quota_row_id,lime_sgqa,value,comparison) + VALUES ($qq,$sgqa,$value,$comparison)"; + + $db->Execute($sql); + } + else + { + //is a sample variable + $comparison = $db->qstr($data[($i*3) + 1]); + $value = $db->qstr($data[($i*3) + 2]); + $var = $db->qstr($data[$i*3]); + + $sql = "INSERT INTO qsqr_sample (questionnaire_sample_quota_row_id,exclude_var,exclude_val,comparison) + VALUES ($qq,$var,$value,$comparison)"; + + $db->Execute($sql); + } + } + } + + //Make sure to calculate on the spot + update_single_row_quota($qq); + } + } + fclose($handle); + } + } + + + + if ($qsqri != false) + { + print "

" . T_("Quota") . ": $qsqrid

"; + print "

" . T_("Go back") . "

"; + + ?> +
" method="post"> +

+
+
+ />
+
+ + "/>

+
+ " . TQ_("Delete") . "') as qdelete + FROM qsqr_question + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + + $rs = $db->GetAll($sql); + + if (empty($rs)) + { + print "

" . T_("All completed responses that match the sample criteria below will be counted towards the quota") . "

"; + } + else + { + print "

" . T_("Only completed responses that have answered the following will be counted") . "

"; + xhtml_table($rs,array('lime_sgqa','comparison','value','qdelete'),array(T_("Question"),T_("Comparison"),T_("Value"),T_("Delete"))); + } + + //add questionnaire references if any (refer to sample only or count completions based on responses to questions) + + $sql = "SELECT lime_sid + FROM questionnaire + WHERE questionnaire_id = $questionnaire_id"; + + $lime_sid = $db->GetOne($sql); + + $ssgqa = "''"; + if (isset($_GET['sgqa'])) + $ssgqa = $db->qstr($_GET['sgqa']); + + //select question + $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 $ssgqa LIKE CONCAT( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END) 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); + + if (!empty($rs)) + { + print "
"; + print "

" . T_("Add restriction based on answered questions") . "

"; + print ""; + display_chooser($rs,"sgqa","sgqa",false,"edit=edit&qsqri=$qsqri",true,false); + ?> +

+
+ "/> +
+ " . TQ_("Delete") . "') as sdelete + FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + $rs = $db->GetAll($sql); + + if (empty($rs)) + { + print "

" . T_("All sample records will be excluded") . "

"; + } + else + { + print "

" . T_("Completed responses that have the following sample details will be counted towards the quota and excluded when the quota is reached") . "

"; + xhtml_table($rs,array('exclude_var','comparison','exclude_val','sdelete'),array(T_("Sample record"),T_("Comparison"),T_("Value"),T_("Delete"))); + } + + + $ssample_var = "''"; + if (isset($_GET['sample_var'])) + $ssample_var = $db->qstr($_GET['sample_var']); + + //add sample references (records from sample to exclude when quota reached) + $sql = "SELECT sv.var as value, sv.var as description, CASE WHEN sv.var LIKE $ssample_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"; + + $rs = $db->GetAll($sql); + + if (!empty($rs)) + { + if ($ssample_var == "''") + $ssample_var = "'" . $rs[0]['value']. "'"; + + print "

" . T_("Add restriction based on sample records") . "

"; + print "
"; + print ""; + display_chooser($rs,"sample_var","sample_var",false,"edit=edit&qsqri=$qsqri",true,false); + ?> +

+ + GetAll($sql),"exclude_val","exclude_val",false,false,false,false); + flush(); + ?> +
"/> +
+ " . T_("Current row quotas (click to edit)") . ""; + + + $sql = "SELECT questionnaire_sample_quota_row_id,qsq.description, + CONCAT('', qsq.description, '') as qedit, + CONCAT('') as qselect, + qsq.completions,qsq.quota_reached,qsq.current_completions, + CASE WHEN qsq.autoprioritise = 1 THEN '" . TQ_("Yes") . "' ELSE '" . TQ_("No") . "' END AS ap, qsq.priority, + CASE WHEN qsq.quota_reached = 1 THEN '" . TQ_("closed") . "' ELSE '" . TQ_("open") . "' END AS status + 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); + + print "
"; + if (empty($r)) + { + print "

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

"; + } + else + { + xhtml_table($r,array('qedit','completions','current_completions','status','priority','ap','qselect'),array(T_("Description"),T_("Quota"),T_("Completions"),T_("Status"),T_("Priority"),T_("Auto prioritise"),T_("Select"))); + print ""; + print ""; + + //select sample + + } + print "
"; + print "

" . T_("Add row quota") . "

"; + ?> +
" method="post"> +

+
+
+
+
+ "/>

+
+ " . T_("Import row quota") . ""; + ?> +
" method="post"> +

+

+

"/>

+
+ diff --git a/admin/samplecallattempts.php b/admin/samplecallattempts.php index 2928e425..0757ca08 100644 --- a/admin/samplecallattempts.php +++ b/admin/samplecallattempts.php @@ -1,216 +1,230 @@ - - * @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"); - -/** - * Generate the sample call attempt report - * - * @param mixed $questionnaire_id The quesitonnaire, if specified - * @param string $sample_id The sample, if speified - * @param mixed $qsqri THe questionnaire sample quota row id, if specified - * - * @return false if empty otherwise true if table drawn - * @author Adam Zammit - * @since 2012-10-02 - */ -function sample_call_attempt_report($questionnaire_id = false, $sample_id = false, $qsqri = false) -{ - global $db; - - $q = ""; - if ($questionnaire_id !== false && $questionnaire_id != -1) - $q = "AND c.questionnaire_id = $questionnaire_id"; - - $s = ""; - if ($sample_id !== false) - $s = "JOIN sample as s ON (s.sample_id = c.sample_id AND s.import_id = '$sample_id')"; - - $qs = ""; - if ($qsqri !== false) - $qs = "JOIN questionnaire_sample_quota_row as q ON (q.questionnaire_sample_quota_row_id = '$qsqri') - JOIN sample_var ON (sample_var.sample_id = c.sample_id AND sample_var.var_id = q.exclude_var_id AND sample_var.val LIKE q.exclude_val)"; - - $sql = "SELECT ca1 AS callattempts, COUNT( ca1 ) AS sample - FROM ( SELECT count( ca.call_attempt_id ) AS ca1 - FROM call_attempt as ca - JOIN `case` as c ON (c.case_id = ca.case_id $q) - $s - $qs - GROUP BY ca.case_id) AS t1 - GROUP BY ca1"; - - $overall = $db->GetAll($sql); - - if (empty($overall)) - return false; - - - $sql = "SELECT outcome_id,description - FROM outcome"; - - $outcomes = $db->GetAssoc($sql); - - translate_array($outcomes,array("description")); - - $rep = array("callattempts","sample"); - $rept = array(T_("Call attempts made"),T_("Number of cases")); - $totals = array("sample"); - - $outcomesfilled = array(); - - foreach($outcomes as $key => $val) - { - $rep[] = $key; - $rept[] = $val; - $outcomesfilled[$key] = 0; - } - - //Add breakdown by call attempt - for ($i = 0; $i < count($overall); $i++) - { - //break down by outcome - $sql = "SELECT oi1, ca1 as callattempts, count(ca1) as sample - FROM ( SELECT count( ca.call_attempt_id ) AS ca1, ca.case_id, c.current_outcome_id as oi1 - FROM call_attempt AS ca - JOIN `case` AS c ON ( c.case_id = ca.case_id $q) - $s - $qs - GROUP BY ca.case_id - ) as t1 - GROUP BY ca1,oi1 - HAVING ca1 = {$overall[$i]['callattempts']}"; - - $byoutcome = $db->GetAssoc($sql); - - foreach($outcomes as $key => $val) - { - $sample = 0; - if (isset($byoutcome[$key])) - { - $outcomesfilled[$key] = 1; - $sample = $byoutcome[$key]['sample']; - } - - $overall[$i][$key] = $sample; - } - } - - //Remove columns that are empty - $count = 2; - foreach ($outcomesfilled as $key => $val) - { - if ($val == 0) - { - unset($rep[$count]); - unset($rept[$count]); - } - else - $totals[] = $key; - - $count++; - } - - xhtml_table($overall,$rep,$rept,"tclass",false,$totals); - print "
"; - return true; -} - - -xhtml_head(T_("Sample call attempt"),true,array("../include/bootstrap-3.3.2/css/bootstrap.min.css","../css/custom.css"),array("../js/window.js")); - -print "

" . T_("Please select a questionnaire") . " 

"; -$questionnaire_id = false; -if (isset($_GET['questionnaire_id'])) $questionnaire_id = intval($_GET['questionnaire_id']); -display_questionnaire_chooser($questionnaire_id,array(-1,T_("Overall")),"form-inline clearfix", "form-control"); - - -if ($questionnaire_id || $questionnaire_id == -1) -{ - if (sample_call_attempt_report($questionnaire_id,false,false)) - { - if ($questionnaire_id != -1) - { - print "

" . T_("Please 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 clearfix", "form-control"); - - if ($sample_import_id) - { - if (sample_call_attempt_report($questionnaire_id,$sample_import_id,false)) - { - $questionnaire_sample_quota_row_id = false; - if (isset($_GET['questionnaire_sample_quota_row_id'])) $questionnaire_sample_quota_row_id = bigintval($_GET['questionnaire_sample_quota_row_id']); - print "

" . T_("Please select a quota") . " 

"; - display_quota_chooser($questionnaire_id,$sample_import_id,$questionnaire_sample_quota_row_id,"form-inline clearfix", "form-control"); - - if ($questionnaire_sample_quota_row_id) - { - if (!sample_call_attempt_report($questionnaire_id,$sample_import_id,$questionnaire_sample_quota_row_id)) - print "

" . T_("No calls for this quota") . "

"; - - } - } - else - print "

" . T_("No calls for this sample") . "

"; - } - } - } - else - print "

" . T_("No calls for this questionnaire") . "

"; -} - -xhtml_foot("../js/custom.js"); - -?> + + * @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"); + +/** + * Generate the sample call attempt report + * + * @param mixed $questionnaire_id The quesitonnaire, if specified + * @param string $sample_id The sample, if speified + * @param mixed $qsqri THe questionnaire sample quota row id, if specified + * + * @return false if empty otherwise true if table drawn + * @author Adam Zammit + * @since 2012-10-02 + */ +function sample_call_attempt_report($questionnaire_id = false, $sample_id = false, $qsqri = false) +{ + global $db; + + $q = ""; + if ($questionnaire_id !== false && $questionnaire_id != -1) + $q = "AND c.questionnaire_id = $questionnaire_id"; + + $s = ""; + if ($sample_id !== false) + $s = "JOIN sample as s ON (s.sample_id = c.sample_id AND s.import_id = '$sample_id')"; + + $qs = ""; + + if ($qsqri !== false) + { + $sql2 = "SELECT exclude_val,exclude_var,exclude_var_id,comparison + FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + $rev = $db->GetAll($sql2); + + //reduce sample by every item in the qsqr_sample table + $x = 1; + foreach($rev as $ev) + { + $qs .= " JOIN sample_var_id as sv$x ON (sv$x.sample_id = c.sample_id AND sv$x.var_id = '{$ev['exclude_var_id']}' AND sv$x.val {$ev['comparison']} '{$ev['exclude_val']}') "; + $x++; + } + } + + $sql = "SELECT ca1 AS callattempts, COUNT( ca1 ) AS sample + FROM ( SELECT count( ca.call_attempt_id ) AS ca1 + FROM call_attempt as ca + JOIN `case` as c ON (c.case_id = ca.case_id $q) + $s + $qs + GROUP BY ca.case_id) AS t1 + GROUP BY ca1"; + + $overall = $db->GetAll($sql); + + if (empty($overall)) + return false; + + + $sql = "SELECT outcome_id,description + FROM outcome"; + + $outcomes = $db->GetAssoc($sql); + + translate_array($outcomes,array("description")); + + $rep = array("callattempts","sample"); + $rept = array(T_("Call attempts made"),T_("Number of cases")); + $totals = array("sample"); + + $outcomesfilled = array(); + + foreach($outcomes as $key => $val) + { + $rep[] = $key; + $rept[] = $val; + $outcomesfilled[$key] = 0; + } + + //Add breakdown by call attempt + for ($i = 0; $i < count($overall); $i++) + { + //break down by outcome + $sql = "SELECT oi1, ca1 as callattempts, count(ca1) as sample + FROM ( SELECT count( ca.call_attempt_id ) AS ca1, ca.case_id, c.current_outcome_id as oi1 + FROM call_attempt AS ca + JOIN `case` AS c ON ( c.case_id = ca.case_id $q) + $s + $qs + GROUP BY ca.case_id + ) as t1 + GROUP BY ca1,oi1 + HAVING ca1 = {$overall[$i]['callattempts']}"; + + $byoutcome = $db->GetAssoc($sql); + + foreach($outcomes as $key => $val) + { + $sample = 0; + if (isset($byoutcome[$key])) + { + $outcomesfilled[$key] = 1; + $sample = $byoutcome[$key]['sample']; + } + + $overall[$i][$key] = $sample; + } + } + + //Remove columns that are empty + $count = 2; + foreach ($outcomesfilled as $key => $val) + { + if ($val == 0) + { + unset($rep[$count]); + unset($rept[$count]); + } + else + $totals[] = $key; + + $count++; + } + + xhtml_table($overall,$rep,$rept,"tclass",false,$totals); + print "
"; + return true; +} + + +xhtml_head(T_("Sample call attempt"),true,array("../include/bootstrap-3.3.2/css/bootstrap.min.css","../css/custom.css"),array("../js/window.js")); + +print "

" . T_("Please select a questionnaire") . " 

"; +$questionnaire_id = false; +if (isset($_GET['questionnaire_id'])) $questionnaire_id = intval($_GET['questionnaire_id']); +display_questionnaire_chooser($questionnaire_id,array(-1,T_("Overall")),"form-inline clearfix", "form-control"); + + +if ($questionnaire_id || $questionnaire_id == -1) +{ + if (sample_call_attempt_report($questionnaire_id,false,false)) + { + if ($questionnaire_id != -1) + { + print "

" . T_("Please 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 clearfix", "form-control"); + + if ($sample_import_id) + { + if (sample_call_attempt_report($questionnaire_id,$sample_import_id,false)) + { + $questionnaire_sample_quota_row_id = false; + if (isset($_GET['questionnaire_sample_quota_row_id'])) $questionnaire_sample_quota_row_id = bigintval($_GET['questionnaire_sample_quota_row_id']); + print "

" . T_("Please select a quota") . " 

"; + display_quota_chooser($questionnaire_id,$sample_import_id,$questionnaire_sample_quota_row_id,"form-inline clearfix", "form-control"); + + if ($questionnaire_sample_quota_row_id) + { + if (!sample_call_attempt_report($questionnaire_id,$sample_import_id,$questionnaire_sample_quota_row_id)) + print "

" . T_("No calls for this quota") . "

"; + + } + } + else + print "

" . T_("No calls for this sample") . "

"; + } + } + } + else + print "

" . T_("No calls for this questionnaire") . "

"; +} + +xhtml_foot("../js/custom.js"); + +?> diff --git a/database/quexs.sql b/database/quexs.sql index f8ba9dba..b04bed29 100644 --- a/database/quexs.sql +++ b/database/quexs.sql @@ -1362,8 +1362,40 @@ CREATE TABLE `process_log` ( -- +-- Table structure for table `qsqr_question` +-- +-- + +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 ; + -- -------------------------------------------------------- +-- +-- Table structure for table `qsqr_sample` +-- + +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 ; + + -- -- Table structure for table `questionnaire` -- @@ -1513,12 +1545,7 @@ CREATE TABLE `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', `current_completions` int(11) NOT NULL default '0', `description` text collate utf8_unicode_ci NOT NULL, @@ -1526,8 +1553,7 @@ CREATE TABLE `questionnaire_sample_quota_row` ( `autoprioritise` tinyint(1) NOT NULL default '0' COMMENT 'Should this row have it''s priority automatically adjusted to 100 - (completions %)', PRIMARY KEY (`questionnaire_sample_quota_row_id`), KEY `questionnaire_id` (`questionnaire_id`), - KEY `sample_import_id` (`sample_import_id`), - KEY `exclude_var` (`exclude_var`) + KEY `sample_import_id` (`sample_import_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- diff --git a/functions/functions.operator.php b/functions/functions.operator.php index 41ba6996..0103b09a 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -1466,6 +1466,17 @@ function open_row_quota($questionnaire_sample_quota_row_id,$delete = true) WHERE questionnaire_sample_quota_row_id = '$questionnaire_sample_quota_row_id'"; $db->Execute($sql); + + $sql = "DELETE FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id = '$questionnaire_sample_quota_row_id'"; + + $db->Execute($sql); + + $sql = "DELETE FROM qsqr_question + WHERE questionnaire_sample_quota_row_id = '$questionnaire_sample_quota_row_id'"; + + $db->Execute($sql); + } $sql = "DELETE FROM questionnaire_sample_quota_row_exclude @@ -1506,16 +1517,28 @@ function close_row_quota($questionnaire_sample_quota_row_id,$update = true) $rs = $db->GetRow($sql); if (isset($coun['c']) && $coun['c'] == 0) - { - //store list of sample records to exclude - $sql = "INSERT INTO questionnaire_sample_quota_row_exclude (questionnaire_sample_quota_row_id,questionnaire_id,sample_id) - SELECT $questionnaire_sample_quota_row_id,qs.questionnaire_id,s.sample_id - FROM sample as s, sample_var as sv, questionnaire_sample_quota_row as qs - WHERE s.import_id = qs.sample_import_id - AND qs.questionnaire_sample_quota_row_id = $questionnaire_sample_quota_row_id - AND s.sample_id = sv.sample_id - AND sv.var_id = qs.exclude_var_id - AND qs.exclude_val LIKE sv.val"; + { + $sql2 = "SELECT exclude_val,exclude_var,exclude_var_id,comparison + FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id = $questionnaire_sample_quota_row_id"; + + $rev = $db->GetAll($sql2); + + //store list of sample records to exclude + $sql = "INSERT INTO questionnaire_sample_quota_row_exclude (questionnaire_sample_quota_row_id,questionnaire_id,sample_id) + SELECT $questionnaire_sample_quota_row_id,qs.questionnaire_id,s.sample_id + FROM sample as s "; + + //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_id = '{$ev['exclude_var_id']}' AND sv$x.val {$ev['comparison']} '{$ev['exclude_val']}') "; + $x++; + } + + $sql = "JOIN questionnaire_sample_quota_row as qs ON (qs.questionnaire_sample_quota_row_id = $questionnaire_sample_quota_row_id) + WHERE s.import_id = qs.sample_import_id"; $db->Execute($sql); @@ -1539,22 +1562,35 @@ function close_row_quota($questionnaire_sample_quota_row_id,$update = true) * @param int $questionnaire_id * @param int $sample_import_id * @param int $copy_sample_import_id The sample_import_id to copy to + * @param bool $blocking Block (copy quota)? */ -function copy_row_quota($questionnaire_id,$sample_import_id,$copy_sample_import_id) +function copy_row_quota($questionnaire_id,$sample_import_id,$copy_sample_import_id, $block = false) { global $db; $db->StartTrans(); - //Set quota_reached to 0 always + //Set quota_reached to 0 always if not blocking + $b = 0; + if ($block == true) + $b = "quota_reached"; - $sql = "INSERT INTO questionnaire_sample_quota_row (questionnaire_id,sample_import_id,lime_sgqa,value,comparison,completions,exclude_var_id,exclude_var,exclude_val,quota_reached,description) - SELECT questionnaire_id, $copy_sample_import_id, lime_sgqa,value,comparison,completions,exclude_var_id,exclude_var,exclude_val,0,description + $sql = "INSERT INTO questionnaire_sample_quota_row (questionnaire_id,sample_import_id,completions,quota_reached,description) + SELECT questionnaire_id, $copy_sample_import_id, completions,$b,description FROM questionnaire_sample_quota_row WHERE questionnaire_id = '$questionnaire_id' AND sample_import_id = '$sample_import_id'"; - $db->Execute($sql); + $db->Execute($sql); + + $nqsqr = $db->Insert_ID(); + + $sql = "INSERT INTO qsqr_sample (questionnaire_sample_quota_row_id,exclude_var_id,exclude_var,exclude_val,comparison,description) + SELECT $nqsqr, qs.exclude_var_id, qs.exclude_var, qs.exclude_val, qs.comparison, qs.description + FROM qsqr_sample as qs, questionnaire_sample_quota_row as q + WHERE qs.questionnaire_sample_quota_row_id = q.questionnaire_sample_quota_row_id + AND q.questionnaire_id = '$questionnaire_id' + AND q.sample_import_id = '$sample_import_id'"; update_quotas($questionnaire_id); @@ -1571,22 +1607,7 @@ function copy_row_quota($questionnaire_id,$sample_import_id,$copy_sample_import_ */ function copy_row_quota_with_blocking($questionnaire_id,$sample_import_id,$copy_sample_import_id) { - global $db; - - $db->StartTrans(); - - //Set quota_reached to 0 always - - $sql = "INSERT INTO questionnaire_sample_quota_row (questionnaire_id,sample_import_id,lime_sgqa,value,comparison,completions,exclude_var_id,exclude_var,exclude_val,quota_reached,description) - SELECT questionnaire_id, $copy_sample_import_id, lime_sgqa,value,comparison,completions, exclude_var_id,exclude_var,exclude_val, quota_reached,description - FROM questionnaire_sample_quota_row - WHERE questionnaire_id = '$questionnaire_id' - AND sample_import_id = '$sample_import_id'"; - - $db->Execute($sql); - - - update_quotas($questionnaire_id); + copy_row_quota($questionnaire_id,$sample_import_id,$copy_sample_import_id,true); $db->CompleteTrans(); } @@ -1605,6 +1626,7 @@ function copy_row_quota_with_adjusting($questionnaire_id,$sample_import_id,$copy // Copy quotas (defalt Quexs function) copy_row_quota_with_blocking($questionnaire_id,$sample_import_id,$copy_sample_import_id); + /* $db->StartTrans(); // Select quotas from the old sample rows and calculate @@ -1645,7 +1667,186 @@ function copy_row_quota_with_adjusting($questionnaire_id,$sample_import_id,$copy } } - $db->CompleteTrans(); + $db->CompleteTrans(); + */ +} + +/** + * Update a single row quota + * + * @param int $qsqri The quota row id + * @param int|bool $case_id The case id if known to limit the scope of the search + * @return bool If priorities need to be updated or not + */ +function update_single_row_quota($qsqri,$case_id = false) +{ + global $db; + + $sql = "SELECT q.lime_sid, qs.questionnaire_id, qs.sample_import_id, qs.completions, qs.autoprioritise + FROM questionnaire as q, questionnaire_sample_quota_row as qs + WHERE q.questionnaire_id = qs.questionnaire_id + AND qs.questionnaire_sample_quota_row_id = $qsqri"; + + $rs = $db->GetRow($sql); + + $lime_sid = $rs['lime_sid']; + $questionnaire_id = $rs['questionnaire_id']; + $sample_import_id = $rs['sample_import_id']; + $target_completions = $rs['completions']; + $autoprioritise = $rs['autoprioritise']; + + //all variables to exclude for this row quota + $sql2 = "SELECT exclude_val,exclude_var,exclude_var_id,comparison + FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + $rev = $db->GetAll($sql2); + + //all variables to check in limesurvey for this row quota + $sql2 = "SELECT lime_sgqa,value,comparison + FROM qsqr_question + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + $qev = $db->GetAll($sql2); + + //whether a completion was changed for this quota + $updatequota = false; + //whether priorites need to be updated + $update = false; + //default completions at 0 + $completions = 0; + + //if a case_Id is specified, we can just check if this case matches + //the quota criteria, and if so, increment the quota completions counter + if ($case_id != false) + { + if (empty($qev)) + { + //just determine if this case is linked to a matching sample record + $sql2 = "SELECT count(*) as c + FROM " . LIME_PREFIX . "survey_$lime_sid as s + JOIN `case` as c ON (c.case_id = '$case_id') + JOIN `sample` as sam ON (c.sample_id = sam.sample_id) "; + + $x = 1; + foreach($rev as $ev) + { + $sql2 .= " JOIN sample_var as sv$x ON (sv$x.sample_id = sam.sample_id AND sv$x.var_id = '{$ev['exclude_var_id']}' AND sv$x.val {$ev['comparison']} '{$ev['exclude_val']}') "; + $x++; + } + + $sql2 .= " WHERE s.token = c.token"; + + $match = $db->GetOne($sql2); + } + else + { + //determine if the case is linked to a matching limesurvey record + $sql2 = "SELECT count(*) as c + FROM " . LIME_PREFIX . "survey_$lime_sid as s + JOIN `case` as c ON (c.case_id = '$case_id') + JOIN `sample` as sam ON (c.sample_id = sam.sample_id) + WHERE s.token = c.token + "; + + foreach($qev as $ev) + $sql2 .= " AND s.`{$ev['lime_sgqa']}` {$ev['comparison']} '{$ev['value']}' "; + + $match = $db->GetOne($sql2); + } + + if ($match == 1) + { + //increment completions + $sql = "SELECT (current_completions + 1) as c + FROM questionnaire_sample_quota_row + WHERE questionnaire_sample_quota_row_id = '$qsqri'"; + $cc = $db->GetRow($sql); + + $completions = $cc['c']; + + $updatequota = true; + } + } + else + { + if (empty($qev)) + { + //find all completions from cases with matching sample records + + $sql2 = "SELECT count(*) as c + FROM " . LIME_PREFIX . "survey_$lime_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')"; + + $x = 1; + foreach($rev as $ev) + { + $sql2 .= " JOIN sample_var as sv$x ON (sv$x.sample_id = sam.sample_id AND sv$x.var_id = '{$ev['exclude_var_id']}' AND sv$x.val {$ev['comparison']} '{$ev['exclude_val']}') "; + $x++; + } + + $sql2 .= " WHERE s.submitdate IS NOT NULL + AND s.token = c.token"; + + $completions = $db->GetOne($sql2); + } + else + { + //find all completions from cases with matching limesurvey records + $sql2 = "SELECT count(*) as c + FROM " . LIME_PREFIX . "survey_$lime_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 s.submitdate IS NOT NULL + AND s.token = c.token "; + + foreach($qev as $ev) + $sql2 .= " AND s.`{$ev['lime_sgqa']}` {$ev['comparison']} '{$ev['value']}' "; + + $completions = $db->GetOne($sql2); + } + + $updatequota = true; + } + + if ($updatequota) + { + if ($completions >= $target_completions) + { + //set row quota to reached + $sql = "UPDATE questionnaire_sample_quota_row + SET quota_reached = '1', current_completions = '$completions' + WHERE questionnaire_sample_quota_row_id = '$qsqri'"; + + $db->Execute($sql); + + close_row_quota($qsqri,false); //don't update priorires just yet + $update = true; + } + else + { + $sql = "UPDATE questionnaire_sample_quota_row + SET current_completions = '$completions' "; + + //If autopriority is set update it here + if ($autoprioritise == 1) + { + //priority is 100 - the percentage of completions + $pr = 100 - round(100 * ($completions / $target_completions)); + $sql .= ", priority = '$pr' "; + + //need to update quotas now + $update = true; + } + + $sql .= " WHERE questionnaire_sample_quota_row_id = '$qsqri'"; + + $db->Execute($sql); + + } + } + return $update; } @@ -1663,106 +1864,30 @@ function update_row_quota($questionnaire_id,$case_id = false) $db->StartTrans(); - $sql = "SELECT questionnaire_sample_quota_row_id,q.questionnaire_id,sample_import_id,lime_sgqa,value,comparison,completions,quota_reached,q.lime_sid, qsq.exclude_var_id,qsq.exclude_var,qsq.exclude_val,qsq.current_completions,qsq.priority,qsq.autoprioritise - FROM questionnaire_sample_quota_row as qsq, questionnaire as q + $sql = "SELECT qsq.questionnaire_sample_quota_row_id + FROM questionnaire_sample_quota_row as qsq WHERE qsq.questionnaire_id = '$questionnaire_id' - AND q.questionnaire_id = '$questionnaire_id' - AND qsq.lime_sgqa != -1"; + AND qsq.quota_reached != '1' + GROUP BY qsq.questionnaire_sample_quota_row_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) - { - //whether a completion was changed for this quota - $updatequota = false; - - //if a case_Id is specified, we can just check if this case matches - //the quota criteria, and if so, increment the quota completions counter - if ($case_id != false) - { - if ($r['lime_sgqa'] == -2) - $match = limesurvey_quota_replicate_match($r['lime_sid'],$case_id,$r['exclude_val'],$r['exclude_var_id'],$r['sample_import_id']); - else - $match = limesurvey_quota_match($r['lime_sgqa'],$r['lime_sid'],$case_id,$r['value'],$r['comparison'],$r['sample_import_id']); - - if ($match == 1) - { - //increment completions - $sql = "SELECT (current_completions + 1) as c - FROM questionnaire_sample_quota_row - WHERE questionnaire_sample_quota_row_id = {$r['questionnaire_sample_quota_row_id']}"; - $cc = $db->GetRow($sql); - - $completions = $cc['c']; - - $updatequota = true; - } - - } - else - { - if ($r['lime_sgqa'] == -2) - $completions = limesurvey_quota_replicate_completions($r['lime_sid'],$r['questionnaire_id'],$r['sample_import_id'],$r['exclude_val'],$r['exclude_var_id']); - else - $completions = limesurvey_quota_completions($r['lime_sgqa'],$r['lime_sid'],$r['questionnaire_id'],$r['sample_import_id'],$r['value'],$r['comparison']); - $updatequota = true; - } - - if ($updatequota) - { - if ($completions >= $r['completions']) - { - //set row quota to reached - $sql = "UPDATE questionnaire_sample_quota_row - SET quota_reached = '1', current_completions = '$completions' - WHERE questionnaire_sample_quota_row_id = {$r['questionnaire_sample_quota_row_id']}"; - - $db->Execute($sql); - - close_row_quota($r['questionnaire_sample_quota_row_id'],false); //don't update priorires just yet - $update = true; - } - else - { - $sql = "UPDATE questionnaire_sample_quota_row - SET current_completions = '$completions' "; - - //If autopriority is set update it here - if ($r['autoprioritise'] == 1) - { - //priority is 100 - the percentage of completions - $pr = 100 - round(100 * ($completions / $r['completions'])); - if ($pr < 0) - $pr = 0; - $sql .= ", priority = '$pr' "; - - //need to update quotas now - $update = true; - } - - $sql .= " WHERE questionnaire_sample_quota_row_id = {$r['questionnaire_sample_quota_row_id']}"; - - $db->Execute($sql); - - } - - - } - - } - if ($update) update_quota_priorities($questionnaire_id); - } + if (isset($rs) && !empty($rs)) + { + foreach ($rs as $r) + { + $tmp = update_single_row_quota($r['questionnaire_sample_quota_row_id'],$case_id); + if ($tmp) + $update = true; + } + } + if ($update) + update_quota_priorities($questionnaire_id); + $db->CompleteTrans(); return false; - } /** @@ -1825,16 +1950,39 @@ function update_quota_priorities($questionnaire_id) $qsqri = $r['questionnaire_sample_quota_row_id']; $priority = $r['priority']; + $sql2 = "SELECT exclude_val,exclude_var,exclude_var_id,comparison + FROM qsqr_sample + WHERE questionnaire_sample_quota_row_id = $qsqri"; + + $rev = $db->GetAll($sql2); + + //find all cases that match this quota, and update it to the new priority - $sql = "UPDATE sample as s, sample_var as sv, questionnaire_sample_quota_row as qs, questionnaire_sample_exclude_priority as qsep + $sql = "UPDATE sample as s, questionnaire_sample_quota_row as qs, questionnaire_sample_exclude_priority as qsep "; + + //reduce sample by every item in the qsqr_sample table + $x = 1; + foreach ($rev as $ev) + { + $sql .= ", sample_var as sv$x"; + $x++; + } + + $sql .= " SET qsep.priority = '$priority' WHERE s.import_id = qs.sample_import_id AND qs.questionnaire_sample_quota_row_id = '$qsqri' - AND s.sample_id = sv.sample_id - AND sv.var_id = qs.exclude_var_id - AND qs.exclude_val LIKE sv.val + AND s.sample_id = sv$x.sample_id + AND sv$x.var_id = qs.exclude_var_id AND qsep.questionnaire_id = qs.questionnaire_id - AND qsep.sample_id = s.sample_id"; + AND qsep.sample_id = s.sample_id "; + + $x = 1; + foreach ($rev as $ev) + { + $sql .= " AND sv$x.sample_id = s.sample_id AND sv$x.var_id = '{$ev['exclude_var_id']}' AND sv$x.val {$ev['comparison']} '{$ev['exclude_val']}' "; + $x++; + } $db->Execute($sql); diff --git a/nocaseavailable.php b/nocaseavailable.php index edf61330..aeed624c 100644 --- a/nocaseavailable.php +++ b/nocaseavailable.php @@ -226,11 +226,10 @@ if (isset($rs) && !empty($rs)) } //quota row's full -$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, operator_questionnaire as oq +$sql = "SELECT qsq.questionnaire_sample_quota_row_id,qsq.quota_reached,qsq.description + FROM questionnaire_sample_quota_row as qsq, operator_questionnaire as oq WHERE oq.operator_id = '$operator_id' - AND qsq.questionnaire_id = oq.questionnaire_id - AND q.questionnaire_id = oq.questionnaire_id"; + AND qsq.questionnaire_id = oq.questionnaire_id"; $rs = $db->GetAll($sql); @@ -240,7 +239,7 @@ if (isset($rs) && !empty($rs)) { if ($r['quota_reached'] == 1) { - 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']; } } }