Merge branch 'MDL-41151-master' of git://github.com/sammarshallou/moodle
[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');
7 require_once($CFG->libdir. '/coursecatlib.php');
9 class course_edit_form extends moodleform {
10     protected $course;
11     protected $context;
13     function definition() {
14         global $USER, $CFG, $DB, $PAGE;
16         $mform    = $this->_form;
17         $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
18                 array(array('formid' => $mform->getAttribute('id'))));
20         $course        = $this->_customdata['course']; // this contains the data of this form
21         $category      = $this->_customdata['category'];
22         $editoroptions = $this->_customdata['editoroptions'];
23         $returnto = $this->_customdata['returnto'];
25         $systemcontext   = context_system::instance();
26         $categorycontext = context_coursecat::instance($category->id);
28         if (!empty($course->id)) {
29             $coursecontext = context_course::instance($course->id);
30             $context = $coursecontext;
31         } else {
32             $coursecontext = null;
33             $context = $categorycontext;
34         }
36         $courseconfig = get_config('moodlecourse');
38         $this->course  = $course;
39         $this->context = $context;
41 /// form definition with new course defaults
42 //--------------------------------------------------------------------------------
43         $mform->addElement('header','general', get_string('general', 'form'));
45         $mform->addElement('hidden', 'returnto', null);
46         $mform->setType('returnto', PARAM_ALPHANUM);
47         $mform->setConstant('returnto', $returnto);
49         $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
50         $mform->addHelpButton('fullname', 'fullnamecourse');
51         $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
52         $mform->setType('fullname', PARAM_TEXT);
53         if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
54             $mform->hardFreeze('fullname');
55             $mform->setConstant('fullname', $course->fullname);
56         }
58         $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
59         $mform->addHelpButton('shortname', 'shortnamecourse');
60         $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
61         $mform->setType('shortname', PARAM_TEXT);
62         if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
63             $mform->hardFreeze('shortname');
64             $mform->setConstant('shortname', $course->shortname);
65         }
67         // Verify permissions to change course category or keep current.
68         if (empty($course->id)) {
69             if (has_capability('moodle/course:create', $categorycontext)) {
70                 $displaylist = coursecat::make_categories_list('moodle/course:create');
71                 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
72                 $mform->addHelpButton('category', 'coursecategory');
73                 $mform->setDefault('category', $category->id);
74             } else {
75                 $mform->addElement('hidden', 'category', null);
76                 $mform->setType('category', PARAM_INT);
77                 $mform->setConstant('category', $category->id);
78             }
79         } else {
80             if (has_capability('moodle/course:changecategory', $coursecontext)) {
81                 $displaylist = coursecat::make_categories_list('moodle/course:create');
82                 if (!isset($displaylist[$course->category])) {
83                     //always keep current
84                     $displaylist[$course->category] = coursecat::get($course->category, MUST_EXIST, true)->get_formatted_name();
85                 }
86                 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
87                 $mform->addHelpButton('category', 'coursecategory');
88             } else {
89                 //keep current
90                 $mform->addElement('hidden', 'category', null);
91                 $mform->setType('category', PARAM_INT);
92                 $mform->setConstant('category', $course->category);
93             }
94         }
96         $choices = array();
97         $choices['0'] = get_string('hide');
98         $choices['1'] = get_string('show');
99         $mform->addElement('select', 'visible', get_string('visible'), $choices);
100         $mform->addHelpButton('visible', 'visible');
101         $mform->setDefault('visible', $courseconfig->visible);
102         if (!empty($course->id)) {
103             if (!has_capability('moodle/course:visibility', $coursecontext)) {
104                 $mform->hardFreeze('visible');
105                 $mform->setConstant('visible', $course->visible);
106             }
107         } else {
108             if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
109                 $mform->hardFreeze('visible');
110                 $mform->setConstant('visible', $courseconfig->visible);
111             }
112         }
114         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
115         $mform->addHelpButton('startdate', 'startdate');
116         $mform->setDefault('startdate', time() + 3600 * 24);
118         $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
119         $mform->addHelpButton('idnumber', 'idnumbercourse');
120         $mform->setType('idnumber', PARAM_RAW);
121         if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
122             $mform->hardFreeze('idnumber');
123             $mform->setConstants('idnumber', $course->idnumber);
124         }
126         // Description.
127         $mform->addElement('header', 'descriptionhdr', get_string('description'));
128         $mform->setExpanded('descriptionhdr');
130         $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
131         $mform->addHelpButton('summary_editor', 'coursesummary');
132         $mform->setType('summary_editor', PARAM_RAW);
133         $summaryfields = 'summary_editor';
135         if ($overviewfilesoptions = course_overviewfiles_options($course)) {
136             $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
137             $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
138             $summaryfields .= ',overviewfiles_filemanager';
139         }
141         if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
142             // Remove the description header it does not contain anything any more.
143             $mform->removeElement('descriptionhdr');
144             $mform->hardFreeze($summaryfields);
145         }
147         // Course format.
148         $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
150         $courseformats = get_sorted_course_formats(true);
151         $formcourseformats = array();
152         foreach ($courseformats as $courseformat) {
153             $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
154         }
155         if (isset($course->format)) {
156             $course->format = course_get_format($course)->get_format(); // replace with default if not found
157             if (!in_array($course->format, $courseformats)) {
158                 // this format is disabled. Still display it in the dropdown
159                 $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
160                         get_string('pluginname', 'format_'.$course->format));
161             }
162         }
164         $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
165         $mform->addHelpButton('format', 'format');
166         $mform->setDefault('format', $courseconfig->format);
168         // Button to update format-specific options on format change (will be hidden by JavaScript).
169         $mform->registerNoSubmitButton('updatecourseformat');
170         $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
172         // Just a placeholder for the course format options.
173         $mform->addElement('hidden', 'addcourseformatoptionshere');
174         $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
176         // Appearance.
177         $mform->addElement('header', 'appearancehdr', get_string('appearance'));
179         if (!empty($CFG->allowcoursethemes)) {
180             $themeobjects = get_list_of_themes();
181             $themes=array();
182             $themes[''] = get_string('forceno');
183             foreach ($themeobjects as $key=>$theme) {
184                 if (empty($theme->hidefromselector)) {
185                     $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
186                 }
187             }
188             $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
189         }
191         $languages=array();
192         $languages[''] = get_string('forceno');
193         $languages += get_string_manager()->get_list_of_translations();
194         $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
195         $mform->setDefault('lang', $courseconfig->lang);
197         $options = range(0, 10);
198         $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
199         $mform->addHelpButton('newsitems', 'newsitemsnumber');
200         $mform->setDefault('newsitems', $courseconfig->newsitems);
202         $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
203         $mform->addHelpButton('showgrades', 'showgrades');
204         $mform->setDefault('showgrades', $courseconfig->showgrades);
206         $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
207         $mform->addHelpButton('showreports', 'showreports');
208         $mform->setDefault('showreports', $courseconfig->showreports);
210         // Files and uploads.
211         $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
213         if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
214             if (empty($course->legacyfiles)) {
215                 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
216                 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
217             } else {
218                 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
219             }
220             $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
221             $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
222             if (!isset($courseconfig->legacyfiles)) {
223                 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
224                 $courseconfig->legacyfiles = 0;
225             }
226             $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
227         }
229         // Handle non-existing $course->maxbytes on course creation.
230         $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
232         // Let's prepare the maxbytes popup.
233         $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
234         $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
235         $mform->addHelpButton('maxbytes', 'maximumupload');
236         $mform->setDefault('maxbytes', $courseconfig->maxbytes);
238         // Completion tracking.
239         if (completion_info::is_enabled_for_site()) {
240             $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
241             $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
242             $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
243             $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
244         } else {
245             $mform->addElement('hidden', 'enablecompletion');
246             $mform->setType('enablecompletion', PARAM_INT);
247             $mform->setDefault('enablecompletion', 0);
248         }
250 //--------------------------------------------------------------------------------
251         enrol_course_edit_form($mform, $course, $context);
253 //--------------------------------------------------------------------------------
254         $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
256         $choices = array();
257         $choices[NOGROUPS] = get_string('groupsnone', 'group');
258         $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
259         $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
260         $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
261         $mform->addHelpButton('groupmode', 'groupmode', 'group');
262         $mform->setDefault('groupmode', $courseconfig->groupmode);
264         $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
265         $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
266         $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
268         //default groupings selector
269         $options = array();
270         $options[0] = get_string('none');
271         $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
273 //--------------------------------------------------------------------------------
275 /// customizable role names in this course
276 //--------------------------------------------------------------------------------
277         $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
278         $mform->addHelpButton('rolerenaming', 'rolerenaming');
280         if ($roles = get_all_roles()) {
281             $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
282             $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
283             foreach ($roles as $role) {
284                 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->localname));
285                 $mform->setType('role_'.$role->id, PARAM_TEXT);
286             }
287         }
289 //--------------------------------------------------------------------------------
290         $this->add_action_buttons();
291 //--------------------------------------------------------------------------------
292         $mform->addElement('hidden', 'id', null);
293         $mform->setType('id', PARAM_INT);
295 /// finally set the current form data
296 //--------------------------------------------------------------------------------
297         $this->set_data($course);
298     }
300     function definition_after_data() {
301         global $DB;
303         $mform = $this->_form;
305         // add available groupings
306         if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
307             $options = array();
308             if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
309                 foreach ($groupings as $grouping) {
310                     $options[$grouping->id] = format_string($grouping->name);
311                 }
312             }
313             $gr_el =& $mform->getElement('defaultgroupingid');
314             $gr_el->load($options);
315         }
317         // add course format options
318         $formatvalue = $mform->getElementValue('format');
319         if (is_array($formatvalue) && !empty($formatvalue)) {
320             $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
322             $elements = $courseformat->create_edit_form_elements($mform);
323             for ($i = 0; $i < count($elements); $i++) {
324                 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
325                         'addcourseformatoptionshere');
326             }
327         }
328     }
330 /// perform some extra moodle validation
331     function validation($data, $files) {
332         global $DB;
334         $errors = parent::validation($data, $files);
336         // Add field validation check for duplicate shortname.
337         if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
338             if (empty($data['id']) || $course->id != $data['id']) {
339                 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
340             }
341         }
343         // Add field validation check for duplicate idnumber.
344         if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
345             if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
346                 if (empty($data['id']) || $course->id != $data['id']) {
347                     $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
348                 }
349             }
350         }
352         $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
354         $courseformat = course_get_format((object)array('format' => $data['format']));
355         $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
356         if (!empty($formaterrors) && is_array($formaterrors)) {
357             $errors = array_merge($errors, $formaterrors);
358         }
360         return $errors;
361     }