MDL-43734: Prevent purging of all user session cache
[moodle.git] / course / edit_form.php
blobb14daec3726d8c076fd90fb1b15b56b3c2b57cd6
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 class course_edit_form extends moodleform {
10 protected $course;
11 protected $context;
13 function definition() {
14 global $USER, $CFG, $DB, $PAGE;
16 $mform = $this->_form;
17 $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
18 array(array('formid' => $mform->getAttribute('id'))));
20 $course = $this->_customdata['course']; // this contains the data of this form
21 $category = $this->_customdata['category'];
22 $editoroptions = $this->_customdata['editoroptions'];
23 $returnto = $this->_customdata['returnto'];
25 $systemcontext = context_system::instance();
26 $categorycontext = context_coursecat::instance($category->id);
28 if (!empty($course->id)) {
29 $coursecontext = context_course::instance($course->id);
30 $context = $coursecontext;
31 } else {
32 $coursecontext = null;
33 $context = $categorycontext;
36 $courseconfig = get_config('moodlecourse');
38 $this->course = $course;
39 $this->context = $context;
41 /// form definition with new course defaults
42 //--------------------------------------------------------------------------------
43 $mform->addElement('header','general', get_string('general', 'form'));
45 $mform->addElement('hidden', 'returnto', null);
46 $mform->setType('returnto', PARAM_ALPHANUM);
47 $mform->setConstant('returnto', $returnto);
49 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
50 $mform->addHelpButton('fullname', 'fullnamecourse');
51 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
52 $mform->setType('fullname', PARAM_TEXT);
53 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
54 $mform->hardFreeze('fullname');
55 $mform->setConstant('fullname', $course->fullname);
58 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
59 $mform->addHelpButton('shortname', 'shortnamecourse');
60 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
61 $mform->setType('shortname', PARAM_TEXT);
62 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
63 $mform->hardFreeze('shortname');
64 $mform->setConstant('shortname', $course->shortname);
67 // Verify permissions to change course category or keep current.
68 if (empty($course->id)) {
69 if (has_capability('moodle/course:create', $categorycontext)) {
70 $displaylist = coursecat::make_categories_list('moodle/course:create');
71 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
72 $mform->addHelpButton('category', 'coursecategory');
73 $mform->setDefault('category', $category->id);
74 } else {
75 $mform->addElement('hidden', 'category', null);
76 $mform->setType('category', PARAM_INT);
77 $mform->setConstant('category', $category->id);
79 } else {
80 if (has_capability('moodle/course:changecategory', $coursecontext)) {
81 $displaylist = coursecat::make_categories_list('moodle/course:create');
82 if (!isset($displaylist[$course->category])) {
83 //always keep current
84 $displaylist[$course->category] = coursecat::get($course->category, MUST_EXIST, true)->get_formatted_name();
86 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
87 $mform->addHelpButton('category', 'coursecategory');
88 } else {
89 //keep current
90 $mform->addElement('hidden', 'category', null);
91 $mform->setType('category', PARAM_INT);
92 $mform->setConstant('category', $course->category);
96 $choices = array();
97 $choices['0'] = get_string('hide');
98 $choices['1'] = get_string('show');
99 $mform->addElement('select', 'visible', get_string('visible'), $choices);
100 $mform->addHelpButton('visible', 'visible');
101 $mform->setDefault('visible', $courseconfig->visible);
102 if (!has_capability('moodle/course:visibility', $context)) {
103 $mform->hardFreeze('visible');
104 if (!empty($course->id)) {
105 $mform->setConstant('visible', $course->visible);
106 } else {
107 $mform->setConstant('visible', $courseconfig->visible);
111 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
112 $mform->addHelpButton('startdate', 'startdate');
113 $mform->setDefault('startdate', time() + 3600 * 24);
115 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
116 $mform->addHelpButton('idnumber', 'idnumbercourse');
117 $mform->setType('idnumber', PARAM_RAW);
118 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
119 $mform->hardFreeze('idnumber');
120 $mform->setConstants('idnumber', $course->idnumber);
123 // Description.
124 $mform->addElement('header', 'descriptionhdr', get_string('description'));
125 $mform->setExpanded('descriptionhdr');
127 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
128 $mform->addHelpButton('summary_editor', 'coursesummary');
129 $mform->setType('summary_editor', PARAM_RAW);
130 $summaryfields = 'summary_editor';
132 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
133 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
134 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
135 $summaryfields .= ',overviewfiles_filemanager';
138 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
139 // Remove the description header it does not contain anything any more.
140 $mform->removeElement('descriptionhdr');
141 $mform->hardFreeze($summaryfields);
144 // Course format.
145 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
147 $courseformats = get_sorted_course_formats(true);
148 $formcourseformats = array();
149 foreach ($courseformats as $courseformat) {
150 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
152 if (isset($course->format)) {
153 $course->format = course_get_format($course)->get_format(); // replace with default if not found
154 if (!in_array($course->format, $courseformats)) {
155 // this format is disabled. Still display it in the dropdown
156 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
157 get_string('pluginname', 'format_'.$course->format));
161 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
162 $mform->addHelpButton('format', 'format');
163 $mform->setDefault('format', $courseconfig->format);
165 // Button to update format-specific options on format change (will be hidden by JavaScript).
166 $mform->registerNoSubmitButton('updatecourseformat');
167 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
169 // Just a placeholder for the course format options.
170 $mform->addElement('hidden', 'addcourseformatoptionshere');
171 $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
173 // Appearance.
174 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
176 if (!empty($CFG->allowcoursethemes)) {
177 $themeobjects = get_list_of_themes();
178 $themes=array();
179 $themes[''] = get_string('forceno');
180 foreach ($themeobjects as $key=>$theme) {
181 if (empty($theme->hidefromselector)) {
182 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
185 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
188 $languages=array();
189 $languages[''] = get_string('forceno');
190 $languages += get_string_manager()->get_list_of_translations();
191 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
192 $mform->setDefault('lang', $courseconfig->lang);
194 $options = range(0, 10);
195 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
196 $mform->addHelpButton('newsitems', 'newsitemsnumber');
197 $mform->setDefault('newsitems', $courseconfig->newsitems);
199 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
200 $mform->addHelpButton('showgrades', 'showgrades');
201 $mform->setDefault('showgrades', $courseconfig->showgrades);
203 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
204 $mform->addHelpButton('showreports', 'showreports');
205 $mform->setDefault('showreports', $courseconfig->showreports);
207 // Files and uploads.
208 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
210 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
211 if (empty($course->legacyfiles)) {
212 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
213 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
214 } else {
215 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
217 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
218 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
219 if (!isset($courseconfig->legacyfiles)) {
220 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
221 $courseconfig->legacyfiles = 0;
223 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
226 // Handle non-existing $course->maxbytes on course creation.
227 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
229 // Let's prepare the maxbytes popup.
230 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
231 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
232 $mform->addHelpButton('maxbytes', 'maximumupload');
233 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
235 // Completion tracking.
236 if (completion_info::is_enabled_for_site()) {
237 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
238 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
239 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
240 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
241 } else {
242 $mform->addElement('hidden', 'enablecompletion');
243 $mform->setType('enablecompletion', PARAM_INT);
244 $mform->setDefault('enablecompletion', 0);
247 //--------------------------------------------------------------------------------
248 enrol_course_edit_form($mform, $course, $context);
250 //--------------------------------------------------------------------------------
251 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
253 $choices = array();
254 $choices[NOGROUPS] = get_string('groupsnone', 'group');
255 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
256 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
257 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
258 $mform->addHelpButton('groupmode', 'groupmode', 'group');
259 $mform->setDefault('groupmode', $courseconfig->groupmode);
261 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
262 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
263 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
265 //default groupings selector
266 $options = array();
267 $options[0] = get_string('none');
268 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
270 //--------------------------------------------------------------------------------
272 /// customizable role names in this course
273 //--------------------------------------------------------------------------------
274 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
275 $mform->addHelpButton('rolerenaming', 'rolerenaming');
277 if ($roles = get_all_roles()) {
278 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
279 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
280 foreach ($roles as $role) {
281 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->localname));
282 $mform->setType('role_'.$role->id, PARAM_TEXT);
286 //--------------------------------------------------------------------------------
287 $this->add_action_buttons();
288 //--------------------------------------------------------------------------------
289 $mform->addElement('hidden', 'id', null);
290 $mform->setType('id', PARAM_INT);
292 /// finally set the current form data
293 //--------------------------------------------------------------------------------
294 $this->set_data($course);
297 function definition_after_data() {
298 global $DB;
300 $mform = $this->_form;
302 // add available groupings
303 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
304 $options = array();
305 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
306 foreach ($groupings as $grouping) {
307 $options[$grouping->id] = format_string($grouping->name);
310 $gr_el =& $mform->getElement('defaultgroupingid');
311 $gr_el->load($options);
314 // add course format options
315 $formatvalue = $mform->getElementValue('format');
316 if (is_array($formatvalue) && !empty($formatvalue)) {
317 $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
319 $elements = $courseformat->create_edit_form_elements($mform);
320 for ($i = 0; $i < count($elements); $i++) {
321 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
322 'addcourseformatoptionshere');
327 /// perform some extra moodle validation
328 function validation($data, $files) {
329 global $DB;
331 $errors = parent::validation($data, $files);
333 // Add field validation check for duplicate shortname.
334 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
335 if (empty($data['id']) || $course->id != $data['id']) {
336 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
340 // Add field validation check for duplicate idnumber.
341 if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
342 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
343 if (empty($data['id']) || $course->id != $data['id']) {
344 $errors['idnumber'] = get_string('idnumbertaken', 'error');
349 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
351 $courseformat = course_get_format((object)array('format' => $data['format']));
352 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
353 if (!empty($formaterrors) && is_array($formaterrors)) {
354 $errors = array_merge($errors, $formaterrors);
357 return $errors;