From d89d9840483f8f68a8add2e21fac0e71faf1db1c Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Thu, 15 Apr 2021 08:08:36 +1000 Subject: [PATCH] MDL-71410 mod_quiz: Cache quiz times in modinfo for performance We update the dates with user/group overrides. The calculation of the override in the quiz module is different from the assignment module as the quiz_overrides table des not have a sortorder column. See quiz_update_effective_access(). --- mod/quiz/lib.php | 67 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 7d5d3afa836..36d31128fa5 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -2186,7 +2186,8 @@ function quiz_get_coursemodule_info($coursemodule) { global $DB; $dbparams = ['id' => $coursemodule->instance]; - $fields = 'id, name, intro, introformat, completionattemptsexhausted, completionpass, completionminattempts'; + $fields = 'id, name, intro, introformat, completionattemptsexhausted, completionpass, completionminattempts, + timeopen, timeclose'; if (!$quiz = $DB->get_record('quiz', $dbparams, $fields)) { return false; } @@ -2213,9 +2214,73 @@ function quiz_get_coursemodule_info($coursemodule) { $result->customdata['customcompletionrules']['completionminattempts'] = $quiz->completionminattempts; } + // Populate some other values that can be used in calendar or on dashboard. + if ($quiz->timeopen) { + $result->customdata['timeopen'] = $quiz->timeopen; + } + if ($quiz->timeclose) { + $result->customdata['timeclose'] = $quiz->timeclose; + } + return $result; } +/** + * Sets dynamic information about a course module + * + * This function is called from cm_info when displaying the module + * + * @param cm_info $cm + */ +function mod_quiz_cm_info_dynamic(cm_info $cm) { + global $USER; + + $cache = cache::make('mod_quiz', 'overrides'); + $override = $cache->get("{$cm->instance}_u_{$USER->id}"); + + if (!$override) { + $override = (object) [ + 'timeopen' => null, + 'timeclose' => null, + ]; + } + + // No need to look for group overrides if there are user overrides for both timeopen and timeclose. + if (is_null($override->timeopen) || is_null($override->timeclose)) { + $opens = []; + $closes = []; + $groupings = groups_get_user_groups($cm->course, $USER->id); + foreach ($groupings[0] as $groupid) { + $groupoverride = $cache->get("{$cm->instance}_g_{$groupid}"); + if (isset($groupoverride->timeopen)) { + $opens[] = $groupoverride->timeopen; + } + if (isset($groupoverride->timeclose)) { + $closes[] = $groupoverride->timeclose; + } + } + // If there is a user override for a setting, ignore the group override. + if (is_null($override->timeopen) && count($opens)) { + $override->timeopen = min($opens); + } + if (is_null($override->timeclose) && count($closes)) { + if (in_array(0, $closes)) { + $override->timeclose = 0; + } else { + $override->timeclose = max($closes); + } + } + } + + // Populate some other values that can be used in calendar or on dashboard. + if (!is_null($override->timeopen)) { + $cm->override_customdata('timeopen', $override->timeopen); + } + if (!is_null($override->timeclose)) { + $cm->override_customdata('timeclose', $override->timeclose); + } +} + /** * Callback which returns human-readable strings describing the active completion custom rules for the module instance. *