Skip to content

Commit

Permalink
Fixed issue #19203: alphabetical order didn't respect survey language (
Browse files Browse the repository at this point in the history
  • Loading branch information
Shnoulle committed Nov 3, 2023
1 parent 2227098 commit 000db27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
21 changes: 15 additions & 6 deletions application/models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,13 @@ public function getbuttons()
return $buttons;
}

public function getOrderedAnswers($scale_id = null)
/**
* get the ordered answers
* @param null|integer scale
* @param null|string $language
* @return array
*/
public function getOrderedAnswers($scale_id = null, $language = null)
{
//reset answers set prior to this call
$aAnswerOptions = [
Expand All @@ -745,17 +751,18 @@ public function getOrderedAnswers($scale_id = null)
return $aAnswerOptions[$scale_id];
}

$aAnswerOptions = $this->sortAnswerOptions($aAnswerOptions);
$aAnswerOptions = $this->sortAnswerOptions($aAnswerOptions, $language);
return $aAnswerOptions;
}

/**
* Returns the specified answer options sorted according to the question attributes.
* Refactored from getOrderedAnswers();
* @param array<int,Answer[]> The answer options to sort
* @param null|string $language
* @return array<int,Answer[]>
*/
private function sortAnswerOptions($answerOptions)
private function sortAnswerOptions($answerOptions, $language = null)
{
// Sort randomly if applicable
if ($this->shouldOrderAnswersRandomly()) {
Expand All @@ -774,15 +781,17 @@ private function sortAnswerOptions($answerOptions)

// Sort alphabetically if applicable
if ($this->shouldOrderAnswersAlphabetically()) {
if (empty($language) || !in_array($language, $this->survey->allLanguages)) {
$language = $this->survey->language;
}
foreach ($answerOptions as $scaleId => $scaleArray) {
$sorted = array();

// We create an aray sorted that will use the answer in the current language as key, and that will store its old index as value
// We create an array sorted that will use the answer in the current language as key, and that will store its old index as value
foreach ($scaleArray as $key => $answer) {
$sorted[$answer->answerl10ns[$this->survey->language]->answer] = $key;
$sorted[$answer->answerl10ns[$language]->answer] = $key;
}
ksort($sorted);

// Now, we create a new array that store the old values of $answerOptions in the order of $sorted
$sortedScaleAnswers = array();
foreach ($sorted as $answer => $key) {
Expand Down
2 changes: 1 addition & 1 deletion application/models/QuestionBaseRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ protected function setSubquestions($scale_id = null)

protected function setAnsweroptions($scale_id = null)
{
$this->aAnswerOptions = $this->oQuestion->getOrderedAnswers($scale_id);
$this->aAnswerOptions = $this->oQuestion->getOrderedAnswers($scale_id, $this->sLanguage);
}

protected function getAnswerCount($iScaleId = 0)
Expand Down
6 changes: 2 additions & 4 deletions application/models/QuestionCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ public static function getInstance($iSurveyId, $type = null, $themeName = null)
}

/**
* @param int|null $scale_id
* @return array|null
* @todo Why return both empty array and null?
* @inheritdoc
*/
public function getOrderedAnswers($scale_id = null)
public function getOrderedAnswers($scale_id = null, $language = null)
{
if ($scale_id == null) {
return [];
Expand Down

0 comments on commit 000db27

Please sign in to comment.