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('select', 'category', get_string('coursecategory'), $displaylist);
81 $mform->addHelpButton('category', 'coursecategory');
82 $mform->setDefault('category', $category->id
);
84 $mform->addElement('hidden', 'category', null);
85 $mform->setType('category', PARAM_INT
);
86 $mform->setConstant('category', $category->id
);
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
])) {
93 $displaylist[$course->category
] = core_course_category
::get($course->category
, MUST_EXIST
, true)
94 ->get_formatted_name();
96 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
97 $mform->addHelpButton('category', 'coursecategory');
100 $mform->addElement('hidden', 'category', null);
101 $mform->setType('category', PARAM_INT
);
102 $mform->setConstant('category', $course->category
);
107 $choices['0'] = get_string('hide');
108 $choices['1'] = get_string('show');
109 $mform->addElement('select', 'visible', get_string('coursevisibility'), $choices);
110 $mform->addHelpButton('visible', 'coursevisibility');
111 $mform->setDefault('visible', $courseconfig->visible
);
112 if (!empty($course->id
)) {
113 if (!has_capability('moodle/course:visibility', $coursecontext)) {
114 $mform->hardFreeze('visible');
115 $mform->setConstant('visible', $course->visible
);
118 if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
119 $mform->hardFreeze('visible');
120 $mform->setConstant('visible', $courseconfig->visible
);
123 $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
124 $mform->addHelpButton('startdate', 'startdate');
125 $date = (new DateTime())->setTimestamp(usergetmidnight(time()));
126 $date->modify('+1 day');
127 $mform->setDefault('startdate', $date->getTimestamp());
129 $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
130 $mform->addHelpButton('enddate', 'enddate');
132 if (!empty($CFG->enablecourserelativedates
)) {
134 'aria-describedby' => 'relativedatesmode_warning'
136 if (!empty($course->id
)) {
137 $attributes['disabled'] = true;
140 0 => get_string('no'),
141 1 => get_string('yes'),
143 $relativedatesmodegroup = [];
144 $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
145 $relativeoptions, $attributes);
146 $relativedatesmodegroup[] = $mform->createElement('html', html_writer
::span(get_string('relativedatesmode_warning'),
147 '', ['id' => 'relativedatesmode_warning']));
148 $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
149 $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
152 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
153 $mform->addHelpButton('idnumber', 'idnumbercourse');
154 $mform->setType('idnumber', PARAM_RAW
);
155 if (!empty($course->id
) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
156 $mform->hardFreeze('idnumber');
157 $mform->setConstants('idnumber', $course->idnumber
);
161 $mform->addElement('header', 'descriptionhdr', get_string('description'));
162 $mform->setExpanded('descriptionhdr');
164 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
165 $mform->addHelpButton('summary_editor', 'coursesummary');
166 $mform->setType('summary_editor', PARAM_RAW
);
167 $summaryfields = 'summary_editor';
169 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
170 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
171 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
172 $summaryfields .= ',overviewfiles_filemanager';
175 if (!empty($course->id
) and !has_capability('moodle/course:changesummary', $coursecontext)) {
176 // Remove the description header it does not contain anything any more.
177 $mform->removeElement('descriptionhdr');
178 $mform->hardFreeze($summaryfields);
182 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
184 $courseformats = get_sorted_course_formats(true);
185 $formcourseformats = array();
186 foreach ($courseformats as $courseformat) {
187 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
189 if (isset($course->format
)) {
190 $course->format
= course_get_format($course)->get_format(); // replace with default if not found
191 if (!in_array($course->format
, $courseformats)) {
192 // this format is disabled. Still display it in the dropdown
193 $formcourseformats[$course->format
] = get_string('withdisablednote', 'moodle',
194 get_string('pluginname', 'format_'.$course->format
));
198 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
199 $mform->addHelpButton('format', 'format');
200 $mform->setDefault('format', $courseconfig->format
);
202 // Button to update format-specific options on format change (will be hidden by JavaScript).
203 $mform->registerNoSubmitButton('updatecourseformat');
204 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
206 // Just a placeholder for the course format options.
207 $mform->addElement('hidden', 'addcourseformatoptionshere');
208 $mform->setType('addcourseformatoptionshere', PARAM_BOOL
);
211 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
213 if (!empty($CFG->allowcoursethemes
)) {
214 $themeobjects = get_list_of_themes();
216 $themes[''] = get_string('forceno');
217 foreach ($themeobjects as $key=>$theme) {
218 if (empty($theme->hidefromselector
)) {
219 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name
);
222 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
226 $languages[''] = get_string('forceno');
227 $languages +
= get_string_manager()->get_list_of_translations();
228 if ((empty($course->id
) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
229 ||
(!empty($course->id
) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
230 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
231 $mform->setDefault('lang', $courseconfig->lang
);
234 // Multi-Calendar Support - see MDL-18375.
235 $calendartypes = \core_calendar\type_factory
::get_list_of_calendar_types();
236 // We do not want to show this option unless there is more than one calendar type to display.
237 if (count($calendartypes) > 1) {
238 $calendars = array();
239 $calendars[''] = get_string('forceno');
240 $calendars +
= $calendartypes;
241 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
244 $options = range(0, 10);
245 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
246 $courseconfig = get_config('moodlecourse');
247 $mform->setDefault('newsitems', $courseconfig->newsitems
);
248 $mform->addHelpButton('newsitems', 'newsitemsnumber');
250 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
251 $mform->addHelpButton('showgrades', 'showgrades');
252 $mform->setDefault('showgrades', $courseconfig->showgrades
);
254 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
255 $mform->addHelpButton('showreports', 'showreports');
256 $mform->setDefault('showreports', $courseconfig->showreports
);
258 // Files and uploads.
259 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
261 if (!empty($course->legacyfiles
) or !empty($CFG->legacyfilesinnewcourses
)) {
262 if (empty($course->legacyfiles
)) {
263 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
264 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
266 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
268 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
269 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
270 if (!isset($courseconfig->legacyfiles
)) {
271 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
272 $courseconfig->legacyfiles
= 0;
274 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles
);
277 // Handle non-existing $course->maxbytes on course creation.
278 $coursemaxbytes = !isset($course->maxbytes
) ?
null : $course->maxbytes
;
280 // Let's prepare the maxbytes popup.
281 $choices = get_max_upload_sizes($CFG->maxbytes
, 0, 0, $coursemaxbytes);
282 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
283 $mform->addHelpButton('maxbytes', 'maximumupload');
284 $mform->setDefault('maxbytes', $courseconfig->maxbytes
);
286 // Completion tracking.
287 if (completion_info
::is_enabled_for_site()) {
288 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
289 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
290 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion
);
291 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
293 $mform->addElement('hidden', 'enablecompletion');
294 $mform->setType('enablecompletion', PARAM_INT
);
295 $mform->setDefault('enablecompletion', 0);
298 enrol_course_edit_form($mform, $course, $context);
300 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
303 $choices[NOGROUPS
] = get_string('groupsnone', 'group');
304 $choices[SEPARATEGROUPS
] = get_string('groupsseparate', 'group');
305 $choices[VISIBLEGROUPS
] = get_string('groupsvisible', 'group');
306 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
307 $mform->addHelpButton('groupmode', 'groupmode', 'group');
308 $mform->setDefault('groupmode', $courseconfig->groupmode
);
310 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
311 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
312 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce
);
314 //default groupings selector
316 $options[0] = get_string('none');
317 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
319 if ((empty($course->id
) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
320 ||
(!empty($course->id
) && has_capability('moodle/course:renameroles', $coursecontext))) {
321 // Customizable role names in this course.
322 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
323 $mform->addHelpButton('rolerenaming', 'rolerenaming');
325 if ($roles = get_all_roles()) {
326 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL
);
327 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE
);
328 foreach ($roles as $role) {
329 $mform->addElement('text', 'role_' . $role->id
, get_string('yourwordforx', '', $role->localname
));
330 $mform->setType('role_' . $role->id
, PARAM_TEXT
);
335 if (core_tag_tag
::is_enabled('core', 'course') &&
336 ((empty($course->id
) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
337 ||
(!empty($course->id
) && has_capability('moodle/course:tag', $coursecontext)))) {
338 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
339 $mform->addElement('tags', 'tags', get_string('tags'),
340 array('itemtype' => 'course', 'component' => 'core'));
343 // Add custom fields to the form.
344 $handler = core_course\customfield\course_handler
::create();
345 $handler->set_parent_context($categorycontext); // For course handler only.
346 $handler->instance_form_definition($mform, empty($course->id
) ?
0 : $course->id
);
348 // When two elements we need a group.
349 $buttonarray = array();
350 $classarray = array('class' => 'form-submit');
351 if ($returnto !== 0) {
352 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
354 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
355 $buttonarray[] = &$mform->createElement('cancel');
356 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
357 $mform->closeHeaderBefore('buttonar');
359 $mform->addElement('hidden', 'id', null);
360 $mform->setType('id', PARAM_INT
);
362 // Prepare custom fields data.
363 $handler->instance_form_before_set_data($course);
364 // Finally set the current form data
365 $this->set_data($course);
369 * Fill in the current page data for this course.
371 function definition_after_data() {
374 $mform = $this->_form
;
376 // add available groupings
377 $courseid = $mform->getElementValue('id');
378 if ($courseid and $mform->elementExists('defaultgroupingid')) {
380 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
381 foreach ($groupings as $grouping) {
382 $options[$grouping->id
] = format_string($grouping->name
);
385 core_collator
::asort($options);
386 $gr_el =& $mform->getElement('defaultgroupingid');
387 $gr_el->load($options);
390 // add course format options
391 $formatvalue = $mform->getElementValue('format');
392 if (is_array($formatvalue) && !empty($formatvalue)) {
394 $params = array('format' => $formatvalue[0]);
395 // Load the course as well if it is available, course formats may need it to work out
396 // they preferred course end date.
398 $params['id'] = $courseid;
400 $courseformat = course_get_format((object)$params);
402 $elements = $courseformat->create_edit_form_elements($mform);
403 for ($i = 0; $i < count($elements); $i++
) {
404 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
405 'addcourseformatoptionshere');
408 // Remove newsitems element if format does not support news.
409 if (!$courseformat->supports_news()) {
410 $mform->removeElement('newsitems');
414 // Tweak the form with values provided by custom fields in use.
415 $handler = core_course\customfield\course_handler
::create();
416 $handler->instance_form_definition_after_data($mform, empty($courseid) ?
0 : $courseid);
423 * @param array $files
424 * @return array the errors that were found
426 function validation($data, $files) {
429 $errors = parent
::validation($data, $files);
431 // Add field validation check for duplicate shortname.
432 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE
)) {
433 if (empty($data['id']) ||
$course->id
!= $data['id']) {
434 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname
);
438 // Add field validation check for duplicate idnumber.
439 if (!empty($data['idnumber']) && (empty($data['id']) ||
$this->course
->idnumber
!= $data['idnumber'])) {
440 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE
)) {
441 if (empty($data['id']) ||
$course->id
!= $data['id']) {
442 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname
);
447 if ($errorcode = course_validate_dates($data)) {
448 $errors['enddate'] = get_string($errorcode, 'error');
451 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context
));
453 $courseformat = course_get_format((object)array('format' => $data['format']));
454 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
455 if (!empty($formaterrors) && is_array($formaterrors)) {
456 $errors = array_merge($errors, $formaterrors);
459 // Add the custom fields validation.
460 $handler = core_course\customfield\course_handler
::create();
461 $errors = array_merge($errors, $handler->instance_form_validation($data, $files));