c46c8d4fe830aa9e0a185c1a5c6f6ecc65d6e328
[moodle.git] / course / edit_form.php
1 <?php
3 defined('MOODLE_INTERNAL') || die;
5 require_once($CFG->libdir.'/formslib.php');
7 class course_edit_form extends moodleform {
8     protected $course;
9     protected $context;
11     function definition() {
12         global $USER, $CFG, $DB;
14         $mform    = $this->_form;
16         $course        = $this->_customdata['course']; // this contains the data of this form
17         $category      = $this->_customdata['category'];
18         $editoroptions = $this->_customdata['editoroptions'];
19         $returnto = $this->_customdata['returnto'];
21         $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
22         $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
24         if (!empty($course->id)) {
25             $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
26             $context = $coursecontext;
27         } else {
28             $coursecontext = null;
29             $context = $categorycontext;
30         }
32         $courseconfig = get_config('moodlecourse');
34         $this->course  = $course;
35         $this->context = $context;
37 /// form definition with new course defaults
38 //--------------------------------------------------------------------------------
39         $mform->addElement('header','general', get_string('general', 'form'));
41         $mform->addElement('hidden', 'returnto', null);
42         $mform->setType('returnto', PARAM_ALPHANUM);
43         $mform->setConstant('returnto', $returnto);
45         // verify permissions to change course category or keep current
46         if (empty($course->id)) {
47             if (has_capability('moodle/course:create', $categorycontext)) {
48                 $displaylist = array();
49                 $parentlist = array();
50                 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
51                 $mform->addElement('select', 'category', get_string('category'), $displaylist);
52                 $mform->addHelpButton('category', 'category');
53                 $mform->setDefault('category', $category->id);
54             } else {
55                 $mform->addElement('hidden', 'category', null);
56                 $mform->setType('category', PARAM_INT);
57                 $mform->setConstant('category', $category->id);
58             }
59         } else {
60             if (has_capability('moodle/course:changecategory', $coursecontext)) {
61                 $displaylist = array();
62                 $parentlist = array();
63                 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
64                 if (!isset($displaylist[$course->category])) {
65                     //always keep current
66                     $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
67                 }
68                 $mform->addElement('select', 'category', get_string('category'), $displaylist);
69                 $mform->addHelpButton('category', 'category');
70             } else {
71                 //keep current
72                 $mform->addElement('hidden', 'category', null);
73                 $mform->setType('category', PARAM_INT);
74                 $mform->setConstant('category', $course->category);
75             }
76         }
78         $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
79         $mform->addHelpButton('fullname', 'fullnamecourse');
80         $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
81         $mform->setType('fullname', PARAM_MULTILANG);
82         if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
83             $mform->hardFreeze('fullname');
84             $mform->setConstant('fullname', $course->fullname);
85         }
87         $mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
88         $mform->addHelpButton('shortname', 'shortnamecourse');
89         $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
90         $mform->setType('shortname', PARAM_MULTILANG);
91         if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
92             $mform->hardFreeze('shortname');
93             $mform->setConstant('shortname', $course->shortname);
94         }
96         $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
97         $mform->addHelpButton('idnumber', 'idnumbercourse');
98         $mform->setType('idnumber', PARAM_RAW);
99         if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
100             $mform->hardFreeze('idnumber');
101             $mform->setConstants('idnumber', $course->idnumber);
102         }
105         $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
106         $mform->addHelpButton('summary_editor', 'coursesummary');
107         $mform->setType('summary_editor', PARAM_RAW);
109         if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
110             $mform->hardFreeze('summary_editor');
111         }
113         $courseformats = get_plugin_list('format');
114         $formcourseformats = array();
115         foreach ($courseformats as $courseformat => $formatdir) {
116             $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
117         }
118         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
119         $mform->addHelpButton('format', 'format');
120         $mform->setDefault('format', $courseconfig->format);
122         for ($i=1; $i<=52; $i++) {
123           $sectionmenu[$i] = "$i";
124         }
125         $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
126         $mform->setDefault('numsections', $courseconfig->numsections);
128         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
129         $mform->addHelpButton('startdate', 'startdate');
130         $mform->setDefault('startdate', time() + 3600 * 24);
132         $choices = array();
133         $choices['0'] = get_string('hiddensectionscollapsed');
134         $choices['1'] = get_string('hiddensectionsinvisible');
135         $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
136         $mform->addHelpButton('hiddensections', 'hiddensections');
137         $mform->setDefault('hiddensections', $courseconfig->hiddensections);
139         $options = range(0, 10);
140         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
141         $mform->addHelpButton('newsitems', 'newsitemsnumber');
142         $mform->setDefault('newsitems', $courseconfig->newsitems);
144         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
145         $mform->addHelpButton('showgrades', 'showgrades');
146         $mform->setDefault('showgrades', $courseconfig->showgrades);
148         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
149         $mform->addHelpButton('showreports', 'showreports');
150         $mform->setDefault('showreports', $courseconfig->showreports);
152         $choices = get_max_upload_sizes($CFG->maxbytes);
153         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
154         $mform->addHelpButton('maxbytes', 'maximumupload');
155         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
157         if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
158             if (empty($course->legacyfiles)) {
159                 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
160                 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
161             } else {
162                 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
163             }
164             $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
165             $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
166             $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
167         }
169         if (!empty($CFG->allowcoursethemes)) {
170             $themeobjects = get_list_of_themes();
171             $themes=array();
172             $themes[''] = get_string('forceno');
173             foreach ($themeobjects as $key=>$theme) {
174                 $themes[$key] = $theme->name;
175             }
176             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
177         }
179 //--------------------------------------------------------------------------------
180         enrol_course_edit_form($mform, $course, $context);
182 //--------------------------------------------------------------------------------
183         $mform->addElement('header','', get_string('groups', 'group'));
185         $choices = array();
186         $choices[NOGROUPS] = get_string('groupsnone', 'group');
187         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
188         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
189         $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
190         $mform->addHelpButton('groupmode', 'groupmode', 'group');
191         $mform->setDefault('groupmode', $courseconfig->groupmode);
193         $choices = array();
194         $choices['0'] = get_string('no');
195         $choices['1'] = get_string('yes');
196         $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
197         $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
198         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
200         //default groupings selector
201         $options = array();
202         $options[0] = get_string('none');
203         $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
205 //--------------------------------------------------------------------------------
206         $mform->addElement('header','', get_string('availability'));
208         $choices = array();
209         $choices['0'] = get_string('courseavailablenot');
210         $choices['1'] = get_string('courseavailable');
211         $mform->addElement('select', 'visible', get_string('availability'), $choices);
212         $mform->addHelpButton('visible', 'availability');
213         $mform->setDefault('visible', $courseconfig->visible);
214         if (!has_capability('moodle/course:visibility', $context)) {
215             $mform->hardFreeze('visible');
216             if (!empty($course->id)) {
217                 $mform->setConstant('visible', $course->visible);
218             } else {
219                 $mform->setConstant('visible', $category->visible);
220             }
221         }
223 //--------------------------------------------------------------------------------
224         $mform->addElement('header','', get_string('language'));
226         $languages=array();
227         $languages[''] = get_string('forceno');
228         $languages += get_string_manager()->get_list_of_translations();
229         $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
230         $mform->setDefault('lang', $courseconfig->lang);
232 //--------------------------------------------------------------------------------
233         require_once($CFG->libdir.'/completionlib.php');
234         if(completion_info::is_enabled_for_site()) {
235             $mform->addElement('header','', get_string('progress','completion'));
236             $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
237                 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
238             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
240             $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
241             $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
242             $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
243         } else {
244             $mform->addElement('hidden', 'enablecompletion');
245             $mform->setType('enablecompletion', PARAM_INT);
246             $mform->setDefault('enablecompletion',0);
248             $mform->addElement('hidden', 'completionstartonenrol');
249             $mform->setType('completionstartonenrol', PARAM_INT);
250             $mform->setDefault('completionstartonenrol',0);
251         }
253 //--------------------------------------------------------------------------------
254         if (has_capability('moodle/site:config', $systemcontext)) {
255             if (((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
256                 $mform->addElement('header', '', get_string('restrictmodules'));
258                 $options = array();
259                 $options['0'] = get_string('no');
260                 $options['1'] = get_string('yes');
261                 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
262                 if (!empty($CFG->restrictbydefault)) {
263                     $mform->setDefault('restrictmodules', 1);
264                 }
266                 $mods = array(0=>get_string('allownone'));
267                 $mods += $DB->get_records_menu('modules', array('visible'=>1), 'name', 'id, name');
268                 $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10'));
269                 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
270                 // defaults are already in $course
271             } else {
272                 // remove any mod restriction
273                 $mform->addElement('hidden', 'restrictmodules', 0);
274                 $mform->setType('restrictmodules', PARAM_INT);
275             }
276         } else {
277             $mform->addElement('hidden', 'restrictmodules');
278             $mform->setType('restrictmodules', PARAM_INT);
279             if (empty($course->id)) {
280                 $mform->setConstant('restrictmodules', (int)($CFG->restrictmodulesfor == 'all'));
281             } else {
282                 // keep previous
283                 $mform->setConstant('restrictmodules', $course->restrictmodules);
284             }
285         }
287 /// customizable role names in this course
288 //--------------------------------------------------------------------------------
289         $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
290         $mform->addHelpButton('rolerenaming', 'rolerenaming');
292         if ($roles = get_all_roles()) {
293             if ($coursecontext) {
294                 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
295             }
296             $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
297             foreach ($roles as $role) {
298                 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
299                 if (isset($role->localname)) {
300                     $mform->setDefault('role_'.$role->id, $role->localname);
301                 }
302                 $mform->setType('role_'.$role->id, PARAM_TEXT);
303                 if (!in_array($role->id, $assignableroles)) {
304                     $mform->setAdvanced('role_'.$role->id);
305                 }
306             }
307         }
309 //--------------------------------------------------------------------------------
310         $this->add_action_buttons();
311 //--------------------------------------------------------------------------------
312         $mform->addElement('hidden', 'id', null);
313         $mform->setType('id', PARAM_INT);
315 /// finally set the current form data
316 //--------------------------------------------------------------------------------
317         $this->set_data($course);
318     }
320     function definition_after_data() {
321         global $DB;
323         $mform = $this->_form;
325         // add available groupings
326         if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
327             $options = array();
328             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
329                 foreach ($groupings as $grouping) {
330                     $options[$grouping->id] = format_string($grouping->name);
331                 }
332             }
333             $gr_el =& $mform->getElement('defaultgroupingid');
334             $gr_el->load($options);
335         }
336     }
339 /// perform some extra moodle validation
340     function validation($data, $files) {
341         global $DB, $CFG;
343         $errors = parent::validation($data, $files);
344         if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
345             if (!empty($data['id'])) {
346                 unset($foundcourses[$data['id']]);
347             }
348             if (!empty($foundcourses)) {
349                 foreach ($foundcourses as $foundcourse) {
350                     $foundcoursenames[] = $foundcourse->fullname;
351                 }
352                 $foundcoursenamestring = implode(',', $foundcoursenames);
353                 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
354             }
355         }
357         $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
359         return $errors;
360     }