MDL-18910 moving modedit features to modname_supports()
[moodle.git] / mod / forum / mod_form.php
1 <?php
2 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
4 class mod_forum_mod_form extends moodleform_mod {
6     function definition() {
7         global $CFG, $COURSE, $DB;
9         $mform    =& $this->_form;
11 //-------------------------------------------------------------------------------
12         $mform->addElement('header', 'general', get_string('general', 'form'));
14         $mform->addElement('text', 'name', get_string('forumname', 'forum'), array('size'=>'64'));
15         if (!empty($CFG->formatstringstriptags)) {
16         $mform->setType('name', PARAM_TEXT);
17         } else {
18             $mform->setType('name', PARAM_CLEAN);
19         }
20         $mform->addRule('name', null, 'required', null, 'client');
21         $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
23         $forum_types = forum_get_forum_types();
25         asort($forum_types);
26         $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forum_types);
27         $mform->setHelpButton('type', array('forumtype', get_string('forumtype', 'forum'), 'forum'));
28         $mform->setDefault('type', 'general');
30         $mform->addElement('htmleditor', 'intro', get_string('forumintro', 'forum'));
31         $mform->setType('intro', PARAM_RAW);
32         $mform->addRule('intro', get_string('required'), 'required', null, 'client');
33         $mform->setHelpButton('intro', array('writing', 'questions', 'richtext2'), false, 'editorhelpbutton');
35         $mform->addElement('format', 'introformat', get_string('format'));
37         $options = array();
38         $options[0] = get_string('no');
39         $options[1] = get_string('yesforever', 'forum');
40         $options[FORUM_INITIALSUBSCRIBE] = get_string('yesinitially', 'forum');
41         $options[FORUM_DISALLOWSUBSCRIBE] = get_string('disallowsubscribe','forum');
42         $mform->addElement('select', 'forcesubscribe', get_string('forcesubscribeq', 'forum'), $options);
43         $mform->setHelpButton('forcesubscribe', array('subscription2', get_string('forcesubscribeq', 'forum'), 'forum'));
45         $options = array();
46         $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
47         $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
48         $options[FORUM_TRACKING_ON] = get_string('trackingon', 'forum');
49         $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
50         $mform->setHelpButton('trackingtype', array('trackingtype', get_string('trackingtype', 'forum'), 'forum'));
52         $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
53         $choices[1] = get_string('uploadnotallowed');
54         $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
55         $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
56         $mform->setHelpButton('maxbytes', array('maxattachmentsize', get_string('maxattachmentsize', 'forum'), 'forum'));
57         $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
59         $choices = array(0,1,2,3,4,5,6,7,8,9);
60         $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
61         $mform->setHelpButton('maxattachments', array('maxattachments', get_string('maxattachments', 'forum'), 'forum'));
62         $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
64         if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
65 //-------------------------------------------------------------------------------
66             $mform->addElement('header', '', get_string('rss'));
67             $choices = array();
68             $choices[0] = get_string('none');
69             $choices[1] = get_string('discussions', 'forum');
70             $choices[2] = get_string('posts', 'forum');
71             $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
72             $mform->setHelpButton('rsstype', array('rsstype', get_string('rsstype'), 'forum'));
74             $choices = array();
75             $choices[0] = '0';
76             $choices[1] = '1';
77             $choices[2] = '2';
78             $choices[3] = '3';
79             $choices[4] = '4';
80             $choices[5] = '5';
81             $choices[10] = '10';
82             $choices[15] = '15';
83             $choices[20] = '20';
84             $choices[25] = '25';
85             $choices[30] = '30';
86             $choices[40] = '40';
87             $choices[50] = '50';
88             $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
89             $mform->setHelpButton('rssarticles', array('rssarticles', get_string('rssarticles'), 'forum'));
90         }
92 //-------------------------------------------------------------------------------
93         $mform->addElement('header', '', get_string('grade'));
95         $mform->addElement('select', 'assessed', get_string('aggregatetype', 'forum') , forum_get_aggregate_types());
96         $mform->setDefault('assessed', 0);
97         $mform->setHelpButton('assessed', array('assessaggregate', get_string('aggregatetype', 'forum'), 'forum'));
99         $mform->addElement('modgrade', 'scale', get_string('grade'), false);
100         $mform->disabledIf('scale', 'assessed', 'eq', 0);
102         $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'forum'));
103         $mform->disabledIf('ratingtime', 'assessed', 'eq', 0);
105         $mform->addElement('date_time_selector', 'assesstimestart', get_string('from'));
106         $mform->disabledIf('assesstimestart', 'assessed', 'eq', 0);
107         $mform->disabledIf('assesstimestart', 'ratingtime');
109         $mform->addElement('date_time_selector', 'assesstimefinish', get_string('to'));
110         $mform->disabledIf('assesstimefinish', 'assessed', 'eq', 0);
111         $mform->disabledIf('assesstimefinish', 'ratingtime');
114 //-------------------------------------------------------------------------------
115         $mform->addElement('header', '', get_string('blockafter', 'forum'));
116         $options = array();
117         $options[0] = get_string('blockperioddisabled','forum');
118         $options[60*60*24]   = '1 '.get_string('day');
119         $options[60*60*24*2] = '2 '.get_string('days');
120         $options[60*60*24*3] = '3 '.get_string('days');
121         $options[60*60*24*4] = '4 '.get_string('days');
122         $options[60*60*24*5] = '5 '.get_string('days');
123         $options[60*60*24*6] = '6 '.get_string('days');
124         $options[60*60*24*7] = '1 '.get_string('week');
125         $mform->addElement('select', 'blockperiod', get_string("blockperiod", "forum") , $options);
126         $mform->setHelpButton('blockperiod', array('manageposts', get_string('blockperiod', 'forum'),'forum'));
128         $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
129         $mform->setType('blockafter', PARAM_INT);
130         $mform->setDefault('blockafter', '0');
131         $mform->addRule('blockafter', null, 'numeric', null, 'client');
132         $mform->setHelpButton('blockafter', array('manageposts', get_string('blockafter', 'forum'),'forum'));
133         $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0);
136         $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
137         $mform->setType('warnafter', PARAM_INT);
138         $mform->setDefault('warnafter', '0');
139         $mform->addRule('warnafter', null, 'numeric', null, 'client');
140         $mform->setHelpButton('warnafter', array('manageposts', get_string('warnafter', 'forum'),'forum'));
141         $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0);
143 //-------------------------------------------------------------------------------
144         $this->standard_coursemodule_elements();
145 //-------------------------------------------------------------------------------
146 // buttons
147         $this->add_action_buttons();
149     }
151     function definition_after_data(){
152         parent::definition_after_data();
153         $mform     =& $this->_form;
154         $type      =& $mform->getElement('type');
155         $typevalue = $mform->getElementValue('type');
157         //we don't want to have these appear as possible selections in the form but
158         //we want the form to display them if they are set.
159         if ($typevalue[0]=='news'){
160             $type->addOption(get_string('namenews', 'forum'), 'news');
161             $type->setHelpButton(array('forumtypenews', get_string('forumtype', 'forum'), 'forum'));
162             $type->freeze();
163             $type->setPersistantFreeze(true);
164         }
165         if ($typevalue[0]=='social'){
166             $type->addOption(get_string('namesocial', 'forum'), 'social');
167             $type->freeze();
168             $type->setPersistantFreeze(true);
169         }
171     }
173     function data_preprocessing(&$default_values){
174         if (empty($default_values['scale'])){
175             $default_values['assessed'] = 0;
176         }
178         if (empty($default_values['assessed'])){
179             $default_values['ratingtime'] = 0;
180         } else {
181             $default_values['ratingtime']=
182                 ($default_values['assesstimestart'] && $default_values['assesstimefinish']) ? 1 : 0;
183         }
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;
192         }
193         $default_values['completionrepliesenabled']=
194             !empty($default_values['completionreplies']) ? 1 : 0;
195         if(empty($default_values['completionreplies'])) {
196             $default_values['completionreplies']=1;
197         }
198         $default_values['completionpostsenabled']=
199             !empty($default_values['completionposts']) ? 1 : 0;
200         if(empty($default_values['completionposts'])) {
201             $default_values['completionposts']=1;
202         }
203     }
205           function add_completion_rules() {
206         $mform =& $this->_form;
208         $group=array();
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->setHelpButton('completionpostsgroup', array('completion', get_string('completionpostshelp', 'forum'), 'forum'));
214         $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
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);
221         $mform->setHelpButton('completiondiscussionsgroup', array('completion', get_string('completiondiscussionshelp', 'forum'), 'forum'));
222         $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
224         $group=array();
225         $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
226         $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
227         $mform->setType('completionreplies',PARAM_INT);
228         $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
229         $mform->setHelpButton('completionrepliesgroup', array('completion', get_string('completionreplieshelp', 'forum'), 'forum'));
230         $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
232         return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
233     }
235     function completion_rule_enabled($data) {
236         return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
237             (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
238             (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
239     }
241     function get_data() {
242         $data=parent::get_data();
243         if(!$data) {
244             return false;
245         }
246         // Turn off completion settings if the checkboxes aren't ticked
247         $autocompletion=!empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
248         if(empty($data->completiondiscussionsenabled) || !$autocompletion) {
249             $data->completiondiscussions=0;
250         }
251         if(empty($data->completionrepliesenabled) || !$autocompletion) {
252             $data->completionreplies=0;
253         }
254         if(empty($data->completionpostsenabled) || !$autocompletion) {
255             $data->completionposts=0;
256         }
257         return $data;
258     }
260 ?>