Merge branch 'MDL-18375_master' of git://github.com/markn86/moodle
[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');
7 require_once($CFG->libdir. '/coursecatlib.php');
9 /**
10  * The form for handling editing a course.
11  */
12 class course_edit_form extends moodleform {
13     protected $course;
14     protected $context;
16     /**
17      * Form definition.
18      */
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;
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('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);
61         }
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);
70         }
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);
83             }
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();
90                 }
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);
98             }
99         }
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);
111             }
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);
116             }
117         }
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);
129         }
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';
144         }
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);
150         }
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");
159         }
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));
166             }
167         }
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);
191                 }
192             }
193             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
194         }
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);
210         }
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'));
234             }
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;
240             }
241             $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
242         }
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);
263         }
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);
296             }
297         }
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);
306     }
308     /**
309      * Fill in the current page data for this course.
310      */
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);
322                 }
323             }
324             $gr_el =& $mform->getElement('defaultgroupingid');
325             $gr_el->load($options);
326         }
328         // add course format options
329         $formatvalue = $mform->getElementValue('format');
330         if (is_array($formatvalue) && !empty($formatvalue)) {
331             $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
333             $elements = $courseformat->create_edit_form_elements($mform);
334             for ($i = 0; $i < count($elements); $i++) {
335                 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
336                         'addcourseformatoptionshere');
337             }
338         }
339     }
341     /**
342      * Validation.
343      *
344      * @param array $data
345      * @param array $files
346      * @return array the errors that were found
347      */
348     function validation($data, $files) {
349         global $DB;
351         $errors = parent::validation($data, $files);
353         // Add field validation check for duplicate shortname.
354         if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
355             if (empty($data['id']) || $course->id != $data['id']) {
356                 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
357             }
358         }
360         // Add field validation check for duplicate idnumber.
361         if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
362             if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
363                 if (empty($data['id']) || $course->id != $data['id']) {
364                     $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
365                 }
366             }
367         }
369         $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
371         $courseformat = course_get_format((object)array('format' => $data['format']));
372         $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
373         if (!empty($formaterrors) && is_array($formaterrors)) {
374             $errors = array_merge($errors, $formaterrors);
375         }
377         return $errors;
378     }