Skip to content

Commit

Permalink
Same fix for subquestions
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Mar 9, 2021
1 parent dc5c347 commit 492502d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 117 deletions.
185 changes: 69 additions & 116 deletions application/controllers/QuestionAdministrationController.php
Expand Up @@ -451,7 +451,12 @@ public function actionSaveQuestionData()
);
}
} else {
// TODO: Update subquestions.
if ($question->questionType->subquestions > 0) {
$this->updateSubquestions(
$question,
$request->getPost('subquestions')
);
}
if ($question->questionType->answerscales > 0) {
$this->updateAnswerOptions(
$question,
Expand Down Expand Up @@ -2689,11 +2694,6 @@ private function storeSubquestions($question, $subquestionsArray)
{
$questionOrder = 0;
foreach ($subquestionsArray as $subquestionId => $subquestionArray) {
if ($subquestionId == 0 or strpos($subquestionId, 'new') === 0) {
// New subquestion
} else {
// Updating subquestion
}
foreach ($subquestionArray as $scaleId => $data) {
$subquestion = new Question();
$subquestion->sid = $question->sid;
Expand Down Expand Up @@ -2733,51 +2733,80 @@ private function storeSubquestions($question, $subquestionsArray)
);
}
}
/*
$oSubQuestion = Question::model()->findByPk($aSubquestionDataSet['qid']);
$oSubQuestion = Question::model()->find(
'sid = :sid AND qid = :qid',
[':sid' => $question->sid, ':qid' => (int) $aSubquestionDataSet['qid']]
);
if ($oSubQuestion != null && !$isCopyProcess) {
$oSubQuestion = $this->updateQuestionData($oSubQuestion, $aSubquestionDataSet);
} elseif (!$question->survey->isActive) {
$aSubquestionDataSet['parent_qid'] = $question->qid;
$oSubQuestion = $this->storeNewQuestionData($aSubquestionDataSet, true);
}
$this->applyI10NSubquestion($oSubQuestion, $aSubquestionDataSet);
*/
}
}
}

/**
* @todo document me
* @todo delete if not used
* Save subquestion.
* Used when survey *is* activated.
*
* @param Question $oQuestion
* @param array $dataSet
* @return boolean
* @param Question $question
* @param array $subquestionsArray Data from request.
* @return void
* @throws CHttpException
*/
private function applyI10NSubquestion($oQuestion, $dataSet)
private function updateSubquestions($question, $subquestionsArray)
{
foreach ($oQuestion->survey->allLanguages as $sLanguage) {
$aI10NBlock = $dataSet[$sLanguage];
$i10N = QuestionL10n::model()->findByAttributes(['qid' => $oQuestion->qid, 'language' => $sLanguage]);
$i10N->setAttributes(
[
'question' => $aI10NBlock['question'],
'help' => $aI10NBlock['help'],
],
false
);
if (!$i10N->save()) {
throw new CHttpException(500, gT("Could not store translation for subquestion"));
$questionOrder = 0;
foreach ($subquestionsArray as $subquestionId => $subquestionArray) {
foreach ($subquestionArray as $scaleId => $data) {
$subquestion = Question::model()->findByAttributes(
[
'parent_qid' => $question->qid,
'title' => $data['code']
]
);
if (empty($subquestion)) {
throw new Exception('Found no subquestion with code ' . $data['code']);
}
$subquestion->sid = $question->sid;
$subquestion->gid = $question->gid;
$subquestion->parent_qid = $question->qid;
$subquestion->question_order = $questionOrder;
$questionOrder++;
if (!isset($data['code'])) {
throw new CHttpException(
500,
'Internal error: Missing mandatory field code for question: ' . json_encode($data)
);
}
$subquestion->title = $data['code'];
if ($scaleId === 0) {
$subquestion->relevance = $data['relevance'];
}
$subquestion->scale_id = $scaleId;
if (!$subquestion->update()) {
throw new CHttpException(
500,
gT("Could not save subquestion") . PHP_EOL
. print_r($subquestion->getErrors(), true)
);
}
$subquestion->refresh();
foreach ($data['subquestionl10n'] as $lang => $questionText) {
$l10n = QuestionL10n::model()->findByAttributes(
[
'qid' => $subquestion->qid,
'language' => $lang
]
);
if (empty($l10n)) {
$l10n = new QuestionL10n();
}
$l10n->qid = $subquestion->qid;
$l10n->language = $lang;
$l10n->question = $questionText;
if (!$l10n->save()) {
throw new CHttpException(
500,
gT("Could not save subquestion") . PHP_EOL
. print_r($l10n->getErrors(), true)
);
}
}
}
}

return true;
}

/**
Expand Down Expand Up @@ -2907,82 +2936,6 @@ private function updateAnswerOptions(Question $question, array $answerOptionsArr
return true;
}

/**
* @todo document me
* @todo delete if not used
*
* @param Question $oQuestion
* @param array $dataSet
* @return void
*/
private function cleanAnsweroptions(&$oQuestion, &$dataSet)
{
$aAnsweroptions = $oQuestion->answers;
array_walk(
$aAnsweroptions,
function ($oAnsweroption) use (&$dataSet) {
$exists = false;
foreach ($dataSet as $scaleId => $aAnsweroptions) {
foreach ($aAnsweroptions as $i => $aAnsweroptionDataSet) {
if (
((is_numeric($aAnsweroptionDataSet['aid'])
&& $oAnsweroption->aid == $aAnsweroptionDataSet['aid'])
|| $oAnsweroption->code == $aAnsweroptionDataSet['code'])
&& ($oAnsweroption->scale_id == $scaleId)
) {
$exists = true;
$dataSet[$scaleId][$i]['aid'] = $oAnsweroption->aid;
}

if (!$exists) {
$oAnsweroption->delete();
}
}
}
}
);
}

/**
* @todo document me
* @todo delete if not used
*
* @param Answer $oAnswer
* @param Question $oQuestion
* @param array $dataSet
*
* @return boolean
* @throws CHttpException
*/
private function applyAnswerI10N($oAnswer, $oQuestion, $dataSet)
{
foreach ($oQuestion->survey->allLanguages as $sLanguage) {
$i10N = AnswerL10n::model()->findByAttributes(['aid' => $oAnswer->aid, 'language' => $sLanguage]);
if ($i10N == null) {
$i10N = new AnswerL10n();
$i10N->setAttributes(
[
'aid' => $oAnswer->aid,
'language' => $sLanguage,
],
false
);
}
$i10N->setAttributes(
[
'answer' => $dataSet[$sLanguage]['answer'],
],
false
);

if (!$i10N->save()) {
throw new CHttpException(500, gT("Could not store translation for answer option"));
}
}

return true;
}

/**
* @param array $aQuestionTypeList Question Type List as Array
* @return array
Expand Down
18 changes: 17 additions & 1 deletion application/views/questionAdministration/subquestionRow.twig
Expand Up @@ -25,7 +25,20 @@

<!-- Code (title) -->
<td class="code-title" style="vertical-align: middle;">
{{ subquestion.title|escape('html') }}
<input
type='text'
class="code form-control input"
id='subquestions[{{ subquestion.qid }}][{{ scale_id }}][code]'
name='subquestions[{{ subquestion.qid }}][{{ scale_id }}][code]'
class='code code-title'
value="{{ subquestion.title|escape('html_attr') }}"
required='required'
maxlength="20"
{% if question.survey.active == 'Y' %}
readonly="readonly"
{% endif %}
onfocusout="LS.questionEditor.showSubquestionCodeUniqueError(this);"
/>
</td>

{# If survey is not activated and first language : move button, code editable #}
Expand Down Expand Up @@ -67,6 +80,9 @@
value="{{ subquestion.title|escape('html_attr') }}"
required='required'
maxlength="20"
{% if question.survey.active == 'Y' %}
readonly="readonly"
{% endif %}
onfocusout="LS.questionEditor.showSubquestionCodeUniqueError(this);"
/>
</td>
Expand Down

0 comments on commit 492502d

Please sign in to comment.