MDL-74321 mod_forum: forum_check_throttling performance improvements

* Return early if the forum's blockafter or blockperiod attributes
are empty.
* If $cm is not passed in forum_check_throttling(), try to fetch it
using get_fast_modinfo() which avoids DB reads. Fetch it via
get_coursemodule_from_instance() as a last resort (though it's unlikely
to happen).
This commit is contained in:
Jun Pataleta
2022-03-28 10:03:57 +08:00
parent efda17d3b0
commit 996534fbea
+16 -4
View File
@@ -5045,10 +5045,6 @@ function forum_check_throttling($forum, $cm = null) {
return false; // This is broken.
}
if (!$cm) {
$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course, false, MUST_EXIST);
}
if (empty($forum->blockafter)) {
return false;
}
@@ -5057,6 +5053,22 @@ function forum_check_throttling($forum, $cm = null) {
return false;
}
if (!$cm) {
// Try to fetch the $cm object via get_fast_modinfo() so we don't incur DB reads.
$modinfo = get_fast_modinfo($forum->course);
$forumcms = $modinfo->get_instances_of('forum');
foreach ($forumcms as $tmpcm) {
if ($tmpcm->instance == $forum->id) {
$cm = $tmpcm;
break;
}
}
// Last resort. Try to fetch via get_coursemodule_from_instance().
if (!$cm) {
$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course, false, MUST_EXIST);
}
}
$modcontext = context_module::instance($cm->id);
if (has_capability('mod/forum:postwithoutthrottling', $modcontext)) {
return false;