Skip to content

Commit

Permalink
Fixed issue #18605: Missing success alerts in participant view (#2876)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieljenik committed May 15, 2023
1 parent 30268ad commit 3ee487b
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 35 deletions.
89 changes: 70 additions & 19 deletions application/controllers/AssessmentController.php
Expand Up @@ -149,12 +149,18 @@ public function actionInsertUpdate($surveyid)
public function actionDelete($surveyid)
{
$iSurveyID = sanitize_int($surveyid);
if (Permission::model()->hasSurveyPermission($iSurveyID, 'assessments', 'delete')) {
//must be deleteAll because of languages...
$assessmentId = (int) App()->request->getPost('id');
Assessment::model()->deleteAllByAttributes(array('id' => $assessmentId, 'sid' => $iSurveyID));
Yii::import('application.helpers.admin.ajax_helper', true);

if (!Permission::model()->hasSurveyPermission($iSurveyID, 'assessments', 'delete')) {
\ls\ajax\AjaxHelper::outputError(gT("You have no permission to delete assessments"));
}
$assessmentId = (int) App()->request->getPost('id');
// Must be deleteAll because there is one record for each language.
$deletedAssessments = Assessment::model()->deleteAllByAttributes(array('id' => $assessmentId, 'sid' => $iSurveyID));
if ($deletedAssessments > 0) {
\ls\ajax\AjaxHelper::outputSuccess(gT('Assessment rule deleted.'));
} else {
Yii::app()->setFlashMessage(gT("You have no permission to delete assessments"), 'error');
\ls\ajax\AjaxHelper::outputError(gT('Could not delete assessment rule.'));
}
}

Expand All @@ -173,18 +179,42 @@ private function add($surveyid)
$languages = $oSurvey->additionalLanguages;
$surveyLanguage = $oSurvey->language;
array_unshift($languages, $surveyLanguage);
foreach ($languages as $sLanguage) {
$aData = $this->getAssessmentPostData($iSurveyID, $sLanguage);
if ($bFirst === false) {
$aData['id'] = $iAssessmentID;
}
$assessment = Assessment::model()->insertRecords($aData);

if ($bFirst === true) {
$bFirst = false;
$iAssessmentID = $assessment->id;
// There is one record for each language, so we either insert them all or none, to avoid inconsistencies.
$transaction = Yii::app()->db->beginTransaction();
$error = false;
try {
foreach ($languages as $sLanguage) {
$aData = $this->getAssessmentPostData($iSurveyID, $sLanguage);
// If not processing the first language, use the ID from the first one.
if ($bFirst === false) {
$aData['id'] = $iAssessmentID;
}
$assessment = Assessment::model()->insertRecords($aData);

if ($assessment->hasErrors()) {
$error = true;
break;
}

// If it's the first language, keep the ID for the next ones.
if ($bFirst === true) {
$bFirst = false;
$iAssessmentID = $assessment->id;
}
}
} catch (Exception $ex) {
$error = true;
}
if (empty($error)) {
$transaction->commit();
Yii::app()->setFlashMessage(gT("Assessment rule successfully added."));
} else {
$transaction->rollback();
Yii::app()->setFlashMessage(gT("Could not add the assessment rule."), 'error');
// TODO: Show error details to the user?
}
} else {
Yii::app()->setFlashMessage(gT("You have no permission to create assessments"), 'error');
}
$this->redirect($this->createUrl('/assessment/index', ['surveyid' => $surveyid]));
}
Expand All @@ -204,12 +234,33 @@ private function update($iSurveyID)
$languages = $oSurvey->additionalLanguages;
$surveyLanguage = $oSurvey->language;
array_unshift($languages, $surveyLanguage);
foreach ($languages as $language) {
$aData = $this->getAssessmentPostData($iSurveyID, $language);
Assessment::model()->updateAssessment($aid, $iSurveyID, $language, $aData);
// There is one record for each language, so we either update them all or none, to avoid inconsistencies.
$transaction = Yii::app()->db->beginTransaction();
$error = false;
try {
foreach ($languages as $language) {
$aData = $this->getAssessmentPostData($iSurveyID, $language);
$updated = Assessment::model()->updateAssessment($aid, $iSurveyID, $language, $aData);
if (!$updated) {
$error = true;
break;
}
}
} catch (Exception $ex) {
$error = true;
}
$this->redirect($this->createUrl('/assessment/index', ['surveyid' => $iSurveyID]));
if (empty($error)) {
$transaction->commit();
Yii::app()->setFlashMessage(gT("Assessment rule successfully updated."));
} else {
$transaction->rollback();
Yii::app()->setFlashMessage(gT("Could not update the assessment rule."), 'error');
// TODO: Show error details to the user?
}
} else {
Yii::app()->setFlashMessage(gT("You have no permission to update assessments"), 'error');
}
$this->redirect($this->createUrl('/assessment/index', ['surveyid' => $iSurveyID]));
}

/**
Expand Down
12 changes: 6 additions & 6 deletions application/controllers/QuotasController.php
Expand Up @@ -424,13 +424,13 @@ public function actionMassiveAction($action, $surveyid)
$aQuotaIds = json_decode($sItems);
if (isset($_POST['QuotaLanguageSetting'])) {
$errors = $quotaService->multipleItemsAction($aQuotaIds, $action, $_POST['QuotaLanguageSetting']);
if (empty($errors)) {
eT("OK!");
} else {
eT("Error!");
}
} else {
$quotaService->multipleItemsAction($aQuotaIds, $action);
$errors = $quotaService->multipleItemsAction($aQuotaIds, $action);
}
if (empty($errors)) {
eT("OK!");
} else {
eT("Error!");
}
} else {
Yii::app()->user->setFlash('error', gT("Access denied."));
Expand Down
2 changes: 2 additions & 0 deletions application/controllers/admin/Themes.php
Expand Up @@ -919,6 +919,8 @@ public function templatesavechanges()
if (!fwrite($handle, $changedtext)) {
Yii::app()->user->setFlash('error', gT('Could not write file ') . $savefilename);
$this->getController()->redirect(array("admin/themes/sa/upload"));
} else {
Yii::app()->setFlashMessage(gT("Changes saved successfully."));
}

$oEditedTemplate->actualizeLastUpdate();
Expand Down
16 changes: 14 additions & 2 deletions application/controllers/admin/Tokens.php
Expand Up @@ -253,7 +253,13 @@ public function deleteMultiple()
if (!Permission::model()->hasSurveyPermission($iSid, 'tokens', 'delete')) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
TokenDynamic::model($iSid)->deleteRecords($aTokenIds);
Yii::import('application.helpers.admin.ajax_helper', true);
$deletedTokenCount = TokenDynamic::model($iSid)->deleteRecords($aTokenIds);
if ($deletedTokenCount > 0) {
ls\ajax\AjaxHelper::outputSuccess(ngT('Deleted {n} survey participant.|Deleted {n} survey participants.', $deletedTokenCount));
} else {
ls\ajax\AjaxHelper::outputError(gT('Could not delete survey participants.'));
}
return true;
}

Expand All @@ -276,7 +282,12 @@ public function deleteToken()
if (!Permission::model()->hasSurveyPermission($iSid, 'tokens', 'delete')) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
TokenDynamic::model($iSid)->deleteRecords(array($aTokenId));
$deletedTokenCount = TokenDynamic::model($iSid)->deleteRecords(array($aTokenId));
if ($deletedTokenCount > 0) {
Yii::app()->setFlashMessage(ngT('Deleted {n} survey participant.|Deleted {n} survey participants.', $deletedTokenCount));
} else {
Yii::app()->setFlashMessage(gT('Could not delete survey participant.'), 'error');
}
$this->getController()->redirect(array("admin/tokens", "sa" => "browse", "surveyid" => $iSid));
}

Expand Down Expand Up @@ -632,6 +643,7 @@ public function addnew(int $iSurveyId)
} else {
$redirectUrl = Yii::app()->createUrl("/admin/tokens/sa/edit", ["iSurveyId" => $iSurveyId, "iTokenId" => $token->tid]);
}
Yii::app()->setFlashMessage(gT("The survey participant was successfully added."));
$this->getController()->redirect($redirectUrl);
}
$this->renderWrappedTemplate('token', array('addtokenpost'), $aData);
Expand Down
12 changes: 10 additions & 2 deletions application/controllers/admin/globalsettings.php
Expand Up @@ -490,16 +490,24 @@ public function surveySettings()

$sPartial = Yii::app()->request->getParam('partial', '_generaloptions_panel');

if (isset($_POST)) {
if (!empty($_POST)) {
$oSurveyGroupSetting->attributes = $_POST;
$oSurveyGroupSetting->gsid = 0;
$oSurveyGroupSetting->usecaptcha = Survey::saveTranscribeCaptchaOptions();

//todo: when changing ipanonymiez from "N" to "Y", call the function that anonymizes the ip-addresses


if ($oSurveyGroupSetting->save()) {
$bRedirect = 1;
Yii::app()->setFlashMessage(gT("Global survey settings were saved."));
} else {
Yii::app()->setFlashMessage(
CHtml::errorSummary(
$oSurveyGroupSetting,
CHtml::tag("p", ['class' => 'strong'], gT("Global survey settings could not be updated, please fix the following error:"))
),
"error"
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion application/models/Assessment.php
Expand Up @@ -230,6 +230,7 @@ public static function insertRecords($data)
* @param integer $iSurveyID
* @param string $language
* @param array $data
* @return bool True if the assessment could be updated. False if the assessment is not found of the update failed.
*/
public static function updateAssessment($id, $iSurveyID, $language, array $data)
{
Expand All @@ -238,8 +239,9 @@ public static function updateAssessment($id, $iSurveyID, $language, array $data)
foreach ($data as $k => $v) {
$assessment->$k = $v;
}
$assessment->save();
return $assessment->save();
}
return false;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion application/models/services/Quotas.php
Expand Up @@ -321,7 +321,9 @@ public function multipleItemsAction($aQuotaIds, $action, $languageSettings = [])
case 'activate':
case 'deactivate':
$oQuota->active = ($action == 'activate' ? 1 : 0);
$oQuota->save();
if (!$oQuota->save()) {
$errors[] = $oQuota->errors;
}
break;
case 'delete':
$oQuota->delete();
Expand Down
6 changes: 3 additions & 3 deletions application/views/quotas/viewquotas_massive_selector.php
Expand Up @@ -23,7 +23,7 @@
// modal
'actionType' => 'modal',
'modalType' => 'cancel-apply',
'keepopen' => 'no',
'keepopen' => 'yes',
'sModalTitle' => gT('Activate quotas'),
'htmlModalBody' => gT('Are you sure you want to activate all selected quotas?'),
),
Expand All @@ -39,7 +39,7 @@
// modal
'actionType' => 'modal',
'modalType' => 'cancel-apply',
'keepopen' => 'no',
'keepopen' => 'yes',
'sModalTitle' => gT('Deactivate quotas'),
'htmlModalBody' => gT('Are you sure you want to deactivate all selected quotas?'),
),
Expand Down Expand Up @@ -88,7 +88,7 @@
// modal
'actionType' => 'modal',
'modalType' => 'cancel-delete',
'keepopen' => 'no',
'keepopen' => 'yes',
'sModalTitle' => gT('Delete quotas'),
'htmlModalBody' => gT('Are you sure you want to delete all selected quotas?'),
),
Expand Down
9 changes: 8 additions & 1 deletion assets/scripts/admin/assessments.js
Expand Up @@ -94,7 +94,14 @@ var bindAction = function(){
url : url,
method: 'post',
data: params,
success : function(){
success : function(result) {
if (result.success) {
window.LS.ajaxAlerts(result.success, 'success');
} else {
var errorMsg = result.error.message ? result.error.message : result.error;
if (!errorMsg) errorMsg = "Unexpected error";
window.LS.ajaxAlerts(errorMsg, 'danger');
}
$('#assessmentsdeleteform').find('input[name=id]').val(' ');
$('#assesements-delete').modal('hide');
$.fn.yiiGridView.update('assessments-grid');
Expand Down
1 change: 1 addition & 0 deletions assets/scripts/admin/tokens.js
Expand Up @@ -138,6 +138,7 @@ function submitEditToken(){
$modal.hide();
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
window.LS.ajaxAlerts(result.success, 'success');
} else {
var errorMsg = result.error.message ? result.error.message : result.error;
if (!errorMsg) errorMsg = "Unexpected error";
Expand Down

0 comments on commit 3ee487b

Please sign in to comment.