Use $this->error for only error report
[moodle-linuxchix.git] / course / edit_form.php
blob6a5d1d2c883bc097458c1ca178c365d9b77da9fc
1 <?php //$Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class course_edit_form extends moodleform {
7 function definition() {
8 global $USER, $CFG;
10 $mform =& $this->_form;
12 $course = $this->_customdata['course'];
13 $category = $this->_customdata['category'];
15 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
16 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
18 $disable_meta = false; // basic meta course state protection; server-side security checks not needed
20 if (!empty($course)) {
21 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
22 $context = $coursecontext;
24 if (course_in_meta($course)) {
25 $disable_meta = get_string('metaalreadyinmeta');
27 } else if ($course->metacourse) {
28 if (count_records('course_meta', 'parent_course', $course->id) > 0) {
29 $disable_meta = get_string('metaalreadyhascourses');
32 } else {
33 $managers = count(get_users_by_capability($coursecontext, 'moodle/course:managemetacourse'));
34 $participants = count(get_users_by_capability($coursecontext, 'moodle/course:view'));
35 if ($participants > $managers) {
36 $disable_meta = get_string('metaalreadyhasenrolments');
39 } else {
40 $coursecontext = null;
41 $context = $categorycontext;
44 /// form definition with new course defaults
45 //--------------------------------------------------------------------------------
46 $mform->addElement('header','general', get_string('general', 'form'));
48 //must have create course capability in both categories in order to move course
49 if (has_capability('moodle/course:create', $categorycontext)) {
50 $displaylist = array();
51 $parentlist = array();
52 make_categories_list($displaylist, $parentlist);
53 foreach ($displaylist as $key=>$val) {
54 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $key))) {
55 unset($displaylist[$key]);
58 $mform->addElement('select', 'category', get_string('category'), $displaylist);
59 } else {
60 $mform->addElement('hidden', 'category', null);
62 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
63 $mform->setDefault('category', $category->id);
64 $mform->setType('category', PARAM_INT);
66 $mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
67 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
68 $mform->setDefault('fullname', get_string('defaultcoursefullname'));
69 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
70 $mform->setType('fullname', PARAM_MULTILANG);
72 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="15" size="10"');
73 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
74 $mform->setDefault('shortname', get_string('defaultcourseshortname'));
75 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
76 $mform->setType('shortname', PARAM_MULTILANG);
78 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
79 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
80 $mform->setType('idnumber', PARAM_RAW);
82 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
83 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
84 $mform->setDefault('summary', get_string('defaultcoursesummary'));
85 $mform->addRule('summary', get_string('missingsummary'), 'required', null, 'client');
86 $mform->setType('summary', PARAM_RAW);
88 $courseformats = get_list_of_plugins('course/format');
89 $formcourseformats = array();
90 foreach ($courseformats as $courseformat) {
91 $formcourseformats["$courseformat"] = get_string("format$courseformat","format_$courseformat");
92 if($formcourseformats["$courseformat"]=="[[format$courseformat]]") {
93 $formcourseformats["$courseformat"] = get_string("format$courseformat");
96 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
97 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
98 $mform->setDefault('format', 'weeks');
100 for ($i=1; $i<=52; $i++) {
101 $sectionmenu[$i] = "$i";
103 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
104 $mform->setDefault('numsections', 10);
106 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
107 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
108 $mform->setDefault('startdate', time() + 3600 * 24);
110 $choices = array();
111 $choices['0'] = get_string('hiddensectionscollapsed');
112 $choices['1'] = get_string('hiddensectionsinvisible');
113 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
114 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
115 $mform->setDefault('hiddensections', 0);
117 $options = range(0, 10);
118 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
119 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
120 $mform->setDefault('newsitems', 5);
122 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
123 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
124 $mform->setDefault('showgrades', 1);
126 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
127 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
128 $mform->setDefault('showreports', 0);
130 $choices = get_max_upload_sizes($CFG->maxbytes);
131 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
132 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
134 if (!empty($CFG->allowcoursethemes)) {
135 $themes=array();
136 $themes[''] = get_string('forceno');
137 $themes += get_list_of_themes();
138 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
141 $meta=array();
142 $meta[0] = get_string('no');
143 $meta[1] = get_string('yes');
144 if ($disable_meta === false) {
145 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
146 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
147 $mform->setDefault('metacourse', 0);
148 } else {
149 // no metacourse element - we do not want to change it anyway!
150 $mform->addElement('static', 'nometacourse', get_string('managemeta'),
151 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
152 $mform->setHelpButton('nometacourse', array('metacourse', get_string('metacourse')), true);
155 $roles = get_assignable_roles($context);
156 if (!empty($course)) {
157 // add current default role, so that it is selectable even when user can not assign it
158 if ($current_role = get_record('role', 'id', $course->defaultrole)) {
159 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
162 $choices = array();
163 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
164 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
165 } else {
166 $choices[0] = get_string('sitedefault');
168 $choices = $choices + $roles;
169 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
170 $mform->setDefault('defaultrole', 0);
172 //--------------------------------------------------------------------------------
173 $mform->addElement('header','enrolhdr', get_string('enrolments'));
175 $choices = array();
176 $modules = explode(',', $CFG->enrol_plugins_enabled);
177 foreach ($modules as $module) {
178 $name = get_string('enrolname', "enrol_$module");
179 $plugin = enrolment_factory::factory($module);
180 if (method_exists($plugin, 'print_entry')) {
181 $choices[$name] = $module;
184 asort($choices);
185 $choices = array_flip($choices);
186 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
187 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
188 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
190 $radio = array();
191 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
192 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
193 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
194 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
195 $mform->setHelpButton('enrollable', array('courseenrollable', get_string('enrollable')), true);
196 $mform->setDefault('enrollable', 1);
198 $enroldatestartgrp = array();
199 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
200 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
201 $mform->addGroup($enroldatestartgrp, 'enrolstartdategrp', get_string('enrolstartdate'), ' ', false);
202 $mform->setDefault('enrolstartdate', 0);
203 $mform->setDefault('enrolstartdisabled', 1);
204 $mform->disabledIf('enrolstartdategrp', 'enrolstartdisabled', 'checked');
206 $enroldateendgrp = array();
207 $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
208 $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
209 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
210 $mform->setDefault('enrolenddate', 0);
211 $mform->setDefault('enrolenddisabled', 1);
212 $mform->disabledIf('enroldateendgrp', 'enrolenddisabled', 'checked');
214 $periodmenu=array();
215 $periodmenu[0] = get_string('unlimited');
216 for ($i=1; $i<=365; $i++) {
217 $seconds = $i * 86400;
218 $periodmenu[$seconds] = get_string('numdays', '', $i);
220 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
221 $mform->setDefault('enrolperiod', 0);
223 //--------------------------------------------------------------------------------
224 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
226 $choices = array();
227 $choices['0'] = get_string('no');
228 $choices['1'] = get_string('yes');
229 $mform->addElement('select', 'expirynotify', get_string('notify'), $choices);
230 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
231 $mform->setDefault('expirynotify', 0);
233 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
234 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
235 $mform->setDefault('notifystudents', 0);
237 $thresholdmenu=array();
238 for ($i=1; $i<=30; $i++) {
239 $seconds = $i * 86400;
240 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
242 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
243 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
244 $mform->setDefault('expirythreshold', 10 * 86400);
246 //--------------------------------------------------------------------------------
247 $mform->addElement('header','', get_string('groups', 'group'));
249 $choices = array();
250 $choices[NOGROUPS] = get_string('no');
251 $choices[SEPARATEGROUPS] = get_string('separate');
252 $choices[VISIBLEGROUPS] = get_string('visible');
253 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
254 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
255 $mform->setDefault('groupmode', 0);
257 $choices = array();
258 $choices['0'] = get_string('no');
259 $choices['1'] = get_string('yes');
260 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
261 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
262 $mform->setDefault('groupmodeforce', 0);
264 //--------------------------------------------------------------------------------
265 $mform->addElement('header','', get_string('availability'));
267 $choices = array();
268 $choices['0'] = get_string('courseavailablenot');
269 $choices['1'] = get_string('courseavailable');
270 $mform->addElement('select', 'visible', get_string('availability'), $choices);
271 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
272 $mform->setDefault('visible', 1);
274 $mform->addElement('text', 'password', get_string('enrolmentkey'), 'size="25"');
275 $mform->setHelpButton('password', array('enrolmentkey', get_string('enrolmentkey')), true);
276 $mform->setDefault('password', '');
277 $mform->setType('password', PARAM_RAW);
279 $choices = array();
280 $choices['0'] = get_string('guestsno');
281 $choices['1'] = get_string('guestsyes');
282 $choices['2'] = get_string('guestskey');
283 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
284 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
285 $mform->setDefault('guest', 0);
287 if (!empty($course) && method_exists(enrolment_factory::factory($course->enrol), 'print_entry') && $course->enrol != 'manual') {
288 $costgroup=array();
289 $currencies = get_list_of_currencies();
290 $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
291 $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
292 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
293 //defining a rule for a form element within a group :
294 $costgrprules=array();
295 //set the message to null to tell Moodle to use a default message
296 //available for most rules, fetched from language pack (err_{rulename}).
297 $costgrprules['cost'][]=array(null, 'numeric', null, 'client');
298 $mform->addGroupRule('costgrp',$costgrprules);
299 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
300 $mform->setDefault('cost', '');
301 $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
305 //--------------------------------------------------------------------------------
306 $mform->addElement('header','', get_string('language'));
308 $languages=array();
309 $languages[''] = get_string('forceno');
310 $languages += get_list_of_languages();
311 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
313 //--------------------------------------------------------------------------------
314 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
315 $mform->addElement('header', '', get_string('restrictmodules'));
317 $options = array();
318 $options['0'] = get_string('no');
319 $options['1'] = get_string('yes');
320 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
321 $mods = array(0=>get_string('allownone'));
322 $mods += get_records_menu('modules', '','','','id, name');
325 $mform->addElement('select', 'allowedmods', get_string('to'), $mods,
326 array('multiple'=>'multiple', 'size'=>'10'));
327 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
328 } else {
329 $mform->addElement('hidden', 'restrictmodules', null);
331 if ($CFG->restrictmodulesfor == 'all') {
332 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
333 if (!empty($CFG->restrictbydefault)) {
334 $mform->setDefault('restrictmodules', 1);
337 $mform->setType('restrictmodules', PARAM_INT);
339 //--------------------------------------------------------------------------------
340 $this->add_action_buttons();
341 //--------------------------------------------------------------------------------
342 $mform->addElement('hidden', 'id', null);
343 $mform->setType('id', PARAM_INT);
345 // fill in default teacher and student names to keep backwards compatibility for a while
346 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
347 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
348 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
349 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
353 /// perform some extra moodle validation
354 function validation($data){
355 $errors= array();
356 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
357 if (!empty($data['id'])) {
358 unset($foundcourses[$data['id']]);
360 if (!empty($foundcourses)) {
361 foreach ($foundcourses as $foundcourse) {
362 $foundcoursenames[] = $foundcourse->fullname;
364 $foundcoursenamestring = implode(',', $foundcoursenames);
365 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
369 if (empty($data['enrolenddisabled'])){
370 if ($data['enrolenddate'] <= $data['enrolstartdate']){
371 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
375 if (0 == count($errors)){
376 return true;
377 } else {
378 return $errors;