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