Merge branch 'MDL-54892-MOODLE_30_STABLE' of https://github.com/tobiasreischmann...
[moodle.git] / course / edit_form.php
blob6e0ed5062efd0d0b9781a6fe2e932abc78f3a5ff
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('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
129 $mform->addHelpButton('idnumber', 'idnumbercourse');
130 $mform->setType('idnumber', PARAM_RAW);
131 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
132 $mform->hardFreeze('idnumber');
133 $mform->setConstants('idnumber', $course->idnumber);
136 // Description.
137 $mform->addElement('header', 'descriptionhdr', get_string('description'));
138 $mform->setExpanded('descriptionhdr');
140 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
141 $mform->addHelpButton('summary_editor', 'coursesummary');
142 $mform->setType('summary_editor', PARAM_RAW);
143 $summaryfields = 'summary_editor';
145 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
146 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
147 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
148 $summaryfields .= ',overviewfiles_filemanager';
151 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
152 // Remove the description header it does not contain anything any more.
153 $mform->removeElement('descriptionhdr');
154 $mform->hardFreeze($summaryfields);
157 // Course format.
158 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
160 $courseformats = get_sorted_course_formats(true);
161 $formcourseformats = array();
162 foreach ($courseformats as $courseformat) {
163 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
165 if (isset($course->format)) {
166 $course->format = course_get_format($course)->get_format(); // replace with default if not found
167 if (!in_array($course->format, $courseformats)) {
168 // this format is disabled. Still display it in the dropdown
169 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
170 get_string('pluginname', 'format_'.$course->format));
174 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
175 $mform->addHelpButton('format', 'format');
176 $mform->setDefault('format', $courseconfig->format);
178 // Button to update format-specific options on format change (will be hidden by JavaScript).
179 $mform->registerNoSubmitButton('updatecourseformat');
180 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
182 // Just a placeholder for the course format options.
183 $mform->addElement('hidden', 'addcourseformatoptionshere');
184 $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
186 // Appearance.
187 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
189 if (!empty($CFG->allowcoursethemes)) {
190 $themeobjects = get_list_of_themes();
191 $themes=array();
192 $themes[''] = get_string('forceno');
193 foreach ($themeobjects as $key=>$theme) {
194 if (empty($theme->hidefromselector)) {
195 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
198 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
201 $languages=array();
202 $languages[''] = get_string('forceno');
203 $languages += get_string_manager()->get_list_of_translations();
204 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
205 $mform->setDefault('lang', $courseconfig->lang);
207 // Multi-Calendar Support - see MDL-18375.
208 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
209 // We do not want to show this option unless there is more than one calendar type to display.
210 if (count($calendartypes) > 1) {
211 $calendars = array();
212 $calendars[''] = get_string('forceno');
213 $calendars += $calendartypes;
214 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
217 $options = range(0, 10);
218 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
219 $mform->addHelpButton('newsitems', 'newsitemsnumber');
220 $mform->setDefault('newsitems', $courseconfig->newsitems);
222 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
223 $mform->addHelpButton('showgrades', 'showgrades');
224 $mform->setDefault('showgrades', $courseconfig->showgrades);
226 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
227 $mform->addHelpButton('showreports', 'showreports');
228 $mform->setDefault('showreports', $courseconfig->showreports);
230 // Files and uploads.
231 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
233 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
234 if (empty($course->legacyfiles)) {
235 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
236 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
237 } else {
238 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
240 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
241 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
242 if (!isset($courseconfig->legacyfiles)) {
243 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
244 $courseconfig->legacyfiles = 0;
246 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
249 // Handle non-existing $course->maxbytes on course creation.
250 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
252 // Let's prepare the maxbytes popup.
253 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
254 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
255 $mform->addHelpButton('maxbytes', 'maximumupload');
256 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
258 // Completion tracking.
259 if (completion_info::is_enabled_for_site()) {
260 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
261 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
262 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
263 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
264 } else {
265 $mform->addElement('hidden', 'enablecompletion');
266 $mform->setType('enablecompletion', PARAM_INT);
267 $mform->setDefault('enablecompletion', 0);
270 enrol_course_edit_form($mform, $course, $context);
272 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
274 $choices = array();
275 $choices[NOGROUPS] = get_string('groupsnone', 'group');
276 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
277 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
278 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
279 $mform->addHelpButton('groupmode', 'groupmode', 'group');
280 $mform->setDefault('groupmode', $courseconfig->groupmode);
282 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
283 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
284 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
286 //default groupings selector
287 $options = array();
288 $options[0] = get_string('none');
289 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
291 // Customizable role names in this course.
292 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
293 $mform->addHelpButton('rolerenaming', 'rolerenaming');
295 if ($roles = get_all_roles()) {
296 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
297 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
298 foreach ($roles as $role) {
299 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->localname));
300 $mform->setType('role_'.$role->id, PARAM_TEXT);
304 if (!empty($CFG->usetags) &&
305 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
306 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
307 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
308 $mform->addElement('tags', 'tags', get_string('tags'));
311 // When two elements we need a group.
312 $buttonarray = array();
313 $classarray = array('class' => 'form-submit');
314 if ($returnto !== 0) {
315 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
317 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
318 $buttonarray[] = &$mform->createElement('cancel');
319 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
320 $mform->closeHeaderBefore('buttonar');
322 $mform->addElement('hidden', 'id', null);
323 $mform->setType('id', PARAM_INT);
325 // Finally set the current form data
326 $this->set_data($course);
330 * Fill in the current page data for this course.
332 function definition_after_data() {
333 global $DB;
335 $mform = $this->_form;
337 // add available groupings
338 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
339 $options = array();
340 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
341 foreach ($groupings as $grouping) {
342 $options[$grouping->id] = format_string($grouping->name);
345 core_collator::asort($options);
346 $gr_el =& $mform->getElement('defaultgroupingid');
347 $gr_el->load($options);
350 // add course format options
351 $formatvalue = $mform->getElementValue('format');
352 if (is_array($formatvalue) && !empty($formatvalue)) {
353 $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
355 $elements = $courseformat->create_edit_form_elements($mform);
356 for ($i = 0; $i < count($elements); $i++) {
357 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
358 'addcourseformatoptionshere');
364 * Validation.
366 * @param array $data
367 * @param array $files
368 * @return array the errors that were found
370 function validation($data, $files) {
371 global $DB;
373 $errors = parent::validation($data, $files);
375 // Add field validation check for duplicate shortname.
376 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
377 if (empty($data['id']) || $course->id != $data['id']) {
378 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
382 // Add field validation check for duplicate idnumber.
383 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
384 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
385 if (empty($data['id']) || $course->id != $data['id']) {
386 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
391 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
393 $courseformat = course_get_format((object)array('format' => $data['format']));
394 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
395 if (!empty($formaterrors) && is_array($formaterrors)) {
396 $errors = array_merge($errors, $formaterrors);
399 return $errors;