3 defined('MOODLE_INTERNAL') || die;
5 require_once($CFG->libdir.'/formslib.php');
7 class course_edit_form extends moodleform {
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;
28 $coursecontext = null;
29 $context = $categorycontext;
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);
55 $mform->addElement('hidden', 'category', null);
56 $mform->setType('category', PARAM_INT);
57 $mform->setConstant('category', $category->id);
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])) {
66 $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
68 $mform->addElement('select', 'category', get_string('category'), $displaylist);
69 $mform->addHelpButton('category', 'category');
72 $mform->addElement('hidden', 'category', null);
73 $mform->setType('category', PARAM_INT);
74 $mform->setConstant('category', $course->category);
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);
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);
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);
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');
113 $courseformats = get_plugin_list('format');
114 $formcourseformats = array();
115 foreach ($courseformats as $courseformat => $formatdir) {
116 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
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";
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);
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'));
162 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
164 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
165 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
166 if (!isset($courseconfig->legacyfiles)) {
167 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
168 $courseconfig->legacyfiles = 0;
170 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
173 if (!empty($CFG->allowcoursethemes)) {
174 $themeobjects = get_list_of_themes();
176 $themes[''] = get_string('forceno');
177 foreach ($themeobjects as $key=>$theme) {
178 $themes[$key] = $theme->name;
180 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
183 //--------------------------------------------------------------------------------
184 enrol_course_edit_form($mform, $course, $context);
186 //--------------------------------------------------------------------------------
187 $mform->addElement('header','', get_string('groups', 'group'));
190 $choices[NOGROUPS] = get_string('groupsnone', 'group');
191 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
192 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
193 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
194 $mform->addHelpButton('groupmode', 'groupmode', 'group');
195 $mform->setDefault('groupmode', $courseconfig->groupmode);
198 $choices['0'] = get_string('no');
199 $choices['1'] = get_string('yes');
200 $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
201 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
202 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
204 //default groupings selector
206 $options[0] = get_string('none');
207 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
209 //--------------------------------------------------------------------------------
210 $mform->addElement('header','', get_string('availability'));
213 $choices['0'] = get_string('courseavailablenot');
214 $choices['1'] = get_string('courseavailable');
215 $mform->addElement('select', 'visible', get_string('availability'), $choices);
216 $mform->addHelpButton('visible', 'availability');
217 $mform->setDefault('visible', $courseconfig->visible);
218 if (!has_capability('moodle/course:visibility', $context)) {
219 $mform->hardFreeze('visible');
220 if (!empty($course->id)) {
221 $mform->setConstant('visible', $course->visible);
223 $mform->setConstant('visible', $category->visible);
227 //--------------------------------------------------------------------------------
228 $mform->addElement('header','', get_string('language'));
231 $languages[''] = get_string('forceno');
232 $languages += get_string_manager()->get_list_of_translations();
233 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
234 $mform->setDefault('lang', $courseconfig->lang);
236 //--------------------------------------------------------------------------------
237 require_once($CFG->libdir.'/completionlib.php');
238 if(completion_info::is_enabled_for_site()) {
239 $mform->addElement('header','', get_string('progress','completion'));
240 $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
241 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
242 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
244 $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
245 $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
246 $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
248 $mform->addElement('hidden', 'enablecompletion');
249 $mform->setType('enablecompletion', PARAM_INT);
250 $mform->setDefault('enablecompletion',0);
252 $mform->addElement('hidden', 'completionstartonenrol');
253 $mform->setType('completionstartonenrol', PARAM_INT);
254 $mform->setDefault('completionstartonenrol',0);
257 //--------------------------------------------------------------------------------
258 if (has_capability('moodle/site:config', $systemcontext)) {
259 if (((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
260 $mform->addElement('header', '', get_string('restrictmodules'));
263 $options['0'] = get_string('no');
264 $options['1'] = get_string('yes');
265 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
266 if (!empty($CFG->restrictbydefault)) {
267 $mform->setDefault('restrictmodules', 1);
270 $mods = array(0=>get_string('allownone'));
271 $mods += $DB->get_records_menu('modules', array('visible'=>1), 'name', 'id, name');
272 $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10'));
273 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
274 // defaults are already in $course
276 // remove any mod restriction
277 $mform->addElement('hidden', 'restrictmodules', 0);
278 $mform->setType('restrictmodules', PARAM_INT);
281 $mform->addElement('hidden', 'restrictmodules');
282 $mform->setType('restrictmodules', PARAM_INT);
283 if (empty($course->id)) {
284 $mform->setConstant('restrictmodules', (int)($CFG->restrictmodulesfor == 'all'));
287 $mform->setConstant('restrictmodules', $course->restrictmodules);
291 /// customizable role names in this course
292 //--------------------------------------------------------------------------------
293 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
294 $mform->addHelpButton('rolerenaming', 'rolerenaming');
296 if ($roles = get_all_roles()) {
297 if ($coursecontext) {
298 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
300 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
301 foreach ($roles as $role) {
302 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
303 if (isset($role->localname)) {
304 $mform->setDefault('role_'.$role->id, $role->localname);
306 $mform->setType('role_'.$role->id, PARAM_TEXT);
307 if (!in_array($role->id, $assignableroles)) {
308 $mform->setAdvanced('role_'.$role->id);
313 //--------------------------------------------------------------------------------
314 $this->add_action_buttons();
315 //--------------------------------------------------------------------------------
316 $mform->addElement('hidden', 'id', null);
317 $mform->setType('id', PARAM_INT);
319 /// finally set the current form data
320 //--------------------------------------------------------------------------------
321 $this->set_data($course);
324 function definition_after_data() {
327 $mform = $this->_form;
329 // add available groupings
330 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
332 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
333 foreach ($groupings as $grouping) {
334 $options[$grouping->id] = format_string($grouping->name);
337 $gr_el =& $mform->getElement('defaultgroupingid');
338 $gr_el->load($options);
343 /// perform some extra moodle validation
344 function validation($data, $files) {
347 $errors = parent::validation($data, $files);
348 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
349 if (!empty($data['id'])) {
350 unset($foundcourses[$data['id']]);
352 if (!empty($foundcourses)) {
353 foreach ($foundcourses as $foundcourse) {
354 $foundcoursenames[] = $foundcourse->fullname;
356 $foundcoursenamestring = implode(',', $foundcoursenames);
357 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
361 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));