forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_form.php
117 lines (94 loc) · 4.38 KB
/
edit_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package core_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/lib/formslib.php');
class cohort_edit_form extends moodleform {
/**
* Define the cohort edit form
*/
public function definition() {
$mform = $this->_form;
$editoroptions = $this->_customdata['editoroptions'];
$cohort = $this->_customdata['data'];
$mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
$mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->setType('name', PARAM_TEXT);
$options = $this->get_category_options($cohort->contextid);
$mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
$mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
$mform->setType('idnumber', PARAM_RAW); // Idnumbers are plain text, must not be changed.
$mform->addElement('advcheckbox', 'visible', get_string('visible', 'cohort'));
$mform->setDefault('visible', 1);
$mform->addHelpButton('visible', 'visible', 'cohort');
$mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
$mform->setType('description_editor', PARAM_RAW);
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
if (isset($this->_customdata['returnurl'])) {
$mform->addElement('hidden', 'returnurl', $this->_customdata['returnurl']->out_as_local_url());
$mform->setType('returnurl', PARAM_LOCALURL);
}
$this->add_action_buttons();
$this->set_data($cohort);
}
public function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
$idnumber = trim($data['idnumber']);
if ($idnumber === '') {
// Fine, empty is ok.
} else if ($data['id']) {
$current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
if ($current->idnumber !== $idnumber) {
if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
$errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
}
}
} else {
if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
$errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
}
}
return $errors;
}
protected function get_category_options($currentcontextid) {
global $CFG;
require_once($CFG->libdir. '/coursecatlib.php');
$displaylist = coursecat::make_categories_list('moodle/cohort:manage');
$options = array();
$syscontext = context_system::instance();
if (has_capability('moodle/cohort:manage', $syscontext)) {
$options[$syscontext->id] = $syscontext->get_context_name();
}
foreach ($displaylist as $cid=>$name) {
$context = context_coursecat::instance($cid);
$options[$context->id] = $name;
}
// Always add current - this is not likely, but if the logic gets changed it might be a problem.
if (!isset($options[$currentcontextid])) {
$context = context::instance_by_id($currentcontextid, MUST_EXIST);
$options[$context->id] = $syscontext->get_context_name();
}
return $options;
}
}