PageRenderTime 41ms CodeModel.GetById 16ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/course/edit_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 381 lines | 277 code | 62 blank | 42 comment | 50 complexity | 739ad6e8a378989b092ac3e9bc038a19 MD5 | raw file
  1<?php
  2
  3defined('MOODLE_INTERNAL') || die;
  4
  5require_once($CFG->libdir.'/formslib.php');
  6require_once($CFG->libdir.'/completionlib.php');
  7require_once($CFG->libdir. '/coursecatlib.php');
  8
  9/**
 10 * The form for handling editing a course.
 11 */
 12class course_edit_form extends moodleform {
 13    protected $course;
 14    protected $context;
 15
 16    /**
 17     * Form definition.
 18     */
 19    function definition() {
 20        global $CFG, $PAGE;
 21
 22        $mform    = $this->_form;
 23        $PAGE->requires->yui_module('moodle-course-formatchooser', 'M.course.init_formatchooser',
 24                array(array('formid' => $mform->getAttribute('id'))));
 25
 26        $course        = $this->_customdata['course']; // this contains the data of this form
 27        $category      = $this->_customdata['category'];
 28        $editoroptions = $this->_customdata['editoroptions'];
 29        $returnto = $this->_customdata['returnto'];
 30
 31        $systemcontext   = context_system::instance();
 32        $categorycontext = context_coursecat::instance($category->id);
 33
 34        if (!empty($course->id)) {
 35            $coursecontext = context_course::instance($course->id);
 36            $context = $coursecontext;
 37        } else {
 38            $coursecontext = null;
 39            $context = $categorycontext;
 40        }
 41
 42        $courseconfig = get_config('moodlecourse');
 43
 44        $this->course  = $course;
 45        $this->context = $context;
 46
 47        // Form definition with new course defaults.
 48        $mform->addElement('header','general', get_string('general', 'form'));
 49
 50        $mform->addElement('hidden', 'returnto', null);
 51        $mform->setType('returnto', PARAM_ALPHANUM);
 52        $mform->setConstant('returnto', $returnto);
 53
 54        $mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
 55        $mform->addHelpButton('fullname', 'fullnamecourse');
 56        $mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
 57        $mform->setType('fullname', PARAM_TEXT);
 58        if (!empty($course->id) and !has_capability('moodle/course:changefullname', $coursecontext)) {
 59            $mform->hardFreeze('fullname');
 60            $mform->setConstant('fullname', $course->fullname);
 61        }
 62
 63        $mform->addElement('text', 'shortname', get_string('shortnamecourse'), 'maxlength="100" size="20"');
 64        $mform->addHelpButton('shortname', 'shortnamecourse');
 65        $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
 66        $mform->setType('shortname', PARAM_TEXT);
 67        if (!empty($course->id) and !has_capability('moodle/course:changeshortname', $coursecontext)) {
 68            $mform->hardFreeze('shortname');
 69            $mform->setConstant('shortname', $course->shortname);
 70        }
 71
 72        // Verify permissions to change course category or keep current.
 73        if (empty($course->id)) {
 74            if (has_capability('moodle/course:create', $categorycontext)) {
 75                $displaylist = coursecat::make_categories_list('moodle/course:create');
 76                $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
 77                $mform->addHelpButton('category', 'coursecategory');
 78                $mform->setDefault('category', $category->id);
 79            } else {
 80                $mform->addElement('hidden', 'category', null);
 81                $mform->setType('category', PARAM_INT);
 82                $mform->setConstant('category', $category->id);
 83            }
 84        } else {
 85            if (has_capability('moodle/course:changecategory', $coursecontext)) {
 86                $displaylist = coursecat::make_categories_list('moodle/course:create');
 87                if (!isset($displaylist[$course->category])) {
 88                    //always keep current
 89                    $displaylist[$course->category] = coursecat::get($course->category, MUST_EXIST, true)->get_formatted_name();
 90                }
 91                $mform->addElement('select', 'category', get_string('coursecategory'), $displaylist);
 92                $mform->addHelpButton('category', 'coursecategory');
 93            } else {
 94                //keep current
 95                $mform->addElement('hidden', 'category', null);
 96                $mform->setType('category', PARAM_INT);
 97                $mform->setConstant('category', $course->category);
 98            }
 99        }
100
101        $choices = array();
102        $choices['0'] = get_string('hide');
103        $choices['1'] = get_string('show');
104        $mform->addElement('select', 'visible', get_string('visible'), $choices);
105        $mform->addHelpButton('visible', 'visible');
106        $mform->setDefault('visible', $courseconfig->visible);
107        if (!empty($course->id)) {
108            if (!has_capability('moodle/course:visibility', $coursecontext)) {
109                $mform->hardFreeze('visible');
110                $mform->setConstant('visible', $course->visible);
111            }
112        } else {
113            if (!guess_if_creator_will_have_course_capability('moodle/course:visibility', $categorycontext)) {
114                $mform->hardFreeze('visible');
115                $mform->setConstant('visible', $courseconfig->visible);
116            }
117        }
118
119        $mform->addElement('date_selector', 'startdate', get_string('startdate'));
120        $mform->addHelpButton('startdate', 'startdate');
121        $mform->setDefault('startdate', time() + 3600 * 24);
122
123        $mform->addElement('text','idnumber', get_string('idnumbercourse'),'maxlength="100"  size="10"');
124        $mform->addHelpButton('idnumber', 'idnumbercourse');
125        $mform->setType('idnumber', PARAM_RAW);
126        if (!empty($course->id) and !has_capability('moodle/course:changeidnumber', $coursecontext)) {
127            $mform->hardFreeze('idnumber');
128            $mform->setConstants('idnumber', $course->idnumber);
129        }
130
131        // Description.
132        $mform->addElement('header', 'descriptionhdr', get_string('description'));
133        $mform->setExpanded('descriptionhdr');
134
135        $mform->addElement('editor','summary_editor', get_string('coursesummary'), null, $editoroptions);
136        $mform->addHelpButton('summary_editor', 'coursesummary');
137        $mform->setType('summary_editor', PARAM_RAW);
138        $summaryfields = 'summary_editor';
139
140        if ($overviewfilesoptions = course_overviewfiles_options($course)) {
141            $mform->addElement('filemanager', 'overviewfiles_filemanager', get_string('courseoverviewfiles'), null, $overviewfilesoptions);
142            $mform->addHelpButton('overviewfiles_filemanager', 'courseoverviewfiles');
143            $summaryfields .= ',overviewfiles_filemanager';
144        }
145
146        if (!empty($course->id) and !has_capability('moodle/course:changesummary', $coursecontext)) {
147            // Remove the description header it does not contain anything any more.
148            $mform->removeElement('descriptionhdr');
149            $mform->hardFreeze($summaryfields);
150        }
151
152        // Course format.
153        $mform->addElement('header', 'courseformathdr', get_string('type_format', 'plugin'));
154
155        $courseformats = get_sorted_course_formats(true);
156        $formcourseformats = array();
157        foreach ($courseformats as $courseformat) {
158            $formcourseformats[$courseformat] = get_string('pluginname', "format_$courseformat");
159        }
160        if (isset($course->format)) {
161            $course->format = course_get_format($course)->get_format(); // replace with default if not found
162            if (!in_array($course->format, $courseformats)) {
163                // this format is disabled. Still display it in the dropdown
164                $formcourseformats[$course->format] = get_string('withdisablednote', 'moodle',
165                        get_string('pluginname', 'format_'.$course->format));
166            }
167        }
168
169        $mform->addElement('select', 'format', get_string('format'), $formcourseformats);
170        $mform->addHelpButton('format', 'format');
171        $mform->setDefault('format', $courseconfig->format);
172
173        // Button to update format-specific options on format change (will be hidden by JavaScript).
174        $mform->registerNoSubmitButton('updatecourseformat');
175        $mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));
176
177        // Just a placeholder for the course format options.
178        $mform->addElement('hidden', 'addcourseformatoptionshere');
179        $mform->setType('addcourseformatoptionshere', PARAM_BOOL);
180
181        // Appearance.
182        $mform->addElement('header', 'appearancehdr', get_string('appearance'));
183
184        if (!empty($CFG->allowcoursethemes)) {
185            $themeobjects = get_list_of_themes();
186            $themes=array();
187            $themes[''] = get_string('forceno');
188            foreach ($themeobjects as $key=>$theme) {
189                if (empty($theme->hidefromselector)) {
190                    $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
191                }
192            }
193            $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
194        }
195
196        $languages=array();
197        $languages[''] = get_string('forceno');
198        $languages += get_string_manager()->get_list_of_translations();
199        $mform->addElement('select', 'lang', get_string('forcelanguage'), $languages);
200        $mform->setDefault('lang', $courseconfig->lang);
201
202        // Multi-Calendar Support - see MDL-18375.
203        $calendartypes = \core_calendar\type_factory::get_list_of_calendar_types();
204        // We do not want to show this option unless there is more than one calendar type to display.
205        if (count($calendartypes) > 1) {
206            $calendars = array();
207            $calendars[''] = get_string('forceno');
208            $calendars += $calendartypes;
209            $mform->addElement('select', 'calendartype', get_string('forcecalendartype', 'calendar'), $calendars);
210        }
211
212        $options = range(0, 10);
213        $mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
214        $mform->addHelpButton('newsitems', 'newsitemsnumber');
215        $mform->setDefault('newsitems', $courseconfig->newsitems);
216
217        $mform->addElement('selectyesno', 'showgrades', get_string('showgrades'));
218        $mform->addHelpButton('showgrades', 'showgrades');
219        $mform->setDefault('showgrades', $courseconfig->showgrades);
220
221        $mform->addElement('selectyesno', 'showreports', get_string('showreports'));
222        $mform->addHelpButton('showreports', 'showreports');
223        $mform->setDefault('showreports', $courseconfig->showreports);
224
225        // Files and uploads.
226        $mform->addElement('header', 'filehdr', get_string('filesanduploads'));
227
228        if (!empty($course->legacyfiles) or !empty($CFG->legacyfilesinnewcourses)) {
229            if (empty($course->legacyfiles)) {
230                //0 or missing means no legacy files ever used in this course - new course or nobody turned on legacy files yet
231                $choices = array('0'=>get_string('no'), '2'=>get_string('yes'));
232            } else {
233                $choices = array('1'=>get_string('no'), '2'=>get_string('yes'));
234            }
235            $mform->addElement('select', 'legacyfiles', get_string('courselegacyfiles'), $choices);
236            $mform->addHelpButton('legacyfiles', 'courselegacyfiles');
237            if (!isset($courseconfig->legacyfiles)) {
238                // in case this was not initialised properly due to switching of $CFG->legacyfilesinnewcourses
239                $courseconfig->legacyfiles = 0;
240            }
241            $mform->setDefault('legacyfiles', $courseconfig->legacyfiles);
242        }
243
244        // Handle non-existing $course->maxbytes on course creation.
245        $coursemaxbytes = !isset($course->maxbytes) ? null : $course->maxbytes;
246
247        // Let's prepare the maxbytes popup.
248        $choices = get_max_upload_sizes($CFG->maxbytes, 0, 0, $coursemaxbytes);
249        $mform->addElement('select', 'maxbytes', get_string('maximumupload'), $choices);
250        $mform->addHelpButton('maxbytes', 'maximumupload');
251        $mform->setDefault('maxbytes', $courseconfig->maxbytes);
252
253        // Completion tracking.
254        if (completion_info::is_enabled_for_site()) {
255            $mform->addElement('header', 'completionhdr', get_string('completion', 'completion'));
256            $mform->addElement('selectyesno', 'enablecompletion', get_string('enablecompletion', 'completion'));
257            $mform->setDefault('enablecompletion', $courseconfig->enablecompletion);
258            $mform->addHelpButton('enablecompletion', 'enablecompletion', 'completion');
259        } else {
260            $mform->addElement('hidden', 'enablecompletion');
261            $mform->setType('enablecompletion', PARAM_INT);
262            $mform->setDefault('enablecompletion', 0);
263        }
264
265        enrol_course_edit_form($mform, $course, $context);
266
267        $mform->addElement('header','groups', get_string('groupsettingsheader', 'group'));
268
269        $choices = array();
270        $choices[NOGROUPS] = get_string('groupsnone', 'group');
271        $choices[SEPARATEGROUPS] = get_string('groupsseparate', 'group');
272        $choices[VISIBLEGROUPS] = get_string('groupsvisible', 'group');
273        $mform->addElement('select', 'groupmode', get_string('groupmode', 'group'), $choices);
274        $mform->addHelpButton('groupmode', 'groupmode', 'group');
275        $mform->setDefault('groupmode', $courseconfig->groupmode);
276
277        $mform->addElement('selectyesno', 'groupmodeforce', get_string('groupmodeforce', 'group'));
278        $mform->addHelpButton('groupmodeforce', 'groupmodeforce', 'group');
279        $mform->setDefault('groupmodeforce', $courseconfig->groupmodeforce);
280
281        //default groupings selector
282        $options = array();
283        $options[0] = get_string('none');
284        $mform->addElement('select', 'defaultgroupingid', get_string('defaultgrouping', 'group'), $options);
285
286        // Customizable role names in this course.
287        $mform->addElement('header','rolerenaming', get_string('rolerenaming'));
288        $mform->addHelpButton('rolerenaming', 'rolerenaming');
289
290        if ($roles = get_all_roles()) {
291            $roles = role_fix_names($roles, null, ROLENAME_ORIGINAL);
292            $assignableroles = get_roles_for_contextlevels(CONTEXT_COURSE);
293            foreach ($roles as $role) {
294                $mform->addElement('text', 'role_'.$role->id, get_string('yourwordforx', '', $role->localname));
295                $mform->setType('role_'.$role->id, PARAM_TEXT);
296            }
297        }
298
299        $this->add_action_buttons();
300
301        $mform->addElement('hidden', 'id', null);
302        $mform->setType('id', PARAM_INT);
303
304        // Finally set the current form data
305        $this->set_data($course);
306    }
307
308    /**
309     * Fill in the current page data for this course.
310     */
311    function definition_after_data() {
312        global $DB;
313
314        $mform = $this->_form;
315
316        // add available groupings
317        if ($courseid = $mform->getElementValue('id') and $mform->elementExists('defaultgroupingid')) {
318            $options = array();
319            if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid))) {
320                foreach ($groupings as $grouping) {
321                    $options[$grouping->id] = format_string($grouping->name);
322                }
323            }
324            core_collator::asort($options);
325            $gr_el =& $mform->getElement('defaultgroupingid');
326            $gr_el->load($options);
327        }
328
329        // add course format options
330        $formatvalue = $mform->getElementValue('format');
331        if (is_array($formatvalue) && !empty($formatvalue)) {
332            $courseformat = course_get_format((object)array('format' => $formatvalue[0]));
333
334            $elements = $courseformat->create_edit_form_elements($mform);
335            for ($i = 0; $i < count($elements); $i++) {
336                $mform->insertElementBefore($mform->removeElement($elements[$i]->getName(), false),
337                        'addcourseformatoptionshere');
338            }
339        }
340    }
341
342    /**
343     * Validation.
344     *
345     * @param array $data
346     * @param array $files
347     * @return array the errors that were found
348     */
349    function validation($data, $files) {
350        global $DB;
351
352        $errors = parent::validation($data, $files);
353
354        // Add field validation check for duplicate shortname.
355        if ($course = $DB->get_record('course', array('shortname' => $data['shortname']), '*', IGNORE_MULTIPLE)) {
356            if (empty($data['id']) || $course->id != $data['id']) {
357                $errors['shortname'] = get_string('shortnametaken', '', $course->fullname);
358            }
359        }
360
361        // Add field validation check for duplicate idnumber.
362        if (!empty($data['idnumber']) && (empty($data['id']) || $this->course->idnumber != $data['idnumber'])) {
363            if ($course = $DB->get_record('course', array('idnumber' => $data['idnumber']), '*', IGNORE_MULTIPLE)) {
364                if (empty($data['id']) || $course->id != $data['id']) {
365                    $errors['idnumber'] = get_string('courseidnumbertaken', 'error', $course->fullname);
366                }
367            }
368        }
369
370        $errors = array_merge($errors, enrol_course_edit_validation($data, $this->context));
371
372        $courseformat = course_get_format((object)array('format' => $data['format']));
373        $formaterrors = $courseformat->edit_form_validation($data, $files, $errors);
374        if (!empty($formaterrors) && is_array($formaterrors)) {
375            $errors = array_merge($errors, $formaterrors);
376        }
377
378        return $errors;
379    }
380}
381