MDL-33472 google plugins: move helper functions to upgradelib.php
[moodle.git] / cohort / edit_form.php
blobfb7409e901ecb0cc3744964a3452acbede9d96a2
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Cohort related management functions, this file needs to be included manually.
20 * @package core_cohort
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot . '/lib/formslib.php');
29 class cohort_edit_form extends moodleform {
31 /**
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_TEXT);
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('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed.
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) {
62 global $DB;
64 $errors = parent::validation($data, $files);
66 $idnumber = trim($data['idnumber']);
67 if ($idnumber === '') {
68 // Fine, empty is ok.
70 } else if ($data['id']) {
71 $current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
72 if ($current->idnumber !== $idnumber) {
73 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
74 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
78 } else {
79 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
80 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
84 return $errors;
87 protected function get_category_options($currentcontextid) {
88 global $CFG;
89 require_once($CFG->libdir. '/coursecatlib.php');
90 $displaylist = coursecat::make_categories_list('moodle/cohort:manage');
91 $options = array();
92 $syscontext = context_system::instance();
93 if (has_capability('moodle/cohort:manage', $syscontext)) {
94 $options[$syscontext->id] = $syscontext->get_context_name();
96 foreach ($displaylist as $cid=>$name) {
97 $context = context_coursecat::instance($cid);
98 $options[$context->id] = $name;
100 // Always add current - this is not likely, but if the logic gets changed it might be a problem.
101 if (!isset($options[$currentcontextid])) {
102 $context = context::instance_by_id($currentcontextid, MUST_EXIST);
103 $options[$context->id] = $syscontext->get_context_name();
105 return $options;