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

New feature: Seperate limesurvey instrument for respondent selection

Dev Merged back changes from mcmasterlimers branch
This commit is contained in:
azammitdcarf
2010-09-29 23:19:08 +00:00
parent 19143de1c0
commit 0ef7c93737
18 changed files with 674 additions and 33 deletions

View File

@@ -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;
}
?>