Skip to content

Commit

Permalink
Dev #14878: Fixed order for 'Question attribute Category' (#2183)
Browse files Browse the repository at this point in the history
Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
  • Loading branch information
gabrieljenik and encuestabizdevgit committed Dec 29, 2021
1 parent 91f3685 commit f4d4c1b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
10 changes: 0 additions & 10 deletions application/helpers/common_helper.php
Expand Up @@ -2051,16 +2051,6 @@ function getQuestionAttributeValue($questionAttributeArray, $attributeName, $lan
}
}


function categorySort($a, $b)
{
$result = strnatcasecmp($a['category'], $b['category']);
if ($result == 0) {
$result = $a['sortorder'] - $b['sortorder'];
}
return $result;
}

function questionTitleSort($a, $b)
{
$result = strnatcasecmp($a['title'], $b['title']);
Expand Down
5 changes: 3 additions & 2 deletions application/models/services/QuestionAttributeFetcher.php
Expand Up @@ -56,9 +56,9 @@ public function fetch()
}

// Sort by category
uasort($allAttributes, 'categorySort');
$sortedAttributes = $questionAttributeHelper->sortAttributesByCategory($allAttributes);

return $allAttributes;
return $sortedAttributes;
}

/**
Expand Down Expand Up @@ -159,4 +159,5 @@ public function setAdvancedOnly($advancedOnly)
{
$this->setOption('advancedOnly', $advancedOnly);
}

}
58 changes: 58 additions & 0 deletions application/models/services/QuestionAttributeHelper.php
Expand Up @@ -218,4 +218,62 @@ public function getQuestionAttributesWithValues($question, $language = null, $qu

return $questionAttributesWithValues;
}

/**
* Comparison function for sorting question attributes by category with uasort().
*
* @param array<string,mixed> $a First question attribute to compare
* @param array<string,mixed> $b Second question attribute to compare
* @return int
*/
private function categorySort($a, $b)
{
$categoryOrders = $this->getCategoryOrders();
$orderA = isset($categoryOrders[$a['category']]) ? $categoryOrders[$a['category']] : PHP_INT_MAX;
$orderB = isset($categoryOrders[$b['category']]) ? $categoryOrders[$b['category']] : PHP_INT_MAX;
if ($orderA == $orderB) {
$result = strnatcasecmp($a['category'], $b['category']);
if ($result == 0) {
$result = $a['sortorder'] - $b['sortorder'];
}
} else {
$result = $orderA - $orderB;
}
return $result;
}

/**
* Returns the array of categories with their assigned order.
* The array doesn't contain all the posible categories, only those with an order assigned.
*
* @return array<string,int>
*/
public function getCategoryOrders()
{
$orders = [
'Logic' => 1,
'Display' => 2,
'Input' => 3,
'Other' => 4,
'Timer' => 5,
'Statistics' => 6,
];
return $orders;
}

/**
* Sorts an array of question attributes by category.
* Sorting is based on a predefined list of orders (see QuestionAtributeHelper::getCategoryOrders()).
* Categories without a predefined order are considered less relevant.
* Categories with the same order are sorted alphabetically.
*
* @param array<string,array> $attributes
* @return array<string,array>
*/
public function sortAttributesByCategory($attributes)
{
$attributesCopy = $attributes;
uasort($attributesCopy, [$this, 'categorySort']);
return $attributesCopy;
}
}

0 comments on commit f4d4c1b

Please sign in to comment.