Skip to content

Commit

Permalink
Fixed issue #18976 - default answers should persist with subquestions (
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-foster-uk committed Aug 15, 2023
1 parent f022f3b commit 4420f62
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
26 changes: 21 additions & 5 deletions application/controllers/QuestionAdministrationController.php
Expand Up @@ -533,8 +533,6 @@ public function actionSaveQuestionData()
}

if ($question->survey->active == 'N') {
// Clean subquestions before save.
$question->deleteAllSubquestions();
// If question type has subquestions, save them.
if ($question->questionType->subquestions > 0) {
$this->storeSubquestions(
Expand Down Expand Up @@ -2857,9 +2855,21 @@ private function storeSubquestions($question, $subquestionsArray)
{
$questionOrder = 0;
$errorQuestions = [];
foreach ($subquestionsArray as $subquestionId => $subquestionArray) {
$subquestionIds = [];
foreach ($subquestionsArray as $subquestionArray) {
foreach ($subquestionArray as $scaleId => $data) {
$subquestion = new Question();
$subquestion = null;
if (isset($data['oldcode'])) {
$subquestion = Question::model()->findByAttributes([
'sid' => $question->sid,
'parent_qid' => $question->qid,
'title' => $data['oldcode']
]);
}
if (!$subquestion) {
$subquestion = new Question();
}

$subquestion->sid = $question->sid;
$subquestion->gid = $question->gid;
$subquestion->parent_qid = $question->qid;
Expand All @@ -2881,8 +2891,13 @@ private function storeSubquestions($question, $subquestionsArray)
continue;
}
$subquestion->refresh();
$subquestionIds[] = $subquestion->qid;
foreach ($data['subquestionl10n'] as $lang => $questionText) {
$l10n = new QuestionL10n();
$l10n = QuestionL10n::model()->findByAttributes([
'qid' => $subquestion->qid,
'language' => $lang
]);
$l10n = $l10n ?? new QuestionL10n();
$l10n->qid = $subquestion->qid;
$l10n->language = $lang;
$l10n->question = $questionText;
Expand All @@ -2893,6 +2908,7 @@ private function storeSubquestions($question, $subquestionsArray)
}
}
}
$question->deleteAllSubquestions($subquestionIds);
foreach ($errorQuestions as $errorQuestion) {
throw (new LSUserException(500, gT("Could not save subquestion")))
->setDetailedErrorsFromModel($errorQuestion);
Expand Down
7 changes: 5 additions & 2 deletions application/models/Question.php
Expand Up @@ -485,12 +485,15 @@ public function removeFromLastVisited()
/**
* Delete all subquestions that belong to this question.
*
* @param ?array $exceptIds Don't delete subquestions with these ids.
* @return void
* @todo Duplication from delete()
*/
public function deleteAllSubquestions()
public function deleteAllSubquestions($exceptIds = [])
{
$ids = $this->allSubQuestionIds;
$ids = !empty($exceptIds)

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Aug 21, 2023

Collaborator

Why not addNotInCondition ?

Then no need extra code, just new CDbCriteria())->addNotInCondition('qid', $exceptIds)

? array_diff($this->allSubQuestionIds, $exceptIds)
: $this->allSubQuestionIds;
$qidsCriteria = (new CDbCriteria())->addInCondition('qid', $ids);
$res = Question::model()->deleteAll((new CDbCriteria())->addInCondition('qid', $ids));

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Aug 21, 2023

Collaborator

Why not use $qidsCriteria here ?

QuestionAttribute::model()->deleteAll($qidsCriteria);
Expand Down

0 comments on commit 4420f62

Please sign in to comment.