Skip to content

Commit

Permalink
Fixed issue #17569: Unable to edit answer in single choice question a…
Browse files Browse the repository at this point in the history
…fter activate survey (#2054)

Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
  • Loading branch information
gabrieljenik and encuestabizdevgit committed Sep 29, 2021
1 parent 3ef77d3 commit 9e5c6bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 94 deletions.
98 changes: 12 additions & 86 deletions application/controllers/QuestionAdministrationController.php
Expand Up @@ -445,10 +445,19 @@ public function actionSaveQuestionData()
SettingsUser::deleteUserSetting('question_default_values_' . $questionData['question']['type']);
}

// Clean subquestions and answer options before save.
// Clean answer options before save.
// NB: Still inside a database transaction.
$question->deleteAllAnswers();
// If question type has answeroptions, save them.
if ($question->questionType->answerscales > 0) {
$this->storeAnswerOptions(
$question,
$request->getPost('answeroptions')
);
}

if ($question->survey->active == 'N') {
$question->deleteAllAnswers();
// Clean subquestions before save.
$question->deleteAllSubquestions();
// If question type has subquestions, save them.
if ($question->questionType->subquestions > 0) {
Expand All @@ -457,26 +466,13 @@ public function actionSaveQuestionData()
$request->getPost('subquestions')
);
}
// If question type has answeroptions, save them.
if ($question->questionType->answerscales > 0) {
$this->storeAnswerOptions(
$question,
$request->getPost('answeroptions')
);
}
} else {
if ($question->questionType->subquestions > 0) {
$this->updateSubquestions(
$question,
$request->getPost('subquestions')
);
}
if ($question->questionType->answerscales > 0) {
$this->updateAnswerOptions(
$question,
$request->getPost('answeroptions')
);
}
}
$transaction->commit();

Expand Down Expand Up @@ -863,6 +859,7 @@ private function getAnswerOptionRow($surveyid, $gid, $qid, $codes, $language, $f
'activated' => $activated,
'first' => $first,
'surveyid' => $surveyid,
'sid' => $surveyid,
'gid' => $gid,
'qid' => $qid,
'language' => $language,
Expand Down Expand Up @@ -2960,77 +2957,6 @@ private function storeAnswerOptions($question, $answerOptionsArray)
return true;
}

/**
* Like storeAnswerOptions, but adapted for when survey is active (not allowed to change codes).
*
* @param Question $question
* @param array $answerOptionsArray
* @return void
* @throws CHttpException
*/
private function updateAnswerOptions(Question $question, array $answerOptionsArray)
{
$i = 0;
foreach ($answerOptionsArray as $answerOptionId => $answerOptionArray) {
foreach ($answerOptionArray as $scaleId => $data) {
if (!isset($data['code'])) {
throw new Exception(
'code is not set in data: ' . json_encode($data)
);
}
$answer = Answer::model()->findByAttributes(
[
'qid' => $question->qid,
'code' => $data['code']
]
);
if (empty($answer)) {
throw new Exception(
'Found no answer option with code ' . $data['code']
);
}
$answer->sortorder = $i;
$i++;
if (isset($data['assessment'])) {
$answer->assessment_value = $data['assessment'];
} else {
$answer->assessment_value = 0;
}
$answer->scale_id = $scaleId;
if (!$answer->update()) {
throw new CHttpException(
500,
gT("Could not save answer option") . PHP_EOL
. print_r($answer->getErrors(), true)
);
}
$answer->refresh();
foreach ($data['answeroptionl10n'] as $lang => $answerOptionText) {
$l10n = AnswerL10n::model()->findByAttributes(
[
'aid' => $answer->aid,
'language' => $lang
]
);
if (empty($l10n)) {
$l10n = new AnswerL10n();
}
$l10n->aid = $answer->aid;
$l10n->language = $lang;
$l10n->answer = $answerOptionText;
if (!$l10n->save()) {
throw new CHttpException(
500,
gT("Could not save answer option") . PHP_EOL
. print_r($l10n->getErrors(), true)
);
}
}
}
}
return true;
}

/**
* @param QuestionTheme[] $questionThemes Question theme List
* @return array
Expand Down
Expand Up @@ -56,9 +56,6 @@
value="{{ answerOption.code }}"
maxlength='5'
required='required'
{% if question.survey.active == 'Y' %}
readonly="readonly"
{% endif %}
onfocusout="LS.questionEditor.showAnswerOptionCodeUniqueError(this);"
/>
</td>
Expand Down Expand Up @@ -134,7 +131,7 @@

<!-- Icons edit/delete -->
<td style="vertical-align: middle; white-space: nowrap;" class="answeroption-actions">
{% if activated != 'Y' and first %}
{% if first %}
<button type="button" class="btn btn-default btn-sm btnaddanswer">
<i
class="icon-add text-success"
Expand Down
4 changes: 0 additions & 4 deletions application/views/questionAdministration/answerOptions.twig
Expand Up @@ -81,10 +81,6 @@
{% if first %}
<!-- TODO: Not used??? -->
<input type='hidden' id='answercount_{{ scale_id }}' name='answercount_{{ scale_id }}' value='{{ anscount }}' />
{% endif %}
{% if activated == 'Y' %}
{% set disabled = 'disabled="disabled"' %}
{% else %}
{% set disabled = '' %}
{% endif %}

Expand Down

0 comments on commit 9e5c6bc

Please sign in to comment.