From 0ef7c9373718ecf54d610f9af6e86136e8d3a72a Mon Sep 17 00:00:00 2001 From: azammitdcarf Date: Wed, 29 Sep 2010 23:19:08 +0000 Subject: [PATCH] New feature: Seperate limesurvey instrument for respondent selection Dev Merged back changes from mcmasterlimers branch --- admin/databasestrings.php | 1 + admin/new.php | 43 +- call.php | 6 +- config.default.php | 5 + database/quexs.sql | 2 + functions/functions.limesurvey.php | 11 +- functions/functions.operator.php | 80 +++- .../admin/export_data_functions.php | 6 +- include/limesurvey/admin/importsurvey.php | 11 +- include/limesurvey/common.php | 24 ++ include/limesurvey/group.php | 7 + include/limesurvey/qanda.php | 82 +++- include/limesurvey/question.php | 5 + include/limesurvey/quexs.php | 397 ++++++++++++++++++ include/limesurvey/save.php | 7 + include/limesurvey/survey.php | 6 + index.php | 2 +- js/new.js | 12 + 18 files changed, 674 insertions(+), 33 deletions(-) diff --git a/admin/databasestrings.php b/admin/databasestrings.php index c3ae8419..e4be4717 100755 --- a/admin/databasestrings.php +++ b/admin/databasestrings.php @@ -64,6 +64,7 @@ T_("Temporary Outcomes (normal cases)"); T_("Supervisor Outcomes (referred to supervisor)"); T_("Refusal Outcomes (respondent refused)"); T_("Final Outcomes (completed, final refusal, etc)"); +T_("Miscellaneous - Unavailable for a week"); T_("Appointments"); T_("Monday"); T_("Tuesday"); diff --git a/admin/new.php b/admin/new.php index 072451ac..4c71ec77 100644 --- a/admin/new.php +++ b/admin/new.php @@ -63,10 +63,11 @@ if (isset($_POST['import_file'])) $testing = 0; $rs = 0; $lime_sid = 0; + $lime_rs_sid = "NULL"; if (isset($_POST['ras'])) $ras = 1; if (isset($_POST['rws'])) $rws = 1; if (isset($_POST['testing'])) $testing = 1; - if (isset($_POST['rs'])) $rs = 1; + if ($_POST['selectrs'] != "none") $rs = 1; $name = $db->qstr($_POST['description'],get_magic_quotes_gpc()); $rs_intro = $db->qstr($_POST['rs_intro'],get_magic_quotes_gpc()); @@ -78,8 +79,7 @@ if (isset($_POST['import_file'])) if ($_POST['select'] == "new") { //create one from scratch - include("../functions/functions.limesurvey.php"); - + include_once("../functions/functions.limesurvey.php"); $lime_sid = create_limesurvey_questionnaire($name); } else @@ -88,8 +88,19 @@ if (isset($_POST['import_file'])) $lime_sid = bigintval($_POST['select']); } - $sql = "INSERT INTO questionnaire (questionnaire_id,description,lime_sid,restrict_appointments_shifts,restrict_work_shifts,respondent_selection,rs_intro,rs_project_intro,rs_project_end,rs_callback,rs_answeringmachine,testing) - VALUES (NULL,$name,'$lime_sid','$ras','$rws','$rs',$rs_intro,$rs_project_intro,$rs_project_end,$rs_callback,$rs_answeringmachine,'$testing')"; + if ($_POST['selectrs'] == "new") + { + //create one from scratch + include_once("../functions/functions.limesurvey.php"); + $lime_rs_sid = create_limesurvey_questionnaire($db->qstr(T_("Respondent Selection for ") . $_POST['description']),false); + } + else if (is_numeric($_POST['selectrs'])) + { + $lime_rs_sid = bigintval($_POST['selectrs']); + } + + $sql = "INSERT INTO questionnaire (questionnaire_id,description,lime_sid,restrict_appointments_shifts,restrict_work_shifts,respondent_selection,rs_intro,rs_project_intro,rs_project_end,rs_callback,rs_answeringmachine,testing,lime_rs_sid) + VALUES (NULL,$name,'$lime_sid','$ras','$rws','$rs',$rs_intro,$rs_project_intro,$rs_project_end,$rs_callback,$rs_answeringmachine,'$testing',$lime_rs_sid)"; $rs = $db->Execute($sql); @@ -128,18 +139,34 @@ if (!empty($surveys)) print ""; } } +?>

+

-

-
+ +

"/>

dial(get_extension($operator_id),get_call_number($call_id)); } if (is_respondent_selection($operator_id)) - $btext = "onload='openParentObject(\"main-content\",\"rs_intro.php\"); parent.closePopup();'"; + $btext = "onload='openParentObject(\"main-content\",\"" . get_respondentselection_url($operator_id) . "\"); parent.closePopup();'"; else $btext = "onload='parent.closePopup();'"; xhtml_head(T_("Call"),true,array("css/call.css"),array("js/window.js"),$btext); @@ -188,7 +188,7 @@ if (isset($_POST['submit'])) } //disable recording $newtext = T_("Start REC"); - xhtml_head(T_("Call"),true,array("css/call.css"),array("js/window.js"),"onload='toggleRec(\"$newtext\",\"record.php?start=start\",\"offline\"); parent.closePopup();'"); + xhtml_head(T_("Call"),true,array("css/call.css"),array("js/window.js"),"onload='toggleRec(\"$newtext\",\"record.php?start=start\",\"offline\"); openParentObject(\"main-content\",\"" . get_respondentselection_url($operator_id) . "\"); parent.closePopup();'"); } else { @@ -202,7 +202,7 @@ if (isset($_POST['submit'])) } //disable recording $newtext = T_("Start REC"); - xhtml_head(T_("Call"),true,array("css/call.css"),array("js/window.js"),"onload='toggleRec(\"$newtext\",\"record.php?start=start\",\"offline\"); parent.closePopup();'"); + xhtml_head(T_("Call"),true,array("css/call.css"),array("js/window.js"),"onload='toggleRec(\"$newtext\",\"record.php?start=start\",\"offline\"); openParentObject(\"main-content\",\"" . get_respondentselection_url($operator_id) . "\"); parent.closePopup();'"); } diff --git a/config.default.php b/config.default.php index 9a246a41..fc4423a8 100644 --- a/config.default.php +++ b/config.default.php @@ -54,6 +54,11 @@ if (!defined('DEFAULT_TIME_ZONE')) define('DEFAULT_TIME_ZONE', 'Australia/Victor */ if (!defined('DATE_TIME_FORMAT')) define('DATE_TIME_FORMAT','%a %d %b %I:%i%p'); +/** + * Date format for displaying: see above for mySQL details + */ +if (!defined('DATE_FORMAT')) define('DATE_FORMAT','%a %d %b'); + /** * Time format for displaying: see above for mySQL details */ diff --git a/database/quexs.sql b/database/quexs.sql index c9335044..a5596ec8 100644 --- a/database/quexs.sql +++ b/database/quexs.sql @@ -409,6 +409,7 @@ INSERT INTO `outcome` VALUES(29, '3.14', 'Answering machine - Not a household', INSERT INTO `outcome` VALUES(30, '4.10', 'Out of sample', 0, 4, 0, 1, 0, 0, 0, ''); INSERT INTO `outcome` VALUES(31, '2.20', 'Non contact', 180, 1, 1, 1, 1, 1, 0, 'NC'); INSERT INTO `outcome` VALUES(32, '4.80', 'Quota filled', 0, 4, 0, 1, 0, 0, 0, ''); +INSERT INTO `outcome` VALUES(33, '2.36', 'Miscellaneous - Unavailable for a week', 10080, 1, 0, 1, 1, 1, 0, 'O'); -- -------------------------------------------------------- @@ -466,6 +467,7 @@ CREATE TABLE `questionnaire` ( `rs_project_end` text collate utf8_unicode_ci NOT NULL, `rs_callback` text collate utf8_unicode_ci NOT NULL, `rs_answeringmachine` text collate utf8_unicode_ci NOT NULL, + `lime_rs_sid` int(11) default NULL, PRIMARY KEY (`questionnaire_id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; diff --git a/functions/functions.limesurvey.php b/functions/functions.limesurvey.php index 465afa5a..05670d02 100644 --- a/functions/functions.limesurvey.php +++ b/functions/functions.limesurvey.php @@ -246,9 +246,10 @@ function getRandomID() * With modifications * * @param string $title Questionnaire name + * @param bool $exittoend Whether to exit to the project end, or to the start of the questionnaire * @link http://www.limesurvey.org/ LimeSurvey */ -function create_limesurvey_questionnaire($title) +function create_limesurvey_questionnaire($title,$exittoend = true) { global $db; @@ -286,7 +287,13 @@ function create_limesurvey_questionnaire($title) . "'', '',\n" . "'', '',\n" . "'', '',\n" - . "'', '" . QUEXS_URL . "rs_project_end.php')"; + . "'', '"; + + if ($exittoend) + $isquery .= QUEXS_URL . "rs_project_end.php')"; + else + $isquery .= "{STARTINTERVIEWURL}')"; + $isresult = $db->Execute($isquery) or die ($isquery."
".$db->ErrorMsg()); diff --git a/functions/functions.operator.php b/functions/functions.operator.php index 7749c5fc..92c514b2 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -228,7 +228,7 @@ function template_replace($string,$operator_id,$case_id) * enabled * * @param int $operator_id The operator id - * @return bool True if respondent selection enabled otherwise false + * @return bool|int True if respondent selection enabled, lime_sid if rs enabled with limesurvey otherwise false * */ function is_respondent_selection($operator_id) @@ -242,7 +242,7 @@ function is_respondent_selection($operator_id) if (!$questionnaire_id) return false; - $sql = "SELECT respondent_selection + $sql = "SELECT respondent_selection, lime_rs_sid FROM questionnaire WHERE questionnaire_id = '$questionnaire_id'"; @@ -255,7 +255,13 @@ function is_respondent_selection($operator_id) return false; if (!$rs) return false; - if ($rs['respondent_selection'] == 1) return true; + if ($rs['respondent_selection'] == 1){ + if ($rs['lime_rs_sid'] > 0) + { + return $rs['lime_rs_sid']; + } + return true; + } return false; } @@ -427,8 +433,10 @@ function get_case_id($operator_id, $create = false) $i = 1; foreach ($r5 as $r5v) { + $tnum = ereg_replace('[^0-9]*','',$r5v['phone']); + if (empty($tnum)) $tnum = "312345678"; //handle error condition $sql = "INSERT INTO contact_phone (case_id,priority,phone) - VALUES ($case_id,$i," . ereg_replace('[^0-9]*','',$r5v['phone']) . ")"; + VALUES ($case_id,$i,$tnum)"; $db->Execute($sql); $i++; } @@ -448,7 +456,8 @@ function get_case_id($operator_id, $create = false) SELECT $case_id as case_id, s1.val as firstName, s2.val as lastName, s3.Time_zone_name as Time_zone_name FROM sample as s3 LEFT JOIN sample_var as s2 on (s2.sample_id = '{$r3['sample_id']}' and s2.type = 7) - LEFT JOIN sample_var as s1 on (s1.sample_id = '{$r3['sample_id']}' and s1.type = 6) WHERE s3.sample_id = '{$r3['sample_id']}'"; + LEFT JOIN sample_var as s1 on (s1.sample_id = '{$r3['sample_id']}' and s1.type = 6) + WHERE s3.sample_id = '{$r3['sample_id']}'"; $db->Execute($sql); @@ -903,6 +912,20 @@ function get_call($operator_id,$respondent_id = "",$contact_phone_id = "",$creat VALUES (NULL,'$operator_id','$case_id','$ca',CONVERT_TZ(NOW(),'System','UTC'),NULL,'$respondent_id','$contact_phone_id','0','1')"; $db->Execute($sql); $id = $db->Insert_Id(); + + //If respondent selection is enabled, add token to RS Limesurvey database + $lime_rsid = is_respondent_selection($operator_id); + + if ($lime_rsid !== true && $lime_rsid > 0 && !$db->HasFailedTrans()) + //if the transaction hasn't failed and Limesurvey RS is enabled + { + $sql = "INSERT INTO ".LIME_PREFIX."tokens_$lime_rsid (tid,firstname,lastname,email,token,language,sent,completed,mpid) + VALUES (NULL,'','','',$id,'en','N','N',NULL)"; + + //Insert the token as the call_id + $db->Execute($sql); + } + } else { @@ -924,6 +947,39 @@ function get_call($operator_id,$respondent_id = "",$contact_phone_id = "",$creat } +/** + * Get the complete URL for the Limesurvey questionnaire of respondent selection + * If no case available, return an error screen + * + * @param int $operator_id The operator id + * @return string The URL of the LimeSurvey questionnaire, or the URL of an error screen if none available + * + */ +function get_respondentselection_url($operator_id) +{ + global $db; + + $db->StartTrans(); + + $url = "nocallavailable.php"; + + $call_id = get_call($operator_id); + + if ($call_id) + { + $sid = get_limesurvey_id($operator_id,true); //true for RS + if ($sid != false && !empty($sid) && $sid != 'NULL') + $url = LIME_URL . "index.php?loadall=reload&sid=$sid&token=$call_id&lang=" . DEFAULT_LOCALE; + else + $url = 'rs_intro.php'; + } + + //if ($db->HasFailedTrans()) { print "FAILED in get_limesurvey_url"; exit; } + $db->CompleteTrans(); + + return $url; +} + /** * Get the complete URL for the Limesurvey questionnaire * If no case available, return an error screen @@ -1782,17 +1838,21 @@ function end_call($operator_id,$outcome_id,$state = 5) * Get the limesurvey "survey id" of the current questionnaire assigned to the operator * * @param int $operator_id The operator + * @param bool $rs If asking for respondent selection * @return bool|int False if none found else the limesurvey sid * - * @todo Implement session destruction here * */ -function get_limesurvey_id($operator_id) +function get_limesurvey_id($operator_id,$rs = false) { global $db; - $sql = "SELECT q.lime_sid as lime_sid - FROM `case` as c, questionnaire_sample as qs, sample as s, questionnaire as q + if ($rs) + $sql = "SELECT q.lime_rs_sid as sid"; + else + $sql = "SELECT q.lime_sid as sid"; + + $sql .= " FROM `case` as c, questionnaire_sample as qs, sample as s, questionnaire as q WHERE c.current_operator_id = '$operator_id' AND c.sample_id = s.sample_id AND s.import_id = qs.sample_import_id @@ -1804,7 +1864,7 @@ function get_limesurvey_id($operator_id) if (empty($rs)) return false; - return $rs['lime_sid']; + return $rs['sid']; } diff --git a/include/limesurvey/admin/export_data_functions.php b/include/limesurvey/admin/export_data_functions.php index 32c18090..baa676d6 100644 --- a/include/limesurvey/admin/export_data_functions.php +++ b/include/limesurvey/admin/export_data_functions.php @@ -308,7 +308,7 @@ function spss_fieldmap($prefix = 'V') { foreach ($tokenattributes as $attributefield=>$attributedescription) { //Drop the token field, since it is in the survey too - if($attributefield!='token') { + if($attributefield!='token' && $attributefield!='callattempts' && $attributefield!='onappointment') { //queXS Addition $fieldno++; $fields[] = array('id'=>"$prefix$fieldno",'name'=>mb_substr($attributefield, 0, 8), 'qid'=>0,'code'=>'','SPSStype'=>'A','LStype'=>'Undef', @@ -431,7 +431,7 @@ function spss_getquery() { $tokenattributes=GetTokenFieldsAndNames($surveyid,false); foreach ($tokenattributes as $attributefield=>$attributedescription) { //Drop the token field, since it is in the survey too - if($attributefield!='token') { + if($attributefield!='token' && $attributefield!='callattempts' && $attributefield!='onappointment') { //queXS Addition $query .= "{$dbprefix}tokens_$surveyid.$attributefield, "; } } @@ -454,4 +454,4 @@ function spss_getquery() { } return $query; } -?> \ No newline at end of file +?> diff --git a/include/limesurvey/admin/importsurvey.php b/include/limesurvey/admin/importsurvey.php index 8f41d51e..56bc9a33 100644 --- a/include/limesurvey/admin/importsurvey.php +++ b/include/limesurvey/admin/importsurvey.php @@ -1282,6 +1282,8 @@ if ($countquota > 0 && (!isset($countquotals) || $countquotals == 0)) { if (isset($conditionsarray) && $conditionsarray) {//ONLY DO THIS IF THERE ARE CONDITIONS! $count='0'; + + foreach ($conditionsarray as $car) { if ($importversion>=111) { @@ -1296,7 +1298,7 @@ if (isset($conditionsarray) && $conditionsarray) {//ONLY DO THIS IF THERE ARE CO } $conditionrowdata=array_combine($fieldorders,$fieldcontents); - $oldcid=$conditionrowdata["cid"]; + $oldcid=$conditionrowdata["cid"]; $oldqid=$conditionrowdata["qid"]; $oldcfieldname=$conditionrowdata["cfieldname"]; $oldcqid=$conditionrowdata["cqid"]; @@ -1321,7 +1323,11 @@ if (isset($conditionsarray) && $conditionsarray) {//ONLY DO THIS IF THERE ARE CO } foreach($fieldnames as $fns) { //if the $fns['oldcfieldname'] is not the same as $fns['oldfieldname'] then this is a multiple type question - if ($fns['oldcfieldname'] == $fns['oldfieldname']) { //The normal method - non multiples + if (stripos($oldcfieldname,'TOKEN') !== false) //detecting tokens + { + $newcfieldname = $oldcfieldname; + } + else if ($fns['oldcfieldname'] == $fns['oldfieldname']) { //The normal method - non multiples if ($oldcfieldname==$fns['oldcfieldname']) { $newcfieldname=$fns['newcfieldname']; } @@ -1332,6 +1338,7 @@ if (isset($conditionsarray) && $conditionsarray) {//ONLY DO THIS IF THERE ARE CO } } if (!isset($newcfieldname)) {$newcfieldname="";} + unset($conditionrowdata["cid"]); $conditionrowdata["qid"]=$newqid; $conditionrowdata["cfieldname"]=$newcfieldname; diff --git a/include/limesurvey/common.php b/include/limesurvey/common.php index 6c8f7b9d..bb6383d1 100644 --- a/include/limesurvey/common.php +++ b/include/limesurvey/common.php @@ -3530,6 +3530,7 @@ function getHeader() { global $embedded, $surveyid, $rooturl,$defaultlang, $js_header_includes, $css_header_includes; + $js_header_includes[] = '/../../js/popup.js'; //queXS addition $js_header_includes = array_unique($js_header_includes); $css_header_includes = array_unique($css_header_includes); @@ -6149,6 +6150,8 @@ function GetTokenConditionsFieldNames($surveyid) { $extra_attrs=GetAttributeFieldNames($surveyid); $basic_attrs=Array('firstname','lastname','email','token','language','sent','remindersent','remindercount'); + $basic_attrs[] = 'callattempts'; //queXS addition + $basic_attrs[] = 'onappointment'; //queXS addition return array_merge($basic_attrs,$extra_attrs); } @@ -6167,6 +6170,8 @@ function GetTokenFieldsAndNames($surveyid, $onlyAttributes=false) } $extra_attrs=GetAttributeFieldNames($surveyid); $basic_attrs=Array('firstname','lastname','email','token','language','sent','remindersent','remindercount'); + $basic_attrs[] = 'callattempts'; //queXS addition + $basic_attrs[] = 'onappointment'; //queXS addition $basic_attrs_names=Array( $clang->gT('First Name'), $clang->gT('Last Name'), @@ -6177,6 +6182,9 @@ function GetTokenFieldsAndNames($surveyid, $onlyAttributes=false) $clang->gT('Last Reminder sent date'), $clang->gT('Total numbers of sent reminders')); + $basic_attrs_names[] = $clang->gT('queXS: Number of call attempts'); //queXS addition + $basic_attrs_names[] = $clang->gT('queXS: On appointment?'); //queXS addition + $thissurvey=getSurveyInfo($surveyid); $attdescriptiondata=!empty($thissurvey['attributedescriptions']) ? $thissurvey['attributedescriptions'] : ""; $attdescriptiondata=explode("\n",$attdescriptiondata); @@ -6228,6 +6236,22 @@ function GetAttributeValue($surveyid,$attrName,$token) $sanitized_token=$connect->qstr($token,get_magic_quotes_gpc()); $surveyid=sanitize_int($surveyid); + if ($attrName == 'callattempts' || $attrName == 'onappointment') //queXS addition + { + include_once("quexs.php"); + $quexs_operator_id = get_operator_id(); + $quexs_case_id = get_case_id($quexs_operator_id); + if ($quexs_case_id) + { + if ($attrName == 'callattempts') + return get_call_attempts($quexs_case_id); + else if ($attrName == 'onappointment') + return is_on_appointment($quexs_case_id,$quexs_operator_id); + } + else + return 0; + } + $query="SELECT $attrName FROM {$dbprefix}tokens_$surveyid WHERE token=$sanitized_token"; $result=db_execute_num($query); $count=$result->RecordCount(); diff --git a/include/limesurvey/group.php b/include/limesurvey/group.php index 6ddac448..aae78f78 100644 --- a/include/limesurvey/group.php +++ b/include/limesurvey/group.php @@ -227,6 +227,13 @@ if ((isset($move) && $move == "movesubmit") && (!isset($notanswered) || !$notan $url=str_replace("{SID}", $surveyid, $url); // to activate the SID in the END URL $url=str_replace("{LANG}", $clang->getlangcode(), $url); // to activate the LANG in the END URL + //queXS Addition + include_once("quexs.php"); + $quexs_url = get_start_interview_url(); + $url = str_replace("{STARTINTERVIEWURL}", $quexs_url, $url); + + + header("Location: {$url}"); } diff --git a/include/limesurvey/qanda.php b/include/limesurvey/qanda.php index 3e2f0ee5..d5f27a4a 100644 --- a/include/limesurvey/qanda.php +++ b/include/limesurvey/qanda.php @@ -961,11 +961,11 @@ function quexs_submit_on_click($do = true) { include_once(dirname(__FILE__) . '/quexs.php'); - $r = ""; + $r = "; $('.submit').css('display', ''); "; if (LIME_AUTO_ADVANCE && $do) { - $r = "; document.limesurvey.move.value = '"; + $r .= " document.limesurvey.move.value = '"; if (isset($_SESSION['step']) && $_SESSION['step'] && ($_SESSION['step'] == $_SESSION['totalsteps'])) $r .= "movesubmit"; else @@ -976,6 +976,35 @@ function quexs_submit_on_click($do = true) return $r; } +function quexs_appointment($do) +{ + include_once(dirname(__FILE__) . '/quexs.php'); + + $r = ""; + + if ($do) + { + $r = "; $('.submit').css('display', 'none'); parent.poptastic('" . QUEXS_URL . "appointment.php'); "; + } + + return $r; +} + +function quexs_outcome($outcome) +{ + include_once(dirname(__FILE__) . '/quexs.php'); + + $r = ""; + + if (is_numeric($outcome) && $outcome != 0) + { + $r = "; $('.submit').css('display', 'none'); parent.poptastic('" . QUEXS_URL . "call.php?defaultoutcome=$outcome'); "; + } + + return $r; + +} + function do_boilerplate($ia) { @@ -2006,7 +2035,31 @@ function do_list_flexible_radio($ia) { $check_ans =''; }; - $answer .= $wrapper['item-start'].' + + //queXS check if this is designed to set an outcome: + $quexs_outcome = false; + $quexs_outcome_code = 0; + if (strncasecmp($ansrow['title'],"{OUTCOME:",9) == 0) + { + $quexs_pos = strrpos($ansrow['title'],"}",8); + if ($quexs_pos != false) + { + $quexs_outcome_code = substr($ansrow['title'],9,$quexs_pos - 9); + $quexs_outcome = true; + include_once(dirname(__FILE__) . '/quexs.php'); + $ansrow['title'] = quexs_template_replace($ansrow['title']); + } + } + + //queXS check if this is designed to schedule an appointment: + $quexs_appointment = false; + if (strncasecmp($ansrow['title'],"{SCHEDULEAPPOINTMENT}",21) == 0) + { + $ansrow['title'] = $clang->gT("Schedule Appointment"); + $quexs_appointment = true; + } + + $answer .= $wrapper['item-start'].' '.$wrapper['item-end']; @@ -3302,6 +3355,23 @@ function do_multipleshorttext($ia) { $myfname = $ia[1].$ansrow['code']; if ($ansrow['answer'] == "") {$ansrow['answer'] = " ";} + + $quexs_answer = false; + + if (strncasecmp($ansrow['answer'],"{SAMPLEUPDATE:",14) == 0) //queXS Addition + { + $ansrow['answer'] = substr($ansrow['answer'],14,-1); //remove token text + include_once('quexs.php'); + $quexs_operator_id = get_operator_id(); + $quexs_case_id = get_case_id($quexs_operator_id); + if ($quexs_case_id) + { + $quexs_answer = get_sample_variable($ansrow['answer'],$quexs_case_id); + $tiwidth = strlen($quexs_answer) + 5; + $maxsize = $tiwidth + 255; + } + } + $answer_main .= "\t
  • \n" . "\t\t\n" . "\t\t\t\n\t\t\t\t".$prefix."\n\t\t\t\t".'getlangcode(), $url); // to activate the LANG in the END URL + //queXS Addition + include_once("quexs.php"); + $quexs_url = get_start_interview_url(); + $url = str_replace("{STARTINTERVIEWURL}", $quexs_url, $url); + header("Location: {$url}"); } diff --git a/include/limesurvey/quexs.php b/include/limesurvey/quexs.php index 4e4dc26e..e8d36193 100644 --- a/include/limesurvey/quexs.php +++ b/include/limesurvey/quexs.php @@ -35,6 +35,214 @@ */ require_once(dirname(__FILE__).'/../../config.inc.php'); +/** + * Return the phone number of the latest appointment for this respondent + * + * @param int $respondent_id The respondent id + * @return string The phone number + */ +function get_appointment_number($respondent_id) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + + $sql = "SELECT CONVERT_TZ(a.start,'UTC',r.Time_zone_name) as start, CONVERT_TZ(a.end,'UTC',r.Time_zone_name) as end, DATE(CONVERT_TZ(a.start,'UTC',r.Time_zone_name)) as startdate, c.phone + FROM appointment as a + JOIN (contact_phone as c, respondent as r) on (a.contact_phone_id = c.contact_phone_id AND r.respondent_id = '$respondent_id') + WHERE a.respondent_id = '$respondent_id' + ORDER BY a.appointment_id DESC"; + + $rs = $db->GetRow($sql); //Get the last one only + + if (!empty($rs)) + return $rs['phone']; + + return ""; +} + + +/** + * Return the time of the latest appointment for this respondent + * + * @param int $respondent_id The respondent id + * @return string The time + */ +function get_appointment_time($respondent_id) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + + $sql = "SELECT DATE_FORMAT(CONVERT_TZ(a.start,'UTC',r.Time_zone_name),'".TIME_FORMAT."') as start + FROM appointment as a + JOIN (contact_phone as c, respondent as r) on (a.contact_phone_id = c.contact_phone_id AND r.respondent_id = '$respondent_id') + WHERE a.respondent_id = '$respondent_id' + ORDER BY a.appointment_id DESC"; + + $rs = $db->GetRow($sql); //Get the last one only + + if (!empty($rs)) + return $rs['start']; + + return ""; +} + + +/** + * Return the date of the latest appointment for this respondent + * + * @param int $respondent_id The respondent id + * @return string The date + */ +function get_appointment_date($respondent_id) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + + $sql = "SELECT DATE_FORMAT(CONVERT_TZ(a.start,'UTC',r.Time_zone_name),'".DATE_FORMAT."') as startdate, c.phone + FROM appointment as a + JOIN (contact_phone as c, respondent as r) on (a.contact_phone_id = c.contact_phone_id AND r.respondent_id = '$respondent_id') + WHERE a.respondent_id = '$respondent_id' + ORDER BY a.appointment_id DESC"; + + $rs = $db->GetRow($sql); //Get the last one only + + if (!empty($rs)) + return $rs['startdate']; + + return ""; +} + + + +/** + * Return the period of the day for the respondent + * + * @param int $respondent_id The respondent id + * @return string Either morning, afternoon or evening based on the respondents time zone + * @todo internationalise text : limesurvey or quexs? + * + */ +function get_period_of_day($respondent_id) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + $sql = "SELECT HOUR(CONVERT_TZ(NOW(),'System',Time_zone_name)) as h + FROM respondent + WHERE respondent_id = '$respondent_id'"; + + $rs = $db->GetRow($sql); + + $hour = 10; + if (!empty($rs)) + $hour = $rs['h']; + + if ($hour < 12) return "morning"; + if ($hour < 17) return "afternoon"; + return "evening"; +} + + +/** + * Update the sample record from data entered in limesurvey form + * + * @param int $lime_sid The limesurvey survey id + * @param int $id The limesurvey record id of the data + * @param array $postedfieldnames An array containing the fields that were just posted by limesurvey + * + */ +function quexs_update_sample($lime_sid,$id,$postedfieldnames) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + //Search over the questionnaire database to find multiple short text responses which reference the sample + $sql = "SELECT lq.sid,lq.gid,lq.qid,la.code,SUBSTR(la.answer,15,CHAR_LENGTH(la.answer)-15) as answer + FROM `lime_questions` AS lq + JOIN `lime_answers` AS la ON ( la.qid = lq.qid ) + WHERE lq.sid = '$lime_sid' + AND lq.type = 'Q' + AND ("; + + foreach($postedfieldnames as $pf) //restrict to only the ones just updated + { + $sql .= " CONCAT(lq.sid, 'X', lq.gid, 'X', lq.qid, la.code) LIKE '$pf' OR "; + } + + $sql = substr($sql,0,-4); + $sql .= ") AND la.answer LIKE '{SAMPLEUPDATE:%'"; + + $rs = $db->GetAll($sql); + + $db->StartTrans(); + + if (!empty($rs)) + { + $operator_id = get_operator_id(); + $case_id = get_case_id($operator_id); + + if ($case_id) + { + $sql = "SELECT sample_id + FROM `case` + WHERE case_id = '$case_id'"; + + $c = $db->GetRow($sql); + + $sample_id = $c['sample_id']; + + foreach($rs as $r) //Update the queXS sample database to reflect the updated data + { + $sgqa = $r['sid'] . 'X' . $r['gid'] . 'X' . $r['qid'] . $r['code']; + $var = $r['answer']; + + $sql = "UPDATE sample_var as sv, ".LIME_PREFIX."survey_$lime_sid as ld + SET sv.val = ld.$sgqa + WHERE sv.var LIKE '$var' + AND sv.sample_id = '$sample_id' + AND ld.id = '$id'"; + + $db->Execute($sql); + } + } + } + + $db->CompleteTrans(); +} + +/** + * Get the number of call attempts for this case + * + * @param $case_id The case id + * @return int The number of call attempts + */ +function get_call_attempts($case_id) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + + $sql = "SELECT COUNT(call_attempt_id) AS c + FROM call_attempt + WHERE case_id = '$case_id'"; + + $rs = $db->GetRow($sql); + + + if (empty($rs)) return ""; + + return $rs['c']; + +} /** @@ -66,6 +274,33 @@ function get_sample_variable($variable,$case_id) } +/** + * Get the outcome code description + * + * @param string $variable The bit of information about the operator (eg firstName) + * @param int $operator_id The operator id + * @return string The information or a blank string if none found + * + */ +function get_outcome_variable($variable) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + + $sql = "SELECT description as r + FROM outcome + WHERE outcome_id = '$variable'"; + + $rs = $db->GetRow($sql); + + if (empty($rs)) return ""; + + return $rs['r']; + +} + /** * Get information about the operator * @@ -241,9 +476,171 @@ function quexs_template_replace($string) $string=str_replace($answreplace, $answreplace3, $string); } + while (stripos($string, "{Outcome:") !== false) + { + $answreplace=substr($string, stripos($string, "{Outcome:"), stripos($string, "}", stripos($string, "{Outcome:"))-stripos($string, "{Outcome:")+1); + $answreplace2=substr($answreplace, 9, stripos($answreplace, "}", stripos($answreplace, "{Outcome:"))-9); + $answreplace3=get_outcome_variable($answreplace2); + $string=str_replace($answreplace, $answreplace3, $string); + } + + while (stripos($string, "{CALLATTEMPTS}") !== false) + { + $call_attempts = get_call_attempts($case_id); + $string=str_ireplace("{CALLATTEMPTS}", $call_attempts, $string); + } + + while (stripos($string, "{ONAPPOINTMENT}") !== false) + { + $on_appointment = is_on_appointment($case_id,$operator_id); + //todo: These must be internationalised, but I think I need to use Limesurveys so as not to conflict? + $str = "Not on an appointment"; + if ($on_appointment) + $str = "On an appointment"; + $string=str_ireplace("{ONAPPOINTMENT}", $str, $string); + } + + if (stripos($string, "{PERIODOFDAY}") !== false) $string=str_ireplace("{PERIODOFDAY}", get_period_of_day($respondent_id), $string); + if (stripos($string, "{APPOINTMENTDATE}") !== false) $string=str_ireplace("{APPOINTMENTDATE}", get_appointment_date($respondent_id), $string); + if (stripos($string, "{APPOINTMENTTIME}") !== false) $string=str_ireplace("{APPOINTMENTTIME}", get_appointment_time($respondent_id), $string); + if (stripos($string, "{APPOINTMENTNUMBER}") !== false) $string=str_ireplace("{APPOINTMENTNUMBER}", get_appointment_number($respondent_id), $string); + return $string; } +/** + * Get the limesurvey "survey id" of the current questionnaire assigned to the operator + * + * @param int $operator_id The operator + * @return bool|int False if none found else the limesurvey sid + * + */ +function get_limesurvey_id($operator_id) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + $sql = "SELECT q.lime_sid as lime_sid + FROM `case` as c, questionnaire_sample as qs, sample as s, questionnaire as q + WHERE c.current_operator_id = '$operator_id' + AND c.sample_id = s.sample_id + AND s.import_id = qs.sample_import_id + AND q.questionnaire_id = qs.questionnaire_id + AND c.questionnaire_id = q.questionnaire_id"; + + $rs = $db->GetRow($sql); + + if (empty($rs)) + return false; + + return $rs['lime_sid']; + +} + + +/** + * Return the current questionnaire assigned to the operator + * false if none + * + * @param int $operator_id The operator id + * @return bool|int False if none otherwise the questionnare id + * + */ +function get_questionnaire_id($operator_id) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + + $sql = "SELECT questionnaire_id + FROM `case` as c + WHERE c.current_operator_id = '$operator_id'"; + + $rs = $db->GetRow($sql); + + if (empty($rs)) return false; + + return $rs['questionnaire_id']; + +} + + +/** + * Get start interviewer URL + * + * @return string The URL to start the interview + */ +function get_start_interview_url() +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + $operator_id = get_operator_id(); + + $url = QUEXS_URL . "nocaseavailable.php"; + + $case_id = get_case_id($operator_id); + + if ($case_id) + { + $sid = get_limesurvey_id($operator_id); + $url = LIME_URL . "index.php?loadall=reload&sid=$sid&token=$case_id&lang=" . DEFAULT_LOCALE; + $questionnaire_id = get_questionnaire_id($operator_id); + + //get prefills + $sql = "SELECT lime_sgqa,value + FROM questionnaire_prefill + WHERE questionnaire_id = '$questionnaire_id'"; + $pf = $db->GetAll($sql); + + if (!empty($pf)) + { + foreach ($pf as $p) + $url .= "&" . $p['lime_sgqa'] . "=" . quexs_template_replace($p['value']); + } + } + + //if ($db->HasFailedTrans()) { print "FAILED in get_limesurvey_url"; exit; } + $db->CompleteTrans(); + + return $url; + + +} + + +/** + * Return 1 if this operator is on an appointment + * + * @param int $case_id The case id + * @param int $operator_id The operator id + * @return int 0 if not on appointment 1 if they are + * + */ +function is_on_appointment($case_id,$operator_id) +{ + $db = newADOConnection(DB_TYPE); + $db->Connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + $db->SetFetchMode(ADODB_FETCH_ASSOC); + + $sql = "SELECT a.appointment_id + FROM call_attempt as ca + LEFT JOIN appointment as a on (a.case_id = ca.case_id and (ca.start >= a.start and ca.start <= a.end) and a.completed_call_id is NULL) + WHERE ca.case_id = '$case_id' + AND ca.operator_id = '$operator_id' + AND ca.end IS NULL"; + + $a = $db->GetRow($sql); + + if (empty($a) || empty($a['appointment_id'])) + return 0; + else + return 1; + +} ?> diff --git a/include/limesurvey/save.php b/include/limesurvey/save.php index 3d39b1b2..0b8e62a2 100644 --- a/include/limesurvey/save.php +++ b/include/limesurvey/save.php @@ -117,12 +117,19 @@ if ((isset($_POST['fieldnames']) && $_POST['fieldnames']) || (isset($_POST['move { if ($result=$connect->Execute($subquery)) // Checked { + if (substr($subquery,0,6)=='INSERT') { $tempID=$connect->Insert_ID($thissurvey['tablename'],"id"); // Find out id immediately if inserted $_SESSION['srid'] = $tempID; $saved_id = $tempID; } + + //queXS addition + include_once('quexs.php'); + global $surveyid; + quexs_update_sample($surveyid,$saved_id,$postedfieldnames); + if ($bFinalizeThisAnswer === true) { $connect->Execute("DELETE FROM ".db_table_name("saved_control")." where srid=".$_SESSION['srid'].' and sid='.$surveyid); // Checked diff --git a/include/limesurvey/survey.php b/include/limesurvey/survey.php index 4454c442..becdee86 100644 --- a/include/limesurvey/survey.php +++ b/include/limesurvey/survey.php @@ -183,6 +183,12 @@ if ((isset($move) && $move == "movesubmit") && (!isset($notanswered) || !$notans $url=str_replace("{TOKEN}",$clienttoken, $url); // to activate the TOKEN in the END URL $url=str_replace("{SID}", $surveyid, $url); // to activate the SID in the END URL $url=str_replace("{LANG}", $clang->getlangcode(), $url); // to activate the LANG in the END URL + + //queXS Addition + include_once("quexs.php"); + $quexs_url = get_start_interview_url(); + $url = str_replace("{STARTINTERVIEWURL}", $quexs_url, $url); + //Automatically redirect the page to the "url" setting for the survey session_write_close(); diff --git a/index.php b/index.php index b6be6cda..5c19c038 100644 --- a/index.php +++ b/index.php @@ -103,7 +103,7 @@ xhtml_head(T_("queXS"), true, array("css/index.css","css/tabber.css","include/jq
  • -" standby="Loading questionnaire..." type="application/xhtml+xml">

    Error, try with Firefox

    +

    Error, try with Firefox

    diff --git a/js/new.js b/js/new.js index 5e1f867b..785377df 100644 --- a/js/new.js +++ b/js/new.js @@ -11,3 +11,15 @@ function showHide(me,id) } } + +function show(me,id) +{ + e = document.getElementById(id); + e.style.display = 'inline'; + +} +function hide(me,id) +{ + e = document.getElementById(id); + e.style.display = 'none'; +}