From 2e25dcbe9807d22dbc0c7eeb73d0931c8b03439d Mon Sep 17 00:00:00 2001 From: Jan-E Date: Mon, 13 Jul 2015 17:42:32 +0200 Subject: [PATCH] Fix #09776: Mail registered participants with RemoteControl API (develop branch) --- application/helpers/admin/token_helper.php | 9 +- .../remotecontrol/remotecontrol_handle.php | 86 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/application/helpers/admin/token_helper.php b/application/helpers/admin/token_helper.php index a89304542c2..cda0be55d86 100644 --- a/application/helpers/admin/token_helper.php +++ b/application/helpers/admin/token_helper.php @@ -17,7 +17,7 @@ * * @param mixed $iSurveyID * @param array $aResultTokens -* @param string $sType type of notification invite|remind +* @param string $sType type of notification invite|register|remind * @return array of results */ function emailTokens($iSurveyID,$aResultTokens,$sType) @@ -117,6 +117,11 @@ function emailTokens($iSurveyID,$aResultTokens,$sType) $sSubject = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_invite_subj']; $sMessage = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_invite']; } + else if($sType == 'register') + { + $sSubject = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_register_subj']; + $sMessage = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_register']; + } else { $sSubject = $aSurveyLocaleData[$sTokenLanguage]['surveyls_email_remind_subj']; @@ -159,7 +164,7 @@ function emailTokens($iSurveyID,$aResultTokens,$sType) 'email'=>$fieldsarray["{EMAIL}"], 'status'=>'OK'); - if($sType == 'invite') + if($sType == 'invite' || $sType == 'register') $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') diff --git a/application/helpers/remotecontrol/remotecontrol_handle.php b/application/helpers/remotecontrol/remotecontrol_handle.php index 8b81f8063b6..c3c4068a739 100644 --- a/application/helpers/remotecontrol/remotecontrol_handle.php +++ b/application/helpers/remotecontrol/remotecontrol_handle.php @@ -2072,6 +2072,92 @@ public function invite_participants($sSessionKey, $iSurveyID ) return array('status' => 'No permission'); } + /** + * RPC Routine to send register mails to 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 array $overrideAllConditions (optional) replace the default consitions, like this: + * $overrideAllConditions = Array(); + * $overrideAllConditions[] = 'tid = 2'; + * $response = $myJSONRPCClient->mail_registered_participants( $sessionKey, $survey_id, $overrideAllConditions ); + * @return array Result of the action + */ + public function mail_registered_participants($sSessionKey, $iSurveyID, $overrideAllConditions=Array() ) + { + 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 (Permission::model()->hasSurveyPermission($iSurveyID, 'tokens', 'update')) + { + + if(!tableExists("{{tokens_$iSurveyID}}")) + return array('status' => 'Error: No token table'); + + $command = new CDbCriteria(); + $command->condition = ''; + if (count($overrideAllConditions)) { + foreach($overrideAllConditions as $condition) + { + $command->addCondition($condition); + } + } + else + { + $command->addCondition('usesleft > 0'); + $command->addCondition("sent = 'N'"); + $command->addCondition("remindersent = 'N'"); + $command->addCondition("(completed ='N') or (completed='')"); + $command->addCondition('ISNULL(validfrom) OR validfrom < NOW()'); + $command->addCondition('ISNULL(validuntil) OR validuntil > NOW()'); + $command->addCondition('emailstatus = "OK"'); + } + $command->order = 'tid'; + + $aAllTokens = Token::model($iSurveyID)->findAll( $command ); + $iAllTokensCount=count($aAllTokens); + unset($aAllTokens); + + $iMaxEmails = (int)Yii::app()->getConfig("maxemails"); + $command->limit = $iMaxEmails; + $aResultTokens = Token::model($iSurveyID)->findAll( $command ); + + if (empty($aResultTokens)) + return array('status' => 'Error: No candidate tokens'); + + foreach($aResultTokens as $key=>$oToken) + { + //pattern taken from php_filter_validate_email PHP_5_4/ext/filter/logical_filters.c + $pattern = '/^(?!(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|(?:[1-9]?[0-9]))){3}))\\]))$/iD'; + + //if(!filter_var($emailaddress, FILTER_VALIDATE_EMAIL)) + if (preg_match($pattern, $oToken['email']) !== 1) + { + unset($aResultTokens[$key]); + //subtract from 'left to send' + $iAllTokensCount--; + } + } + + if (empty($aResultTokens)) + return array('status' => 'Error: No candidate tokens'); + $aResult = emailTokens($iSurveyID,$aResultTokens,'register'); + $iLeft = $iAllTokensCount - 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