Merge branch 'wip-MDL-39294-m25' of git://github.com/samhemelryk/moodle
[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/**
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
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)) {
4e781c7b 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
65bcf17b 49 $forum_types = forum_get_forum_types();
50
51 asort($forum_types);
52 $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forum_types);
530d2d72 53 $mform->addHelpButton('type', 'forumtype', 'forum');
2ee60b49 54 $mform->setDefault('type', 'general');
535e0ab6 55
4ba2221a 56 $this->add_intro_editor(true, get_string('forumintro', 'forum'));
b2a9505b 57
535e0ab6 58 $options = array();
afef965e
SH
59 $options[FORUM_CHOOSESUBSCRIBE] = get_string('subscriptionoptional', 'forum');
60 $options[FORUM_FORCESUBSCRIBE] = get_string('subscriptionforced', 'forum');
61 $options[FORUM_INITIALSUBSCRIBE] = get_string('subscriptionauto', 'forum');
62 $options[FORUM_DISALLOWSUBSCRIBE] = get_string('subscriptiondisabled','forum');
63 $mform->addElement('select', 'forcesubscribe', get_string('subscriptionmode', 'forum'), $options);
530d2d72 64 $mform->addHelpButton('forcesubscribe', 'subscriptionmode', 'forum');
535e0ab6 65
66 $options = array();
67 $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
68 $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
69 $options[FORUM_TRACKING_ON] = get_string('trackingon', 'forum');
70 $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
530d2d72 71 $mform->addHelpButton('trackingtype', 'trackingtype', 'forum');
535e0ab6 72
a02c77dc 73 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
535e0ab6 74 $choices[1] = get_string('uploadnotallowed');
535e0ab6 75 $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
530d2d72 76 $mform->addHelpButton('maxbytes', 'maxattachmentsize', 'forum');
2ee60b49 77 $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
535e0ab6 78
3db7b878 79 $choices = array(0,1,2,3,4,5,6,7,8,9,10,20,50,100);
30a9aff5 80 $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
530d2d72 81 $mform->addHelpButton('maxattachments', 'maxattachments', 'forum');
30a9aff5 82 $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
83
506522d5
FM
84 $mform->addElement('selectyesno', 'displaywordcount', get_string('displaywordcount', 'forum'));
85 $mform->addHelpButton('displaywordcount', 'displaywordcount', 'forum');
86 $mform->setDefault('displaywordcount', 0);
87 $mform->setAdvanced('displaywordcount');
88
535e0ab6 89 if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
a02c77dc 90//-------------------------------------------------------------------------------
807cf3d0 91 $mform->addElement('header', 'rssheader', get_string('rss'));
535e0ab6 92 $choices = array();
93 $choices[0] = get_string('none');
94 $choices[1] = get_string('discussions', 'forum');
95 $choices[2] = get_string('posts', 'forum');
96 $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
530d2d72 97 $mform->addHelpButton('rsstype', 'rsstype', 'forum');
535e0ab6 98
99 $choices = array();
100 $choices[0] = '0';
101 $choices[1] = '1';
102 $choices[2] = '2';
103 $choices[3] = '3';
104 $choices[4] = '4';
105 $choices[5] = '5';
106 $choices[10] = '10';
107 $choices[15] = '15';
108 $choices[20] = '20';
109 $choices[25] = '25';
110 $choices[30] = '30';
111 $choices[40] = '40';
112 $choices[50] = '50';
113 $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
530d2d72 114 $mform->addHelpButton('rssarticles', 'rssarticles', 'forum');
cf626656 115 $mform->disabledIf('rssarticles', 'rsstype', 'eq', '0');
535e0ab6 116 }
117
a02c77dc 118//-------------------------------------------------------------------------------
807cf3d0 119 $mform->addElement('header', 'blockafterheader', get_string('blockafter', 'forum'));
2ee60b49 120 $options = array();
535e0ab6 121 $options[0] = get_string('blockperioddisabled','forum');
122 $options[60*60*24] = '1 '.get_string('day');
123 $options[60*60*24*2] = '2 '.get_string('days');
124 $options[60*60*24*3] = '3 '.get_string('days');
125 $options[60*60*24*4] = '4 '.get_string('days');
126 $options[60*60*24*5] = '5 '.get_string('days');
127 $options[60*60*24*6] = '6 '.get_string('days');
128 $options[60*60*24*7] = '1 '.get_string('week');
530d2d72
DM
129 $mform->addElement('select', 'blockperiod', get_string('blockperiod', 'forum'), $options);
130 $mform->addHelpButton('blockperiod', 'blockperiod', 'forum');
535e0ab6 131
2ee60b49 132 $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
133 $mform->setType('blockafter', PARAM_INT);
134 $mform->setDefault('blockafter', '0');
135 $mform->addRule('blockafter', null, 'numeric', null, 'client');
530d2d72 136 $mform->addHelpButton('blockafter', 'blockafter', 'forum');
2ee60b49 137 $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0);
9c03bbaa 138
139
140 $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
2ee60b49 141 $mform->setType('warnafter', PARAM_INT);
142 $mform->setDefault('warnafter', '0');
143 $mform->addRule('warnafter', null, 'numeric', null, 'client');
530d2d72 144 $mform->addHelpButton('warnafter', 'warnafter', 'forum');
2ee60b49 145 $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0);
9c03bbaa 146
103e7cba 147 $coursecontext = context_course::instance($COURSE->id);
67fbfe8b 148 plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_forum');
103e7cba 149
a02c77dc 150//-------------------------------------------------------------------------------
16306628
AD
151
152 $this->standard_grading_coursemodule_elements();
153
42f103be 154 $this->standard_coursemodule_elements();
a23f0aaf 155//-------------------------------------------------------------------------------
2ee60b49 156// buttons
a23f0aaf 157 $this->add_action_buttons();
535e0ab6 158
2ee60b49 159 }
535e0ab6 160
4ba2221a 161 function definition_after_data() {
4050572d 162 parent::definition_after_data();
163 $mform =& $this->_form;
164 $type =& $mform->getElement('type');
165 $typevalue = $mform->getElementValue('type');
166
d24206d6 167 //we don't want to have these appear as possible selections in the form but
168 //we want the form to display them if they are set.
4ba2221a 169 if ($typevalue[0]=='news') {
d24206d6 170 $type->addOption(get_string('namenews', 'forum'), 'news');
530d2d72 171 $mform->addHelpButton('type', 'namenews', 'forum');
d24206d6 172 $type->freeze();
173 $type->setPersistantFreeze(true);
174 }
4ba2221a 175 if ($typevalue[0]=='social') {
d24206d6 176 $type->addOption(get_string('namesocial', 'forum'), 'social');
535e0ab6 177 $type->freeze();
178 $type->setPersistantFreeze(true);
179 }
02f8f37d 180
2ee60b49 181 }
535e0ab6 182
4ba2221a 183 function data_preprocessing(&$default_values) {
55d95d90
AD
184 parent::data_preprocessing($default_values);
185
4e781c7b 186 // Set up the completion checkboxes which aren't part of standard data.
187 // We also make the default value (if you turn on the checkbox) for those
188 // numbers to be 1, this will not apply unless checkbox is ticked.
189 $default_values['completiondiscussionsenabled']=
190 !empty($default_values['completiondiscussions']) ? 1 : 0;
4ba2221a 191 if (empty($default_values['completiondiscussions'])) {
4e781c7b 192 $default_values['completiondiscussions']=1;
193 }
194 $default_values['completionrepliesenabled']=
195 !empty($default_values['completionreplies']) ? 1 : 0;
4ba2221a 196 if (empty($default_values['completionreplies'])) {
4e781c7b 197 $default_values['completionreplies']=1;
198 }
199 $default_values['completionpostsenabled']=
200 !empty($default_values['completionposts']) ? 1 : 0;
4ba2221a 201 if (empty($default_values['completionposts'])) {
4e781c7b 202 $default_values['completionposts']=1;
203 }
2ee60b49 204 }
e24b7f85 205
4454447d 206 function add_completion_rules() {
4e781c7b 207 $mform =& $this->_form;
208
209 $group=array();
210 $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '', get_string('completionposts','forum'));
211 $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
212 $mform->setType('completionposts',PARAM_INT);
213 $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup','forum'), array(' '), false);
4e781c7b 214 $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
215
216 $group=array();
217 $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '', get_string('completiondiscussions','forum'));
218 $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
219 $mform->setType('completiondiscussions',PARAM_INT);
220 $mform->addGroup($group, 'completiondiscussionsgroup', get_string('completiondiscussionsgroup','forum'), array(' '), false);
4e781c7b 221 $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
222
223 $group=array();
224 $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
225 $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
226 $mform->setType('completionreplies',PARAM_INT);
227 $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
4e781c7b 228 $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
229
230 return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
231 }
232
233 function completion_rule_enabled($data) {
234 return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
235 (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
236 (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
237 }
238
49f6e5f4 239 function get_data() {
4ba2221a 240 $data = parent::get_data();
241 if (!$data) {
4e781c7b 242 return false;
243 }
244 // Turn off completion settings if the checkboxes aren't ticked
4ba2221a 245 $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
246 if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
247 $data->completiondiscussions = 0;
4e781c7b 248 }
4ba2221a 249 if (empty($data->completionrepliesenabled) || !$autocompletion) {
250 $data->completionreplies = 0;
4e781c7b 251 }
4ba2221a 252 if (empty($data->completionpostsenabled) || !$autocompletion) {
253 $data->completionposts = 0;
4e781c7b 254 }
255 return $data;
256 }
535e0ab6 257}
1adbd2c3 258