Skip to content

Commit

Permalink
Dev #T266: Implement CPDB blacklist functions (#2202)
Browse files Browse the repository at this point in the history
* Dev #9629: Global opt out link for CPDB missing (#2125)

* Dev #9629: Global opt out link for CPDB missing

* Dev #9629: Global opt out link for CPDB missing

- Update email templates

* Revert "Dev #9629: Global opt out link for CPDB missing"

This reverts commit 17be686.

* Dev #9629: Global opt out link for CPDB missing

- Update descriptions

* Dev #9629: Global opt out link for CPDB missing

Code Review

Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>

* Dev #T266: Implement CPDB blacklist functions

* Dev #T266: Implement CPDB blacklist functions

* Dev #T266: Implement CPDB blacklist functions

* Dev #T266: Implement CPDB blacklist functions

* Dev #T266: Implement CPDB blacklist functions

- Hide the progress bar

* Dev #T266: Implement CPDB blacklist functions

- Fix setting checks
- Set 'Blacklisted' filter to 'Yes' if 'hideblacklisted' is set.

* Dev #T266: Implement CPDB blacklist functions

* Dev #T266: Implement CPDB blacklist functions

* Dev #T266: Implement CPDB blacklist functions

* Dev #T266: Implement CPDB blacklist functions

* Dev #T266: Implement CPDB blacklist functions

- Add unit test

* Dev #T266: Implement CPDB blacklist functions

- Add missing file

* Dev #T266: Implement CPDB blacklist functions

- Fix test

* Dev #T266: Implement CPDB blacklist functions

- Add extra data for the view

Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
  • Loading branch information
gabrieljenik and encuestabizdevgit committed Feb 21, 2022
1 parent 021dadf commit 78b022f
Show file tree
Hide file tree
Showing 16 changed files with 621 additions and 126 deletions.
2 changes: 2 additions & 0 deletions application/config/routes.php
Expand Up @@ -37,7 +37,9 @@
//optout - optin
$route['optout/<_sid:\d+>/(:any)/(:any)'] = "optout/index/<_sid>/$2/$3";
$route['optout/tokens/<surveyid:\d+>'] = array('optout/tokens', 'matchValue'=>true);
$route['optout/participants/<surveyid:\d+>'] = array('optout/participants', 'matchValue'=>true);
$route['optin/tokens/<surveyid:\d+>'] = array('optin/tokens', 'matchValue'=>true);
$route['optin/participants/<surveyid:\d+>'] = array('optin/participants', 'matchValue'=>true);
$route['statistics_user/<surveyid:\d+>'] = array('statistics_user/action', 'matchValue'=>true);

$route['<_controller:\w+>/<_action:\w+>'] = '<_controller>/<_action>';
Expand Down
Expand Up @@ -318,6 +318,7 @@ private function getReplacementFields($fieldtype, $surveyid)
$replFields['OPTOUTURL'] = gT("Participant - Opt-out URL");
$replFields['GLOBALOPTOUTURL'] = gT("Participant - Central Participant Opt-out URL");
$replFields['OPTINURL'] = gT("Participant - Opt-in URL");
$replFields['GLOBALOPTINURL'] = gT("Participant - Central Participant Opt-in URL");
$replFields['FIRSTNAME'] = gT("Participant - First name");
$replFields['LASTNAME'] = gT("Participant - Last name");
$replFields['SURVEYNAME'] = gT("Survey title");
Expand Down
215 changes: 183 additions & 32 deletions application/controllers/OptinController.php
Expand Up @@ -27,68 +27,219 @@ class OptinController extends LSYii_Controller
public $layout = 'bare';
public $defaultAction = 'tokens';

public function actiontokens($surveyid, $token, $langcode = '')
/**
* Display the confirmation for individual survey opt in
*/
public function actiontokens()
{
Yii::app()->loadHelper('database');
Yii::app()->loadHelper('sanitize');
$sLanguageCode = $langcode;
$iSurveyID = $surveyid;
$oSurvey = Survey::model()->findByPk($iSurveyID);
$sToken = $token;
$sToken = Token::sanitizeToken($sToken);

if (!$iSurveyID) {
$this->redirect(array('/'));

$surveyId = Yii::app()->request->getQuery('surveyid');
$languageCode = Yii::app()->request->getQuery('langcode');
$accessToken = Token::sanitizeToken(Yii::app()->request->getQuery('token'));

//IF there is no survey id, redirect back to the default public page
if (!$surveyId) {
$this->redirect(['/']);
}

$survey = Survey::model()->findByPk($surveyId);
if (empty($survey) || !$survey->hasTokensTable) {
throw new CHttpException(404, "This survey does not seem to exist. It may have been deleted or the link you were given is outdated or incorrect.");
}
$iSurveyID = $oSurvey->primaryKey;

//Check that there is a SID
// Get passed language from form, so that we dont loose this!
if (!isset($sLanguageCode) || $sLanguageCode == "" || !$sLanguageCode) {
$sBaseLanguage = $oSurvey->language;
// Get passed language from form, so that we dont lose this!
if (!isset($languageCode) || $languageCode == "" || !$languageCode) {
$baseLanguage = $survey->language;
} else {
$sBaseLanguage = sanitize_languagecode($sLanguageCode);
$baseLanguage = sanitize_languagecode($languageCode);
}

Yii::app()->setLanguage($sBaseLanguage);
Yii::app()->setLanguage($baseLanguage);

LimeExpressionManager::singleton()->loadTokenInformation($surveyId, $accessToken, false);
$token = Token::model($surveyId)->findByAttributes(['token' => $accessToken]);

if (empty($oSurvey) || !$oSurvey->hasTokensTable) {
$link = '';
$tokenAttributes = [];
if (!isset($token)) {
$message = gT('You are not a participant of this survey.');
} else {
if ($token->emailstatus != 'OptOut') {
$message = gT('You are already a participant of this survey.');
} else {
$message = gT('Please confirm that you want to be added back to this survey by clicking the button below.') . '<br>' . gT("After confirmation you may start receiving invitations and reminders for this survey.");
$link = Yii::app()->createUrl('optin/addtokens', ['surveyid' => $surveyId, 'langcode' => $baseLanguage, 'token' => $accessToken]);
}
$tokenAttributes = $token->getAttributes();
}

$this->renderHtml($message, $survey, $link, $tokenAttributes);
}

/**
* Display the confirmation for global opt in
*/
public function actionparticipants()
{
Yii::app()->loadHelper('database');
Yii::app()->loadHelper('sanitize');

$surveyId = Yii::app()->request->getQuery('surveyid');
$languageCode = Yii::app()->request->getQuery('langcode');
$accessToken = Token::sanitizeToken(Yii::app()->request->getQuery('token'));

//IF there is no survey id, redirect back to the default public page
if (!$surveyId) {
$this->redirect(['/']);
}

$survey = Survey::model()->findByPk($surveyId);
if (empty($survey) || !$survey->hasTokensTable) {
throw new CHttpException(404, "This survey does not seem to exist. It may have been deleted or the link you were given is outdated or incorrect.");
}

//Check that there is a SID
// Get passed language from form, so that we dont lose this!
if (!isset($languageCode) || $languageCode == "" || !$languageCode) {
$baseLanguage = $survey->language;
} else {
$baseLanguage = sanitize_languagecode($languageCode);
}

Yii::app()->setLanguage($baseLanguage);

LimeExpressionManager::singleton()->loadTokenInformation($surveyId, $accessToken, false);
$token = Token::model($surveyId)->findByAttributes(['token' => $accessToken]);

$link = '';
$tokenAttributes = [];
$participantAttributes = [];
if (!isset($token)) {
$message = gT('You are not a participant of this survey.');
} else {
LimeExpressionManager::singleton()->loadTokenInformation($iSurveyID, $sToken, false);
$oToken = Token::model($iSurveyID)->findByAttributes(array('token' => $sToken));
$optedOutFromSurvey = substr($token->emailstatus, 0, strlen('OptOut')) == 'OptOut';

if (!isset($oToken)) {
$sMessage = gT('You are not a participant of this survey.');
$blacklistHandler = new LimeSurvey\Models\Services\ParticipantBlacklistHandler();
$participant = $blacklistHandler->getCentralParticipantFromToken($token);
$isBlacklisted = !empty($participant) && $participant->blacklisted == 'Y';

if (!Yii::app()->getConfig('allowunblacklist') == "Y") {
$message = gT('Removing yourself from the blacklist is currently disabled.');
} elseif ($isBlacklisted) {
$message = gT('Please confirm that you want to be added back to the central participants list for this site.');
$link = Yii::app()->createUrl('optin/addtokens', ['surveyid' => $surveyId, 'langcode' => $baseLanguage, 'token' => $accessToken, 'global' => true]);
} elseif ($optedOutFromSurvey) {
$message = gT('Please confirm that you want to be added back to this survey by clicking the button below.') . '<br>' . gT("After confirmation you may start receiving invitations and reminders for this survey.");
$link = Yii::app()->createUrl('optin/addtokens', ['surveyid' => $surveyId, 'langcode' => $baseLanguage, 'token' => $accessToken]);
} elseif (empty($participant)) {
$message = gT('You are already a participant of this survey.');
} else {
$message = gT('You are already part of the central participants list for this site.');
}

$tokenAttributes = $token->getAttributes();
if (!empty($participant)) {
$participantAttributes = $participant->getAttributes();
}
}

$this->renderHtml($message, $survey, $link, $tokenAttributes, $participantAttributes);
}

/**
* Add token back to the survey (remove 'OptOut' status) and/or add participant back to the CPDB (remove from blacklist).
* The participant is only removed from the blacklist if the 'global' URL param is true and 'allowunblacklist' is enabled.
*/
public function actionaddtokens()
{
$surveyId = Yii::app()->request->getQuery('surveyid');
$languageCode = Yii::app()->request->getQuery('langcode');
$accessToken = Token::sanitizeToken(Yii::app()->request->getQuery('token'));
$global = Yii::app()->request->getQuery('global');

Yii::app()->loadHelper('database');
Yii::app()->loadHelper('sanitize');

if (!$surveyId) {
$this->redirect(['/']);
}

$survey = Survey::model()->findByPk($surveyId);
if (empty($survey) || !$survey->hasTokensTable) {
throw new CHttpException(404, "This survey does not seem to exist. It may have been deleted or the link you were given is outdated or incorrect.");
}

//Check that there is a SID
// Get passed language from form, so that we dont loose this!
if (!isset($languageCode) || $languageCode == "" || !$languageCode) {
$baseLanguage = $survey->language;
} else {
$baseLanguage = sanitize_languagecode($languageCode);
}

Yii::app()->setLanguage($baseLanguage);

LimeExpressionManager::singleton()->loadTokenInformation($surveyId, $accessToken, false);
$token = Token::model($surveyId)->findByAttributes(['token' => $accessToken]);

$tokenAttributes = [];
$participantAttributes = [];
if (!isset($token)) {
$message = gT('You are not a participant of this survey.');
} else {
if ($token->emailstatus == 'OptOut') {
$token->emailstatus = 'OK';
$token->save();
$message = gT('You have been successfully added back to this survey.');
} elseif ($token->emailstatus == 'OK') {
$message = gT('You are already a participant of this survey.');
} else {
if ($oToken->emailstatus == 'OptOut') {
$oToken->emailstatus = 'OK';
$oToken->save();
$sMessage = gT('You have been successfully added back to this survey.');
} elseif ($oToken->emailstatus == 'OK') {
$sMessage = gT('You are already a participant of this survey.');
} else {
$sMessage = gT('You have been already removed from this survey.');
$message = gT('You have been already removed from this survey.');
}
// If the $global param is true and 'allowunblacklist' is enabled, remove from the blacklist
if ($global && Yii::app()->getConfig('allowunblacklist') == "Y") {
$blacklistHandler = new LimeSurvey\Models\Services\ParticipantBlacklistHandler();
$blacklistResult = $blacklistHandler->removeFromBlacklist($token);
if (!$blacklistResult->isBlacklisted()) {
foreach ($blacklistResult->getMessages() as $blacklistMessage) {
$message .= "<br>" . $blacklistMessage;
}
}
$participant = $blacklistHandler->getCentralParticipantFromToken($token);
if (!empty($participant)) {
$participantAttributes = $participant->getAttributes();
}
}
$tokenAttributes = $token->getAttributes();
}

$this->renderHtml($sMessage, $oSurvey);

$this->renderHtml($message, $survey, '', $tokenAttributes, $participantAttributes);
}

/**
* Render stuff
*
* @param string $html
* @param string $message
* @param Survey $survey
* @param string $link
* @param array<string,mixed> $token
* @param array<string,mixed> $participant
* @return void
*/
private function renderHtml($html, $survey)
private function renderHtml($message, $survey, $link = '', $token = [], $participant = [])
{
$aSurveyInfo = getSurveyInfo($survey->primaryKey);

$aSurveyInfo['include_content'] = 'optin';
$aSurveyInfo['optin_message'] = $html;
$aSurveyInfo['optin_message'] = $message;
$aSurveyInfo['optin_link'] = $link;
$aSurveyInfo['aCompleted'] = true; // Avoid showing the progress bar
$aSurveyInfo['token'] = $token;
$aSurveyInfo['participant'] = $participant;
Template::getInstance('', $survey->primaryKey);

Yii::app()->twigRenderer->renderTemplateFromFile(
Expand Down

0 comments on commit 78b022f

Please sign in to comment.