MDL-59460 mod_forum: default setting for Subscription mode.
[moodle.git] / mod / forum / mod_form.php
CommitLineData
535e0ab6 1<?php
8f685009
SH
2
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.
14//
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/>.
17
18/**
01030f1b 19 * @package mod_forum
8f685009
SH
20 * @copyright Jamie Pratt <me@jamiep.org>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
bfebaf64
MD
24if (!defined('MOODLE_INTERNAL')) {
25 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
26}
27
23a7718b 28require_once ($CFG->dirroot.'/course/moodleform_mod.php');
e24b7f85 29
f07b9627 30class mod_forum_mod_form extends moodleform_mod {
535e0ab6 31
2ee60b49 32 function definition() {
c18269c7 33 global $CFG, $COURSE, $DB;
535e0ab6 34
2ee60b49 35 $mform =& $this->_form;
535e0ab6 36
37//-------------------------------------------------------------------------------
38 $mform->addElement('header', 'general', get_string('general', 'form'));
39
26de8d35 40 $mform->addElement('text', 'name', get_string('forumname', 'forum'), array('size'=>'64'));
8eb1d25f 41 if (!empty($CFG->formatstringstriptags)) {
fbe9f668 42 $mform->setType('name', PARAM_TEXT);
8eb1d25f 43 } else {
b8ea3041 44 $mform->setType('name', PARAM_CLEANHTML);
8eb1d25f 45 }
2ee60b49 46 $mform->addRule('name', null, 'required', null, 'client');
b230cac1 47 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
535e0ab6 48
6398ff53 49 $this->standard_intro_elements(get_string('forumintro', 'forum'));
65bcf17b 50
f1b0908b 51 $forumtypes = forum_get_forum_types();
2f1e464a 52 core_collator::asort($forumtypes, core_collator::SORT_STRING);
fbe9f668 53 $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forumtypes);
530d2d72 54 $mform->addHelpButton('type', 'forumtype', 'forum');
2ee60b49 55 $mform->setDefault('type', 'general');
535e0ab6 56
fbe9f668
FM
57 // Attachments and word count.
58 $mform->addElement('header', 'attachmentswordcounthdr', get_string('attachmentswordcount', 'forum'));
535e0ab6 59
367b977d 60 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes, 0, $CFG->forum_maxbytes);
535e0ab6 61 $choices[1] = get_string('uploadnotallowed');
535e0ab6 62 $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
530d2d72 63 $mform->addHelpButton('maxbytes', 'maxattachmentsize', 'forum');
2ee60b49 64 $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
535e0ab6 65
7a98bbab
KW
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
81 );
30a9aff5 82 $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
530d2d72 83 $mform->addHelpButton('maxattachments', 'maxattachments', 'forum');
30a9aff5 84 $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
85
506522d5
FM
86 $mform->addElement('selectyesno', 'displaywordcount', get_string('displaywordcount', 'forum'));
87 $mform->addHelpButton('displaywordcount', 'displaywordcount', 'forum');
88 $mform->setDefault('displaywordcount', 0);
fbe9f668
FM
89
90 // Subscription and tracking.
91 $mform->addElement('header', 'subscriptionandtrackinghdr', get_string('subscriptionandtracking', 'forum'));
92
58affdc8 93 $options = forum_get_subscriptionmode_opions();
fbe9f668
FM
94 $mform->addElement('select', 'forcesubscribe', get_string('subscriptionmode', 'forum'), $options);
95 $mform->addHelpButton('forcesubscribe', 'subscriptionmode', 'forum');
58affdc8 96 $mform->setDefault('forcesubscribe', $CFG->forum_subscription);
fbe9f668
FM
97
98 $options = array();
99 $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
100 $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
20e6782f 101 if ($CFG->forum_allowforcedreadtracking) {
bd8f5d45 102 $options[FORUM_TRACKING_FORCED] = get_string('trackingon', 'forum');
20e6782f 103 }
fbe9f668
FM
104 $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
105 $mform->addHelpButton('trackingtype', 'trackingtype', 'forum');
20e6782f 106 $default = $CFG->forum_trackingtype;
bd8f5d45 107 if ((!$CFG->forum_allowforcedreadtracking) && ($default == FORUM_TRACKING_FORCED)) {
20e6782f
EM
108 $default = FORUM_TRACKING_OPTIONAL;
109 }
110 $mform->setDefault('trackingtype', $default);
506522d5 111
535e0ab6 112 if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
a02c77dc 113//-------------------------------------------------------------------------------
807cf3d0 114 $mform->addElement('header', 'rssheader', get_string('rss'));
535e0ab6 115 $choices = array();
116 $choices[0] = get_string('none');
117 $choices[1] = get_string('discussions', 'forum');
118 $choices[2] = get_string('posts', 'forum');
119 $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
530d2d72 120 $mform->addHelpButton('rsstype', 'rsstype', 'forum');
c9f2d347
RL
121 if (isset($CFG->forum_rsstype)) {
122 $mform->setDefault('rsstype', $CFG->forum_rsstype);
123 }
535e0ab6 124
125 $choices = array();
126 $choices[0] = '0';
127 $choices[1] = '1';
128 $choices[2] = '2';
129 $choices[3] = '3';
130 $choices[4] = '4';
131 $choices[5] = '5';
132 $choices[10] = '10';
133 $choices[15] = '15';
134 $choices[20] = '20';
135 $choices[25] = '25';
136 $choices[30] = '30';
137 $choices[40] = '40';
138 $choices[50] = '50';
139 $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
530d2d72 140 $mform->addHelpButton('rssarticles', 'rssarticles', 'forum');
cf626656 141 $mform->disabledIf('rssarticles', 'rsstype', 'eq', '0');
c9f2d347
RL
142 if (isset($CFG->forum_rssarticles)) {
143 $mform->setDefault('rssarticles', $CFG->forum_rssarticles);
144 }
535e0ab6 145 }
146
0f3bbfd4
AN
147 $mform->addElement('header', 'discussionlocking', get_string('discussionlockingheader', 'forum'));
148 $options = [
149 0 => get_string('discussionlockingdisabled', 'forum'),
150 1 * DAYSECS => get_string('numday', 'core', 1),
151 1 * WEEKSECS => get_string('numweek', 'core', 1),
152 2 * WEEKSECS => get_string('numweeks', 'core', 2),
153 30 * DAYSECS => get_string('nummonth', 'core', 1),
154 60 * DAYSECS => get_string('nummonths', 'core', 2),
155 90 * DAYSECS => get_string('nummonths', 'core', 3),
156 180 * DAYSECS => get_string('nummonths', 'core', 6),
157 1 * YEARSECS => get_string('numyear', 'core', 1),
158 ];
159 $mform->addElement('select', 'lockdiscussionafter', get_string('lockdiscussionafter', 'forum'), $options);
160 $mform->addHelpButton('lockdiscussionafter', 'lockdiscussionafter', 'forum');
161 $mform->disabledIf('lockdiscussionafter', 'type', 'eq', 'single');
162
a02c77dc 163//-------------------------------------------------------------------------------
807cf3d0 164 $mform->addElement('header', 'blockafterheader', get_string('blockafter', 'forum'));
2ee60b49 165 $options = array();
535e0ab6 166 $options[0] = get_string('blockperioddisabled','forum');
167 $options[60*60*24] = '1 '.get_string('day');
168 $options[60*60*24*2] = '2 '.get_string('days');
169 $options[60*60*24*3] = '3 '.get_string('days');
170 $options[60*60*24*4] = '4 '.get_string('days');
171 $options[60*60*24*5] = '5 '.get_string('days');
172 $options[60*60*24*6] = '6 '.get_string('days');
173 $options[60*60*24*7] = '1 '.get_string('week');
530d2d72
DM
174 $mform->addElement('select', 'blockperiod', get_string('blockperiod', 'forum'), $options);
175 $mform->addHelpButton('blockperiod', 'blockperiod', 'forum');
535e0ab6 176
2ee60b49 177 $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
178 $mform->setType('blockafter', PARAM_INT);
179 $mform->setDefault('blockafter', '0');
180 $mform->addRule('blockafter', null, 'numeric', null, 'client');
530d2d72 181 $mform->addHelpButton('blockafter', 'blockafter', 'forum');
2ee60b49 182 $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0);
9c03bbaa 183
9c03bbaa 184 $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
2ee60b49 185 $mform->setType('warnafter', PARAM_INT);
186 $mform->setDefault('warnafter', '0');
187 $mform->addRule('warnafter', null, 'numeric', null, 'client');
530d2d72 188 $mform->addHelpButton('warnafter', 'warnafter', 'forum');
2ee60b49 189 $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0);
9c03bbaa 190
103e7cba 191 $coursecontext = context_course::instance($COURSE->id);
67fbfe8b 192 plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_forum');
103e7cba 193
a02c77dc 194//-------------------------------------------------------------------------------
16306628
AD
195
196 $this->standard_grading_coursemodule_elements();
197
42f103be 198 $this->standard_coursemodule_elements();
a23f0aaf 199//-------------------------------------------------------------------------------
2ee60b49 200// buttons
a23f0aaf 201 $this->add_action_buttons();
535e0ab6 202
2ee60b49 203 }
535e0ab6 204
4ba2221a 205 function definition_after_data() {
4050572d 206 parent::definition_after_data();
207 $mform =& $this->_form;
208 $type =& $mform->getElement('type');
209 $typevalue = $mform->getElementValue('type');
210
d24206d6 211 //we don't want to have these appear as possible selections in the form but
212 //we want the form to display them if they are set.
4ba2221a 213 if ($typevalue[0]=='news') {
d24206d6 214 $type->addOption(get_string('namenews', 'forum'), 'news');
530d2d72 215 $mform->addHelpButton('type', 'namenews', 'forum');
d24206d6 216 $type->freeze();
217 $type->setPersistantFreeze(true);
218 }
4ba2221a 219 if ($typevalue[0]=='social') {
d24206d6 220 $type->addOption(get_string('namesocial', 'forum'), 'social');
535e0ab6 221 $type->freeze();
222 $type->setPersistantFreeze(true);
223 }
02f8f37d 224
2ee60b49 225 }
535e0ab6 226
4ba2221a 227 function data_preprocessing(&$default_values) {
55d95d90
AD
228 parent::data_preprocessing($default_values);
229
4e781c7b 230 // Set up the completion checkboxes which aren't part of standard data.
231 // We also make the default value (if you turn on the checkbox) for those
232 // numbers to be 1, this will not apply unless checkbox is ticked.
233 $default_values['completiondiscussionsenabled']=
234 !empty($default_values['completiondiscussions']) ? 1 : 0;
4ba2221a 235 if (empty($default_values['completiondiscussions'])) {
4e781c7b 236 $default_values['completiondiscussions']=1;
237 }
238 $default_values['completionrepliesenabled']=
239 !empty($default_values['completionreplies']) ? 1 : 0;
4ba2221a 240 if (empty($default_values['completionreplies'])) {
4e781c7b 241 $default_values['completionreplies']=1;
242 }
e44d71a6
JP
243 // Tick by default if Add mode or if completion posts settings is set to 1 or more.
244 if (empty($this->_instance) || !empty($default_values['completionposts'])) {
245 $default_values['completionpostsenabled'] = 1;
246 } else {
247 $default_values['completionpostsenabled'] = 0;
248 }
4ba2221a 249 if (empty($default_values['completionposts'])) {
4e781c7b 250 $default_values['completionposts']=1;
251 }
2ee60b49 252 }
e24b7f85 253
197b6d9e
JP
254 /**
255 * Add custom completion rules.
256 *
257 * @return array Array of string IDs of added items, empty array if none
258 */
259 public function add_completion_rules() {
4e781c7b 260 $mform =& $this->_form;
261
262 $group=array();
263 $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '', get_string('completionposts','forum'));
264 $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
265 $mform->setType('completionposts',PARAM_INT);
266 $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup','forum'), array(' '), false);
4e781c7b 267 $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
268
269 $group=array();
270 $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '', get_string('completiondiscussions','forum'));
271 $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
272 $mform->setType('completiondiscussions',PARAM_INT);
273 $mform->addGroup($group, 'completiondiscussionsgroup', get_string('completiondiscussionsgroup','forum'), array(' '), false);
4e781c7b 274 $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
275
276 $group=array();
277 $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
278 $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
279 $mform->setType('completionreplies',PARAM_INT);
280 $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
4e781c7b 281 $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
282
283 return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
284 }
285
286 function completion_rule_enabled($data) {
287 return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
288 (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
289 (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
290 }
291
06cdda46
MG
292 /**
293 * Allows module to modify the data returned by form get_data().
294 * This method is also called in the bulk activity completion form.
295 *
296 * Only available on moodleform_mod.
297 *
273d3106 298 * @param stdClass $data the form data to be modified.
06cdda46 299 */
273d3106 300 public function data_postprocessing($data) {
06cdda46 301 parent::data_postprocessing($data);
4e781c7b 302 // Turn off completion settings if the checkboxes aren't ticked
a9f5fc15 303 if (!empty($data->completionunlocked)) {
304 $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
305 if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
306 $data->completiondiscussions = 0;
307 }
308 if (empty($data->completionrepliesenabled) || !$autocompletion) {
309 $data->completionreplies = 0;
310 }
311 if (empty($data->completionpostsenabled) || !$autocompletion) {
312 $data->completionposts = 0;
313 }
4e781c7b 314 }
4e781c7b 315 }
535e0ab6 316}
1adbd2c3 317