fixed phpdocs package and improved author link
[moodle.git] / cohort / edit_form.php
CommitLineData
b980c56e
PS
1<?php
2
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.
14//
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/>.
17
18/**
19 * Cohort related management functions, this file needs to be included manually.
20 *
707f4136 21 * @package core
b980c56e 22 * @subpackage cohort
707f4136 23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
b980c56e
PS
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
bfebaf64
MD
27if (!defined('MOODLE_INTERNAL')) {
28 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
29}
30
a379029c 31require_once($CFG->dirroot . '/lib/formslib.php');
b980c56e
PS
32
33class cohort_edit_form extends moodleform {
34
a379029c 35 /**
9e1065a8 36 * Define the cohort edit form
a379029c 37 */
9e1065a8 38 public function definition() {
b980c56e
PS
39
40 $mform = $this->_form;
41 $editoroptions = $this->_customdata['editoroptions'];
9e1065a8 42 $cohort = $this->_customdata['data'];
b980c56e
PS
43
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);
47
9e1065a8
PS
48 $options = $this->get_category_options($cohort->contextid);
49 $mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
50
b980c56e
PS
51 $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
52 $mform->setType('name', PARAM_RAW);
53
54 $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
55 $mform->setType('description_editor', PARAM_RAW);
56
a379029c 57 $mform->addElement('hidden', 'id');
b980c56e
PS
58 $mform->setType('id', PARAM_INT);
59
b980c56e 60 $this->add_action_buttons();
9e1065a8
PS
61
62 $this->set_data($cohort);
b980c56e
PS
63 }
64
9e1065a8 65 public function validation($data, $files) {
b980c56e
PS
66 global $DB;
67
68 $errors = parent::validation($data, $files);
b980c56e
PS
69
70 $idnumber = trim($data['idnumber']);
0defeb16
PS
71 if ($idnumber === '') {
72 // fine, empty is ok
73
74 } else if ($data['id']) {
a379029c
PS
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');
79 }
80 }
0defeb16 81
a379029c
PS
82 } else {
83 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
84 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
85 }
b980c56e
PS
86 }
87
88 return $errors;
89 }
9e1065a8
PS
90
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);
99 }
100 foreach ($displaylist as $cid=>$name) {
101 $context = get_context_instance(CONTEXT_COURSECAT, $cid, MUST_EXIST);
102 $options[$context->id] = $name;
103 }
edaf7ff3 104 // always add current - this is not likely, but if the logic gets changed it might be a problem
9e1065a8
PS
105 if (!isset($options[$currentcontextid])) {
106 $context = get_context_instance_by_id($currentcontextid, MUST_EXIST);
107 $options[$context->id] = print_context_name($syscontext);
108 }
109 return $options;
110 }
b980c56e 111}
0defeb16 112