diff --git a/admin/questionnairelist.php b/admin/questionnairelist.php
index 46f861cc..b8a01b0f 100644
--- a/admin/questionnairelist.php
+++ b/admin/questionnairelist.php
@@ -225,9 +225,11 @@ if (isset($_POST['update']) && isset($_GET['modify']))
$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']))
@@ -243,7 +245,7 @@ if (isset($_POST['update']) && isset($_GET['modify']))
$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
+ 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);
@@ -303,7 +305,7 @@ if (isset($_GET['modify']))
$rs = $db->GetRow($sql);
- $testing = $rws = $ras = $rsc = "checked=\"checked\"";
+ $referral = $testing = $rws = $ras = $rsc = "checked=\"checked\"";
$rscd = "";
$aio = $qbq = $gat = "";
@@ -315,6 +317,7 @@ if (isset($_GET['modify']))
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 = "";
@@ -330,6 +333,7 @@ if (isset($_GET['modify']))
>
:
diff --git a/database/quexs.sql b/database/quexs.sql
index d5962156..3893f4a8 100644
--- a/database/quexs.sql
+++ b/database/quexs.sql
@@ -1384,6 +1384,7 @@ CREATE TABLE `questionnaire` (
`lime_rs_sid` int(11) default NULL,
`info` text collate utf8_unicode_ci,
`self_complete` tinyint(1) NOT NULL default '0',
+ `referral` TINYINT( 1 ) NOT NULL DEFAULT '0',
`lime_mode` varchar(64) collate utf8_unicode_ci default NULL COMMENT 'Limesurvey mode for respondent self completion',
`lime_template` varchar(128) collate utf8_unicode_ci default NULL COMMENT 'Limesurvey template for respondent self completion',
`lime_endurl` varchar(256) collate utf8_unicode_ci default NULL COMMENT 'Forwarding end URL for respondent self completion',
diff --git a/functions/functions.import.php b/functions/functions.import.php
index 622dc986..92fe97cb 100644
--- a/functions/functions.import.php
+++ b/functions/functions.import.php
@@ -257,7 +257,10 @@ function import_file($file, $description, $fields, $firstrow = 2)
{
$dkey = only_numbers($data[$key - 1]);
if (!empty($dkey))
+ {
+ $data[$key - 1] = $dkey;
$numberavail = 1;
+ }
}
}
@@ -274,38 +277,14 @@ function import_file($file, $description, $fields, $firstrow = 2)
*
*/
foreach($selected_type as $key => $val)
- {
- $sql = "SELECT `table`
- FROM sample_var_type
- WHERE type = '$val'";
-
- $tname = $db->GetRow($sql);
-
- if (!empty($tname))
+ {
+ $tz = get_time_zone($data[$key - 1],$val);
+
+ if ($tz !== false)
{
- $tname = $tname['table'];
- if (!empty($tname))
- {
-
- $value = $db->Quote(only_numbers($data[$key - 1]));
-
- $sql = "SELECT Time_zone_name as tz
- FROM `$tname`
- WHERE val = SUBSTR($value, 1, CHAR_LENGTH( val ) )";
-
- $tz = $db->GetRow($sql);
-
- //print("$sql
");
- //if ($db->HasFailedTrans()) { print "FAILED"; exit(); }
-
- if (!empty($tz))
- {
- $tzone = $tz['tz'];
- break;
- }
-
- }
- }
+ $tzone = $tz;
+ break;
+ }
}
@@ -353,5 +332,42 @@ function import_file($file, $description, $fields, $firstrow = 2)
}
+/**
+ * Get the timezone given the sample value and type
+ *
+ * @param string $value A sample value
+ * @param integer $type The type of sample var (see sample_var_type table)
+ *
+ * @return string|bool Return the timezone name or false if not found
+ */
+function get_time_zone($value,$type)
+{
+ global $db;
+
+ $sql = "SELECT `table`
+ FROM sample_var_type
+ WHERE type = '$type'";
+
+ $tname = $db->GetOne($sql);
+
+ if (!empty($tname))
+ {
+ $value = $db->Quote($value);
+
+ $sql = "SELECT Time_zone_name as tz
+ FROM `$tname`
+ WHERE val = SUBSTR($value, 1, CHAR_LENGTH( val ) )
+ ORDER BY CHAR_LENGTH(val) DESC";
+
+ $tz = $db->GetOne($sql);
+
+ if (!empty($tz))
+ {
+ return $tz;
+ }
+ }
+ return false;
+}
+
?>
diff --git a/functions/functions.operator.php b/functions/functions.operator.php
index 88b5cdb9..6d68dd60 100644
--- a/functions/functions.operator.php
+++ b/functions/functions.operator.php
@@ -338,6 +338,117 @@ function is_respondent_selection($operator_id)
return false;
}
+/**
+ * Add a case to the system based on a sample record
+ *
+ * @param int $sample_id The sample 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
+ *
+ * @return int The case id
+ */
+function add_case($sample_id,$questionnaire_id,$operator_id = "NULL",$testing = 0)
+{
+ 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')";
+
+ $db->Execute($sql);
+
+ $case_id = $db->Insert_ID();
+
+ //if this sample is set as testing, assign internal numbers as numbers
+ if ($testing == 1)
+ {
+ $db->Execute("SET @row := 0");
+
+ $sql = "INSERT INTO contact_phone (case_id,priority,phone,description)
+ SELECT $case_id as case_id,@row := @row + 1 AS priority,SUBSTRING_INDEX(e.extension,'/',-1) as phone, CONCAT(o.firstName, ' ', o.lastName)
+ FROM operator as o, `extension` as e
+ WHERE o.enabled = 1
+ AND e.current_operator_id = o.operator_id";
+
+ $db->Execute($sql);
+ }
+ else
+ {
+ //add any phone numbers to contact phone
+
+ //$db->Execute("SET @row := 0");
+
+ $sql = "SELECT val as phone
+ FROM sample_var
+ WHERE sample_id = '$sample_id'
+ AND val is NOT NULL
+ AND val != \"\"
+ AND (`type` = 2 or `type` = 3)
+ ORDER BY `type` DESC";
+
+ $r5 = $db->GetAll($sql);
+
+ if (!empty($r5))
+ {
+ $i = 1;
+ foreach ($r5 as $r5v)
+ {
+ $tnum = preg_replace("/[^0-9]/", "",$r5v['phone']);
+ if (empty($tnum)) $tnum = "312345678"; //handle error condition
+ $sql = "INSERT INTO contact_phone (case_id,priority,phone,description)
+ VALUES ($case_id,$i,$tnum,'')";
+ $db->Execute($sql);
+ $i++;
+ }
+ }
+ else
+ {
+ $sql = "INSERT INTO contact_phone (case_id,priority,phone,description)
+ VALUES ($case_id,1,312345678,'test only')";
+ $db->Execute($sql);
+ }
+
+ }
+
+ //add respondent details to respondent (if such details exist in the sample)
+
+ $sql = "INSERT INTO respondent (case_id,firstName,lastName,Time_zone_name)
+ SELECT $case_id as case_id, IFNULL(s1.val,'') as firstName, IFNULL(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 = '$sample_id' and s2.type = 7)
+ LEFT JOIN sample_var as s1 on (s1.sample_id = '$sample_id' and s1.type = 6)
+ WHERE s3.sample_id = '$sample_id'";
+
+ $db->Execute($sql);
+
+
+ //add resopndent to Lime Survey token table for this questionnaire
+
+ //first we need to get the limesurvey survey id
+
+ if (!$db->HasFailedTrans()) //if the transaction hasn't failed
+ {
+ $sql = "SELECT lime_sid
+ FROM questionnaire
+ WHERE questionnaire_id = '$questionnaire_id'";
+
+ $lime_sid = $db->GetOne($sql);
+
+ if ($lime_sid)
+ {
+ $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)";
+
+ $db->Execute($sql);
+ }
+ }
+
+ return $case_id;
+}
+
+
/**
* Get the current or next case id
*
@@ -603,95 +714,8 @@ function get_case_id($operator_id, $create = false)
if (!empty($r3))
{
- $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, {$r3['sample_id']}, {$r3['questionnaire_id']} , NULL, $operator_id, NULL, 1, '$token')";
-
- $db->Execute($sql);
-
- $case_id = $db->Insert_ID();
-
- //if this sample is set as testing, assign internal numbers as numbers
- if ($r3['testing'] == 1)
- {
- $db->Execute("SET @row := 0");
-
- $sql = "INSERT INTO contact_phone (case_id,priority,phone,description)
- SELECT $case_id as case_id,@row := @row + 1 AS priority,SUBSTRING_INDEX(o.extension,'/',-1) as phone, CONCAT(o.firstName, ' ', o.lastName)
- FROM operator as o, `extension` as e
- WHERE o.enabled = 1
- AND e.current_operator_id = o.operator_id";
-
- $db->Execute($sql);
- }
- else
- {
- //add any phone numbers to contact phone
-
- //$db->Execute("SET @row := 0");
-
- $sql = "SELECT val as phone
- FROM sample_var
- WHERE sample_id = '{$r3['sample_id']}'
- AND val is NOT NULL
- AND val != \"\"
- AND (`type` = 2 or `type` = 3)
- ORDER BY `type` DESC";
-
- $r5 = $db->GetAll($sql);
-
- if (!empty($r5))
- {
- $i = 1;
- foreach ($r5 as $r5v)
- {
- $tnum = preg_replace("/[^0-9]/", "",$r5v['phone']);
- if (empty($tnum)) $tnum = "312345678"; //handle error condition
- $sql = "INSERT INTO contact_phone (case_id,priority,phone,description)
- VALUES ($case_id,$i,$tnum,'')";
- $db->Execute($sql);
- $i++;
- }
- }
- else
- {
- $sql = "INSERT INTO contact_phone (case_id,priority,phone,description)
- VALUES ($case_id,1,312345678,'test only')";
- $db->Execute($sql);
- }
-
- }
-
- //add respondent details to respondent (if such details exist in the sample)
-
- $sql = "INSERT INTO respondent (case_id,firstName,lastName,Time_zone_name)
- SELECT $case_id as case_id, IFNULL(s1.val,'') as firstName, IFNULL(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']}'";
-
- $db->Execute($sql);
-
-
- //add resopndent to Lime Survey token table for this questionnaire
-
- //first we need to get the limesurvey survey id
-
- if (!$db->HasFailedTrans()) //if the transaction hasn't failed
- {
- $lime_sid = get_limesurvey_id($operator_id);
-
- if ($lime_sid)
- {
- $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)";
-
- $db->Execute($sql);
- }
- }
- }
+ $case_id = add_case($r3['sample_id'],$r3['questionnaire_id'],$operator_id,$r3['testing']);
+ }
}
else
{
diff --git a/index.php b/index.php
index 0f488633..e9bc0a0d 100644
--- a/index.php
+++ b/index.php
@@ -147,12 +147,14 @@ print $script;
$case_id = get_case_id($operator_id,true);
-$sql = "SELECT q.self_complete
+$sql = "SELECT q.self_complete as sc,q.referral as r
FROM questionnaire as q, `case` as c
WHERE c.case_id = $case_id
AND c.questionnaire_id = q.questionnaire_id";
-$sc = $db->GetOne($sql);
+$scr = $db->GetRow($sql);
+$sc = $scr['sc'];
+$ref = $scr['r'];
?>
@@ -162,6 +164,7 @@ $sc = $db->GetOne($sql);
+

'/>
diff --git a/index_interface2.php b/index_interface2.php
index b40ca4fa..ff582265 100644
--- a/index_interface2.php
+++ b/index_interface2.php
@@ -197,12 +197,14 @@ print $script;
$case_id = get_case_id($operator_id,true);
-$sql = "SELECT q.self_complete
+$sql = "SELECT q.self_complete, q.referral
FROM questionnaire as q, `case` as c
WHERE c.case_id = $case_id
AND c.questionnaire_id = q.questionnaire_id";
-$sc = $db->GetOne($sql);
+$scr = $db->GetRow($sql);
+$sc = $scr['self_complete'];
+$ref = $scr['referral'];
?>
@@ -210,6 +212,7 @@ $sc = $db->GetOne($sql);

+

diff --git a/referral.php b/referral.php
new file mode 100644
index 00000000..4cb99cf2
--- /dev/null
+++ b/referral.php
@@ -0,0 +1,253 @@
+
+ * @copyright Australian Consortium for Social and Political Research Incorporated (ACSPRI) 2013
+ * @package queXS
+ * @subpackage user
+ * @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");
+
+/**
+ * Operator functions
+ */
+include("functions/functions.operator.php");
+
+/**
+ * Input functions
+ */
+include("functions/functions.input.php");
+
+/**
+ * Import functions
+ */
+include("functions/functions.import.php");
+
+global $db;
+
+$operator_id = get_operator_id();
+
+$msg = "";
+
+if (isset($_POST['submit']))
+{
+ $case_id = get_case_id($operator_id);
+
+ $sql = "SELECT s.var
+ FROM sample_var as s, `case` as c
+ WHERE c.case_id = '$case_id'
+ AND s.sample_id = c.sample_id
+ AND s.type = 3";
+
+ $pphone = $db->GetOne($sql);
+
+ //validate primary phone number supplied
+ if (isset($_POST["v_$pphone"]) && only_numbers($_POST["v_$pphone"]) != "")
+ {
+ $phone = $db->qstr(only_numbers($_POST["v_$pphone"]));
+
+ //Create a new sample record and add CASEREFERREDFROM to be this current case id
+ $db->StartTrans();
+
+ //create a new sample entry
+ $sql = "SELECT s.import_id
+ FROM `sample` as s, `case` as c
+ WHERE c.case_id = '$case_id'
+ AND s.sample_id = c.sample_id";
+
+ $import_id = $db->GetOne($sql);
+
+ //get all sample records
+ $sql = "SELECT s.var,s.val, s.type
+ FROM sample_var as s, `case` as c
+ WHERE c.case_id = '$case_id'
+ AND s.sample_id = c.sample_id";
+
+ $rs = $db->GetAll($sql);
+
+ $tzone = DEFAULT_TIME_ZONE; //set this to default
+
+ //Get the timezone
+ foreach($rs as $r)
+ {
+ $tz = get_time_zone($_POST["v_" . $r['var']],$r['type']);
+ if ($tz !== false)
+ {
+ $tzone = $tz;
+ break;
+ }
+ }
+
+ $sql = "INSERT INTO `sample` (import_id,Time_zone_name,phone)
+ VALUES ($import_id,'$tzone',$phone)";
+
+ $db->Execute($sql);
+
+ $sample_id = $db->Insert_ID();
+
+ //insert sample var records
+ foreach($rs as $r)
+ {
+ $sql = "INSERT INTO `sample_var` (`sample_id`,`var`,`val`,`type`)
+ VALUES ('$sample_id','{$r['var']}'," . $db->qstr($_POST['v_' . $r['var']]) . ",'{$r['type']}')";
+ $db->Execute($sql);
+ }
+
+ //Add CASEREFERREDFROM record
+ $sql = "INSERT INTO `sample_var` (`sample_id`,`var`,`val`,`type`)
+ VALUES ('$sample_id','CASEREFERREDFROM','$case_id','1')";
+
+ $db->Execute($sql);
+
+ //Create a new case
+ $sql = "SELECT questionnaire_id
+ FROM `case`
+ WHERE case_id = '$case_id'";
+
+ $questionnaire_id = $db->GetOne($sql);
+
+ $ncase_id = add_case($sample_id,$questionnaire_id);
+
+ //If selected to call now - assign to this operator
+ if (isset($_POST['makecase']))
+ {
+ $sql = "SELECT MAX(sortorder)
+ FROM case_queue
+ WHERE operator_id = '$operator_id'";
+
+ $sortorder = $db->GetOne($sql);
+
+ $sortorder++;
+
+ $sql = "INSERT INTO case_queue (case_id,operator_id,sortorder)
+ VALUES ('$ncase_id', '$operator_id', '$sortorder')";
+
+ $db->Execute($sql);
+
+ }
+
+ //Add a note that we have referred another case
+ $sql = "INSERT INTO `case_note` (case_id,operator_id,note,datetime)
+ VALUES ($case_id,$operator_id,'" . T_("Generated referral to case id") . ": $ncase_id',CONVERT_TZ(NOW(),'System','UTC'))";
+
+ $db->Execute($sql);
+
+ //Add a note that it is referred from another case
+ $sql = "INSERT INTO `case_note` (case_id,operator_id,note,datetime)
+ VALUES ($ncase_id,$operator_id,'" . T_("Generated as referral from case id") . ": $case_id',CONVERT_TZ(NOW(),'System','UTC'))";
+
+ $db->Execute($sql);
+
+ if ($db->CompleteTrans())
+ {
+ $msg = T_("Created referral case - you may now close this window");
+ }
+ else
+ {
+ $msg = T_("Failed to create referral case - please check your input and try again");
+ }
+
+ }
+ else
+ {
+ $msg = T_("You must supply a primary phone number");
+ }
+}
+
+$case_id = get_case_id($operator_id);
+
+$js = "js/window.js";
+if (browser_ie()) $js = "js/window_ie6.js";
+
+xhtml_head(T_("Referral"),true,array("css/call.css"),array($js));
+
+$sql = "SELECT q.referral
+ FROM questionnaire as q, `case` as c
+ WHERE c.case_id = $case_id
+ AND c.questionnaire_id = q.questionnaire_id";
+
+$sc = $db->GetOne($sql);
+
+if ($sc == 1)
+{
+ print "
" . T_("Create referral") . "
";
+ if (!empty($msg)) print "
$msg
";
+ print "
";
+}
+else
+{
+ print "
" . T_("Referrals not available for this questionnaire") . "
";
+}
+
+xhtml_foot();
+
+?>