Merge branch 'MDL-47713-master' of git://github.com/damyon/moodle
[moodle.git] / course / edit_form.php
blob1de01c93539b6b28117cf0490c5cc3ded4c31cf9
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'];
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('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
55 $mform->addHelpButton('fullname', 'fullnamecourse');
56 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
57 $mform->setType('fullname', PARAM_TEXT);
58 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
59 $mform->hardFreeze('fullname');
60 $mform->setConstant('fullname', $course->fullname);
63 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
64 $mform->addHelpButton('shortname', 'shortnamecourse');
65 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
66 $mform->setType('shortname', PARAM_TEXT);
67 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
68 $mform->hardFreeze('shortname');
69 $mform->setConstant('shortname', $course->shortname);
72 // Verify permissions to change course category or keep current.
73 if (empty($course->id)) {
74 if (has_capability('moodle/course:create', $categorycontext)) {
75 $displaylist = coursecat::make_categories_list('moodle/course:create');
76 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
77 $mform->addHelpButton('category', 'coursecategory');
78 $mform->setDefault('category', $category->id);
79 } else {
80 $mform->addElement('hidden', 'category', null);
81 $mform->setType('category', PARAM_INT);
82 $mform->setConstant('category', $category->id);
84 } else {
85 if (has_capability('moodle/course:changecategory', $coursecontext)) {
86 $displaylist = coursecat::make_categories_list('moodle/course:create');
87 if (!isset($displaylist[$course->category])) {
88 //always keep current
89 $displaylist[$course->category] = coursecat::get($course->category, MUST_EXIST, true)->get_formatted_name();
91 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
92 $mform->addHelpButton('category', 'coursecategory');
93 } else {
94 //keep current
95 $mform->addElement('hidden', 'category', null);
96 $mform->setType('category', PARAM_INT);
97 $mform->setConstant('category', $course->category);
101 $choices = array();
102 $choices['0'] = get_string('hide');
103 $choices['1'] = get_string('show');
104 $mform->addElement('select', 'visible', get_string('visible'), $choices);
105 $mform->addHelpButton('visible', 'visible');
106 $mform->setDefault('visible', $courseconfig->visible);
107 if (!empty($course->id)) {
108 if (!has_capability('moodle/course:visibility', $coursecontext)) {
109 $mform->hardFreeze('visible');
110 $mform->setConstant('visible', $course->visible);
112 } else {
113 if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
114 $mform->hardFreeze('visible');
115 $mform->setConstant('visible', $courseconfig->visible);
119 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
120 $mform->addHelpButton('startdate', 'startdate');
121 $mform->setDefault('startdate', time() + 3600 * 24);
123 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
124 $mform->addHelpButton('idnumber', 'idnumbercourse');
125 $mform->setType('idnumber', PARAM_RAW);
126 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
127 $mform->hardFreeze('idnumber');
128 $mform->setConstants('idnumber', $course->idnumber);
131 // Description.
132 $mform->addElement('header', 'descriptionhdr', get_string('description'));
133 $mform->setExpanded('descriptionhdr');
135 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
136 $mform->addHelpButton('summary_editor', 'coursesummary');
137 $mform->setType('summary_editor', PARAM_RAW);
138 $summaryfields = 'summary_editor';
140 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
141 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
142 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
143 $summaryfields .= ',overviewfiles_filemanager';
146 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
147 // Remove the description header it does not contain anything any more.
148 $mform->removeElement('descriptionhdr');
149 $mform->hardFreeze($summaryfields);
152 // Course format.
153 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
155 $courseformats = get_sorted_course_formats(true);
156 $formcourseformats = array();
157 foreach ($courseformats as $courseformat) {
158 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
160 if (isset($course->format)) {
161 $course->format = course_get_format($course)->get_format(); // replace with default if not found
162 if (!in_array($course->format, $courseformats)) {
163 // this format is disabled. Still display it in the dropdown
164 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
165 get_string('pluginname', 'format_'.$course->format));
169 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
170 $mform->addHelpButton('format', 'format');
171 $mform->setDefault('format', $courseconfig->format);
173 // Button to update format-specific options on format change (will be hidden by JavaScript).
174 $mform->registerNoSubmitButton('updatecourseformat');
175 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
177 // Just a placeholder for the course format options.
178 $mform->addElement('hidden', 'addcourseformatoptionshere');
179 $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
181 // Appearance.
182 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
184 if (!empty($CFG->allowcoursethemes)) {
185 $themeobjects = get_list_of_themes();
186 $themes=array();
187 $themes[''] = get_string('forceno');
188 foreach ($themeobjects as $key=>$theme) {
189 if (empty($theme->hidefromselector)) {
190 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
193 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
196 $languages=array();
197 $languages[''] = get_string('forceno');
198 $languages += get_string_manager()->get_list_of_translations();
199 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
200 $mform->setDefault('lang', $courseconfig->lang);
202 // Multi-Calendar Support - see MDL-18375.
203 $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
204 // We do not want to show this option unless there is more than one calendar type to display.
205 if (count($calendartypes) > 1) {
206 $calendars = array();
207 $calendars[''] = get_string('forceno');
208 $calendars += $calendartypes;
209 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
212 $options = range(0, 10);
213 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
214 $mform->addHelpButton('newsitems', 'newsitemsnumber');
215 $mform->setDefault('newsitems', $courseconfig->newsitems);
217 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
218 $mform->addHelpButton('showgrades', 'showgrades');
219 $mform->setDefault('showgrades', $courseconfig->showgrades);
221 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
222 $mform->addHelpButton('showreports', 'showreports');
223 $mform->setDefault('showreports', $courseconfig->showreports);
225 // Files and uploads.
226 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
228 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
229 if (empty($course->legacyfiles)) {
230 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
231 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
232 } else {
233 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
235 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
236 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
237 if (!isset($courseconfig->legacyfiles)) {
238 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
239 $courseconfig->legacyfiles = 0;
241 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
244 // Handle non-existing $course->maxbytes on course creation.
245 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
247 // Let's prepare the maxbytes popup.
248 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
249 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
250 $mform->addHelpButton('maxbytes', 'maximumupload');
251 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
253 // Completion tracking.
254 if (completion_info::is_enabled_for_site()) {
255 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
256 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
257 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
258 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
259 } else {
260 $mform->addElement('hidden', 'enablecompletion');
261 $mform->setType('enablecompletion', PARAM_INT);
262 $mform->setDefault('enablecompletion', 0);
265 enrol_course_edit_form($mform, $course, $context);
267 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
269 $choices = array();
270 $choices[NOGROUPS] = get_string('groupsnone', 'group');
271 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
272 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
273 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
274 $mform->addHelpButton('groupmode', 'groupmode', 'group');
275 $mform->setDefault('groupmode', $courseconfig->groupmode);
277 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
278 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
279 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
281 //default groupings selector
282 $options = array();
283 $options[0] = get_string('none');
284 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
286 // Customizable role names in this course.
287 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
288 $mform->addHelpButton('rolerenaming', 'rolerenaming');
290 if ($roles = get_all_roles()) {
291 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
292 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
293 foreach ($roles as $role) {
294 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->localname));
295 $mform->setType('role_'.$role->id, PARAM_TEXT);
299 $this->add_action_buttons();
301 $mform->addElement('hidden', 'id', null);
302 $mform->setType('id', PARAM_INT);
304 // Finally set the current form data
305 $this->set_data($course);
309 * Fill in the current page data for this course.
311 function definition_after_data() {
312 global $DB;
314 $mform = $this->_form;
316 // add available groupings
317 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
318 $options = array();
319 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
320 foreach ($groupings as $grouping) {
321 $options[$grouping->id] = format_string($grouping->name);
324 core_collator::asort($options);
325 $gr_el =& $mform->getElement('defaultgroupingid');
326 $gr_el->load($options);
329 // add course format options
330 $formatvalue = $mform->getElementValue('format');
331 if (is_array($formatvalue) && !empty($formatvalue)) {
332 $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
334 $elements = $courseformat->create_edit_form_elements($mform);
335 for ($i = 0; $i < count($elements); $i++) {
336 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
337 'addcourseformatoptionshere');
343 * Validation.
345 * @param array $data
346 * @param array $files
347 * @return array the errors that were found
349 function validation($data, $files) {
350 global $DB;
352 $errors = parent::validation($data, $files);
354 // Add field validation check for duplicate shortname.
355 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
356 if (empty($data['id']) || $course->id != $data['id']) {
357 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
361 // Add field validation check for duplicate idnumber.
362 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
363 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
364 if (empty($data['id']) || $course->id != $data['id']) {
365 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
370 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
372 $courseformat = course_get_format((object)array('format' => $data['format']));
373 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
374 if (!empty($formaterrors) && is_array($formaterrors)) {
375 $errors = array_merge($errors, $formaterrors);
378 return $errors;