Skip to content

Commit

Permalink
Merge branch 'Yii' of https://github.com/LimeSurvey/LimeSurvey into Yii
Browse files Browse the repository at this point in the history
  • Loading branch information
jcleeland committed Aug 25, 2012
2 parents adc1b0e + 7b47934 commit 02ee215
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 2 deletions.
111 changes: 111 additions & 0 deletions application/controllers/admin/remotecontrol.php
Expand Up @@ -1976,6 +1976,117 @@ public function activate_tokens($sSessionKey, $iSurveyID, $aAttributeFields=arra
return array('status' => 'No permission');
}

/**
* RPC Routine to invite participants in a survey
* Returns array of results of sending
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the survey that participants belong
* @return array Result of the action
*/
public function invite_participants($sSessionKey, $iSurveyID )
{
Yii::app()->loadHelper('admin/token');
if (!$this->_checkSessionKey($sSessionKey))
return array('status' => 'Invalid session key');

$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey))
return array('status' => 'Error: Invalid survey ID');

if (hasSurveyPermission($iSurveyID, 'tokens', 'update'))
{

if(!tableExists("{{tokens_$iSurveyID}}"))
return array('status' => 'Error: No token table');

$iMaxEmails = (int)Yii::app()->getConfig("maxemails");
$SQLemailstatuscondition = " AND emailstatus = 'OK'";

$oTokens = Tokens_dynamic::model($iSurveyID);
$aResultTokens = $oTokens->findUninvited(false, $iMaxEmails, true, $SQLemailstatuscondition);
$aAllTokens = $oTokens->findUninvited(false, 0, true, $SQLemailstatuscondition);
if (empty($aResultTokens))
return array('status' => 'Error: No candidate tokens');

$aResult = emailTokens($iSurveyID,$aResultTokens,'invite');
$iLeft = count($aAllTokens) - count($aResultTokens);
$aResult['status'] =$iLeft. " left to send";

return $aResult;
}
else
return array('status' => 'No permission');
}


/**
* RPC Routine to send reminder for participants in a survey
* Returns array of results of sending
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the survey that participants belong
* @param int $iMinDaysBetween Optional parameter days from last reminder
* @param int $iMaxReminders Optional parameter Maximum reminders count
* @return array Result of the action
*/
public function remind_participants($sSessionKey, $iSurveyID, $iMinDaysBetween=null, $iMaxReminders=null )
{
Yii::app()->loadHelper('admin/token');
if (!$this->_checkSessionKey($sSessionKey))
return array('status' => 'Invalid session key');

$oSurvey = Survey::model()->findByPk($iSurveyID);
if (!isset($oSurvey))
return array('status' => 'Error: Invalid survey ID');

if (hasSurveyPermission($iSurveyID, 'tokens', 'update'))
{
$timeadjust = Yii::app()->getConfig("timeadjust");

if(!tableExists("{{tokens_$iSurveyID}}"))
return array('status' => 'Error: No token table');

if (getEmailFormat($iSurveyID) == 'html')
$bHtml = true;
else
$bHtml = false;

$SQLemailstatuscondition = " AND emailstatus = 'OK'";
$SQLremindercountcondition = '';
$SQLreminderdelaycondition = '';
$attributes = getTokenFieldsAndNames($iSurveyID);
$iMaxEmails = (int)Yii::app()->getConfig("maxemails");

if(!is_null($iMinDaysBetween))
{
$compareddate = dateShift(date("Y-m-d H:i:s", time() - 86400 * $iMinDaysBetween), "Y-m-d H:i", $timeadjust);
$SQLreminderdelaycondition = " AND ((remindersent = 'N' AND sent < '" . $compareddate . "') OR (remindersent < '" . $compareddate . "'))";
}

if(!is_null($iMaxReminders))
$SQLremindercountcondition = " AND remindercount < " . $iMaxReminders;

$oTokens = Tokens_dynamic::model($iSurveyID);
$aResultTokens = $oTokens->findUninvited(false, $iMaxEmails, false, $SQLemailstatuscondition, $SQLremindercountcondition, $SQLreminderdelaycondition);
$aAllTokens = $oTokens->findUninvited(false, 0, false, $SQLemailstatuscondition, $SQLremindercountcondition, $SQLreminderdelaycondition);

if (empty($aResultTokens))
return array('status' => 'Error: No candidate tokens');

$aResult = emailTokens($iSurveyID, $aResultTokens, 'remind');

$iLeft = count($aAllTokens) - count($aResultTokens);
$aResult['status'] =$iLeft. " left to send";
return $aResult;
}
else
return array('status' => 'No permission');

}


/* Response specific functions */

Expand Down
179 changes: 179 additions & 0 deletions application/helpers/admin/token_helper.php
Expand Up @@ -55,3 +55,182 @@ function createTokenTable($iSurveyID, $aAttributeFields=array())
}

}


/**
* Sends email to tokens - invitation and reminders
*
* @param mixed $iSurveyID
* @param array $aResultTokens
* @param string $sType type of notification invite|remind
* @return array of results
*/
function emailTokens($iSurveyID,$aResultTokens,$sType)
{
Yii::app()->loadHelper('common');
$oSurvey = Survey::model()->findByPk($iSurveyID);
if (getEmailFormat($iSurveyID) == 'html')
$bHtml = true;
else
$bHtml = false;

$attributes = getTokenFieldsAndNames($iSurveyID);
$oSurveyLocale=Surveys_languagesettings::model()->findAllByAttributes(array('surveyls_survey_id' => $iSurveyID));
$oTokens = Tokens_dynamic::model($iSurveyID);
$aSurveyLangs = $oSurvey->additionalLanguages;
array_unshift($aSurveyLangs, $oSurvey->language);

//Convert result to associative array to minimize SurveyLocale access attempts
foreach($oSurveyLocale as $rows)
{
$oTempObject=array();
foreach($rows as $k=>$v)
{
$oTempObject[$k] = $v;
}
$aSurveyLocaleData[$rows['surveyls_language']]=$oTempObject;
}

foreach ($aResultTokens as $aTokenRow)
{
//Select language
$aTokenRow['language'] = trim($aTokenRow['language']);
$found = array_search($aTokenRow['language'], $aSurveyLangs);
if ($aTokenRow['language'] == '' || $found == false)
{
$aTokenRow['language'] = $oSurvey['language'];
}
$sTokenLanguage = $aTokenRow['language'];


//Build recipient
$to = array();
$aEmailaddresses = explode(';', $aTokenRow['email']);
foreach ($aEmailaddresses as $sEmailaddress)
{
$to[] = ($aTokenRow['firstname'] . " " . $aTokenRow['lastname'] . " <{$sEmailaddress}>");
}


//Populate attributes
$fieldsarray["{SURVEYNAME}"] = $aSurveyLocaleData[$sTokenLanguage]['surveyls_title'];
if ($fieldsarray["{SURVEYNAME}"] == '')
$fieldsarray["{SURVEYNAME}"] = $aSurveyLocaleData[$oSurvey['language']]['surveyls_title'];

$fieldsarray["{SURVEYDESCRIPTION}"] = $aSurveyLocaleData[$sTokenLanguage]['surveyls_description'];
if ($fieldsarray["{SURVEYDESCRIPTION}"] == '')
$fieldsarray["{SURVEYDESCRIPTION}"] = $aSurveyLocaleData[$oSurvey['language']]['surveyls_description'];

$fieldsarray["{ADMINNAME}"] = $oSurvey['admin'];
$fieldsarray["{ADMINEMAIL}"] = $oSurvey['adminemail'];
$from = $fieldsarray["{ADMINEMAIL}"];
if($from == '')
$from = Yii::app()->getConfig('siteadminemail');

foreach ($attributes as $attributefield => $attributedescription)
{
$fieldsarray['{' . strtoupper($attributefield) . '}'] = $aTokenRow[$attributefield];
$fieldsarray['{TOKEN:'.strtoupper($attributefield).'}']=$aTokenRow[$attributefield];
}

//create urls
$fieldsarray["{OPTOUTURL}"] = Yii::app()->getController()->createAbsoluteUrl("/optout/tokens/langcode/" . trim($aTokenRow['language']) . "/surveyid/{$iSurveyID}/token/{$aTokenRow['token']}");
$fieldsarray["{OPTINURL}"] = Yii::app()->getController()->createAbsoluteUrl("/optin/tokens/langcode/" . trim($aTokenRow['language']) . "/surveyid/{$iSurveyID}/token/{$aTokenRow['token']}");
$fieldsarray["{SURVEYURL}"] = Yii::app()->getController()->createAbsoluteUrl("/survey/index/sid/{$iSurveyID}/token/{$aTokenRow['token']}/langcode/" . trim($aTokenRow['language']) . "/");

if($bEmail == true)
{
foreach(array('OPTOUT', 'OPTIN', 'SURVEY') as $key)
{
$url = $fieldsarray["{{$key}URL}"];
$fieldsarray["{{$key}URL}"] = "<a href='{$url}'>" . htmlspecialchars($url) . '</a>';
if ($key == 'SURVEY')
{
$barebone_link = $url;
}
}
}

//mail headers
$customheaders = array('1' => "X-surveyid: " . $iSurveyID,'2' => "X-tokenid: " . $fieldsarray["{TOKEN}"]);

global $maildebug;

//choose appriopriate email message
if($sType == 'invite')
{
$sSubject = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_invite_subj'];
$sMessage = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_invite'];
}
else
{
$sSubject = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_remind_subj'];
$sMessage = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_remind'];
}

$modsubject = Replacefields($sSubject, $fieldsarray);
$modmessage = Replacefields($sMessage, $fieldsarray);

if (isset($barebone_link))
{
$modsubject = str_replace("@@SURVEYURL@@", $barebone_link, $modsubject);
$modmessage = str_replace("@@SURVEYURL@@", $barebone_link, $modmessage);
}




if (isset($aTokenRow['validfrom']) && trim($aTokenRow['validfrom']) != '' && convertDateTimeFormat($aTokenRow['validfrom'], 'Y-m-d H:i:s', 'U') * 1 > date('U') * 1)
{
$aResult[$aTokenRow['tid']] = array('name'=>$fieldsarray["{FIRSTNAME}"]." ".$fieldsarray["{LASTNAME}"],
'email'=>$fieldsarray["{EMAIL}"],
'status'=>'fail',
'error'=>'Token not valid yet');

}
elseif (isset($aTokenRow['validuntil']) && trim($aTokenRow['validuntil']) != '' && convertDateTimeFormat($aTokenRow['validuntil'], 'Y-m-d H:i:s', 'U') * 1 < date('U') * 1)
{
$aResult[$aTokenRow['tid']] = array('name'=>$fieldsarray["{FIRSTNAME}"]." ".$fieldsarray["{LASTNAME}"],
'email'=>$fieldsarray["{EMAIL}"],
'status'=>'fail',
'error'=>'Token not valid anymore');

}
else
{
if (SendEmailMessage($modmessage, $modsubject, $to, $from, Yii::app()->getConfig("sitename"), $bHtml, getBounceEmail($iSurveyID), null, $customheaders))
{
$aResult[$aTokenRow['tid']] = array('name'=>$fieldsarray["{FIRSTNAME}"]." ".$fieldsarray["{LASTNAME}"],
'email'=>$fieldsarray["{EMAIL}"],
'status'=>'OK');

if($sType == 'invite')
$oTokens->updateByPk($aTokenRow['tid'], array('sent' => dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"))));

if($sType == 'remind')
{
$iRCount = $oTokens->findByPk($aTokenRow['tid'])->remindercount +1;
$oTokens->updateByPk($aTokenRow['tid'], array('remindersent' => dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"))));
$oTokens->updateByPk($aTokenRow['tid'],array('remindercount' => $iRCount));
}

}
else
{

$aResult[$aTokenRow['tid']] = array('name'=>$fieldsarray["{FIRSTNAME}"]." ".$fieldsarray["{LASTNAME}"],
'email'=>$fieldsarray["{EMAIL}"],
'status'=>'fail',
'error'=>$maildebug);
}
}

unset($fieldsarray);
}


return $aResult;
}



4 changes: 2 additions & 2 deletions application/models/Tokens_dynamic.php
Expand Up @@ -153,9 +153,9 @@ public function findUninvited($aTokenIds = false, $iMaxEmails = 0, $bEmail = tru
if ($SQLremindercountcondition) {$emquery .= " $SQLremindercountcondition";}
if ($SQLreminderdelaycondition) {$emquery .= " $SQLreminderdelaycondition";}
if ($aTokenIds) {$emquery .= " AND tid IN ('".implode("', '", $aTokenIds)."')";}
if ($iMaxEmails) {$emquery .= " LIMIT $iMaxEmails"; }
$emquery .= " ORDER BY tid";

if ($iMaxEmails) {$emquery .= " LIMIT $iMaxEmails"; }

return Yii::app()->db->createCommand($emquery)->queryAll();
}

Expand Down
Binary file modified locale/de-informal/LC_MESSAGES/de-informal.mo
Binary file not shown.
Binary file modified locale/de/LC_MESSAGES/de.mo
Binary file not shown.
Binary file modified locale/it/LC_MESSAGES/it.mo
Binary file not shown.
Binary file modified locale/ja/LC_MESSAGES/ja.mo
Binary file not shown.

0 comments on commit 02ee215

Please sign in to comment.