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');
10 * The form for handling editing a course.
12 class course_edit_form
extends moodleform
{
19 function definition() {
22 $mform = $this->_form
;
23 $PAGE->requires
->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
24 array(array('formid' => $mform->getAttribute('id'))));
26 $course = $this->_customdata
['course']; // this contains the data of this form
27 $category = $this->_customdata
['category'];
28 $editoroptions = $this->_customdata
['editoroptions'];
29 $returnto = $this->_customdata
['returnto'];
30 $returnurl = $this->_customdata
['returnurl'];
32 $systemcontext = context_system
::instance();
33 $categorycontext = context_coursecat
::instance($category->id
);
35 if (!empty($course->id
)) {
36 $coursecontext = context_course
::instance($course->id
);
37 $context = $coursecontext;
39 $coursecontext = null;
40 $context = $categorycontext;
43 $courseconfig = get_config('moodlecourse');
45 $this->course
= $course;
46 $this->context
= $context;
48 // Form definition with new course defaults.
49 $mform->addElement('header','general', get_string('general', 'form'));
51 $mform->addElement('hidden', 'returnto', null);
52 $mform->setType('returnto', PARAM_ALPHANUM
);
53 $mform->setConstant('returnto', $returnto);
55 $mform->addElement('hidden', 'returnurl', null);
56 $mform->setType('returnurl', PARAM_LOCALURL
);
57 $mform->setConstant('returnurl', $returnurl);
59 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
60 $mform->addHelpButton('fullname', 'fullnamecourse');
61 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
62 $mform->setType('fullname', PARAM_TEXT
);
63 if (!empty($course->id
) and !has_capability('moodle/course:changefullname', $coursecontext)) {
64 $mform->hardFreeze('fullname');
65 $mform->setConstant('fullname', $course->fullname
);
68 $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
69 $mform->addHelpButton('shortname', 'shortnamecourse');
70 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
71 $mform->setType('shortname', PARAM_TEXT
);
72 if (!empty($course->id
) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
73 $mform->hardFreeze('shortname');
74 $mform->setConstant('shortname', $course->shortname
);
77 // Verify permissions to change course category or keep current.
78 if (empty($course->id
)) {
79 if (has_capability('moodle/course:create', $categorycontext)) {
80 $displaylist = coursecat
::make_categories_list('moodle/course:create');
81 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
82 $mform->addHelpButton('category', 'coursecategory');
83 $mform->setDefault('category', $category->id
);
85 $mform->addElement('hidden', 'category', null);
86 $mform->setType('category', PARAM_INT
);
87 $mform->setConstant('category', $category->id
);
90 if (has_capability('moodle/course:changecategory', $coursecontext)) {
91 $displaylist = coursecat
::make_categories_list('moodle/course:create');
92 if (!isset($displaylist[$course->category
])) {
94 $displaylist[$course->category
] = coursecat
::get($course->category
, MUST_EXIST
, true)->get_formatted_name();
96 $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
97 $mform->addHelpButton('category', 'coursecategory');
100 $mform->addElement('hidden', 'category', null);
101 $mform->setType('category', PARAM_INT
);
102 $mform->setConstant('category', $course->category
);
107 $choices['0'] = get_string('hide');
108 $choices['1'] = get_string('show');
109 $mform->addElement('select', 'visible', get_string('visible'), $choices);
110 $mform->addHelpButton('visible', 'visible');
111 $mform->setDefault('visible', $courseconfig->visible
);
112 if (!empty($course->id
)) {
113 if (!has_capability('moodle/course:visibility', $coursecontext)) {
114 $mform->hardFreeze('visible');
115 $mform->setConstant('visible', $course->visible
);
118 if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
119 $mform->hardFreeze('visible');
120 $mform->setConstant('visible', $courseconfig->visible
);
124 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
125 $mform->addHelpButton('startdate', 'startdate');
126 $mform->setDefault('startdate', time() +
3600 * 24);
128 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
129 $mform->addHelpButton('idnumber', 'idnumbercourse');
130 $mform->setType('idnumber', PARAM_RAW
);
131 if (!empty($course->id
) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
132 $mform->hardFreeze('idnumber');
133 $mform->setConstants('idnumber', $course->idnumber
);
137 $mform->addElement('header', 'descriptionhdr', get_string('description'));
138 $mform->setExpanded('descriptionhdr');
140 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
141 $mform->addHelpButton('summary_editor', 'coursesummary');
142 $mform->setType('summary_editor', PARAM_RAW
);
143 $summaryfields = 'summary_editor';
145 if ($overviewfilesoptions = course_overviewfiles_options($course)) {
146 $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
147 $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
148 $summaryfields .= ',overviewfiles_filemanager';
151 if (!empty($course->id
) and !has_capability('moodle/course:changesummary', $coursecontext)) {
152 // Remove the description header it does not contain anything any more.
153 $mform->removeElement('descriptionhdr');
154 $mform->hardFreeze($summaryfields);
158 $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
160 $courseformats = get_sorted_course_formats(true);
161 $formcourseformats = array();
162 foreach ($courseformats as $courseformat) {
163 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
165 if (isset($course->format
)) {
166 $course->format
= course_get_format($course)->get_format(); // replace with default if not found
167 if (!in_array($course->format
, $courseformats)) {
168 // this format is disabled. Still display it in the dropdown
169 $formcourseformats[$course->format
] = get_string('withdisablednote', 'moodle',
170 get_string('pluginname', 'format_'.$course->format
));
174 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
175 $mform->addHelpButton('format', 'format');
176 $mform->setDefault('format', $courseconfig->format
);
178 // Button to update format-specific options on format change (will be hidden by JavaScript).
179 $mform->registerNoSubmitButton('updatecourseformat');
180 $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
182 // Just a placeholder for the course format options.
183 $mform->addElement('hidden', 'addcourseformatoptionshere');
184 $mform->setType('addcourseformatoptionshere', PARAM_BOOL
);
187 $mform->addElement('header', 'appearancehdr', get_string('appearance'));
189 if (!empty($CFG->allowcoursethemes
)) {
190 $themeobjects = get_list_of_themes();
192 $themes[''] = get_string('forceno');
193 foreach ($themeobjects as $key=>$theme) {
194 if (empty($theme->hidefromselector
)) {
195 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name
);
198 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
202 $languages[''] = get_string('forceno');
203 $languages +
= get_string_manager()->get_list_of_translations();
204 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
205 $mform->setDefault('lang', $courseconfig->lang
);
207 // Multi-Calendar Support - see MDL-18375.
208 $calendartypes = \core_calendar\type_factory
::get_list_of_calendar_types();
209 // We do not want to show this option unless there is more than one calendar type to display.
210 if (count($calendartypes) > 1) {
211 $calendars = array();
212 $calendars[''] = get_string('forceno');
213 $calendars +
= $calendartypes;
214 $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
217 $options = range(0, 10);
218 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
219 $mform->addHelpButton('newsitems', 'newsitemsnumber');
220 $mform->setDefault('newsitems', $courseconfig->newsitems
);
222 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
223 $mform->addHelpButton('showgrades', 'showgrades');
224 $mform->setDefault('showgrades', $courseconfig->showgrades
);
226 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
227 $mform->addHelpButton('showreports', 'showreports');
228 $mform->setDefault('showreports', $courseconfig->showreports
);
230 // Files and uploads.
231 $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
233 if (!empty($course->legacyfiles
) or !empty($CFG->legacyfilesinnewcourses
)) {
234 if (empty($course->legacyfiles
)) {
235 //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
236 $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
238 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
240 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
241 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
242 if (!isset($courseconfig->legacyfiles
)) {
243 // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
244 $courseconfig->legacyfiles
= 0;
246 $mform->setDefault('legacyfiles', $courseconfig->legacyfiles
);
249 // Handle non-existing $course->maxbytes on course creation.
250 $coursemaxbytes = !isset($course->maxbytes
) ?
null : $course->maxbytes
;
252 // Let's prepare the maxbytes popup.
253 $choices = get_max_upload_sizes($CFG->maxbytes
, 0, 0, $coursemaxbytes);
254 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
255 $mform->addHelpButton('maxbytes', 'maximumupload');
256 $mform->setDefault('maxbytes', $courseconfig->maxbytes
);
258 // Completion tracking.
259 if (completion_info
::is_enabled_for_site()) {
260 $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
261 $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
262 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion
);
263 $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
265 $mform->addElement('hidden', 'enablecompletion');
266 $mform->setType('enablecompletion', PARAM_INT
);
267 $mform->setDefault('enablecompletion', 0);
270 enrol_course_edit_form($mform, $course, $context);
272 $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
275 $choices[NOGROUPS
] = get_string('groupsnone', 'group');
276 $choices[SEPARATEGROUPS
] = get_string('groupsseparate', 'group');
277 $choices[VISIBLEGROUPS
] = get_string('groupsvisible', 'group');
278 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
279 $mform->addHelpButton('groupmode', 'groupmode', 'group');
280 $mform->setDefault('groupmode', $courseconfig->groupmode
);
282 $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
283 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
284 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce
);
286 //default groupings selector
288 $options[0] = get_string('none');
289 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
291 if ((empty($course->id
) && guess_if_creator_will_have_course_capability('moodle/course:renameroles', $categorycontext))
292 ||
(!empty($course->id
) && has_capability('moodle/course:renameroles', $coursecontext))) {
293 // Customizable role names in this course.
294 $mform->addElement('header', 'rolerenaming', get_string('rolerenaming'));
295 $mform->addHelpButton('rolerenaming', 'rolerenaming');
297 if ($roles = get_all_roles()) {
298 $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL
);
299 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE
);
300 foreach ($roles as $role) {
301 $mform->addElement('text', 'role_' . $role->id
, get_string('yourwordforx', '', $role->localname
));
302 $mform->setType('role_' . $role->id
, PARAM_TEXT
);
307 if (!empty($CFG->usetags
) &&
308 ((empty($course->id
) && guess_if_creator_will_have_course_capability('moodle/course:tag', $categorycontext))
309 ||
(!empty($course->id
) && has_capability('moodle/course:tag', $coursecontext)))) {
310 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
311 $mform->addElement('tags', 'tags', get_string('tags'));
314 // When two elements we need a group.
315 $buttonarray = array();
316 $classarray = array('class' => 'form-submit');
317 if ($returnto !== 0) {
318 $buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'), $classarray);
320 $buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'), $classarray);
321 $buttonarray[] = &$mform->createElement('cancel');
322 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
323 $mform->closeHeaderBefore('buttonar');
325 $mform->addElement('hidden', 'id', null);
326 $mform->setType('id', PARAM_INT
);
328 // Finally set the current form data
329 $this->set_data($course);
333 * Fill in the current page data for this course.
335 function definition_after_data() {
338 $mform = $this->_form
;
340 // add available groupings
341 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
343 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
344 foreach ($groupings as $grouping) {
345 $options[$grouping->id
] = format_string($grouping->name
);
348 core_collator
::asort($options);
349 $gr_el =& $mform->getElement('defaultgroupingid');
350 $gr_el->load($options);
353 // add course format options
354 $formatvalue = $mform->getElementValue('format');
355 if (is_array($formatvalue) && !empty($formatvalue)) {
356 $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
358 $elements = $courseformat->create_edit_form_elements($mform);
359 for ($i = 0; $i < count($elements); $i++
) {
360 $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
361 'addcourseformatoptionshere');
370 * @param array $files
371 * @return array the errors that were found
373 function validation($data, $files) {
376 $errors = parent
::validation($data, $files);
378 // Add field validation check for duplicate shortname.
379 if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE
)) {
380 if (empty($data['id']) ||
$course->id
!= $data['id']) {
381 $errors['shortname'] = get_string('shortnametaken', '', $course->fullname
);
385 // Add field validation check for duplicate idnumber.
386 if (!empty($data['idnumber']) && (empty($data['id']) ||
$this->course
->idnumber
!= $data['idnumber'])) {
387 if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE
)) {
388 if (empty($data['id']) ||
$course->id
!= $data['id']) {
389 $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname
);
394 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context
));
396 $courseformat = course_get_format((object)array('format' => $data['format']));
397 $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
398 if (!empty($formaterrors) && is_array($formaterrors)) {
399 $errors = array_merge($errors, $formaterrors);