From 552ca4cb6b7c2fdce57e7080dc205bc71356632f Mon Sep 17 00:00:00 2001 From: Adam Zammit Date: Wed, 11 Aug 2021 11:49:11 +1000 Subject: [PATCH] New Feature: Search for opt outs in LimeSurvey and mark as refused on queXS using system sort process Fixed Bug: LimeSurvey RPC functions memory leak Fixed Bug: If participants already email invited, couldn't be sent an email again using queXS interface (now will send reminder) --- CHANGELOG | 4 + admin/systemsortprocess.php | 62 +++++++++++++- database/quexs.sql | 1 + functions/functions.limesurvey.php | 127 +++++++++++++++++++++-------- 4 files changed, 157 insertions(+), 37 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 53b87c02..08291cd1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ + + +INSERT INTO `outcome` (`outcome_id`, `aapor_id`, `description`, `default_delay_minutes`, `outcome_type_id`, `tryanother`, `contacted`, `tryagain`, `eligible`, `require_note`, `calc`, `default`, `permanent`) VALUES(46, '2.112b', 'Opted out online', 10080, 3, 0, 1, 1, 1, 1, 'R', 1, 1); + queXS 2.3.1 - Changes since 2.3.0 Fixed bug: Remove deprecated functions for php 7.4F diff --git a/admin/systemsortprocess.php b/admin/systemsortprocess.php index 0a5e37b8..53fa7d2d 100644 --- a/admin/systemsortprocess.php +++ b/admin/systemsortprocess.php @@ -80,7 +80,6 @@ function disable_systemsort() set_setting('systemsort',false); } - //get the arguments from the command line (this process_id) if ($argc != 2) exit(); @@ -105,6 +104,13 @@ while (!is_process_killed($process_id)) //check if process killed every $sleepin //Make sure that the system knows we are system sorting set_setting('systemsort',true); + $rs = ""; + $qs = ""; + $lcompleted = ""; + $loptout = ""; + + + if ($closecasescounter == 0 || $closecasescounter > $closecasesinterval) { $time_start = microtime(true); @@ -275,6 +281,59 @@ while (!is_process_killed($process_id)) //check if process killed every $sleepin } } + $lcompleted = false; +unset($lcompleted); + + $loptout = lime_get_optout_by_questionnaire($questionnaire_id); + + if ($loptout !== false) { + $sql = "SELECT c.token, c.token as tok, c.case_id as case_id + FROM `case` as c + WHERE c.questionnaire_id = $questionnaire_id + AND c.current_outcome_id not in (10,46)"; + + $rs = $db->GetAssoc($sql); + + foreach($loptout as $l) { + if (isset($l['token']) && isset($rs[$l['token']])) { + //not already completed in queXS but is in Limesurvey + //insert a case and call record + $resp_id = 0; + $case_id = $rs[$l['token']]['case_id']; + + $sql = "SELECT respondent_id + FROM respondent + WHERE case_id = $case_id"; + $rsp = $db->GetOne($sql); + + if (!empty($rsp)) { + $resp_id = $rsp; + } + + $sql = "INSERT INTO call_attempt (case_id,operator_id,respondent_id,start,end) + VALUES ($case_id, 1, $resp_id, CONVERT_TZ(NOW(),'System','UTC'), CONVERT_TZ(NOW(),'System','UTC'))"; + $db->Execute($sql); + + $call_attempt_id = $db->Insert_ID(); + + $sql = "INSERT INTO `call` (operator_id,respondent_id,case_id,contact_phone_id,call_attempt_id,start,end,outcome_id,state) + VALUES (1,$resp_id,$case_id,0,$call_attempt_id,CONVERT_TZ(NOW(),'System','UTC'),CONVERT_TZ(NOW(),'System','UTC'),46,5)"; //opted out online + $db->Execute($sql); + + $call_id = $db->Insert_ID(); + + $sql = "UPDATE `case` + SET last_call_id = $call_id, current_outcome_id = 46 + WHERE case_id = $case_id"; + + $db->Execute($sql); + + print T_("Updating case as opted out online") . " - " . T_("Case id") . ": $case_id"; + } + } + } + $loptout = false; + //Delete all completed call attempts with no call in them $sql = "SELECT ca.call_attempt_id FROM call_attempt as ca JOIN `case` as cs ON (cs.case_id = ca.case_id and cs.questionnaire_id = '$questionnaire_id') @@ -360,7 +419,6 @@ while (!is_process_killed($process_id)) //check if process killed every $sleepin $i++; } - //First set all sample records as unavailable $sql = "UPDATE `questionnaire_sample_exclude_priority` SET sortorder = NULL diff --git a/database/quexs.sql b/database/quexs.sql index 737d973c..e7862751 100644 --- a/database/quexs.sql +++ b/database/quexs.sql @@ -504,6 +504,7 @@ INSERT INTO `outcome` (`outcome_id`, `aapor_id`, `description`, `default_delay_m INSERT INTO `outcome` (`outcome_id`, `aapor_id`, `description`, `default_delay_minutes`, `outcome_type_id`, `tryanother`, `contacted`, `tryagain`, `eligible`, `require_note`, `calc`, `default`, `permanent`) VALUES(43, '3.90', 'Max calls reached (Unknown eligibility)', 0, 1, 0, 1, 1, 0, 0, 'UH', 1, 0); INSERT INTO `outcome` (`outcome_id`, `aapor_id`, `description`, `default_delay_minutes`, `outcome_type_id`, `tryanother`, `contacted`, `tryagain`, `eligible`, `require_note`, `calc`, `default`, `permanent`) VALUES(44, '2.30', 'Max call attempts reached (Eligible)', 0, 1, 0, 1, 1, 1, 0, 'O', 1, 0); INSERT INTO `outcome` (`outcome_id`, `aapor_id`, `description`, `default_delay_minutes`, `outcome_type_id`, `tryanother`, `contacted`, `tryagain`, `eligible`, `require_note`, `calc`, `default`, `permanent`) VALUES(45, '2.30', 'Max calls reached (Eligible)', 0, 1, 0, 1, 1, 1, 0, 'O', 1, 0); +INSERT INTO `outcome` (`outcome_id`, `aapor_id`, `description`, `default_delay_minutes`, `outcome_type_id`, `tryanother`, `contacted`, `tryagain`, `eligible`, `require_note`, `calc`, `default`, `permanent`) VALUES(46, '2.112b', 'Opted out online', 10080, 3, 0, 1, 1, 1, 1, 'R', 1, 1); -- Auto increment start from 100 for manual entries diff --git a/functions/functions.limesurvey.php b/functions/functions.limesurvey.php index 0b0826a3..93f0feca 100644 --- a/functions/functions.limesurvey.php +++ b/functions/functions.limesurvey.php @@ -50,24 +50,47 @@ include_once(dirname(__FILE__).'/../include/JsonRPCClient.php'); $limeRPC = ""; $limeKey = ""; +$limeTime = 0; function limerpc_init ($url,$user,$pass) { global $limeRPC; global $limeKey; + global $limeTime; - $limeRPC = new \org\jsonrpcphp\JsonRPCClient($url); + if ($limeKey == "" || (time() - $limeTime) > 600) { + if ($limeKey != "") { + limerpc_close(); + } + $limeTime = time(); - try { - $limeKey = $limeRPC->get_session_key($user,$pass); - } catch (Exception $e) { - die($e->getMessage()); + $limeRPC = new \org\jsonrpcphp\JsonRPCClient($url); + + $connected = true; + + try { + $limeKey = $limeRPC->get_session_key($user,$pass); + $connected = true; + } catch (Exception $e) { + $connected = false; + //die($e->getMessage()); + } + + if (!$connected) { + usleep(100000); + try { + $limeKey = $limeRPC->get_session_key($user,$pass); + } catch (Exception $e) { + die($e->getMessage()); + } + + } + + if (is_array($limeKey) && isset($limeKey['status'])) { + die($limeKey['status']); + } } - - if (is_array($limeKey) && isset($limeKey['status'])) { - die($limeKey['status']); - } - return true; +return true; } function limerpc_close() @@ -77,6 +100,8 @@ function limerpc_close() if (!empty($limeRPC) && !empty($limeKey)) $limeRPC->release_session_key($limeKey); + + $limeKey = ""; } function limerpc_init_qid($qid) @@ -115,7 +140,6 @@ function lime_list_questions($qid) } } - limerpc_close(); return $ret; } @@ -144,7 +168,6 @@ function lime_list_answeroptions($qid,$qcode) } } - limerpc_close(); return $ret; } @@ -168,18 +191,25 @@ function lime_send_email($case_id,$email,$firstname,$lastname) $ret = false; - if ($lime_id !== false) { - $q = $limeRPC->set_participant_properties($limeKey,$lime_id,array('token' => $token),array('firstname' => $firstname, 'email' => $email, 'lastname' => $lastname)); - if (!isset($q['status'])) { - //send email - $q2 = $limeRPC->invite_participants($limeKey, $lime_id, array($q['tid'])); - if (!isset($q['status'])) { - $ret = true; - } - } - } - limerpc_close(); + if ($lime_id !== false) { + $q = $limeRPC->set_participant_properties($limeKey,$lime_id,array('token' => $token),array('firstname' => $firstname, 'email' => $email, 'lastname' => $lastname)); + if (!isset($q['status'])) { + //send email + $q2 = $limeRPC->invite_participants($limeKey, $lime_id, array($q['tid'])); + if (isset($q2['status']) && $q2['status'] == "0 left to send") { + $ret = true; + } else { + //try reminding + $q3= $limeRPC->remind_participants($limeKey, $lime_id, null, null, array($q['tid'])); + if (isset($q3['status']) && $q3['status'] == "0 left to send") { + $ret = true; + } + } + } + } + + return $ret; } @@ -210,7 +240,6 @@ function lime_set_token_properties($case_id,$params = array('emailstatus' => 'Op } } - limerpc_close(); return $ret; } @@ -244,13 +273,34 @@ function lime_get_responses_by_case($case_id,$fields = null) } } - limerpc_close(); return $ret; } +function lime_get_optout_by_questionnaire($qid) +{ + global $limeRPC; + global $limeKey; + + $lime_id = limerpc_init_qid($qid); + + $ret = false; + + if ($lime_id !== false) { + $q = $limeRPC->list_participants($limeKey,$lime_id,0,10000000,true,array('emailstatus'),array('emailstatus'=>'OptOut')); + if (!isset($q['status'])) { + $ret = $q; + } + } + + return $ret; + + +} + + /** Get completd responses as an array based on the questionnaire * indexed by token */ @@ -270,7 +320,6 @@ function lime_get_responses_by_questionnaire($qid,$fields = null) } } - limerpc_close(); return $ret; } @@ -303,7 +352,6 @@ function lime_get_token_attributes($qid) } } - limerpc_close(); return $ret; } @@ -316,13 +364,25 @@ function lime_add_token($qid,$params) $lime_id = limerpc_init_qid($qid); if ($lime_id !== false) { - $l = $limeRPC->add_participants($limeKey,$lime_id,array($params),false); //don't create token - if (!isset($l['status'])) { - $ret = $l; //array of data - } + //check if record with token already exists + try { + $l = $limeRPC->get_participant_properties($limeKey,$lime_id,array('token'=>$params['token']),array('tid')); + } catch (Exception $e) { + usleep(100000); + $l = $limeRPC->get_participant_properties($limeKey,$lime_id,array('token'=>$params['token']),array('tid')); + + } + if (isset($l['tid'])) { + $ret = $l['tid']; + } else { + $l = $limeRPC->add_participants($limeKey,$lime_id,array($params),false); //don't create token + if (!isset($l['status'])) { + $ret = $l; //array of data + } + } } - limerpc_close(); + return $ret; } @@ -341,7 +401,6 @@ function get_token_value($questionnaire_id,$token, $value = 'sent') } } - limerpc_close(); return $ret; } @@ -376,7 +435,6 @@ function get_survey_list () } } } - limerpc_close(); } } @@ -735,7 +793,6 @@ function get_lime_id($case_id) } } - limerpc_close(); return $ret; }