MDL-38731 course: split course group settings header
[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');
8 class course_edit_form extends moodleform {
9     protected $course;
10     protected $context;
12     function definition() {
13         global $USER, $CFG, $DB;
15         $mform    = $this->_form;
17         $course        = $this->_customdata['course']; // this contains the data of this form
18         $category      = $this->_customdata['category'];
19         $editoroptions = $this->_customdata['editoroptions'];
20         $returnto = $this->_customdata['returnto'];
22         $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
23         $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
25         if (!empty($course->id)) {
26             $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
27             $context = $coursecontext;
28         } else {
29             $coursecontext = null;
30             $context = $categorycontext;
31         }
33         $courseconfig = get_config('moodlecourse');
35         $this->course  = $course;
36         $this->context = $context;
38 /// form definition with new course defaults
39 //--------------------------------------------------------------------------------
40         $mform->addElement('header','general', get_string('general', 'form'));
42         $mform->addElement('hidden', 'returnto', null);
43         $mform->setType('returnto', PARAM_ALPHANUM);
44         $mform->setConstant('returnto', $returnto);
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             }
60         } else {
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             }
77         }
79         $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
80         $mform->addHelpButton('fullname', 'fullnamecourse');
81         $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
82         $mform->setType('fullname', PARAM_MULTILANG);
83         if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
84             $mform->hardFreeze('fullname');
85             $mform->setConstant('fullname', $course->fullname);
86         }
88         $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
89         $mform->addHelpButton('shortname', 'shortnamecourse');
90         $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
91         $mform->setType('shortname', PARAM_MULTILANG);
92         if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
93             $mform->hardFreeze('shortname');
94             $mform->setConstant('shortname', $course->shortname);
95         }
97         $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
98         $mform->addHelpButton('idnumber', 'idnumbercourse');
99         $mform->setType('idnumber', PARAM_RAW);
100         if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
101             $mform->hardFreeze('idnumber');
102             $mform->setConstants('idnumber', $course->idnumber);
103         }
106         $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
107         $mform->addHelpButton('summary_editor', 'coursesummary');
108         $mform->setType('summary_editor', PARAM_RAW);
110         if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
111             $mform->hardFreeze('summary_editor');
112         }
114         $courseformats = get_plugin_list('format');
115         $formcourseformats = array();
116         foreach ($courseformats as $courseformat => $formatdir) {
117             $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
118         }
119         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
120         $mform->addHelpButton('format', 'format');
121         $mform->setDefault('format', $courseconfig->format);
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');
127         $mform->setDefault('coursedisplay', $courseconfig->coursedisplay);
129         $max = $courseconfig->maxsections;
130         if (!isset($max) || !is_numeric($max)) {
131             $max = 52;
132         }
133         for ($i = 0; $i <= $max; $i++) {
134             $sectionmenu[$i] = "$i";
135         }
136         $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
137         $mform->setDefault('numsections', $courseconfig->numsections);
139         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
140         $mform->addHelpButton('startdate', 'startdate');
141         $mform->setDefault('startdate', time() + 3600 * 24);
143         $choices = array();
144         $choices['0'] = get_string('hiddensectionscollapsed');
145         $choices['1'] = get_string('hiddensectionsinvisible');
146         $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
147         $mform->addHelpButton('hiddensections', 'hiddensections');
148         $mform->setDefault('hiddensections', $courseconfig->hiddensections);
150         $options = range(0, 10);
151         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
152         $mform->addHelpButton('newsitems', 'newsitemsnumber');
153         $mform->setDefault('newsitems', $courseconfig->newsitems);
155         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
156         $mform->addHelpButton('showgrades', 'showgrades');
157         $mform->setDefault('showgrades', $courseconfig->showgrades);
159         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
160         $mform->addHelpButton('showreports', 'showreports');
161         $mform->setDefault('showreports', $courseconfig->showreports);
163         // Handle non-existing $course->maxbytes on course creation.
164         $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
166         // Let's prepare the maxbytes popup.
167         $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
168         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
169         $mform->addHelpButton('maxbytes', 'maximumupload');
170         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
172         if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
173             if (empty($course->legacyfiles)) {
174                 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
175                 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
176             } else {
177                 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
178             }
179             $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
180             $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
181             if (!isset($courseconfig->legacyfiles)) {
182                 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
183                 $courseconfig->legacyfiles = 0;
184             }
185             $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
186         }
188         if (!empty($CFG->allowcoursethemes)) {
189             $themeobjects = get_list_of_themes();
190             $themes=array();
191             $themes[''] = get_string('forceno');
192             foreach ($themeobjects as $key=>$theme) {
193                 if (empty($theme->hidefromselector)) {
194                     $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
195                 }
196             }
197             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
198         }
200 //--------------------------------------------------------------------------------
201         enrol_course_edit_form($mform, $course, $context);
203 //--------------------------------------------------------------------------------
204         $mform->addElement('header', '', get_string('groupsettingsheader', 'group'));
206         $choices = array();
207         $choices[NOGROUPS] = get_string('groupsnone', 'group');
208         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
209         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
210         $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
211         $mform->addHelpButton('groupmode', 'groupmode', 'group');
212         $mform->setDefault('groupmode', $courseconfig->groupmode);
214         $choices = array();
215         $choices['0'] = get_string('no');
216         $choices['1'] = get_string('yes');
217         $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
218         $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
219         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
221         //default groupings selector
222         $options = array();
223         $options[0] = get_string('none');
224         $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
226 //--------------------------------------------------------------------------------
227         $mform->addElement('header','', get_string('availability'));
229         $choices = array();
230         $choices['0'] = get_string('courseavailablenot');
231         $choices['1'] = get_string('courseavailable');
232         $mform->addElement('select', 'visible', get_string('availability'), $choices);
233         $mform->addHelpButton('visible', 'availability');
234         $mform->setDefault('visible', $courseconfig->visible);
235         if (!has_capability('moodle/course:visibility', $context)) {
236             $mform->hardFreeze('visible');
237             if (!empty($course->id)) {
238                 $mform->setConstant('visible', $course->visible);
239             } else {
240                 $mform->setConstant('visible', $courseconfig->visible);
241             }
242         }
244 //--------------------------------------------------------------------------------
245         $mform->addElement('header','', get_string('language'));
247         $languages=array();
248         $languages[''] = get_string('forceno');
249         $languages += get_string_manager()->get_list_of_translations();
250         $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
251         $mform->setDefault('lang', $courseconfig->lang);
253 //--------------------------------------------------------------------------------
254         if (completion_info::is_enabled_for_site()) {
255             $mform->addElement('header','', get_string('progress','completion'));
256             $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
257                 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
258             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
260             $mform->addElement('advcheckbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
261             $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
262             $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
263         } else {
264             $mform->addElement('hidden', 'enablecompletion');
265             $mform->setType('enablecompletion', PARAM_INT);
266             $mform->setDefault('enablecompletion',0);
268             $mform->addElement('hidden', 'completionstartonenrol');
269             $mform->setType('completionstartonenrol', PARAM_INT);
270             $mform->setDefault('completionstartonenrol',0);
271         }
273 /// customizable role names in this course
274 //--------------------------------------------------------------------------------
275         $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
276         $mform->addHelpButton('rolerenaming', 'rolerenaming');
278         if ($roles = get_all_roles()) {
279             if ($coursecontext) {
280                 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
281             }
282             $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
283             foreach ($roles as $role) {
284                 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
285                 if (isset($role->localname)) {
286                     $mform->setDefault('role_'.$role->id, $role->localname);
287                 }
288                 $mform->setType('role_'.$role->id, PARAM_TEXT);
289                 if (!in_array($role->id, $assignableroles)) {
290                     $mform->setAdvanced('role_'.$role->id);
291                 }
292             }
293         }
295 //--------------------------------------------------------------------------------
296         $this->add_action_buttons();
297 //--------------------------------------------------------------------------------
298         $mform->addElement('hidden', 'id', null);
299         $mform->setType('id', PARAM_INT);
301 /// finally set the current form data
302 //--------------------------------------------------------------------------------
303         $this->set_data($course);
304     }
306     function definition_after_data() {
307         global $DB;
309         $mform = $this->_form;
311         // add available groupings
312         if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
313             $options = array();
314             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
315                 foreach ($groupings as $grouping) {
316                     $options[$grouping->id] = format_string($grouping->name);
317                 }
318             }
319             $gr_el =& $mform->getElement('defaultgroupingid');
320             $gr_el->load($options);
321         }
322     }
325 /// perform some extra moodle validation
326     function validation($data, $files) {
327         global $DB, $CFG;
329         $errors = parent::validation($data, $files);
330         if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
331             if (!empty($data['id'])) {
332                 unset($foundcourses[$data['id']]);
333             }
334             if (!empty($foundcourses)) {
335                 foreach ($foundcourses as $foundcourse) {
336                     $foundcoursenames[] = $foundcourse->fullname;
337                 }
338                 $foundcoursenamestring = implode(',', $foundcoursenames);
339                 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
340             }
341         }
343         $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
345         return $errors;
346     }