Merge branch 'MDL-60819-35' of https://github.com/snake/moodle into MOODLE_35_STABLE
[moodle.git] / mod / forum / mod_form.php
blobdecea410987304f4ee99adb89774074da2fd10e0
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * @package mod_forum
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);
43 } else {
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 // Attachments and word count.
58 $mform->addElement('header', 'attachmentswordcounthdr', get_string('attachmentswordcount', 'forum'));
60 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes, 0, $CFG->forum_maxbytes);
61 $choices[1] = get_string('uploadnotallowed');
62 $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
63 $mform->addHelpButton('maxbytes', 'maxattachmentsize', 'forum');
64 $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
66 $choices = array(
67 0 => 0,
68 1 => 1,
69 2 => 2,
70 3 => 3,
71 4 => 4,
72 5 => 5,
73 6 => 6,
74 7 => 7,
75 8 => 8,
76 9 => 9,
77 10 => 10,
78 20 => 20,
79 50 => 50,
80 100 => 100
82 $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
83 $mform->addHelpButton('maxattachments', 'maxattachments', 'forum');
84 $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
86 $mform->addElement('selectyesno', 'displaywordcount', get_string('displaywordcount', 'forum'));
87 $mform->addHelpButton('displaywordcount', 'displaywordcount', 'forum');
88 $mform->setDefault('displaywordcount', 0);
90 // Subscription and tracking.
91 $mform->addElement('header', 'subscriptionandtrackinghdr', get_string('subscriptionandtracking', 'forum'));
93 $options = forum_get_subscriptionmode_options();
94 $mform->addElement('select', 'forcesubscribe', get_string('subscriptionmode', 'forum'), $options);
95 $mform->addHelpButton('forcesubscribe', 'subscriptionmode', 'forum');
96 if (isset($CFG->forum_subscription)) {
97 $defaultforumsubscription = $CFG->forum_subscription;
98 } else {
99 $defaultforumsubscription = FORUM_CHOOSESUBSCRIBE;
101 $mform->setDefault('forcesubscribe', $defaultforumsubscription);
103 $options = array();
104 $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
105 $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
106 if ($CFG->forum_allowforcedreadtracking) {
107 $options[FORUM_TRACKING_FORCED] = get_string('trackingon', 'forum');
109 $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
110 $mform->addHelpButton('trackingtype', 'trackingtype', 'forum');
111 $default = $CFG->forum_trackingtype;
112 if ((!$CFG->forum_allowforcedreadtracking) && ($default == FORUM_TRACKING_FORCED)) {
113 $default = FORUM_TRACKING_OPTIONAL;
115 $mform->setDefault('trackingtype', $default);
117 if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
118 //-------------------------------------------------------------------------------
119 $mform->addElement('header', 'rssheader', get_string('rss'));
120 $choices = array();
121 $choices[0] = get_string('none');
122 $choices[1] = get_string('discussions', 'forum');
123 $choices[2] = get_string('posts', 'forum');
124 $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
125 $mform->addHelpButton('rsstype', 'rsstype', 'forum');
126 if (isset($CFG->forum_rsstype)) {
127 $mform->setDefault('rsstype', $CFG->forum_rsstype);
130 $choices = array();
131 $choices[0] = '0';
132 $choices[1] = '1';
133 $choices[2] = '2';
134 $choices[3] = '3';
135 $choices[4] = '4';
136 $choices[5] = '5';
137 $choices[10] = '10';
138 $choices[15] = '15';
139 $choices[20] = '20';
140 $choices[25] = '25';
141 $choices[30] = '30';
142 $choices[40] = '40';
143 $choices[50] = '50';
144 $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
145 $mform->addHelpButton('rssarticles', 'rssarticles', 'forum');
146 $mform->disabledIf('rssarticles', 'rsstype', 'eq', '0');
147 if (isset($CFG->forum_rssarticles)) {
148 $mform->setDefault('rssarticles', $CFG->forum_rssarticles);
152 $mform->addElement('header', 'discussionlocking', get_string('discussionlockingheader', 'forum'));
153 $options = [
154 0 => get_string('discussionlockingdisabled', 'forum'),
155 1 * DAYSECS => get_string('numday', 'core', 1),
156 1 * WEEKSECS => get_string('numweek', 'core', 1),
157 2 * WEEKSECS => get_string('numweeks', 'core', 2),
158 30 * DAYSECS => get_string('nummonth', 'core', 1),
159 60 * DAYSECS => get_string('nummonths', 'core', 2),
160 90 * DAYSECS => get_string('nummonths', 'core', 3),
161 180 * DAYSECS => get_string('nummonths', 'core', 6),
162 1 * YEARSECS => get_string('numyear', 'core', 1),
164 $mform->addElement('select', 'lockdiscussionafter', get_string('lockdiscussionafter', 'forum'), $options);
165 $mform->addHelpButton('lockdiscussionafter', 'lockdiscussionafter', 'forum');
166 $mform->disabledIf('lockdiscussionafter', 'type', 'eq', 'single');
168 //-------------------------------------------------------------------------------
169 $mform->addElement('header', 'blockafterheader', get_string('blockafter', 'forum'));
170 $options = array();
171 $options[0] = get_string('blockperioddisabled','forum');
172 $options[60*60*24] = '1 '.get_string('day');
173 $options[60*60*24*2] = '2 '.get_string('days');
174 $options[60*60*24*3] = '3 '.get_string('days');
175 $options[60*60*24*4] = '4 '.get_string('days');
176 $options[60*60*24*5] = '5 '.get_string('days');
177 $options[60*60*24*6] = '6 '.get_string('days');
178 $options[60*60*24*7] = '1 '.get_string('week');
179 $mform->addElement('select', 'blockperiod', get_string('blockperiod', 'forum'), $options);
180 $mform->addHelpButton('blockperiod', 'blockperiod', 'forum');
182 $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
183 $mform->setType('blockafter', PARAM_INT);
184 $mform->setDefault('blockafter', '0');
185 $mform->addRule('blockafter', null, 'numeric', null, 'client');
186 $mform->addHelpButton('blockafter', 'blockafter', 'forum');
187 $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0);
189 $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
190 $mform->setType('warnafter', PARAM_INT);
191 $mform->setDefault('warnafter', '0');
192 $mform->addRule('warnafter', null, 'numeric', null, 'client');
193 $mform->addHelpButton('warnafter', 'warnafter', 'forum');
194 $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0);
196 $coursecontext = context_course::instance($COURSE->id);
197 plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_forum');
199 //-------------------------------------------------------------------------------
201 $this->standard_grading_coursemodule_elements();
203 $this->standard_coursemodule_elements();
204 //-------------------------------------------------------------------------------
205 // buttons
206 $this->add_action_buttons();
210 function definition_after_data() {
211 parent::definition_after_data();
212 $mform =& $this->_form;
213 $type =& $mform->getElement('type');
214 $typevalue = $mform->getElementValue('type');
216 //we don't want to have these appear as possible selections in the form but
217 //we want the form to display them if they are set.
218 if ($typevalue[0]=='news') {
219 $type->addOption(get_string('namenews', 'forum'), 'news');
220 $mform->addHelpButton('type', 'namenews', 'forum');
221 $type->freeze();
222 $type->setPersistantFreeze(true);
224 if ($typevalue[0]=='social') {
225 $type->addOption(get_string('namesocial', 'forum'), 'social');
226 $type->freeze();
227 $type->setPersistantFreeze(true);
232 function data_preprocessing(&$default_values) {
233 parent::data_preprocessing($default_values);
235 // Set up the completion checkboxes which aren't part of standard data.
236 // We also make the default value (if you turn on the checkbox) for those
237 // numbers to be 1, this will not apply unless checkbox is ticked.
238 $default_values['completiondiscussionsenabled']=
239 !empty($default_values['completiondiscussions']) ? 1 : 0;
240 if (empty($default_values['completiondiscussions'])) {
241 $default_values['completiondiscussions']=1;
243 $default_values['completionrepliesenabled']=
244 !empty($default_values['completionreplies']) ? 1 : 0;
245 if (empty($default_values['completionreplies'])) {
246 $default_values['completionreplies']=1;
248 // Tick by default if Add mode or if completion posts settings is set to 1 or more.
249 if (empty($this->_instance) || !empty($default_values['completionposts'])) {
250 $default_values['completionpostsenabled'] = 1;
251 } else {
252 $default_values['completionpostsenabled'] = 0;
254 if (empty($default_values['completionposts'])) {
255 $default_values['completionposts']=1;
260 * Add custom completion rules.
262 * @return array Array of string IDs of added items, empty array if none
264 public function add_completion_rules() {
265 $mform =& $this->_form;
267 $group=array();
268 $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '', get_string('completionposts','forum'));
269 $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
270 $mform->setType('completionposts',PARAM_INT);
271 $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup','forum'), array(' '), false);
272 $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
274 $group=array();
275 $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '', get_string('completiondiscussions','forum'));
276 $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
277 $mform->setType('completiondiscussions',PARAM_INT);
278 $mform->addGroup($group, 'completiondiscussionsgroup', get_string('completiondiscussionsgroup','forum'), array(' '), false);
279 $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
281 $group=array();
282 $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
283 $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
284 $mform->setType('completionreplies',PARAM_INT);
285 $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
286 $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
288 return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
291 function completion_rule_enabled($data) {
292 return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
293 (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
294 (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
298 * Allows module to modify the data returned by form get_data().
299 * This method is also called in the bulk activity completion form.
301 * Only available on moodleform_mod.
303 * @param stdClass $data the form data to be modified.
305 public function data_postprocessing($data) {
306 parent::data_postprocessing($data);
307 // Turn off completion settings if the checkboxes aren't ticked
308 if (!empty($data->completionunlocked)) {
309 $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
310 if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
311 $data->completiondiscussions = 0;
313 if (empty($data->completionrepliesenabled) || !$autocompletion) {
314 $data->completionreplies = 0;
316 if (empty($data->completionpostsenabled) || !$autocompletion) {
317 $data->completionposts = 0;