MDL-34311 course: prevent course edit form warnings
[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, $PAGE;
15         $mform    = $this->_form;
16         $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
17                 array(array('formid' => $mform->getAttribute('id'))));
19         $course        = $this->_customdata['course']; // this contains the data of this form
20         $category      = $this->_customdata['category'];
21         $editoroptions = $this->_customdata['editoroptions'];
22         $returnto = $this->_customdata['returnto'];
24         $systemcontext   = context_system::instance();
25         $categorycontext = context_coursecat::instance($category->id);
27         if (!empty($course->id)) {
28             $coursecontext = context_course::instance($course->id);
29             $context = $coursecontext;
30         } else {
31             $coursecontext = null;
32             $context = $categorycontext;
33         }
35         $courseconfig = get_config('moodlecourse');
37         $this->course  = $course;
38         $this->context = $context;
40 /// form definition with new course defaults
41 //--------------------------------------------------------------------------------
42         $mform->addElement('header','general', get_string('general', 'form'));
44         $mform->addElement('hidden', 'returnto', null);
45         $mform->setType('returnto', PARAM_ALPHANUM);
46         $mform->setConstant('returnto', $returnto);
48         // verify permissions to change course category or keep current
49         if (empty($course->id)) {
50             if (has_capability('moodle/course:create', $categorycontext)) {
51                 $displaylist = array();
52                 $parentlist = array();
53                 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
54                 $mform->addElement('select', 'category', get_string('category'), $displaylist);
55                 $mform->addHelpButton('category', 'category');
56                 $mform->setDefault('category', $category->id);
57             } else {
58                 $mform->addElement('hidden', 'category', null);
59                 $mform->setType('category', PARAM_INT);
60                 $mform->setConstant('category', $category->id);
61             }
62         } else {
63             if (has_capability('moodle/course:changecategory', $coursecontext)) {
64                 $displaylist = array();
65                 $parentlist = array();
66                 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
67                 if (!isset($displaylist[$course->category])) {
68                     //always keep current
69                     $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
70                 }
71                 $mform->addElement('select', 'category', get_string('category'), $displaylist);
72                 $mform->addHelpButton('category', 'category');
73             } else {
74                 //keep current
75                 $mform->addElement('hidden', 'category', null);
76                 $mform->setType('category', PARAM_INT);
77                 $mform->setConstant('category', $course->category);
78             }
79         }
81         $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
82         $mform->addHelpButton('fullname', 'fullnamecourse');
83         $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
84         $mform->setType('fullname', PARAM_TEXT);
85         if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
86             $mform->hardFreeze('fullname');
87             $mform->setConstant('fullname', $course->fullname);
88         }
90         $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
91         $mform->addHelpButton('shortname', 'shortnamecourse');
92         $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
93         $mform->setType('shortname', PARAM_TEXT);
94         if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
95             $mform->hardFreeze('shortname');
96             $mform->setConstant('shortname', $course->shortname);
97         }
99         $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
100         $mform->addHelpButton('idnumber', 'idnumbercourse');
101         $mform->setType('idnumber', PARAM_RAW);
102         if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
103             $mform->hardFreeze('idnumber');
104             $mform->setConstants('idnumber', $course->idnumber);
105         }
108         $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
109         $mform->addHelpButton('summary_editor', 'coursesummary');
110         $mform->setType('summary_editor', PARAM_RAW);
112         if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
113             $mform->hardFreeze('summary_editor');
114         }
116         $courseformats = get_sorted_course_formats(true);
117         $formcourseformats = array();
118         foreach ($courseformats as $courseformat) {
119             $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
120         }
121         if (isset($course->format)) {
122             $course->format = course_get_format($course)->get_format(); // replace with default if not found
123             if (!in_array($course->format, $courseformats)) {
124                 // this format is disabled. Still display it in the dropdown
125                 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
126                         get_string('pluginname', 'format_'.$course->format));
127             }
128         }
130         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
131         $mform->addHelpButton('format', 'format');
132         $mform->setDefault('format', $courseconfig->format);
134         // button to update format-specific options on format change (will be hidden by JavaScript)
135         $mform->registerNoSubmitButton('updatecourseformat');
136         $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
138         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
139         $mform->addHelpButton('startdate', 'startdate');
140         $mform->setDefault('startdate', time() + 3600 * 24);
142         $options = range(0, 10);
143         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
144         $mform->addHelpButton('newsitems', 'newsitemsnumber');
145         $mform->setDefault('newsitems', $courseconfig->newsitems);
147         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
148         $mform->addHelpButton('showgrades', 'showgrades');
149         $mform->setDefault('showgrades', $courseconfig->showgrades);
151         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
152         $mform->addHelpButton('showreports', 'showreports');
153         $mform->setDefault('showreports', $courseconfig->showreports);
155         // Handle non-existing $course->maxbytes on course creation.
156         $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
158         // Let's prepare the maxbytes popup.
159         $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
160         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
161         $mform->addHelpButton('maxbytes', 'maximumupload');
162         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
164         if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
165             if (empty($course->legacyfiles)) {
166                 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
167                 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
168             } else {
169                 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
170             }
171             $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
172             $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
173             if (!isset($courseconfig->legacyfiles)) {
174                 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
175                 $courseconfig->legacyfiles = 0;
176             }
177             $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
178         }
180         if (!empty($CFG->allowcoursethemes)) {
181             $themeobjects = get_list_of_themes();
182             $themes=array();
183             $themes[''] = get_string('forceno');
184             foreach ($themeobjects as $key=>$theme) {
185                 if (empty($theme->hidefromselector)) {
186                     $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
187                 }
188             }
189             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
190         }
192 //--------------------------------------------------------------------------------
193         // Just a placeholder..
194         $mform->addElement('hidden', 'addcourseformatoptionshere');
195         $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
197 //--------------------------------------------------------------------------------
198         enrol_course_edit_form($mform, $course, $context);
200 //--------------------------------------------------------------------------------
201         $mform->addElement('header','', get_string('groups', 'group'));
203         $choices = array();
204         $choices[NOGROUPS] = get_string('groupsnone', 'group');
205         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
206         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
207         $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
208         $mform->addHelpButton('groupmode', 'groupmode', 'group');
209         $mform->setDefault('groupmode', $courseconfig->groupmode);
211         $choices = array();
212         $choices['0'] = get_string('no');
213         $choices['1'] = get_string('yes');
214         $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
215         $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
216         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
218         //default groupings selector
219         $options = array();
220         $options[0] = get_string('none');
221         $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
223 //--------------------------------------------------------------------------------
224         $mform->addElement('header','', get_string('availability'));
226         $choices = array();
227         $choices['0'] = get_string('courseavailablenot');
228         $choices['1'] = get_string('courseavailable');
229         $mform->addElement('select', 'visible', get_string('availability'), $choices);
230         $mform->addHelpButton('visible', 'availability');
231         $mform->setDefault('visible', $courseconfig->visible);
232         if (!has_capability('moodle/course:visibility', $context)) {
233             $mform->hardFreeze('visible');
234             if (!empty($course->id)) {
235                 $mform->setConstant('visible', $course->visible);
236             } else {
237                 $mform->setConstant('visible', $courseconfig->visible);
238             }
239         }
241 //--------------------------------------------------------------------------------
242         $mform->addElement('header','', get_string('language'));
244         $languages=array();
245         $languages[''] = get_string('forceno');
246         $languages += get_string_manager()->get_list_of_translations();
247         $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
248         $mform->setDefault('lang', $courseconfig->lang);
250 //--------------------------------------------------------------------------------
251         if (completion_info::is_enabled_for_site()) {
252             $mform->addElement('header','', get_string('progress','completion'));
253             $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
254                 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
255             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
257             $mform->addElement('advcheckbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
258             $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
259             $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
260         } else {
261             $mform->addElement('hidden', 'enablecompletion');
262             $mform->setType('enablecompletion', PARAM_INT);
263             $mform->setDefault('enablecompletion',0);
265             $mform->addElement('hidden', 'completionstartonenrol');
266             $mform->setType('completionstartonenrol', PARAM_INT);
267             $mform->setDefault('completionstartonenrol',0);
268         }
270 /// customizable role names in this course
271 //--------------------------------------------------------------------------------
272         $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
273         $mform->addHelpButton('rolerenaming', 'rolerenaming');
275         if ($roles = get_all_roles()) {
276             $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
277             $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
278             foreach ($roles as $role) {
279                 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->localname));
280                 $mform->setType('role_'.$role->id, PARAM_TEXT);
281                 if (!in_array($role->id, $assignableroles)) {
282                     $mform->setAdvanced('role_'.$role->id);
283                 }
284             }
285         }
287 //--------------------------------------------------------------------------------
288         $this->add_action_buttons();
289 //--------------------------------------------------------------------------------
290         $mform->addElement('hidden', 'id', null);
291         $mform->setType('id', PARAM_INT);
293 /// finally set the current form data
294 //--------------------------------------------------------------------------------
295         $this->set_data($course);
296     }
298     function definition_after_data() {
299         global $DB;
301         $mform = $this->_form;
303         // add available groupings
304         if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
305             $options = array();
306             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
307                 foreach ($groupings as $grouping) {
308                     $options[$grouping->id] = format_string($grouping->name);
309                 }
310             }
311             $gr_el =& $mform->getElement('defaultgroupingid');
312             $gr_el->load($options);
313         }
315         // add course format options
316         $formatvalue = $mform->getElementValue('format');
317         if (is_array($formatvalue) && !empty($formatvalue)) {
318             $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
319             $newel = $mform->createElement('header', '', get_string('courseformatoptions', 'moodle',
320                     $courseformat->get_format_name()));
321             $mform->insertElementBefore($newel, 'addcourseformatoptionshere');
323             $elements = $courseformat->create_edit_form_elements($mform);
324             for ($i = 0; $i < count($elements); $i++) {
325                 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
326                         'addcourseformatoptionshere');
327             }
328         }
329     }
331 /// perform some extra moodle validation
332     function validation($data, $files) {
333         global $DB, $CFG;
335         $errors = parent::validation($data, $files);
336         if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
337             if (!empty($data['id'])) {
338                 unset($foundcourses[$data['id']]);
339             }
340             if (!empty($foundcourses)) {
341                 foreach ($foundcourses as $foundcourse) {
342                     $foundcoursenames[] = $foundcourse->fullname;
343                 }
344                 $foundcoursenamestring = implode(',', $foundcoursenames);
345                 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
346             }
347         }
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);
355         }
357         return $errors;
358     }