From b4e84e014832a6484db5374226f167546b68da72 Mon Sep 17 00:00:00 2001 From: Adam Zammit Date: Fri, 7 Aug 2015 01:46:43 -0400 Subject: [PATCH] New Feature: Pre-generation of cases and copying of sample variables to Limesurvey attributes for "Web First" then CATI methodology --- admin/assignsample.php | 58 +++++++++++++++++++++++++ functions/functions.operator.php | 73 ++++++++++++++++++++++++++++++-- 2 files changed, 128 insertions(+), 3 deletions(-) diff --git a/admin/assignsample.php b/admin/assignsample.php index fc3798fe..61c563a0 100644 --- a/admin/assignsample.php +++ b/admin/assignsample.php @@ -92,6 +92,57 @@ if (isset($_GET['questionnaire_id']) && isset($_GET['sample']) && isset($_GET[' VALUES('$questionnaire_id','$sid','$cm','$cam','$selecttype','$am', '$an')"; $db->Execute($sql); + + if (isset($_GET['generatecases'])) + { + include_once("../functions/functions.operator.php"); + + $db->StartTrans(); + + $lime_sid = $db->GetOne("SELECT lime_sid FROM questionnaire WHERE questionnaire_id = '$questionnaire_id'"); + $testing = $db->GetOne("SELECT testing FROM questionnaire WHERE questionnaire_id = '$questionnaire_id'"); + + //add limesurvey attribute for each sample var record + $sql = "SELECT var,type + FROM sample_import_var_restrict + WHERE sample_import_id = '$sid'"; + + $rs = $db->GetAll($sql); + + $i = 1; + + $fields = array(); + $fieldcontents=''; + foreach($rs as $r) + { + $fields[]=array('attribute_'.$i,'C','255'); + $fieldcontents.='attribute_'.$i.'='.$r['var']."\n"; + $i++; + } + $dict = NewDataDictionary($db); + $sqlarray = $dict->ChangeTableSQL(LIME_PREFIX ."tokens_$lime_sid", $fields); + $execresult=$dict->ExecuteSQLArray($sqlarray, false); + + $sql = "UPDATE " . LIME_PREFIX . "surveys + SET attributedescriptions = " . $db->qstr($fieldcontents) . " + WHERE sid='$lime_sid'"; + + $db->Execute($sql); + + //generate one case for each sample record and set outcome to 41 + $sql = "SELECT sample_id + FROM sample + WHERE import_id = '$sid'"; + + $rs = $db->GetAll($sql); + + foreach($rs as $r) + { + add_case($r['sample_id'],$questionnaire_id,"NULL",$testing,41, true); + } + + $db->CompleteTrans(); + } } if (isset($_POST['edit'])) @@ -266,7 +317,14 @@ if ($questionnaire_id != false)
" data-off="" data-width="85"/>



+ + GetOne("SELECT self_complete FROM questionnaire WHERE questionnaire_id = '$questionnaire_id'"); + if ($self_complete) {?> + +
" data-off="" data-width="85"/>



+ +
diff --git a/functions/functions.operator.php b/functions/functions.operator.php index 50fbea9d..e8367ab6 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -345,17 +345,19 @@ function is_respondent_selection($operator_id) * @param int $questionnaire_id The questionnaire id * @param int $operator_id The operator id (Default NULL) * @param int $testing 0 if a live case otherwise 1 for a testing case + * @param int $current_outcome_id The current outcome id (defaults to 1 - not attempted) + * @param bool $addlimeattributes If true, add sample values as lime attributes * * @return int The case id */ -function add_case($sample_id,$questionnaire_id,$operator_id = "NULL",$testing = 0) +function add_case($sample_id,$questionnaire_id,$operator_id = "NULL",$testing = 0, $current_outcome_id = 1, $addlimeattributes = false) { global $db; $token = sRandomChars(); $sql = "INSERT INTO `case` (case_id, sample_id, questionnaire_id, last_call_id, current_operator_id, current_call_id, current_outcome_id,token) - VALUES (NULL, $sample_id, $questionnaire_id, NULL, $operator_id, NULL, 1, '$token')"; + VALUES (NULL, $sample_id, $questionnaire_id, NULL, $operator_id, NULL, '$current_outcome_id','$token')"; $db->Execute($sql); @@ -440,10 +442,75 @@ function add_case($sample_id,$questionnaire_id,$operator_id = "NULL",$testing = if ($lime_sid) { + $lfirstname = ''; + $llastname = ''; + $lemail = ''; + + if ($addlimeattributes) + { + $lfirstname = $db->GetOne("SELECT sv.val + FROM sample_var as sv, sample_import_var_restrict as s + WHERE sv.var_id = s.var_id + AND sv.sample_id = '$sample_id' + AND s.type = '6'"); + + $llastname = $db->GetOne("SELECT sv.val + FROM sample_var as sv, sample_import_var_restrict as s + WHERE sv.var_id = s.var_id + AND sv.sample_id = '$sample_id' + AND s.type = '7'"); + + $lemail = $db->GetOne("SELECT sv.val + FROM sample_var as sv, sample_import_var_restrict as s + WHERE sv.var_id = s.var_id + AND sv.sample_id = '$sample_id' + AND s.type = '8'"); + + } + $sql = "INSERT INTO ".LIME_PREFIX."tokens_$lime_sid (tid,firstname,lastname,email,token,language,sent,completed,mpid) - VALUES (NULL,'','','','$token','".DEFAULT_LOCALE."','N','N',NULL)"; + VALUES (NULL,'$lfirstname','$llastname','$lemail','$token','".DEFAULT_LOCALE."','N','N',NULL)"; $db->Execute($sql); + + $tid = $db->Insert_Id(); + + if ($addlimeattributes) + { + //also add sample values as attributes + //match by name + + $sql = "SELECT attributedescriptions + FROM " . LIME_PREFIX . "surveys + WHERE sid = '$lime_sid'"; + + $names = $db->GetOne($sql); + + $attdescriptiondata=explode("\n",$names); + $atts=array(); + + foreach ($attdescriptiondata as $attdescription) + { + if (!empty($attdescription)) + $atts['attribute_'.substr($attdescription,10,strpos($attdescription,'=')-10)] = substr($attdescription,strpos($attdescription,'=')+1); + } + + foreach($atts as $key => $val) + { + $lval = $db->GetOne("SELECT sv.val + FROM sample_var as sv, sample_import_var_restrict as s + WHERE sv.var_id = s.var_id + AND sv.sample_id = '$sample_id' + AND s.var LIKE '$val'"); + + $sql = "UPDATE " . LIME_PREFIX . "tokens_$lime_sid + SET $key = '$lval' + WHERE tid = '$tid'"; + + $db->Execute($sql); + } + + } } }