From 21fb2daf42d2856377c2bc89bd208a2dbf63e1b4 Mon Sep 17 00:00:00 2001 From: Olle Haerstedt Date: Mon, 8 Mar 2021 17:42:15 +0100 Subject: [PATCH] Fixed issue #17154: Cannot edit answer options on active surveys --- .../QuestionAdministrationController.php | 92 ++++++++++++++----- .../answerOptionRow.twig | 25 ++--- 2 files changed, 82 insertions(+), 35 deletions(-) diff --git a/application/controllers/QuestionAdministrationController.php b/application/controllers/QuestionAdministrationController.php index ad59c549a0a..3f67fe4c4a0 100644 --- a/application/controllers/QuestionAdministrationController.php +++ b/application/controllers/QuestionAdministrationController.php @@ -452,7 +452,12 @@ public function actionSaveQuestionData() } } else { // TODO: Update subquestions. - // TODO: Update answer options. + if ($question->questionType->answerscales > 0) { + $this->updateAnswerOptions( + $question, + $request->getPost('answeroptions') + ); + } } $transaction->commit(); @@ -2828,38 +2833,77 @@ private function storeAnswerOptions($question, $answerOptionsArray) } } } - /* - foreach ($dataSet as $aAnswerOptions) { - foreach ($aAnswerOptions as $iScaleId => $aAnswerOptionDataSet) { - $aAnswerOptionDataSet['sortorder'] = (int)$aAnswerOptionDataSet['sortorder']; - $oAnswer = Answer::model()->findByPk($aAnswerOptionDataSet['aid']); - if ($oAnswer == null || $isCopyProcess) { - $oAnswer = new Answer(); - $oAnswer->qid = $question->qid; - unset($aAnswerOptionDataSet['aid']); - unset($aAnswerOptionDataSet['qid']); - } + return true; + } - $codeIsEmpty = (!isset($aAnswerOptionDataSet['code'])); - if ($codeIsEmpty) { - throw new CHttpException( - 500, - "Answer option code cannot be empty" + /** + * 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) ); } - $oAnswer->setAttributes($aAnswerOptionDataSet); - $answerSaved = $oAnswer->save(); - if (!$answerSaved) { + $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, - "Answer option couldn't be saved. Error: " - . print_r($oAnswer->getErrors(), true) + gT("Could not save answer option") . PHP_EOL + . print_r($answer->getErrors(), true) ); } - $this->applyAnswerI10N($oAnswer, $question, $aAnswerOptionDataSet); + $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; } diff --git a/application/views/questionAdministration/answerOptionRow.twig b/application/views/questionAdministration/answerOptionRow.twig index 836b7d529dd..620f2938c64 100644 --- a/application/views/questionAdministration/answerOptionRow.twig +++ b/application/views/questionAdministration/answerOptionRow.twig @@ -47,17 +47,20 @@ /> {% endif %} - + {# If survey is not active, and it's not the first language : no move button, code not editable #} {% else %}