diff --git a/mod/forum/lib.php b/mod/forum/lib.php index ec24c8a800d..c3d54972101 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -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;