Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Mar 10, 2021
2 parents e6b3a31 + 1046aee commit 84278f8
Show file tree
Hide file tree
Showing 14 changed files with 1,459 additions and 4,089 deletions.
269 changes: 133 additions & 136 deletions application/controllers/QuestionAdministrationController.php
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 4 additions & 2 deletions application/helpers/export_helper.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 14 additions & 11 deletions application/views/questionAdministration/answerOptionRow.twig
Expand Up @@ -47,17 +47,20 @@
/>
{% endif %}

<input
type='text'
class="code form-control input"
id='answeroptions[{{ answerOption.aid }}][{{ scale_id }}][code]'
name='answeroptions[{{ answerOption.aid }}][{{ scale_id }}][code]'
class='code code-title'
value="{{ answerOption.code }}"
maxlength='5'
required='required'
onfocusout="LS.questionEditor.showAnswerOptionCodeUniqueError(this);"
/>
<input
type='text'
class="code form-control input"
id='answeroptions[{{ answerOption.aid }}][{{ scale_id }}][code]'
name='answeroptions[{{ answerOption.aid }}][{{ scale_id }}][code]'
class='code code-title'
value="{{ answerOption.code }}"
maxlength='5'
required='required'
{% if question.survey.active == 'Y' %}
readonly="readonly"
{% endif %}
onfocusout="LS.questionEditor.showAnswerOptionCodeUniqueError(this);"
/>
</td>
{# If survey is not active, and it's not the first language : no move button, code not editable #}
{% else %}
Expand Down
7 changes: 5 additions & 2 deletions application/views/questionAdministration/answerOptions.twig
Expand Up @@ -85,9 +85,12 @@
{% if first %}
<!-- TODO: Not used??? -->
<input type='hidden' id='answercount_{{ scale_id }}' name='answercount_{{ scale_id }}' value='{{ anscount }}' />
{% set disabled="" %}
{% endif %}
<br/>
{% if activated == 'Y' %}
{% set disabled = 'disabled="disabled"' %}
{% else %}
{% set disabled = '' %}
{% endif %}

<button
{{ disabled }}
Expand Down

0 comments on commit 84278f8

Please sign in to comment.