weekly back-to-dev release 4.5dev
[moodle.git] / course / edit_form.php
bloba6e1f2edecfbdf2c86c16efa086f1d5ec2dfdd7d
1 <?php
3 use core\di;
4 use core\hook;
6 defined('MOODLE_INTERNAL') || die;
8 require_once($CFG->libdir.'/formslib.php');
9 require_once($CFG->libdir.'/completionlib.php');
10 require_once($CFG->libdir . '/pdflib.php');
12 /**
13 * The form for handling editing a course.
15 class course_edit_form extends moodleform {
16 protected $course;
17 protected $context;
19 /**
20 * Form definition.
22 function definition() {
23 global $CFG, $PAGE;
25 $mform = $this->_form;
26 $PAGE->requires->js_call_amd('core_course/formatchooser', 'init');
28 $course = $this->_customdata['course']; // this contains the data of this form
29 $category = $this->_customdata['category'];
30 $editoroptions = $this->_customdata['editoroptions'];
31 $returnto = $this->_customdata['returnto'];
32 $returnurl = $this->_customdata['returnurl'];
34 $systemcontext = context_system::instance();
35 $categorycontext = context_coursecat::instance($category->id);
37 if (!empty($course->id)) {
38 $coursecontext = context_course::instance($course->id);
39 $context = $coursecontext;
40 } else {
41 $coursecontext = null;
42 $context = $categorycontext;
45 $courseconfig = get_config('moodlecourse');
47 $this->course = $course;
48 $this->context = $context;
50 // Form definition with new course defaults.
51 $mform->addElement('header','general', get_string('general', 'form'));
53 $mform->addElement('hidden', 'returnto', null);
54 $mform->setType('returnto', PARAM_ALPHANUM);
55 $mform->setConstant('returnto', $returnto);
57 $mform->addElement('hidden', 'returnurl', null);
58 $mform->setType('returnurl', PARAM_LOCALURL);
59 $mform->setConstant('returnurl', $returnurl);
61 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
62 $mform->addHelpButton('fullname', 'fullnamecourse');
63 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
64 $mform->setType('fullname', PARAM_TEXT);
65 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
66 $mform->hardFreeze('fullname');
67 $mform->setConstant('fullname', $course->fullname);
70 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
71 $mform->addHelpButton('shortname', 'shortnamecourse');
72 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
73 $mform->setType('shortname', PARAM_TEXT);
74 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
75 $mform->hardFreeze('shortname');
76 $mform->setConstant('shortname', $course->shortname);
79 // Verify permissions to change course category or keep current.
80 if (empty($course->id)) {
81 if (has_capability('moodle/course:create', $categorycontext)) {
82 $displaylist = core_course_category::make_categories_list('moodle/course:create');
83 $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
84 $mform->addRule('category', null, 'required', null, 'client');
85 $mform->addHelpButton('category', 'coursecategory');
86 $mform->setDefault('category', $category->id);
87 } else {
88 $mform->addElement('hidden', 'category', null);
89 $mform->setType('category', PARAM_INT);
90 $mform->setConstant('category', $category->id);
92 } else {
93 if (has_capability('moodle/course:changecategory', $coursecontext)) {
94 $displaylist = core_course_category::make_categories_list('moodle/course:changecategory');
95 if (!isset($displaylist[$course->category])) {
96 //always keep current
97 $displaylist[$course->category] = core_course_category::get($course->category, MUST_EXIST, true)
98 ->get_formatted_name();
100 $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
101 $mform->addRule('category', null, 'required', null, 'client');
102 $mform->addHelpButton('category', 'coursecategory');
103 } else {
104 //keep current
105 $mform->addElement('hidden', 'category', null);
106 $mform->setType('category', PARAM_INT);
107 $mform->setConstant('category', $course->category);
111 $choices = array();
112 $choices['0'] = get_string('hide');
113 $choices['1'] = get_string('show');
114 $mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
115 $mform->addHelpButton('visible', 'coursevisibility');
116 $mform->setDefault('visible', $courseconfig->visible);
117 if (!empty($course->id)) {
118 if (!has_capability('moodle/course:visibility', $coursecontext)) {
119 $mform->hardFreeze('visible');
120 $mform->setConstant('visible', $course->visible);
122 } else {
123 if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
124 $mform->hardFreeze('visible');
125 $mform->setConstant('visible', $courseconfig->visible);
129 // Download course content.
130 if ($CFG->downloadcoursecontentallowed) {
131 $downloadchoices = [
132 DOWNLOAD_COURSE_CONTENT_DISABLED => get_string('no'),
133 DOWNLOAD_COURSE_CONTENT_ENABLED => get_string('yes'),
135 $sitedefaultstring = $downloadchoices[$courseconfig->downloadcontentsitedefault];
136 $downloadchoices[DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT] = get_string('sitedefaultspecified', '', $sitedefaultstring);
137 $downloadselectdefault = $courseconfig->downloadcontent ?? DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT;
139 $mform->addElement('select', 'downloadcontent', get_string('enabledownloadcoursecontent', 'course'), $downloadchoices);
140 $mform->addHelpButton('downloadcontent', 'downloadcoursecontent', 'course');
141 $mform->setDefault('downloadcontent', $downloadselectdefault);
143 if ((!empty($course->id) && !has_capability('moodle/course:configuredownloadcontent', $coursecontext)) ||
144 (empty($course->id) &&
145 !guess_if_creator_will_have_course_capability('moodle/course:configuredownloadcontent', $categorycontext))) {
146 $mform->hardFreeze('downloadcontent');
147 $mform->setConstant('downloadcontent', $downloadselectdefault);
151 // Get the task to change automatically the course visibility when the current day matches the course start date.
152 $task = \core\task\manager::get_scheduled_task('\core\task\show_started_courses_task');
153 $startdatestring = 'startdate';
154 if (!empty($task) && !$task->get_disabled()) {
155 // When the task is enabled, display a different help message.
156 $startdatestring = 'startdatewithtaskenabled';
158 $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
159 $mform->addHelpButton('startdate', $startdatestring);
160 $date = (new DateTime())->setTimestamp(usergetmidnight(time()));
161 $date->modify('+1 day');
162 $mform->setDefault('startdate', $date->getTimestamp());
164 // Get the task to change automatically the course visibility when the current day matches the course end date.
165 $task = \core\task\manager::get_scheduled_task('\core\task\hide_ended_courses_task');
166 $enddatestring = 'enddate';
167 if (!empty($task) && !$task->get_disabled()) {
168 // When the task is enabled, display a different help message.
169 $enddatestring = 'enddatewithtaskenabled';
171 $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
172 $mform->addHelpButton('enddate', $enddatestring);
174 if (!empty($CFG->enablecourserelativedates)) {
175 $attributes = [
176 'aria-describedby' => 'relativedatesmode_warning'
178 if (!empty($course->id)) {
179 $attributes['disabled'] = true;
181 $relativeoptions = [
182 0 => get_string('no'),
183 1 => get_string('yes'),
185 $relativedatesmodegroup = [];
186 $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
187 $relativeoptions, $attributes);
188 $relativedatesmodegroup[] = $mform->createElement('html', html_writer::span(get_string('relativedatesmode_warning'),
189 '', ['id' => 'relativedatesmode_warning']));
190 $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
191 $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
194 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
195 $mform->addHelpButton('idnumber', 'idnumbercourse');
196 $mform->setType('idnumber', PARAM_RAW);
197 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
198 $mform->hardFreeze('idnumber');
199 $mform->setConstants('idnumber', $course->idnumber);
202 // Description.
203 $mform->addElement('header', 'descriptionhdr', get_string('description'));
204 $mform->setExpanded('descriptionhdr');
206 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
207 $mform->addHelpButton('summary_editor', 'coursesummary');
208 $mform->setType('summary_editor', PARAM_RAW);
209 $summaryfields = 'summary_editor';
211 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
212 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
213 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
214 $summaryfields .= ',overviewfiles_filemanager';
217 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
218 // Remove the description header it does not contain anything any more.
219 $mform->removeElement('descriptionhdr');
220 $mform->hardFreeze($summaryfields);
223 // Course format.
224 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
226 $courseformats = get_sorted_course_formats(true);
227 $formcourseformats = new core\output\choicelist();
228 $formcourseformats->set_allow_empty(false);
229 foreach ($courseformats as $courseformat) {
230 $definition = [];
231 $component = "format_$courseformat";
232 if (get_string_manager()->string_exists('plugin_description', $component)) {
233 $definition['description'] = get_string('plugin_description', $component);
235 $formcourseformats->add_option(
236 $courseformat,
237 get_string('pluginname', "format_$courseformat"),
239 'description' => $definition,
243 if (isset($course->format)) {
244 $course->format = course_get_format($course)->get_format(); // Replace with default if not found.
245 if (!in_array($course->format, $courseformats)) {
246 // This format is disabled. Still display it in the dropdown.
247 $formcourseformats->add_option(
248 $course->format,
249 get_string('withdisablednote', 'moodle', get_string('pluginname', 'format_'.$course->format)),
254 $mform->addElement(
255 'choicedropdown',
256 'format',
257 get_string('format'),
258 $formcourseformats,
259 ['data-formatchooser-field' => 'selector'],
261 $mform->setDefault('format', $courseconfig->format);
263 // Button to update format-specific options on format change (will be hidden by JavaScript).
264 $mform->registerNoSubmitButton('updatecourseformat');
265 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'), [
266 'data-formatchooser-field' => 'updateButton',
267 'class' => 'd-none',
270 // Just a placeholder for the course format options.
271 $mform->addElement('hidden', 'addcourseformatoptionshere');
272 $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
274 // Appearance.
275 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
277 if (!empty($CFG->allowcoursethemes)) {
278 $themeobjects = get_list_of_themes();
279 $themes=array();
280 $themes[''] = get_string('forceno');
281 foreach ($themeobjects as $key=>$theme) {
282 if (empty($theme->hidefromselector)) {
283 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
286 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
289 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
290 || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
292 $languages = ['' => get_string('forceno')];
293 $languages += get_string_manager()->get_list_of_translations();
295 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
296 $mform->setDefault('lang', $courseconfig->lang);
299 // Multi-Calendar Support - see MDL-18375.
300 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
301 // We do not want to show this option unless there is more than one calendar type to display.
302 if (count($calendartypes) > 1) {
303 $calendars = array();
304 $calendars[''] = get_string('forceno');
305 $calendars += $calendartypes;
306 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
309 $options = range(0, 10);
310 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
311 $courseconfig = get_config('moodlecourse');
312 $mform->setDefault('newsitems', $courseconfig->newsitems);
313 $mform->addHelpButton('newsitems', 'newsitemsnumber');
315 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
316 $mform->addHelpButton('showgrades', 'showgrades');
317 $mform->setDefault('showgrades', $courseconfig->showgrades);
319 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
320 $mform->addHelpButton('showreports', 'showreports');
321 $mform->setDefault('showreports', $courseconfig->showreports);
323 // Show activity dates.
324 $mform->addElement('selectyesno', 'showactivitydates', get_string('showactivitydates'));
325 $mform->addHelpButton('showactivitydates', 'showactivitydates');
326 $mform->setDefault('showactivitydates', $courseconfig->showactivitydates);
328 // Files and uploads.
329 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
331 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
332 if (empty($course->legacyfiles)) {
333 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
334 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
335 } else {
336 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
338 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
339 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
340 if (!isset($courseconfig->legacyfiles)) {
341 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
342 $courseconfig->legacyfiles = 0;
344 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
347 // Handle non-existing $course->maxbytes on course creation.
348 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
350 // Let's prepare the maxbytes popup.
351 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
352 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
353 $mform->addHelpButton('maxbytes', 'maximumupload');
354 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
356 // PDF font.
357 if (!empty($CFG->enablepdfexportfont)) {
358 $pdf = new \pdf;
359 $fontlist = $pdf->get_export_fontlist();
360 // Show the option if the font is defined more than one.
361 if (count($fontlist) > 1) {
362 $defaultfont = $courseconfig->pdfexportfont ?? 'freesans';
363 if (empty($fontlist[$defaultfont])) {
364 $defaultfont = current($fontlist);
366 $mform->addElement('select', 'pdfexportfont', get_string('pdfexportfont', 'course'), $fontlist);
367 $mform->addHelpButton('pdfexportfont', 'pdfexportfont', 'course');
368 $mform->setDefault('pdfexportfont', $defaultfont);
372 // Completion tracking.
373 if (completion_info::is_enabled_for_site()) {
374 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
375 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
376 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
377 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
379 $showcompletionconditions = $courseconfig->showcompletionconditions ?? COMPLETION_SHOW_CONDITIONS;
380 $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion'));
381 $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion');
382 $mform->setDefault('showcompletionconditions', $showcompletionconditions);
383 $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_DISABLED);
384 } else {
385 $mform->addElement('hidden', 'enablecompletion');
386 $mform->setType('enablecompletion', PARAM_INT);
387 $mform->setDefault('enablecompletion', 0);
390 enrol_course_edit_form($mform, $course, $context);
392 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
394 $choices = array();
395 $choices[NOGROUPS] = get_string('groupsnone', 'group');
396 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
397 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
398 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
399 $mform->addHelpButton('groupmode', 'groupmode', 'group');
400 $mform->setDefault('groupmode', $courseconfig->groupmode);
402 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
403 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
404 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
406 //default groupings selector
407 $options = array();
408 $options[0] = get_string('none');
409 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
411 if (core_tag_tag::is_enabled('core', 'course') &&
412 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
413 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
414 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
415 $mform->addElement('tags', 'tags', get_string('tags'),
416 array('itemtype' => 'course', 'component' => 'core'));
419 // Add custom fields to the form.
420 $handler = core_course\customfield\course_handler::create();
421 $handler->set_parent_context($categorycontext); // For course handler only.
422 $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
424 $hook = new \core_course\hook\after_form_definition($this, $mform);
425 di::get(hook\manager::class)->dispatch($hook);
427 // When two elements we need a group.
428 $buttonarray = array();
429 $classarray = array('class' => 'form-submit');
430 if ($returnto !== 0) {
431 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
433 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
434 $buttonarray[] = &$mform->createElement('cancel');
435 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
436 $mform->closeHeaderBefore('buttonar');
438 $mform->addElement('hidden', 'id', null);
439 $mform->setType('id', PARAM_INT);
441 // Communication api call to set the communication data in the form for handling actions for group feature changes.
442 // We only need to set the data for courses already created.
443 if (!empty($course->id)) {
444 $communication = core_communication\helper::load_by_course(
445 courseid: $course->id,
446 context: $coursecontext,
448 $communication->set_data($course);
451 // Prepare custom fields data.
452 $handler->instance_form_before_set_data($course);
453 // Finally set the current form data
454 $this->set_data($course);
458 * Fill in the current page data for this course.
460 function definition_after_data() {
461 global $DB;
463 $mform = $this->_form;
465 // add available groupings
466 $courseid = $mform->getElementValue('id');
467 if ($courseid and $mform->elementExists('defaultgroupingid')) {
468 $options = array();
469 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
470 foreach ($groupings as $grouping) {
471 $options[$grouping->id] = format_string($grouping->name);
474 core_collator::asort($options);
475 $gr_el =& $mform->getElement('defaultgroupingid');
476 $gr_el->load($options);
479 // add course format options
480 $formatvalue = $mform->getElementValue('format');
481 if (is_array($formatvalue) && !empty($formatvalue)) {
483 $params = array('format' => $formatvalue[0]);
484 // Load the course as well if it is available, course formats may need it to work out
485 // they preferred course end date.
486 if ($courseid) {
487 $params['id'] = $courseid;
489 $courseformat = course_get_format((object)$params);
491 $elements = $courseformat->create_edit_form_elements($mform);
492 for ($i = 0; $i < count($elements); $i++) {
493 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
494 'addcourseformatoptionshere');
497 // Remove newsitems element if format does not support news.
498 if (!$courseformat->supports_news()) {
499 $mform->removeElement('newsitems');
503 // Tweak the form with values provided by custom fields in use.
504 $handler = core_course\customfield\course_handler::create();
505 $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
507 $hook = new \core_course\hook\after_form_definition_after_data($this, $mform);
508 di::get(hook\manager::class)->dispatch($hook);
512 * Validation.
514 * @param array $data
515 * @param array $files
516 * @return array the errors that were found
518 function validation($data, $files) {
519 global $DB;
521 $errors = parent::validation($data, $files);
523 // Add field validation check for duplicate shortname.
524 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
525 if (empty($data['id']) || $course->id != $data['id']) {
526 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
530 // Add field validation check for duplicate idnumber.
531 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
532 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
533 if (empty($data['id']) || $course->id != $data['id']) {
534 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
539 if ($errorcode = course_validate_dates($data)) {
540 $errors['enddate'] = get_string($errorcode, 'error');
543 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
545 $courseformat = course_get_format((object)array('format' => $data['format']));
546 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
547 if (!empty($formaterrors) && is_array($formaterrors)) {
548 $errors = array_merge($errors, $formaterrors);
551 // Add the custom fields validation.
552 $handler = core_course\customfield\course_handler::create();
553 $errors = array_merge($errors, $handler->instance_form_validation($data, $files));
555 $hook = new \core_course\hook\after_form_validation($this, $data, $files);
556 di::get(hook\manager::class)->dispatch($hook);
557 $pluginerrors = $hook->get_errors();
558 if (!empty($pluginerrors)) {
559 $errors = array_merge($errors, $pluginerrors);
562 return $errors;
566 * Returns course object.
568 * @return \stdClass
570 public function get_course(): stdClass {
571 return $this->course;
575 * Returns context.
577 * @return \core\context
579 public function get_context(): \core\context {
580 return $this->context;