Skip to content

Commit

Permalink
MDL-74321 mod_forum: forum_check_throttling performance improvements
Browse files Browse the repository at this point in the history
* 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).
  • Loading branch information
junpataleta committed Mar 28, 2022
1 parent 49171c9 commit 2c2686d
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions mod/forum/lib.php
Expand Up @@ -5057,10 +5057,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;
}
Expand All @@ -5069,6 +5065,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;
Expand Down

0 comments on commit 2c2686d

Please sign in to comment.