MDL-56110 forms: debug message for php 7.1 compat
[moodle.git] / course / edit_form.php
blobc549c9e1028890f09b8bbbb9755141b860936095
1 <?php
3 defined('MOODLE_INTERNAL') || die;
5 require_once($CFG->libdir.'/formslib.php');
6 require_once($CFG->libdir.'/completionlib.php');
7 require_once($CFG->libdir. '/coursecatlib.php');
9 /**
10 * The form for handling editing a course.
12 class course_edit_form extends moodleform {
13 protected $course;
14 protected $context;
16 /**
17 * Form definition.
19 function definition() {
20 global $CFG, $PAGE;
22 $mform = $this->_form;
23 $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
24 array(array('formid' => $mform->getAttribute('id'))));
26 $course = $this->_customdata['course']; // this contains the data of this form
27 $category = $this->_customdata['category'];
28 $editoroptions = $this->_customdata['editoroptions'];
29 $returnto = $this->_customdata['returnto'];
30 $returnurl = $this->_customdata['returnurl'];
32 $systemcontext = context_system::instance();
33 $categorycontext = context_coursecat::instance($category->id);
35 if (!empty($course->id)) {
36 $coursecontext = context_course::instance($course->id);
37 $context = $coursecontext;
38 } else {
39 $coursecontext = null;
40 $context = $categorycontext;
43 $courseconfig = get_config('moodlecourse');
45 $this->course = $course;
46 $this->context = $context;
48 // Form definition with new course defaults.
49 $mform->addElement('header','general', get_string('general', 'form'));
51 $mform->addElement('hidden', 'returnto', null);
52 $mform->setType('returnto', PARAM_ALPHANUM);
53 $mform->setConstant('returnto', $returnto);
55 $mform->addElement('hidden', 'returnurl', null);
56 $mform->setType('returnurl', PARAM_LOCALURL);
57 $mform->setConstant('returnurl', $returnurl);
59 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
60 $mform->addHelpButton('fullname', 'fullnamecourse');
61 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
62 $mform->setType('fullname', PARAM_TEXT);
63 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
64 $mform->hardFreeze('fullname');
65 $mform->setConstant('fullname', $course->fullname);
68 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
69 $mform->addHelpButton('shortname', 'shortnamecourse');
70 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
71 $mform->setType('shortname', PARAM_TEXT);
72 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
73 $mform->hardFreeze('shortname');
74 $mform->setConstant('shortname', $course->shortname);
77 // Verify permissions to change course category or keep current.
78 if (empty($course->id)) {
79 if (has_capability('moodle/course:create', $categorycontext)) {
80 $displaylist = coursecat::make_categories_list('moodle/course:create');
81 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
82 $mform->addHelpButton('category', 'coursecategory');
83 $mform->setDefault('category', $category->id);
84 } else {
85 $mform->addElement('hidden', 'category', null);
86 $mform->setType('category', PARAM_INT);
87 $mform->setConstant('category', $category->id);
89 } else {
90 if (has_capability('moodle/course:changecategory', $coursecontext)) {
91 $displaylist = coursecat::make_categories_list('moodle/course:create');
92 if (!isset($displaylist[$course->category])) {
93 //always keep current
94 $displaylist[$course->category] = coursecat::get($course->category, MUST_EXIST, true)->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('visible'), $choices);
110 $mform->addHelpButton('visible', 'visible');
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);
124 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
125 $mform->addHelpButton('startdate', 'startdate');
126 $mform->setDefault('startdate', time() + 3600 * 24);
128 $mform->addElement('date_selector', 'enddate', get_string('enddate'), array('optional' => true));
129 $mform->addHelpButton('enddate', 'enddate');
131 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
132 $mform->addHelpButton('idnumber', 'idnumbercourse');
133 $mform->setType('idnumber', PARAM_RAW);
134 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
135 $mform->hardFreeze('idnumber');
136 $mform->setConstants('idnumber', $course->idnumber);
139 // Description.
140 $mform->addElement('header', 'descriptionhdr', get_string('description'));
141 $mform->setExpanded('descriptionhdr');
143 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
144 $mform->addHelpButton('summary_editor', 'coursesummary');
145 $mform->setType('summary_editor', PARAM_RAW);
146 $summaryfields = 'summary_editor';
148 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
149 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
150 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
151 $summaryfields .= ',overviewfiles_filemanager';
154 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
155 // Remove the description header it does not contain anything any more.
156 $mform->removeElement('descriptionhdr');
157 $mform->hardFreeze($summaryfields);
160 // Course format.
161 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
163 $courseformats = get_sorted_course_formats(true);
164 $formcourseformats = array();
165 foreach ($courseformats as $courseformat) {
166 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
168 if (isset($course->format)) {
169 $course->format = course_get_format($course)->get_format(); // replace with default if not found
170 if (!in_array($course->format, $courseformats)) {
171 // this format is disabled. Still display it in the dropdown
172 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
173 get_string('pluginname', 'format_'.$course->format));
177 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
178 $mform->addHelpButton('format', 'format');
179 $mform->setDefault('format', $courseconfig->format);
181 // Button to update format-specific options on format change (will be hidden by JavaScript).
182 $mform->registerNoSubmitButton('updatecourseformat');
183 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
185 // Just a placeholder for the course format options.
186 $mform->addElement('hidden', 'addcourseformatoptionshere');
187 $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
189 // Appearance.
190 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
192 if (!empty($CFG->allowcoursethemes)) {
193 $themeobjects = get_list_of_themes();
194 $themes=array();
195 $themes[''] = get_string('forceno');
196 foreach ($themeobjects as $key=>$theme) {
197 if (empty($theme->hidefromselector)) {
198 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
201 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
204 $languages=array();
205 $languages[''] = get_string('forceno');
206 $languages += get_string_manager()->get_list_of_translations();
207 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
208 $mform->setDefault('lang', $courseconfig->lang);
210 // Multi-Calendar Support - see MDL-18375.
211 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
212 // We do not want to show this option unless there is more than one calendar type to display.
213 if (count($calendartypes) > 1) {
214 $calendars = array();
215 $calendars[''] = get_string('forceno');
216 $calendars += $calendartypes;
217 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
220 $options = range(0, 10);
221 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
222 $mform->addHelpButton('newsitems', 'newsitemsnumber');
223 $mform->setDefault('newsitems', $courseconfig->newsitems);
225 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
226 $mform->addHelpButton('showgrades', 'showgrades');
227 $mform->setDefault('showgrades', $courseconfig->showgrades);
229 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
230 $mform->addHelpButton('showreports', 'showreports');
231 $mform->setDefault('showreports', $courseconfig->showreports);
233 // Files and uploads.
234 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
236 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
237 if (empty($course->legacyfiles)) {
238 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
239 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
240 } else {
241 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
243 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
244 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
245 if (!isset($courseconfig->legacyfiles)) {
246 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
247 $courseconfig->legacyfiles = 0;
249 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
252 // Handle non-existing $course->maxbytes on course creation.
253 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
255 // Let's prepare the maxbytes popup.
256 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
257 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
258 $mform->addHelpButton('maxbytes', 'maximumupload');
259 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
261 // Completion tracking.
262 if (completion_info::is_enabled_for_site()) {
263 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
264 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
265 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
266 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
267 } else {
268 $mform->addElement('hidden', 'enablecompletion');
269 $mform->setType('enablecompletion', PARAM_INT);
270 $mform->setDefault('enablecompletion', 0);
273 enrol_course_edit_form($mform, $course, $context);
275 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
277 $choices = array();
278 $choices[NOGROUPS] = get_string('groupsnone', 'group');
279 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
280 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
281 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
282 $mform->addHelpButton('groupmode', 'groupmode', 'group');
283 $mform->setDefault('groupmode', $courseconfig->groupmode);
285 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
286 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
287 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
289 //default groupings selector
290 $options = array();
291 $options[0] = get_string('none');
292 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
294 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
295 || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
296 // Customizable role names in this course.
297 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
298 $mform->addHelpButton('rolerenaming', 'rolerenaming');
300 if ($roles = get_all_roles()) {
301 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
302 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
303 foreach ($roles as $role) {
304 $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
305 $mform->setType('role_' . $role->id, PARAM_TEXT);
310 if (core_tag_tag::is_enabled('core', 'course') &&
311 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
312 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
313 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
314 $mform->addElement('tags', 'tags', get_string('tags'),
315 array('itemtype' => 'course', 'component' => 'core'));
318 // When two elements we need a group.
319 $buttonarray = array();
320 $classarray = array('class' => 'form-submit');
321 if ($returnto !== 0) {
322 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
324 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
325 $buttonarray[] = &$mform->createElement('cancel');
326 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
327 $mform->closeHeaderBefore('buttonar');
329 $mform->addElement('hidden', 'id', null);
330 $mform->setType('id', PARAM_INT);
332 // Finally set the current form data
333 $this->set_data($course);
337 * Fill in the current page data for this course.
339 function definition_after_data() {
340 global $DB;
342 $mform = $this->_form;
344 // add available groupings
345 $courseid = $mform->getElementValue('id');
346 if ($courseid and $mform->elementExists('defaultgroupingid')) {
347 $options = array();
348 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
349 foreach ($groupings as $grouping) {
350 $options[$grouping->id] = format_string($grouping->name);
353 core_collator::asort($options);
354 $gr_el =& $mform->getElement('defaultgroupingid');
355 $gr_el->load($options);
358 // add course format options
359 $formatvalue = $mform->getElementValue('format');
360 if (is_array($formatvalue) && !empty($formatvalue)) {
362 $params = array('format' => $formatvalue[0]);
363 // Load the course as well if it is available, course formats may need it to work out
364 // they preferred course end date.
365 if ($courseid) {
366 $params['id'] = $courseid;
368 $courseformat = course_get_format((object)$params);
370 $elements = $courseformat->create_edit_form_elements($mform);
371 for ($i = 0; $i < count($elements); $i++) {
372 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
373 'addcourseformatoptionshere');
379 * Validation.
381 * @param array $data
382 * @param array $files
383 * @return array the errors that were found
385 function validation($data, $files) {
386 global $DB;
388 $errors = parent::validation($data, $files);
390 // Add field validation check for duplicate shortname.
391 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
392 if (empty($data['id']) || $course->id != $data['id']) {
393 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
397 // Add field validation check for duplicate idnumber.
398 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
399 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
400 if (empty($data['id']) || $course->id != $data['id']) {
401 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
406 if ($errorcode = course_validate_dates($data)) {
407 $errors['enddate'] = get_string($errorcode, 'error');
410 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
412 $courseformat = course_get_format((object)array('format' => $data['format']));
413 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
414 if (!empty($formaterrors) && is_array($formaterrors)) {
415 $errors = array_merge($errors, $formaterrors);
418 return $errors;