3 defined('MOODLE_INTERNAL') ||
die;
5 require_once($CFG->libdir
.'/formslib.php');
6 require_once($CFG->libdir
.'/completionlib.php');
9 * The form for handling editing a course.
11 class course_edit_form
extends moodleform
{
18 function definition() {
21 $mform = $this->_form
;
22 $PAGE->requires
->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
23 array(array('formid' => $mform->getAttribute('id'))));
25 $course = $this->_customdata
['course']; // this contains the data of this form
26 $category = $this->_customdata
['category'];
27 $editoroptions = $this->_customdata
['editoroptions'];
28 $returnto = $this->_customdata
['returnto'];
29 $returnurl = $this->_customdata
['returnurl'];
31 $systemcontext = context_system
::instance();
32 $categorycontext = context_coursecat
::instance($category->id
);
34 if (!empty($course->id
)) {
35 $coursecontext = context_course
::instance($course->id
);
36 $context = $coursecontext;
38 $coursecontext = null;
39 $context = $categorycontext;
42 $courseconfig = get_config('moodlecourse');
44 $this->course
= $course;
45 $this->context
= $context;
47 // Form definition with new course defaults.
48 $mform->addElement('header','general', get_string('general', 'form'));
50 $mform->addElement('hidden', 'returnto', null);
51 $mform->setType('returnto', PARAM_ALPHANUM
);
52 $mform->setConstant('returnto', $returnto);
54 $mform->addElement('hidden', 'returnurl', null);
55 $mform->setType('returnurl', PARAM_LOCALURL
);
56 $mform->setConstant('returnurl', $returnurl);
58 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
59 $mform->addHelpButton('fullname', 'fullnamecourse');
60 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
61 $mform->setType('fullname', PARAM_TEXT
);
62 if (!empty($course->id
) and !has_capability('moodle/course:changefullname', $coursecontext)) {
63 $mform->hardFreeze('fullname');
64 $mform->setConstant('fullname', $course->fullname
);
67 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
68 $mform->addHelpButton('shortname', 'shortnamecourse');
69 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
70 $mform->setType('shortname', PARAM_TEXT
);
71 if (!empty($course->id
) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
72 $mform->hardFreeze('shortname');
73 $mform->setConstant('shortname', $course->shortname
);
76 // Verify permissions to change course category or keep current.
77 if (empty($course->id
)) {
78 if (has_capability('moodle/course:create', $categorycontext)) {
79 $displaylist = core_course_category
::make_categories_list('moodle/course:create');
80 $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
81 $mform->addRule('category', null, 'required', null, 'client');
82 $mform->addHelpButton('category', 'coursecategory');
83 $mform->setDefault('category', $category->id
);
85 $mform->addElement('hidden', 'category', null);
86 $mform->setType('category', PARAM_INT
);
87 $mform->setConstant('category', $category->id
);
90 if (has_capability('moodle/course:changecategory', $coursecontext)) {
91 $displaylist = core_course_category
::make_categories_list('moodle/course:changecategory');
92 if (!isset($displaylist[$course->category
])) {
94 $displaylist[$course->category
] = core_course_category
::get($course->category
, MUST_EXIST
, true)
95 ->get_formatted_name();
97 $mform->addElement('autocomplete', 'category', get_string('coursecategory'), $displaylist);
98 $mform->addRule('category', null, 'required', null, 'client');
99 $mform->addHelpButton('category', 'coursecategory');
102 $mform->addElement('hidden', 'category', null);
103 $mform->setType('category', PARAM_INT
);
104 $mform->setConstant('category', $course->category
);
109 $choices['0'] = get_string('hide');
110 $choices['1'] = get_string('show');
111 $mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
112 $mform->addHelpButton('visible', 'coursevisibility');
113 $mform->setDefault('visible', $courseconfig->visible
);
114 if (!empty($course->id
)) {
115 if (!has_capability('moodle/course:visibility', $coursecontext)) {
116 $mform->hardFreeze('visible');
117 $mform->setConstant('visible', $course->visible
);
120 if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
121 $mform->hardFreeze('visible');
122 $mform->setConstant('visible', $courseconfig->visible
);
126 // Download course content.
127 if ($CFG->downloadcoursecontentallowed
) {
129 DOWNLOAD_COURSE_CONTENT_DISABLED
=> get_string('no'),
130 DOWNLOAD_COURSE_CONTENT_ENABLED
=> get_string('yes'),
132 $sitedefaultstring = $downloadchoices[$courseconfig->downloadcontentsitedefault
];
133 $downloadchoices[DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT
] = get_string('sitedefaultspecified', '', $sitedefaultstring);
134 $downloadselectdefault = $courseconfig->downloadcontent ?? DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT
;
136 $mform->addElement('select', 'downloadcontent', get_string('enabledownloadcoursecontent', 'course'), $downloadchoices);
137 $mform->addHelpButton('downloadcontent', 'downloadcoursecontent', 'course');
138 $mform->setDefault('downloadcontent', $downloadselectdefault);
140 if ((!empty($course->id
) && !has_capability('moodle/course:configuredownloadcontent', $coursecontext)) ||
141 (empty($course->id
) &&
142 !guess_if_creator_will_have_course_capability('moodle/course:configuredownloadcontent', $categorycontext))) {
143 $mform->hardFreeze('downloadcontent');
144 $mform->setConstant('downloadcontent', $downloadselectdefault);
148 $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
149 $mform->addHelpButton('startdate', 'startdate');
150 $date = (new DateTime())->setTimestamp(usergetmidnight(time()));
151 $date->modify('+1 day');
152 $mform->setDefault('startdate', $date->getTimestamp());
154 $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
155 $mform->addHelpButton('enddate', 'enddate');
157 if (!empty($CFG->enablecourserelativedates
)) {
159 'aria-describedby' => 'relativedatesmode_warning'
161 if (!empty($course->id
)) {
162 $attributes['disabled'] = true;
165 0 => get_string('no'),
166 1 => get_string('yes'),
168 $relativedatesmodegroup = [];
169 $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
170 $relativeoptions, $attributes);
171 $relativedatesmodegroup[] = $mform->createElement('html', html_writer
::span(get_string('relativedatesmode_warning'),
172 '', ['id' => 'relativedatesmode_warning']));
173 $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
174 $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
177 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
178 $mform->addHelpButton('idnumber', 'idnumbercourse');
179 $mform->setType('idnumber', PARAM_RAW
);
180 if (!empty($course->id
) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
181 $mform->hardFreeze('idnumber');
182 $mform->setConstants('idnumber', $course->idnumber
);
186 $mform->addElement('header', 'descriptionhdr', get_string('description'));
187 $mform->setExpanded('descriptionhdr');
189 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
190 $mform->addHelpButton('summary_editor', 'coursesummary');
191 $mform->setType('summary_editor', PARAM_RAW
);
192 $summaryfields = 'summary_editor';
194 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
195 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
196 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
197 $summaryfields .= ',overviewfiles_filemanager';
200 if (!empty($course->id
) and !has_capability('moodle/course:changesummary', $coursecontext)) {
201 // Remove the description header it does not contain anything any more.
202 $mform->removeElement('descriptionhdr');
203 $mform->hardFreeze($summaryfields);
207 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
209 $courseformats = get_sorted_course_formats(true);
210 $formcourseformats = array();
211 foreach ($courseformats as $courseformat) {
212 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
214 if (isset($course->format
)) {
215 $course->format
= course_get_format($course)->get_format(); // replace with default if not found
216 if (!in_array($course->format
, $courseformats)) {
217 // this format is disabled. Still display it in the dropdown
218 $formcourseformats[$course->format
] = get_string('withdisablednote', 'moodle',
219 get_string('pluginname', 'format_'.$course->format
));
223 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
224 $mform->addHelpButton('format', 'format');
225 $mform->setDefault('format', $courseconfig->format
);
227 // Button to update format-specific options on format change (will be hidden by JavaScript).
228 $mform->registerNoSubmitButton('updatecourseformat');
229 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
231 // Just a placeholder for the course format options.
232 $mform->addElement('hidden', 'addcourseformatoptionshere');
233 $mform->setType('addcourseformatoptionshere', PARAM_BOOL
);
236 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
238 if (!empty($CFG->allowcoursethemes
)) {
239 $themeobjects = get_list_of_themes();
241 $themes[''] = get_string('forceno');
242 foreach ($themeobjects as $key=>$theme) {
243 if (empty($theme->hidefromselector
)) {
244 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name
);
247 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
251 $languages[''] = get_string('forceno');
252 $languages +
= get_string_manager()->get_list_of_translations();
253 if ((empty($course->id
) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
254 ||
(!empty($course->id
) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
255 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
256 $mform->setDefault('lang', $courseconfig->lang
);
259 // Multi-Calendar Support - see MDL-18375.
260 $calendartypes = \core_calendar\type_factory
::get_list_of_calendar_types();
261 // We do not want to show this option unless there is more than one calendar type to display.
262 if (count($calendartypes) > 1) {
263 $calendars = array();
264 $calendars[''] = get_string('forceno');
265 $calendars +
= $calendartypes;
266 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
269 $options = range(0, 10);
270 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
271 $courseconfig = get_config('moodlecourse');
272 $mform->setDefault('newsitems', $courseconfig->newsitems
);
273 $mform->addHelpButton('newsitems', 'newsitemsnumber');
275 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
276 $mform->addHelpButton('showgrades', 'showgrades');
277 $mform->setDefault('showgrades', $courseconfig->showgrades
);
279 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
280 $mform->addHelpButton('showreports', 'showreports');
281 $mform->setDefault('showreports', $courseconfig->showreports
);
283 // Show activity dates.
284 $mform->addElement('selectyesno', 'showactivitydates', get_string('showactivitydates'));
285 $mform->addHelpButton('showactivitydates', 'showactivitydates');
286 $mform->setDefault('showactivitydates', $courseconfig->showactivitydates
);
288 // Files and uploads.
289 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
291 if (!empty($course->legacyfiles
) or !empty($CFG->legacyfilesinnewcourses
)) {
292 if (empty($course->legacyfiles
)) {
293 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
294 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
296 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
298 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
299 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
300 if (!isset($courseconfig->legacyfiles
)) {
301 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
302 $courseconfig->legacyfiles
= 0;
304 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles
);
307 // Handle non-existing $course->maxbytes on course creation.
308 $coursemaxbytes = !isset($course->maxbytes
) ?
null : $course->maxbytes
;
310 // Let's prepare the maxbytes popup.
311 $choices = get_max_upload_sizes($CFG->maxbytes
, 0, 0, $coursemaxbytes);
312 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
313 $mform->addHelpButton('maxbytes', 'maximumupload');
314 $mform->setDefault('maxbytes', $courseconfig->maxbytes
);
316 // Completion tracking.
317 if (completion_info
::is_enabled_for_site()) {
318 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
319 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
320 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion
);
321 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
323 $showcompletionconditions = $courseconfig->showcompletionconditions ?? COMPLETION_SHOW_CONDITIONS
;
324 $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion'));
325 $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion');
326 $mform->setDefault('showcompletionconditions', $showcompletionconditions);
327 $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_DISABLED
);
329 $mform->addElement('hidden', 'enablecompletion');
330 $mform->setType('enablecompletion', PARAM_INT
);
331 $mform->setDefault('enablecompletion', 0);
334 enrol_course_edit_form($mform, $course, $context);
336 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
339 $choices[NOGROUPS
] = get_string('groupsnone', 'group');
340 $choices[SEPARATEGROUPS
] = get_string('groupsseparate', 'group');
341 $choices[VISIBLEGROUPS
] = get_string('groupsvisible', 'group');
342 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
343 $mform->addHelpButton('groupmode', 'groupmode', 'group');
344 $mform->setDefault('groupmode', $courseconfig->groupmode
);
346 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
347 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
348 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce
);
350 //default groupings selector
352 $options[0] = get_string('none');
353 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
355 if ((empty($course->id
) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
356 ||
(!empty($course->id
) && has_capability('moodle/course:renameroles', $coursecontext))) {
357 // Customizable role names in this course.
358 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
359 $mform->addHelpButton('rolerenaming', 'rolerenaming');
361 if ($roles = get_all_roles()) {
362 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL
);
363 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE
);
364 foreach ($roles as $role) {
365 $mform->addElement('text', 'role_' . $role->id
, get_string('yourwordforx', '', $role->localname
));
366 $mform->setType('role_' . $role->id
, PARAM_TEXT
);
371 if (core_tag_tag
::is_enabled('core', 'course') &&
372 ((empty($course->id
) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
373 ||
(!empty($course->id
) && has_capability('moodle/course:tag', $coursecontext)))) {
374 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
375 $mform->addElement('tags', 'tags', get_string('tags'),
376 array('itemtype' => 'course', 'component' => 'core'));
379 // Add custom fields to the form.
380 $handler = core_course\customfield\course_handler
::create();
381 $handler->set_parent_context($categorycontext); // For course handler only.
382 $handler->instance_form_definition($mform, empty($course->id
) ?
0 : $course->id
);
384 // When two elements we need a group.
385 $buttonarray = array();
386 $classarray = array('class' => 'form-submit');
387 if ($returnto !== 0) {
388 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
390 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
391 $buttonarray[] = &$mform->createElement('cancel');
392 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
393 $mform->closeHeaderBefore('buttonar');
395 $mform->addElement('hidden', 'id', null);
396 $mform->setType('id', PARAM_INT
);
398 // Prepare custom fields data.
399 $handler->instance_form_before_set_data($course);
400 // Finally set the current form data
401 $this->set_data($course);
405 * Fill in the current page data for this course.
407 function definition_after_data() {
410 $mform = $this->_form
;
412 // add available groupings
413 $courseid = $mform->getElementValue('id');
414 if ($courseid and $mform->elementExists('defaultgroupingid')) {
416 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
417 foreach ($groupings as $grouping) {
418 $options[$grouping->id
] = format_string($grouping->name
);
421 core_collator
::asort($options);
422 $gr_el =& $mform->getElement('defaultgroupingid');
423 $gr_el->load($options);
426 // add course format options
427 $formatvalue = $mform->getElementValue('format');
428 if (is_array($formatvalue) && !empty($formatvalue)) {
430 $params = array('format' => $formatvalue[0]);
431 // Load the course as well if it is available, course formats may need it to work out
432 // they preferred course end date.
434 $params['id'] = $courseid;
436 $courseformat = course_get_format((object)$params);
438 $elements = $courseformat->create_edit_form_elements($mform);
439 for ($i = 0; $i < count($elements); $i++
) {
440 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
441 'addcourseformatoptionshere');
444 // Remove newsitems element if format does not support news.
445 if (!$courseformat->supports_news()) {
446 $mform->removeElement('newsitems');
450 // Tweak the form with values provided by custom fields in use.
451 $handler = core_course\customfield\course_handler
::create();
452 $handler->instance_form_definition_after_data($mform, empty($courseid) ?
0 : $courseid);
459 * @param array $files
460 * @return array the errors that were found
462 function validation($data, $files) {
465 $errors = parent
::validation($data, $files);
467 // Add field validation check for duplicate shortname.
468 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE
)) {
469 if (empty($data['id']) ||
$course->id
!= $data['id']) {
470 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname
);
474 // Add field validation check for duplicate idnumber.
475 if (!empty($data['idnumber']) && (empty($data['id']) ||
$this->course
->idnumber
!= $data['idnumber'])) {
476 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE
)) {
477 if (empty($data['id']) ||
$course->id
!= $data['id']) {
478 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname
);
483 if ($errorcode = course_validate_dates($data)) {
484 $errors['enddate'] = get_string($errorcode, 'error');
487 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context
));
489 $courseformat = course_get_format((object)array('format' => $data['format']));
490 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
491 if (!empty($formaterrors) && is_array($formaterrors)) {
492 $errors = array_merge($errors, $formaterrors);
495 // Add the custom fields validation.
496 $handler = core_course\customfield\course_handler
::create();
497 $errors = array_merge($errors, $handler->instance_form_validation($data, $files));