From ae5c2f8431825b9b97ba02557b2a88e73ef0e79d Mon Sep 17 00:00:00 2001 From: Adam Zammit Date: Mon, 25 Feb 2013 15:25:08 +1100 Subject: [PATCH] Added email to respondent popup Changed id's of self completion outcomes to allow for a few custom ones to not effect Added email validation and token getting functions Optout function of limesurvey to set outcome to refused and add a case note for clarity Need to do: Complete email function to actually send an email invitation --- CHANGELOG | 4 +- call.php | 9 +- email.php | 156 ++++++++++++++++++++++ functions/functions.limesurvey.php | 208 +++++++++++++++++++++++++++++ functions/functions.operator.php | 25 ++++ include/limesurvey/optout.php | 23 ++++ index.php | 1 + 7 files changed, 423 insertions(+), 3 deletions(-) create mode 100644 email.php diff --git a/CHANGELOG b/CHANGELOG index 0349772c..da8213e0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -15,8 +15,8 @@ UPDATE `case` SET token = case_id; New outcomes for email invitation and online self completion: INSERT INTO `outcome` (`outcome_id`, `aapor_id`, `description`, `default_delay_minutes`, `outcome_type_id`, `tryanother`, `contacted`, `tryagain`, `eligible`, `require_note`, `calc`) VALUES -(34, '1.1', 'Self completed online', 0, 4, 0, 1, 1, 1, 0, 'I'), -(35, '2.36', 'Self completion email invitation sent', 10080, 1, 0, 1, 1, 1, 0, 'O'); +(40, '1.1', 'Self completed online', 0, 4, 0, 1, 1, 1, 0, 'I'), +(41, '2.36', 'Self completion email invitation sent', 10080, 1, 0, 1, 1, 1, 0, 'O'); New sample variable type (Email address): diff --git a/call.php b/call.php index 99b8d515..3f5fb3c9 100644 --- a/call.php +++ b/call.php @@ -126,6 +126,7 @@ function display_outcomes($contacted,$ca,$case_id) WHERE contacted = '$contacted' AND outcome_id != 10"; //don't show completed if not } + } } $rs = $db->GetAll($sql); @@ -133,7 +134,13 @@ function display_outcomes($contacted,$ca,$case_id) print "
"; if (!empty($rs)) { - $do = false; + //Get current outcome to set as default + $sql = "SELECT outcome + FROM `call` + WHERE call_attempt_id = $ca + AND `end` IS NULL"; + + $do = $db->GetOne($sql); if (isset($_GET['defaultoutcome'])) $do = bigintval($_GET['defaultoutcome']); foreach($rs as $r) { diff --git a/email.php b/email.php new file mode 100644 index 00000000..c4ec61e7 --- /dev/null +++ b/email.php @@ -0,0 +1,156 @@ + + * @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"); + +/** + * LimeSurvey functions + */ +include("functions/functions.limesurvey.php"); + + +global $db; + +$operator_id = get_operator_id(); + +$msg = ""; + +if (isset($_POST['firstname'])) +{ + //validate email address + if (validate_email($_POST['email'])) + { + $case_id = get_case_id($operator_id); + $lime_sid = get_lime_sid($case_id); + $token = get_token($case_id); + $email = $db->qstr($_POST['email']); + $firstname = $db->qstr($_POST['firstname']); + $lastname = $db->qstr($_POST['lastname']); + + //update the limesurvey database email details + $sql = "UPDATE " . LIME_PREFIX ."tokens_{$lime_sid} + SET email = $email, firstname = $firstname, lastname = $lastname, emailstatus = 'OK' + WHERE token = '$token'"; + + $db->Execute($sql); + + + if (isset($_POST['submith'])) + { + end_call($operator_id,41); //end with outcome sent + if (is_voip_enabled($operator_id)) + { + include("functions/functions.voip.php"); + $v = new voip(); + $v->connect(VOIP_SERVER); + $v->hangup(get_extension($operator_id)); + } + //disable recording + $newtext = T_("Start REC"); + $js = "js/window.js"; + if (browser_ie()) $js = "js/window_ie6.js"; + xhtml_head(T_("Email"),true,array("css/call.css"),array($js),"onload='toggleRec(\"$newtext\",\"record.php?start=start\",\"offline\"); openParentObject(\"main-content\",\"" . get_respondentselection_url($operator_id) . "\"); parent.closePopup();'"); + + } + else if (isset($_POST['submit'])) + { + $call_id = get_call($operator_id); + + $sql = "UPDATE `call` as c + SET c.outcome_id = 41 + WHERE c.call_id = $call_id"; + + $db->Execute($sql); + + xhtml_head(T_("Email"),true,array("css/call.css"),array($js),"onload='parent.closePopup();'"); + } + xhtml_foot(); + die(); + } + else + { + $msg = T_("The email address is not valid"); + } +} + +$case_id = get_case_id($operator_id); + +$js = "js/window.js"; +if (browser_ie()) $js = "js/window_ie6.js"; + +xhtml_head(T_("Email"),true,array("css/call.css"),array($js)); + + +$sql = "SELECT sv1.val as firstname, sv2.val as lastname, sv3.val as email + FROM `case` as c + LEFT JOIN sample_var as sv1 on (sv1.sample_id = c.sample_id AND sv1.type = 6) + LEFT JOIN sample_var as sv2 on (sv2.sample_id = c.sample_id AND sv2.type = 7) + LEFT JOIN sample_var as sv3 on (sv3.sample_id = c.sample_id AND sv3.type = 8) + WHERE c.case_id = $case_id"; + +$rs = $db->GetRow($sql); + +print "
" . T_("Email respondent for self completion") . "
"; +if (!empty($msg)) print "

$msg

"; +print "
"; +print "
"; +print "
"; +print "
"; +print "
"; +print "
"; + + +xhtml_foot(); + +?> diff --git a/functions/functions.limesurvey.php b/functions/functions.limesurvey.php index f2e51479..352a4780 100644 --- a/functions/functions.limesurvey.php +++ b/functions/functions.limesurvey.php @@ -42,6 +42,214 @@ include_once(dirname(__FILE__).'/../config.inc.php'); */ include_once(dirname(__FILE__).'/../db.inc.php'); +/*function validate_email($email) +{ +// Create the syntactical validation regular expression +// Validate the syntax + +// see http://data.iana.org/TLD/tlds-alpha-by-domain.txt +$maxrootdomainlength = 6; +return ( ! preg_match("/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,".$maxrootdomainlength."}))$/ix", $email)) ? FALSE : TRUE; +}*/ + +function validate_email($email){ + + + $no_ws_ctl = "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]"; + $alpha = "[\\x41-\\x5a\\x61-\\x7a]"; + $digit = "[\\x30-\\x39]"; + $cr = "\\x0d"; + $lf = "\\x0a"; + $crlf = "(?:$cr$lf)"; + + + $obs_char = "[\\x00-\\x09\\x0b\\x0c\\x0e-\\x7f]"; + $obs_text = "(?:$lf*$cr*(?:$obs_char$lf*$cr*)*)"; + $text = "(?:[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f]|$obs_text)"; + + + $text = "(?:$lf*$cr*$obs_char$lf*$cr*)"; + $obs_qp = "(?:\\x5c[\\x00-\\x7f])"; + $quoted_pair = "(?:\\x5c$text|$obs_qp)"; + + + $wsp = "[\\x20\\x09]"; + $obs_fws = "(?:$wsp+(?:$crlf$wsp+)*)"; + $fws = "(?:(?:(?:$wsp*$crlf)?$wsp+)|$obs_fws)"; + $ctext = "(?:$no_ws_ctl|[\\x21-\\x27\\x2A-\\x5b\\x5d-\\x7e])"; + $ccontent = "(?:$ctext|$quoted_pair)"; + $comment = "(?:\\x28(?:$fws?$ccontent)*$fws?\\x29)"; + $cfws = "(?:(?:$fws?$comment)*(?:$fws?$comment|$fws))"; + + + $outer_ccontent_dull = "(?:$fws?$ctext|$quoted_pair)"; + $outer_ccontent_nest = "(?:$fws?$comment)"; + $outer_comment = "(?:\\x28$outer_ccontent_dull*(?:$outer_ccontent_nest$outer_ccontent_dull*)+$fws?\\x29)"; + + + + $atext = "(?:$alpha|$digit|[\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2f\\x3d\\x3f\\x5e\\x5f\\x60\\x7b-\\x7e])"; + $atext_domain = "(?:$alpha|$digit|[\\x2b\\x2d\\x5f])"; + + $atom = "(?:$cfws?(?:$atext)+$cfws?)"; + $atom_domain = "(?:$cfws?(?:$atext_domain)+$cfws?)"; + + + $qtext = "(?:$no_ws_ctl|[\\x21\\x23-\\x5b\\x5d-\\x7e])"; + $qcontent = "(?:$qtext|$quoted_pair)"; + $quoted_string = "(?:$cfws?\\x22(?:$fws?$qcontent)*$fws?\\x22$cfws?)"; + + + $quoted_string = "(?:$cfws?\\x22(?:$fws?$qcontent)+$fws?\\x22$cfws?)"; + $word = "(?:$atom|$quoted_string)"; + + + $obs_local_part = "(?:$word(?:\\x2e$word)*)"; + + + $obs_domain = "(?:$atom_domain(?:\\x2e$atom_domain)*)"; + + $dot_atom_text = "(?:$atext+(?:\\x2e$atext+)*)"; + $dot_atom_text_domain = "(?:$atext_domain+(?:\\x2e$atext_domain+)*)"; + + + $dot_atom = "(?:$cfws?$dot_atom_text$cfws?)"; + $dot_atom_domain = "(?:$cfws?$dot_atom_text_domain$cfws?)"; + + + $dtext = "(?:$no_ws_ctl|[\\x21-\\x5a\\x5e-\\x7e])"; + $dcontent = "(?:$dtext|$quoted_pair)"; + $domain_literal = "(?:$cfws?\\x5b(?:$fws?$dcontent)*$fws?\\x5d$cfws?)"; + + + $local_part = "(($dot_atom)|($quoted_string)|($obs_local_part))"; + $domain = "(($dot_atom_domain)|($domain_literal)|($obs_domain))"; + $addr_spec = "$local_part\\x40$domain"; + + + if (strlen($email) > 256) return FALSE; + + + $email = strip_comments($outer_comment, $email, "(x)"); + + + + if (!preg_match("!^$addr_spec$!", $email, $m)){ + + return FALSE; + } + + $bits = array( + 'local' => isset($m[1]) ? $m[1] : '', + 'local-atom' => isset($m[2]) ? $m[2] : '', + 'local-quoted' => isset($m[3]) ? $m[3] : '', + 'local-obs' => isset($m[4]) ? $m[4] : '', + 'domain' => isset($m[5]) ? $m[5] : '', + 'domain-atom' => isset($m[6]) ? $m[6] : '', + 'domain-literal' => isset($m[7]) ? $m[7] : '', + 'domain-obs' => isset($m[8]) ? $m[8] : '', + ); + + + + $bits['local'] = strip_comments($comment, $bits['local']); + $bits['domain'] = strip_comments($comment, $bits['domain']); + + + + + if (strlen($bits['local']) > 64) return FALSE; + if (strlen($bits['domain']) > 255) return FALSE; + + + + if (strlen($bits['domain-literal'])){ + + $Snum = "(\d{1,3})"; + $IPv4_address_literal = "$Snum\.$Snum\.$Snum\.$Snum"; + + $IPv6_hex = "(?:[0-9a-fA-F]{1,4})"; + + $IPv6_full = "IPv6\:$IPv6_hex(:?\:$IPv6_hex){7}"; + + $IPv6_comp_part = "(?:$IPv6_hex(?:\:$IPv6_hex){0,5})?"; + $IPv6_comp = "IPv6\:($IPv6_comp_part\:\:$IPv6_comp_part)"; + + $IPv6v4_full = "IPv6\:$IPv6_hex(?:\:$IPv6_hex){5}\:$IPv4_address_literal"; + + $IPv6v4_comp_part = "$IPv6_hex(?:\:$IPv6_hex){0,3}"; + $IPv6v4_comp = "IPv6\:((?:$IPv6v4_comp_part)?\:\:(?:$IPv6v4_comp_part\:)?)$IPv4_address_literal"; + + + + if (preg_match("!^\[$IPv4_address_literal\]$!", $bits['domain'], $m)){ + + if (intval($m[1]) > 255) return FALSE; + if (intval($m[2]) > 255) return FALSE; + if (intval($m[3]) > 255) return FALSE; + if (intval($m[4]) > 255) return FALSE; + + }else{ + + + while (1){ + + if (preg_match("!^\[$IPv6_full\]$!", $bits['domain'])){ + break; + } + + if (preg_match("!^\[$IPv6_comp\]$!", $bits['domain'], $m)){ + list($a, $b) = explode('::', $m[1]); + $folded = (strlen($a) && strlen($b)) ? "$a:$b" : "$a$b"; + $groups = explode(':', $folded); + if (count($groups) > 6) return FALSE; + break; + } + + if (preg_match("!^\[$IPv6v4_full\]$!", $bits['domain'], $m)){ + + if (intval($m[1]) > 255) return FALSE; + if (intval($m[2]) > 255) return FALSE; + if (intval($m[3]) > 255) return FALSE; + if (intval($m[4]) > 255) return FALSE; + break; + } + + if (preg_match("!^\[$IPv6v4_comp\]$!", $bits['domain'], $m)){ + list($a, $b) = explode('::', $m[1]); + $b = substr($b, 0, -1); # remove the trailing colon before the IPv4 address + $folded = (strlen($a) && strlen($b)) ? "$a:$b" : "$a$b"; + $groups = explode(':', $folded); + if (count($groups) > 4) return FALSE; + break; + } + + return FALSE; + } + } + }else{ + + + $labels = explode('.', $bits['domain']); + + + if (count($labels) == 1) return FALSE; + + + foreach ($labels as $label){ + + if (strlen($label) > 63) return FALSE; + if (substr($label, 0, 1) == '-') return FALSE; + if (substr($label, -1) == '-') return FALSE; + } + + if (preg_match('!^[0-9]+$!', array_pop($labels))) return FALSE; + } + + + return TRUE; +} + /** * Return the number of completions for a given * questionnaire, where the given sample var has diff --git a/functions/functions.operator.php b/functions/functions.operator.php index 597fe83e..ddd25eae 100644 --- a/functions/functions.operator.php +++ b/functions/functions.operator.php @@ -646,6 +646,31 @@ function get_case_id($operator_id, $create = false) } + +/** + * Get the token based on the case id + * + * @param int $case_id The case id + * + * @return string|bool The token otherwise false if case doesn't exist + * @author Adam Zammit + * @since 2013-02-25 + */ +function get_token($case_id) +{ + global $db; + + $sql = "SELECT token + FROM `case` + WHERE case_id = $case_id"; + + $token = $db->GetOne($sql); + + if (empty($token)) return FALSE; + + return $token; +} + /** * Return the phone number of a call * diff --git a/include/limesurvey/optout.php b/include/limesurvey/optout.php index b986ba5a..a7e9fc17 100644 --- a/include/limesurvey/optout.php +++ b/include/limesurvey/optout.php @@ -60,6 +60,29 @@ else { $usquery = "Update ".db_table_name("tokens_{$surveyid}")." set emailstatus='OptOut', usesleft=0 where token=".db_quoteall($token,true); $usresult = $connect->Execute($usquery); + + //queXS addition + + //Set to Hard Refusal, respondent + $sql = "UPDATE `case` + SET current_outcome_id = 9 + WHERE token = '$token'"; + + $connect->Execute($sql); + + $sql = "SELECT case_id + FROM `case` + WHERE token = '$token'"; + + $case_id = $connect->GetOne($sql); + + + //Add a case note to clarify (need to translate this string) + $sql = "INSERT INTO `case_note` (case_id,operator_id,note,datetime) + VALUES ($case_id,1,'Self completion refused via opt out function',NOW())"; + + $connect->Execute($sql); + $html .= $clang->gT('You have been successfully removed from this survey.'); } else diff --git a/index.php b/index.php index 3a5ed9d0..e838f105 100644 --- a/index.php +++ b/index.php @@ -137,6 +137,7 @@ print $script;
+
<?php  echo T_('/>