on-demand release 4.0dev+
[moodle.git] / tag / edit_form.php
CommitLineData
18ec15e8 1<?php
000ebd5c
DM
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
bcdcee90 17
000ebd5c 18/**
bcdcee90
GGC
19 * @package core_tag
20 * @category tag
000ebd5c
DM
21 * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
bfebaf64
MD
25if (!defined('MOODLE_INTERNAL')) {
26 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
27}
28
c933a060 29require_once($CFG->dirroot.'/lib/formslib.php');
30
bcdcee90
GGC
31/**
32 * Defines the form for editing tags
33 *
34 * @package core_tag
35 * @category tag
36 * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
c933a060 39class tag_edit_form extends moodleform {
40
bcdcee90
GGC
41 /**
42 * Overrides the abstract moodleform::definition method for defining what the form that is to be
43 * presented to the user.
44 */
7e6e76a6 45 function definition () {
c933a060 46
7e6e76a6 47 $mform =& $this->_form;
d4be9d6d 48
c933a060 49 $mform->addElement('header', 'tag', get_string('description','tag'));
d4be9d6d 50
c933a060 51 $mform->addElement('hidden', 'id');
d18e0fe6 52 $mform->setType('id', PARAM_INT);
d4be9d6d 53
8e355853
MG
54 $mform->addElement('hidden', 'returnurl');
55 $mform->setType('returnurl', PARAM_LOCALURL);
56
43731030 57 $systemcontext = context_system::instance();
7e6e76a6 58
59 if (has_capability('moodle/tag:manage', $systemcontext)) {
6bfe7aac 60 $mform->addElement('text', 'rawname', get_string('name', 'tag'),
7e6e76a6 61 'maxlength="'.TAG_MAX_LENGTH.'" size="'.TAG_MAX_LENGTH.'"');
8e355853 62 $mform->setType('rawname', PARAM_TAG);
7e6e76a6 63 }
64
8bdc9cac 65 $mform->addElement('editor', 'description_editor', get_string('description', 'tag'), null, $this->_customdata['editoroptions']);
d4be9d6d 66
603443b5 67 if (has_capability('moodle/tag:manage', $systemcontext)) {
e11d7380 68 $mform->addElement('checkbox', 'isstandard', get_string('standardtag', 'tag'));
603443b5 69 }
70
c026a28d
MG
71 $mform->addElement('tags', 'relatedtags', get_string('relatedtags', 'tag'),
72 array('tagcollid' => $this->_customdata['tag']->tagcollid));
d4be9d6d 73
8e355853 74 $this->add_action_buttons(true, get_string('updatetag', 'tag'));
d4be9d6d 75
c933a060 76 }
77
c026a28d
MG
78 /**
79 * Custom form validation
80 *
81 * @param array $data
82 * @param array $files
83 * @return array
84 */
85 public function validation($data, $files) {
86 $errors = parent::validation($data, $files);
87
88 if (isset($data['rawname'])) {
89 $newname = core_text::strtolower($data['rawname']);
90 $tag = $this->_customdata['tag'];
91 if ($tag->name != $newname) {
92 // The name has changed, let's make sure it's not another existing tag.
93 if (core_tag_tag::get_by_name($tag->tagcollid, $newname)) {
94 // Something exists already, so flag an error.
95 $errors['rawname'] = get_string('namesalreadybeeingused', 'tag');
96 }
97 }
98 }
99
100 return $errors;
101 }
102
ed15e8a8 103}