Skip to content

Commit

Permalink
MDL-74321 mod_forum: Forum_check_throttling improvements and fixes
Browse files Browse the repository at this point in the history
* Fetch only the fields required by the function.
* Quick validation of the passed forum parameter. If the forum parameter
is an object, it should have the forum ID and the course ID.
* Default return when there's no need to show a warning yet.
  • Loading branch information
junpataleta committed Mar 28, 2022
1 parent 9d827a2 commit 8a9fea3
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions mod/forum/lib.php
Expand Up @@ -5050,11 +5050,16 @@ function forum_check_throttling($forum, $cm = null) {
global $CFG, $DB, $USER;

if (is_numeric($forum)) {
$forum = $DB->get_record('forum', array('id' => $forum), '*', MUST_EXIST);
$forum = $DB->get_record('forum', ['id' => $forum], 'id, course, blockperiod, blockafter, warnafter', MUST_EXIST);
}

if (!is_object($forum)) {
return false; // This is broken.
if (!is_object($forum) || !isset($forum->id) || !isset($forum->course)) {
// The passed forum parameter is invalid. This can happen if:
// - a non-object and non-numeric forum is passed; or
// - the forum object does not have an ID or course attributes.
// This is unlikely to happen with properly formed forum record fetched from the database,
// so it's most likely a dev error if we hit such this case.
throw new coding_exception('Invalid forum parameter passed');
}

if (empty($forum->blockafter)) {
Expand Down Expand Up @@ -5120,6 +5125,9 @@ function forum_check_throttling($forum, $cm = null) {

return $warning;
}

// No warning needs to be shown yet.
return false;
}

/**
Expand Down

0 comments on commit 8a9fea3

Please sign in to comment.