weekly release 5.0dev
[moodle.git] / tag / edit_form.php
blob8a628f9f2951ea550ede56e4c8cd4e590d17a500
1 <?php
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.
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/>.
17 if (!defined('MOODLE_INTERNAL')) {
18 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
21 require_once($CFG->dirroot.'/lib/formslib.php');
23 /**
24 * Defines the form for editing tags
26 * @package core_tag
27 * @category tag
28 * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 class tag_edit_form extends moodleform {
33 /**
34 * Overrides the abstract moodleform::definition method for defining what the form that is to be
35 * presented to the user.
37 function definition() {
39 $mform =& $this->_form;
41 $mform->addElement('header', 'tag', get_string('description','tag'));
43 $mform->addElement('hidden', 'id');
44 $mform->setType('id', PARAM_INT);
46 $mform->addElement('hidden', 'returnurl');
47 $mform->setType('returnurl', PARAM_LOCALURL);
49 $systemcontext = context_system::instance();
51 if (has_capability('moodle/tag:manage', $systemcontext)) {
52 $mform->addElement('text', 'rawname', get_string('name', 'tag'), ['maxlength' => TAG_MAX_LENGTH, 'size' => 50]);
53 $mform->setType('rawname', PARAM_TAG);
56 $mform->addElement('editor', 'description_editor', get_string('description', 'tag'), null, $this->_customdata['editoroptions']);
58 if (has_capability('moodle/tag:manage', $systemcontext)) {
59 $mform->addElement('checkbox', 'isstandard', get_string('standardtag', 'tag'));
62 $mform->addElement('tags', 'relatedtags', get_string('relatedtags', 'tag'),
63 array('tagcollid' => $this->_customdata['tag']->tagcollid));
65 $this->add_action_buttons(true, get_string('updatetag', 'tag'));
69 /**
70 * Custom form validation
72 * @param array $data
73 * @param array $files
74 * @return array
76 public function validation($data, $files) {
77 $errors = parent::validation($data, $files);
79 if (isset($data['rawname'])) {
80 $newname = core_text::strtolower($data['rawname']);
81 $tag = $this->_customdata['tag'];
82 if ($tag->name != $newname) {
83 // The name has changed, let's make sure it's not another existing tag.
84 if (core_tag_tag::get_by_name($tag->tagcollid, $newname)) {
85 // Something exists already, so flag an error.
86 $errors['rawname'] = get_string('namesalreadybeeingused', 'tag');
91 return $errors;