weekly release 2.3dev
[moodle.git] / lib / form / tags.php
CommitLineData
9f1c9dfc 1<?php
2///////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
7// http://moodle.org //
8// //
9// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
10// //
11// This program is free software; you can redistribute it and/or modify //
12// it under the terms of the GNU General Public License as published by //
13// the Free Software Foundation; either version 2 of the License, or //
14// (at your option) later version. //
15// //
16// This program is distributed in the hope that it will be useful, //
17// but WITHOUT ANY WARRANTY; without even the implied warranty of //
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19// GNU General Public License for more details: //
20// //
21// http://www.gnu.org/copyleft/gpl.html //
22// //
23///////////////////////////////////////////////////////////////////////////
24
25/**
26 * Formslib field type for editing tags, both official and peronal.
27 *
28 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
29 * @package formslib
30 *//* **/
31global $CFG;
32require_once($CFG->libdir . '/form/group.php');
33
34/**
35 * Formslib field type for editing tags.
36 */
37class MoodleQuickForm_tags extends MoodleQuickForm_group {
38 /** Inidcates that the user should be the usual interface, with the official
39 * tags listed seprately, and a text box where they can type anything.
40 * @var integer */
f48dccd4 41 const DEFAULTUI = 'defaultui';
9f1c9dfc 42 /** Indicates that the user should only be allowed to select official tags.
43 * @var integer */
f48dccd4 44 const ONLYOFFICIAL = 'onlyofficial';
9f1c9dfc 45 /** Indicates that the user should just be given a text box to type in (they
46 * can still type official tags though.
47 * @var integer */
f48dccd4 48 const NOOFFICIAL = 'noofficial';
9f1c9dfc 49
50 /**
51 * Control the fieldnames for form elements
52 *
53 * display => integer, one of the constants above.
54 */
f48dccd4 55 protected $_options = array('display' => MoodleQuickForm_tags::DEFAULTUI);
9f1c9dfc 56
f48dccd4 57 /**
58 * Caches the list of official tags, to save repeat DB queries.
59 * @var array
60 */
61 protected $_officialtags = null;
9f1c9dfc 62
63 /**
f48dccd4 64 * Constructor
65 *
66 * @param string $elementName Element name
67 * @param mixed $elementLabel Label(s) for an element
68 * @param array $options Options to control the element's display
69 * @param mixed $attributes Either a typical HTML attribute string or an associative array.
70 */
9f1c9dfc 71 function MoodleQuickForm_tags($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
72 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
73 $this->_persistantFreeze = true;
74 $this->_appendName = true;
75 $this->_type = 'tags';
76 // set the options, do not bother setting bogus ones
77 if (is_array($options)) {
78 foreach ($options as $name => $value) {
79 if (isset($this->_options[$name])) {
80 if (is_array($value) && is_array($this->_options[$name])) {
f48dccd4 81 $this->_options[$name] = array_merge($this->_options[$name], $value);
9f1c9dfc 82 } else {
83 $this->_options[$name] = $value;
84 }
85 }
86 }
87 }
8942fd77 88 global $CFG;
89 if (empty($CFG->usetags)) {
90 debugging('A tags formslib field has been created even thought $CFG->usetags is false.', DEBUG_DEVELOPER);
91 }
9f1c9dfc 92 }
93
f48dccd4 94 protected function _load_official_tags() {
9f1c9dfc 95 global $CFG, $DB;
f48dccd4 96 if (!is_null($this->_officialtags)) {
97 return;
98 }
99 $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
100 $this->_officialtags = $DB->get_records_menu('tag', array('tagtype' => 'official'), $namefield, 'id,' . $namefield);
101 }
102
103 function _createElements() {
3bd6b994 104 global $CFG, $OUTPUT;
9f1c9dfc 105 $this->_elements = array();
106
107 // Official tags.
f48dccd4 108 $showingofficial = $this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL;
109 if ($showingofficial) {
110 $this->_load_official_tags();
111
9f1c9dfc 112 // If the user can manage official tags, give them a link to manage them.
113 $label = get_string('otags', 'tag');
114 if (has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM))) {
75015e5f
PS
115 $url = $CFG->wwwroot .'/tag/manage.php';
116 $label .= ' (' . $OUTPUT->action_link(
117 $url,
118 get_string('manageofficialtags', 'tag'),
119 new popup_action('click', $url, 'managetags'),
120 array('title'=>get_string('newwindow'))) . ')';
9f1c9dfc 121 }
122
123 // Get the list of official tags.
124 $noofficial = false;
f48dccd4 125 if (empty($this->_officialtags)) {
9f1c9dfc 126 $officialtags = array('' => get_string('none'));
127 $noofficial = true;
128 } else {
f48dccd4 129 $officialtags = array_combine($this->_officialtags, $this->_officialtags);
9f1c9dfc 130 }
131
132 // Create the element.
133 $size = min(5, count($officialtags));
134 $officialtagsselect = MoodleQuickForm::createElement('select', 'officialtags', $label, $officialtags, array('size' => $size));
135 $officialtagsselect->setMultiple(true);
136 if ($noofficial) {
137 $officialtagsselect->updateAttributes(array('disabled' => 'disabled'));
138 }
9f1c9dfc 139 $this->_elements[] = $officialtagsselect;
140 }
141
142 // Other tags.
143 if ($this->_options['display'] != MoodleQuickForm_tags::ONLYOFFICIAL) {
f48dccd4 144 if ($showingofficial) {
145 $label = get_string('othertags', 'tag');
146 } else {
147 $label = get_string('entertags', 'tag');
148 }
149 $othertags = MoodleQuickForm::createElement('textarea', 'othertags', $label, array('cols'=>'40', 'rows'=>'5'));
9f1c9dfc 150 $this->_elements[] = $othertags;
151 }
152
153 // Paradoxically, the only way to get labels output is to ask for 'hidden'
154 // labels, and then override the .accesshide class in the CSS!
155 foreach ($this->_elements as $element){
156 if (method_exists($element, 'setHiddenLabel')){
157 $element->setHiddenLabel(true);
158 }
159 }
160 }
161
f48dccd4 162 function onQuickFormEvent($event, $arg, &$caller) {
163 switch ($event) {
164 case 'updateValue':
165 // Get the value we should be setting.
166 $value = $this->_findValue($caller->_constantValues);
167 if (null === $value) {
168 // if no boxes were checked, then there is no value in the array
169 // yet we don't want to display default value in this case
170 if ($caller->isSubmitted()) {
171 $value = $this->_findValue($caller->_submitValues);
172 } else {
173 $value = $this->_findValue($caller->_defaultValues);
174 }
175 }
176
177 if (!empty($value) && !(isset($value['officialtags']) || isset($value['othertags']))) {
178 // Separate the official and unoffical tags, if necessary.
179 $official = array();
180 $other = array();
181 if ($this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL) {
182 $this->_load_official_tags();
6f5f3cef 183 if (!empty($this->_officialtags)) {
184 $officaltags = array_combine($this->_officialtags, $this->_officialtags);
185 } else {
186 $officaltags = array();
187 }
f48dccd4 188 foreach ($value as $tag) {
189 if (isset($officaltags[$tag])) {
190 $official[] = $tag;
191 } else {
192 $other[] = $tag;
193 }
194 }
195 } else {
196 $other = $value;
197 }
198 $value = array('officialtags' => $official, 'othertags' => implode(', ', $other));
199 }
200 if (!empty($value)) {
201 $this->setValue($value);
202 }
203
204 break;
205 default:
206 return parent::onQuickFormEvent($event, $arg, $caller);
207 }
208 }
209
9f1c9dfc 210 function toHtml() {
211 require_once('HTML/QuickForm/Renderer/Default.php');
bd69b9db 212 $renderer = new HTML_QuickForm_Renderer_Default();
9f1c9dfc 213 $renderer->setElementTemplate('{element}');
214 parent::accept($renderer);
f48dccd4 215 return $renderer->toHtml();
216 }
217
218 function accept(&$renderer, $required = false, $error = null)
219 {
220 $renderer->renderElement($this, $required, $error);
9f1c9dfc 221 }
222
223 function exportValue(&$submitValues, $assoc = false) {
224 $valuearray = array();
225
226 // Get the data out of our child elements.
227 foreach ($this->_elements as $element){
228 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
229 if ($thisexport != null){
230 $valuearray += $thisexport;
231 }
232 }
233
234 // Get any manually typed tags.
235 $tags = array();
236 if ($this->_options['display'] != MoodleQuickForm_tags::ONLYOFFICIAL &&
237 !empty($valuearray['othertags'])) {
238 $rawtags = explode(',', clean_param($valuearray['othertags'], PARAM_NOTAGS));
239 foreach ($rawtags as $tag) {
240 $tags[] = trim($tag);
241 }
242 }
243
244 // Add any official tags that were selected.
245 if ($this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL &&
246 !empty($valuearray['officialtags'])) {
247 $tags = array_unique(array_merge($tags, $valuearray['officialtags']));
248 }
249
250 return array($this->getName() => $tags);
251 }
252}