MDL-35517 course: Handle non-existing $course->maxbytes on course creation.
[moodle.git] / course / edit_form.php
CommitLineData
8bdc9cac 1<?php
a81bf9c3 2
df997f84 3defined('MOODLE_INTERNAL') || die;
bfebaf64 4
39625498 5require_once($CFG->libdir.'/formslib.php');
516c5eca 6require_once($CFG->libdir.'/completionlib.php');
39625498 7
a81bf9c3 8class course_edit_form extends moodleform {
df997f84
PS
9 protected $course;
10 protected $context;
a81bf9c3 11
12 function definition() {
f33e1ed4 13 global $USER, $CFG, $DB;
a81bf9c3 14
df997f84 15 $mform = $this->_form;
a81bf9c3 16
df997f84
PS
17 $course = $this->_customdata['course']; // this contains the data of this form
18 $category = $this->_customdata['category'];
8bdc9cac 19 $editoroptions = $this->_customdata['editoroptions'];
7feebf6f 20 $returnto = $this->_customdata['returnto'];
a81bf9c3 21
df997f84 22 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
a81bf9c3 23 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
24
8bdc9cac 25 if (!empty($course->id)) {
a81bf9c3 26 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
27 $context = $coursecontext;
461f5127 28 } else {
a81bf9c3 29 $coursecontext = null;
30 $context = $categorycontext;
31 }
aa6c1ced 32
df997f84
PS
33 $courseconfig = get_config('moodlecourse');
34
35 $this->course = $course;
36 $this->context = $context;
37
a81bf9c3 38/// form definition with new course defaults
39//--------------------------------------------------------------------------------
2b63df96 40 $mform->addElement('header','general', get_string('general', 'form'));
a81bf9c3 41
7feebf6f 42 $mform->addElement('hidden', 'returnto', null);
43 $mform->setType('returnto', PARAM_ALPHANUM);
44 $mform->setConstant('returnto', $returnto);
45
df997f84
PS
46 // verify permissions to change course category or keep current
47 if (empty($course->id)) {
48 if (has_capability('moodle/course:create', $categorycontext)) {
49 $displaylist = array();
50 $parentlist = array();
51 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
52 $mform->addElement('select', 'category', get_string('category'), $displaylist);
53 $mform->addHelpButton('category', 'category');
54 $mform->setDefault('category', $category->id);
55 } else {
56 $mform->addElement('hidden', 'category', null);
57 $mform->setType('category', PARAM_INT);
58 $mform->setConstant('category', $category->id);
59 }
a81bf9c3 60 } else {
df997f84
PS
61 if (has_capability('moodle/course:changecategory', $coursecontext)) {
62 $displaylist = array();
63 $parentlist = array();
64 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
65 if (!isset($displaylist[$course->category])) {
66 //always keep current
67 $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
68 }
69 $mform->addElement('select', 'category', get_string('category'), $displaylist);
70 $mform->addHelpButton('category', 'category');
71 } else {
72 //keep current
73 $mform->addElement('hidden', 'category', null);
74 $mform->setType('category', PARAM_INT);
75 $mform->setConstant('category', $course->category);
76 }
50569ba3 77 }
78
828a673d 79 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
503d243a 80 $mform->addHelpButton('fullname', 'fullnamecourse');
01bc9ff1 81 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
82 $mform->setType('fullname', PARAM_MULTILANG);
8bdc9cac 83 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
01bc9ff1 84 $mform->hardFreeze('fullname');
cc444336 85 $mform->setConstant('fullname', $course->fullname);
f7c0d84b 86 }
c3df0901 87
29cbe431 88 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
5e67868e 89 $mform->addHelpButton('shortname', 'shortnamecourse');
01bc9ff1 90 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
91 $mform->setType('shortname', PARAM_MULTILANG);
8bdc9cac 92 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
01bc9ff1 93 $mform->hardFreeze('shortname');
cc444336 94 $mform->setConstant('shortname', $course->shortname);
f7c0d84b 95 }
96
01bc9ff1 97 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
5e67868e 98 $mform->addHelpButton('idnumber', 'idnumbercourse');
01bc9ff1 99 $mform->setType('idnumber', PARAM_RAW);
8bdc9cac 100 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
01bc9ff1 101 $mform->hardFreeze('idnumber');
cc444336 102 $mform->setConstants('idnumber', $course->idnumber);
f7c0d84b 103 }
01963b95 104
aa6c1ced 105
5e67868e
DM
106 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
107 $mform->addHelpButton('summary_editor', 'coursesummary');
8bdc9cac 108 $mform->setType('summary_editor', PARAM_RAW);
a81bf9c3 109
8bdc9cac
SH
110 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
111 $mform->hardFreeze('summary_editor');
50569ba3 112 }
113
17da2e6f 114 $courseformats = get_plugin_list('format');
a81bf9c3 115 $formcourseformats = array();
17da2e6f 116 foreach ($courseformats as $courseformat => $formatdir) {
ff67afbd 117 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
a81bf9c3 118 }
119 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
5e67868e 120 $mform->addHelpButton('format', 'format');
cfec0454 121 $mform->setDefault('format', $courseconfig->format);
a81bf9c3 122
1156d38d
DP
123 $mform->addElement('select', 'coursedisplay', get_string('coursedisplay'),
124 array(COURSE_DISPLAY_SINGLEPAGE => get_string('coursedisplay_single'),
125 COURSE_DISPLAY_MULTIPAGE => get_string('coursedisplay_multi')));
126 $mform->addHelpButton('coursedisplay', 'coursedisplay');
5b918d01 127 $mform->setDefault('coursedisplay', $courseconfig->coursedisplay);
1156d38d 128
c141b05f
TH
129 for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
130 $sectionmenu[$i] = "$i";
a81bf9c3 131 }
132 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
cfec0454 133 $mform->setDefault('numsections', $courseconfig->numsections);
a81bf9c3 134
135 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
5e67868e 136 $mform->addHelpButton('startdate', 'startdate');
a81bf9c3 137 $mform->setDefault('startdate', time() + 3600 * 24);
138
139 $choices = array();
140 $choices['0'] = get_string('hiddensectionscollapsed');
141 $choices['1'] = get_string('hiddensectionsinvisible');
142 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
5e67868e 143 $mform->addHelpButton('hiddensections', 'hiddensections');
cfec0454 144 $mform->setDefault('hiddensections', $courseconfig->hiddensections);
a81bf9c3 145
65877218 146 $options = range(0, 10);
a81bf9c3 147 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
5e67868e 148 $mform->addHelpButton('newsitems', 'newsitemsnumber');
cfec0454 149 $mform->setDefault('newsitems', $courseconfig->newsitems);
a81bf9c3 150
65877218 151 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
5e67868e 152 $mform->addHelpButton('showgrades', 'showgrades');
cfec0454 153 $mform->setDefault('showgrades', $courseconfig->showgrades);
a81bf9c3 154
65877218 155 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
5e67868e 156 $mform->addHelpButton('showreports', 'showreports');
cfec0454 157 $mform->setDefault('showreports', $courseconfig->showreports);
a81bf9c3 158
29740306
EL
159 // Handle non-existing $course->maxbytes on course creation.
160 $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
161
162 // Let's prepare the maxbytes popup.
163 $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
a81bf9c3 164 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
5e67868e 165 $mform->addHelpButton('maxbytes', 'maximumupload');
cfec0454 166 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
a81bf9c3 167
deb73728
PS
168 if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
169 if (empty($course->legacyfiles)) {
170 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
171 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
172 } else {
173 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
174 }
bf34822b
PS
175 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
176 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
532a91d4
PS
177 if (!isset($courseconfig->legacyfiles)) {
178 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
179 $courseconfig->legacyfiles = 0;
180 }
c00280a1 181 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
bf34822b
PS
182 }
183
a81bf9c3 184 if (!empty($CFG->allowcoursethemes)) {
33c07420 185 $themeobjects = get_list_of_themes();
a81bf9c3 186 $themes=array();
187 $themes[''] = get_string('forceno');
33c07420 188 foreach ($themeobjects as $key=>$theme) {
d609d962
SH
189 if (empty($theme->hidefromselector)) {
190 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
191 }
33c07420 192 }
a81bf9c3 193 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
194 }
461f5127 195
e15d4dd0 196//--------------------------------------------------------------------------------
df997f84 197 enrol_course_edit_form($mform, $course, $context);
a81bf9c3 198
199//--------------------------------------------------------------------------------
ec3dac98 200 $mform->addElement('header','', get_string('groups', 'group'));
a81bf9c3 201
202 $choices = array();
467cebb0 203 $choices[NOGROUPS] = get_string('groupsnone', 'group');
204 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
205 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
83fc9994
DM
206 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
207 $mform->addHelpButton('groupmode', 'groupmode', 'group');
cfec0454 208 $mform->setDefault('groupmode', $courseconfig->groupmode);
a81bf9c3 209
210 $choices = array();
211 $choices['0'] = get_string('no');
212 $choices['1'] = get_string('yes');
83fc9994
DM
213 $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
214 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
cfec0454 215 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
a81bf9c3 216
98da6021
PS
217 //default groupings selector
218 $options = array();
219 $options[0] = get_string('none');
220 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
24f41672 221
a81bf9c3 222//--------------------------------------------------------------------------------
223 $mform->addElement('header','', get_string('availability'));
224
225 $choices = array();
226 $choices['0'] = get_string('courseavailablenot');
227 $choices['1'] = get_string('courseavailable');
228 $mform->addElement('select', 'visible', get_string('availability'), $choices);
5e67868e 229 $mform->addHelpButton('visible', 'availability');
cfec0454 230 $mform->setDefault('visible', $courseconfig->visible);
db1218a9 231 if (!has_capability('moodle/course:visibility', $context)) {
9136a60c 232 $mform->hardFreeze('visible');
db1218a9
PS
233 if (!empty($course->id)) {
234 $mform->setConstant('visible', $course->visible);
235 } else {
eb88700a 236 $mform->setConstant('visible', $courseconfig->visible);
db1218a9 237 }
9136a60c 238 }
a81bf9c3 239
a81bf9c3 240//--------------------------------------------------------------------------------
241 $mform->addElement('header','', get_string('language'));
242
243 $languages=array();
244 $languages[''] = get_string('forceno');
1f96e907 245 $languages += get_string_manager()->get_list_of_translations();
a81bf9c3 246 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
cfec0454 247 $mform->setDefault('lang', $courseconfig->lang);
a81bf9c3 248
4e781c7b 249//--------------------------------------------------------------------------------
516c5eca 250 if (completion_info::is_enabled_for_site()) {
4e781c7b 251 $mform->addElement('header','', get_string('progress','completion'));
f7c0d84b 252 $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
4e781c7b 253 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
cfec0454 254 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
2be4d090
MD
255
256 $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
257 $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
258 $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
4e781c7b 259 } else {
260 $mform->addElement('hidden', 'enablecompletion');
d18e0fe6 261 $mform->setType('enablecompletion', PARAM_INT);
4e781c7b 262 $mform->setDefault('enablecompletion',0);
2be4d090
MD
263
264 $mform->addElement('hidden', 'completionstartonenrol');
265 $mform->setType('completionstartonenrol', PARAM_INT);
266 $mform->setDefault('completionstartonenrol',0);
4e781c7b 267 }
268
0150c561 269/// customizable role names in this course
270//--------------------------------------------------------------------------------
7d2e9a52 271 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
5e67868e 272 $mform->addHelpButton('rolerenaming', 'rolerenaming');
0150c561 273
bef12c99 274 if ($roles = get_all_roles()) {
275 if ($coursecontext) {
9101efd3 276 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
bef12c99 277 }
278 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
0150c561 279 foreach ($roles as $role) {
bef12c99 280 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
281 if (isset($role->localname)) {
282 $mform->setDefault('role_'.$role->id, $role->localname);
283 }
284 $mform->setType('role_'.$role->id, PARAM_TEXT);
285 if (!in_array($role->id, $assignableroles)) {
286 $mform->setAdvanced('role_'.$role->id);
0150c561 287 }
288 }
289 }
290
a81bf9c3 291//--------------------------------------------------------------------------------
a23f0aaf 292 $this->add_action_buttons();
a81bf9c3 293//--------------------------------------------------------------------------------
294 $mform->addElement('hidden', 'id', null);
295 $mform->setType('id', PARAM_INT);
df997f84
PS
296
297/// finally set the current form data
298//--------------------------------------------------------------------------------
299 $this->set_data($course);
c08ac016 300 }
2dbd6409 301
24f41672 302 function definition_after_data() {
c3df0901 303 global $DB;
24f41672 304
df997f84 305 $mform = $this->_form;
24f41672 306
df997f84 307 // add available groupings
24f41672 308 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
309 $options = array();
c3df0901 310 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
24f41672 311 foreach ($groupings as $grouping) {
312 $options[$grouping->id] = format_string($grouping->name);
313 }
314 }
315 $gr_el =& $mform->getElement('defaultgroupingid');
316 $gr_el->load($options);
317 }
318 }
f7c0d84b 319
a81bf9c3 320
321/// perform some extra moodle validation
a78890d5 322 function validation($data, $files) {
82672532 323 global $DB, $CFG;
c3df0901 324
a78890d5 325 $errors = parent::validation($data, $files);
c3df0901 326 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
a81bf9c3 327 if (!empty($data['id'])) {
328 unset($foundcourses[$data['id']]);
01963b95 329 }
330 if (!empty($foundcourses)) {
331 foreach ($foundcourses as $foundcourse) {
332 $foundcoursenames[] = $foundcourse->fullname;
333 }
a81bf9c3 334 $foundcoursenamestring = implode(',', $foundcoursenames);
335 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 336 }
01963b95 337 }
a81bf9c3 338
df997f84 339 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
82672532 340
a78890d5 341 return $errors;
01963b95 342 }
a81bf9c3 343}
aa6c1ced 344