Skip to content

Commit 60ea456

Browse files
Noémie Aristedlmsr
andcommitted
Improve performance of get_all_groups_of_choices
Co-authored-by: Daniel Meißner <[email protected]>
1 parent 1961fab commit 60ea456

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

classes/ratingallocate.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,21 @@ private function process_action_manual_allocation() {
685685
* @return array of group ids used in rateable choices
686686
*/
687687
public function get_all_groups_of_choices(): array {
688-
$rateablechoiceswithgrouprestrictions = array_filter($this->get_rateable_choices(),
689-
fn($choice) => !empty($choice->usegroups) && !empty($this->get_choice_groups($choice->id)));
690-
$rateablechoiceids = array_map(fn($choice) => $choice->id, $rateablechoiceswithgrouprestrictions);
691-
$groupids = [];
692-
foreach ($rateablechoiceids as $choiceid) {
693-
$groupids = array_merge($groupids, array_map(fn($group) => $group->id, $this->get_choice_groups($choiceid)));
694-
}
695-
return array_unique($groupids);
688+
$sql = '
689+
SELECT DISTINCT
690+
g.id
691+
FROM
692+
{ratingallocate_choices} c
693+
JOIN {ratingallocate_group_choices} gc ON gc.choiceid = c.id
694+
JOIN {groups} g ON gc.groupid = g.id
695+
WHERE
696+
c.ratingallocateid = :id
697+
AND c.usegroups = 1
698+
AND c.active = 1';
699+
700+
$groupids = $this->db->get_fieldset_sql($sql, ['id' => $this->ratingallocateid]);
701+
702+
return $groupids;
696703
}
697704

698705
/**

0 commit comments

Comments
 (0)