Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-39182 mod_forum: display the post threshold warning message in a …

…more noticeable area
  • Loading branch information...
commit f5ad424b1196adde46e19b5c6def29af8a148b40 1 parent b3661ab
@markn86 markn86 authored
View
72 mod/forum/lib.php
@@ -7082,50 +7082,49 @@ function forum_get_post_actions() {
}
/**
- * @global object
- * @global object
- * @global object
- * @param object $forum
- * @param object $cm
- * @return bool
+ * Returns a warning object if a user has reached the number of posts equal to
+ * the warning/blocking setting, or false if there is no warning to show.
+ *
+ * @param int|stdClass $forum the forum id or the forum object
+ * @param stdClass $cm the course module
+ * @return stdClass|bool returns an object with the warning information, else
+ * returns false if no warning is required.
*/
-function forum_check_throttling($forum, $cm=null) {
- global $USER, $CFG, $DB, $OUTPUT;
+function forum_check_throttling($forum, $cm = null) {
+ global $CFG, $DB, $USER;
if (is_numeric($forum)) {
- $forum = $DB->get_record('forum',array('id'=>$forum));
+ $forum = $DB->get_record('forum', array('id' => $forum), '*', MUST_EXIST);
}
+
if (!is_object($forum)) {
- return false; // this is broken.
+ return false; // This is broken.
}
- if (empty($forum->blockafter)) {
- return true;
+ if (!$cm) {
+ $cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course, false, MUST_EXIST);
}
- if (empty($forum->blockperiod)) {
- return true;
+ if (empty($forum->blockafter)) {
+ return false;
}
- if (!$cm) {
- if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
- print_error('invalidcoursemodule');
- }
+ if (empty($forum->blockperiod)) {
+ return false;
}
$modcontext = context_module::instance($cm->id);
- if(has_capability('mod/forum:postwithoutthrottling', $modcontext)) {
- return true;
+ if (has_capability('mod/forum:postwithoutthrottling', $modcontext)) {
+ return false;
}
- // 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;
@@ -7133,13 +7132,26 @@ function forum_check_throttling($forum, $cm=null) {
$a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
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));
+ $warning = new stdClass();
+ $warning->canpost = false;
+ $warning->errorcode = 'forumblockingtoomanyposts';
+ $warning->module = 'error';
+ $warning->additional = $a;
+ $warning->link = $CFG->wwwroot . '/mod/forum/view.php?f=' . $forum->id;
+
+ return $warning;
}
+ if ($forum->warnafter <= $numposts) {
+ $warning = new stdClass();
+ $warning->canpost = true;
+ $warning->errorcode = 'forumblockingalmosttoomanyposts';
+ $warning->module = 'forum';
+ $warning->additional = $a;
+ $warning->link = null;
+ return $warning;
+ }
}
View
18 mod/forum/post.php
@@ -507,7 +507,14 @@
require_once('post_form.php');
-$mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post), 'post', '', array('id' => 'mformforum'));
+$thresholdwarning = forum_check_throttling($forum, $cm);
+$mform_post = new mod_forum_post_form('post.php', array('course' => $course,
+ 'cm' => $cm,
+ 'coursecontext' => $coursecontext,
+ 'modcontext' => $modcontext,
+ 'forum' => $forum,
+ 'post' => $post,
+ 'thresholdwarning' => $thresholdwarning), 'post', '', array('id' => 'mformforum'));
$draftitemid = file_get_submitted_draft_itemid('attachments');
file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', empty($post->id)?null:$post->id, mod_forum_post_form::attachment_options($forum));
@@ -864,10 +871,15 @@
echo $OUTPUT->notification(get_string('qandanotify','forum'));
}
-forum_check_throttling($forum, $cm);
+if (!empty($thresholdwarning)) {
+ if (!$thresholdwarning->canpost) {
+ print_error($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->link,
+ $thresholdwarning->additional);
+ }
+}
if (!empty($parent)) {
- if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
+ if (!$discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
print_error('notpartofdiscussion', 'forum');
}
View
23 mod/forum/post_form.php
@@ -65,18 +65,27 @@ public static function editor_options() {
}
function definition() {
+ global $CFG, $OUTPUT;
- global $CFG;
- $mform =& $this->_form;
+ $mform =& $this->_form;
- $course = $this->_customdata['course'];
- $cm = $this->_customdata['cm'];
+ $course = $this->_customdata['course'];
+ $cm = $this->_customdata['cm'];
$coursecontext = $this->_customdata['coursecontext'];
- $modcontext = $this->_customdata['modcontext'];
- $forum = $this->_customdata['forum'];
- $post = $this->_customdata['post'];
+ $modcontext = $this->_customdata['modcontext'];
+ $forum = $this->_customdata['forum'];
+ $post = $this->_customdata['post'];
+ $thresholdwarning = $this->_customdata['thresholdwarning'];
$mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
+
+ if (!empty($thresholdwarning)) {
+ if ($thresholdwarning->canpost) {
+ $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
+ $mform->addElement('html', $OUTPUT->notification($message));
+ }
+ }
+
$mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
$mform->setType('subject', PARAM_TEXT);
$mform->addRule('subject', get_string('required'), 'required', null, 'client');
View
6 mod/forum/upgrade.txt
@@ -1,8 +1,14 @@
This files describes API changes in /mod/forum/*,
information provided here is intended especially for developers.
+=== 2.5 ===
+
+The function forum_check_throttling has been changed so that a warning object is returned when a user has reached the 'Post threshold for warning' or
+'Post threshold for blocking' setting, rather than echoing this on screen. This allows the warning to be moved inside the post form as it can be added
+as a HTML element, where it is more noticeable. False is returned if there is no need to warn, or restrict the user from posting - see MDL-39182.
=== 2.3.5, 2.4.2 ===
+
* mod/forum:allowforcesubscribe capability will be forcefully assigned to frontpage role, as it was mistakenly missed off
when the capability was initially created. If you don't want users with frontpage role to get forum (with forcesubscribe) emails,
then please remove this capability for frontpage role.
Please sign in to comment.
Something went wrong with that request. Please try again.