files MDL-20635 Added support for cleaning up files associated with a context when...
[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() {
f33e1ed4 8 global $USER, $CFG, $DB;
a81bf9c3 9
cfec0454 10 $courseconfig = get_config('moodlecourse');
a81bf9c3 11 $mform =& $this->_form;
a81bf9c3 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) {
c3df0901 29 if ($DB->count_records('course_meta', array('parent_course'=>$course->id)) > 0) {
a81bf9c3 30 $disable_meta = get_string('metaalreadyhascourses');
31 }
32
33 } else {
6682b29c 34 // if users already enrolled directly into coures, do not allow switching to meta,
35 // users with metacourse manage permission are exception
36 // please note that we do not need exact results - anything unexpected here prevents metacourse
37 $managers = get_users_by_capability($coursecontext, 'moodle/course:managemetacourse', 'u.id');
38 $enrolroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $coursecontext);
39 if ($users = get_role_users(array_keys($enrolroles), $coursecontext, false, 'u.id', 'u.id ASC')) {
40 foreach($users as $user) {
41 if (!isset($managers[$user->id])) {
42 $disable_meta = get_string('metaalreadyhasenrolments');
43 break;
44 }
45 }
a81bf9c3 46 }
6682b29c 47 unset($managers);
48 unset($users);
49 unset($enrolroles);
461f5127 50 }
51 } else {
a81bf9c3 52 $coursecontext = null;
53 $context = $categorycontext;
54 }
55
56/// form definition with new course defaults
57//--------------------------------------------------------------------------------
2b63df96 58 $mform->addElement('header','general', get_string('general', 'form'));
a81bf9c3 59
8ed5dd63 60 // Must have create course capability in both categories in order to move course
a81bf9c3 61 if (has_capability('moodle/course:create', $categorycontext)) {
62 $displaylist = array();
63 $parentlist = array();
8ed5dd63 64 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
a81bf9c3 65 $mform->addElement('select', 'category', get_string('category'), $displaylist);
66 } else {
67 $mform->addElement('hidden', 'category', null);
d18e0fe6 68 $mform->setType('category', PARAM_INT);
461f5127 69 }
a81bf9c3 70 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
853a3d8e 71 $mform->setDefault('category', $category->id);
a81bf9c3 72 $mform->setType('category', PARAM_INT);
461f5127 73
50569ba3 74 if ($course and !has_capability('moodle/course:changecategory', $coursecontext)) {
75 $mform->hardFreeze('category');
76 $mform->setConstant('category', $category->id);
77 }
78
828a673d 79 $fullname = get_string('defaultcoursefullname');
80 $shortname = get_string('defaultcourseshortname');
81 while ($DB->record_exists('course', array('fullname'=>$fullname))
82 or $DB->record_exists('course', array('fullname'=>$fullname))) {
83 $fullname++;
84 $shortname++;
85 }
86
87 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
88 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullnamecourse')), true);
01bc9ff1 89 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
90 $mform->setType('fullname', PARAM_MULTILANG);
91 if ($course and !has_capability('moodle/course:changefullname', $coursecontext)) {
92 $mform->hardFreeze('fullname');
cc444336 93 $mform->setConstant('fullname', $course->fullname);
f7c0d84b 94 }
c3df0901 95
828a673d 96 $mform->setDefault('fullname', $fullname);
97
98 $mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
99 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortnamecourse')), true);
01bc9ff1 100 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
101 $mform->setType('shortname', PARAM_MULTILANG);
102 if ($course and !has_capability('moodle/course:changeshortname', $coursecontext)) {
103 $mform->hardFreeze('shortname');
cc444336 104 $mform->setConstant('shortname', $course->shortname);
f7c0d84b 105 }
106
828a673d 107 $mform->setDefault('shortname', $shortname);
108
01bc9ff1 109 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
110 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
111 $mform->setType('idnumber', PARAM_RAW);
112 if ($course and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
113 $mform->hardFreeze('idnumber');
cc444336 114 $mform->setConstants('idnumber', $course->idnumber);
f7c0d84b 115 }
01963b95 116
a81bf9c3 117 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
80f69dc0 118 $mform->setHelpButton('summary', array('text2', get_string('helptext')), true);
a81bf9c3 119 $mform->setType('summary', PARAM_RAW);
120
50569ba3 121 if ($course and !has_capability('moodle/course:changesummary', $coursecontext)) {
122 $mform->hardFreeze('summary');
123 }
124
17da2e6f 125 $courseformats = get_plugin_list('format');
a81bf9c3 126 $formcourseformats = array();
17da2e6f 127 foreach ($courseformats as $courseformat => $formatdir) {
128 $formcourseformats["$courseformat"] = get_string("format$courseformat", "format_$courseformat");
ae628043 129 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
130 $formcourseformats["$courseformat"] = get_string("format$courseformat");
a23f0aaf 131 }
a81bf9c3 132 }
133 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
134 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
cfec0454 135 $mform->setDefault('format', $courseconfig->format);
a81bf9c3 136
137 for ($i=1; $i<=52; $i++) {
138 $sectionmenu[$i] = "$i";
139 }
140 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
cfec0454 141 $mform->setDefault('numsections', $courseconfig->numsections);
a81bf9c3 142
143 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
144 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
145 $mform->setDefault('startdate', time() + 3600 * 24);
146
147 $choices = array();
148 $choices['0'] = get_string('hiddensectionscollapsed');
149 $choices['1'] = get_string('hiddensectionsinvisible');
150 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
151 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
cfec0454 152 $mform->setDefault('hiddensections', $courseconfig->hiddensections);
a81bf9c3 153
65877218 154 $options = range(0, 10);
a81bf9c3 155 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
156 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
cfec0454 157 $mform->setDefault('newsitems', $courseconfig->newsitems);
a81bf9c3 158
65877218 159 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
a81bf9c3 160 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
cfec0454 161 $mform->setDefault('showgrades', $courseconfig->showgrades);
a81bf9c3 162
65877218 163 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
a81bf9c3 164 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
cfec0454 165 $mform->setDefault('showreports', $courseconfig->showreports);
a81bf9c3 166
167 $choices = get_max_upload_sizes($CFG->maxbytes);
168 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
169 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
cfec0454 170 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
a81bf9c3 171
172 if (!empty($CFG->allowcoursethemes)) {
173 $themes=array();
174 $themes[''] = get_string('forceno');
175 $themes += get_list_of_themes();
176 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
177 }
461f5127 178
01963b95 179 $meta=array();
180 $meta[0] = get_string('no');
181 $meta[1] = get_string('yes');
a81bf9c3 182 if ($disable_meta === false) {
183 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
3d397ee0 184 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
cfec0454 185 $mform->setDefault('metacourse', $courseconfig->metacourse);
a81bf9c3 186 } else {
3d397ee0 187 // no metacourse element - we do not want to change it anyway!
188 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
a81bf9c3 189 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
3d397ee0 190 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
a81bf9c3 191 }
01963b95 192
e15d4dd0 193//--------------------------------------------------------------------------------
194 $mform->addElement('header','enrolhdr', get_string('enrolments'));
195
196 $choices = array();
197 $modules = explode(',', $CFG->enrol_plugins_enabled);
198 foreach ($modules as $module) {
199 $name = get_string('enrolname', "enrol_$module");
200 $plugin = enrolment_factory::factory($module);
201 if (method_exists($plugin, 'print_entry')) {
202 $choices[$name] = $module;
203 }
204 }
205 asort($choices);
206 $choices = array_flip($choices);
207 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
208 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
209 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
cfec0454 210 $mform->setDefault('enrol', $courseconfig->enrol);
e15d4dd0 211
212
a81bf9c3 213 $roles = get_assignable_roles($context);
214 if (!empty($course)) {
215 // add current default role, so that it is selectable even when user can not assign it
c3df0901 216 if ($current_role = $DB->get_record('role', array('id'=>$course->defaultrole))) {
a81bf9c3 217 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
218 }
219 }
220 $choices = array();
c3df0901 221 if ($sitedefaultrole = $DB->get_record('role', array('id'=>$CFG->defaultcourseroleid))) {
a81bf9c3 222 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
01963b95 223 } else {
a81bf9c3 224 $choices[0] = get_string('sitedefault');
01963b95 225 }
a81bf9c3 226 $choices = $choices + $roles;
f20d7c41 227
228 // fix for MDL-9197
229 foreach ($choices as $choiceid => $choice) {
230 $choices[$choiceid] = format_string($choice);
231 }
232
a81bf9c3 233 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
234 $mform->setDefault('defaultrole', 0);
235
01963b95 236
a81bf9c3 237 $radio = array();
238 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
239 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
240 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
241 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
6be58b1c 242 $mform->setHelpButton('enrollable', array('courseenrollable2', get_string('enrollable')), true);
cfec0454 243 $mform->setDefault('enrollable', $courseconfig->enrollable);
a81bf9c3 244
feed8193 245 $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate'), array('optional' => true));
a81bf9c3 246 $mform->setDefault('enrolstartdate', 0);
feed8193 247 $mform->disabledIf('enrolstartdate', 'enrollable', 'neq', 2);
a81bf9c3 248
feed8193 249 $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate'), array('optional' => true));
a81bf9c3 250 $mform->setDefault('enrolenddate', 0);
feed8193 251 $mform->disabledIf('enrolenddate', 'enrollable', 'neq', 2);
252
253 $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod'), array('optional' => true, 'defaultunit' => 86400));
cfec0454 254 $mform->setDefault('enrolperiod', $courseconfig->enrolperiod);
a81bf9c3 255
e15d4dd0 256
a81bf9c3 257//--------------------------------------------------------------------------------
258 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
259
260 $choices = array();
261 $choices['0'] = get_string('no');
262 $choices['1'] = get_string('yes');
ec3dac98 263 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
a81bf9c3 264 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
cfec0454 265 $mform->setDefault('expirynotify', $courseconfig->expirynotify);
a81bf9c3 266
267 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
268 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
cfec0454 269 $mform->setDefault('notifystudents', $courseconfig->notifystudents);
a81bf9c3 270
271 $thresholdmenu=array();
272 for ($i=1; $i<=30; $i++) {
273 $seconds = $i * 86400;
274 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
275 }
276 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
277 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
cfec0454 278 $mform->setDefault('expirythreshold', $courseconfig->expirythreshold);
a81bf9c3 279
280//--------------------------------------------------------------------------------
ec3dac98 281 $mform->addElement('header','', get_string('groups', 'group'));
a81bf9c3 282
283 $choices = array();
467cebb0 284 $choices[NOGROUPS] = get_string('groupsnone', 'group');
285 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
286 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
a81bf9c3 287 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
288 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
cfec0454 289 $mform->setDefault('groupmode', $courseconfig->groupmode);
a81bf9c3 290
291 $choices = array();
292 $choices['0'] = get_string('no');
293 $choices['1'] = get_string('yes');
294 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
295 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
cfec0454 296 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
a81bf9c3 297
24f41672 298 if (!empty($CFG->enablegroupings)) {
299 //default groupings selector
300 $options = array();
301 $options[0] = get_string('none');
302 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
303 }
304
a81bf9c3 305//--------------------------------------------------------------------------------
306 $mform->addElement('header','', get_string('availability'));
307
308 $choices = array();
309 $choices['0'] = get_string('courseavailablenot');
310 $choices['1'] = get_string('courseavailable');
311 $mform->addElement('select', 'visible', get_string('availability'), $choices);
312 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
cfec0454 313 $mform->setDefault('visible', $courseconfig->visible);
9136a60c 314 if ($course and !has_capability('moodle/course:visibility', $coursecontext)) {
315 $mform->hardFreeze('visible');
316 $mform->setConstant('visible', $course->visible);
317 }
a81bf9c3 318
1898ccac 319 $mform->addElement('passwordunmask', 'enrolpassword', get_string('enrolmentkey'), 'size="25"');
320 $mform->setHelpButton('enrolpassword', array('enrolmentkey', get_string('enrolmentkey')), true);
321 $mform->setDefault('enrolpassword', '');
cfec0454 322 $mform->setDefault('enrolpassword', $courseconfig->enrolpassword);
1898ccac 323 $mform->setType('enrolpassword', PARAM_RAW);
a81bf9c3 324
82672532 325 if (empty($course) or ($course->password !== '' and $course->id != SITEID)) {
326 // do not require password in existing courses that do not have password yet - backwards compatibility ;-)
327 if (!empty($CFG->enrol_manual_requirekey)) {
328 $mform->addRule('enrolpassword', get_string('required'), 'required', null, 'client');
329 }
330 }
331
a81bf9c3 332 $choices = array();
333 $choices['0'] = get_string('guestsno');
334 $choices['1'] = get_string('guestsyes');
335 $choices['2'] = get_string('guestskey');
336 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
337 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
cfec0454 338 $mform->setDefault('guest', $courseconfig->guest);
f7c0d84b 339
a937b164 340 // If we are creating a course, its enrol method isn't yet chosen, BUT the site has a default enrol method which we can use here
341 $enrol_object = $CFG;
342 if (!empty($course)) {
82d3334a 343 $enrol_object = $course;
a937b164 344 }
c5340330 345 // If the print_entry method exists and the course enrol method isn't manual (both set or inherited from site), show cost
346 if (method_exists(enrolment_factory::factory($enrol_object->enrol), 'print_entry') && !($enrol_object->enrol == 'manual' || (empty($enrol_object->enrol) && $CFG->enrol == 'manual'))) {
a81bf9c3 347 $costgroup=array();
348 $currencies = get_list_of_currencies();
349 $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
350 $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
351 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
5bc97c98 352 //defining a rule for a form element within a group :
353 $costgrprules=array();
354 //set the message to null to tell Moodle to use a default message
355 //available for most rules, fetched from language pack (err_{rulename}).
356 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
357 $mform->addGroupRule('costgrp',$costgrprules);
a81bf9c3 358 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
359 $mform->setDefault('cost', '');
360 $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
e995c46e 361
a81bf9c3 362 }
363
364//--------------------------------------------------------------------------------
365 $mform->addElement('header','', get_string('language'));
366
367 $languages=array();
368 $languages[''] = get_string('forceno');
369 $languages += get_list_of_languages();
370 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
cfec0454 371 $mform->setDefault('lang', $courseconfig->lang);
a81bf9c3 372
4e781c7b 373//--------------------------------------------------------------------------------
374 require_once($CFG->libdir.'/completionlib.php');
375 if(completion_info::is_enabled_for_site()) {
376 $mform->addElement('header','', get_string('progress','completion'));
f7c0d84b 377 $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
4e781c7b 378 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
cfec0454 379 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
4e781c7b 380 } else {
381 $mform->addElement('hidden', 'enablecompletion');
d18e0fe6 382 $mform->setType('enablecompletion', PARAM_INT);
4e781c7b 383 $mform->setDefault('enablecompletion',0);
384 }
385
a81bf9c3 386//--------------------------------------------------------------------------------
387 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
388 $mform->addElement('header', '', get_string('restrictmodules'));
389
390 $options = array();
391 $options['0'] = get_string('no');
392 $options['1'] = get_string('yes');
2dbd6409 393 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
a81bf9c3 394 $mods = array(0=>get_string('allownone'));
3b112723 395 $mods += $DB->get_records_menu('modules', array(), 'name', 'id, name');
a81bf9c3 396
397
2dbd6409 398 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
399 array('multiple'=>'multiple', 'size'=>'10'));
400 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
401 } else {
a81bf9c3 402 $mform->addElement('hidden', 'restrictmodules', null);
d18e0fe6 403 $mform->setType('restrictmodules', PARAM_INT);
a81bf9c3 404 }
405 if ($CFG->restrictmodulesfor == 'all') {
406 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
407 if (!empty($CFG->restrictbydefault)) {
408 $mform->setDefault('restrictmodules', 1);
409 }
410 }
411 $mform->setType('restrictmodules', PARAM_INT);
412
0150c561 413/// customizable role names in this course
414//--------------------------------------------------------------------------------
7d2e9a52 415 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
416 $mform->setHelpButton('rolerenaming', array('rolerenaming', get_string('rolerenaming')), true);
0150c561 417
bef12c99 418 if ($roles = get_all_roles()) {
419 if ($coursecontext) {
9101efd3 420 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
bef12c99 421 }
422 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
0150c561 423 foreach ($roles as $role) {
bef12c99 424 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
425 if (isset($role->localname)) {
426 $mform->setDefault('role_'.$role->id, $role->localname);
427 }
428 $mform->setType('role_'.$role->id, PARAM_TEXT);
429 if (!in_array($role->id, $assignableroles)) {
430 $mform->setAdvanced('role_'.$role->id);
0150c561 431 }
432 }
433 }
434
a81bf9c3 435//--------------------------------------------------------------------------------
a23f0aaf 436 $this->add_action_buttons();
a81bf9c3 437//--------------------------------------------------------------------------------
438 $mform->addElement('hidden', 'id', null);
439 $mform->setType('id', PARAM_INT);
c08ac016 440 }
2dbd6409 441
24f41672 442 function definition_after_data() {
c3df0901 443 global $DB;
24f41672 444
445 $mform =& $this->_form;
446
447 // add availabe groupings
448 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
449 $options = array();
c3df0901 450 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
24f41672 451 foreach ($groupings as $grouping) {
452 $options[$grouping->id] = format_string($grouping->name);
453 }
454 }
455 $gr_el =& $mform->getElement('defaultgroupingid');
456 $gr_el->load($options);
457 }
458 }
f7c0d84b 459
a81bf9c3 460
461/// perform some extra moodle validation
a78890d5 462 function validation($data, $files) {
82672532 463 global $DB, $CFG;
c3df0901 464
a78890d5 465 $errors = parent::validation($data, $files);
c3df0901 466 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
a81bf9c3 467 if (!empty($data['id'])) {
468 unset($foundcourses[$data['id']]);
01963b95 469 }
470 if (!empty($foundcourses)) {
471 foreach ($foundcourses as $foundcourse) {
472 $foundcoursenames[] = $foundcourse->fullname;
473 }
a81bf9c3 474 $foundcoursenamestring = implode(',', $foundcoursenames);
475 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 476 }
01963b95 477 }
a81bf9c3 478
1d693c8f 479 if (!empty($data['enrolstartdate']) && !empty($data['enrolenddate']) &&
480 $data['enrolenddate'] <= $data['enrolstartdate']){
481 $errors['enrolenddate'] = get_string('enrolenddaterror');
01963b95 482 }
a81bf9c3 483
82672532 484 if (!empty($CFG->enrol_manual_usepasswordpolicy) and isset($data['enrolpassword']) and $data['enrolpassword'] != '') {
485 $course = $this->_customdata['course'];
486 if ($course->password !== $data['enrolpassword']) {
487 // enforce password policy only if changing password - backwards compatibility
488 $errmsg = '';
489 if (!check_password_policy($data['enrolpassword'], $errmsg)) {
490 $errors['enrolpassword'] = $errmsg;
491 }
492 }
493 }
494
a78890d5 495 return $errors;
01963b95 496 }
a81bf9c3 497}
a937b164 498?>