weekly release 2.1.8+
[moodle.git] / cohort / edit_form.php
blobd449980f87e62a38dedb425532cce9501d24ceb6
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * Cohort related management functions, this file needs to be included manually.
21 * @package core
22 * @subpackage cohort
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 if (!defined('MOODLE_INTERNAL')) {
28 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
31 require_once($CFG->dirroot . '/lib/formslib.php');
33 class cohort_edit_form extends moodleform {
35 /**
36 * Define the cohort edit form
38 public function definition() {
40 $mform = $this->_form;
41 $editoroptions = $this->_customdata['editoroptions'];
42 $cohort = $this->_customdata['data'];
44 $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
45 $mform->addRule('name', get_string('required'), 'required', null, 'client');
46 $mform->setType('name', PARAM_MULTILANG);
48 $options = $this->get_category_options($cohort->contextid);
49 $mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
51 $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
52 $mform->setType('idnumber', PARAM_RAW); // idnumbers are plain text, must not be changed
54 $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
55 $mform->setType('description_editor', PARAM_RAW);
57 $mform->addElement('hidden', 'id');
58 $mform->setType('id', PARAM_INT);
60 $this->add_action_buttons();
62 $this->set_data($cohort);
65 public function validation($data, $files) {
66 global $DB;
68 $errors = parent::validation($data, $files);
70 $idnumber = trim($data['idnumber']);
71 if ($idnumber === '') {
72 // fine, empty is ok
74 } else if ($data['id']) {
75 $current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
76 if ($current->idnumber !== $idnumber) {
77 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
78 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
82 } else {
83 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
84 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
88 return $errors;
91 protected function get_category_options($currentcontextid) {
92 $displaylist = array();
93 $parentlist = array();
94 make_categories_list($displaylist, $parentlist, 'moodle/cohort:manage');
95 $options = array();
96 $syscontext = get_context_instance(CONTEXT_SYSTEM);
97 if (has_capability('moodle/cohort:manage', $syscontext)) {
98 $options[$syscontext->id] = print_context_name($syscontext);
100 foreach ($displaylist as $cid=>$name) {
101 $context = get_context_instance(CONTEXT_COURSECAT, $cid, MUST_EXIST);
102 $options[$context->id] = $name;
104 // always add current - this is not likely, but if the logic gets changed it might be a problem
105 if (!isset($options[$currentcontextid])) {
106 $context = get_context_instance_by_id($currentcontextid, MUST_EXIST);
107 $options[$context->id] = print_context_name($syscontext);
109 return $options;