3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * @copyright Jamie Pratt <me@jamiep.org>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 if (!defined('MOODLE_INTERNAL')) {
25 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
30 class mod_forum_mod_form extends moodleform_mod {
32 function definition() {
33 global $CFG, $COURSE, $DB;
35 $mform =& $this->_form;
37 //-------------------------------------------------------------------------------
38 $mform->addElement('header', 'general', get_string('general', 'form'));
40 $mform->addElement('text', 'name', get_string('forumname', 'forum'), array('size'=>'64'));
41 if (!empty($CFG->formatstringstriptags)) {
42 $mform->setType('name', PARAM_TEXT);
44 $mform->setType('name', PARAM_CLEANHTML);
46 $mform->addRule('name', null, 'required', null, 'client');
47 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
49 $this->standard_intro_elements(get_string('forumintro', 'forum'));
51 $forumtypes = forum_get_forum_types();
52 core_collator::asort($forumtypes, core_collator::SORT_STRING);
53 $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forumtypes);
54 $mform->addHelpButton('type', 'forumtype', 'forum');
55 $mform->setDefault('type', 'general');
57 $mform->addElement('header', 'availability', get_string('availability', 'forum'));
59 $name = get_string('duedate', 'forum');
60 $mform->addElement('date_time_selector', 'duedate', $name, array('optional' => true));
61 $mform->addHelpButton('duedate', 'duedate', 'forum');
63 $name = get_string('cutoffdate', 'forum');
64 $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional' => true));
65 $mform->addHelpButton('cutoffdate', 'cutoffdate', 'forum');
67 // Attachments and word count.
68 $mform->addElement('header', 'attachmentswordcounthdr', get_string('attachmentswordcount', 'forum'));
70 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes, 0, $CFG->forum_maxbytes);
71 $choices[1] = get_string('uploadnotallowed');
72 $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
73 $mform->addHelpButton('maxbytes', 'maxattachmentsize', 'forum');
74 $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
92 $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
93 $mform->addHelpButton('maxattachments', 'maxattachments', 'forum');
94 $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
96 $mform->addElement('selectyesno', 'displaywordcount', get_string('displaywordcount', 'forum'));
97 $mform->addHelpButton('displaywordcount', 'displaywordcount', 'forum');
98 $mform->setDefault('displaywordcount', 0);
100 // Subscription and tracking.
101 $mform->addElement('header', 'subscriptionandtrackinghdr', get_string('subscriptionandtracking', 'forum'));
103 $options = forum_get_subscriptionmode_options();
104 $mform->addElement('select', 'forcesubscribe', get_string('subscriptionmode', 'forum'), $options);
105 $mform->addHelpButton('forcesubscribe', 'subscriptionmode', 'forum');
106 if (isset($CFG->forum_subscription)) {
107 $defaultforumsubscription = $CFG->forum_subscription;
109 $defaultforumsubscription = FORUM_CHOOSESUBSCRIBE;
111 $mform->setDefault('forcesubscribe', $defaultforumsubscription);
114 $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
115 $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
116 if ($CFG->forum_allowforcedreadtracking) {
117 $options[FORUM_TRACKING_FORCED] = get_string('trackingon', 'forum');
119 $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
120 $mform->addHelpButton('trackingtype', 'trackingtype', 'forum');
121 $default = $CFG->forum_trackingtype;
122 if ((!$CFG->forum_allowforcedreadtracking) && ($default == FORUM_TRACKING_FORCED)) {
123 $default = FORUM_TRACKING_OPTIONAL;
125 $mform->setDefault('trackingtype', $default);
127 if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
128 //-------------------------------------------------------------------------------
129 $mform->addElement('header', 'rssheader', get_string('rss'));
131 $choices[0] = get_string('none');
132 $choices[1] = get_string('discussions', 'forum');
133 $choices[2] = get_string('posts', 'forum');
134 $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
135 $mform->addHelpButton('rsstype', 'rsstype', 'forum');
136 if (isset($CFG->forum_rsstype)) {
137 $mform->setDefault('rsstype', $CFG->forum_rsstype);
154 $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
155 $mform->addHelpButton('rssarticles', 'rssarticles', 'forum');
156 $mform->hideIf('rssarticles', 'rsstype', 'eq', '0');
157 if (isset($CFG->forum_rssarticles)) {
158 $mform->setDefault('rssarticles', $CFG->forum_rssarticles);
162 $mform->addElement('header', 'discussionlocking', get_string('discussionlockingheader', 'forum'));
164 0 => get_string('discussionlockingdisabled', 'forum'),
165 1 * DAYSECS => get_string('numday', 'core', 1),
166 1 * WEEKSECS => get_string('numweek', 'core', 1),
167 2 * WEEKSECS => get_string('numweeks', 'core', 2),
168 30 * DAYSECS => get_string('nummonth', 'core', 1),
169 60 * DAYSECS => get_string('nummonths', 'core', 2),
170 90 * DAYSECS => get_string('nummonths', 'core', 3),
171 180 * DAYSECS => get_string('nummonths', 'core', 6),
172 1 * YEARSECS => get_string('numyear', 'core', 1),
174 $mform->addElement('select', 'lockdiscussionafter', get_string('lockdiscussionafter', 'forum'), $options);
175 $mform->addHelpButton('lockdiscussionafter', 'lockdiscussionafter', 'forum');
176 $mform->disabledIf('lockdiscussionafter', 'type', 'eq', 'single');
178 //-------------------------------------------------------------------------------
179 $mform->addElement('header', 'blockafterheader', get_string('blockafter', 'forum'));
181 $options[0] = get_string('blockperioddisabled','forum');
182 $options[60*60*24] = '1 '.get_string('day');
183 $options[60*60*24*2] = '2 '.get_string('days');
184 $options[60*60*24*3] = '3 '.get_string('days');
185 $options[60*60*24*4] = '4 '.get_string('days');
186 $options[60*60*24*5] = '5 '.get_string('days');
187 $options[60*60*24*6] = '6 '.get_string('days');
188 $options[60*60*24*7] = '1 '.get_string('week');
189 $mform->addElement('select', 'blockperiod', get_string('blockperiod', 'forum'), $options);
190 $mform->addHelpButton('blockperiod', 'blockperiod', 'forum');
192 $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
193 $mform->setType('blockafter', PARAM_INT);
194 $mform->setDefault('blockafter', '0');
195 $mform->addRule('blockafter', null, 'numeric', null, 'client');
196 $mform->addHelpButton('blockafter', 'blockafter', 'forum');
197 $mform->hideIf('blockafter', 'blockperiod', 'eq', 0);
199 $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
200 $mform->setType('warnafter', PARAM_INT);
201 $mform->setDefault('warnafter', '0');
202 $mform->addRule('warnafter', null, 'numeric', null, 'client');
203 $mform->addHelpButton('warnafter', 'warnafter', 'forum');
204 $mform->hideIf('warnafter', 'blockperiod', 'eq', 0);
206 $coursecontext = context_course::instance($COURSE->id);
207 plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_forum');
209 //-------------------------------------------------------------------------------
211 $this->standard_grading_coursemodule_elements();
213 $this->standard_coursemodule_elements();
214 //-------------------------------------------------------------------------------
216 $this->add_action_buttons();
220 function definition_after_data() {
221 parent::definition_after_data();
222 $mform =& $this->_form;
223 $type =& $mform->getElement('type');
224 $typevalue = $mform->getElementValue('type');
226 //we don't want to have these appear as possible selections in the form but
227 //we want the form to display them if they are set.
228 if ($typevalue[0]=='news') {
229 $type->addOption(get_string('namenews', 'forum'), 'news');
230 $mform->addHelpButton('type', 'namenews', 'forum');
232 $type->setPersistantFreeze(true);
234 if ($typevalue[0]=='social') {
235 $type->addOption(get_string('namesocial', 'forum'), 'social');
237 $type->setPersistantFreeze(true);
242 public function validation($data, $files) {
243 $errors = parent::validation($data, $files);
245 if ($data['duedate'] && $data['cutoffdate']) {
246 if ($data['duedate'] > $data['cutoffdate']) {
247 $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'forum');
254 function data_preprocessing(&$default_values) {
255 parent::data_preprocessing($default_values);
257 // Set up the completion checkboxes which aren't part of standard data.
258 // We also make the default value (if you turn on the checkbox) for those
259 // numbers to be 1, this will not apply unless checkbox is ticked.
260 $default_values['completiondiscussionsenabled']=
261 !empty($default_values['completiondiscussions']) ? 1 : 0;
262 if (empty($default_values['completiondiscussions'])) {
263 $default_values['completiondiscussions']=1;
265 $default_values['completionrepliesenabled']=
266 !empty($default_values['completionreplies']) ? 1 : 0;
267 if (empty($default_values['completionreplies'])) {
268 $default_values['completionreplies']=1;
270 // Tick by default if Add mode or if completion posts settings is set to 1 or more.
271 if (empty($this->_instance) || !empty($default_values['completionposts'])) {
272 $default_values['completionpostsenabled'] = 1;
274 $default_values['completionpostsenabled'] = 0;
276 if (empty($default_values['completionposts'])) {
277 $default_values['completionposts']=1;
282 * Add custom completion rules.
284 * @return array Array of string IDs of added items, empty array if none
286 public function add_completion_rules() {
287 $mform =& $this->_form;
290 $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '', get_string('completionposts','forum'));
291 $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
292 $mform->setType('completionposts',PARAM_INT);
293 $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup','forum'), array(' '), false);
294 $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
297 $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '', get_string('completiondiscussions','forum'));
298 $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
299 $mform->setType('completiondiscussions',PARAM_INT);
300 $mform->addGroup($group, 'completiondiscussionsgroup', get_string('completiondiscussionsgroup','forum'), array(' '), false);
301 $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
304 $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
305 $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
306 $mform->setType('completionreplies',PARAM_INT);
307 $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
308 $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
310 return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
313 function completion_rule_enabled($data) {
314 return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
315 (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
316 (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
320 * Allows module to modify the data returned by form get_data().
321 * This method is also called in the bulk activity completion form.
323 * Only available on moodleform_mod.
325 * @param stdClass $data the form data to be modified.
327 public function data_postprocessing($data) {
328 parent::data_postprocessing($data);
329 // Turn off completion settings if the checkboxes aren't ticked
330 if (!empty($data->completionunlocked)) {
331 $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
332 if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
333 $data->completiondiscussions = 0;
335 if (empty($data->completionrepliesenabled) || !$autocompletion) {
336 $data->completionreplies = 0;
338 if (empty($data->completionpostsenabled) || !$autocompletion) {
339 $data->completionposts = 0;