diff --git a/application/controllers/QuestionAdministrationController.php b/application/controllers/QuestionAdministrationController.php index ad59c549a0a..9a4481aceb4 100644 --- a/application/controllers/QuestionAdministrationController.php +++ b/application/controllers/QuestionAdministrationController.php @@ -451,8 +451,18 @@ public function actionSaveQuestionData() ); } } else { - // TODO: Update subquestions. - // TODO: Update answer options. + if ($question->questionType->subquestions > 0) { + $this->updateSubquestions( + $question, + $request->getPost('subquestions') + ); + } + if ($question->questionType->answerscales > 0) { + $this->updateAnswerOptions( + $question, + $request->getPost('answeroptions') + ); + } } $transaction->commit(); @@ -2684,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; @@ -2728,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; } /** @@ -2828,114 +2862,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']); - } - - $codeIsEmpty = (!isset($aAnswerOptionDataSet['code'])); - if ($codeIsEmpty) { - throw new CHttpException( - 500, - "Answer option code cannot be empty" - ); - } - $oAnswer->setAttributes($aAnswerOptionDataSet); - $answerSaved = $oAnswer->save(); - if (!$answerSaved) { - throw new CHttpException( - 500, - "Answer option couldn't be saved. Error: " - . print_r($oAnswer->getErrors(), true) - ); - } - $this->applyAnswerI10N($oAnswer, $question, $aAnswerOptionDataSet); - } - } - */ return true; } /** - * @todo document me - * @todo delete if not used + * Like storeAnswerOptions, but adapted for when survey is active (not allowed to change codes). * - * @param Question $oQuestion - * @param array $dataSet + * @param Question $question + * @param array $answerOptionsArray * @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) + private function updateAnswerOptions(Question $question, array $answerOptionsArray) { - foreach ($oQuestion->survey->allLanguages as $sLanguage) { - $i10N = AnswerL10n::model()->findByAttributes(['aid' => $oAnswer->aid, 'language' => $sLanguage]); - if ($i10N == null) { - $i10N = new AnswerL10n(); - $i10N->setAttributes( + $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( [ - 'aid' => $oAnswer->aid, - 'language' => $sLanguage, - ], - false + 'qid' => $question->qid, + 'code' => $data['code'] + ] ); - } - $i10N->setAttributes( - [ - 'answer' => $dataSet[$sLanguage]['answer'], - ], - false - ); - - if (!$i10N->save()) { - throw new CHttpException(500, gT("Could not store translation for answer option")); + 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; } diff --git a/application/helpers/export_helper.php b/application/helpers/export_helper.php index 4c08d5c0e67..e5f2834ebed 100644 --- a/application/helpers/export_helper.php +++ b/application/helpers/export_helper.php @@ -110,6 +110,8 @@ function SPSSExportData($iSurveyID, $iLength, $na = '', $sEmptyAnswerValue = '', { // Build array that has to be returned $fields = SPSSFieldMap($iSurveyID, 'V', $sLanguage); + $survey = Survey::model()->findByPk($iSurveyID); + // Now see if we have parameters for from (offset) & num (limit) $limit = App()->getRequest()->getParam('limit'); $offset = App()->getRequest()->getParam('offset'); @@ -166,8 +168,8 @@ function SPSSExportData($iSurveyID, $iLength, $na = '', $sEmptyAnswerValue = '', // convert mysql datestamp (yyyy-mm-dd hh:mm:ss) to SPSS datetime (dd-mmm-yyyy hh:mm:ss) format if (isset($row[$fieldno])) { list($year, $month, $day, $hour, $minute, $second) = preg_split('([^0-9])', $row[$fieldno]); - if ($year != '' && (int) $year >= 1900) { - echo quoteSPSS(date('d-m-Y H:i:s', mktime($hour, $minute, $second, $month, $day, $year)), $q, field); + if ($year != '' && (int) $year >= 1900) { + echo quoteSPSS(date('d-m-Y H:i:s', mktime($hour, $minute, $second, $month, $day, $year)), $q, $field); } elseif ($row[$fieldno] === '') { echo quoteSPSS($sEmptyAnswerValue, $q, $field); } else { 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 %} diff --git a/application/views/questionAdministration/answerOptions.twig b/application/views/questionAdministration/answerOptions.twig index a5a9121f6a0..36087dd9dc1 100644 --- a/application/views/questionAdministration/answerOptions.twig +++ b/application/views/questionAdministration/answerOptions.twig @@ -85,9 +85,12 @@ {% if first %} - {% set disabled="" %} {% endif %} -
+ {% if activated == 'Y' %} + {% set disabled = 'disabled="disabled"' %} + {% else %} + {% set disabled = '' %} + {% endif %}