Merge branch 'install_24_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_24...
[moodle.git] / course / edit_form.php
blob8f7e87532a819451af9b3b191b58c47ac95f006c
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;
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);
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)));
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);
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);
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);
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);
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');
116 $courseformats = get_sorted_course_formats(true);
117 $formcourseformats = array();
118 foreach ($courseformats as $courseformat) {
119 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
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));
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'));
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;
177 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
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);
189 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
192 //--------------------------------------------------------------------------------
193 $mform->addElement('hidden', 'addcourseformatoptionshere');
195 //--------------------------------------------------------------------------------
196 enrol_course_edit_form($mform, $course, $context);
198 //--------------------------------------------------------------------------------
199 $mform->addElement('header','', get_string('groups', 'group'));
201 $choices = array();
202 $choices[NOGROUPS] = get_string('groupsnone', 'group');
203 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
204 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
205 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
206 $mform->addHelpButton('groupmode', 'groupmode', 'group');
207 $mform->setDefault('groupmode', $courseconfig->groupmode);
209 $choices = array();
210 $choices['0'] = get_string('no');
211 $choices['1'] = get_string('yes');
212 $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
213 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
214 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
216 //default groupings selector
217 $options = array();
218 $options[0] = get_string('none');
219 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
221 //--------------------------------------------------------------------------------
222 $mform->addElement('header','', get_string('availability'));
224 $choices = array();
225 $choices['0'] = get_string('courseavailablenot');
226 $choices['1'] = get_string('courseavailable');
227 $mform->addElement('select', 'visible', get_string('availability'), $choices);
228 $mform->addHelpButton('visible', 'availability');
229 $mform->setDefault('visible', $courseconfig->visible);
230 if (!has_capability('moodle/course:visibility', $context)) {
231 $mform->hardFreeze('visible');
232 if (!empty($course->id)) {
233 $mform->setConstant('visible', $course->visible);
234 } else {
235 $mform->setConstant('visible', $courseconfig->visible);
239 //--------------------------------------------------------------------------------
240 $mform->addElement('header','', get_string('language'));
242 $languages=array();
243 $languages[''] = get_string('forceno');
244 $languages += get_string_manager()->get_list_of_translations();
245 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
246 $mform->setDefault('lang', $courseconfig->lang);
248 //--------------------------------------------------------------------------------
249 if (completion_info::is_enabled_for_site()) {
250 $mform->addElement('header','', get_string('progress','completion'));
251 $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
252 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
253 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
255 $mform->addElement('advcheckbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
256 $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
257 $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
258 } else {
259 $mform->addElement('hidden', 'enablecompletion');
260 $mform->setType('enablecompletion', PARAM_INT);
261 $mform->setDefault('enablecompletion',0);
263 $mform->addElement('hidden', 'completionstartonenrol');
264 $mform->setType('completionstartonenrol', PARAM_INT);
265 $mform->setDefault('completionstartonenrol',0);
268 /// customizable role names in this course
269 //--------------------------------------------------------------------------------
270 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
271 $mform->addHelpButton('rolerenaming', 'rolerenaming');
273 if ($roles = get_all_roles()) {
274 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
275 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
276 foreach ($roles as $role) {
277 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->localname));
278 $mform->setType('role_'.$role->id, PARAM_TEXT);
279 if (!in_array($role->id, $assignableroles)) {
280 $mform->setAdvanced('role_'.$role->id);
285 //--------------------------------------------------------------------------------
286 $this->add_action_buttons();
287 //--------------------------------------------------------------------------------
288 $mform->addElement('hidden', 'id', null);
289 $mform->setType('id', PARAM_INT);
291 /// finally set the current form data
292 //--------------------------------------------------------------------------------
293 $this->set_data($course);
296 function definition_after_data() {
297 global $DB;
299 $mform = $this->_form;
301 // add available groupings
302 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
303 $options = array();
304 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
305 foreach ($groupings as $grouping) {
306 $options[$grouping->id] = format_string($grouping->name);
309 $gr_el =& $mform->getElement('defaultgroupingid');
310 $gr_el->load($options);
313 // add course format options
314 $formatvalue = $mform->getElementValue('format');
315 if (is_array($formatvalue) && !empty($formatvalue)) {
316 $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
317 $newel = $mform->createElement('header', '', get_string('courseformatoptions', 'moodle',
318 $courseformat->get_format_name()));
319 $mform->insertElementBefore($newel, 'addcourseformatoptionshere');
321 $elements = $courseformat->create_edit_form_elements($mform);
322 for ($i = 0; $i < count($elements); $i++) {
323 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
324 'addcourseformatoptionshere');
329 /// perform some extra moodle validation
330 function validation($data, $files) {
331 global $DB, $CFG;
333 $errors = parent::validation($data, $files);
334 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
335 if (!empty($data['id'])) {
336 unset($foundcourses[$data['id']]);
338 if (!empty($foundcourses)) {
339 foreach ($foundcourses as $foundcourse) {
340 $foundcoursenames[] = $foundcourse->fullname;
342 $foundcoursenamestring = implode(',', $foundcoursenames);
343 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
347 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
349 $courseformat = course_get_format((object)array('format' => $data['format']));
350 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
351 if (!empty($formaterrors) && is_array($formaterrors)) {
352 $errors = array_merge($errors, $formaterrors);
355 return $errors;