2
0
mirror of https://github.com/ACSPRI/queXS synced 2024-04-02 12:12:16 +00:00

Merged Ability to create new cases as a referral from an existing case feature

Also improves timezone detection
This commit is contained in:
Adam Zammit
2014-01-08 15:20:41 +11:00
8 changed files with 435 additions and 128 deletions

View File

@@ -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<br/>");
//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;
}
?>

View File

@@ -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
{