MDL-67264 core_course: Activity chooser new feature
[moodle.git] / course / edit_form.php
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.
10  */
11 class course_edit_form extends moodleform {
12     protected $course;
13     protected $context;
15     /**
16      * Form definition.
17      */
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;
40         }
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);
65         }
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);
74         }
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);
87             }
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();
95                 }
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);
103             }
104         }
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);
116             }
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);
121             }
122         }
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)) {
133             $attributes = [
134                 'aria-describedby' => 'relativedatesmode_warning'
135             ];
136             if (!empty($course->id)) {
137                 $attributes['disabled'] = true;
138             }
139             $relativeoptions = [
140                 0 => get_string('no'),
141                 1 => get_string('yes'),
142             ];
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');
150         }
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);
158         }
160         // Description.
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';
173         }
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);
179         }
181         // Course format.
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");
188         }
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));
195             }
196         }
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);
210         // Appearance.
211         $mform->addElement('header', 'appearancehdr', get_string('appearance'));
213         if (!empty($CFG->allowcoursethemes)) {
214             $themeobjects = get_list_of_themes();
215             $themes=array();
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);
220                 }
221             }
222             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
223         }
225         $languages=array();
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);
232         }
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);
242         }
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'));
265             } else {
266                 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
267             }
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;
273             }
274             $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
275         }
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');
292         } else {
293             $mform->addElement('hidden', 'enablecompletion');
294             $mform->setType('enablecompletion', PARAM_INT);
295             $mform->setDefault('enablecompletion', 0);
296         }
298         enrol_course_edit_form($mform, $course, $context);
300         $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
302         $choices = array();
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
315         $options = array();
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);
331                 }
332             }
333         }
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'));
341         }
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);
353         }
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);
366     }
368     /**
369      * Fill in the current page data for this course.
370      */
371     function definition_after_data() {
372         global $DB;
374         $mform = $this->_form;
376         // add available groupings
377         $courseid = $mform->getElementValue('id');
378         if ($courseid and $mform->elementExists('defaultgroupingid')) {
379             $options = array();
380             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
381                 foreach ($groupings as $grouping) {
382                     $options[$grouping->id] = format_string($grouping->name);
383                 }
384             }
385             core_collator::asort($options);
386             $gr_el =& $mform->getElement('defaultgroupingid');
387             $gr_el->load($options);
388         }
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.
397             if ($courseid) {
398                 $params['id'] = $courseid;
399             }
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');
406             }
408             // Remove newsitems element if format does not support news.
409             if (!$courseformat->supports_news()) {
410                 $mform->removeElement('newsitems');
411             }
412         }
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);
417     }
419     /**
420      * Validation.
421      *
422      * @param array $data
423      * @param array $files
424      * @return array the errors that were found
425      */
426     function validation($data, $files) {
427         global $DB;
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);
435             }
436         }
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);
443                 }
444             }
445         }
447         if ($errorcode = course_validate_dates($data)) {
448             $errors['enddate'] = get_string($errorcode, 'error');
449         }
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);
457         }
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));
463         return $errors;
464     }