fix for MDL-7497, add mouseless feedback to cloze questions
[moodle.git] / course / edit_form.php
CommitLineData
a81bf9c3 1<?php //$Id$
2
39625498 3require_once($CFG->libdir.'/formslib.php');
4
a81bf9c3 5class course_edit_form extends moodleform {
6
7 function definition() {
8 global $USER, $CFG;
9
10 $mform =& $this->_form;
11 $renderer =& $mform->defaultRenderer();
12
13 $course = $this->_customdata['course'];
14 $category = $this->_customdata['category'];
15
16 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
17 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
18
19 $disable_meta = false; // basic meta course state protection; server-side security checks not needed
20
21 if (!empty($course)) {
22 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
23 $context = $coursecontext;
24
25 if (course_in_meta($course)) {
26 $disable_meta = get_string('metaalreadyinmeta');
27
28 } else if ($course->metacourse) {
3d397ee0 29 if (count_records('course_meta', 'parent_course', $course->id) > 0) {
a81bf9c3 30 $disable_meta = get_string('metaalreadyhascourses');
31 }
32
33 } else {
34 $managers = count(get_users_by_capability($coursecontext, 'moodle/course:managemetacourse'));
35 $participants = count(get_users_by_capability($coursecontext, 'moodle/course:view'));
36 if ($participants > $managers) {
37 $disable_meta = get_string('metaalreadyhasenrolments');
38 }
461f5127 39 }
40 } else {
a81bf9c3 41 $coursecontext = null;
42 $context = $categorycontext;
43 }
44
45/// form definition with new course defaults
46//--------------------------------------------------------------------------------
2b63df96 47 $mform->addElement('header','general', get_string('general', 'form'));
a81bf9c3 48
49 //must have create course capability in both categories in order to move course
50 if (has_capability('moodle/course:create', $categorycontext)) {
51 $displaylist = array();
52 $parentlist = array();
53 make_categories_list($displaylist, $parentlist);
54 foreach ($displaylist as $key=>$val) {
55 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $key))) {
56 unset($displaylist[$key]);
57 }
461f5127 58 }
a81bf9c3 59 $mform->addElement('select', 'category', get_string('category'), $displaylist);
60 } else {
61 $mform->addElement('hidden', 'category', null);
461f5127 62 }
a81bf9c3 63 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
64 $mform->setDefault('category', $category->id);
65 $mform->setType('category', PARAM_INT);
461f5127 66
a81bf9c3 67 $mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
68 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
69 $mform->setDefault('fullname', get_string('defaultcoursefullname'));
70 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
71 $mform->setType('fullname', PARAM_MULTILANG);
461f5127 72
a81bf9c3 73 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="15" size="10"');
74 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
75 $mform->setDefault('shortname', get_string('defaultcourseshortname'));
76 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
77 $mform->setType('shortname', PARAM_MULTILANG);
01963b95 78
a81bf9c3 79 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
80 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
81 $mform->setType('idnumber', PARAM_RAW);
01963b95 82
a81bf9c3 83 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
84 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
85 $mform->setDefault('summary', get_string('defaultcoursesummary'));
86 $mform->addRule('summary', get_string('missingsummary'), 'required', null, 'client');
87 $mform->setType('summary', PARAM_RAW);
88
89 $courseformats = get_list_of_plugins('course/format');
90 $formcourseformats = array();
91 foreach ($courseformats as $courseformat) {
92 $formcourseformats["$courseformat"] = get_string("format$courseformat");
93 }
94 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
95 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
96 $mform->setDefault('format', 'weeks');
97
98 for ($i=1; $i<=52; $i++) {
99 $sectionmenu[$i] = "$i";
100 }
101 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
102 $mform->setDefault('numsections', 10);
103
104 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
105 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
106 $mform->setDefault('startdate', time() + 3600 * 24);
107
108 $choices = array();
109 $choices['0'] = get_string('hiddensectionscollapsed');
110 $choices['1'] = get_string('hiddensectionsinvisible');
111 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
112 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
113 $mform->setDefault('hiddensections', 0);
114
115 $newsitem = get_string('newsitem');
116 $newsitems = get_string('newsitems');
117 $options = array('0' => "0 $newsitems",
118 '1' => "1 $newsitem",
119 '2' => "2 $newsitems",
120 '3' => "3 $newsitems",
121 '4' => "4 $newsitems",
122 '5' => "5 $newsitems",
123 '6' => "6 $newsitems",
124 '7' => "7 $newsitems",
125 '8' => "8 $newsitems",
126 '9' => "9 $newsitems",
127 '10' => "10 $newsitems");
128 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
129 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
130 $mform->setDefault('newsitems', 5);
131
132 $choices = array();
133 $choices['0'] = get_string('no');
134 $choices['1'] = get_string('yes');
135 $mform->addElement('select', 'showgrades', get_string('showgrades'), $choices);
136 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
137 $mform->setDefault('showgrades', 1);
138
139 $choices = array();
140 $choices['0'] = get_string('no');
141 $choices['1'] = get_string('yes');
142 $mform->addElement('select', 'showreports', get_string('showreports'), $choices);
143 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
144 $mform->setDefault('showreports', 0);
145
146 $choices = get_max_upload_sizes($CFG->maxbytes);
147 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
148 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
149
150 if (!empty($CFG->allowcoursethemes)) {
151 $themes=array();
152 $themes[''] = get_string('forceno');
153 $themes += get_list_of_themes();
154 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
155 }
461f5127 156
01963b95 157 $meta=array();
158 $meta[0] = get_string('no');
159 $meta[1] = get_string('yes');
a81bf9c3 160 if ($disable_meta === false) {
161 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
3d397ee0 162 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
163 $mform->setDefault('metacourse', 0);
a81bf9c3 164 } else {
3d397ee0 165 // no metacourse element - we do not want to change it anyway!
166 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
a81bf9c3 167 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
3d397ee0 168 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
a81bf9c3 169 }
01963b95 170
a81bf9c3 171 $roles = get_assignable_roles($context);
172 if (!empty($course)) {
173 // add current default role, so that it is selectable even when user can not assign it
174 if ($current_role = get_record('role', 'id', $course->defaultrole)) {
175 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
176 }
177 }
178 $choices = array();
179 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
180 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
01963b95 181 } else {
a81bf9c3 182 $choices[0] = get_string('sitedefault');
01963b95 183 }
a81bf9c3 184 $choices = $choices + $roles;
185 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
186 $mform->setDefault('defaultrole', 0);
187
188//--------------------------------------------------------------------------------
189 $mform->addElement('header','enrolhdr', get_string('enrolments'));
190
e517b6da 191 $choices = array();
a81bf9c3 192 $modules = explode(',', $CFG->enrol_plugins_enabled);
193 foreach ($modules as $module) {
194 $name = get_string('enrolname', "enrol_$module");
195 $plugin = enrolment_factory::factory($module);
196 if (method_exists($plugin, 'print_entry')) {
197 $choices[$name] = $module;
198 }
01963b95 199 }
a81bf9c3 200 asort($choices);
201 $choices = array_flip($choices);
202 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
203 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
204 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
01963b95 205
a81bf9c3 206 $radio = array();
207 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
208 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
209 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
210 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
211 $mform->setHelpButton('enrollable', array('courseenrollable', get_string('enrollable')), true);
212 $mform->setDefault('enrollable', 1);
213
214 $enroldatestartgrp = array();
215 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
216 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
217 $mform->addGroup($enroldatestartgrp, '', get_string('enrolstartdate'), ' ', false);
218 $mform->setDefault('enrolstartdate', 0);
219 $mform->setDefault('enrolstartdisabled', 1);
220
221 $enroldateendgrp = array();
222 $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
223 $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
224 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
225 $mform->setDefault('enrolenddate', 0);
226 $mform->setDefault('enrolenddisabled', 1);
227
228 $periodmenu=array();
229 $periodmenu[0] = get_string('unlimited');
230 for ($i=1; $i<=365; $i++) {
231 $seconds = $i * 86400;
232 $periodmenu[$seconds] = get_string('numdays', '', $i);
233 }
234 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
235 $mform->setDefault('enrolperiod', 0);
236
237//--------------------------------------------------------------------------------
238 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
239
240 $choices = array();
241 $choices['0'] = get_string('no');
242 $choices['1'] = get_string('yes');
243 $mform->addElement('select', 'expirynotify', get_string('expirynotify'), $choices);
244 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
245 $mform->setDefault('expirynotify', 0);
246
247 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
248 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
249 $mform->setDefault('notifystudents', 0);
250
251 $thresholdmenu=array();
252 for ($i=1; $i<=30; $i++) {
253 $seconds = $i * 86400;
254 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
255 }
256 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
257 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
258 $mform->setDefault('expirythreshold', 10 * 86400);
259
260//--------------------------------------------------------------------------------
261 $mform->addElement('header','', get_string('groupmode'));
262
263 $choices = array();
264 $choices[NOGROUPS] = get_string('groupsnone');
265 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
266 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
267 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
268 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
269 $mform->setDefault('groupmode', 0);
270
271 $choices = array();
272 $choices['0'] = get_string('no');
273 $choices['1'] = get_string('yes');
274 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
275 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
276 $mform->setDefault('groupmodeforce', 0);
277
278//--------------------------------------------------------------------------------
279 $mform->addElement('header','', get_string('availability'));
280
281 $choices = array();
282 $choices['0'] = get_string('courseavailablenot');
283 $choices['1'] = get_string('courseavailable');
284 $mform->addElement('select', 'visible', get_string('availability'), $choices);
285 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
286 $mform->setDefault('visible', 1);
287
288 $mform->addElement('text', 'password', get_string('enrolmentkey'), 'size="25"');
289 $mform->setHelpButton('password', array('enrolmentkey', get_string('enrolmentkey')), true);
290 $mform->setDefault('password', '');
291 $mform->setType('password', PARAM_RAW);
292
293 $choices = array();
294 $choices['0'] = get_string('guestsno');
295 $choices['1'] = get_string('guestsyes');
296 $choices['2'] = get_string('guestskey');
297 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
298 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
299 $mform->setDefault('guest', 0);
300
301 if (!empty($course) && method_exists(enrolment_factory::factory($course->enrol), 'print_entry') && $course->enrol != 'manual') {
302 $costgroup=array();
303 $currencies = get_list_of_currencies();
304 $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
305 $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
306 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
5bc97c98 307 //defining a rule for a form element within a group :
308 $costgrprules=array();
309 //set the message to null to tell Moodle to use a default message
310 //available for most rules, fetched from language pack (err_{rulename}).
311 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
312 $mform->addGroupRule('costgrp',$costgrprules);
a81bf9c3 313 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
314 $mform->setDefault('cost', '');
315 $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
e995c46e 316
a81bf9c3 317 }
318
319//--------------------------------------------------------------------------------
320 $mform->addElement('header','', get_string('language'));
321
322 $languages=array();
323 $languages[''] = get_string('forceno');
324 $languages += get_list_of_languages();
325 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
326
327//--------------------------------------------------------------------------------
328 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
329 $mform->addElement('header', '', get_string('restrictmodules'));
330
331 $options = array();
332 $options['0'] = get_string('no');
333 $options['1'] = get_string('yes');
334 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options,
335 array('onChange'=>"document.getElementById('id_allowedmods').disabled=".
336 "((this.selectedIndex==0)?true:false);"));
337 $mods = array(0=>get_string('allownone'));
338 $mods += get_records_menu('modules', '','','','id, name');
a81bf9c3 339
340
341 $mform->addElement('select', 'allowedmods', get_string('to'),$mods,
c08ac016 342 array('multiple'=>'multiple', 'size'=>'10', 'id'=>'allowedmods'));
a81bf9c3 343 }else {
344 $mform->addElement('hidden', 'restrictmodules', null);
345 }
346 if ($CFG->restrictmodulesfor == 'all') {
347 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
348 if (!empty($CFG->restrictbydefault)) {
349 $mform->setDefault('restrictmodules', 1);
350 }
351 }
352 $mform->setType('restrictmodules', PARAM_INT);
353
354//--------------------------------------------------------------------------------
355 $mform->addElement('submit', 'submit', get_string('savechanges'));
356
357//--------------------------------------------------------------------------------
358 $mform->addElement('hidden', 'id', null);
359 $mform->setType('id', PARAM_INT);
360
361 // fill in default teacher and student names to keep backwards compatibility for a while
362 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
363 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
364 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
365 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
366
367
368//--------------------------------------------------------------------------------
369 $renderer->addStopFieldsetElements('submit');
370
371
372// now override defaults if course already exists
373 if (!empty($course)) {
374 $this->set_defaults($course);
375 }
5f2256f3 376
c08ac016 377
378
379 }
380 function definition_after_data(){
e995c46e 381 $mform=&$this->_form;
382 if ($mform->elementExists('allowedmods')){
383 if ($mform->exportValue('restrictmodules')!=1){
384 $allowedmods=&$mform->getElement('allowedmods');
385 $allowedmods->
386 updateAttributes(array('disabled'
387 => 'disabled')) ;
388 }
389 }
390 }
a81bf9c3 391
392/// perform some extra moodle validation
393 function validation($data){
01963b95 394 $errors= array();
a81bf9c3 395 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
396 if (!empty($data['id'])) {
397 unset($foundcourses[$data['id']]);
01963b95 398 }
399 if (!empty($foundcourses)) {
400 foreach ($foundcourses as $foundcourse) {
401 $foundcoursenames[] = $foundcourse->fullname;
402 }
a81bf9c3 403 $foundcoursenamestring = implode(',', $foundcoursenames);
404 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 405 }
01963b95 406 }
a81bf9c3 407
408 if (empty($data['enrolenddisabled'])){
409 if ($data['enrolenddate'] <= $data['enrolstartdate']){
410 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
01963b95 411 }
412 }
a81bf9c3 413
414 if (0 == count($errors)){
01963b95 415 return true;
a81bf9c3 416 } else {
01963b95 417 return $errors;
418 }
419 }
a81bf9c3 420}
421?>