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 $forum_types = forum_get_forum_types();
52 $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forum_types);
53 $mform->addHelpButton('type', 'forumtype', 'forum');
54 $mform->setDefault('type', 'general');
56 $this->add_intro_editor(true, get_string('forumintro', 'forum'));
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);
64 $mform->addHelpButton('forcesubscribe', 'subscriptionmode', 'forum');
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);
71 $mform->addHelpButton('trackingtype', 'trackingtype', 'forum');
73 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
74 $choices[1] = get_string('uploadnotallowed');
75 $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
76 $mform->addHelpButton('maxbytes', 'maxattachmentsize', 'forum');
77 $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
79 $choices = array(0,1,2,3,4,5,6,7,8,9,10,20,50,100);
80 $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
81 $mform->addHelpButton('maxattachments', 'maxattachments', 'forum');
82 $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
84 $mform->addElement('selectyesno', 'displaywordcount', get_string('displaywordcount', 'forum'));
85 $mform->addHelpButton('displaywordcount', 'displaywordcount', 'forum');
86 $mform->setDefault('displaywordcount', 0);
87 $mform->setAdvanced('displaywordcount');
89 if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
90 //-------------------------------------------------------------------------------
91 $mform->addElement('header', '', get_string('rss'));
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);
97 $mform->addHelpButton('rsstype', 'rsstype', 'forum');
113 $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
114 $mform->addHelpButton('rssarticles', 'rssarticles', 'forum');
117 //-------------------------------------------------------------------------------
118 $mform->addElement('header', '', get_string('blockafter', 'forum'));
120 $options[0] = get_string('blockperioddisabled','forum');
121 $options[60*60*24] = '1 '.get_string('day');
122 $options[60*60*24*2] = '2 '.get_string('days');
123 $options[60*60*24*3] = '3 '.get_string('days');
124 $options[60*60*24*4] = '4 '.get_string('days');
125 $options[60*60*24*5] = '5 '.get_string('days');
126 $options[60*60*24*6] = '6 '.get_string('days');
127 $options[60*60*24*7] = '1 '.get_string('week');
128 $mform->addElement('select', 'blockperiod', get_string('blockperiod', 'forum'), $options);
129 $mform->addHelpButton('blockperiod', 'blockperiod', 'forum');
131 $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
132 $mform->setType('blockafter', PARAM_INT);
133 $mform->setDefault('blockafter', '0');
134 $mform->addRule('blockafter', null, 'numeric', null, 'client');
135 $mform->addHelpButton('blockafter', 'blockafter', 'forum');
136 $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0);
139 $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
140 $mform->setType('warnafter', PARAM_INT);
141 $mform->setDefault('warnafter', '0');
142 $mform->addRule('warnafter', null, 'numeric', null, 'client');
143 $mform->addHelpButton('warnafter', 'warnafter', 'forum');
144 $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0);
146 $coursecontext = context_course::instance($COURSE->id);
147 plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_forum');
149 //-------------------------------------------------------------------------------
151 $this->standard_grading_coursemodule_elements();
153 $this->standard_coursemodule_elements();
154 //-------------------------------------------------------------------------------
156 $this->add_action_buttons();
160 function definition_after_data() {
161 parent::definition_after_data();
162 $mform =& $this->_form;
163 $type =& $mform->getElement('type');
164 $typevalue = $mform->getElementValue('type');
166 //we don't want to have these appear as possible selections in the form but
167 //we want the form to display them if they are set.
168 if ($typevalue[0]=='news') {
169 $type->addOption(get_string('namenews', 'forum'), 'news');
170 $mform->addHelpButton('type', 'namenews', 'forum');
172 $type->setPersistantFreeze(true);
174 if ($typevalue[0]=='social') {
175 $type->addOption(get_string('namesocial', 'forum'), 'social');
177 $type->setPersistantFreeze(true);
182 function data_preprocessing(&$default_values) {
183 parent::data_preprocessing($default_values);
185 // Set up the completion checkboxes which aren't part of standard data.
186 // We also make the default value (if you turn on the checkbox) for those
187 // numbers to be 1, this will not apply unless checkbox is ticked.
188 $default_values['completiondiscussionsenabled']=
189 !empty($default_values['completiondiscussions']) ? 1 : 0;
190 if (empty($default_values['completiondiscussions'])) {
191 $default_values['completiondiscussions']=1;
193 $default_values['completionrepliesenabled']=
194 !empty($default_values['completionreplies']) ? 1 : 0;
195 if (empty($default_values['completionreplies'])) {
196 $default_values['completionreplies']=1;
198 $default_values['completionpostsenabled']=
199 !empty($default_values['completionposts']) ? 1 : 0;
200 if (empty($default_values['completionposts'])) {
201 $default_values['completionposts']=1;
205 function add_completion_rules() {
206 $mform =& $this->_form;
209 $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '', get_string('completionposts','forum'));
210 $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
211 $mform->setType('completionposts',PARAM_INT);
212 $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup','forum'), array(' '), false);
213 $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
216 $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '', get_string('completiondiscussions','forum'));
217 $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
218 $mform->setType('completiondiscussions',PARAM_INT);
219 $mform->addGroup($group, 'completiondiscussionsgroup', get_string('completiondiscussionsgroup','forum'), array(' '), false);
220 $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
223 $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
224 $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
225 $mform->setType('completionreplies',PARAM_INT);
226 $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
227 $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
229 return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
232 function completion_rule_enabled($data) {
233 return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
234 (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
235 (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
238 function get_data() {
239 $data = parent::get_data();
243 // Turn off completion settings if the checkboxes aren't ticked
244 $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
245 if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
246 $data->completiondiscussions = 0;
248 if (empty($data->completionrepliesenabled) || !$autocompletion) {
249 $data->completionreplies = 0;
251 if (empty($data->completionpostsenabled) || !$autocompletion) {
252 $data->completionposts = 0;