MDL-75708 reportbuilder: consider stress tests as requiring longtest.
[moodle.git] / course / edit_form.php
blob0c6d9028ff1c8388baecc3cb6156fe38bf7decc3
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 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
255 || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
257 $languages = ['' => get_string('forceno')];
258 $languages += get_string_manager()->get_list_of_translations();
260 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
261 $mform->setDefault('lang', $courseconfig->lang);
264 // Multi-Calendar Support - see MDL-18375.
265 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
266 // We do not want to show this option unless there is more than one calendar type to display.
267 if (count($calendartypes) > 1) {
268 $calendars = array();
269 $calendars[''] = get_string('forceno');
270 $calendars += $calendartypes;
271 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
274 $options = range(0, 10);
275 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
276 $courseconfig = get_config('moodlecourse');
277 $mform->setDefault('newsitems', $courseconfig->newsitems);
278 $mform->addHelpButton('newsitems', 'newsitemsnumber');
280 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
281 $mform->addHelpButton('showgrades', 'showgrades');
282 $mform->setDefault('showgrades', $courseconfig->showgrades);
284 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
285 $mform->addHelpButton('showreports', 'showreports');
286 $mform->setDefault('showreports', $courseconfig->showreports);
288 // Show activity dates.
289 $mform->addElement('selectyesno', 'showactivitydates', get_string('showactivitydates'));
290 $mform->addHelpButton('showactivitydates', 'showactivitydates');
291 $mform->setDefault('showactivitydates', $courseconfig->showactivitydates);
293 // Files and uploads.
294 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
296 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
297 if (empty($course->legacyfiles)) {
298 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
299 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
300 } else {
301 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
303 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
304 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
305 if (!isset($courseconfig->legacyfiles)) {
306 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
307 $courseconfig->legacyfiles = 0;
309 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
312 // Handle non-existing $course->maxbytes on course creation.
313 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
315 // Let's prepare the maxbytes popup.
316 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
317 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
318 $mform->addHelpButton('maxbytes', 'maximumupload');
319 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
321 // Completion tracking.
322 if (completion_info::is_enabled_for_site()) {
323 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
324 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
325 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
326 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
328 $showcompletionconditions = $courseconfig->showcompletionconditions ?? COMPLETION_SHOW_CONDITIONS;
329 $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion'));
330 $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion');
331 $mform->setDefault('showcompletionconditions', $showcompletionconditions);
332 $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_DISABLED);
333 } else {
334 $mform->addElement('hidden', 'enablecompletion');
335 $mform->setType('enablecompletion', PARAM_INT);
336 $mform->setDefault('enablecompletion', 0);
339 enrol_course_edit_form($mform, $course, $context);
341 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
343 $choices = array();
344 $choices[NOGROUPS] = get_string('groupsnone', 'group');
345 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
346 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
347 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
348 $mform->addHelpButton('groupmode', 'groupmode', 'group');
349 $mform->setDefault('groupmode', $courseconfig->groupmode);
351 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
352 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
353 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
355 //default groupings selector
356 $options = array();
357 $options[0] = get_string('none');
358 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
360 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
361 || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
362 // Customizable role names in this course.
363 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
364 $mform->addHelpButton('rolerenaming', 'rolerenaming');
366 if ($roles = get_all_roles()) {
367 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
368 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
369 foreach ($roles as $role) {
370 $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
371 $mform->setType('role_' . $role->id, PARAM_TEXT);
376 if (core_tag_tag::is_enabled('core', 'course') &&
377 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
378 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
379 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
380 $mform->addElement('tags', 'tags', get_string('tags'),
381 array('itemtype' => 'course', 'component' => 'core'));
384 // Add custom fields to the form.
385 $handler = core_course\customfield\course_handler::create();
386 $handler->set_parent_context($categorycontext); // For course handler only.
387 $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
389 // When two elements we need a group.
390 $buttonarray = array();
391 $classarray = array('class' => 'form-submit');
392 if ($returnto !== 0) {
393 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
395 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
396 $buttonarray[] = &$mform->createElement('cancel');
397 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
398 $mform->closeHeaderBefore('buttonar');
400 $mform->addElement('hidden', 'id', null);
401 $mform->setType('id', PARAM_INT);
403 // Prepare custom fields data.
404 $handler->instance_form_before_set_data($course);
405 // Finally set the current form data
406 $this->set_data($course);
410 * Fill in the current page data for this course.
412 function definition_after_data() {
413 global $DB;
415 $mform = $this->_form;
417 // add available groupings
418 $courseid = $mform->getElementValue('id');
419 if ($courseid and $mform->elementExists('defaultgroupingid')) {
420 $options = array();
421 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
422 foreach ($groupings as $grouping) {
423 $options[$grouping->id] = format_string($grouping->name);
426 core_collator::asort($options);
427 $gr_el =& $mform->getElement('defaultgroupingid');
428 $gr_el->load($options);
431 // add course format options
432 $formatvalue = $mform->getElementValue('format');
433 if (is_array($formatvalue) && !empty($formatvalue)) {
435 $params = array('format' => $formatvalue[0]);
436 // Load the course as well if it is available, course formats may need it to work out
437 // they preferred course end date.
438 if ($courseid) {
439 $params['id'] = $courseid;
441 $courseformat = course_get_format((object)$params);
443 $elements = $courseformat->create_edit_form_elements($mform);
444 for ($i = 0; $i < count($elements); $i++) {
445 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
446 'addcourseformatoptionshere');
449 // Remove newsitems element if format does not support news.
450 if (!$courseformat->supports_news()) {
451 $mform->removeElement('newsitems');
455 // Tweak the form with values provided by custom fields in use.
456 $handler = core_course\customfield\course_handler::create();
457 $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
461 * Validation.
463 * @param array $data
464 * @param array $files
465 * @return array the errors that were found
467 function validation($data, $files) {
468 global $DB;
470 $errors = parent::validation($data, $files);
472 // Add field validation check for duplicate shortname.
473 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
474 if (empty($data['id']) || $course->id != $data['id']) {
475 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
479 // Add field validation check for duplicate idnumber.
480 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
481 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
482 if (empty($data['id']) || $course->id != $data['id']) {
483 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
488 if ($errorcode = course_validate_dates($data)) {
489 $errors['enddate'] = get_string($errorcode, 'error');
492 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
494 $courseformat = course_get_format((object)array('format' => $data['format']));
495 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
496 if (!empty($formaterrors) && is_array($formaterrors)) {
497 $errors = array_merge($errors, $formaterrors);
500 // Add the custom fields validation.
501 $handler = core_course\customfield\course_handler::create();
502 $errors = array_merge($errors, $handler->instance_form_validation($data, $files));
504 return $errors;