From d0726d9a034c63a59a80075b4170e3b75378550d Mon Sep 17 00:00:00 2001 From: dkutin Date: Fri, 6 May 2022 10:01:29 -0400 Subject: [PATCH] Performance improvements for large courses --- classes/external.php | 7 +- lib.php | 194 +++++++++++++++++++++++++++---------------- renderer.php | 6 +- report.php | 14 +++- view.php | 8 +- 5 files changed, 143 insertions(+), 86 deletions(-) diff --git a/classes/external.php b/classes/external.php index 4396877..c2dda71 100755 --- a/classes/external.php +++ b/classes/external.php @@ -79,7 +79,8 @@ public static function get_choicegroup_options($choicegroupid, $userid, $allopti require_capability('mod/choicegroup:choose', $context); $groupmode = groups_get_activity_groupmode($cm); - $allresponses = choicegroup_get_response_data($choicegroup, $cm, $groupmode, $choicegroup->onlyactive); + $current_group = $groupmode > 0 ? groups_get_activity_group($cm) : 0; + $options_count = choicegroup_get_options_count($choicegroup, $context, $current_group, $choicegroup->onlyactive); $answers = choicegroup_get_user_answer($choicegroup, $userid, true); foreach ($choicegroup->option as $optionid => $text) { @@ -91,8 +92,8 @@ public static function get_choicegroup_options($choicegroupid, $userid, $allopti $option['maxanswers'] = $choicegroup->maxanswers[$optionid]; $option['displaylayout'] = $choicegroup->display; - if (isset($allresponses[$text])) { - $option['countanswers'] = count($allresponses[$text]); + if (isset($options_count[$text])) { + $option['countanswers'] = $options_count[$text]; } else { $option['countanswers'] = 0; } diff --git a/lib.php b/lib.php index 7b25768..8b1f213 100644 --- a/lib.php +++ b/lib.php @@ -304,10 +304,10 @@ function choicegroup_update_instance($choicegroup) { * @param object $choicegroup * @param object $user * @param object $coursemodule - * @param array $allresponses + * @param int $groupmode * @return array */ -function choicegroup_prepare_options($choicegroup, $user, $coursemodule, $allresponses) { +function choicegroup_prepare_options($choicegroup, $user, $coursemodule, $groupmode) { $cdisplay = array('options'=>array()); @@ -315,9 +315,13 @@ function choicegroup_prepare_options($choicegroup, $user, $coursemodule, $allres $context = context_module::instance($coursemodule->id); $answers = choicegroup_get_user_answer($choicegroup, $user, true, true); + $currentgroup = $groupmode > 0 ? groups_get_activity_group($coursemodule) : 0; + $options_count = choicegroup_get_options_count($choicegroup, $context, $currentgroup, $choicegroup->onlyactive); + if (!isset($choicegroup->option)) { $choicegroup->option = []; } + foreach ($choicegroup->option as $optionid => $text) { if (isset($text)) { //make sure there are no dud entries in the db with blank text values. $option = new stdClass; @@ -327,8 +331,8 @@ function choicegroup_prepare_options($choicegroup, $user, $coursemodule, $allres $option->maxanswers = $choicegroup->maxanswers[$optionid]; $option->displaylayout = $choicegroup->display; - if (isset($allresponses[$text])) { - $option->countanswers = count($allresponses[$text]); + if (isset($options_count[$text])) { + $option->countanswers = $options_count[$text]; } else { $option->countanswers = 0; } @@ -431,28 +435,18 @@ function choicegroup_user_submit_response($formanswer, $choicegroup, $userid, $c /** * @param object $choicegroup - * @param array $allresponses * @param object $cm + * @param int $groupmode * @return void Output is echo'd */ -function choicegroup_show_reportlink($choicegroup, $allresponses, $cm) { - $responsecount = 0; - $respondents = array(); - foreach($allresponses as $optionid => $userlist) { - if ($optionid) { - $responsecount += count($userlist); - if ($choicegroup->multipleenrollmentspossible) { - foreach ($userlist as $user) { - if (!in_array($user->id, $respondents)) { - $respondents[] = $user->id; - } - } - } - } - } - echo '