Merge branch 'install_37_STABLE' of https://git.in.moodle.com/amosbot/moodle-install...
[moodle.git] / course / edit_form.php
blob1f42e6c27bad49f6bad1211dea49c43f98c1d5b6
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->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;
37 } else {
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);
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('select', 'category', get_string('coursecategory'), $displaylist);
97 $mform->addHelpButton('category', 'coursecategory');
98 } else {
99 //keep current
100 $mform->addElement('hidden', 'category', null);
101 $mform->setType('category', PARAM_INT);
102 $mform->setConstant('category', $course->category);
106 $choices = array();
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);
117 } else {
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 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
133 $mform->addHelpButton('idnumber', 'idnumbercourse');
134 $mform->setType('idnumber', PARAM_RAW);
135 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
136 $mform->hardFreeze('idnumber');
137 $mform->setConstants('idnumber', $course->idnumber);
140 // Description.
141 $mform->addElement('header', 'descriptionhdr', get_string('description'));
142 $mform->setExpanded('descriptionhdr');
144 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
145 $mform->addHelpButton('summary_editor', 'coursesummary');
146 $mform->setType('summary_editor', PARAM_RAW);
147 $summaryfields = 'summary_editor';
149 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
150 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
151 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
152 $summaryfields .= ',overviewfiles_filemanager';
155 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
156 // Remove the description header it does not contain anything any more.
157 $mform->removeElement('descriptionhdr');
158 $mform->hardFreeze($summaryfields);
161 // Course format.
162 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
164 $courseformats = get_sorted_course_formats(true);
165 $formcourseformats = array();
166 foreach ($courseformats as $courseformat) {
167 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
169 if (isset($course->format)) {
170 $course->format = course_get_format($course)->get_format(); // replace with default if not found
171 if (!in_array($course->format, $courseformats)) {
172 // this format is disabled. Still display it in the dropdown
173 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
174 get_string('pluginname', 'format_'.$course->format));
178 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
179 $mform->addHelpButton('format', 'format');
180 $mform->setDefault('format', $courseconfig->format);
182 // Button to update format-specific options on format change (will be hidden by JavaScript).
183 $mform->registerNoSubmitButton('updatecourseformat');
184 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
186 // Just a placeholder for the course format options.
187 $mform->addElement('hidden', 'addcourseformatoptionshere');
188 $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
190 // Appearance.
191 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
193 if (!empty($CFG->allowcoursethemes)) {
194 $themeobjects = get_list_of_themes();
195 $themes=array();
196 $themes[''] = get_string('forceno');
197 foreach ($themeobjects as $key=>$theme) {
198 if (empty($theme->hidefromselector)) {
199 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
202 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
205 $languages=array();
206 $languages[''] = get_string('forceno');
207 $languages += get_string_manager()->get_list_of_translations();
208 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
209 || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
210 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
211 $mform->setDefault('lang', $courseconfig->lang);
214 // Multi-Calendar Support - see MDL-18375.
215 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
216 // We do not want to show this option unless there is more than one calendar type to display.
217 if (count($calendartypes) > 1) {
218 $calendars = array();
219 $calendars[''] = get_string('forceno');
220 $calendars += $calendartypes;
221 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
224 $options = range(0, 10);
225 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
226 $courseconfig = get_config('moodlecourse');
227 $mform->setDefault('newsitems', $courseconfig->newsitems);
228 $mform->addHelpButton('newsitems', 'newsitemsnumber');
230 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
231 $mform->addHelpButton('showgrades', 'showgrades');
232 $mform->setDefault('showgrades', $courseconfig->showgrades);
234 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
235 $mform->addHelpButton('showreports', 'showreports');
236 $mform->setDefault('showreports', $courseconfig->showreports);
238 // Files and uploads.
239 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
241 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
242 if (empty($course->legacyfiles)) {
243 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
244 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
245 } else {
246 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
248 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
249 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
250 if (!isset($courseconfig->legacyfiles)) {
251 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
252 $courseconfig->legacyfiles = 0;
254 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
257 // Handle non-existing $course->maxbytes on course creation.
258 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
260 // Let's prepare the maxbytes popup.
261 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
262 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
263 $mform->addHelpButton('maxbytes', 'maximumupload');
264 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
266 // Completion tracking.
267 if (completion_info::is_enabled_for_site()) {
268 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
269 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
270 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
271 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
272 } else {
273 $mform->addElement('hidden', 'enablecompletion');
274 $mform->setType('enablecompletion', PARAM_INT);
275 $mform->setDefault('enablecompletion', 0);
278 enrol_course_edit_form($mform, $course, $context);
280 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
282 $choices = array();
283 $choices[NOGROUPS] = get_string('groupsnone', 'group');
284 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
285 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
286 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
287 $mform->addHelpButton('groupmode', 'groupmode', 'group');
288 $mform->setDefault('groupmode', $courseconfig->groupmode);
290 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
291 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
292 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
294 //default groupings selector
295 $options = array();
296 $options[0] = get_string('none');
297 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
299 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
300 || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
301 // Customizable role names in this course.
302 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
303 $mform->addHelpButton('rolerenaming', 'rolerenaming');
305 if ($roles = get_all_roles()) {
306 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
307 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
308 foreach ($roles as $role) {
309 $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
310 $mform->setType('role_' . $role->id, PARAM_TEXT);
315 if (core_tag_tag::is_enabled('core', 'course') &&
316 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
317 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
318 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
319 $mform->addElement('tags', 'tags', get_string('tags'),
320 array('itemtype' => 'course', 'component' => 'core'));
323 // Add custom fields to the form.
324 $handler = core_course\customfield\course_handler::create();
325 $handler->set_parent_context($categorycontext); // For course handler only.
326 $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
328 // When two elements we need a group.
329 $buttonarray = array();
330 $classarray = array('class' => 'form-submit');
331 if ($returnto !== 0) {
332 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
334 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
335 $buttonarray[] = &$mform->createElement('cancel');
336 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
337 $mform->closeHeaderBefore('buttonar');
339 $mform->addElement('hidden', 'id', null);
340 $mform->setType('id', PARAM_INT);
342 // Prepare custom fields data.
343 $handler->instance_form_before_set_data($course);
344 // Finally set the current form data
345 $this->set_data($course);
349 * Fill in the current page data for this course.
351 function definition_after_data() {
352 global $DB;
354 $mform = $this->_form;
356 // add available groupings
357 $courseid = $mform->getElementValue('id');
358 if ($courseid and $mform->elementExists('defaultgroupingid')) {
359 $options = array();
360 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
361 foreach ($groupings as $grouping) {
362 $options[$grouping->id] = format_string($grouping->name);
365 core_collator::asort($options);
366 $gr_el =& $mform->getElement('defaultgroupingid');
367 $gr_el->load($options);
370 // add course format options
371 $formatvalue = $mform->getElementValue('format');
372 if (is_array($formatvalue) && !empty($formatvalue)) {
374 $params = array('format' => $formatvalue[0]);
375 // Load the course as well if it is available, course formats may need it to work out
376 // they preferred course end date.
377 if ($courseid) {
378 $params['id'] = $courseid;
380 $courseformat = course_get_format((object)$params);
382 $elements = $courseformat->create_edit_form_elements($mform);
383 for ($i = 0; $i < count($elements); $i++) {
384 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
385 'addcourseformatoptionshere');
388 // Remove newsitems element if format does not support news.
389 if (!$courseformat->supports_news()) {
390 $mform->removeElement('newsitems');
394 // Tweak the form with values provided by custom fields in use.
395 $handler = core_course\customfield\course_handler::create();
396 $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
400 * Validation.
402 * @param array $data
403 * @param array $files
404 * @return array the errors that were found
406 function validation($data, $files) {
407 global $DB;
409 $errors = parent::validation($data, $files);
411 // Add field validation check for duplicate shortname.
412 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
413 if (empty($data['id']) || $course->id != $data['id']) {
414 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
418 // Add field validation check for duplicate idnumber.
419 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
420 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
421 if (empty($data['id']) || $course->id != $data['id']) {
422 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
427 if ($errorcode = course_validate_dates($data)) {
428 $errors['enddate'] = get_string($errorcode, 'error');
431 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
433 $courseformat = course_get_format((object)array('format' => $data['format']));
434 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
435 if (!empty($formaterrors) && is_array($formaterrors)) {
436 $errors = array_merge($errors, $formaterrors);
439 // Add the custom fields validation.
440 $handler = core_course\customfield\course_handler::create();
441 $errors = array_merge($errors, $handler->instance_form_validation($data, $files));
443 return $errors;