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'];
20 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
21 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
23 if (!empty($course->id)) {
24 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
25 $context = $coursecontext;
27 $coursecontext = null;
28 $context = $categorycontext;
31 $courseconfig = get_config('moodlecourse');
33 $this->course = $course;
34 $this->context = $context;
36 /// form definition with new course defaults
37 //--------------------------------------------------------------------------------
38 $mform->addElement('header','general', get_string('general', 'form'));
40 // verify permissions to change course category or keep current
41 if (empty($course->id)) {
42 if (has_capability('moodle/course:create', $categorycontext)) {
43 $displaylist = array();
44 $parentlist = array();
45 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
46 $mform->addElement('select', 'category', get_string('category'), $displaylist);
47 $mform->addHelpButton('category', 'category');
48 $mform->setDefault('category', $category->id);
50 $mform->addElement('hidden', 'category', null);
51 $mform->setType('category', PARAM_INT);
52 $mform->setConstant('category', $category->id);
55 if (has_capability('moodle/course:changecategory', $coursecontext)) {
56 $displaylist = array();
57 $parentlist = array();
58 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
59 if (!isset($displaylist[$course->category])) {
61 $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
63 $mform->addElement('select', 'category', get_string('category'), $displaylist);
64 $mform->addHelpButton('category', 'category');
67 $mform->addElement('hidden', 'category', null);
68 $mform->setType('category', PARAM_INT);
69 $mform->setConstant('category', $course->category);
73 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
74 $mform->addHelpButton('fullname', 'fullnamecourse');
75 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
76 $mform->setType('fullname', PARAM_MULTILANG);
77 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
78 $mform->hardFreeze('fullname');
79 $mform->setConstant('fullname', $course->fullname);
82 $mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
83 $mform->addHelpButton('shortname', 'shortnamecourse');
84 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
85 $mform->setType('shortname', PARAM_MULTILANG);
86 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
87 $mform->hardFreeze('shortname');
88 $mform->setConstant('shortname', $course->shortname);
91 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
92 $mform->addHelpButton('idnumber', 'idnumbercourse');
93 $mform->setType('idnumber', PARAM_RAW);
94 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
95 $mform->hardFreeze('idnumber');
96 $mform->setConstants('idnumber', $course->idnumber);
100 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
101 $mform->addHelpButton('summary_editor', 'coursesummary');
102 $mform->setType('summary_editor', PARAM_RAW);
104 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
105 $mform->hardFreeze('summary_editor');
108 $courseformats = get_plugin_list('format');
109 $formcourseformats = array();
110 foreach ($courseformats as $courseformat => $formatdir) {
111 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
113 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
114 $mform->addHelpButton('format', 'format');
115 $mform->setDefault('format', $courseconfig->format);
117 for ($i=1; $i<=52; $i++) {
118 $sectionmenu[$i] = "$i";
120 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
121 $mform->setDefault('numsections', $courseconfig->numsections);
123 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
124 $mform->addHelpButton('startdate', 'startdate');
125 $mform->setDefault('startdate', time() + 3600 * 24);
128 $choices['0'] = get_string('hiddensectionscollapsed');
129 $choices['1'] = get_string('hiddensectionsinvisible');
130 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
131 $mform->addHelpButton('hiddensections', 'hiddensections');
132 $mform->setDefault('hiddensections', $courseconfig->hiddensections);
134 $options = range(0, 10);
135 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
136 $mform->addHelpButton('newsitems', 'newsitemsnumber');
137 $mform->setDefault('newsitems', $courseconfig->newsitems);
139 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
140 $mform->addHelpButton('showgrades', 'showgrades');
141 $mform->setDefault('showgrades', $courseconfig->showgrades);
143 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
144 $mform->addHelpButton('showreports', 'showreports');
145 $mform->setDefault('showreports', $courseconfig->showreports);
147 $choices = get_max_upload_sizes($CFG->maxbytes);
148 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
149 $mform->addHelpButton('maxbytes', 'maximumupload');
150 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
152 if (!empty($course->legacyfiles)) {
153 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
154 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
155 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
158 if (!empty($CFG->allowcoursethemes)) {
159 $themeobjects = get_list_of_themes();
161 $themes[''] = get_string('forceno');
162 foreach ($themeobjects as $key=>$theme) {
163 $themes[$key] = $theme->name;
165 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
168 //--------------------------------------------------------------------------------
169 enrol_course_edit_form($mform, $course, $context);
171 //--------------------------------------------------------------------------------
172 $mform->addElement('header','', get_string('groups', 'group'));
175 $choices[NOGROUPS] = get_string('groupsnone', 'group');
176 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
177 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
178 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
179 $mform->addHelpButton('groupmode', 'groupmode', 'group');
180 $mform->setDefault('groupmode', $courseconfig->groupmode);
183 $choices['0'] = get_string('no');
184 $choices['1'] = get_string('yes');
185 $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
186 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
187 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
189 //default groupings selector
191 $options[0] = get_string('none');
192 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
194 //--------------------------------------------------------------------------------
195 $mform->addElement('header','', get_string('availability'));
198 $choices['0'] = get_string('courseavailablenot');
199 $choices['1'] = get_string('courseavailable');
200 $mform->addElement('select', 'visible', get_string('availability'), $choices);
201 $mform->addHelpButton('visible', 'availability');
202 $mform->setDefault('visible', $courseconfig->visible);
203 if (!has_capability('moodle/course:visibility', $context)) {
204 $mform->hardFreeze('visible');
205 if (!empty($course->id)) {
206 $mform->setConstant('visible', $course->visible);
208 $mform->setConstant('visible', $category->visible);
212 //--------------------------------------------------------------------------------
213 $mform->addElement('header','', get_string('language'));
216 $languages[''] = get_string('forceno');
217 $languages += get_string_manager()->get_list_of_translations();
218 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
219 $mform->setDefault('lang', $courseconfig->lang);
221 //--------------------------------------------------------------------------------
222 require_once($CFG->libdir.'/completionlib.php');
223 if(completion_info::is_enabled_for_site()) {
224 $mform->addElement('header','', get_string('progress','completion'));
225 $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
226 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
227 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
229 $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
230 $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
231 $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
233 $mform->addElement('hidden', 'enablecompletion');
234 $mform->setType('enablecompletion', PARAM_INT);
235 $mform->setDefault('enablecompletion',0);
237 $mform->addElement('hidden', 'completionstartonenrol');
238 $mform->setType('completionstartonenrol', PARAM_INT);
239 $mform->setDefault('completionstartonenrol',0);
242 //--------------------------------------------------------------------------------
243 if (has_capability('moodle/site:config', $systemcontext)) {
244 if (((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
245 $mform->addElement('header', '', get_string('restrictmodules'));
248 $options['0'] = get_string('no');
249 $options['1'] = get_string('yes');
250 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
251 if (!empty($CFG->restrictbydefault)) {
252 $mform->setDefault('restrictmodules', 1);
255 $mods = array(0=>get_string('allownone'));
256 $mods += $DB->get_records_menu('modules', array('visible'=>1), 'name', 'id, name');
257 $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10'));
258 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
259 // defaults are already in $course
261 // remove any mod restriction
262 $mform->addElement('hidden', 'restrictmodules', 0);
263 $mform->setType('restrictmodules', PARAM_INT);
266 $mform->addElement('hidden', 'restrictmodules');
267 $mform->setType('restrictmodules', PARAM_INT);
268 if (empty($course->id)) {
269 $mform->setConstant('restrictmodules', (int)($CFG->restrictmodulesfor == 'all'));
272 $mform->setConstant('restrictmodules', $course->restrictmodules);
276 /// customizable role names in this course
277 //--------------------------------------------------------------------------------
278 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
279 $mform->addHelpButton('rolerenaming', 'rolerenaming');
281 if ($roles = get_all_roles()) {
282 if ($coursecontext) {
283 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
285 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
286 foreach ($roles as $role) {
287 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
288 if (isset($role->localname)) {
289 $mform->setDefault('role_'.$role->id, $role->localname);
291 $mform->setType('role_'.$role->id, PARAM_TEXT);
292 if (!in_array($role->id, $assignableroles)) {
293 $mform->setAdvanced('role_'.$role->id);
298 //--------------------------------------------------------------------------------
299 $this->add_action_buttons();
300 //--------------------------------------------------------------------------------
301 $mform->addElement('hidden', 'id', null);
302 $mform->setType('id', PARAM_INT);
304 /// finally set the current form data
305 //--------------------------------------------------------------------------------
306 $this->set_data($course);
309 function definition_after_data() {
312 $mform = $this->_form;
314 // add available groupings
315 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
317 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
318 foreach ($groupings as $grouping) {
319 $options[$grouping->id] = format_string($grouping->name);
322 $gr_el =& $mform->getElement('defaultgroupingid');
323 $gr_el->load($options);
328 /// perform some extra moodle validation
329 function validation($data, $files) {
332 $errors = parent::validation($data, $files);
333 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
334 if (!empty($data['id'])) {
335 unset($foundcourses[$data['id']]);
337 if (!empty($foundcourses)) {
338 foreach ($foundcourses as $foundcourse) {
339 $foundcoursenames[] = $foundcourse->fullname;
341 $foundcoursenamestring = implode(',', $foundcoursenames);
342 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
346 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));