Skip to content

Commit

Permalink
Merge branch 'task/LS-CR-1025_activateSurvey' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	application/views/admin/token/tokensummary.php
#	application/views/admin/token/tokenwarning.php
#	application/views/surveyAdministration/surveyActivation/_activation_feedback.php
  • Loading branch information
Trischi80 committed Jun 12, 2023
2 parents 42d6762 + 460ec59 commit 561cfeb
Show file tree
Hide file tree
Showing 21 changed files with 745 additions and 420 deletions.
251 changes: 167 additions & 84 deletions application/controllers/SurveyAdministrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ public function actionView()

// Display 'Overview' in Green Bar
// $aData['subaction'] = gT('Overview');
$surveyActivationFeedback = Yii::app()->request->getParam('surveyActivationFeedback', null);
$aData['surveyActivationFeedback'] = $surveyActivationFeedback;

$this->aData = $aData;
$this->render('sidebody', [
Expand Down Expand Up @@ -1722,111 +1724,192 @@ public function actionDeactivate()
$this->render('deactivateSurvey_view', $aData);
}

/**
* fixes the numbering of questions
* This can happen if question 1 have subquestion code 1 and
* have question 11 in same survey and group (then same SGQA).
*
* @todo: maybe this one could not happen anymore ?
*
* @return array|false|string|string[]|null
* @throws CException
*/
public function actionFixNumbering()
{
//get params surveyid and questionid
$surveyId = sanitize_int(Yii::app()->request->getParam('iSurveyID', 0));
$questionId = sanitize_int(Yii::app()->request->getParam('questionId', 0));

$success = false;
if (($surveyId > 0) && ($questionId > 0)) {
fixNumbering($questionId, $surveyId);
$success = true;
}

return $this->renderPartial(
'/admin/super/_renderJson',
[
'data' => [
'success' => $success,
//'html' => $html, todo: should we give any feedback here?
]
],
);
}

/**
* Function responsible to activate survey.
* This action renders the view for survey activation where
* the user can preselect some options like "ipanonymize" etc.
* It is also possible to switch between the "open access mode" and
* the "close-access-mode" before the survey is activated.
* The action also checks if it is even possible to activate the survey
* (see checkGroup() and checkQuestions() for more information).
*
* @param int $iSurveyID Given Survey ID
* @return void
*/
public function actionActivateSurvey()
{
$surveyId = (int) Yii::app()->request->getPost('surveyId');
if (!Permission::model()->hasSurveyPermission($surveyId, 'surveyactivation', 'update')) {
Yii::app()->user->setFlash('error', gT("Access denied"));
$this->redirect(Yii::app()->request->urlReferrer);
}
$oSurvey = Survey::model()->findByPk($surveyId);
$aSurveysettings = getSurveyInfo($surveyId);

Yii::app()->loadHelper("admin/activate");
$failedgroupcheck = checkGroup($surveyId);
$failedcheck = checkQuestions($surveyId, $surveyId);
$checkFailed = (isset($failedcheck) && $failedcheck) || (isset($failedgroupcheck) && $failedgroupcheck);
$footerButton = '';
if ($checkFailed) {
//survey can not be activated
$html = $this->renderPartial(
'/surveyAdministration/surveyActivation/_activateSurveyCheckFailed',
[
'failedcheck' => $failedcheck,
'failedgroupcheck' => $failedgroupcheck,
'surveyid' => $oSurvey->sid
],
true
);
$footerButton = $this->renderPartial(
'/surveyAdministration/surveyActivation/_failedFooterBtn',
[],
true
);
} else {
//check if survey is in "open-access-mode"
$survey = Survey::model()->findByPk($surveyId);
$surveyActivator = new SurveyActivator($survey);
$html = $this->renderPartial(
'/surveyAdministration/surveyActivation/_activateSurveyOptions',
[
'oSurvey' => $oSurvey,
'aSurveysettings' => $aSurveysettings,
'closeAccessMode' => $surveyActivator->isCloseAccessMode(),
],
true
);
}

return $this->renderPartial(
'/admin/super/_renderJson',
[
'data' => [
'success' => true,
'html' => $html,
'checkFailed' => $checkFailed,
'footerButton' => $footerButton
]
],
);
}

/**
* This action activates the survey with selected options.
*
* @return void
* @access public
* @throws CException
*/
public function actionActivate($iSurveyID)
public function actionActivate()
{
if (!Permission::model()->hasSurveyPermission($iSurveyID, 'surveyactivation', 'update')) {
$surveyId = (int) Yii::app()->request->getPost('surveyId');
if (!Permission::model()->hasSurveyPermission($surveyId, 'surveyactivation', 'update')) {
Yii::app()->user->setFlash('error', gT("Access denied"));
$this->redirect(Yii::app()->request->urlReferrer);
}

$iSurveyID = (int) $iSurveyID;
$survey = Survey::model()->findByPk($iSurveyID);
$survey = Survey::model()->findByPk($surveyId);
$surveyActivator = new SurveyActivator($survey);

Yii::app()->user->setState('sql_' . $iSurveyID, ''); //If user has set some filters for responses from statistics on a previous activation, it must be wiped out
$aData = array();
$aData['oSurvey'] = $survey;
$aData['sidemenu']['state'] = false;
$aData['aSurveysettings'] = getSurveyInfo($iSurveyID);
$aData['surveyid'] = $iSurveyID;
$aData['sid'] = $iSurveyID;
$aData['title_bar']['title'] = $survey->currentLanguageSettings->surveyls_title . " (" . gT("ID") . ":" . $iSurveyID . ")";
$aData['topBar']['hide'] = true;
// Redirect if this is not possible
if (!isset($aData['aSurveysettings']['active']) || $aData['aSurveysettings']['active'] == 'Y') {
Yii::app()->setFlashMessage(gT("This survey is already active."), 'error');
$this->redirect(array('surveyAdministration/view', 'surveyid' => $iSurveyID));
}
Yii::app()->loadHelper("admin/activate");
$aData['aSurveysettings'] = getSurveyInfo($surveyId);
$aData['surveyid'] = $surveyId;

if (Yii::app()->request->getPost('ok') == '') {
if (isset($_GET['fixnumbering']) && $_GET['fixnumbering']) {
fixNumbering($_GET['fixnumbering'], $iSurveyID);
}
$openAccessMode = Yii::app()->request->getPost('openAccessMode', null);

// Check consistency for groups and questions
$failedgroupcheck = checkGroup($iSurveyID);
$failedcheck = checkQuestions($iSurveyID, $iSurveyID);
if (!is_null($survey)) {
$survey->anonymized = Yii::app()->request->getPost('anonymized');
$survey->datestamp = Yii::app()->request->getPost('datestamp');
$survey->ipaddr = Yii::app()->request->getPost('ipaddr');
$survey->ipanonymize = Yii::app()->request->getPost('ipanonymize');
$survey->refurl = Yii::app()->request->getPost('refurl');
$survey->savetimings = Yii::app()->request->getPost('savetimings');
$survey->save();

$aData['failedcheck'] = $failedcheck;
$aData['failedgroupcheck'] = $failedgroupcheck;
$aData['aSurveysettings'] = getSurveyInfo($iSurveyID);
// Make sure the saved values will be picked up
Survey::model()->resetCache();
$survey->setOptions();
}

$this->aData = $aData;
$this->render('activateSurvey_view', $aData);
$aResult = $surveyActivator->activate();
if (
(isset($aResult['error']) && $aResult['error'] == 'plugin')
|| (isset($aResult['blockFeedback']) && $aResult['blockFeedback'])
) {
// Got false from plugin, redirect to survey front-page
$this->redirect(array('surveyAdministration/view', 'surveyid' => $surveyId));
} elseif (isset($aResult['pluginFeedback'])) {
// Special feedback from plugin should be given to user
//todo: what should be done here ...
$this->render('surveyActivation/_activation_feedback', $aResult);
} elseif (isset($aResult['error'])) {
$data['result'] = $aResult;
//$this->aData = $aData;
//todo: what should be done here ...
$this->render('surveyActivation/_activation_error', $data);
} else {
if (!is_null($survey)) {
$survey->anonymized = Yii::app()->request->getPost('anonymized');
$survey->datestamp = Yii::app()->request->getPost('datestamp');
$survey->ipaddr = Yii::app()->request->getPost('ipaddr');
$survey->ipanonymize = Yii::app()->request->getPost('ipanonymize');
$survey->refurl = Yii::app()->request->getPost('refurl');
$survey->savetimings = Yii::app()->request->getPost('savetimings');
$survey->save();

// Make sure the saved values will be picked up
Survey::model()->resetCache();
$survey->setOptions();
$warning = (isset($aResult['warning'])) ? true : false;
$allowregister = $survey->isAllowRegister; //todo: where to ask for this one here

if ($openAccessMode !== null) {
switch ($openAccessMode) {
case 'Y': //show a modal or give feedback on another page
$this->redirect([
'/surveyAdministration/view/',
'surveyid' => $surveyId,
'surveyActivationFeedback' => 'surveyActivationFeedback'
]);
break;
case 'N': //check if token table exists or 'allowRegister' set to true
$this->redirect([
'/admin/tokens/sa/index/',
'surveyid' => $surveyId,
'surveyActivationFeedback' => 'surveyActivationFeedback'
]);
break;
default: //this should never happen exception ...
}
}

$aResult = $surveyActivator->activate();

$aViewUrls = array();
if (
(isset($aResult['error']) && $aResult['error'] == 'plugin')
|| (isset($aResult['blockFeedback']) && $aResult['blockFeedback'])
) {
// Got false from plugin, redirect to survey front-page
$this->redirect(array('surveyAdministration/view', 'surveyid' => $iSurveyID));
} elseif (isset($aResult['pluginFeedback'])) {
// Special feedback from plugin
$aViewUrls['output'] = $aResult['pluginFeedback'];
//check if feedback is given to user ...
$this->aData = $aData;
$this->render('_activation_feedback', $aData);
} elseif (isset($aResult['error'])) {
$data['result'] = $aResult;
$this->aData = $aData;
$this->render('_activation_error', $data);
} else {
$warning = (isset($aResult['warning'])) ? true : false;
$allowregister = $survey->isAllowRegister;
$onclickAction = convertGETtoPOST(Yii::app()->getController()->createUrl("admin/tokens/sa/index/surveyid/" . $iSurveyID));
$closedOnclickAction = convertGETtoPOST(Yii::app()->getController()->createUrl("admin/tokens/sa/index/surveyid/" . $iSurveyID));
$noOnclickAction = "window.location.href='" . (Yii::app()->getController()->createUrl("surveyAdministration/view/surveyid/" . $iSurveyID)) . "'";

$activationData = array(
'iSurveyID' => $iSurveyID,
'survey' => $survey,
'warning' => $warning,
'allowregister' => $allowregister,
'onclickAction' => $onclickAction,
'closedOnclickAction' => $closedOnclickAction,
'noOnclickAction' => $noOnclickAction,
);
$this->aData = $aData;
$this->render('_activation_feedback', $activationData);
}
$activationData = array(
'iSurveyID' => $surveyId,
'survey' => $survey,
'warning' => $warning,
'allowregister' => $allowregister,
);
$this->aData = $aData;
$this->render('surveyActivation/_activation_feedback', $activationData);
}
}

Expand Down
8 changes: 5 additions & 3 deletions application/controllers/admin/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function index(int $surveyid, $limit = 50, $start = 0)
$iSurveyId = (int) $iSurveyId;
$survey = Survey::model()->findByPk($iSurveyId);

//// TODO : check if it does something different than the model function
//TODO : check if it does something different than the model function
$thissurvey = getSurveyInfo($iSurveyId);
if (
!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'read') && !Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'create') && !Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'update')
Expand Down Expand Up @@ -143,6 +143,7 @@ public function index(int $surveyid, $limit = 50, $start = 0)
$aData['massiveAction'] = App()->getController()->renderPartial('/admin/token/massive_actions/_selector', $aData, true, false);

// CHECK TO SEE IF A Survey participants table EXISTS FOR THIS SURVEY
$aData['surveyActivationFeedback'] = Yii::app()->request->getParam('surveyActivationFeedback', null);
if (!$survey->hasTokensTable) {
$this->newtokentable($iSurveyId);
} else {
Expand Down Expand Up @@ -2756,7 +2757,8 @@ private function newtokentable($iSurveyId)
{
$aSurveyInfo = getSurveyInfo($iSurveyId);
$survey = Survey::model()->findByPk($iSurveyId);
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'surveysettings', 'update') && !Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'create')) {
if (!Permission::model()->hasSurveyPermission($iSurveyId, 'surveysettings', 'update') &&
!Permission::model()->hasSurveyPermission($iSurveyId, 'tokens', 'create')) {
Yii::app()->session['flashmessage'] = gT("Survey participants have not been initialised for this survey.");
$this->getController()->redirect(array("/surveyAdministration/view/surveyid/{$iSurveyId}"));
}
Expand Down Expand Up @@ -2872,7 +2874,7 @@ private function newtokentable($iSurveyId)
$aData
);
}
} else {
} else { //this is the case when "allowPublicRegistration" = Y and no token table has been created
Yii::app()->loadHelper('database');
Survey::model()->updateByPk($iSurveyId, array('tokenencryptionoptions' => ls_json_encode($aTokenencryptionoptions)));
$result = Yii::app()->db->createCommand(dbSelectTablesLike("{{old_tokens_" . intval($iSurveyId) . "_%}}"))->queryAll();
Expand Down
3 changes: 2 additions & 1 deletion application/helpers/admin/activate_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ function checkQuestions($postsid, $iSurveyID)
$fieldmap = createFieldMap($survey, 'full', true, false, $survey->language, $aDuplicateQIDs);
if (count($aDuplicateQIDs)) {
foreach ($aDuplicateQIDs as $iQID => $aDuplicate) {
$sFixLink = "[<a class='selector__fixConsistencyProblem' href='" . Yii::app()->getController()->createUrl("/surveyAdministration/activate/iSurveyID/{$iSurveyID}/fixnumbering/{$iQID}") . "'>Click here to fix</a>]";
$sFixLink = "[<a class='selector__fixConsistencyProblem'
href='" . Yii::app()->getController()->createUrl("/surveyAdministration/fixNumbering/iSurveyID/{$iSurveyID}/questionId/{$iQID}") . "'>Click here to fix</a>]";
$failedcheck[] = array($iQID, $aDuplicate['question'], ": Bad duplicate fieldname {$sFixLink}", $aDuplicate['gid']);
}
}
Expand Down
14 changes: 14 additions & 0 deletions application/models/SurveyActivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function __construct($survey)
}

/**
* Sets a survey into "activate" state.
* Creates necessary tables "responseTable", "timingTable".
* Fires events "beforeSurveyActivate" and "afterSurveyActivation"
*
* @return array
* @throws CException
*/
Expand Down Expand Up @@ -457,4 +461,14 @@ private function setMySQLDefaultEngine($dbEngine)
$db->createCommand(new CDbExpression(sprintf('SET default_storage_engine=%s;', $dbEngine)))
->execute();
}

/**
* Checks if the survey is in close access mode.
*
* @return bool
*/
public function isCloseAccessMode()
{
return $this->survey->isAllowRegister || tableExists('tokens_' . $this->survey->sid);
}
}
5 changes: 5 additions & 0 deletions application/views/admin/super/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ class='btn btn-warning btn-sm'
</div>
</div>

<?php
//modal for survey activation
App()->getController()->renderPartial('/surveyAdministration/partial/topbar/_modalSurveyActivation');
?>

<!-- Modal for admin notifications -->
<div id="admin-notification-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
Expand Down

0 comments on commit 561cfeb

Please sign in to comment.