3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Cohort related management functions, this file needs to be included manually.
23 * @copyright 2010 Petr Skoda (info@skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once($CFG->dirroot . '/lib/formslib.php');
29 class cohort_edit_form extends moodleform {
32 * Define the cohort edit form
34 public function definition() {
36 $mform = $this->_form;
37 $editoroptions = $this->_customdata['editoroptions'];
38 $cohort = $this->_customdata['data'];
40 $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
41 $mform->addRule('name', get_string('required'), 'required', null, 'client');
42 $mform->setType('name', PARAM_MULTILANG);
44 $options = $this->get_category_options($cohort->contextid);
45 $mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
47 $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
48 $mform->setType('name', PARAM_RAW);
50 $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
51 $mform->setType('description_editor', PARAM_RAW);
53 $mform->addElement('hidden', 'id');
54 $mform->setType('id', PARAM_INT);
56 $this->add_action_buttons();
58 $this->set_data($cohort);
61 public function validation($data, $files) {
64 $errors = parent::validation($data, $files);
66 $idnumber = trim($data['idnumber']);
68 $current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
69 if ($current->idnumber !== $idnumber) {
70 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
71 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
75 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
76 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
83 protected function get_category_options($currentcontextid) {
84 $displaylist = array();
85 $parentlist = array();
86 make_categories_list($displaylist, $parentlist, 'moodle/cohort:manage');
88 $syscontext = get_context_instance(CONTEXT_SYSTEM);
89 if (has_capability('moodle/cohort:manage', $syscontext)) {
90 $options[$syscontext->id] = print_context_name($syscontext);
92 foreach ($displaylist as $cid=>$name) {
93 $context = get_context_instance(CONTEXT_COURSECAT, $cid, MUST_EXIST);
94 $options[$context->id] = $name;
96 // always add current - this is not likely, but if the logic get's changed it might be a problem
97 if (!isset($options[$currentcontextid])) {
98 $context = get_context_instance_by_id($currentcontextid, MUST_EXIST);
99 $options[$context->id] = print_context_name($syscontext);