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
{
12 function definition() {
13 global $USER, $CFG, $DB;
15 $mform = $this->_form
;
17 $course = $this->_customdata
['course']; // this contains the data of this form
18 $category = $this->_customdata
['category'];
19 $editoroptions = $this->_customdata
['editoroptions'];
20 $returnto = $this->_customdata
['returnto'];
22 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
23 $categorycontext = get_context_instance(CONTEXT_COURSECAT
, $category->id
);
25 if (!empty($course->id
)) {
26 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
);
27 $context = $coursecontext;
29 $coursecontext = null;
30 $context = $categorycontext;
33 $courseconfig = get_config('moodlecourse');
35 $this->course
= $course;
36 $this->context
= $context;
38 /// form definition with new course defaults
39 //--------------------------------------------------------------------------------
40 $mform->addElement('header','general', get_string('general', 'form'));
42 $mform->addElement('hidden', 'returnto', null);
43 $mform->setType('returnto', PARAM_ALPHANUM
);
44 $mform->setConstant('returnto', $returnto);
46 // verify permissions to change course category or keep current
47 if (empty($course->id
)) {
48 if (has_capability('moodle/course:create', $categorycontext)) {
49 $displaylist = array();
50 $parentlist = array();
51 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
52 $mform->addElement('select', 'category', get_string('category'), $displaylist);
53 $mform->addHelpButton('category', 'category');
54 $mform->setDefault('category', $category->id
);
56 $mform->addElement('hidden', 'category', null);
57 $mform->setType('category', PARAM_INT
);
58 $mform->setConstant('category', $category->id
);
61 if (has_capability('moodle/course:changecategory', $coursecontext)) {
62 $displaylist = array();
63 $parentlist = array();
64 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
65 if (!isset($displaylist[$course->category
])) {
67 $displaylist[$course->category
] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category
)));
69 $mform->addElement('select', 'category', get_string('category'), $displaylist);
70 $mform->addHelpButton('category', 'category');
73 $mform->addElement('hidden', 'category', null);
74 $mform->setType('category', PARAM_INT
);
75 $mform->setConstant('category', $course->category
);
79 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
80 $mform->addHelpButton('fullname', 'fullnamecourse');
81 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
82 $mform->setType('fullname', PARAM_MULTILANG
);
83 if (!empty($course->id
) and !has_capability('moodle/course:changefullname', $coursecontext)) {
84 $mform->hardFreeze('fullname');
85 $mform->setConstant('fullname', $course->fullname
);
88 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
89 $mform->addHelpButton('shortname', 'shortnamecourse');
90 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
91 $mform->setType('shortname', PARAM_MULTILANG
);
92 if (!empty($course->id
) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
93 $mform->hardFreeze('shortname');
94 $mform->setConstant('shortname', $course->shortname
);
97 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
98 $mform->addHelpButton('idnumber', 'idnumbercourse');
99 $mform->setType('idnumber', PARAM_RAW
);
100 if (!empty($course->id
) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
101 $mform->hardFreeze('idnumber');
102 $mform->setConstants('idnumber', $course->idnumber
);
106 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
107 $mform->addHelpButton('summary_editor', 'coursesummary');
108 $mform->setType('summary_editor', PARAM_RAW
);
110 if (!empty($course->id
) and !has_capability('moodle/course:changesummary', $coursecontext)) {
111 $mform->hardFreeze('summary_editor');
114 $courseformats = get_plugin_list('format');
115 $formcourseformats = array();
116 foreach ($courseformats as $courseformat => $formatdir) {
117 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
119 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
120 $mform->addHelpButton('format', 'format');
121 $mform->setDefault('format', $courseconfig->format
);
123 for ($i = 0; $i <= $courseconfig->maxsections
; $i++
) {
124 $sectionmenu[$i] = "$i";
126 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
127 $mform->setDefault('numsections', $courseconfig->numsections
);
129 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
130 $mform->addHelpButton('startdate', 'startdate');
131 $mform->setDefault('startdate', time() +
3600 * 24);
134 $choices['0'] = get_string('hiddensectionscollapsed');
135 $choices['1'] = get_string('hiddensectionsinvisible');
136 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
137 $mform->addHelpButton('hiddensections', 'hiddensections');
138 $mform->setDefault('hiddensections', $courseconfig->hiddensections
);
140 $options = range(0, 10);
141 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
142 $mform->addHelpButton('newsitems', 'newsitemsnumber');
143 $mform->setDefault('newsitems', $courseconfig->newsitems
);
145 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
146 $mform->addHelpButton('showgrades', 'showgrades');
147 $mform->setDefault('showgrades', $courseconfig->showgrades
);
149 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
150 $mform->addHelpButton('showreports', 'showreports');
151 $mform->setDefault('showreports', $courseconfig->showreports
);
153 $choices = get_max_upload_sizes($CFG->maxbytes
);
154 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
155 $mform->addHelpButton('maxbytes', 'maximumupload');
156 $mform->setDefault('maxbytes', $courseconfig->maxbytes
);
158 if (!empty($course->legacyfiles
) or !empty($CFG->legacyfilesinnewcourses
)) {
159 if (empty($course->legacyfiles
)) {
160 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
161 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
163 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
165 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
166 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
167 if (!isset($courseconfig->legacyfiles
)) {
168 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
169 $courseconfig->legacyfiles
= 0;
171 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles
);
174 if (!empty($CFG->allowcoursethemes
)) {
175 $themeobjects = get_list_of_themes();
177 $themes[''] = get_string('forceno');
178 foreach ($themeobjects as $key=>$theme) {
179 if (empty($theme->hidefromselector
)) {
180 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name
);
183 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
186 //--------------------------------------------------------------------------------
187 enrol_course_edit_form($mform, $course, $context);
189 //--------------------------------------------------------------------------------
190 $mform->addElement('header','', get_string('groups', 'group'));
193 $choices[NOGROUPS
] = get_string('groupsnone', 'group');
194 $choices[SEPARATEGROUPS
] = get_string('groupsseparate', 'group');
195 $choices[VISIBLEGROUPS
] = get_string('groupsvisible', 'group');
196 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
197 $mform->addHelpButton('groupmode', 'groupmode', 'group');
198 $mform->setDefault('groupmode', $courseconfig->groupmode
);
201 $choices['0'] = get_string('no');
202 $choices['1'] = get_string('yes');
203 $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
204 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
205 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce
);
207 //default groupings selector
209 $options[0] = get_string('none');
210 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
212 //--------------------------------------------------------------------------------
213 $mform->addElement('header','', get_string('availability'));
216 $choices['0'] = get_string('courseavailablenot');
217 $choices['1'] = get_string('courseavailable');
218 $mform->addElement('select', 'visible', get_string('availability'), $choices);
219 $mform->addHelpButton('visible', 'availability');
220 $mform->setDefault('visible', $courseconfig->visible
);
221 if (!has_capability('moodle/course:visibility', $context)) {
222 $mform->hardFreeze('visible');
223 if (!empty($course->id
)) {
224 $mform->setConstant('visible', $course->visible
);
226 $mform->setConstant('visible', $category->visible
);
230 //--------------------------------------------------------------------------------
231 $mform->addElement('header','', get_string('language'));
234 $languages[''] = get_string('forceno');
235 $languages +
= get_string_manager()->get_list_of_translations();
236 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
237 $mform->setDefault('lang', $courseconfig->lang
);
239 //--------------------------------------------------------------------------------
240 if (completion_info
::is_enabled_for_site()) {
241 $mform->addElement('header','', get_string('progress','completion'));
242 $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
243 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
244 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion
);
246 $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
247 $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol
);
248 $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
250 $mform->addElement('hidden', 'enablecompletion');
251 $mform->setType('enablecompletion', PARAM_INT
);
252 $mform->setDefault('enablecompletion',0);
254 $mform->addElement('hidden', 'completionstartonenrol');
255 $mform->setType('completionstartonenrol', PARAM_INT
);
256 $mform->setDefault('completionstartonenrol',0);
259 //--------------------------------------------------------------------------------
260 if (has_capability('moodle/site:config', $systemcontext)) {
261 if (((!empty($course->requested
) && $CFG->restrictmodulesfor
== 'requested') ||
$CFG->restrictmodulesfor
== 'all')) {
262 $mform->addElement('header', '', get_string('restrictmodules'));
265 $options['0'] = get_string('no');
266 $options['1'] = get_string('yes');
267 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
268 if (!empty($CFG->restrictbydefault
)) {
269 $mform->setDefault('restrictmodules', 1);
272 $mods = array(0=>get_string('allownone'));
273 $allmods = $DB->get_records_menu('modules', array('visible' => 1),
275 foreach ($allmods as $key => $value) {
276 // Add module to list unless it cannot be added by users anyway
277 if (plugin_supports('mod', $value, FEATURE_MOD_ARCHETYPE
, MOD_ARCHETYPE_OTHER
) !==
278 MOD_ARCHETYPE_SYSTEM
) {
279 $mods[$key] = get_string('pluginname', $value);
282 $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10'));
283 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
284 // defaults are already in $course
286 // remove any mod restriction
287 $mform->addElement('hidden', 'restrictmodules', 0);
288 $mform->setType('restrictmodules', PARAM_INT
);
291 $mform->addElement('hidden', 'restrictmodules');
292 $mform->setType('restrictmodules', PARAM_INT
);
293 if (empty($course->id
)) {
294 $mform->setConstant('restrictmodules', (int)($CFG->restrictmodulesfor
== 'all'));
297 $mform->setConstant('restrictmodules', $course->restrictmodules
);
301 /// customizable role names in this course
302 //--------------------------------------------------------------------------------
303 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
304 $mform->addHelpButton('rolerenaming', 'rolerenaming');
306 if ($roles = get_all_roles()) {
307 if ($coursecontext) {
308 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW
);
310 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE
);
311 foreach ($roles as $role) {
312 $mform->addElement('text', 'role_'.$role->id
, get_string('yourwordforx', '', $role->name
));
313 if (isset($role->localname
)) {
314 $mform->setDefault('role_'.$role->id
, $role->localname
);
316 $mform->setType('role_'.$role->id
, PARAM_TEXT
);
317 if (!in_array($role->id
, $assignableroles)) {
318 $mform->setAdvanced('role_'.$role->id
);
323 //--------------------------------------------------------------------------------
324 $this->add_action_buttons();
325 //--------------------------------------------------------------------------------
326 $mform->addElement('hidden', 'id', null);
327 $mform->setType('id', PARAM_INT
);
329 /// finally set the current form data
330 //--------------------------------------------------------------------------------
331 $this->set_data($course);
334 function definition_after_data() {
337 $mform = $this->_form
;
339 // add available groupings
340 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
342 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
343 foreach ($groupings as $grouping) {
344 $options[$grouping->id
] = format_string($grouping->name
);
347 $gr_el =& $mform->getElement('defaultgroupingid');
348 $gr_el->load($options);
353 /// perform some extra moodle validation
354 function validation($data, $files) {
357 $errors = parent
::validation($data, $files);
358 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
359 if (!empty($data['id'])) {
360 unset($foundcourses[$data['id']]);
362 if (!empty($foundcourses)) {
363 foreach ($foundcourses as $foundcourse) {
364 $foundcoursenames[] = $foundcourse->fullname
;
366 $foundcoursenamestring = implode(',', $foundcoursenames);
367 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
371 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context
));