Skip to content

Commit

Permalink
Fixed issue #18060: 'Save as default values' question setting not wor…
Browse files Browse the repository at this point in the history
…king properly (#2386)
  • Loading branch information
gabrieljenik committed May 25, 2022
1 parent b92b14f commit f9a58ce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(Question $question)
'save_as_default',
null,
gT('All attribute values for this question type will be saved as default'),
$question->same_default == 1 ? 'Y' : 'N',
'N',
[
'classes' => [],
'options' => [
Expand Down
6 changes: 5 additions & 1 deletion application/models/services/QuestionAttributeFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ public function populateValues($attributeDefinitions, $language = null)
$questionAttributeHelper = new QuestionAttributeHelper();

// Get attribute values
$attributeValues = \QuestionAttribute::model()->getAttributesAsArrayFromDB($this->question->qid);
if (!empty($this->question->qid)) {
$attributeValues = \QuestionAttribute::model()->getAttributesAsArrayFromDB($this->question->qid);
} else {
$attributeValues = $questionAttributeHelper->getUserDefaultsForQuestionType($this->question->type);
}

// Fill attributes with values
$languages = is_null($language) ? $survey->allLanguages : [$language];
Expand Down
23 changes: 23 additions & 0 deletions application/models/services/QuestionAttributeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,27 @@ public function sortAttributesByCategory($attributes)
uasort($attributesCopy, [$this, 'categorySort']);
return $attributesCopy;
}

/**
* Returns the user's default values for the specified question type
* @param string $questionType
* @return array<string,mixed>
*/
public function getUserDefaultsForQuestionType($questionType)
{
$defaultValues = [];
$userDefaultQuestionAttributes = \SettingsUser::getUserSettingValue('question_default_values_' . $questionType);
if ($userDefaultQuestionAttributes !== null) {
$defaultValuesByCategory = json_decode($userDefaultQuestionAttributes, true);
foreach ($defaultValuesByCategory as $attributes) {
foreach ($attributes as $attribute => $value) {
if (!is_array($value)) {
$value = ['' => $value];
}
$defaultValues[$attribute] = $value;
}
}
}
return $defaultValues;
}
}

0 comments on commit f9a58ce

Please sign in to comment.