MDL-18910 full support for module intro editor with embedded images
[moodle.git] / mod / forum / mod_form.php
CommitLineData
535e0ab6 1<?php
23a7718b 2require_once ($CFG->dirroot.'/course/moodleform_mod.php');
e24b7f85 3
f07b9627 4class mod_forum_mod_form extends moodleform_mod {
535e0ab6 5
2ee60b49 6 function definition() {
c18269c7 7 global $CFG, $COURSE, $DB;
535e0ab6 8
2ee60b49 9 $mform =& $this->_form;
535e0ab6 10
11//-------------------------------------------------------------------------------
12 $mform->addElement('header', 'general', get_string('general', 'form'));
13
26de8d35 14 $mform->addElement('text', 'name', get_string('forumname', 'forum'), array('size'=>'64'));
8eb1d25f 15 if (!empty($CFG->formatstringstriptags)) {
4e781c7b 16 $mform->setType('name', PARAM_TEXT);
8eb1d25f 17 } else {
18 $mform->setType('name', PARAM_CLEAN);
19 }
2ee60b49 20 $mform->addRule('name', null, 'required', null, 'client');
b230cac1 21 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
535e0ab6 22
65bcf17b 23 $forum_types = forum_get_forum_types();
24
25 asort($forum_types);
26 $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forum_types);
2ee60b49 27 $mform->setHelpButton('type', array('forumtype', get_string('forumtype', 'forum'), 'forum'));
28 $mform->setDefault('type', 'general');
535e0ab6 29
2ee60b49 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');
16851b22 33 $mform->setHelpButton('intro', array('writing', 'questions', 'richtext2'), false, 'editorhelpbutton');
535e0ab6 34
6507d1a9 35 $mform->addElement('format', 'introformat', get_string('format'));
b2a9505b 36
535e0ab6 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);
2ee60b49 43 $mform->setHelpButton('forcesubscribe', array('subscription2', get_string('forcesubscribeq', 'forum'), 'forum'));
535e0ab6 44
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);
2ee60b49 50 $mform->setHelpButton('trackingtype', array('trackingtype', get_string('trackingtype', 'forum'), 'forum'));
535e0ab6 51
a02c77dc 52 $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
535e0ab6 53 $choices[1] = get_string('uploadnotallowed');
a02c77dc 54 $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
535e0ab6 55 $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
2ee60b49 56 $mform->setHelpButton('maxbytes', array('maxattachmentsize', get_string('maxattachmentsize', 'forum'), 'forum'));
57 $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
535e0ab6 58
30a9aff5 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);
63
535e0ab6 64 if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
a02c77dc 65//-------------------------------------------------------------------------------
535e0ab6 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'));
73
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 }
91
a02c77dc 92//-------------------------------------------------------------------------------
535e0ab6 93 $mform->addElement('header', '', get_string('grade'));
13bbe067 94
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'));
535e0ab6 98
a02c77dc 99 $mform->addElement('modgrade', 'scale', get_string('grade'), false);
13bbe067 100 $mform->disabledIf('scale', 'assessed', 'eq', 0);
6e372b25 101
102 $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'forum'));
13bbe067 103 $mform->disabledIf('ratingtime', 'assessed', 'eq', 0);
535e0ab6 104
105 $mform->addElement('date_time_selector', 'assesstimestart', get_string('from'));
13bbe067 106 $mform->disabledIf('assesstimestart', 'assessed', 'eq', 0);
abf11956 107 $mform->disabledIf('assesstimestart', 'ratingtime');
535e0ab6 108
109 $mform->addElement('date_time_selector', 'assesstimefinish', get_string('to'));
13bbe067 110 $mform->disabledIf('assesstimefinish', 'assessed', 'eq', 0);
abf11956 111 $mform->disabledIf('assesstimefinish', 'ratingtime');
6e372b25 112
535e0ab6 113
a02c77dc 114//-------------------------------------------------------------------------------
535e0ab6 115 $mform->addElement('header', '', get_string('blockafter', 'forum'));
2ee60b49 116 $options = array();
535e0ab6 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);
2ee60b49 126 $mform->setHelpButton('blockperiod', array('manageposts', get_string('blockperiod', 'forum'),'forum'));
535e0ab6 127
2ee60b49 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);
9c03bbaa 134
135
136 $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
2ee60b49 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);
9c03bbaa 142
a02c77dc 143//-------------------------------------------------------------------------------
42f103be 144 $this->standard_coursemodule_elements();
a23f0aaf 145//-------------------------------------------------------------------------------
2ee60b49 146// buttons
a23f0aaf 147 $this->add_action_buttons();
535e0ab6 148
2ee60b49 149 }
535e0ab6 150
2ee60b49 151 function definition_after_data(){
4050572d 152 parent::definition_after_data();
153 $mform =& $this->_form;
154 $type =& $mform->getElement('type');
155 $typevalue = $mform->getElementValue('type');
156
d24206d6 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');
d4175332 161 $type->setHelpButton(array('forumtypenews', get_string('forumtype', 'forum'), 'forum'));
d24206d6 162 $type->freeze();
163 $type->setPersistantFreeze(true);
164 }
165 if ($typevalue[0]=='social'){
166 $type->addOption(get_string('namesocial', 'forum'), 'social');
535e0ab6 167 $type->freeze();
168 $type->setPersistantFreeze(true);
169 }
02f8f37d 170
2ee60b49 171 }
535e0ab6 172
c3cec421 173 function data_preprocessing(&$default_values){
174 if (empty($default_values['scale'])){
175 $default_values['assessed'] = 0;
65bcf17b 176 }
c3cec421 177
353228d8 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;
2ee60b49 183 }
4e781c7b 184
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 }
2ee60b49 203 }
e24b7f85 204
4e781c7b 205 function add_completion_rules() {
206 $mform =& $this->_form;
207
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');
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);
221 $mform->setHelpButton('completiondiscussionsgroup', array('completion', get_string('completiondiscussionshelp', 'forum'), 'forum'));
222 $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
223
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');
231
232 return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
233 }
234
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 }
240
49f6e5f4 241 function get_data() {
242 $data=parent::get_data();
4e781c7b 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 }
535e0ab6 259}
2ee60b49 260?>