From 24ca210c2fc845c3aae040b5cda9b015d50afbe4 Mon Sep 17 00:00:00 2001 From: Noemie Ariste Date: Mon, 16 Sep 2024 11:30:10 +1200 Subject: [PATCH] Improve performance of get_all_groups_of_choices --- locallib.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/locallib.php b/locallib.php index c1754b47..fa7f395b 100644 --- a/locallib.php +++ b/locallib.php @@ -765,14 +765,17 @@ private function process_action_manual_allocation() { * @return array of group ids used in rateable choices */ public function get_all_groups_of_choices(): array { - $rateablechoiceswithgrouprestrictions = array_filter($this->get_rateable_choices(), - fn($choice) => !empty($choice->usegroups) && !empty($this->get_choice_groups($choice->id))); - $rateablechoiceids = array_map(fn($choice) => $choice->id, $rateablechoiceswithgrouprestrictions); - $groupids = []; - foreach ($rateablechoiceids as $choiceid) { - $groupids = array_merge($groupids, array_map(fn($group) => $group->id, $this->get_choice_groups($choiceid))); - } - return array_unique($groupids); + global $DB; + + $sql = 'SELECT DISTINCT g.id + FROM {ratingallocate_group_choices} gc + JOIN {ratingallocate_choices} rc on rc.id = gc.choiceid + JOIN {groups} g ON gc.groupid=g.id + WHERE rc.ratingallocateid=:id'; + + $groupids = $DB->get_fieldset_sql($sql, ['id' => $this->ratingallocateid]); + + return $groupids; } /**