diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 8994e713475..928fda435d5 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -7060,21 +7060,26 @@ function forum_get_separate_modules($courseid) { } /** - * @global object - * @global object - * @global object - * @param object $forum - * @param object $cm - * @return bool + * Handles the situation where the user has reached the blocking or warning threshold. + * The function will either echo out a message, or throw an exception depending on the + * threshold reached (warning or blocked). If the forum passed is invalid false is + * returned, otherwise if no restriction is needed true is returned. + * + * @param int|stdClass $forum the forum id or the forum object + * @param stdClass $cm the course module + * @param bool $display do we want to echo out the message? + * @return bool returns false if $forum is invalid or true + * if there is no message to show. */ -function forum_check_throttling($forum, $cm=null) { +function forum_check_throttling($forum, $cm = null, $display = true) { global $USER, $CFG, $DB, $OUTPUT; if (is_numeric($forum)) { $forum = $DB->get_record('forum',array('id'=>$forum)); } + if (!is_object($forum)) { - return false; // this is broken. + return false; // This is broken. } if (empty($forum->blockafter)) { @@ -7092,18 +7097,18 @@ function forum_check_throttling($forum, $cm=null) { } $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); - if(has_capability('mod/forum:postwithoutthrottling', $modcontext)) { + if (has_capability('mod/forum:postwithoutthrottling', $modcontext)) { return true; } - // get the number of posts in the last period we care about + // Get the number of posts in the last period we care about. $timenow = time(); $timeafter = $timenow - $forum->blockperiod; - $numposts = $DB->count_records_sql('SELECT COUNT(p.id) FROM {forum_posts} p' - .' JOIN {forum_discussions} d' - .' ON p.discussion = d.id WHERE d.forum = ?' - .' AND p.userid = ? AND p.created > ?', array($forum->id, $USER->id, $timeafter)); + $numposts = $DB->count_records_sql('SELECT COUNT(p.id) FROM {forum_posts} p + JOIN {forum_discussions} d + ON p.discussion = d.id WHERE d.forum = ? + AND p.userid = ? AND p.created > ?', array($forum->id, $USER->id, $timeafter)); $a = new stdClass(); $a->blockafter = $forum->blockafter; @@ -7113,11 +7118,12 @@ function forum_check_throttling($forum, $cm=null) { if ($forum->blockafter <= $numposts) { print_error('forumblockingtoomanyposts', 'error', $CFG->wwwroot.'/mod/forum/view.php?f='.$forum->id, $a); } + if ($forum->warnafter <= $numposts) { - echo $OUTPUT->notification(get_string('forumblockingalmosttoomanyposts','forum',$a)); + if ($display) { + echo $OUTPUT->notification(get_string('forumblockingalmosttoomanyposts', 'forum', $a)); + } } - - } diff --git a/mod/forum/post.php b/mod/forum/post.php index 3ecf2ab188d..da522fce053 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -680,7 +680,11 @@ if ($fromform = $mform_post->get_data()) { } else if ($fromform->discussion) { // Adding a new post to an existing discussion + // Before we add this we must check that the user will not exceed the blocking threshold. + forum_check_throttling($forum, $cm, false); + unset($fromform->groupid); + $message = ''; $addpost = $fromform; $addpost->forum=$forum->id; @@ -728,7 +732,10 @@ if ($fromform = $mform_post->get_data()) { } exit; - } else { // Adding a new discussion + } else { // Adding a new discussion. + // Before we add this we must check that the user will not exceed the blocking threshold. + forum_check_throttling($forum, $cm, false); + if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) { print_error('cannotcreatediscussion', 'forum'); }