Skip to content

Commit

Permalink
Fixed issue: Survey general settings error message detail (#3809)
Browse files Browse the repository at this point in the history
* Dev: set model to persist exception to allow capturing of active record errors

* Dev: remove debug code
  • Loading branch information
kevin-foster-uk committed Apr 9, 2024
1 parent af609f0 commit 2d2b759
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
39 changes: 23 additions & 16 deletions application/controllers/admin/Database.php
Expand Up @@ -16,13 +16,13 @@
use LimeSurvey\Models\Services\Exception\PersistErrorException;

/**
* Database
*
* @package LimeSurvey
* @author
* @copyright 2011
* @access public
*/
* Database
*
* @package LimeSurvey
* @author
* @copyright 2011
* @access public
*/
class Database extends SurveyCommonAction
{
/**
Expand Down Expand Up @@ -97,18 +97,18 @@ public function updateDefaultValues($qid, $sqid, $scale_id, $specialtype, $langu
->find(
'specialtype = :specialtype AND qid = :qid AND sqid = :sqid AND scale_id = :scale_id',
array(
':specialtype' => $specialtype,
':qid' => $qid,
':sqid' => $sqid,
':scale_id' => $scale_id,
':specialtype' => $specialtype,
':qid' => $qid,
':sqid' => $sqid,
':scale_id' => $scale_id,
)
);
$dvid = !empty($arDefaultValue->dvid) ? $arDefaultValue->dvid : null;

if ($defaultvalue == '') {
// Remove the default value if it is empty
if ($dvid !== null) {
DefaultValueL10n::model()->deleteAllByAttributes(array('dvid' => $dvid, 'language' => $language ));
DefaultValueL10n::model()->deleteAllByAttributes(array('dvid' => $dvid, 'language' => $language));
$iRowCount = DefaultValueL10n::model()->countByAttributes(array('dvid' => $dvid));
if ($iRowCount == 0) {
DefaultValue::model()->deleteByPk($dvid);
Expand Down Expand Up @@ -199,7 +199,7 @@ private function actionUpdateDefaultValues($iSurveyID)
}

if (Yii::app()->request->getPost('defaultanswerscale_0_' . $sLanguage) == 'EM') {
// Case EM, write expression to database
// Case EM, write expression to database
$this->updateDefaultValues($this->iQuestionID, 0, 0, '', $sLanguage, Yii::app()->request->getPost('defaultanswerscale_0_' . $sLanguage . '_EM'));
} else {
// Case "other", write list value to database
Expand Down Expand Up @@ -379,9 +379,16 @@ protected function actionUpdateSurveyLocaleSettingsGeneralSettings($surveyId)
Yii::app()
->setFlashMessage(gT('Survey settings were successfully saved.'));
} catch (PersistErrorException $e) {
Yii::app()->setFlashMessage(
$e->getMessage(),
'error'
\Yii::app()->setFlashMessage(
\CHtml::errorSummary(
$e->getErrorModel(),
\CHtml::tag(
"p",
array('class' => 'strong'),
gT("Survey could not be updated, please fix the following error:")
)
),
"error"
);
}

Expand Down
13 changes: 13 additions & 0 deletions application/models/services/Exception.php
Expand Up @@ -2,6 +2,19 @@

namespace LimeSurvey\Models\Services;

use LSActiveRecord;

class Exception extends \Exception
{
private $errorModel = null;

public function setErrorModel(LSActiveRecord $errorModel)
{
$this->errorModel = $errorModel;
}

public function getErrorModel()
{
return $this->errorModel;
}
}
5 changes: 5 additions & 0 deletions application/models/services/SurveyAggregateService.php
Expand Up @@ -60,6 +60,11 @@ public function setRestMode($restMode)
$this->generalSettings->setRestMode($this->restMode);
}

public function getRestMode()
{
return $this->restMode;
}

/**
* Update
*
Expand Down
Expand Up @@ -177,12 +177,14 @@ private function updateGeneralSettings(Survey $survey, array $input)
);

if (!$survey->save()) {
throw new PersistErrorException(
$e = new PersistErrorException(
sprintf(
'Failed saving general settings for survey #%s',
$survey->sid
)
);
$e->setErrorModel($survey);
throw $e;
}
}

Expand Down
Expand Up @@ -142,13 +142,15 @@ protected function updateLanguageSettings(Survey $survey, $input)

$surveyLanguageSetting->setAttributes($data);
if (!$surveyLanguageSetting->save()) {
throw new PersistErrorException(
$e = new PersistErrorException(
sprintf(
'Failed saving language settings for survey #%s and language "%s"',
$survey->sid,
$languageCode
)
);
$e->setErrorModel($surveyLanguageSetting);
throw $e;
}
}
}
Expand Down

0 comments on commit 2d2b759

Please sign in to comment.