MDL-59212 analytics: Abstract equal parts and accum time splittings
[moodle.git] / course / edit_form.php
blob4c03e2db7610d9b25f9cf5ed51ccdad784155707
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('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);
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 $courseconfig = get_config('moodlecourse');
223 $mform->setDefault('newsitems', $courseconfig->newsitems);
224 $mform->addHelpButton('newsitems', 'newsitemsnumber');
226 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
227 $mform->addHelpButton('showgrades', 'showgrades');
228 $mform->setDefault('showgrades', $courseconfig->showgrades);
230 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
231 $mform->addHelpButton('showreports', 'showreports');
232 $mform->setDefault('showreports', $courseconfig->showreports);
234 // Files and uploads.
235 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
237 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
238 if (empty($course->legacyfiles)) {
239 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
240 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
241 } else {
242 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
244 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
245 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
246 if (!isset($courseconfig->legacyfiles)) {
247 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
248 $courseconfig->legacyfiles = 0;
250 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
253 // Handle non-existing $course->maxbytes on course creation.
254 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
256 // Let's prepare the maxbytes popup.
257 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
258 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
259 $mform->addHelpButton('maxbytes', 'maximumupload');
260 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
262 // Completion tracking.
263 if (completion_info::is_enabled_for_site()) {
264 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
265 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
266 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
267 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
268 } else {
269 $mform->addElement('hidden', 'enablecompletion');
270 $mform->setType('enablecompletion', PARAM_INT);
271 $mform->setDefault('enablecompletion', 0);
274 enrol_course_edit_form($mform, $course, $context);
276 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
278 $choices = array();
279 $choices[NOGROUPS] = get_string('groupsnone', 'group');
280 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
281 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
282 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
283 $mform->addHelpButton('groupmode', 'groupmode', 'group');
284 $mform->setDefault('groupmode', $courseconfig->groupmode);
286 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
287 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
288 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
290 //default groupings selector
291 $options = array();
292 $options[0] = get_string('none');
293 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
295 if ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
296 || (!empty($course->id) && has_capability('moodle/course:renameroles', $coursecontext))) {
297 // Customizable role names in this course.
298 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
299 $mform->addHelpButton('rolerenaming', 'rolerenaming');
301 if ($roles = get_all_roles()) {
302 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
303 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
304 foreach ($roles as $role) {
305 $mform->addElement('text', 'role_' . $role->id, get_string('yourwordforx', '', $role->localname));
306 $mform->setType('role_' . $role->id, PARAM_TEXT);
311 if (core_tag_tag::is_enabled('core', 'course') &&
312 ((empty($course->id) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
313 || (!empty($course->id) && has_capability('moodle/course:tag', $coursecontext)))) {
314 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
315 $mform->addElement('tags', 'tags', get_string('tags'),
316 array('itemtype' => 'course', 'component' => 'core'));
319 // When two elements we need a group.
320 $buttonarray = array();
321 $classarray = array('class' => 'form-submit');
322 if ($returnto !== 0) {
323 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
325 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
326 $buttonarray[] = &$mform->createElement('cancel');
327 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
328 $mform->closeHeaderBefore('buttonar');
330 $mform->addElement('hidden', 'id', null);
331 $mform->setType('id', PARAM_INT);
333 // Finally set the current form data
334 $this->set_data($course);
338 * Fill in the current page data for this course.
340 function definition_after_data() {
341 global $DB;
343 $mform = $this->_form;
345 // add available groupings
346 $courseid = $mform->getElementValue('id');
347 if ($courseid and $mform->elementExists('defaultgroupingid')) {
348 $options = array();
349 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
350 foreach ($groupings as $grouping) {
351 $options[$grouping->id] = format_string($grouping->name);
354 core_collator::asort($options);
355 $gr_el =& $mform->getElement('defaultgroupingid');
356 $gr_el->load($options);
359 // add course format options
360 $formatvalue = $mform->getElementValue('format');
361 if (is_array($formatvalue) && !empty($formatvalue)) {
363 $params = array('format' => $formatvalue[0]);
364 // Load the course as well if it is available, course formats may need it to work out
365 // they preferred course end date.
366 if ($courseid) {
367 $params['id'] = $courseid;
369 $courseformat = course_get_format((object)$params);
371 $elements = $courseformat->create_edit_form_elements($mform);
372 for ($i = 0; $i < count($elements); $i++) {
373 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
374 'addcourseformatoptionshere');
377 // Remove newsitems element if format does not support news.
378 if (!$courseformat->supports_news()) {
379 $mform->removeElement('newsitems');
385 * Validation.
387 * @param array $data
388 * @param array $files
389 * @return array the errors that were found
391 function validation($data, $files) {
392 global $DB;
394 $errors = parent::validation($data, $files);
396 // Add field validation check for duplicate shortname.
397 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
398 if (empty($data['id']) || $course->id != $data['id']) {
399 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
403 // Add field validation check for duplicate idnumber.
404 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
405 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
406 if (empty($data['id']) || $course->id != $data['id']) {
407 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
412 if ($errorcode = course_validate_dates($data)) {
413 $errors['enddate'] = get_string($errorcode, 'error');
416 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
418 $courseformat = course_get_format((object)array('format' => $data['format']));
419 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
420 if (!empty($formaterrors) && is_array($formaterrors)) {
421 $errors = array_merge($errors, $formaterrors);
424 return $errors;