MDL-38596 Added caching to the list of course contacts
[moodle.git] / mod / forum / mod_form.php
blob4403004daad48fdf3432dd5a9e05233b3411bffb
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->add_intro_editor(true, get_string('forumintro', 'forum'));
51 $forumtypes = forum_get_forum_types();
52 collatorlib::asort($forumtypes, collatorlib::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(0,1,2,3,4,5,6,7,8,9,10,20,50,100);
67 $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
68 $mform->addHelpButton('maxattachments', 'maxattachments', 'forum');
69 $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
71 $mform->addElement('selectyesno', 'displaywordcount', get_string('displaywordcount', 'forum'));
72 $mform->addHelpButton('displaywordcount', 'displaywordcount', 'forum');
73 $mform->setDefault('displaywordcount', 0);
75 // Subscription and tracking.
76 $mform->addElement('header', 'subscriptionandtrackinghdr', get_string('subscriptionandtracking', 'forum'));
78 $options = array();
79 $options[FORUM_CHOOSESUBSCRIBE] = get_string('subscriptionoptional', 'forum');
80 $options[FORUM_FORCESUBSCRIBE] = get_string('subscriptionforced', 'forum');
81 $options[FORUM_INITIALSUBSCRIBE] = get_string('subscriptionauto', 'forum');
82 $options[FORUM_DISALLOWSUBSCRIBE] = get_string('subscriptiondisabled','forum');
83 $mform->addElement('select', 'forcesubscribe', get_string('subscriptionmode', 'forum'), $options);
84 $mform->addHelpButton('forcesubscribe', 'subscriptionmode', 'forum');
86 $options = array();
87 $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
88 $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
89 $options[FORUM_TRACKING_ON] = get_string('trackingon', 'forum');
90 $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
91 $mform->addHelpButton('trackingtype', 'trackingtype', 'forum');
93 if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
94 //-------------------------------------------------------------------------------
95 $mform->addElement('header', 'rssheader', get_string('rss'));
96 $choices = array();
97 $choices[0] = get_string('none');
98 $choices[1] = get_string('discussions', 'forum');
99 $choices[2] = get_string('posts', 'forum');
100 $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
101 $mform->addHelpButton('rsstype', 'rsstype', 'forum');
103 $choices = array();
104 $choices[0] = '0';
105 $choices[1] = '1';
106 $choices[2] = '2';
107 $choices[3] = '3';
108 $choices[4] = '4';
109 $choices[5] = '5';
110 $choices[10] = '10';
111 $choices[15] = '15';
112 $choices[20] = '20';
113 $choices[25] = '25';
114 $choices[30] = '30';
115 $choices[40] = '40';
116 $choices[50] = '50';
117 $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
118 $mform->addHelpButton('rssarticles', 'rssarticles', 'forum');
119 $mform->disabledIf('rssarticles', 'rsstype', 'eq', '0');
122 //-------------------------------------------------------------------------------
123 $mform->addElement('header', 'blockafterheader', get_string('blockafter', 'forum'));
124 $options = array();
125 $options[0] = get_string('blockperioddisabled','forum');
126 $options[60*60*24] = '1 '.get_string('day');
127 $options[60*60*24*2] = '2 '.get_string('days');
128 $options[60*60*24*3] = '3 '.get_string('days');
129 $options[60*60*24*4] = '4 '.get_string('days');
130 $options[60*60*24*5] = '5 '.get_string('days');
131 $options[60*60*24*6] = '6 '.get_string('days');
132 $options[60*60*24*7] = '1 '.get_string('week');
133 $mform->addElement('select', 'blockperiod', get_string('blockperiod', 'forum'), $options);
134 $mform->addHelpButton('blockperiod', 'blockperiod', 'forum');
136 $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
137 $mform->setType('blockafter', PARAM_INT);
138 $mform->setDefault('blockafter', '0');
139 $mform->addRule('blockafter', null, 'numeric', null, 'client');
140 $mform->addHelpButton('blockafter', 'blockafter', 'forum');
141 $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0);
143 $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
144 $mform->setType('warnafter', PARAM_INT);
145 $mform->setDefault('warnafter', '0');
146 $mform->addRule('warnafter', null, 'numeric', null, 'client');
147 $mform->addHelpButton('warnafter', 'warnafter', 'forum');
148 $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0);
150 $coursecontext = context_course::instance($COURSE->id);
151 plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_forum');
153 //-------------------------------------------------------------------------------
155 $this->standard_grading_coursemodule_elements();
157 $this->standard_coursemodule_elements();
158 //-------------------------------------------------------------------------------
159 // buttons
160 $this->add_action_buttons();
164 function definition_after_data() {
165 parent::definition_after_data();
166 $mform =& $this->_form;
167 $type =& $mform->getElement('type');
168 $typevalue = $mform->getElementValue('type');
170 //we don't want to have these appear as possible selections in the form but
171 //we want the form to display them if they are set.
172 if ($typevalue[0]=='news') {
173 $type->addOption(get_string('namenews', 'forum'), 'news');
174 $mform->addHelpButton('type', 'namenews', 'forum');
175 $type->freeze();
176 $type->setPersistantFreeze(true);
178 if ($typevalue[0]=='social') {
179 $type->addOption(get_string('namesocial', 'forum'), 'social');
180 $type->freeze();
181 $type->setPersistantFreeze(true);
186 function data_preprocessing(&$default_values) {
187 parent::data_preprocessing($default_values);
189 // Set up the completion checkboxes which aren't part of standard data.
190 // We also make the default value (if you turn on the checkbox) for those
191 // numbers to be 1, this will not apply unless checkbox is ticked.
192 $default_values['completiondiscussionsenabled']=
193 !empty($default_values['completiondiscussions']) ? 1 : 0;
194 if (empty($default_values['completiondiscussions'])) {
195 $default_values['completiondiscussions']=1;
197 $default_values['completionrepliesenabled']=
198 !empty($default_values['completionreplies']) ? 1 : 0;
199 if (empty($default_values['completionreplies'])) {
200 $default_values['completionreplies']=1;
202 $default_values['completionpostsenabled']=
203 !empty($default_values['completionposts']) ? 1 : 0;
204 if (empty($default_values['completionposts'])) {
205 $default_values['completionposts']=1;
209 function add_completion_rules() {
210 $mform =& $this->_form;
212 $group=array();
213 $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '', get_string('completionposts','forum'));
214 $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
215 $mform->setType('completionposts',PARAM_INT);
216 $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup','forum'), array(' '), false);
217 $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
219 $group=array();
220 $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '', get_string('completiondiscussions','forum'));
221 $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
222 $mform->setType('completiondiscussions',PARAM_INT);
223 $mform->addGroup($group, 'completiondiscussionsgroup', get_string('completiondiscussionsgroup','forum'), array(' '), false);
224 $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
226 $group=array();
227 $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
228 $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
229 $mform->setType('completionreplies',PARAM_INT);
230 $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
231 $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
233 return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
236 function completion_rule_enabled($data) {
237 return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
238 (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
239 (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
242 function get_data() {
243 $data = parent::get_data();
244 if (!$data) {
245 return false;
247 // Turn off completion settings if the checkboxes aren't ticked
248 $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
249 if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
250 $data->completiondiscussions = 0;
252 if (empty($data->completionrepliesenabled) || !$autocompletion) {
253 $data->completionreplies = 0;
255 if (empty($data->completionpostsenabled) || !$autocompletion) {
256 $data->completionposts = 0;
258 return $data;