Skip to content

Commit

Permalink
New feature: send survey responses to email recipient email from ente…
Browse files Browse the repository at this point in the history
…red email in field, SGQA answer or token field

git-svn-id: file:///Users/Shitiz/Downloads/lssvn/source/limesurvey_dev@7683 b72ed6b6-b9f8-46b5-92b4-906544132732
  • Loading branch information
jcleeland committed Sep 26, 2009
1 parent 95ce2cb commit c393b5c
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 42 deletions.
6 changes: 4 additions & 2 deletions admin/database.php
Expand Up @@ -1134,7 +1134,8 @@ function get_max_question_order($gid)
'listpublic'=>$_POST['public'],
'htmlemail'=>$_POST['htmlemail'],
'tokenanswerspersistence'=>$_POST['tokenanswerspersistence'],
'usecaptcha'=>$_POST['usecaptcha']
'usecaptcha'=>$_POST['usecaptcha'],
'emailresponseto'=>$_POST['emailresponseto']
);

$usquery=$connect->GetUpdateSQL($rs, $updatearray);
Expand Down Expand Up @@ -1389,7 +1390,8 @@ function get_max_question_order($gid)
'usecaptcha'=>$_POST['usecaptcha'],
'publicstatistics'=>$_POST['publicstatistics'],
'publicgraphs'=>$_POST['publicgraphs'],
'assessments'=>$_POST['assessments']
'assessments'=>$_POST['assessments'],
'emailresponseto'=>$_POST['emailresponseto']
);
$dbtablename=db_table_name_nq('surveys');
$isquery = $connect->GetInsertSQL($dbtablename, $insertarray);
Expand Down
12 changes: 11 additions & 1 deletion admin/html.php
Expand Up @@ -2513,6 +2513,11 @@
. "</select></span>\n"
. "</div>\n";

//EMAIL SURVEY RESPONSES TO
$editsurvey .= "<div class='settingrow'><span class='settingcaption'>".$clang->gT("Email Responses To:")."</span>\n"
. "<span class='settingentry'><input type='text' value=\"{$esrow['emailresponseto']}\" name='emailresponseto'>\n"
. "</span>"
. "</div>\n";

//ANONYMOUS
$editsurvey .= "<div class='settingrow'><span class='settingcaption'>".$clang->gT("Anonymous answers?")."\n";
Expand Down Expand Up @@ -3316,7 +3321,12 @@
. "</select></span>\n"
. "</div>\n";


//EMAIL SURVEY RESPONSES TO
$newsurvey .= "<div class='settingrow'><span class='settingcaption'>".$clang->gT("Email Responses To:")."</span>\n"
. "<span class='settingentry'><input type='text' name='emailresponseto'>\n"
. "</span>"
. "</div>\n";

// ANONYMOUS
$newsurvey .= "<div class='settingrow'><span class='settingcaption'>".$clang->gT("Anonymous answers?")."\n";
// warning message if anonymous + datestamped anwsers
Expand Down
1 change: 1 addition & 0 deletions admin/install/create-mssql.sql
Expand Up @@ -247,6 +247,7 @@ CREATE TABLE [prefix_surveys] (
[usetokens] char(1) default 'N',
[bounce_email] VARCHAR(320) default NULL,
[attributedescriptions] text,
[emailresponseto] text

PRIMARY KEY ([sid])
)
Expand Down
1 change: 1 addition & 0 deletions admin/install/create-mysql.sql
Expand Up @@ -205,6 +205,7 @@ CREATE TABLE `prefix_surveys` (
`usetokens` char(1) default 'N',
`bounce_email` varchar(320) default NULL,
`attributedescriptions` text,
`emailresponseto` text default NULL,
PRIMARY KEY (`sid`)
) ENGINE=$databasetabletype CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Expand Down
3 changes: 2 additions & 1 deletion admin/install/create-postgres.sql
Expand Up @@ -259,7 +259,8 @@ CREATE TABLE prefix_surveys (
usecaptcha character(1) DEFAULT 'N'::bpchar,
usetokens character(1) DEFAULT 'N'::bpchar,
"bounce_email" character varying(320) NOT NULL,
attributedescriptions text
attributedescriptions text,
emailresponseto text
);


Expand Down
7 changes: 7 additions & 0 deletions admin/update/upgrade-mssql.php
Expand Up @@ -335,6 +335,13 @@ function db_upgrade($oldversion) {
upgrade_survey_tables139();
modify_database("", "UPDATE [prefix_settings_global] SET [stg_value]='139' WHERE stg_name='DBVersion'"); echo $modifyoutput; flush();
}

if ($oldversion < 140) //Modify surveys table
{
modify_database("", "ALTER TABLE [prefix_surveys] ADD [emailresponseto] text"); echo $modifyoutput; flush();
modify_database("", "UPDATE [prefix_settings_global] SET [stg_value]='140' WHERE stg_name='DBVersion'"); echo $modifyoutput; flush();
}

return true;
}

Expand Down
6 changes: 6 additions & 0 deletions admin/update/upgrade-mysql.php
Expand Up @@ -323,6 +323,12 @@ function db_upgrade($oldversion) {
upgrade_survey_tables139();
modify_database("", "UPDATE `prefix_settings_global` SET `stg_value`='139' WHERE stg_name='DBVersion'"); echo $modifyoutput; flush();
}

if ($oldversion < 140) //Modify surveys table
{
modify_database("", "ALTER TABLE `prefix_surveys` ADD `emailresponseto` text DEFAULT NULL"); echo $modifyoutput; flush();
modify_database("", "UPDATE `prefix_settings_global` SET `stg_value`='140' WHERE stg_name='DBVersion'"); echo $modifyoutput; flush();
}
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions admin/update/upgrade-postgres.php
Expand Up @@ -163,6 +163,12 @@ function db_upgrade($oldversion) {
modify_database("", "UPDATE prefix_settings_global SET stg_value='139' WHERE stg_name='DBVersion'"); echo $modifyoutput; flush();
}

if ($oldversion < 140) //Modify surveys table
{
modify_database("", "ALTER TABLE prefix_surveys ADD \"emailresponseto\" TEXT"); echo $modifyoutput; flush();
modify_database("", "UPDATE prefix_settings_global SET stg_value='140' WHERE stg_name='DBVersion'"); echo $modifyoutput; flush();
}

return true;
}

Expand Down
75 changes: 51 additions & 24 deletions common.php
Expand Up @@ -2824,30 +2824,8 @@ function templatereplace($line, $replacements=array())
}
$line=str_replace("{SUBMITREVIEW}", $strreview, $line);
}
if (isset($_SESSION['thistoken']))
{
if (strpos($line, "{TOKEN:FIRSTNAME}") !== false) $line=str_replace("{TOKEN:FIRSTNAME}", $_SESSION['thistoken']['firstname'], $line);
if (strpos($line, "{TOKEN:LASTNAME}") !== false) $line=str_replace("{TOKEN:LASTNAME}", $_SESSION['thistoken']['lastname'], $line);
if (strpos($line, "{TOKEN:EMAIL}") !== false) $line=str_replace("{TOKEN:EMAIL}", $_SESSION['thistoken']['email'], $line);
}
else
{
if (strpos($line, "{TOKEN:FIRSTNAME}") !== false) $line=str_replace("{TOKEN:FIRSTNAME}", "", $line);
if (strpos($line, "{TOKEN:LASTNAME}") !== false) $line=str_replace("{TOKEN:LASTNAME}", "", $line);
if (strpos($line, "{TOKEN:EMAIL}") !== false) $line=str_replace("{TOKEN:EMAIL}", "", $line);
}

while (strpos($line, "{TOKEN:ATTRIBUTE_")!== false)
{
$templine=substr($line,strpos($line, "{TOKEN:ATTRIBUTE_"));
$templine=substr($templine,0,strpos($templine, "}")+1);
$attr_no=(int)substr($templine,17,strpos($templine, "}")-17);
$replacestr='';
if (isset($_SESSION['thistoken']['attribute_'.$attr_no])) $replacestr=$_SESSION['thistoken']['attribute_'.$attr_no];
$line=str_replace($templine, $replacestr, $line);
}



$line=tokenReplace($line);

if (strpos($line, "{ANSWERSCLEARED}") !== false) $line=str_replace("{ANSWERSCLEARED}", $clang->gT("Answers Cleared"), $line);
if (strpos($line, "{RESTART}") !== false)
Expand Down Expand Up @@ -3035,6 +3013,55 @@ function insertansReplace($line)
return $line;
}

/**
* tokenReplace() takes a string and looks for any {TOKEN:xxxx} variables
* which it then, one by one, substitutes the TOKEN code with the relevant token
* from the session array containing token information
*
* The operations of this function were previously in the templatereplace function
* but have been moved to a function of their own to make it available
* to other areas of the script.
*
* @param mixed $line string - the string to iterate, and then return
*
* @return string This string is returned containing the substituted responses
*
*/
function tokenReplace($line)
{
global $surveyid;

if (isset($_SESSION['token']) && $_SESSION['token'] != '')
{
//Gather survey data for tokenised surveys, for use in presenting questions
$_SESSION['thistoken']=getTokenData($surveyid, $_SESSION['token']);
}

if (isset($_SESSION['thistoken']))
{
if (strpos($line, "{TOKEN:FIRSTNAME}") !== false) $line=str_replace("{TOKEN:FIRSTNAME}", $_SESSION['thistoken']['firstname'], $line);
if (strpos($line, "{TOKEN:LASTNAME}") !== false) $line=str_replace("{TOKEN:LASTNAME}", $_SESSION['thistoken']['lastname'], $line);
if (strpos($line, "{TOKEN:EMAIL}") !== false) $line=str_replace("{TOKEN:EMAIL}", $_SESSION['thistoken']['email'], $line);
}
else
{
if (strpos($line, "{TOKEN:FIRSTNAME}") !== false) $line=str_replace("{TOKEN:FIRSTNAME}", "", $line);
if (strpos($line, "{TOKEN:LASTNAME}") !== false) $line=str_replace("{TOKEN:LASTNAME}", "", $line);
if (strpos($line, "{TOKEN:EMAIL}") !== false) $line=str_replace("{TOKEN:EMAIL}", "", $line);
}

while (strpos($line, "{TOKEN:ATTRIBUTE_")!== false)
{
$templine=substr($line,strpos($line, "{TOKEN:ATTRIBUTE_"));
$templine=substr($templine,0,strpos($templine, "}")+1);
$attr_no=(int)substr($templine,17,strpos($templine, "}")-17);
$replacestr='';
if (isset($_SESSION['thistoken']['attribute_'.$attr_no])) $replacestr=$_SESSION['thistoken']['attribute_'.$attr_no];
$line=str_replace($templine, $replacestr, $line);
}
return $line;
}

/**
* passthruReplace() takes a string and looks for {PASSTHRULABEL} and {PASSTHRUVALUE} variables
* which it then substitutes for passthru data sent in the initial URL and stored
Expand Down
78 changes: 66 additions & 12 deletions index.php
Expand Up @@ -1569,7 +1569,7 @@ function sendsubmitnotification($sendnotification)
{
global $thissurvey, $debug;
global $dbprefix, $clang, $emailcharset;
global $sitename, $homeurl, $surveyid, $publicurl, $maildebug;
global $sitename, $homeurl, $surveyid, $publicurl, $maildebug, $tokensexist;

$subject = $clang->gT("Answer Submission for Survey","unescaped")." ".$thissurvey['name'];

Expand All @@ -1589,9 +1589,47 @@ function sendsubmitnotification($sendnotification)
. " $homeurl/admin.php?action=dataentry&sid=$surveyid&subaction=edit&surveytable=survey_$surveyid&id=".$_SESSION['srid']."\n\n"
. $clang->gT("View statistics by clicking here:","unescaped")."\n"
. " $homeurl/admin.php?action=statistics&sid=$surveyid\n\n";
if ($sendnotification > 1)

$emailresponseto=null;
if (!empty($thissurvey['emailresponseto']))
{
if (isset($_SESSION['token']) && $_SESSION['token'] != '')
{
//Gather token data for tokenised surveys
$_SESSION['thistoken']=getTokenData($surveyid, $_SESSION['token']);
}
//Make an array of email addresses to send to
if($erts=explode(";", $thissurvey['emailresponseto']))
{
foreach($erts as $ert)
{
$ert=insertansReplace($ert);
$ert=tokenReplace($ert);
$emailresponsetos[]=$ert;
}
}
else
{
$ert=$thissurvey['emailresponseto'];
$ert=insertansReplace($ert);
$ert=tokenReplace($ert);
$emailresponsetos[]=$ert;
}

//Now check each of the email addresses that they are valid before creating/adding to the $emailresponseto array
foreach($emailresponsetos as $ert)
{
if(validate_email($ert))
{
$emailresponseto[]=$ert;
}
}
}

$results="";
if ($sendnotification > 1 || $emailresponseto)
{ //Send results as well. Currently just bare-bones - will be extended in later release
$message .= "----------------------------\n";
$results = "----------------------------\n";
$prevquestion='';
foreach ($_SESSION['insertarray'] as $value)
{
Expand All @@ -1600,38 +1638,39 @@ function sendsubmitnotification($sendnotification)
{
$prevquestion=$qaarray[0];
$questiontitle=strip_tags(html_entity_decode($prevquestion, ENT_QUOTES, $emailcharset));
$message .= "\n$questiontitle: ";
$results .= "\n$questiontitle: ";
if ($qaarray[1]!='')
{
$message .= "\n";
$results .= "\n";
}
}
if ($qaarray[1]!='')
{
$answeroption=strip_tags(html_entity_decode($qaarray[1], ENT_QUOTES, $emailcharset));
$message .= "[$answeroption]: ";
$results .= "[$answeroption]: ";
}
$details = arraySearchByKey($value, createFieldMap($surveyid),"fieldname", 1);
if ( $details['type'] == "T" or $details['type'] == "U")
{
$message .= "\r\n";
$results .= "\r\n";
if (isset($_SESSION[$value]))
{
foreach (explode("\n",getextendedanswer($value,$_SESSION[$value])) as $line)
{
$message .= "\t" . strip_tags(html_entity_decode($line, ENT_QUOTES, $emailcharset));
$message .= "\n";
$results .= "\t" . strip_tags(html_entity_decode($line, ENT_QUOTES, $emailcharset));
$results .= "\n";
}
}
}
elseif (isset($_SESSION[$value]))
{
$message .= strip_tags(html_entity_decode(getextendedanswer($value, $_SESSION[$value]),ENT_QUOTES, $emailcharset));
$message .= "\n";
$results .= strip_tags(html_entity_decode(getextendedanswer($value, $_SESSION[$value]),ENT_QUOTES, $emailcharset));
$results .= "\n";
}
}
$message .= "\n\n----------------------------\n\n";
$results .= "\n\n----------------------------\n\n";
}
$message .= $results;
$message.= "LimeSurvey";
$from = $thissurvey['adminname'].' <'.$thissurvey['adminemail'].'>';

Expand All @@ -1652,6 +1691,21 @@ function sendsubmitnotification($sendnotification)
if ($debug>0) {echo '<br />Email could not be sent. Reason: '.$maildebug.'<br/>';}
}
}

if($emailresponseto)
{
$ertmessage = $clang->gT("This email contains confirmation of the responses you made to the survey")." ".$thissurvey['name']."\n";
$ertmessage .= $results;
$ertsubject = $clang->gT("Survey Submission Confirmation");

foreach($emailresponseto as $ert)
{
if(!MailTextMessage($ertmessage, $ertsubject, $ert, $from, $sitename, false, getBounceEmail($surveyid)))
{
if ($debug>0) {echo '<br />Email could not be sent to EmailReponseTo field. Reason: '.$maildebug.'<br />';}
}
}
}
}

function submitfailed($errormsg)
Expand Down
2 changes: 1 addition & 1 deletion question.php
Expand Up @@ -284,7 +284,7 @@
}

//Send notification to survey administrator //Thanks to Jeff Clement http://jclement.ca
if ($thissurvey['sendnotification'] > 0 && $thissurvey['adminemail'])
if (($thissurvey['sendnotification'] > 0 && $thissurvey['adminemail']) || !empty($thissurvey['emailresponseto']))
{
sendsubmitnotification($thissurvey['sendnotification']);
}
Expand Down
2 changes: 1 addition & 1 deletion version.php
@@ -1,7 +1,7 @@
<?php

$versionnumber = "1.86RC";
$dbversionnumber = 139;
$dbversionnumber = 140;
$buildnumber = '';

?>

0 comments on commit c393b5c

Please sign in to comment.