Fixed some words
[moodle.git] / course / edit_form.php
CommitLineData
a81bf9c3 1<?php //$Id$
2
3class course_edit_form extends moodleform {
4
5 function definition() {
6 global $USER, $CFG;
7
8 $mform =& $this->_form;
9 $renderer =& $mform->defaultRenderer();
10
11 $course = $this->_customdata['course'];
12 $category = $this->_customdata['category'];
13
14 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
15 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
16
17 $disable_meta = false; // basic meta course state protection; server-side security checks not needed
18
19 if (!empty($course)) {
20 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
21 $context = $coursecontext;
22
23 if (course_in_meta($course)) {
24 $disable_meta = get_string('metaalreadyinmeta');
25
26 } else if ($course->metacourse) {
27 if (count_records('course_meta', 'parent_course', $coursecourse->id) > 0) {
28 $disable_meta = get_string('metaalreadyhascourses');
29 }
30
31 } else {
32 $managers = count(get_users_by_capability($coursecontext, 'moodle/course:managemetacourse'));
33 $participants = count(get_users_by_capability($coursecontext, 'moodle/course:view'));
34 if ($participants > $managers) {
35 $disable_meta = get_string('metaalreadyhasenrolments');
36 }
461f5127 37 }
38 } else {
a81bf9c3 39 $coursecontext = null;
40 $context = $categorycontext;
41 }
42
43/// form definition with new course defaults
44//--------------------------------------------------------------------------------
45 $mform->addElement('header','general', get_string('general'));
46
47 //must have create course capability in both categories in order to move course
48 if (has_capability('moodle/course:create', $categorycontext)) {
49 $displaylist = array();
50 $parentlist = array();
51 make_categories_list($displaylist, $parentlist);
52 foreach ($displaylist as $key=>$val) {
53 if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $key))) {
54 unset($displaylist[$key]);
55 }
461f5127 56 }
a81bf9c3 57 $mform->addElement('select', 'category', get_string('category'), $displaylist);
58 } else {
59 $mform->addElement('hidden', 'category', null);
461f5127 60 }
a81bf9c3 61 $mform->setHelpButton('category', array('coursecategory', get_string('category')));
62 $mform->setDefault('category', $category->id);
63 $mform->setType('category', PARAM_INT);
461f5127 64
a81bf9c3 65 $mform->addElement('text','fullname', get_string('fullname'),'maxlength="254" size="50"');
66 $mform->setHelpButton('fullname', array('coursefullname', get_string('fullname')), true);
67 $mform->setDefault('fullname', get_string('defaultcoursefullname'));
68 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
69 $mform->setType('fullname', PARAM_MULTILANG);
461f5127 70
a81bf9c3 71 $mform->addElement('text','shortname', get_string('shortname'),'maxlength="15" size="10"');
72 $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
73 $mform->setDefault('shortname', get_string('defaultcourseshortname'));
74 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
75 $mform->setType('shortname', PARAM_MULTILANG);
01963b95 76
a81bf9c3 77 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
78 $mform->setHelpButton('idnumber', array('courseidnumber', get_string('idnumbercourse')), true);
79 $mform->setType('idnumber', PARAM_RAW);
01963b95 80
a81bf9c3 81 $mform->addElement('htmleditor','summary', get_string('summary'), array('rows'=> '10', 'cols'=>'65'));
82 $mform->setHelpButton('summary', array('text', get_string('helptext')), true);
83 $mform->setDefault('summary', get_string('defaultcoursesummary'));
84 $mform->addRule('summary', get_string('missingsummary'), 'required', null, 'client');
85 $mform->setType('summary', PARAM_RAW);
86
87 $courseformats = get_list_of_plugins('course/format');
88 $formcourseformats = array();
89 foreach ($courseformats as $courseformat) {
90 $formcourseformats["$courseformat"] = get_string("format$courseformat");
91 }
92 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
93 $mform->setHelpButton('format', array('courseformats', get_string('courseformats')), true);
94 $mform->setDefault('format', 'weeks');
95
96 for ($i=1; $i<=52; $i++) {
97 $sectionmenu[$i] = "$i";
98 }
99 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
100 $mform->setDefault('numsections', 10);
101
102 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
103 $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
104 $mform->setDefault('startdate', time() + 3600 * 24);
105
106 $choices = array();
107 $choices['0'] = get_string('hiddensectionscollapsed');
108 $choices['1'] = get_string('hiddensectionsinvisible');
109 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
110 $mform->setHelpButton('hiddensections', array('coursehiddensections', get_string('hiddensections')), true);
111 $mform->setDefault('hiddensections', 0);
112
113 $newsitem = get_string('newsitem');
114 $newsitems = get_string('newsitems');
115 $options = array('0' => "0 $newsitems",
116 '1' => "1 $newsitem",
117 '2' => "2 $newsitems",
118 '3' => "3 $newsitems",
119 '4' => "4 $newsitems",
120 '5' => "5 $newsitems",
121 '6' => "6 $newsitems",
122 '7' => "7 $newsitems",
123 '8' => "8 $newsitems",
124 '9' => "9 $newsitems",
125 '10' => "10 $newsitems");
126 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
127 $mform->setHelpButton('newsitems', array('coursenewsitems', get_string('newsitemsnumber')), true);
128 $mform->setDefault('newsitems', 5);
129
130 $choices = array();
131 $choices['0'] = get_string('no');
132 $choices['1'] = get_string('yes');
133 $mform->addElement('select', 'showgrades', get_string('showgrades'), $choices);
134 $mform->setHelpButton('showgrades', array('coursegrades', get_string('grades')), true);
135 $mform->setDefault('showgrades', 1);
136
137 $choices = array();
138 $choices['0'] = get_string('no');
139 $choices['1'] = get_string('yes');
140 $mform->addElement('select', 'showreports', get_string('showreports'), $choices);
141 $mform->setHelpButton('showreports', array('coursereports', get_string('activityreport')), true);
142 $mform->setDefault('showreports', 0);
143
144 $choices = get_max_upload_sizes($CFG->maxbytes);
145 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
146 $mform->setHelpButton('maxbytes', array('courseuploadsize', get_string('maximumupload')), true);
147
148 if (!empty($CFG->allowcoursethemes)) {
149 $themes=array();
150 $themes[''] = get_string('forceno');
151 $themes += get_list_of_themes();
152 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
153 }
461f5127 154
01963b95 155 $meta=array();
156 $meta[0] = get_string('no');
157 $meta[1] = get_string('yes');
a81bf9c3 158 if ($disable_meta === false) {
159 $mform->addElement('select', 'metacourse', get_string('managemeta'), $meta);
160 } else {
161 $mform->addElement('static', 'metacourse', get_string('managemeta'),
162 ((empty($course->metacourse)) ? $meta[0] : $meta[1]) . " - $disable_meta ");
163 }
164 $mform->setHelpButton('metacourse', array('metacourse', get_string('metacourse')), true);
165 $mform->setDefault('metacourse', 0);
01963b95 166
a81bf9c3 167 $roles = get_assignable_roles($context);
168 if (!empty($course)) {
169 // add current default role, so that it is selectable even when user can not assign it
170 if ($current_role = get_record('role', 'id', $course->defaultrole)) {
171 $roles[$current_role->id] = strip_tags(format_string($current_role->name, true));
172 }
173 }
174 $choices = array();
175 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
176 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
01963b95 177 } else {
a81bf9c3 178 $choices[0] = get_string('sitedefault');
01963b95 179 }
a81bf9c3 180 $choices = $choices + $roles;
181 $mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices);
182 $mform->setDefault('defaultrole', 0);
183
184//--------------------------------------------------------------------------------
185 $mform->addElement('header','enrolhdr', get_string('enrolments'));
186
187 $modules = explode(',', $CFG->enrol_plugins_enabled);
188 foreach ($modules as $module) {
189 $name = get_string('enrolname', "enrol_$module");
190 $plugin = enrolment_factory::factory($module);
191 if (method_exists($plugin, 'print_entry')) {
192 $choices[$name] = $module;
193 }
01963b95 194 }
a81bf9c3 195 asort($choices);
196 $choices = array_flip($choices);
197 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string('enrolname', "enrol_$CFG->enrol").')'), $choices);
198 $mform->addElement('select', 'enrol', get_string('enrolmentplugins'), $choices);
199 $mform->setHelpButton('enrol', array('courseenrolmentplugins', get_string('enrolmentplugins')), true);
01963b95 200
a81bf9c3 201 $radio = array();
202 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('no'), 0);
203 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('yes'), 1);
204 $radio[] = &MoodleQuickForm::createElement('radio', 'enrollable', null, get_string('enroldate'), 2);
205 $mform->addGroup($radio, 'enrollable', get_string('enrollable'), ' ', false);
206 $mform->setHelpButton('enrollable', array('courseenrollable', get_string('enrollable')), true);
207 $mform->setDefault('enrollable', 1);
208
209 $enroldatestartgrp = array();
210 $enroldatestartgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolstartdate');
211 $enroldatestartgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
212 $mform->addGroup($enroldatestartgrp, '', get_string('enrolstartdate'), ' ', false);
213 $mform->setDefault('enrolstartdate', 0);
214 $mform->setDefault('enrolstartdisabled', 1);
215
216 $enroldateendgrp = array();
217 $enroldateendgrp[] = &MoodleQuickForm::createElement('date_selector', 'enrolenddate');
218 $enroldateendgrp[] = &MoodleQuickForm::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
219 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
220 $mform->setDefault('enrolenddate', 0);
221 $mform->setDefault('enrolenddisabled', 1);
222
223 $periodmenu=array();
224 $periodmenu[0] = get_string('unlimited');
225 for ($i=1; $i<=365; $i++) {
226 $seconds = $i * 86400;
227 $periodmenu[$seconds] = get_string('numdays', '', $i);
228 }
229 $mform->addElement('select', 'enrolperiod', get_string('enrolperiod'), $periodmenu);
230 $mform->setDefault('enrolperiod', 0);
231
232//--------------------------------------------------------------------------------
233 $mform->addElement('header','expirynotifyhdr', get_string('expirynotify'));
234
235 $choices = array();
236 $choices['0'] = get_string('no');
237 $choices['1'] = get_string('yes');
238 $mform->addElement('select', 'expirynotify', get_string('expirynotify'), $choices);
239 $mform->setHelpButton('expirynotify', array('expirynotify', get_string('expirynotify')), true);
240 $mform->setDefault('expirynotify', 0);
241
242 $mform->addElement('select', 'notifystudents', get_string('expirynotifystudents'), $choices);
243 $mform->setHelpButton('notifystudents', array('expirynotifystudents', get_string('expirynotifystudents')), true);
244 $mform->setDefault('notifystudents', 0);
245
246 $thresholdmenu=array();
247 for ($i=1; $i<=30; $i++) {
248 $seconds = $i * 86400;
249 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
250 }
251 $mform->addElement('select', 'expirythreshold', get_string('expirythreshold'), $thresholdmenu);
252 $mform->setHelpButton('expirythreshold', array('expirythreshold', get_string('expirythreshold')), true);
253 $mform->setDefault('expirythreshold', 10 * 86400);
254
255//--------------------------------------------------------------------------------
256 $mform->addElement('header','', get_string('groupmode'));
257
258 $choices = array();
259 $choices[NOGROUPS] = get_string('groupsnone');
260 $choices[SEPARATEGROUPS] = get_string('groupsseparate');
261 $choices[VISIBLEGROUPS] = get_string('groupsvisible');
262 $mform->addElement('select', 'groupmode', get_string('groupmode'), $choices);
263 $mform->setHelpButton('groupmode', array('groupmode', get_string('groupmode')), true);
264 $mform->setDefault('groupmode', 0);
265
266 $choices = array();
267 $choices['0'] = get_string('no');
268 $choices['1'] = get_string('yes');
269 $mform->addElement('select', 'groupmodeforce', get_string('force'), $choices);
270 $mform->setHelpButton('groupmodeforce', array('groupmodeforce', get_string('groupmodeforce')), true);
271 $mform->setDefault('groupmodeforce', 0);
272
273//--------------------------------------------------------------------------------
274 $mform->addElement('header','', get_string('availability'));
275
276 $choices = array();
277 $choices['0'] = get_string('courseavailablenot');
278 $choices['1'] = get_string('courseavailable');
279 $mform->addElement('select', 'visible', get_string('availability'), $choices);
280 $mform->setHelpButton('visible', array('courseavailability', get_string('availability')), true);
281 $mform->setDefault('visible', 1);
282
283 $mform->addElement('text', 'password', get_string('enrolmentkey'), 'size="25"');
284 $mform->setHelpButton('password', array('enrolmentkey', get_string('enrolmentkey')), true);
285 $mform->setDefault('password', '');
286 $mform->setType('password', PARAM_RAW);
287
288 $choices = array();
289 $choices['0'] = get_string('guestsno');
290 $choices['1'] = get_string('guestsyes');
291 $choices['2'] = get_string('guestskey');
292 $mform->addElement('select', 'guest', get_string('opentoguests'), $choices);
293 $mform->setHelpButton('guest', array('guestaccess', get_string('opentoguests')), true);
294 $mform->setDefault('guest', 0);
295
296 if (!empty($course) && method_exists(enrolment_factory::factory($course->enrol), 'print_entry') && $course->enrol != 'manual') {
297 $costgroup=array();
298 $currencies = get_list_of_currencies();
299 $costgroup[]= &MoodleQuickForm::createElement('text','cost', '', 'maxlength="6" size="6"');
300 $costgroup[]= &MoodleQuickForm::createElement('select', 'currency', '', $currencies);
301 $mform->addGroup($costgroup, 'costgrp', get_string('cost'), '&nbsp;', false);
302 $mform->setHelpButton('costgrp', array('cost', get_string('cost')), true);
303 $mform->setDefault('cost', '');
304 $mform->setDefault('currency', empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency);
305 // TODO: fix rule usage and add proper error message
306 //$mform->addRule('cost', get_string('error'), 'numeric', null, 'client');
307 }
308
309//--------------------------------------------------------------------------------
310 $mform->addElement('header','', get_string('language'));
311
312 $languages=array();
313 $languages[''] = get_string('forceno');
314 $languages += get_list_of_languages();
315 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
316
317//--------------------------------------------------------------------------------
318 if (has_capability('moodle/site:config', $systemcontext) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
319 $mform->addElement('header', '', get_string('restrictmodules'));
320
321 $options = array();
322 $options['0'] = get_string('no');
323 $options['1'] = get_string('yes');
324 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options,
325 array('onChange'=>"document.getElementById('id_allowedmods').disabled=".
326 "((this.selectedIndex==0)?true:false);"));
327 $mods = array(0=>get_string('allownone'));
328 $mods += get_records_menu('modules', '','','','id, name');
329 $disabled=($mform->exportValue('restrictmodules')==1) ? array() :array('disabled' => 'disabled') ;
330
331
332 $mform->addElement('select', 'allowedmods', get_string('to'),$mods,
333 array('multiple'=>'multiple', 'size'=>'10', 'id'=>'allowedmods')
334 +$disabled);
335 }else {
336 $mform->addElement('hidden', 'restrictmodules', null);
337 }
338 if ($CFG->restrictmodulesfor == 'all') {
339 $mform->setDefault('allowedmods', explode(',',$CFG->defaultallowedmodules));
340 if (!empty($CFG->restrictbydefault)) {
341 $mform->setDefault('restrictmodules', 1);
342 }
343 }
344 $mform->setType('restrictmodules', PARAM_INT);
345
346//--------------------------------------------------------------------------------
347 $mform->addElement('submit', 'submit', get_string('savechanges'));
348
349//--------------------------------------------------------------------------------
350 $mform->addElement('hidden', 'id', null);
351 $mform->setType('id', PARAM_INT);
352
353 // fill in default teacher and student names to keep backwards compatibility for a while
354 $mform->addElement('hidden', 'teacher', get_string('defaultcourseteacher'));
355 $mform->addElement('hidden', 'teachers', get_string('defaultcourseteachers'));
356 $mform->addElement('hidden', 'student', get_string('defaultcoursestudent'));
357 $mform->addElement('hidden', 'students', get_string('defaultcoursestudents'));
358
359
360//--------------------------------------------------------------------------------
361 $renderer->addStopFieldsetElements('submit');
362
363
364// now override defaults if course already exists
365 if (!empty($course)) {
366 $this->set_defaults($course);
367 }
5f2256f3 368
01963b95 369 }
a81bf9c3 370
371/// perform some extra moodle validation
372 function validation($data){
01963b95 373 $errors= array();
a81bf9c3 374 if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
375 if (!empty($data['id'])) {
376 unset($foundcourses[$data['id']]);
01963b95 377 }
378 if (!empty($foundcourses)) {
379 foreach ($foundcourses as $foundcourse) {
380 $foundcoursenames[] = $foundcourse->fullname;
381 }
a81bf9c3 382 $foundcoursenamestring = implode(',', $foundcoursenames);
383 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
01963b95 384 }
01963b95 385 }
a81bf9c3 386
387 if (empty($data['enrolenddisabled'])){
388 if ($data['enrolenddate'] <= $data['enrolstartdate']){
389 $errors['enroldateendgrp'] = get_string('enrolenddaterror');
01963b95 390 }
391 }
a81bf9c3 392
393 if (0 == count($errors)){
01963b95 394 return true;
a81bf9c3 395 } else {
01963b95 396 return $errors;
397 }
398 }
a81bf9c3 399}
400?>