weekly release 3.1dev
[moodle.git] / lib / form / tags.php
CommitLineData
9f1c9dfc 1<?php
6c1fd304
RT
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
9f1c9dfc 17
18/**
bb6ca6f3 19 * Tag autocomplete field.
6c1fd304 20 *
bb6ca6f3 21 * Contains HTML class for editing tags, both official and personal.
9f1c9dfc 22 *
6c1fd304
RT
23 * @package core_form
24 * @copyright 2009 Tim Hunt
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
9f1c9dfc 28global $CFG;
bb6ca6f3 29require_once($CFG->libdir . '/form/autocomplete.php');
9f1c9dfc 30
31/**
6c1fd304
RT
32 * Form field type for editing tags.
33 *
bb6ca6f3 34 * HTML class for editing tags, both official and personal.
6c1fd304
RT
35 *
36 * @package core_form
6c1fd304
RT
37 * @copyright 2009 Tim Hunt
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9f1c9dfc 39 */
bb6ca6f3 40class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete {
6c1fd304
RT
41 /**
42 * Inidcates that the user should be the usual interface, with the official
9f1c9dfc 43 * tags listed seprately, and a text box where they can type anything.
6c1fd304
RT
44 * @var int
45 */
f48dccd4 46 const DEFAULTUI = 'defaultui';
6c1fd304
RT
47
48 /**
49 * Indicates that the user should only be allowed to select official tags.
50 * @var int
51 */
f48dccd4 52 const ONLYOFFICIAL = 'onlyofficial';
6c1fd304
RT
53
54 /**
55 * Indicates that the user should just be given a text box to type in (they
9f1c9dfc 56 * can still type official tags though.
6c1fd304
RT
57 * @var int
58 */
f48dccd4 59 const NOOFFICIAL = 'noofficial';
9f1c9dfc 60
61 /**
bb6ca6f3 62 * @var boolean $showingofficial Official tags shown? (if not, then don't show link to manage official tags).
9f1c9dfc 63 */
bb6ca6f3 64 protected $showingofficial = false;
9f1c9dfc 65
66 /**
f48dccd4 67 * Constructor
68 *
69 * @param string $elementName Element name
70 * @param mixed $elementLabel Label(s) for an element
71 * @param array $options Options to control the element's display
72 * @param mixed $attributes Either a typical HTML attribute string or an associative array.
73 */
9f1c9dfc 74 function MoodleQuickForm_tags($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
bb6ca6f3
DW
75 if (!isset($options['display'])) {
76 $options['display'] = self::DEFAULTUI;
9f1c9dfc 77 }
9f1c9dfc 78
bb6ca6f3 79 $this->showingofficial = $options['display'] != MoodleQuickForm_tags::NOOFFICIAL;
9f1c9dfc 80
bb6ca6f3
DW
81 $validoptions = array();
82 if ($this->showingofficial) {
83 $validoptions = $this->load_official_tags();
9f1c9dfc 84 }
bb6ca6f3
DW
85 // 'tags' option allows us to type new tags.
86 if ($options['display'] == MoodleQuickForm_tags::ONLYOFFICIAL) {
87 $attributes['tags'] = false;
88 } else {
89 $attributes['tags'] = true;
9f1c9dfc 90 }
bb6ca6f3
DW
91 $attributes['multiple'] = 'multiple';
92 $attributes['placeholder'] = get_string('entertags', 'tag');
97d2ea7f 93 $attributes['showsuggestions'] = $this->showingofficial;
9f1c9dfc 94
bb6ca6f3 95 parent::MoodleQuickForm_autocomplete($elementName, $elementLabel, $validoptions, $attributes);
9f1c9dfc 96 }
97
6c1fd304 98 /**
bb6ca6f3 99 * Returns HTML for select form element.
6c1fd304 100 *
bb6ca6f3 101 * @return string
6c1fd304 102 */
bb6ca6f3
DW
103 function toHtml(){
104 global $CFG, $OUTPUT;
f48dccd4 105
bb6ca6f3
DW
106 if (empty($CFG->usetags)) {
107 debugging('A tags formslib field has been created even thought $CFG->usetags is false.', DEBUG_DEVELOPER);
f48dccd4 108 }
f48dccd4 109
bb6ca6f3
DW
110 $managelink = '';
111 if (has_capability('moodle/tag:manage', context_system::instance()) && $this->showingofficial) {
112 $url = $CFG->wwwroot .'/tag/manage.php';
113 $managelink = ' ' . $OUTPUT->action_link($url, get_string('manageofficialtags', 'tag'));
114 }
f48dccd4 115
bb6ca6f3 116 return parent::toHTML() . $managelink;
9f1c9dfc 117 }
118
6c1fd304 119 /**
bb6ca6f3 120 * Internal function to load official tags
6c1fd304 121 *
bb6ca6f3 122 * @access protected
6c1fd304 123 */
bb6ca6f3
DW
124 protected function load_official_tags() {
125 global $CFG, $DB;
9f1c9dfc 126
bb6ca6f3
DW
127 $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
128 $records = $DB->get_records('tag', array('tagtype' => 'official'), $namefield, 'id,' . $namefield);
9f1c9dfc 129 $tags = array();
9f1c9dfc 130
bb6ca6f3
DW
131 foreach ($records as $record) {
132 $tags[$record->$namefield] = $record->$namefield;
9f1c9dfc 133 }
bb6ca6f3 134 return $tags;
9f1c9dfc 135 }
bb6ca6f3 136
9f1c9dfc 137}