Merge branch 'MDL-76525-MOODLE_400_STABLE' of https://github.com/PhMemmel/moodle...
[moodle.git] / course / edit_form.php
blob50677e56a530dabf4fa81ce12f54efc6a552da2d
1 <?php
3 defined('MOODLE_INTERNAL') || die;
5 require_once($CFG->libdir.'/formslib.php');
6 require_once($CFG->libdir.'/completionlib.php');
8 /**
9 * The form for handling editing a course.
11 class course_edit_form extends moodleform {
12 protected $course;
13 protected $context;
15 /**
16 * Form definition.
18 function definition() {
19 global $CFG, $PAGE;
21 $mform = $this->_form;
22 $PAGE->requires->js_call_amd('core_course/formatchooser', 'init');
24 $course = $this->_customdata['course']; // this contains the data of this form
25 $category = $this->_customdata['category'];
26 $editoroptions = $this->_customdata['editoroptions'];
27 $returnto = $this->_customdata['returnto'];
28 $returnurl = $this->_customdata['returnurl'];
30 $systemcontext = context_system::instance();
31 $categorycontext = context_coursecat::instance($category->id);
33 if (!empty($course->id)) {
34 $coursecontext = context_course::instance($course->id);
35 $context = $coursecontext;
36 } else {
37 $coursecontext = null;
38 $context = $categorycontext;
41 $courseconfig = get_config('moodlecourse');
43 $this->course = $course;
44 $this->context = $context;
46 // Form definition with new course defaults.
47 $mform->addElement('header','general', get_string('general', 'form'));
49 $mform->addElement('hidden', 'returnto', null);
50 $mform->setType('returnto', PARAM_ALPHANUM);
51 $mform->setConstant('returnto', $returnto);
53 $mform->addElement('hidden', 'returnurl', null);
54 $mform->setType('returnurl', PARAM_LOCALURL);
55 $mform->setConstant('returnurl', $returnurl);
57 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
58 $mform->addHelpButton('fullname', 'fullnamecourse');
59 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
60 $mform->setType('fullname', PARAM_TEXT);
61 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
62 $mform->hardFreeze('fullname');
63 $mform->setConstant('fullname', $course->fullname);
66 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
67 $mform->addHelpButton('shortname', 'shortnamecourse');
68 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
69 $mform->setType('shortname', PARAM_TEXT);
70 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
71 $mform->hardFreeze('shortname');
72 $mform->setConstant('shortname', $course->shortname);
75 // Verify permissions to change course category or keep current.
76 if (empty($course->id)) {
77 if (has_capability('moodle/course:create', $categorycontext)) {
78 $displaylist = core_course_category::make_categories_list('moodle/course:create');
79 $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
80 $mform->addRule('category', null, 'required', null, 'client');
81 $mform->addHelpButton('category', 'coursecategory');
82 $mform->setDefault('category', $category->id);
83 } else {
84 $mform->addElement('hidden', 'category', null);
85 $mform->setType('category', PARAM_INT);
86 $mform->setConstant('category', $category->id);
88 } else {
89 if (has_capability('moodle/course:changecategory', $coursecontext)) {
90 $displaylist = core_course_category::make_categories_list('moodle/course:changecategory');
91 if (!isset($displaylist[$course->category])) {
92 //always keep current
93 $displaylist[$course->category] = core_course_category::get($course->category, MUST_EXIST, true)
94 ->get_formatted_name();
96 $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
97 $mform->addRule('category', null, 'required', null, 'client');
98 $mform->addHelpButton('category', 'coursecategory');
99 } else {
100 //keep current
101 $mform->addElement('hidden', 'category', null);
102 $mform->setType('category', PARAM_INT);
103 $mform->setConstant('category', $course->category);
107 $choices = array();
108 $choices['0'] = get_string('hide');
109 $choices['1'] = get_string('show');
110 $mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
111 $mform->addHelpButton('visible', 'coursevisibility');
112 $mform->setDefault('visible', $courseconfig->visible);
113 if (!empty($course->id)) {
114 if (!has_capability('moodle/course:visibility', $coursecontext)) {
115 $mform->hardFreeze('visible');
116 $mform->setConstant('visible', $course->visible);
118 } else {
119 if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
120 $mform->hardFreeze('visible');
121 $mform->setConstant('visible', $courseconfig->visible);
125 // Download course content.
126 if ($CFG->downloadcoursecontentallowed) {
127 $downloadchoices = [
128 DOWNLOAD_COURSE_CONTENT_DISABLED => get_string('no'),
129 DOWNLOAD_COURSE_CONTENT_ENABLED => get_string('yes'),
131 $sitedefaultstring = $downloadchoices[$courseconfig->downloadcontentsitedefault];
132 $downloadchoices[DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT] = get_string('sitedefaultspecified', '', $sitedefaultstring);
133 $downloadselectdefault = $courseconfig->downloadcontent ?? DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT;
135 $mform->addElement('select', 'downloadcontent', get_string('enabledownloadcoursecontent', 'course'), $downloadchoices);
136 $mform->addHelpButton('downloadcontent', 'downloadcoursecontent', 'course');
137 $mform->setDefault('downloadcontent', $downloadselectdefault);
139 if ((!empty($course->id) && !has_capability('moodle/course:configuredownloadcontent', $coursecontext)) ||
140 (empty($course->id) &&
141 !guess_if_creator_will_have_course_capability('moodle/course:configuredownloadcontent', $categorycontext))) {
142 $mform->hardFreeze('downloadcontent');
143 $mform->setConstant('downloadcontent', $downloadselectdefault);
147 $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
148 $mform->addHelpButton('startdate', 'startdate');
149 $date = (new DateTime())->setTimestamp(usergetmidnight(time()));
150 $date->modify('+1 day');
151 $mform->setDefault('startdate', $date->getTimestamp());
153 $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
154 $mform->addHelpButton('enddate', 'enddate');
156 if (!empty($CFG->enablecourserelativedates)) {
157 $attributes = [
158 'aria-describedby' => 'relativedatesmode_warning'
160 if (!empty($course->id)) {
161 $attributes['disabled'] = true;
163 $relativeoptions = [
164 0 => get_string('no'),
165 1 => get_string('yes'),
167 $relativedatesmodegroup = [];
168 $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
169 $relativeoptions, $attributes);
170 $relativedatesmodegroup[] = $mform->createElement('html', html_writer::span(get_string('relativedatesmode_warning'),
171 '', ['id' => 'relativedatesmode_warning']));
172 $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
173 $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
176 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
177 $mform->addHelpButton('idnumber', 'idnumbercourse');
178 $mform->setType('idnumber', PARAM_RAW);
179 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
180 $mform->hardFreeze('idnumber');
181 $mform->setConstants('idnumber', $course->idnumber);
184 // Description.
185 $mform->addElement('header', 'descriptionhdr', get_string('description'));
186 $mform->setExpanded('descriptionhdr');
188 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
189 $mform->addHelpButton('summary_editor', 'coursesummary');
190 $mform->setType('summary_editor', PARAM_RAW);
191 $summaryfields = 'summary_editor';
193 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
194 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
195 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
196 $summaryfields .= ',overviewfiles_filemanager';
199 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
200 // Remove the description header it does not contain anything any more.
201 $mform->removeElement('descriptionhdr');
202 $mform->hardFreeze($summaryfields);
205 // Course format.
206 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
208 $courseformats = get_sorted_course_formats(true);
209 $formcourseformats = array();
210 foreach ($courseformats as $courseformat) {
211 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
213 if (isset($course->format)) {
214 $course->format = course_get_format($course)->get_format(); // replace with default if not found
215 if (!in_array($course->format, $courseformats)) {
216 // this format is disabled. Still display it in the dropdown
217 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
218 get_string('pluginname', 'format_'.$course->format));
222 $mform->addElement('select', 'format', get_string('format'), $formcourseformats, [
223 'data-formatchooser-field' => 'selector',
225 $mform->addHelpButton('format', 'format');
226 $mform->setDefault('format', $courseconfig->format);
228 // Button to update format-specific options on format change (will be hidden by JavaScript).
229 $mform->registerNoSubmitButton('updatecourseformat');
230 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'), [
231 'data-formatchooser-field' => 'updateButton',
232 'class' => 'd-none',
235 // Just a placeholder for the course format options.
236 $mform->addElement('hidden', 'addcourseformatoptionshere');
237 $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
239 // Appearance.
240 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
242 if (!empty($CFG->allowcoursethemes)) {
243 $themeobjects = get_list_of_themes();
244 $themes=array();
245 $themes[''] = get_string('forceno');
246 foreach ($themeobjects as $key=>$theme) {
247 if (empty($theme->hidefromselector)) {
248 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
251 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
254 $languages=array();
255 $languages[''] = get_string('forceno');
256 $languages += get_string_manager()->get_list_of_translations();
257 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
258 || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
259 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
260 $mform->setDefault('lang', $courseconfig->lang);
263 // Multi-Calendar Support - see MDL-18375.
264 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
265 // We do not want to show this option unless there is more than one calendar type to display.
266 if (count($calendartypes) > 1) {
267 $calendars = array();
268 $calendars[''] = get_string('forceno');
269 $calendars += $calendartypes;
270 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
273 $options = range(0, 10);
274 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
275 $courseconfig = get_config('moodlecourse');
276 $mform->setDefault('newsitems', $courseconfig->newsitems);
277 $mform->addHelpButton('newsitems', 'newsitemsnumber');
279 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
280 $mform->addHelpButton('showgrades', 'showgrades');
281 $mform->setDefault('showgrades', $courseconfig->showgrades);
283 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
284 $mform->addHelpButton('showreports', 'showreports');
285 $mform->setDefault('showreports', $courseconfig->showreports);
287 // Show activity dates.
288 $mform->addElement('selectyesno', 'showactivitydates', get_string('showactivitydates'));
289 $mform->addHelpButton('showactivitydates', 'showactivitydates');
290 $mform->setDefault('showactivitydates', $courseconfig->showactivitydates);
292 // Files and uploads.
293 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
295 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
296 if (empty($course->legacyfiles)) {
297 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
298 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
299 } else {
300 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
302 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
303 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
304 if (!isset($courseconfig->legacyfiles)) {
305 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
306 $courseconfig->legacyfiles = 0;
308 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
311 // Handle non-existing $course->maxbytes on course creation.
312 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
314 // Let's prepare the maxbytes popup.
315 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
316 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
317 $mform->addHelpButton('maxbytes', 'maximumupload');
318 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
320 // Completion tracking.
321 if (completion_info::is_enabled_for_site()) {
322 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
323 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
324 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
325 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
327 $showcompletionconditions = $courseconfig->showcompletionconditions ?? COMPLETION_SHOW_CONDITIONS;
328 $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion'));
329 $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion');
330 $mform->setDefault('showcompletionconditions', $showcompletionconditions);
331 $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_DISABLED);
332 } else {
333 $mform->addElement('hidden', 'enablecompletion');
334 $mform->setType('enablecompletion', PARAM_INT);
335 $mform->setDefault('enablecompletion', 0);
338 enrol_course_edit_form($mform, $course, $context);
340 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
342 $choices = array();
343 $choices[NOGROUPS] = get_string('groupsnone', 'group');
344 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
345 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
346 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
347 $mform->addHelpButton('groupmode', 'groupmode', 'group');
348 $mform->setDefault('groupmode', $courseconfig->groupmode);
350 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
351 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
352 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
354 //default groupings selector
355 $options = array();
356 $options[0] = get_string('none');
357 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
359 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
360 || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
361 // Customizable role names in this course.
362 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
363 $mform->addHelpButton('rolerenaming', 'rolerenaming');
365 if ($roles = get_all_roles()) {
366 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
367 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
368 foreach ($roles as $role) {
369 $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
370 $mform->setType('role_' . $role->id, PARAM_TEXT);
375 if (core_tag_tag::is_enabled('core', 'course') &&
376 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
377 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
378 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
379 $mform->addElement('tags', 'tags', get_string('tags'),
380 array('itemtype' => 'course', 'component' => 'core'));
383 // Add custom fields to the form.
384 $handler = core_course\customfield\course_handler::create();
385 $handler->set_parent_context($categorycontext); // For course handler only.
386 $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
388 // When two elements we need a group.
389 $buttonarray = array();
390 $classarray = array('class' => 'form-submit');
391 if ($returnto !== 0) {
392 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
394 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
395 $buttonarray[] = &$mform->createElement('cancel');
396 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
397 $mform->closeHeaderBefore('buttonar');
399 $mform->addElement('hidden', 'id', null);
400 $mform->setType('id', PARAM_INT);
402 // Prepare custom fields data.
403 $handler->instance_form_before_set_data($course);
404 // Finally set the current form data
405 $this->set_data($course);
409 * Fill in the current page data for this course.
411 function definition_after_data() {
412 global $DB;
414 $mform = $this->_form;
416 // add available groupings
417 $courseid = $mform->getElementValue('id');
418 if ($courseid and $mform->elementExists('defaultgroupingid')) {
419 $options = array();
420 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
421 foreach ($groupings as $grouping) {
422 $options[$grouping->id] = format_string($grouping->name);
425 core_collator::asort($options);
426 $gr_el =& $mform->getElement('defaultgroupingid');
427 $gr_el->load($options);
430 // add course format options
431 $formatvalue = $mform->getElementValue('format');
432 if (is_array($formatvalue) && !empty($formatvalue)) {
434 $params = array('format' => $formatvalue[0]);
435 // Load the course as well if it is available, course formats may need it to work out
436 // they preferred course end date.
437 if ($courseid) {
438 $params['id'] = $courseid;
440 $courseformat = course_get_format((object)$params);
442 $elements = $courseformat->create_edit_form_elements($mform);
443 for ($i = 0; $i < count($elements); $i++) {
444 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
445 'addcourseformatoptionshere');
448 // Remove newsitems element if format does not support news.
449 if (!$courseformat->supports_news()) {
450 $mform->removeElement('newsitems');
454 // Tweak the form with values provided by custom fields in use.
455 $handler = core_course\customfield\course_handler::create();
456 $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
460 * Validation.
462 * @param array $data
463 * @param array $files
464 * @return array the errors that were found
466 function validation($data, $files) {
467 global $DB;
469 $errors = parent::validation($data, $files);
471 // Add field validation check for duplicate shortname.
472 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
473 if (empty($data['id']) || $course->id != $data['id']) {
474 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
478 // Add field validation check for duplicate idnumber.
479 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
480 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
481 if (empty($data['id']) || $course->id != $data['id']) {
482 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
487 if ($errorcode = course_validate_dates($data)) {
488 $errors['enddate'] = get_string($errorcode, 'error');
491 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
493 $courseformat = course_get_format((object)array('format' => $data['format']));
494 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
495 if (!empty($formaterrors) && is_array($formaterrors)) {
496 $errors = array_merge($errors, $formaterrors);
499 // Add the custom fields validation.
500 $handler = core_course\customfield\course_handler::create();
501 $errors = array_merge($errors, $handler->instance_form_validation($data, $files));
503 return $errors;