MDL-64506 phpunit: Update unit tests to use classic
[moodle.git] / cohort / edit_form.php
CommitLineData
b980c56e 1<?php
b980c56e
PS
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.
13//
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/>.
16
17/**
18 * Cohort related management functions, this file needs to be included manually.
19 *
6eb8bf7f 20 * @package core_cohort
707f4136 21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
b980c56e
PS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
6eb8bf7f 25defined('MOODLE_INTERNAL') || die();
bfebaf64 26
a379029c 27require_once($CFG->dirroot . '/lib/formslib.php');
b980c56e
PS
28
29class cohort_edit_form extends moodleform {
30
a379029c 31 /**
9e1065a8 32 * Define the cohort edit form
a379029c 33 */
9e1065a8 34 public function definition() {
88cb8b78 35 global $CFG;
b980c56e
PS
36
37 $mform = $this->_form;
38 $editoroptions = $this->_customdata['editoroptions'];
9e1065a8 39 $cohort = $this->_customdata['data'];
b980c56e
PS
40
41 $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
42 $mform->addRule('name', get_string('required'), 'required', null, 'client');
071e68f9 43 $mform->setType('name', PARAM_TEXT);
b980c56e 44
9e1065a8
PS
45 $options = $this->get_category_options($cohort->contextid);
46 $mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
47
b980c56e 48 $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
6eb8bf7f 49 $mform->setType('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed.
b980c56e 50
80f98467
MG
51 $mform->addElement('advcheckbox', 'visible', get_string('visible', 'cohort'));
52 $mform->setDefault('visible', 1);
53 $mform->addHelpButton('visible', 'visible', 'cohort');
54
b980c56e
PS
55 $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
56 $mform->setType('description_editor', PARAM_RAW);
57
88cb8b78
SA
58 if (!empty($CFG->allowcohortthemes)) {
59 $themes = array_merge(array('' => get_string('forceno')), cohort_get_list_of_themes());
60 $mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
61 }
62
a379029c 63 $mform->addElement('hidden', 'id');
b980c56e
PS
64 $mform->setType('id', PARAM_INT);
65
274dc068
MG
66 if (isset($this->_customdata['returnurl'])) {
67 $mform->addElement('hidden', 'returnurl', $this->_customdata['returnurl']->out_as_local_url());
68 $mform->setType('returnurl', PARAM_LOCALURL);
69 }
70
b980c56e 71 $this->add_action_buttons();
9e1065a8
PS
72
73 $this->set_data($cohort);
b980c56e
PS
74 }
75
9e1065a8 76 public function validation($data, $files) {
b980c56e
PS
77 global $DB;
78
79 $errors = parent::validation($data, $files);
b980c56e
PS
80
81 $idnumber = trim($data['idnumber']);
0defeb16 82 if ($idnumber === '') {
6eb8bf7f 83 // Fine, empty is ok.
0defeb16
PS
84
85 } else if ($data['id']) {
a379029c
PS
86 $current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
87 if ($current->idnumber !== $idnumber) {
88 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
89 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
90 }
91 }
0defeb16 92
a379029c
PS
93 } else {
94 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
95 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
96 }
b980c56e
PS
97 }
98
99 return $errors;
100 }
9e1065a8
PS
101
102 protected function get_category_options($currentcontextid) {
442f12f8 103 $displaylist = core_course_category::make_categories_list('moodle/cohort:manage');
9e1065a8 104 $options = array();
6ca657a7 105 $syscontext = context_system::instance();
9e1065a8 106 if (has_capability('moodle/cohort:manage', $syscontext)) {
6eb8bf7f 107 $options[$syscontext->id] = $syscontext->get_context_name();
9e1065a8
PS
108 }
109 foreach ($displaylist as $cid=>$name) {
abdf2b67 110 $context = context_coursecat::instance($cid);
9e1065a8
PS
111 $options[$context->id] = $name;
112 }
6eb8bf7f 113 // Always add current - this is not likely, but if the logic gets changed it might be a problem.
9e1065a8 114 if (!isset($options[$currentcontextid])) {
d197ea43 115 $context = context::instance_by_id($currentcontextid, MUST_EXIST);
6eb8bf7f 116 $options[$context->id] = $syscontext->get_context_name();
9e1065a8
PS
117 }
118 return $options;
119 }
b980c56e 120}
0defeb16 121