This is a demo of the experimental formslib. It is used here to print and process...
[moodle.git] / course / edit_form.php
CommitLineData
01963b95 1<?php
2 include_once $CFG->libdir.'/formslib.php';
3
4 $mform = new moodleform('edit_course', 'post', 'edit2.php');
5 $mform->addElement('header','general', get_string("general"));
6 if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $default->category))) {
7 $displaylist = array();
8 $parentlist = array();
9 make_categories_list($displaylist, $parentlist);
10 $mform->addElement('select', "category", get_string("category"), $displaylist );
11 }
12
13 $mform->addElement('text','fullname', get_string("fullname"),'maxlength="254" size="50"');
14 $mform->addElement('text','shortname', get_string("shortname"),'maxlength="15" size="10"');
15 $mform->addElement('text','idnumber', get_string("idnumbercourse"),'maxlength="100" size="10"');
16 $mform->addElement('htmleditor','summary', get_string("summary"), 'width="500" height="200" rows="10", cols="40"');
17
18 $courseformats = get_list_of_plugins("course/format");
19 $formcourseformats = array();
20
21 foreach ($courseformats as $courseformat) {
22 $formcourseformats["$courseformat"] = get_string("format$courseformat");
23 }
24
25 $mform->addElement('select', 'format', get_string("format"), $formcourseformats );
26
27 for ($i=1; $i<=52; $i++) {
28 $sectionmenu[$i] = "$i";
29 }
30 $mform->addElement('select', 'numsections', get_string("numberweeks"), $sectionmenu);
31 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
32
33 unset($choices);
34 $choices["0"] = get_string("hiddensectionscollapsed");
35 $choices["1"] = get_string("hiddensectionsinvisible");
36 $mform->addElement('select', 'hiddensections', get_string("hiddensections"), $choices);
37
38 $newsitem = get_string("newsitem");
39 $newsitems = get_string("newsitems");
40 $options = array("0" => "0 $newsitems",
41 "1" => "1 $newsitem",
42 "2" => "2 $newsitems",
43 "3" => "3 $newsitems",
44 "4" => "4 $newsitems",
45 "5" => "5 $newsitems",
46 "6" => "6 $newsitems",
47 "7" => "7 $newsitems",
48 "8" => "8 $newsitems",
49 "9" => "9 $newsitems",
50 "10" => "10 $newsitems");
51 $mform->addElement('select', 'newsitems', get_string("newsitemsnumber"), $options);
52
53 unset($choices);
54 $choices["0"] = get_string("no");
55 $choices["1"] = get_string("yes");
56 $mform->addElement('select', 'showgrades', get_string("showgrades"), $choices);
57 unset($choices);
58 $choices["0"] = get_string("no");
59 $choices["1"] = get_string("yes");
60 $mform->addElement('select', 'showreports', get_string("showreports"), $choices);
61 $choices = get_max_upload_sizes($CFG->maxbytes);
62 $mform->addElement('select', 'maxbytes', get_string("maximumupload"), $choices);
63 if (!empty($CFG->allowcoursethemes)) {
64 $themes=array();
65 $themes[''] = get_string("forceno");
66 $themes += get_list_of_themes();
67 $mform->addElement('select', 'theme', get_string("forcetheme"), $themes);
68 }
69 unset($choices);
70 if (empty($disable_meta)) {
71 $meta=array();
72 $meta[0] = get_string('no');
73 $meta[1] = get_string('yes');
74 $mform->addElement('select', 'metacourse', get_string("managemeta"), $meta);
75 }else{
76 $mform->addElement('static', 'metacourse', get_string("managemeta"),
77 ((empty($default->metacourse)) ? get_string("no") : get_string("yes"))
78 . " - $disable_meta ");
79
80 }
81
82 $roles = get_assignable_roles($context);
83 asort($roles);
84 $choices = array();
85
86 if ($sitedefaultrole = get_record('role', 'id', $CFG->defaultcourseroleid)) {
87 $choices[0] = get_string('sitedefault').' ('.$sitedefaultrole->name.')';
88 } else {
89 $choices[0] = get_string('sitedefault');
90 }
91
92 $choices = $choices + $roles;
93 $defaultroleelement=$mform->addElement('select', 'defaultrole', get_string('defaultrole'), $choices );
94 if ($mform->exportValue('defaultrole') && !isset($roles[$mform->exportValue('defaultrole')])) { // Existing setting is one we can't choose
95 if ($coursedefaultrole = get_record('role', 'id', $mform->exportValue('defaultrole'))) {
96
97 $defaultroleelement->addOption(get_string('currentrole', 'role').' ('.$coursedefaultrole->name.')',-1);
98 } else {
99 $defaultroleelement->addOption(get_string('currentrole', 'role'),-1);
100 }
101 }
102
103
104 $mform->addElement('header','enrolhdr', get_string("enrolment"));
105
106 $modules = explode(',', $CFG->enrol_plugins_enabled);
107 foreach ($modules as $module) {
108 $name = get_string("enrolname", "enrol_$module");
109 $plugin = enrolment_factory::factory($module);
110 if (method_exists($plugin, 'print_entry')) {
111 $choices[$name] = $module;
112 }
113 }
114 asort($choices);
115 $choices = array_flip($choices);
116 $choices = array_merge(array('' => get_string('sitedefault').' ('.get_string("enrolname", "enrol_$CFG->enrol").')'), $choices);
117 $mform->addElement('select', 'enrol', get_string("enrolmentplugins"), $choices );
118
119 $radio = array();
120 $radio[] = &moodleform::createElement('radio', 'enrollable', null, get_string("no"), 0);
121 $radio[] = &moodleform::createElement('radio', 'enrollable', null, get_string("yes"), 1);
122 $radio[] = &moodleform::createElement('radio', 'enrollable', null, get_string("enroldate"), 2);
123 $mform->addGroup($radio, 'enrollable', get_string("enrollable"), ' ', false);
124
125 $enroldatestartgrp = array();
126 $enroldatestartgrp[] = &moodleform::createElement('date_selector', 'enrolstartdate');
127 $enroldatestartgrp[] = &moodleform::createElement('checkbox', 'enrolstartdisabled', null, get_string('disable'));
128 $mform->addGroup($enroldatestartgrp, '', get_string('enrolstartdate'), ' ', false);
129
130 $enroldateendgrp = array();
131 $enroldateendgrp[] = &moodleform::createElement('date_selector', 'enrolenddate');
132 $enroldateendgrp[] = &moodleform::createElement('checkbox', 'enrolenddisabled', null, get_string('disable'));
133 $mform->addGroup($enroldateendgrp, 'enroldateendgrp', get_string('enrolenddate'), ' ', false);
134
135 $periodmenu=array();
136 $periodmenu[0] = get_string('unlimited');
137 for ($i=1; $i<=365; $i++) {
138 $seconds = $i * 86400;
139 $periodmenu[$seconds] = get_string('numdays', '', $i);
140 }
141 $mform->addElement('select', 'enrolperiod', get_string("enrolperiod"), $periodmenu);
142
143
144 $mform->addElement('header','expirynotifyhdr', get_string("expirynotify"));
145
146 unset($choices);
147 $choices["0"] = get_string("no");
148 $choices["1"] = get_string("yes");
149 $mform->addElement('select', 'expirynotify', get_string("expirynotify"), $choices);
150 $mform->addElement('select', 'notifystudents', get_string("expirynotifystudents"), $choices);
151 $thresholdmenu=array();
152 for ($i=1; $i<=30; $i++) {
153 $seconds = $i * 86400;
154 $thresholdmenu[$seconds] = get_string('numdays', '', $i);
155 }
156 $mform->addElement('select', 'expirythreshold', get_string("expirythreshold"), $thresholdmenu);
157
158
159 $mform->addElement('header','', get_string("groupmode"));
160 unset($choices);
161 $choices[NOGROUPS] = get_string("groupsnone");
162 $choices[SEPARATEGROUPS] = get_string("groupsseparate");
163 $choices[VISIBLEGROUPS] = get_string("groupsvisible");
164 $mform->addElement('select', 'groupmode', get_string("groupmode"), $choices);
165 unset($choices);
166 $choices["0"] = get_string("no");
167 $choices["1"] = get_string("yes");
168 $mform->addElement('select', 'groupmodeforce', get_string("force"), $choices);
169 $mform->addElement('header','', get_string("availability"));
170 unset($choices);
171 $choices["0"] = get_string("courseavailablenot");
172 $choices["1"] = get_string("courseavailable");
173 $mform->addElement('select', 'visible', get_string("availability"), $choices);
174 $mform->addElement('text', 'password', get_string("enrolmentkey"), 'size="25"');
175 unset($choices);
176 $choices["0"] = get_string("guestsno");
177 $choices["1"] = get_string("guestsyes");
178 $choices["2"] = get_string("guestskey");
179 $mform->addElement('select', 'guest', get_string("opentoguests"), $choices);
180 if (isset($course) && method_exists(enrolment_factory::factory($course->enrol), 'print_entry') && $course->enrol != 'manual') {
181 $costgroup=array();
182 $currencies = get_list_of_currencies();
183 $costgroup[]= &moodleform::createElement('text','cost', '', 'maxlength="6" size="6"');
184 $costgroup[]= &moodleform::createElement('select', 'currency', '', $currencies);
185 $mform->addGroup($costgroup, 'costgrp', get_string("cost"), '&nbsp;', false);
186 }
187
188 $mform->addElement('header','', get_string("language"));
189 $languages=array();
190 $languages[''] = get_string("forceno");
191 $languages += get_list_of_languages();
192 $mform->addElement('select', 'lang', get_string("forcelanguage"), $languages);
193 if(!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $default->category))) {
194 $mform->addElement('hidden', 'category', $default->category);
195 }
196 $mform->addElement('hidden', 'id', $default->id);
197
198
199 if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID)) && ((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
200 unset($options);
201 $options[0] = get_string("no");
202 $options[1] = get_string("yes");
203 $mform->addElement('header', '', get_string("restrictmodules"));
204 $mform->addElement('select', 'restrictmodules', get_string("restrictmodules"), $options,
205 array('onChange'=>"document.getElementById('allowedmods').disabled=".
206 "((this.selectedIndex==0)?true:false);"));
207 $mods = array(0=>get_string('allownone'));
208 $mods += get_records_menu("modules", '','','','id, name');
209 $disabled=($mform->exportValue('restrictmodules')==0) ? " =\"disabled\"" : "";
210 $mform->addElement('select', 'allowedmods', get_string("to"),$mods,
211 array('multiple'=>"multiple", 'size'=>"10", 'id'=>"allowedmods",
212 'disabled' => $disabled));
213 }else {
214 $mform->addElement('hidden', 'restrictmodules', $default->restrictmodules);
215 }
216 $mform->add_help_buttons(array('category'=>array("coursecategory", get_string("category")),
217 'fullname'=>array("coursefullname", get_string("fullname")),
218 'shortname'=>array("courseshortname", get_string("shortname")),
219 'idnumber'=>array("courseidnumber", get_string("idnumbercourse")),
220 'summary'=>array("text", get_string("helptext")),
221 'format'=>array("courseformats", get_string("courseformats")),
222 'summary'=>array("coursestartdate", get_string("startdate")),
223 'enrol'=>array("courseenrolmentplugins", get_string("enrolmentplugins")),
224 'enrollable'=>array("courseenrollable", get_string("enrollable")),
225 'expirynotify'=>array("expirynotify", get_string("expirynotify")),
226 'notifystudents'=>array("expirynotifystudents", get_string("expirynotifystudents")),
227 'expirythreshold'=>array("expirythreshold", get_string("expirythreshold")),
228 'groupmode'=>array("groupmode", get_string("groupmode")),
229 'groupmodeforce'=>array("groupmodeforce", get_string("groupmodeforce")),
230 'visible'=>array("courseavailability", get_string("availability")),
231 'password'=>array("enrolmentkey", get_string("enrolmentkey")),
232 'guest'=>array("guestaccess", get_string("opentoguests")),
233 'costgrp'=>array("cost", get_string("cost")),
234 'hiddensections'=>array("coursehiddensections", get_string("hiddensections")),
235 'newsitems'=>array("coursenewsitems", get_string("newsitemsnumber")),
236 'showgrades'=>array("coursegrades", get_string("grades")),
237 'showreports'=>array("coursereports", get_string("activityreport")),
238 'maxbytes'=>array("courseuploadsize", get_string("maximumupload")),
239 'metacourse'=>array("metacourse", get_string("metacourse")),
240 'defaultrole'=>array("coursedefaultrole", get_string("defaultrole"))),
241 true);
242 $mform->addElement('submit', 'submit', get_string("savechanges"));
243
244 $mform->addRule('fullname', get_string("missingfullname"), 'required', null, 'client');
245 $mform->addRule('shortname', get_string("missingshortname"), 'required', null, 'client');
246 $mform->addRule('summary', get_string("missingsummary"), 'required', null, 'client');
247 $mform->addRule('category', get_string("missingcategory"), 'required', null, 'client');
248
249 /**
250 * a Form rule check - if we need to create rules which involve the contents of more than one
251 * field this is the way to do it. We can do it all at once in one function or create several
252 * form rules.
253 *
254 * @param array $fields values from form without slashes
255 * @return mixed array of errors or true if passed check
256 */
257 function form_check($fields){
258 global $course;
259 $errors= array();
260 if ($foundcourses = get_records("course", "shortname", addslashes($fields['shortname']))) {
261 if (!empty($course->id)) {
262 unset($foundcourses[$course->id]);
263 }
264 if (!empty($foundcourses)) {
265 foreach ($foundcourses as $foundcourse) {
266 $foundcoursenames[] = $foundcourse->fullname;
267 }
268 $foundcoursenamestring = addslashes(implode(',', $foundcoursenames));
269
270 $errors['shortname']= get_string("shortnametaken", "", $foundcoursenamestring);
271
272 }
273
274
275 }
276 if (empty($fields['enrolenddisabled'])){
277 $enrolenddate=make_timestamp($fields['enrolenddate']['year'],
278 $fields['enrolenddate']['month'],
279 $fields['enrolenddate']['day']);
280 $enrolstartdate=make_timestamp($fields['enrolstartdate']['year'],
281 $fields['enrolstartdate']['month'],
282 $fields['enrolstartdate']['day']);
283 if ($enrolenddate <= $enrolstartdate){
284 $errors['enroldateendgrp']=get_string("enrolenddaterror");
285 }
286 }
287 if (0==count($errors)){
288 return true;
289 }else {
290 return $errors;
291 }
292 }
293 $mform->addFormRule('form_check');
294
295 $renderer=$mform->defaultRenderer();
296 $renderer->addStopFieldsetElements('submit');
297
298?>