Automatic installer.php lang files by installer_builder (20061013)
[moodle.git] / course / edit_form.php
CommitLineData
01963b95 1<?php
461f5127 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.
6
d9dce4e7 7// $toform will be used for the default value of a field if no data has been submitted.
461f5127 8
9 if (!empty($course)) {
10 //this is what we do if we are editing a record from the db
d9dce4e7 11 $toform = $course;
12 $toform->allowedmods = array();
461f5127 13 if ($am = get_records("course_allowed_modules","course",$course->id)) {
14 foreach ($am as $m) {
d9dce4e7 15 $toform->allowedmods[] = $m->module;
461f5127 16 }
17 } else {
18 if (empty($course->restrictmodules)) {
d9dce4e7 19 $toform->allowedmods = explode(',',$CFG->defaultallowedmodules);
461f5127 20 } // it'll be greyed out but we want these by default anyway.
21 }
22 } else {
23 //this is a brand new course!
d9dce4e7 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();
461f5127 43 if ($CFG->restrictmodulesfor == 'all') {
d9dce4e7 44 $toform->allowedmods = explode(',',$CFG->defaultallowedmodules);
461f5127 45 if (!empty($CFG->restrictbydefault)) {
d9dce4e7 46 $toform->restrictmodules = 1;
461f5127 47 }
48 }
49
50
51 }
01963b95 52
01963b95 53 $mform->addElement('header','general', get_string("general"));
d9dce4e7 54 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $toform->category))) {
01963b95 55 $displaylist = array();
56 $parentlist = array();
57 make_categories_list($displaylist, $parentlist);
58 $mform->addElement('select', "category", get_string("category"), $displaylist );
59 }
d9dce4e7 60 $mform->setDefault('fullname', $toform->fullname);
01963b95 61 $mform->addElement('text','fullname', get_string("fullname"),'maxlength="254" size="50"');
d9dce4e7 62 $mform->setDefault('shortname', $toform->shortname);
01963b95 63 $mform->addElement('text','shortname', get_string("shortname"),'maxlength="15" size="10"');
d9dce4e7 64 $mform->setDefault('idnumber', $toform->idnumber);
01963b95 65 $mform->addElement('text','idnumber', get_string("idnumbercourse"),'maxlength="100" size="10"');
d9dce4e7 66 $mform->setDefault('summary', $toform->summary);
5ae70385 67 $mform->addElement('htmleditor','summary', get_string("summary"), array('rows'=>'10', 'cols'=>'65'));
01963b95 68
d9dce4e7 69 $mform->setDefault('format', $toform->format);
01963b95 70 $courseformats = get_list_of_plugins("course/format");
71 $formcourseformats = array();
72
73 foreach ($courseformats as $courseformat) {
74 $formcourseformats["$courseformat"] = get_string("format$courseformat");
75 }
76
77 $mform->addElement('select', 'format', get_string("format"), $formcourseformats );
78
d9dce4e7 79 $mform->setDefault('numsections', $toform->numsections);
01963b95 80 for ($i=1; $i<=52; $i++) {
81 $sectionmenu[$i] = "$i";
82 }
83 $mform->addElement('select', 'numsections', get_string("numberweeks"), $sectionmenu);
d9dce4e7 84 $mform->setDefault('startdate', $toform->startdate);
01963b95 85 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
86
d9dce4e7 87 $mform->setDefault('hiddensections',(isset($toform->hiddensections))?$toform->hiddensections:0);
01963b95 88 unset($choices);
89 $choices["0"] = get_string("hiddensectionscollapsed");
90 $choices["1"] = get_string("hiddensectionsinvisible");
91 $mform->addElement('select', 'hiddensections', get_string("hiddensections"), $choices);
92
d9dce4e7 93 $mform->setDefault('newsitems', $toform->newsitems);
01963b95 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);
108
d9dce4e7 109 $mform->setDefault('showgrades', $toform->showgrades);
01963b95 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);
d9dce4e7 115 $mform->setDefault('showreports',(isset($toform->showreports))?$toform->showreports:0);
01963b95 116 $choices["0"] = get_string("no");
117 $choices["1"] = get_string("yes");
118 $mform->addElement('select', 'showreports', get_string("showreports"), $choices);
d9dce4e7 119 $mform->setDefault('maxbytes',(isset($toform->maxbytes))?$toform->maxbytes:0);
01963b95 120 $choices = get_max_upload_sizes($CFG->maxbytes);
121 $mform->addElement('select', 'maxbytes', get_string("maximumupload"), $choices);
122 if (!empty($CFG->allowcoursethemes)) {
d9dce4e7 123 $mform->setDefault('theme',(isset($toform->theme))?$toform->theme:'');
461f5127 124
01963b95 125 $themes=array();
126 $themes[''] = get_string("forceno");
127 $themes += get_list_of_themes();
128 $mform->addElement('select', 'theme', get_string("forcetheme"), $themes);
129 }
130 unset($choices);
d9dce4e7 131 $mform->setDefault('metacourse',(isset($toform->metacourse))?$toform->metacourse:0);
461f5127 132
01963b95 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"),
d9dce4e7 140 ((empty($toform->metacourse)) ? get_string("no") : get_string("yes"))
01963b95 141 . " - $disable_meta ");
142
143 }
144
d9dce4e7 145 $mform->setDefault('defaultrole',(isset($toform->defaultrole))?$toform->defaultrole:0);
01963b95 146 $roles = get_assignable_roles($context);
147 asort($roles);
148 $choices = array();
149
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');
154 }
155
156 $choices = $choices + $roles;
461f5127 157 $defaultroleelement=$mform->addElement('select', 'defaultrole', get_string('defaultrole', 'role'), $choices );
01963b95 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'))) {
160
161 $defaultroleelement->addOption(get_string('currentrole', 'role').' ('.$coursedefaultrole->name.')',-1);
162 } else {
163 $defaultroleelement->addOption(get_string('currentrole', 'role'),-1);
164 }
165 }
166
167
168 $mform->addElement('header','enrolhdr', get_string("enrolment"));
d9dce4e7 169 $mform->setDefault('enrol',(isset($toform->enrol))?$toform->enrol:'');
461f5127 170
01963b95 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;
177 }
178 }
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 );
183
d9dce4e7 184 $mform->setDefault('enrollable',(isset($toform->enrollable))?$toform->enrollable:1);
01963b95 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);
190
d9dce4e7 191 $mform->setDefault('enrolstartdate',(isset($toform->enrolstartdate))?$toform->enrolstartdate:0);
461f5127 192 $mform->setDefault('enrolstartdisabled',
d9dce4e7 193 (!isset($toform->enrolstartdate)||
194 (isset($toform->enrolstartdate)&&!$toform->enrolstartdate)
461f5127 195 ?1:0));
01963b95 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);
200
d9dce4e7 201 $mform->setDefault('enrolenddate',(isset($toform->enrolenddate))?$toform->enrolenddate:0);
461f5127 202 $mform->setDefault('enrolenddisabled',
d9dce4e7 203 (!isset($toform->enrolenddate)||
204 (isset($toform->enrolenddate)&&!$toform->enrolenddate)
461f5127 205 ?1:0));
01963b95 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);
210
d9dce4e7 211 $mform->setDefault('enrolperiod',(isset($toform->enrolperiod))?$toform->enrolperiod:0);
01963b95 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);
217 }
218 $mform->addElement('select', 'enrolperiod', get_string("enrolperiod"), $periodmenu);
219
220
221 $mform->addElement('header','expirynotifyhdr', get_string("expirynotify"));
222
d9dce4e7 223 $mform->setDefault('expirynotify',(isset($toform->expirynotify))?$toform->expirynotify:0);
01963b95 224 unset($choices);
225 $choices["0"] = get_string("no");
226 $choices["1"] = get_string("yes");
227 $mform->addElement('select', 'expirynotify', get_string("expirynotify"), $choices);
d9dce4e7 228 $mform->setDefault('notifystudents',(isset($toform->notifystudents))?$toform->notifystudents:0);
01963b95 229 $mform->addElement('select', 'notifystudents', get_string("expirynotifystudents"), $choices);
d9dce4e7 230 $mform->setDefault('expirythreshold',(isset($toform->expirythreshold))?$toform->expirythreshold:10 * 86400);
01963b95 231 $thresholdmenu=array();
232 for ($i=1; $i<=30; $i++) {
233 $seconds = $i * 86400;
234 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
235 }
236 $mform->addElement('select', 'expirythreshold', get_string("expirythreshold"), $thresholdmenu);
237
238
239 $mform->addElement('header','', get_string("groupmode"));
461f5127 240
d9dce4e7 241 $mform->setDefault('groupmode', $toform->groupmode);
01963b95 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);
d9dce4e7 247 $mform->setDefault('groupmodeforce', $toform->groupmodeforce);
01963b95 248 unset($choices);
249 $choices["0"] = get_string("no");
250 $choices["1"] = get_string("yes");
251 $mform->addElement('select', 'groupmodeforce', get_string("force"), $choices);
461f5127 252
01963b95 253 $mform->addElement('header','', get_string("availability"));
461f5127 254
d9dce4e7 255 $mform->setDefault('visible', $toform->visible);
01963b95 256 unset($choices);
257 $choices["0"] = get_string("courseavailablenot");
258 $choices["1"] = get_string("courseavailable");
259 $mform->addElement('select', 'visible', get_string("availability"), $choices);
d9dce4e7 260 $mform->setDefault('password', $toform->password);
01963b95 261 $mform->addElement('text', 'password', get_string("enrolmentkey"), 'size="25"');
d9dce4e7 262 $mform->setDefault('guest', $toform->guest);
01963b95 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') {
d9dce4e7 269 $mform->setDefault('cost', $toform->cost);
270 $mform->setDefault('currency', $toform->currency);
01963b95 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);
276 }
277
278 $mform->addElement('header','', get_string("language"));
d9dce4e7 279 $mform->setDefault('lang',(isset($toform->lang))?$toform->lang:'');
01963b95 280 $languages=array();
281 $languages[''] = get_string("forceno");
282 $languages += get_list_of_languages();
283 $mform->addElement('select', 'lang', get_string("forcelanguage"), $languages);
5f2256f3 284
d9dce4e7 285 if(!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $toform->category))) {
5f2256f3 286 $mform->addElement('hidden', 'category', null);
01963b95 287 }
5f2256f3 288 $mform->addElement('hidden', 'id', null);
01963b95 289
d9dce4e7 290 $mform->setDefault('restrictmodules',(isset($toform->restrictmodules))?$toform->restrictmodules:0);
01963b95 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,
49c53687 297 array('onChange'=>"document.getElementById('id_allowedmods').disabled=".
01963b95 298 "((this.selectedIndex==0)?true:false);"));
299 $mods = array(0=>get_string('allownone'));
300 $mods += get_records_menu("modules", '','','','id, name');
0d26ea7a 301 $disabled=($mform->exportValue('restrictmodules')==1) ? array() :array('disabled' => 'disabled') ;
302
303
d9dce4e7 304 $mform->setDefault('allowedmods', $toform->allowedmods);
01963b95 305 $mform->addElement('select', 'allowedmods', get_string("to"),$mods,
230a910a 306 array('multiple'=>"multiple", 'size'=>"10", 'id'=>"allowedmods")
307 +$disabled);
01963b95 308 }else {
d9dce4e7 309 $mform->addElement('hidden', 'restrictmodules', $toform->restrictmodules);
01963b95 310 }
5f2256f3 311 $mform->setHelpButtons(array('category'=>array("coursecategory", get_string("category")),
01963b95 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")),
461f5127 335 'defaultrole'=>array("coursedefaultrole", get_string("defaultrole", 'role'))),
01963b95 336 true);
337 $mform->addElement('submit', 'submit', get_string("savechanges"));
338
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');
343
344 /**
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.
348 *
349 * @param array $fields values from form without slashes
350 * @return mixed array of errors or true if passed check
351 */
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]);
358 }
359 if (!empty($foundcourses)) {
360 foreach ($foundcourses as $foundcourse) {
361 $foundcoursenames[] = $foundcourse->fullname;
362 }
363 $foundcoursenamestring = addslashes(implode(',', $foundcoursenames));
364
365 $errors['shortname']= get_string("shortnametaken", "", $foundcoursenamestring);
366
367 }
368
369
370 }
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");
380 }
381 }
382 if (0==count($errors)){
383 return true;
384 }else {
385 return $errors;
386 }
387 }
388 $mform->addFormRule('form_check');
389
2462a50a 390 $renderer =& $mform->defaultRenderer();
01963b95 391 $renderer->addStopFieldsetElements('submit');
01963b95 392?>