MDL-20808 "Create AMF test client" Some updates to client
[moodle.git] / course / edit_form.php
blobf69f1b2031467802817a4af083eea95b9a67c006
1 <?php
3 defined('MOODLE_INTERNAL') || die;
5 require_once($CFG->libdir.'/formslib.php');
7 class course_edit_form extends moodleform {
8 protected $course;
9 protected $context;
11 function definition() {
12 global $USER, $CFG, $DB;
14 $mform = $this->_form;
16 $course = $this->_customdata['course']; // this contains the data of this form
17 $category = $this->_customdata['category'];
18 $editoroptions = $this->_customdata['editoroptions'];
20 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
21 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id);
23 if (!empty($course->id)) {
24 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
25 $context = $coursecontext;
26 } else {
27 $coursecontext = null;
28 $context = $categorycontext;
31 $courseconfig = get_config('moodlecourse');
33 $this->course = $course;
34 $this->context = $context;
36 /// form definition with new course defaults
37 //--------------------------------------------------------------------------------
38 $mform->addElement('header','general', get_string('general', 'form'));
40 // verify permissions to change course category or keep current
41 if (empty($course->id)) {
42 if (has_capability('moodle/course:create', $categorycontext)) {
43 $displaylist = array();
44 $parentlist = array();
45 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
46 $mform->addElement('select', 'category', get_string('category'), $displaylist);
47 $mform->addHelpButton('category', 'category');
48 $mform->setDefault('category', $category->id);
49 } else {
50 $mform->addElement('hidden', 'category', null);
51 $mform->setType('category', PARAM_INT);
52 $mform->setConstant('category', $category->id);
54 } else {
55 if (has_capability('moodle/course:changecategory', $coursecontext)) {
56 $displaylist = array();
57 $parentlist = array();
58 make_categories_list($displaylist, $parentlist, 'moodle/course:create');
59 if (!isset($displaylist[$course->category])) {
60 //always keep current
61 $displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
63 $mform->addElement('select', 'category', get_string('category'), $displaylist);
64 $mform->addHelpButton('category', 'category');
65 } else {
66 //keep current
67 $mform->addElement('hidden', 'category', null);
68 $mform->setType('category', PARAM_INT);
69 $mform->setConstant('category', $course->category);
73 $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
74 $mform->addHelpButton('fullname', 'fullnamecourse');
75 $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
76 $mform->setType('fullname', PARAM_MULTILANG);
77 if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
78 $mform->hardFreeze('fullname');
79 $mform->setConstant('fullname', $course->fullname);
82 $mform->addElement('text','shortname', get_string('shortnamecourse'),'maxlength="100" size="20"');
83 $mform->addHelpButton('shortname', 'shortnamecourse');
84 $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
85 $mform->setType('shortname', PARAM_MULTILANG);
86 if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
87 $mform->hardFreeze('shortname');
88 $mform->setConstant('shortname', $course->shortname);
91 $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100" size="10"');
92 $mform->addHelpButton('idnumber', 'idnumbercourse');
93 $mform->setType('idnumber', PARAM_RAW);
94 if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
95 $mform->hardFreeze('idnumber');
96 $mform->setConstants('idnumber', $course->idnumber);
100 $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
101 $mform->addHelpButton('summary_editor', 'coursesummary');
102 $mform->setType('summary_editor', PARAM_RAW);
104 if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
105 $mform->hardFreeze('summary_editor');
108 $courseformats = get_plugin_list('format');
109 $formcourseformats = array();
110 foreach ($courseformats as $courseformat => $formatdir) {
111 $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
113 $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
114 $mform->addHelpButton('format', 'format');
115 $mform->setDefault('format', $courseconfig->format);
117 for ($i=1; $i<=52; $i++) {
118 $sectionmenu[$i] = "$i";
120 $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
121 $mform->setDefault('numsections', $courseconfig->numsections);
123 $mform->addElement('date_selector', 'startdate', get_string('startdate'));
124 $mform->addHelpButton('startdate', 'startdate');
125 $mform->setDefault('startdate', time() + 3600 * 24);
127 $choices = array();
128 $choices['0'] = get_string('hiddensectionscollapsed');
129 $choices['1'] = get_string('hiddensectionsinvisible');
130 $mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
131 $mform->addHelpButton('hiddensections', 'hiddensections');
132 $mform->setDefault('hiddensections', $courseconfig->hiddensections);
134 $options = range(0, 10);
135 $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
136 $mform->addHelpButton('newsitems', 'newsitemsnumber');
137 $mform->setDefault('newsitems', $courseconfig->newsitems);
139 $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
140 $mform->addHelpButton('showgrades', 'showgrades');
141 $mform->setDefault('showgrades', $courseconfig->showgrades);
143 $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
144 $mform->addHelpButton('showreports', 'showreports');
145 $mform->setDefault('showreports', $courseconfig->showreports);
147 $choices = get_max_upload_sizes($CFG->maxbytes);
148 $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
149 $mform->addHelpButton('maxbytes', 'maximumupload');
150 $mform->setDefault('maxbytes', $courseconfig->maxbytes);
152 if (!empty($course->legacyfiles)) {
153 $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
154 $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
155 $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
158 if (!empty($CFG->allowcoursethemes)) {
159 $themeobjects = get_list_of_themes();
160 $themes=array();
161 $themes[''] = get_string('forceno');
162 foreach ($themeobjects as $key=>$theme) {
163 $themes[$key] = $theme->name;
165 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
168 //--------------------------------------------------------------------------------
169 enrol_course_edit_form($mform, $course, $context);
171 //--------------------------------------------------------------------------------
172 $mform->addElement('header','', get_string('groups', 'group'));
174 $choices = array();
175 $choices[NOGROUPS] = get_string('groupsnone', 'group');
176 $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
177 $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
178 $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
179 $mform->addHelpButton('groupmode', 'groupmode', 'group');
180 $mform->setDefault('groupmode', $courseconfig->groupmode);
182 $choices = array();
183 $choices['0'] = get_string('no');
184 $choices['1'] = get_string('yes');
185 $mform->addElement('select', 'groupmodeforce', get_string('groupmodeforce', 'group'), $choices);
186 $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
187 $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
189 //default groupings selector
190 $options = array();
191 $options[0] = get_string('none');
192 $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
194 //--------------------------------------------------------------------------------
195 $mform->addElement('header','', get_string('availability'));
197 $choices = array();
198 $choices['0'] = get_string('courseavailablenot');
199 $choices['1'] = get_string('courseavailable');
200 $mform->addElement('select', 'visible', get_string('availability'), $choices);
201 $mform->addHelpButton('visible', 'availability');
202 $mform->setDefault('visible', $courseconfig->visible);
203 if (!empty($course->id) and !has_capability('moodle/course:visibility', $coursecontext)) {
204 $mform->hardFreeze('visible');
205 $mform->setConstant('visible', $course->visible);
208 //--------------------------------------------------------------------------------
209 $mform->addElement('header','', get_string('language'));
211 $languages=array();
212 $languages[''] = get_string('forceno');
213 $languages += get_string_manager()->get_list_of_translations();
214 $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
215 $mform->setDefault('lang', $courseconfig->lang);
217 //--------------------------------------------------------------------------------
218 require_once($CFG->libdir.'/completionlib.php');
219 if(completion_info::is_enabled_for_site()) {
220 $mform->addElement('header','', get_string('progress','completion'));
221 $mform->addElement('select', 'enablecompletion', get_string('completion','completion'),
222 array(0=>get_string('completiondisabled','completion'), 1=>get_string('completionenabled','completion')));
223 $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
225 $mform->addElement('checkbox', 'completionstartonenrol', get_string('completionstartonenrol', 'completion'));
226 $mform->setDefault('completionstartonenrol', $courseconfig->completionstartonenrol);
227 $mform->disabledIf('completionstartonenrol', 'enablecompletion', 'eq', 0);
228 } else {
229 $mform->addElement('hidden', 'enablecompletion');
230 $mform->setType('enablecompletion', PARAM_INT);
231 $mform->setDefault('enablecompletion',0);
233 $mform->addElement('hidden', 'completionstartonenrol');
234 $mform->setType('completionstartonenrol', PARAM_INT);
235 $mform->setDefault('completionstartonenrol',0);
238 //--------------------------------------------------------------------------------
239 if (has_capability('moodle/site:config', $systemcontext)) {
240 if (((!empty($course->requested) && $CFG->restrictmodulesfor == 'requested') || $CFG->restrictmodulesfor == 'all')) {
241 $mform->addElement('header', '', get_string('restrictmodules'));
243 $options = array();
244 $options['0'] = get_string('no');
245 $options['1'] = get_string('yes');
246 $mform->addElement('select', 'restrictmodules', get_string('restrictmodules'), $options);
247 if (!empty($CFG->restrictbydefault)) {
248 $mform->setDefault('restrictmodules', 1);
251 $mods = array(0=>get_string('allownone'));
252 $mods += $DB->get_records_menu('modules', array('visible'=>1), 'name', 'id, name');
253 $mform->addElement('select', 'allowedmods', get_string('to'), $mods, array('multiple'=>'multiple', 'size'=>'10'));
254 $mform->disabledIf('allowedmods', 'restrictmodules', 'eq', 0);
255 // defaults are already in $course
256 } else {
257 // remove any mod restriction
258 $mform->addElement('hidden', 'restrictmodules', 0);
259 $mform->setType('restrictmodules', PARAM_INT);
261 } else {
262 $mform->addElement('hidden', 'restrictmodules');
263 $mform->setType('restrictmodules', PARAM_INT);
264 if (empty($course->id)) {
265 $mform->setConstant('restrictmodules', (int)($CFG->restrictmodulesfor == 'all'));
266 } else {
267 // keep previous
268 $mform->setConstant('restrictmodules', $course->restrictmodules);
272 /// customizable role names in this course
273 //--------------------------------------------------------------------------------
274 $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
275 $mform->addHelpButton('rolerenaming', 'rolerenaming');
277 if ($roles = get_all_roles()) {
278 if ($coursecontext) {
279 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS_RAW);
281 $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
282 foreach ($roles as $role) {
283 $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->name));
284 if (isset($role->localname)) {
285 $mform->setDefault('role_'.$role->id, $role->localname);
287 $mform->setType('role_'.$role->id, PARAM_TEXT);
288 if (!in_array($role->id, $assignableroles)) {
289 $mform->setAdvanced('role_'.$role->id);
294 //--------------------------------------------------------------------------------
295 $this->add_action_buttons();
296 //--------------------------------------------------------------------------------
297 $mform->addElement('hidden', 'id', null);
298 $mform->setType('id', PARAM_INT);
300 /// finally set the current form data
301 //--------------------------------------------------------------------------------
302 $this->set_data($course);
305 function definition_after_data() {
306 global $DB;
308 $mform = $this->_form;
310 // add available groupings
311 if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
312 $options = array();
313 if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
314 foreach ($groupings as $grouping) {
315 $options[$grouping->id] = format_string($grouping->name);
318 $gr_el =& $mform->getElement('defaultgroupingid');
319 $gr_el->load($options);
324 /// perform some extra moodle validation
325 function validation($data, $files) {
326 global $DB, $CFG;
328 $errors = parent::validation($data, $files);
329 if ($foundcourses = $DB->get_records('course', array('shortname'=>$data['shortname']))) {
330 if (!empty($data['id'])) {
331 unset($foundcourses[$data['id']]);
333 if (!empty($foundcourses)) {
334 foreach ($foundcourses as $foundcourse) {
335 $foundcoursenames[] = $foundcourse->fullname;
337 $foundcoursenamestring = implode(',', $foundcoursenames);
338 $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
342 $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
344 return $errors;