Пример #1
0
 /**
  * Send the register email with $_POST value
  * @param $iSurveyId Survey Id to register
  * @return boolean : if email is set to sent (before SMTP problem)
  */
 public function sendRegistrationEmail($iSurveyId, $iTokenId)
 {
     $sLanguage = App()->language;
     $aSurveyInfo = getSurveyInfo($iSurveyId, $sLanguage);
     $aMail['subject'] = $aSurveyInfo['email_register_subj'];
     $aMail['message'] = $aSurveyInfo['email_register'];
     $aReplacementFields = array();
     $aReplacementFields["{ADMINNAME}"] = $aSurveyInfo['adminname'];
     $aReplacementFields["{ADMINEMAIL}"] = $aSurveyInfo['adminemail'];
     $aReplacementFields["{SURVEYNAME}"] = $aSurveyInfo['name'];
     $aReplacementFields["{SURVEYDESCRIPTION}"] = $aSurveyInfo['description'];
     $aReplacementFields["{EXPIRY}"] = $aSurveyInfo["expiry"];
     $oToken = Token::model($iSurveyId)->findByPk($iTokenId);
     // Reload the token (needed if just created)
     foreach ($oToken->attributes as $attribute => $value) {
         $aReplacementFields["{" . strtoupper($attribute) . "}"] = $value;
     }
     $sToken = $oToken->token;
     $useHtmlEmail = getEmailFormat($iSurveyId) == 'html';
     $aMail['subject'] = preg_replace("/{TOKEN:([A-Z0-9_]+)}/", "{" . "\$1" . "}", $aMail['subject']);
     $aMail['message'] = preg_replace("/{TOKEN:([A-Z0-9_]+)}/", "{" . "\$1" . "}", $aMail['message']);
     $aReplacementFields["{SURVEYURL}"] = App()->createAbsoluteUrl("/survey/index/sid/{$iSurveyId}", array('lang' => $sLanguage, 'token' => $sToken));
     $aReplacementFields["{OPTOUTURL}"] = App()->createAbsoluteUrl("/optout/tokens/surveyid/{$iSurveyId}", array('langcode' => $sLanguage, 'token' => $sToken));
     $aReplacementFields["{OPTINURL}"] = App()->createAbsoluteUrl("/optin/tokens/surveyid/{$iSurveyId}", array('langcode' => $sLanguage, 'token' => $sToken));
     foreach (array('OPTOUT', 'OPTIN', 'SURVEY') as $key) {
         $url = $aReplacementFields["{{$key}URL}"];
         if ($useHtmlEmail) {
             $aReplacementFields["{{$key}URL}"] = "<a href='{$url}'>" . htmlspecialchars($url) . '</a>';
         }
         $aMail['subject'] = str_replace("@@{$key}URL@@", $url, $aMail['subject']);
         $aMail['message'] = str_replace("@@{$key}URL@@", $url, $aMail['message']);
     }
     // Replace the fields
     $aMail['subject'] = ReplaceFields($aMail['subject'], $aReplacementFields);
     $aMail['message'] = ReplaceFields($aMail['message'], $aReplacementFields);
     $sFrom = "{$aSurveyInfo['adminname']} <{$aSurveyInfo['adminemail']}>";
     $sBounce = getBounceEmail($iSurveyId);
     $sTo = $oToken->email;
     $sitename = Yii::app()->getConfig('sitename');
     // Plugin event for email handling (Same than admin token but with register type)
     $event = new PluginEvent('beforeTokenEmail');
     $event->set('type', 'register');
     $event->set('subject', $aMail['subject']);
     $event->set('to', $sTo);
     $event->set('body', $aMail['message']);
     $event->set('from', $sFrom);
     $event->set('bounce', $sBounce);
     $event->set('token', $oToken->attributes);
     $aMail['subject'] = $event->get('subject');
     $aMail['message'] = $event->get('body');
     $sTo = $event->get('to');
     $sFrom = $event->get('from');
     if ($event->get('send', true) == false) {
         $this->sMessage = $event->get('message', '');
         if ($event->get('error') == null) {
             // mimic token system, set send to today
             $today = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig('timeadjust'));
             $oToken->sent = $today;
             $oToken->save();
         }
     } elseif (SendEmailMessage($aMail['message'], $aMail['subject'], $sTo, $sFrom, $sitename, $useHtmlEmail, $sBounce)) {
         // TLR change to put date into sent
         $today = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig('timeadjust'));
         $oToken->sent = $today;
         $oToken->save();
         $this->sMessage = "<div id='wrapper' class='message tokenmessage'>" . "<p>" . gT("Thank you for registering to participate in this survey.") . "</p>\n" . "<p>{$this->sMailMessage}</p>\n" . "<p>" . sprintf(gT("Survey administrator %s (%s)"), $aSurveyInfo['adminname'], $aSurveyInfo['adminemail']) . "</p>" . "</div>\n";
     } else {
         $this->sMessage = "<div id='wrapper' class='message tokenmessage'>" . "<p>" . gT("Thank you for registering to participate in this survey.") . "</p>\n" . "<p>" . gT("You are registered but an error happened when trying to send the email - please contact the survey administrator.") . "</p>\n" . "<p>" . sprintf(gT("Survey administrator %s (%s)"), $aSurveyInfo['adminname'], $aSurveyInfo['adminemail']) . "</p>" . "</div>\n";
     }
     // Allways return true : if we come here, we allways trye to send an email
     return true;
 }
Пример #2
0
/**
* checkCompletedQuota() returns matched quotas information for the current response
* @param integer $surveyid - Survey identification number
* @param bool $return - set to true to return information, false do the quota
* @return array - nested array, Quotas->Members->Fields, includes quota information matched in session.
*/
function checkCompletedQuota($surveyid, $return = false)
{
    if (!isset($_SESSION['survey_' . $surveyid]['srid'])) {
        return;
    }
    static $aMatchedQuotas;
    // EM call 2 times quotas with 3 lines of php code, then use static.
    if (!$aMatchedQuotas) {
        $aMatchedQuotas = array();
        $quota_info = $aQuotasInfo = getQuotaInformation($surveyid, $_SESSION['survey_' . $surveyid]['s_lang']);
        // $aQuotasInfo have an 'active' key, we don't use it ?
        if (!$aQuotasInfo || empty($aQuotasInfo)) {
            return $aMatchedQuotas;
        }
        // Test only completed quota, other is not needed
        $aQuotasCompleted = array();
        foreach ($aQuotasInfo as $aQuotaInfo) {
            $iCompleted = getQuotaCompletedCount($surveyid, $aQuotaInfo['id']);
            // Return a string
            if (ctype_digit($iCompleted) && (int) $iCompleted >= (int) $aQuotaInfo['qlimit']) {
                // This remove invalid quota and not completed
                $aQuotasCompleted[] = $aQuotaInfo;
            }
        }
        if (empty($aQuotasCompleted)) {
            return $aMatchedQuotas;
        }
        // OK, we have some quota, then find if this $_SESSION have some set
        $aPostedFields = explode("|", Yii::app()->request->getPost('fieldnames', ''));
        // Needed for quota allowing update
        foreach ($aQuotasCompleted as $aQuotaCompleted) {
            $iMatchedAnswers = 0;
            $bPostedField = false;
            // Array of field with quota array value
            $aQuotaFields = array();
            // Array of fieldnames with relevance value : EM fill $_SESSION with default value even is unrelevant (em_manager_helper line 6548)
            $aQuotaRelevantFieldnames = array();
            foreach ($aQuotaCompleted['members'] as $aQuotaMember) {
                $aQuotaFields[$aQuotaMember['fieldname']][] = $aQuotaMember['value'];
                $aQuotaRelevantFieldnames[$aQuotaMember['fieldname']] = isset($_SESSION['survey_' . $surveyid]['relevanceStatus'][$aQuotaMember['qid']]) && $_SESSION['survey_' . $surveyid]['relevanceStatus'][$aQuotaMember['qid']];
            }
            // For each field : test if actual responses is in quota (and is relevant)
            foreach ($aQuotaFields as $sFieldName => $aValues) {
                $bInQuota = isset($_SESSION['survey_' . $surveyid][$sFieldName]) && in_array($_SESSION['survey_' . $surveyid][$sFieldName], $aValues);
                if ($bInQuota && $aQuotaRelevantFieldnames[$sFieldName]) {
                    $iMatchedAnswers++;
                }
                if (in_array($sFieldName, $aPostedFields)) {
                    $bPostedField = true;
                }
            }
            if ($iMatchedAnswers == count($aQuotaFields)) {
                switch ($aQuotaCompleted['action']) {
                    case '1':
                    default:
                        $aMatchedQuotas[] = $aQuotaCompleted;
                        break;
                    case '2':
                        if ($bPostedField) {
                            // Action 2 allow to correct last answers, then need to be posted
                            $aMatchedQuotas[] = $aQuotaCompleted;
                        }
                        break;
                }
            }
        }
    }
    if ($return) {
        return $aMatchedQuotas;
    }
    if (empty($aMatchedQuotas)) {
        return;
    }
    // Now we have all the information we need about the quotas and their status.
    // We need to construct the page and do all needed action
    $thissurvey = getSurveyInfo($surveyid, $_SESSION['survey_' . $surveyid]['s_lang']);
    $sTemplatePath = getTemplatePath($thissurvey['templatedir']);
    $sClientToken = isset($_SESSION['survey_' . $surveyid]['token']) ? $_SESSION['survey_' . $surveyid]['token'] : "";
    $redata = compact(array_keys(get_defined_vars()));
    // We take only the first matched quota, no need for each
    $aMatchedQuota = $aMatchedQuotas[0];
    // If a token is used then mark the token as completed, do it before event : this allow plugin to update token information
    $event = new PluginEvent('afterSurveyQuota');
    $event->set('surveyId', $surveyid);
    $event->set('responseId', $_SESSION['survey_' . $surveyid]['srid']);
    // We allways have a responseId
    $event->set('aMatchedQuotas', $aMatchedQuotas);
    // Give all the matched quota : the first is the active
    $blocks = array();
    foreach ($event->getAllContent() as $blockData) {
        /* @var $blockData PluginEventContent */
        $blocks[] = CHtml::tag('div', array('id' => $blockData->getCssId(), 'class' => $blockData->getCssClass()), $blockData->getContent());
    }
    // Allow plugin to update message, url, url description and action
    $sMessage = $event->get('message', $aMatchedQuota['quotals_message']);
    $sUrl = $event->get('url', $aMatchedQuota['quotals_url']);
    $sUrlDescription = $event->get('urldescrip', $aMatchedQuota['quotals_urldescrip']);
    $sAction = $event->get('action', $aMatchedQuota['action']);
    $sAutoloadUrl = $event->get('autoloadurl', $aMatchedQuota['autoload_url']);
    // Doing the action and show the page
    if ($sAction == "1" && $sClientToken) {
        submittokens(true);
    }
    // Construct the default message
    $sMessage = templatereplace($sMessage, array(), $redata);
    $sUrl = passthruReplace($sUrl, $thissurvey);
    $sUrl = templatereplace($sUrl, array(), $redata);
    $sUrlDescription = templatereplace($sUrlDescription, array(), $redata);
    // Construction of default message inside quotamessage class
    $quotaMessage = "<div class='quotamessage limesurveycore'>\n";
    $quotaMessage .= "\t" . $sMessage . "\n";
    if ($sUrl || $sUrl) {
        echo "<br /><br />\t<a href='" . $sUrl . "'>" . $sUrlDescription . "</a><br />\n";
    }
    // Add the navigator with Previous button if quota allow modification.
    if ($sAction == "2") {
        $sQuotaStep = isset($_SESSION['survey_' . $surveyid]['step']) ? $_SESSION['survey_' . $surveyid]['step'] : 0;
        // Surely not needed
        $sNavigator = CHtml::htmlButton(gT("Previous"), array('type' => 'submit', 'id' => "moveprevbtn", 'value' => $sQuotaStep, 'name' => 'move', 'accesskey' => 'p', 'class' => "submit button"));
        //$sNavigator .= " ".CHtml::htmlButton(gT("Submit"),array('type'=>'submit','id'=>"movesubmit",'value'=>"movesubmit",'name'=>"movesubmit",'accesskey'=>'l','class'=>"submit button"));
        $quotaMessage .= CHtml::form(array("/survey/index"), 'post', array('id' => 'limesurvey', 'name' => 'limesurvey'));
        $quotaMessage .= templatereplace(file_get_contents($sTemplatePath . "/navigator.pstpl"), array('NAVIGATOR' => $sNavigator, 'SAVE' => ''), $redata);
        $quotaMessage .= CHtml::hiddenField('sid', $surveyid);
        $quotaMessage .= CHtml::hiddenField('token', $sClientToken);
        // Did we really need it ?
        $quotaMessage .= CHtml::endForm();
    }
    $quotaMessage .= "</div>\n";
    // Add the plugin message before default message
    $quotaMessage = implode("\n", $blocks) . "\n" . $quotaMessage;
    // Send page to user and end.
    sendCacheHeaders();
    if ($sAutoloadUrl == 1 && $sUrl != "") {
        if ($sAction == "1") {
            killSurveySession($surveyid);
        }
        header("Location: " . $sUrl);
    }
    doHeader();
    echo templatereplace(file_get_contents($sTemplatePath . "/startpage.pstpl"), array(), $redata);
    // Better to use completed.pstpl, but some template can need update , leave it for 2.06 #09133
    echo $quotaMessage;
    echo templatereplace(file_get_contents($sTemplatePath . "/endpage.pstpl"), array(), $redata);
    doFooter();
    if ($sAction == "1") {
        killSurveySession($surveyid);
    }
    Yii::app()->end();
}