diff --git a/application/controllers/admin/remotecontrol.php b/application/controllers/admin/remotecontrol.php index c58b4637c04..b4502e12382 100644 --- a/application/controllers/admin/remotecontrol.php +++ b/application/controllers/admin/remotecontrol.php @@ -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 */ diff --git a/application/helpers/admin/token_helper.php b/application/helpers/admin/token_helper.php index 353ebc03d1b..1968f7d5927 100644 --- a/application/helpers/admin/token_helper.php +++ b/application/helpers/admin/token_helper.php @@ -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}"] = "" . htmlspecialchars($url) . ''; + 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; +} + + + diff --git a/application/models/Tokens_dynamic.php b/application/models/Tokens_dynamic.php index 0a4949192f8..e922d0a2058 100644 --- a/application/models/Tokens_dynamic.php +++ b/application/models/Tokens_dynamic.php @@ -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(); } diff --git a/locale/de-informal/LC_MESSAGES/de-informal.mo b/locale/de-informal/LC_MESSAGES/de-informal.mo index ea8c39e997b..3568a0ecc98 100644 Binary files a/locale/de-informal/LC_MESSAGES/de-informal.mo and b/locale/de-informal/LC_MESSAGES/de-informal.mo differ diff --git a/locale/de/LC_MESSAGES/de.mo b/locale/de/LC_MESSAGES/de.mo index b62a5dc0b38..eea4931ecf3 100644 Binary files a/locale/de/LC_MESSAGES/de.mo and b/locale/de/LC_MESSAGES/de.mo differ diff --git a/locale/it/LC_MESSAGES/it.mo b/locale/it/LC_MESSAGES/it.mo index 338c6abed18..3ac91e6a971 100644 Binary files a/locale/it/LC_MESSAGES/it.mo and b/locale/it/LC_MESSAGES/it.mo differ diff --git a/locale/ja/LC_MESSAGES/ja.mo b/locale/ja/LC_MESSAGES/ja.mo index 816e44ad14a..e99fb9c96ec 100644 Binary files a/locale/ja/LC_MESSAGES/ja.mo and b/locale/ja/LC_MESSAGES/ja.mo differ