* @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"); /** * Authentication file */ require ("auth-admin.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_id, 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'])) { $exvar_id = $db->qstr(substr($_POST['sample_var_id'],15,strpos($_POST['sample_var_id'],'&')-15)); $exvar = $db->qstr($_POST['sample_var']); $exval = $db->qstr($_POST['exclude_val']); $comparison = $db->qstr($_POST['comparisons']); $description = $db->qstr($_POST['description']); //add ssample $sql = "INSERT INTO qsqr_sample (questionnaire_sample_quota_row_id,exclude_var_id,exclude_var,exclude_val,comparison,description) VALUES ($qsqri,$exvar_id,$exvar,$exval,$comparison,$description)"; $db->Execute($sql); //Make sure to calculate on the spot update_single_row_quota($qsqri); } if (isset($_POST['addq'])) { $sgqa = $db->qstr(substr($_POST['sgqa'],6,strpos($_POST['sgqa'],'&')-6)); $value = $db->qstr($_POST['value']); $comparison = $db->qstr($_POST['comparison']); $description = $db->qstr($_POST['description']); //add ssample $sql = "INSERT INTO qsqr_question (questionnaire_sample_quota_row_id,lime_sgqa,value,comparison,description) VALUES ($qsqri,$sgqa,$value,$comparison,$description)"; $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/css/bootstrap.min.css","../include/bootstrap-toggle/css/bootstrap-toggle.min.css","../include/font-awesome/css/font-awesome.css","../include/iCheck/skins/square/blue.css","../css/custom.css"),array("../include/jquery/jquery.min.js","../include/bootstrap/js/bootstrap.min.js","../include/bootstrap-toggle/js/bootstrap-toggle.min.js","../include/iCheck/icheck.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 "
 " . T_("To quota report") . "
"; 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, placed in order of records sequence $description = $db->quote($data[0]); $completions = intval($data[1]); $autoprioritise = 0; if ($data[2] != 0) $autoprioritise = 1; $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 $var_id = $db->qstr($data[$i*3]); $var = $db->qstr($data[($i*3) + 1]); $comparison = $db->qstr($data[($i*3) + 2]); $value = $db->qstr($data[($i*3) + 3]); $sql = "INSERT INTO qsqr_sample (questionnaire_sample_quota_row_id,exclude_var_id,exclude_var,exclude_val,comparison) VALUES ($qq,$var_id,$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_("To Row quotas") . "

"; print "

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

"; ?>
" method="post" class="form-inline table">

  

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

 ') as qdelete FROM qsqr_question WHERE questionnaire_sample_quota_row_id = $qsqri"; $rs = $db->GetAll($sql); print "
"; print "

" . T_("Restrictions based on answered questions") . "

"; print "
"; if (empty($rs)) { print "

" . T_("Currently NO Restrictions based on answered questions") . "

" . 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('description','lime_sgqa','comparison','value','qdelete'),array(T_("Description"),T_("SGQ code"),T_("Comparison"),T_("Value"),"  ")); print "
"; } //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 + corrected question order as in questionnaire with subquestions $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, CONCAT( lq.sid, 'X', lq.gid, 'X', CASE WHEN lq.parent_qid = 0 THEN lq.qid ELSE CONCAT(lq.parent_qid, lq.title) END, ' -> ' , 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 CASE WHEN lq2.question_order IS NULL THEN lq.question_order ELSE lq2.question_order + (lq.question_order / 1000) END ASC"; $rsgqa = $db->GetAll($sql); if (!empty($rsgqa)) { print "
"; print "

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

"; print ""; display_chooser($rsgqa,"sgqa","sgqa",true,"edit=edit&qsqri=$qsqri"); if (isset($_GET['sgqa'])){ ?>

  

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

"; $rs = ""; if (isset($_GET['sgqa'])) { $sgqa = $_GET['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'"; $rsc = $db->GetAll($sql); } if (!isset($rsc) || empty($rsc)) print "

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

"; else xhtml_table($rsc,array('code','title'),array(T_("Code value"), T_("Description"))); } else { print ""; } print "
"; } print "
"; //list sample records to exclude $sql = "SELECT qsqr_sample_id,exclude_var_id,exclude_var,exclude_val,comparison,description, CONCAT('  ') as sdelete FROM qsqr_sample WHERE questionnaire_sample_quota_row_id = $qsqri"; $rs = $db->GetAll($sql); print "
"; print "

" . T_("Restrictions based on sample records") . "

"; print "
"; if (empty($rs)) { print "

" . T_("Currently NO Restrictions based on sample records") . "

" . T_("This sample will be limited to number of completions set in quota") . "

" . T_("Caling cases for this sample will be stopped when the quota is reached") . "

"; } 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('description','exclude_var_id','exclude_var','comparison','exclude_val','sdelete'),array(T_("Description"),T_("Sample var ID"),T_("Sample variable"),T_("Comparison"),T_("Value"),"  ")); print "
"; } $ssample_var_id = "''"; if (isset($_GET['sample_var_id'])) $ssample_var_id = $db->qstr($_GET['sample_var_id']); //add sample references (records from sample to exclude when quota reached) $sql = "SELECT sivr.var_id as value, sivr.var as description, CASE WHEN sivr.var_id = $ssample_var_id THEN 'selected=\'selected\'' ELSE '' END AS selected FROM `sample_import_var_restrict` as sivr, `sample_var` AS sv, `sample` AS s WHERE sivr.sample_import_id = $sample_import_id AND s.sample_id = sv.sample_id AND sivr.var_id = sv.var_id GROUP BY sivr.var_id"; $rsvi = $db->GetAll($sql); if (!empty($rsvi)) { if ($ssample_var_id == "''") $ssample_var_id = $rsvi[0]['value']; print "

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

"; print "
"; print "

: "; display_chooser($rsvi,"sample_var_id","sample_var_id",true,"edit=edit&qsqri=$qsqri",true,false); if (isset($_GET['sample_var_id'])){ ?>  :  GetAll($sql); $sample_var = $val[0]['var']; display_chooser($val,"exclude_val","exclude_val",false,false,false,false); flush(); ?>  

";} print "
"; } print "
"; } else { $sql = "SELECT questionnaire_sample_quota_row_id,qsq.description, CONCAT('  ') 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); if (empty($r)) { print "

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

"; } else { print "
"; print "

" . T_("Current row quotas") . "

"; xhtml_table($r,array('description','qedit','completions','current_completions','status','priority','ap','qselect'),array(T_("Description"),"  ",T_("Quota"),T_("Completions"),T_("Status"),T_("Priority"),T_("Auto prioritise"),"  ")); print "

"; print "

"; print "
"; print "
"; } print "

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

"; ?>
" method="post" class="form-inline table">

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

" . T_("Import row quota") . "

"; $ua = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/Firefox/i', $ua)) $csv= "text/csv"; else $csv= ".csv"; ?>
" method="post">

..." accept=""/>