MDL-41143 mod_forum: Prevent unnessary loading of formslib

This commit is contained in:
Frederic Massart 2013-08-12 15:24:26 +08:00
parent 838d78a9ff
commit 2ecd284779
6 changed files with 251 additions and 203 deletions

View File

@ -0,0 +1,237 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* File containing the form definition to post in the forum.
*
* @package mod_forum
* @copyright Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/formslib.php');
/**
* Class to post in a forum.
*
* @package mod_forum
* @copyright Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_forum_post_form extends moodleform {
/**
* Returns the options array to use in filemanager for forum attachments
*
* @param stdClass $forum
* @return array
*/
public static function attachment_options($forum) {
global $COURSE, $PAGE, $CFG;
$maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
return array(
'subdirs' => 0,
'maxbytes' => $maxbytes,
'maxfiles' => $forum->maxattachments,
'accepted_types' => '*',
'return_types' => FILE_INTERNAL
);
}
/**
* Returns the options array to use in forum text editor
*
* @return array
*/
public static function editor_options() {
global $COURSE, $PAGE, $CFG;
// TODO: add max files and max size support
$maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
return array(
'maxfiles' => EDITOR_UNLIMITED_FILES,
'maxbytes' => $maxbytes,
'trusttext'=> true,
'return_types'=> FILE_INTERNAL | FILE_EXTERNAL
);
}
/**
* Form definition
*
* @return void
*/
function definition() {
global $CFG, $OUTPUT;
$mform =& $this->_form;
$course = $this->_customdata['course'];
$cm = $this->_customdata['cm'];
$coursecontext = $this->_customdata['coursecontext'];
$modcontext = $this->_customdata['modcontext'];
$forum = $this->_customdata['forum'];
$post = $this->_customdata['post'];
$edit = $this->_customdata['edit'];
$thresholdwarning = $this->_customdata['thresholdwarning'];
$mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
// If there is a warning message and we are not editing a post we need to handle the warning.
if (!empty($thresholdwarning) && !$edit) {
// Here we want to display a warning if they can still post but have reached the warning threshold.
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');
$mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options());
$mform->setType('message', PARAM_RAW);
$mform->addRule('message', get_string('required'), 'required', null, 'client');
if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
$mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
$mform->addElement('hidden', 'subscribe');
$mform->setType('subscribe', PARAM_INT);
$mform->addHelpButton('subscribemessage', 'subscription', 'forum');
} else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
has_capability('moodle/course:manageactivities', $coursecontext)) {
$options = array();
$options[0] = get_string('subscribestop', 'forum');
$options[1] = get_string('subscribestart', 'forum');
$mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
$mform->addHelpButton('subscribe', 'subscription', 'forum');
} else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
$mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
$mform->addElement('hidden', 'subscribe');
$mform->setType('subscribe', PARAM_INT);
$mform->addHelpButton('subscribemessage', 'subscription', 'forum');
}
if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
$mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
$mform->addHelpButton('attachments', 'attachment', 'forum');
}
if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
$mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
}
if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
$mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
$mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
$mform->addHelpButton('timestart', 'displaystart', 'forum');
$mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
$mform->addHelpButton('timeend', 'displayend', 'forum');
} else {
$mform->addElement('hidden', 'timestart');
$mform->setType('timestart', PARAM_INT);
$mform->addElement('hidden', 'timeend');
$mform->setType('timeend', PARAM_INT);
$mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
}
if (groups_get_activity_groupmode($cm, $course)) { // hack alert
$groupdata = groups_get_activity_allowed_groups($cm);
$groupcount = count($groupdata);
$modulecontext = context_module::instance($cm->id);
$contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
if ($contextcheck) {
$groupinfo = array('0' => get_string('allparticipants'));
foreach ($groupdata as $grouptemp) {
$groupinfo[$grouptemp->id] = $grouptemp->name;
}
$mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
$mform->setDefault('groupinfo', $post->groupid);
} else {
if (empty($post->groupid)) {
$groupname = get_string('allparticipants');
} else {
$groupname = format_string($groupdata[$post->groupid]->name);
}
$mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
}
}
//-------------------------------------------------------------------------------
// buttons
if (isset($post->edit)) { // hack alert
$submit_string = get_string('savechanges');
} else {
$submit_string = get_string('posttoforum', 'forum');
}
$this->add_action_buttons(false, $submit_string);
$mform->addElement('hidden', 'course');
$mform->setType('course', PARAM_INT);
$mform->addElement('hidden', 'forum');
$mform->setType('forum', PARAM_INT);
$mform->addElement('hidden', 'discussion');
$mform->setType('discussion', PARAM_INT);
$mform->addElement('hidden', 'parent');
$mform->setType('parent', PARAM_INT);
$mform->addElement('hidden', 'userid');
$mform->setType('userid', PARAM_INT);
$mform->addElement('hidden', 'groupid');
$mform->setType('groupid', PARAM_INT);
$mform->addElement('hidden', 'edit');
$mform->setType('edit', PARAM_INT);
$mform->addElement('hidden', 'reply');
$mform->setType('reply', PARAM_INT);
}
/**
* Form validation
*
* @param array $data data from the form.
* @param array $files files uploaded.
* @return array of errors.
*/
function validation($data, $files) {
$errors = parent::validation($data, $files);
if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
$errors['timeend'] = get_string('timestartenderror', 'forum');
}
if (empty($data['message']['text'])) {
$errors['message'] = get_string('erroremptymessage', 'forum');
}
if (empty($data['subject'])) {
$errors['subject'] = get_string('erroremptysubject', 'forum');
}
return $errors;
}
}

View File

@ -27,7 +27,6 @@ defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->libdir.'/eventslib.php');
require_once($CFG->dirroot.'/user/selector/lib.php');
require_once($CFG->dirroot.'/mod/forum/post_form.php');
/// CONSTANTS ///////////////////////////////////////////////////////////

View File

@ -505,8 +505,6 @@ if (!isset($forum->maxattachments)) { // TODO - delete this once we add a field
$forum->maxattachments = 3;
}
require_once('post_form.php');
$thresholdwarning = forum_check_throttling($forum, $cm);
$mform_post = new mod_forum_post_form('post.php', array('course' => $course,
'cm' => $cm,

View File

@ -16,204 +16,13 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package mod-forum
* @copyright Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package mod_forum
* @copyright Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @deprecated since 2.6
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
require_once($CFG->libdir.'/formslib.php');
class mod_forum_post_form extends moodleform {
/**
* Returns the options array to use in filemanager for forum attachments
*
* @param stdClass $forum
* @return array
*/
public static function attachment_options($forum) {
global $COURSE, $PAGE, $CFG;
$maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
return array(
'subdirs' => 0,
'maxbytes' => $maxbytes,
'maxfiles' => $forum->maxattachments,
'accepted_types' => '*',
'return_types' => FILE_INTERNAL
);
}
/**
* Returns the options array to use in forum text editor
*
* @return array
*/
public static function editor_options() {
global $COURSE, $PAGE, $CFG;
// TODO: add max files and max size support
$maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
return array(
'maxfiles' => EDITOR_UNLIMITED_FILES,
'maxbytes' => $maxbytes,
'trusttext'=> true,
'return_types'=> FILE_INTERNAL | FILE_EXTERNAL
);
}
function definition() {
global $CFG, $OUTPUT;
$mform =& $this->_form;
$course = $this->_customdata['course'];
$cm = $this->_customdata['cm'];
$coursecontext = $this->_customdata['coursecontext'];
$modcontext = $this->_customdata['modcontext'];
$forum = $this->_customdata['forum'];
$post = $this->_customdata['post'];
$edit = $this->_customdata['edit'];
$thresholdwarning = $this->_customdata['thresholdwarning'];
$mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
// If there is a warning message and we are not editing a post we need to handle the warning.
if (!empty($thresholdwarning) && !$edit) {
// Here we want to display a warning if they can still post but have reached the warning threshold.
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');
$mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
$mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options());
$mform->setType('message', PARAM_RAW);
$mform->addRule('message', get_string('required'), 'required', null, 'client');
if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
$mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
$mform->addElement('hidden', 'subscribe');
$mform->setType('subscribe', PARAM_INT);
$mform->addHelpButton('subscribemessage', 'subscription', 'forum');
} else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
has_capability('moodle/course:manageactivities', $coursecontext)) {
$options = array();
$options[0] = get_string('subscribestop', 'forum');
$options[1] = get_string('subscribestart', 'forum');
$mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
$mform->addHelpButton('subscribe', 'subscription', 'forum');
} else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
$mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
$mform->addElement('hidden', 'subscribe');
$mform->setType('subscribe', PARAM_INT);
$mform->addHelpButton('subscribemessage', 'subscription', 'forum');
}
if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
$mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
$mform->addHelpButton('attachments', 'attachment', 'forum');
}
if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
$mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
}
if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
$mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
$mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
$mform->addHelpButton('timestart', 'displaystart', 'forum');
$mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
$mform->addHelpButton('timeend', 'displayend', 'forum');
} else {
$mform->addElement('hidden', 'timestart');
$mform->setType('timestart', PARAM_INT);
$mform->addElement('hidden', 'timeend');
$mform->setType('timeend', PARAM_INT);
$mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
}
if (groups_get_activity_groupmode($cm, $course)) { // hack alert
$groupdata = groups_get_activity_allowed_groups($cm);
$groupcount = count($groupdata);
$modulecontext = context_module::instance($cm->id);
$contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
if ($contextcheck) {
$groupinfo = array('0' => get_string('allparticipants'));
foreach ($groupdata as $grouptemp) {
$groupinfo[$grouptemp->id] = $grouptemp->name;
}
$mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
$mform->setDefault('groupinfo', $post->groupid);
} else {
if (empty($post->groupid)) {
$groupname = get_string('allparticipants');
} else {
$groupname = format_string($groupdata[$post->groupid]->name);
}
$mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
}
}
//-------------------------------------------------------------------------------
// buttons
if (isset($post->edit)) { // hack alert
$submit_string = get_string('savechanges');
} else {
$submit_string = get_string('posttoforum', 'forum');
}
$this->add_action_buttons(false, $submit_string);
$mform->addElement('hidden', 'course');
$mform->setType('course', PARAM_INT);
$mform->addElement('hidden', 'forum');
$mform->setType('forum', PARAM_INT);
$mform->addElement('hidden', 'discussion');
$mform->setType('discussion', PARAM_INT);
$mform->addElement('hidden', 'parent');
$mform->setType('parent', PARAM_INT);
$mform->addElement('hidden', 'userid');
$mform->setType('userid', PARAM_INT);
$mform->addElement('hidden', 'groupid');
$mform->setType('groupid', PARAM_INT);
$mform->addElement('hidden', 'edit');
$mform->setType('edit', PARAM_INT);
$mform->addElement('hidden', 'reply');
$mform->setType('reply', PARAM_INT);
}
function validation($data, $files) {
$errors = parent::validation($data, $files);
if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
$errors['timeend'] = get_string('timestartenderror', 'forum');
}
if (empty($data['message']['text'])) {
$errors['message'] = get_string('erroremptymessage', 'forum');
}
if (empty($data['subject'])) {
$errors['subject'] = get_string('erroremptysubject', 'forum');
}
return $errors;
}
}
defined('MOODLE_INTERNAL') || die();
// TODO MDL-41313 Remove this file.
debugging('Do not include post_form.php directly, it is now using automatic class loading.', DEBUG_DEVELOPER);

View File

@ -1,6 +1,11 @@
This files describes API changes in /mod/forum/*,
information provided here is intended especially for developers.
=== 2.6 ===
* The file post_form.php should not be included, the class it contained has
been moved so that it can benefit from autoloading.
=== 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
@ -11,4 +16,4 @@ as a HTML element, where it is more noticeable. False is returned if there is no
* 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.
then please remove this capability for frontpage role.

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2013081500.00; // YYYYMMDD = weekly release date of this DEV branch
$version = 2013081500.01; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes