Skip to content

Commit

Permalink
WIP: reduce complexity of ratings class
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Jun 21, 2024
1 parent e7360ba commit ab36877
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 64 deletions.
65 changes: 7 additions & 58 deletions classes/ratings.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) {
$startsolved = $index;
$starthelpful = $index;
$startother = $index;
self::moodleoverflow_quicksort_posts($sortedposts, $startsolvedandhelpful, $index - 1, 'votesdifference');
moodleoverflow_quick_array_sort($sortedposts, $startsolvedandhelpful, $index - 1, 'votesdifference', 'desc');
self::moodleoverflow_check_equal_votes($sortedposts, $startsolvedandhelpful, $index - 1);
}

Expand All @@ -285,7 +285,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) {
if ($index > $startsolved) {
$starthelpful = $index;
$startother = $index;
self::moodleoverflow_quicksort_posts($sortedposts, $startsolved, $index - 1, 'votesdifference');
moodleoverflow_quick_array_sort($sortedposts, $startsolved, $index - 1, 'votesdifference', 'desc');
self::moodleoverflow_check_equal_votes($sortedposts, $startsolved, $index - 1);
}

Expand All @@ -299,7 +299,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) {
// Update the indices and sort the group by votes.
if ($index > $starthelpful) {
$startother = $index;
self::moodleoverflow_quicksort_posts($sortedposts, $starthelpful, $index - 1, 'votesdifference');
moodleoverflow_quick_array_sort($sortedposts, $starthelpful, $index - 1, 'votesdifference', 'desc');
self::moodleoverflow_check_equal_votes($sortedposts, $starthelpful, $index - 1);
}
} else {
Expand All @@ -315,7 +315,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) {
if ($index > $starthelpful) {
$startsolved = $index;
$startother = $index;
self::moodleoverflow_quicksort_posts($sortedposts, $starthelpful, $index - 1, 'votesdifference');
moodleoverflow_quick_array_sort($sortedposts, $starthelpful, $index - 1, 'votesdifference', 'desc');
self::moodleoverflow_check_equal_votes($sortedposts, $starthelpful, $index - 1);
}

Expand All @@ -329,7 +329,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) {
// Update the indices and sort the group by votes.
if ($index > $startsolved) {
$startother = $index;
self::moodleoverflow_quicksort_posts($sortedposts, $startsolved, $index - 1, 'votesdifference');
moodleoverflow_quick_array_sort($sortedposts, $startsolved, $index - 1, 'votesdifference', 'desc');
self::moodleoverflow_check_equal_votes($sortedposts, $startsolved, $index - 1);
}
}
Expand All @@ -343,7 +343,7 @@ public static function moodleoverflow_sort_answers_by_ratings($posts) {
}
// Update the indices and sort the group by votes.
if ($index > $startother) {
self::moodleoverflow_quicksort_posts($sortedposts, $startother, $index - 1, 'votesdifference');
moodleoverflow_quick_array_sort($sortedposts, $startother, $index - 1, 'votesdifference', 'desc');
self::moodleoverflow_check_equal_votes($sortedposts, $startother, $index - 1);
}

Expand Down Expand Up @@ -815,57 +815,6 @@ public static function moodleoverflow_user_can_rate($post, $modulecontext, $user
&& $post->reviewed == 1;
}

/**
* Sorts answerposts of a discussion with quicksort algorithm
* @param array $posts the posts that are being sorted
* @param int $low the index from where the sorting begins
* @param int $high the index until the array is being sorted
* @param string $sortby the attribute by which the posts are being sorted, can be 'votesdifference' or 'modified'
*/
private static function moodleoverflow_quicksort_posts(array &$posts, $low, $high, $sortby): void {
if ($low >= $high) {
return;
}
$left = $low;
$right = $high;
$pivot = 0;
if ($sortby == 'votesdifference') {
$pivot = $posts[intval(($low + $high) / 2)]->votesdifference;
} else if ($sortby == 'modified') {
$pivot = $posts[intval(($low + $high) / 2)]->modified;
}
do {
if ($sortby == 'votesdifference') {
while ($posts[$left]->votesdifference > $pivot) {
$left++;
}
while ($posts[$right]->votesdifference < $pivot) {
$right--;
}
} else if ($sortby == 'modified') {
while ($posts[$left]->modified < $pivot) {
$left++;
}
while ($posts[$right]->modified > $pivot) {
$right--;
}
}
if ($left <= $right) {
$temp = $posts[$right];
$posts[$right] = $posts[$left];
$posts[$left] = $temp;
$right--;
$left++;
}
} while ($left <= $right);
if ($low < $right) {
self::moodleoverflow_quicksort_posts($posts, $low, $right, $sortby);
}
if ($high > $left ) {
self::moodleoverflow_quicksort_posts($posts, $left, $high, $sortby);
}
}

/**
* Helper function for moodleoverflow_sort_answer_by_rating. For posts that have the same mark and votesdifference,
* the posts are sorted by time modified
Expand All @@ -883,7 +832,7 @@ private static function moodleoverflow_check_equal_votes(&$posts, $low, $high) {
($posts[$tempendindex]->votesdifference == $posts[$tempendindex + 1]->votesdifference)) {
$tempendindex++;
}
self::moodleoverflow_quicksort_posts($posts, $tempstartindex, $tempendindex, 'modified');
moodleoverflow_quick_array_sort($posts, $tempstartindex, $tempendindex, 'modified', 'asc');
$low = $tempendindex + 1;
} else {
$low++;
Expand Down
4 changes: 2 additions & 2 deletions classes/tables/userstats_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@ private function sort_table_data($sortorder) {
$length = count($this->userstatsdata);
if ($sortorder['sortorder'] == 4) {
// 4 means sort in ascending order.
moodleoverflow_quick_array_sort(0, $length - 1, $this->userstatsdata, $key, 'asc');
moodleoverflow_quick_array_sort($this->userstatsdata, 0, $length - 1, $key, 'asc');
} else if ($sortorder['sortorder'] == 3) {
// 3 means sort in descending order.
moodleoverflow_quick_array_sort(0, $length - 1, $this->userstatsdata, $key, 'desc');
moodleoverflow_quick_array_sort($this->userstatsdata, 0, $length - 1, $key, 'desc');
}
}
}
9 changes: 5 additions & 4 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2136,14 +2136,15 @@ function moodleoverflow_update_all_grades() {
* Function to sort an array with a quicksort algorithm. This function is a recursive function that needs to
* be called from outside.
*
* @param array $array The array to be sorted. It is passed by reference.
* @param int $low The lowest index of the array. The first call should set it to 0.
* @param int $high The highest index of the array. The first call should set it to the length of the array - 1.
* @param array $array The array to be sorted. It is passed by reference.
*
* @param string $key The key/attribute after what the algorithm sorts. The key should be an comparable integer.
* @param string $order The order of the sorting. It can be 'asc' or 'desc'.
* @return void
*/
function moodleoverflow_quick_array_sort($low, $high, &$array, $key, $order) {
function moodleoverflow_quick_array_sort(&$array, $low, $high, $key, $order) {
if ($low >= $high) {
return;
}
Expand Down Expand Up @@ -2175,9 +2176,9 @@ function moodleoverflow_quick_array_sort($low, $high, &$array, $key, $order) {
}
} while ($left <= $right);
if ($low < $right) {
moodleoverflow_quick_array_sort($low, $right, $array, $key, $order);
moodleoverflow_quick_array_sort($array, $low, $right, $key, $order);
}
if ($high > $left) {
moodleoverflow_quick_array_sort($left, $high, $array, $key, $order);
moodleoverflow_quick_array_sort($array, $left, $high, $key, $order);
}
}

0 comments on commit ab36877

Please sign in to comment.