MDL-70781 course: setting for displaying completion conditions
[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('autocomplete', '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('autocomplete', '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         }
124         // Download course content.
125         if ($CFG->downloadcoursecontentallowed) {
126             $downloadchoices = [
127                 DOWNLOAD_COURSE_CONTENT_DISABLED => get_string('no'),
128                 DOWNLOAD_COURSE_CONTENT_ENABLED => get_string('yes'),
129             ];
130             $sitedefaultstring = $downloadchoices[$courseconfig->downloadcontentsitedefault];
131             $downloadchoices[DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT] = get_string('sitedefaultspecified', '', $sitedefaultstring);
132             $downloadselectdefault = $courseconfig->downloadcontent ?? DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT;
134             $mform->addElement('select', 'downloadcontent', get_string('enabledownloadcoursecontent', 'course'), $downloadchoices);
135             $mform->addHelpButton('downloadcontent', 'downloadcoursecontent', 'course');
136             $mform->setDefault('downloadcontent', $downloadselectdefault);
138             if ((!empty($course->id) && !has_capability('moodle/course:configuredownloadcontent', $coursecontext)) ||
139                     (empty($course->id) &&
140                     !guess_if_creator_will_have_course_capability('moodle/course:configuredownloadcontent', $categorycontext))) {
141                 $mform->hardFreeze('downloadcontent');
142                 $mform->setConstant('downloadcontent', $downloadselectdefault);
143             }
144         }
146         $mform->addElement('date_time_selector', 'startdate', get_string('startdate'));
147         $mform->addHelpButton('startdate', 'startdate');
148         $date = (new DateTime())->setTimestamp(usergetmidnight(time()));
149         $date->modify('+1 day');
150         $mform->setDefault('startdate', $date->getTimestamp());
152         $mform->addElement('date_time_selector', 'enddate', get_string('enddate'), array('optional' => true));
153         $mform->addHelpButton('enddate', 'enddate');
155         if (!empty($CFG->enablecourserelativedates)) {
156             $attributes = [
157                 'aria-describedby' => 'relativedatesmode_warning'
158             ];
159             if (!empty($course->id)) {
160                 $attributes['disabled'] = true;
161             }
162             $relativeoptions = [
163                 0 => get_string('no'),
164                 1 => get_string('yes'),
165             ];
166             $relativedatesmodegroup = [];
167             $relativedatesmodegroup[] = $mform->createElement('select', 'relativedatesmode', get_string('relativedatesmode'),
168                 $relativeoptions, $attributes);
169             $relativedatesmodegroup[] = $mform->createElement('html', html_writer::span(get_string('relativedatesmode_warning'),
170                 '', ['id' => 'relativedatesmode_warning']));
171             $mform->addGroup($relativedatesmodegroup, 'relativedatesmodegroup', get_string('relativedatesmode'), null, false);
172             $mform->addHelpButton('relativedatesmodegroup', 'relativedatesmode');
173         }
175         $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
176         $mform->addHelpButton('idnumber', 'idnumbercourse');
177         $mform->setType('idnumber', PARAM_RAW);
178         if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
179             $mform->hardFreeze('idnumber');
180             $mform->setConstants('idnumber', $course->idnumber);
181         }
183         // Description.
184         $mform->addElement('header', 'descriptionhdr', get_string('description'));
185         $mform->setExpanded('descriptionhdr');
187         $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
188         $mform->addHelpButton('summary_editor', 'coursesummary');
189         $mform->setType('summary_editor', PARAM_RAW);
190         $summaryfields = 'summary_editor';
192         if ($overviewfilesoptions = course_overviewfiles_options($course)) {
193             $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
194             $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
195             $summaryfields .= ',overviewfiles_filemanager';
196         }
198         if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
199             // Remove the description header it does not contain anything any more.
200             $mform->removeElement('descriptionhdr');
201             $mform->hardFreeze($summaryfields);
202         }
204         // Course format.
205         $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
207         $courseformats = get_sorted_course_formats(true);
208         $formcourseformats = array();
209         foreach ($courseformats as $courseformat) {
210             $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
211         }
212         if (isset($course->format)) {
213             $course->format = course_get_format($course)->get_format(); // replace with default if not found
214             if (!in_array($course->format, $courseformats)) {
215                 // this format is disabled. Still display it in the dropdown
216                 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
217                         get_string('pluginname', 'format_'.$course->format));
218             }
219         }
221         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
222         $mform->addHelpButton('format', 'format');
223         $mform->setDefault('format', $courseconfig->format);
225         // Button to update format-specific options on format change (will be hidden by JavaScript).
226         $mform->registerNoSubmitButton('updatecourseformat');
227         $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
229         // Just a placeholder for the course format options.
230         $mform->addElement('hidden', 'addcourseformatoptionshere');
231         $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
233         // Appearance.
234         $mform->addElement('header', 'appearancehdr', get_string('appearance'));
236         if (!empty($CFG->allowcoursethemes)) {
237             $themeobjects = get_list_of_themes();
238             $themes=array();
239             $themes[''] = get_string('forceno');
240             foreach ($themeobjects as $key=>$theme) {
241                 if (empty($theme->hidefromselector)) {
242                     $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
243                 }
244             }
245             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
246         }
248         $languages=array();
249         $languages[''] = get_string('forceno');
250         $languages += get_string_manager()->get_list_of_translations();
251         if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $categorycontext))
252                 || (!empty($course->id) && has_capability('moodle/course:setforcedlanguage', $coursecontext))) {
253             $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
254             $mform->setDefault('lang', $courseconfig->lang);
255         }
257         // Multi-Calendar Support - see MDL-18375.
258         $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
259         // We do not want to show this option unless there is more than one calendar type to display.
260         if (count($calendartypes) > 1) {
261             $calendars = array();
262             $calendars[''] = get_string('forceno');
263             $calendars += $calendartypes;
264             $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
265         }
267         $options = range(0, 10);
268         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
269         $courseconfig = get_config('moodlecourse');
270         $mform->setDefault('newsitems', $courseconfig->newsitems);
271         $mform->addHelpButton('newsitems', 'newsitemsnumber');
273         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
274         $mform->addHelpButton('showgrades', 'showgrades');
275         $mform->setDefault('showgrades', $courseconfig->showgrades);
277         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
278         $mform->addHelpButton('showreports', 'showreports');
279         $mform->setDefault('showreports', $courseconfig->showreports);
281         // Show activity dates.
282         $mform->addElement('selectyesno', 'showactivitydates', get_string('showactivitydates'));
283         $mform->addHelpButton('showactivitydates', 'showactivitydates');
284         $mform->setDefault('showactivitydates', $courseconfig->showactivitydates);
286         // Files and uploads.
287         $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
289         if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
290             if (empty($course->legacyfiles)) {
291                 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
292                 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
293             } else {
294                 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
295             }
296             $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
297             $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
298             if (!isset($courseconfig->legacyfiles)) {
299                 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
300                 $courseconfig->legacyfiles = 0;
301             }
302             $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
303         }
305         // Handle non-existing $course->maxbytes on course creation.
306         $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
308         // Let's prepare the maxbytes popup.
309         $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
310         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
311         $mform->addHelpButton('maxbytes', 'maximumupload');
312         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
314         // Completion tracking.
315         if (completion_info::is_enabled_for_site()) {
316             $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
317             $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
318             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
319             $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
321             $showcompletionconditions = $courseconfig->showcompletionconditions ?? COMPLETION_SHOW_CONDITIONS;
322             $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion'));
323             $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion');
324             $mform->setDefault('showcompletionconditions', $showcompletionconditions);
325             $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_HIDE_CONDITIONS);
326         } else {
327             $mform->addElement('hidden', 'enablecompletion');
328             $mform->setType('enablecompletion', PARAM_INT);
329             $mform->setDefault('enablecompletion', 0);
330         }
332         enrol_course_edit_form($mform, $course, $context);
334         $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
336         $choices = array();
337         $choices[NOGROUPS] = get_string('groupsnone', 'group');
338         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
339         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
340         $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
341         $mform->addHelpButton('groupmode', 'groupmode', 'group');
342         $mform->setDefault('groupmode', $courseconfig->groupmode);
344         $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
345         $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
346         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
348         //default groupings selector
349         $options = array();
350         $options[0] = get_string('none');
351         $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
353         if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
354                 || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
355             // Customizable role names in this course.
356             $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
357             $mform->addHelpButton('rolerenaming', 'rolerenaming');
359             if ($roles = get_all_roles()) {
360                 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
361                 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
362                 foreach ($roles as $role) {
363                     $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
364                     $mform->setType('role_' . $role->id, PARAM_TEXT);
365                 }
366             }
367         }
369         if (core_tag_tag::is_enabled('core', 'course') &&
370                 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
371                 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
372             $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
373             $mform->addElement('tags', 'tags', get_string('tags'),
374                     array('itemtype' => 'course', 'component' => 'core'));
375         }
377         // Add custom fields to the form.
378         $handler = core_course\customfield\course_handler::create();
379         $handler->set_parent_context($categorycontext); // For course handler only.
380         $handler->instance_form_definition($mform, empty($course->id) ? 0 : $course->id);
382         // When two elements we need a group.
383         $buttonarray = array();
384         $classarray = array('class' => 'form-submit');
385         if ($returnto !== 0) {
386             $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
387         }
388         $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
389         $buttonarray[] = &$mform->createElement('cancel');
390         $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
391         $mform->closeHeaderBefore('buttonar');
393         $mform->addElement('hidden', 'id', null);
394         $mform->setType('id', PARAM_INT);
396         // Prepare custom fields data.
397         $handler->instance_form_before_set_data($course);
398         // Finally set the current form data
399         $this->set_data($course);
400     }
402     /**
403      * Fill in the current page data for this course.
404      */
405     function definition_after_data() {
406         global $DB;
408         $mform = $this->_form;
410         // add available groupings
411         $courseid = $mform->getElementValue('id');
412         if ($courseid and $mform->elementExists('defaultgroupingid')) {
413             $options = array();
414             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
415                 foreach ($groupings as $grouping) {
416                     $options[$grouping->id] = format_string($grouping->name);
417                 }
418             }
419             core_collator::asort($options);
420             $gr_el =& $mform->getElement('defaultgroupingid');
421             $gr_el->load($options);
422         }
424         // add course format options
425         $formatvalue = $mform->getElementValue('format');
426         if (is_array($formatvalue) && !empty($formatvalue)) {
428             $params = array('format' => $formatvalue[0]);
429             // Load the course as well if it is available, course formats may need it to work out
430             // they preferred course end date.
431             if ($courseid) {
432                 $params['id'] = $courseid;
433             }
434             $courseformat = course_get_format((object)$params);
436             $elements = $courseformat->create_edit_form_elements($mform);
437             for ($i = 0; $i < count($elements); $i++) {
438                 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
439                         'addcourseformatoptionshere');
440             }
442             // Remove newsitems element if format does not support news.
443             if (!$courseformat->supports_news()) {
444                 $mform->removeElement('newsitems');
445             }
446         }
448         // Tweak the form with values provided by custom fields in use.
449         $handler  = core_course\customfield\course_handler::create();
450         $handler->instance_form_definition_after_data($mform, empty($courseid) ? 0 : $courseid);
451     }
453     /**
454      * Validation.
455      *
456      * @param array $data
457      * @param array $files
458      * @return array the errors that were found
459      */
460     function validation($data, $files) {
461         global $DB;
463         $errors = parent::validation($data, $files);
465         // Add field validation check for duplicate shortname.
466         if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
467             if (empty($data['id']) || $course->id != $data['id']) {
468                 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
469             }
470         }
472         // Add field validation check for duplicate idnumber.
473         if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
474             if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
475                 if (empty($data['id']) || $course->id != $data['id']) {
476                     $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
477                 }
478             }
479         }
481         if ($errorcode = course_validate_dates($data)) {
482             $errors['enddate'] = get_string($errorcode, 'error');
483         }
485         $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
487         $courseformat = course_get_format((object)array('format' => $data['format']));
488         $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
489         if (!empty($formaterrors) && is_array($formaterrors)) {
490             $errors = array_merge($errors, $formaterrors);
491         }
493         // Add the custom fields validation.
494         $handler = core_course\customfield\course_handler::create();
495         $errors  = array_merge($errors, $handler->instance_form_validation($data, $files));
497         return $errors;
498     }