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