Added some styles to make role descriptions a bit smaller MDL-6923
[moodle.git] / course / edit_form.php
blobf1975e71965609be53fb766afc8f37a61409ec59
1 <?php
2 /// For moodleform we reconstruct all the data about the form after submission before we
3 /// extract data submitted. So we can then tell for select, checkbox and radio fields what
4 /// options could have been submitted by the form eg submitted value for a single selection
5 /// select field must be one of the options of the select field.
7 // $toform will be used for the default value of a field if no data has been submitted.
9 if (!empty($course)) {
10 //this is what we do if we are editing a record from the db
11 $toform = $course;
12 $toform->allowedmods = array();
13 if ($am = get_records("course_allowed_modules","course",$course->id)) {
14 foreach ($am as $m) {
15 $toform->allowedmods[] = $m->module;
17 } else {
18 if (empty($course->restrictmodules)) {
19 $toform->allowedmods = explode(',',$CFG->defaultallowedmodules);
20 } // it'll be greyed out but we want these by default anyway.
22 } else {
23 //this is a brand new course!
24 $toform->startdate = time() + 3600 * 24;
25 $toform->fullname = get_string("defaultcoursefullname");
26 $toform->shortname = get_string("defaultcourseshortname");
27 $toform->summary = get_string("defaultcoursesummary");
28 $toform->format = "weeks";
29 $toform->password = "";
30 $toform->guest = 0;
31 $toform->numsections = 10;
32 $toform->idnumber = '';
33 $toform->cost = '';
34 $toform->currency = empty($CFG->enrol_currency) ? 'USD' : $CFG->enrol_currency;
35 $toform->newsitems = 5;
36 $toform->showgrades = 1;
37 $toform->groupmode = 0;
38 $toform->groupmodeforce = 0;
39 $toform->category = $category;
40 $toform->id = "";
41 $toform->visible = 1;
42 $toform->allowedmods = array();
43 if ($CFG->restrictmodulesfor == 'all') {
44 $toform->allowedmods = explode(',',$CFG->defaultallowedmodules);
45 if (!empty($CFG->restrictbydefault)) {
46 $toform->restrictmodules = 1;
53 $mform->addElement('header','general', get_string("general"));
54 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $toform->category))) {
55 $displaylist = array();
56 $parentlist = array();
57 make_categories_list($displaylist, $parentlist);
58 $mform->addElement('select', "category", get_string("category"), $displaylist );
60 $mform->setDefault('fullname', $toform->fullname);
61 $mform->addElement('text','fullname', get_string("fullname"),'maxlength="254" size="50"');
62 $mform->setDefault('shortname', $toform->shortname);
63 $mform->addElement('text','shortname', get_string("shortname"),'maxlength="15" size="10"');
64 $mform->setDefault('idnumber', $toform->idnumber);
65 $mform->addElement('text','idnumber', get_string("idnumbercourse"),'maxlength="100" size="10"');
66 $mform->setDefault('summary', $toform->summary);
67 $mform->addElement('htmleditor','summary', get_string("summary"), array('rows'=>'10', 'cols'=>'65'));
69 $mform->setDefault('format', $toform->format);
70 $courseformats = get_list_of_plugins("course/format");
71 $formcourseformats = array();
73 foreach ($courseformats as $courseformat) {
74 $formcourseformats["$courseformat"] = get_string("format$courseformat");
77 $mform->addElement('select', 'format', get_string("format"), $formcourseformats );
79 $mform->setDefault('numsections', $toform->numsections);
80 for ($i=1; $i<=52; $i++) {
81 $sectionmenu[$i] = "$i";
83 $mform->addElement('select', 'numsections', get_string("numberweeks"), $sectionmenu);
84 $mform->setDefault('startdate', $toform->startdate);
85 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
87 $mform->setDefault('hiddensections',(isset($toform->hiddensections))?$toform->hiddensections:0);
88 unset($choices);
89 $choices["0"] = get_string("hiddensectionscollapsed");
90 $choices["1"] = get_string("hiddensectionsinvisible");
91 $mform->addElement('select', 'hiddensections', get_string("hiddensections"), $choices);
93 $mform->setDefault('newsitems', $toform->newsitems);
94 $newsitem = get_string("newsitem");
95 $newsitems = get_string("newsitems");
96 $options = array("0" => "0 $newsitems",
97 "1" => "1 $newsitem",
98 "2" => "2 $newsitems",
99 "3" => "3 $newsitems",
100 "4" => "4 $newsitems",
101 "5" => "5 $newsitems",
102 "6" => "6 $newsitems",
103 "7" => "7 $newsitems",
104 "8" => "8 $newsitems",
105 "9" => "9 $newsitems",
106 "10" => "10 $newsitems");
107 $mform->addElement('select', 'newsitems', get_string("newsitemsnumber"), $options);
109 $mform->setDefault('showgrades', $toform->showgrades);
110 unset($choices);
111 $choices["0"] = get_string("no");
112 $choices["1"] = get_string("yes");
113 $mform->addElement('select', 'showgrades', get_string("showgrades"), $choices);
114 unset($choices);
115 $mform->setDefault('showreports',(isset($toform->showreports))?$toform->showreports:0);
116 $choices["0"] = get_string("no");
117 $choices["1"] = get_string("yes");
118 $mform->addElement('select', 'showreports', get_string("showreports"), $choices);
119 $mform->setDefault('maxbytes',(isset($toform->maxbytes))?$toform->maxbytes:0);
120 $choices = get_max_upload_sizes($CFG->maxbytes);
121 $mform->addElement('select', 'maxbytes', get_string("maximumupload"), $choices);
122 if (!empty($CFG->allowcoursethemes)) {
123 $mform->setDefault('theme',(isset($toform->theme))?$toform->theme:'');
125 $themes=array();
126 $themes[''] = get_string("forceno");
127 $themes += get_list_of_themes();
128 $mform->addElement('select', 'theme', get_string("forcetheme"), $themes);
130 unset($choices);
131 $mform->setDefault('metacourse',(isset($toform->metacourse))?$toform->metacourse:0);
133 if (empty($disable_meta)) {
134 $meta=array();
135 $meta[0] = get_string('no');
136 $meta[1] = get_string('yes');
137 $mform->addElement('select', 'metacourse', get_string("managemeta"), $meta);
138 }else{
139 $mform->addElement('static', 'metacourse', get_string("managemeta"),
140 ((empty($toform->metacourse)) ? get_string("no") : get_string("yes"))
141 . " - $disable_meta ");
145 $mform->setDefault('defaultrole',(isset($toform->defaultrole))?$toform->defaultrole:0);
146 $roles = get_assignable_roles($context);
147 asort($roles);
148 $choices = array();
150 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
151 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
152 } else {
153 $choices[0] = get_string('sitedefault');
156 $choices = $choices + $roles;
157 $defaultroleelement=$mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices );
158 if ($mform->exportValue('defaultrole') && !isset($roles[$mform->exportValue('defaultrole')])) { // Existing setting is one we can't choose
159 if ($coursedefaultrole = get_record('role', 'id', $mform->exportValue('defaultrole'))) {
161 $defaultroleelement->addOption(get_string('currentrole', 'role').' ('.$coursedefaultrole->name.')',-1);
162 } else {
163 $defaultroleelement->addOption(get_string('currentrole', 'role'),-1);
168 $mform->addElement('header','enrolhdr', get_string("enrolment"));
169 $mform->setDefault('enrol',(isset($toform->enrol))?$toform->enrol:'');
171 $modules = explode(',', $CFG->enrol_plugins_enabled);
172 foreach ($modules as $module) {
173 $name = get_string("enrolname", "enrol_$module");
174 $plugin = enrolment_factory::factory($module);
175 if (method_exists($plugin, 'print_entry')) {
176 $choices[$name] = $module;
179 asort($choices);
180 $choices = array_flip($choices);
181 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string("enrolname", "enrol_$CFG->enrol").')'), $choices);
182 $mform->addElement('select', 'enrol', get_string("enrolmentplugins"), $choices );
184 $mform->setDefault('enrollable',(isset($toform->enrollable))?$toform->enrollable:1);
185 $radio = array();
186 $radio[] = &moodleform::createElement('radio', 'enrollable', null, get_string("no"), 0);
187 $radio[] = &moodleform::createElement('radio', 'enrollable', null, get_string("yes"), 1);
188 $radio[] = &moodleform::createElement('radio', 'enrollable', null, get_string("enroldate"), 2);
189 $mform->addGroup($radio, 'enrollable', get_string("enrollable"), ' ', false);
191 $mform->setDefault('enrolstartdate',(isset($toform->enrolstartdate))?$toform->enrolstartdate:0);
192 $mform->setDefault('enrolstartdisabled',
193 (!isset($toform->enrolstartdate)||
194 (isset($toform->enrolstartdate)&&!$toform->enrolstartdate)
195 ?1:0));
196 $enroldatestartgrp = array();
197 $enroldatestartgrp[] = &moodleform::createElement('date_selector', 'enrolstartdate');
198 $enroldatestartgrp[] = &moodleform::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
199 $mform->addGroup($enroldatestartgrp, '', get_string('enrolstartdate'), ' ', false);
201 $mform->setDefault('enrolenddate',(isset($toform->enrolenddate))?$toform->enrolenddate:0);
202 $mform->setDefault('enrolenddisabled',
203 (!isset($toform->enrolenddate)||
204 (isset($toform->enrolenddate)&&!$toform->enrolenddate)
205 ?1:0));
206 $enroldateendgrp = array();
207 $enroldateendgrp[] = &moodleform::createElement('date_selector', 'enrolenddate');
208 $enroldateendgrp[] = &moodleform::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
209 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
211 $mform->setDefault('enrolperiod',(isset($toform->enrolperiod))?$toform->enrolperiod:0);
212 $periodmenu=array();
213 $periodmenu[0] = get_string('unlimited');
214 for ($i=1; $i<=365; $i++) {
215 $seconds = $i * 86400;
216 $periodmenu[$seconds] = get_string('numdays', '', $i);
218 $mform->addElement('select', 'enrolperiod', get_string("enrolperiod"), $periodmenu);
221 $mform->addElement('header','expirynotifyhdr', get_string("expirynotify"));
223 $mform->setDefault('expirynotify',(isset($toform->expirynotify))?$toform->expirynotify:0);
224 unset($choices);
225 $choices["0"] = get_string("no");
226 $choices["1"] = get_string("yes");
227 $mform->addElement('select', 'expirynotify', get_string("expirynotify"), $choices);
228 $mform->setDefault('notifystudents',(isset($toform->notifystudents))?$toform->notifystudents:0);
229 $mform->addElement('select', 'notifystudents', get_string("expirynotifystudents"), $choices);
230 $mform->setDefault('expirythreshold',(isset($toform->expirythreshold))?$toform->expirythreshold:10 * 86400);
231 $thresholdmenu=array();
232 for ($i=1; $i<=30; $i++) {
233 $seconds = $i * 86400;
234 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
236 $mform->addElement('select', 'expirythreshold', get_string("expirythreshold"), $thresholdmenu);
239 $mform->addElement('header','', get_string("groupmode"));
241 $mform->setDefault('groupmode', $toform->groupmode);
242 unset($choices);
243 $choices[NOGROUPS] = get_string("groupsnone");
244 $choices[SEPARATEGROUPS] = get_string("groupsseparate");
245 $choices[VISIBLEGROUPS] = get_string("groupsvisible");
246 $mform->addElement('select', 'groupmode', get_string("groupmode"), $choices);
247 $mform->setDefault('groupmodeforce', $toform->groupmodeforce);
248 unset($choices);
249 $choices["0"] = get_string("no");
250 $choices["1"] = get_string("yes");
251 $mform->addElement('select', 'groupmodeforce', get_string("force"), $choices);
253 $mform->addElement('header','', get_string("availability"));
255 $mform->setDefault('visible', $toform->visible);
256 unset($choices);
257 $choices["0"] = get_string("courseavailablenot");
258 $choices["1"] = get_string("courseavailable");
259 $mform->addElement('select', 'visible', get_string("availability"), $choices);
260 $mform->setDefault('password', $toform->password);
261 $mform->addElement('text', 'password', get_string("enrolmentkey"), 'size="25"');
262 $mform->setDefault('guest', $toform->guest);
263 unset($choices);
264 $choices["0"] = get_string("guestsno");
265 $choices["1"] = get_string("guestsyes");
266 $choices["2"] = get_string("guestskey");
267 $mform->addElement('select', 'guest', get_string("opentoguests"), $choices);
268 if (isset($course) && method_exists(enrolment_factory::factory($course->enrol), 'print_entry') && $course->enrol != 'manual') {
269 $mform->setDefault('cost', $toform->cost);
270 $mform->setDefault('currency', $toform->currency);
271 $costgroup=array();
272 $currencies = get_list_of_currencies();
273 $costgroup[]= &moodleform::createElement('text','cost', '', 'maxlength="6" size="6"');
274 $costgroup[]= &moodleform::createElement('select', 'currency', '', $currencies);
275 $mform->addGroup($costgroup, 'costgrp', get_string("cost"), '&nbsp;', false);
278 $mform->addElement('header','', get_string("language"));
279 $mform->setDefault('lang',(isset($toform->lang))?$toform->lang:'');
280 $languages=array();
281 $languages[''] = get_string("forceno");
282 $languages += get_list_of_languages();
283 $mform->addElement('select', 'lang', get_string("forcelanguage"), $languages);
285 if(!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $toform->category))) {
286 $mform->addElement('hidden', 'category', null);
288 $mform->addElement('hidden', 'id', null);
290 $mform->setDefault('restrictmodules',(isset($toform->restrictmodules))?$toform->restrictmodules:0);
291 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
292 unset($options);
293 $options[0] = get_string("no");
294 $options[1] = get_string("yes");
295 $mform->addElement('header', '', get_string("restrictmodules"));
296 $mform->addElement('select', 'restrictmodules', get_string("restrictmodules"), $options,
297 array('onChange'=>"document.getElementById('id_allowedmods').disabled=".
298 "((this.selectedIndex==0)?true:false);"));
299 $mods = array(0=>get_string('allownone'));
300 $mods += get_records_menu("modules", '','','','id, name');
301 $disabled=($mform->exportValue('restrictmodules')==1) ? array() :array('disabled' => 'disabled') ;
304 $mform->setDefault('allowedmods', $toform->allowedmods);
305 $mform->addElement('select', 'allowedmods', get_string("to"),$mods,
306 array('multiple'=>"multiple", 'size'=>"10", 'id'=>"allowedmods")
307 +$disabled);
308 }else {
309 $mform->addElement('hidden', 'restrictmodules', $toform->restrictmodules);
311 $mform->setHelpButtons(array('category'=>array("coursecategory", get_string("category")),
312 'fullname'=>array("coursefullname", get_string("fullname")),
313 'shortname'=>array("courseshortname", get_string("shortname")),
314 'idnumber'=>array("courseidnumber", get_string("idnumbercourse")),
315 'summary'=>array("text", get_string("helptext")),
316 'format'=>array("courseformats", get_string("courseformats")),
317 'summary'=>array("coursestartdate", get_string("startdate")),
318 'enrol'=>array("courseenrolmentplugins", get_string("enrolmentplugins")),
319 'enrollable'=>array("courseenrollable", get_string("enrollable")),
320 'expirynotify'=>array("expirynotify", get_string("expirynotify")),
321 'notifystudents'=>array("expirynotifystudents", get_string("expirynotifystudents")),
322 'expirythreshold'=>array("expirythreshold", get_string("expirythreshold")),
323 'groupmode'=>array("groupmode", get_string("groupmode")),
324 'groupmodeforce'=>array("groupmodeforce", get_string("groupmodeforce")),
325 'visible'=>array("courseavailability", get_string("availability")),
326 'password'=>array("enrolmentkey", get_string("enrolmentkey")),
327 'guest'=>array("guestaccess", get_string("opentoguests")),
328 'costgrp'=>array("cost", get_string("cost")),
329 'hiddensections'=>array("coursehiddensections", get_string("hiddensections")),
330 'newsitems'=>array("coursenewsitems", get_string("newsitemsnumber")),
331 'showgrades'=>array("coursegrades", get_string("grades")),
332 'showreports'=>array("coursereports", get_string("activityreport")),
333 'maxbytes'=>array("courseuploadsize", get_string("maximumupload")),
334 'metacourse'=>array("metacourse", get_string("metacourse")),
335 'defaultrole'=>array("coursedefaultrole", get_string("defaultrole", 'role'))),
336 true);
337 $mform->addElement('submit', 'submit', get_string("savechanges"));
339 $mform->addRule('fullname', get_string("missingfullname"), 'required', null, 'client');
340 $mform->addRule('shortname', get_string("missingshortname"), 'required', null, 'client');
341 $mform->addRule('summary', get_string("missingsummary"), 'required', null, 'client');
342 $mform->addRule('category', get_string("missingcategory"), 'required', null, 'client');
345 * a Form rule check - if we need to create rules which involve the contents of more than one
346 * field this is the way to do it. We can do it all at once in one function or create several
347 * form rules.
349 * @param array $fields values from form without slashes
350 * @return mixed array of errors or true if passed check
352 function form_check($fields){
353 global $course;
354 $errors= array();
355 if ($foundcourses = get_records("course", "shortname", addslashes($fields['shortname']))) {
356 if (!empty($course->id)) {
357 unset($foundcourses[$course->id]);
359 if (!empty($foundcourses)) {
360 foreach ($foundcourses as $foundcourse) {
361 $foundcoursenames[] = $foundcourse->fullname;
363 $foundcoursenamestring = addslashes(implode(',', $foundcoursenames));
365 $errors['shortname']= get_string("shortnametaken", "", $foundcoursenamestring);
371 if (empty($fields['enrolenddisabled'])){
372 $enrolenddate=make_timestamp($fields['enrolenddate']['year'],
373 $fields['enrolenddate']['month'],
374 $fields['enrolenddate']['day']);
375 $enrolstartdate=make_timestamp($fields['enrolstartdate']['year'],
376 $fields['enrolstartdate']['month'],
377 $fields['enrolstartdate']['day']);
378 if ($enrolenddate <= $enrolstartdate){
379 $errors['enroldateendgrp']=get_string("enrolenddaterror");
382 if (0==count($errors)){
383 return true;
384 }else {
385 return $errors;
388 $mform->addFormRule('form_check');
390 $renderer =& $mform->defaultRenderer();
391 $renderer->addStopFieldsetElements('submit');