Whitespace cleanup.
[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 */
41 const DEFAULTUI = 0;
42 /** Indicates that the user should only be allowed to select official tags.
43 * @var integer */
44 const ONLYOFFICIAL = 1;
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 */
48 const NOOFFICIAL = 2;
49
50 /**
51 * Control the fieldnames for form elements
52 *
53 * display => integer, one of the constants above.
54 */
55 var $_options = array('display' => MoodleQuickForm_tags::DEFAULTUI);
56
57 /**
58 * These complement separators, they are appended to the resultant HTML
59 * @access private
60 * @var array
61 */
62 var $_wrap = array('', '');
63
64 /**
65 * Constructor
66 *
67 * @param string $elementName Element name
68 * @param mixed $elementLabel Label(s) for an element
69 * @param array $options Options to control the element's display
70 * @param mixed $attributes Either a typical HTML attribute string or an associative array.
71 */
72 function MoodleQuickForm_tags($elementName = null, $elementLabel = null, $options = array(), $attributes = null) {
73 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
74 $this->_persistantFreeze = true;
75 $this->_appendName = true;
76 $this->_type = 'tags';
77 // set the options, do not bother setting bogus ones
78 if (is_array($options)) {
79 foreach ($options as $name => $value) {
80 if (isset($this->_options[$name])) {
81 if (is_array($value) && is_array($this->_options[$name])) {
82 $this->_options[$name] = @array_merge($this->_options[$name], $value);
83 } else {
84 $this->_options[$name] = $value;
85 }
86 }
87 }
88 }
89 }
90
91 function _createElements() {
92 global $CFG, $DB;
93 $this->_elements = array();
94
95 // Official tags.
96 if ($this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL) {
97 // If the user can manage official tags, give them a link to manage them.
98 $label = get_string('otags', 'tag');
99 if (has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM))) {
100 $label .= ' (' . link_to_popup_window($CFG->wwwroot .'/tag/manage.php',
101 'managetags', get_string('manageofficialtags', 'tag'), '', '', get_string('newwindow'), null, true) . ')';
102 }
103
104 // Get the list of official tags.
105 $noofficial = false;
106 $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
107 $officialtags = $DB->get_records_sql_menu("SELECT id, $namefield FROM {tag} WHERE tagtype='official' ORDER by $namefield ASC");
108 if (empty($officialtags)) {
109 $officialtags = array('' => get_string('none'));
110 $noofficial = true;
111 } else {
112 $officialtags = array_combine($officialtags, $officialtags);
113 }
114
115 // Create the element.
116 $size = min(5, count($officialtags));
117 $officialtagsselect = MoodleQuickForm::createElement('select', 'officialtags', $label, $officialtags, array('size' => $size));
118 $officialtagsselect->setMultiple(true);
119 if ($noofficial) {
120 $officialtagsselect->updateAttributes(array('disabled' => 'disabled'));
121 }
122
123 //
124 $this->_elements[] = $officialtagsselect;
125 }
126
127 // Other tags.
128 if ($this->_options['display'] != MoodleQuickForm_tags::ONLYOFFICIAL) {
129 $othertags = MoodleQuickForm::createElement('textarea', 'othertags', get_string('othertags', 'tag'), array('cols'=>'40', 'rows'=>'5'));
130 $this->_elements[] = $othertags;
131 }
132
133 // Paradoxically, the only way to get labels output is to ask for 'hidden'
134 // labels, and then override the .accesshide class in the CSS!
135 foreach ($this->_elements as $element){
136 if (method_exists($element, 'setHiddenLabel')){
137 $element->setHiddenLabel(true);
138 }
139 }
140 }
141
142 function toHtml() {
143 require_once('HTML/QuickForm/Renderer/Default.php');
144 $renderer =& new HTML_QuickForm_Renderer_Default();
145 $renderer->setElementTemplate('{element}');
146 parent::accept($renderer);
147 return $this->_wrap[0] . $renderer->toHtml() . $this->_wrap[1];
148 }
149
150 function exportValue(&$submitValues, $assoc = false) {
151 $valuearray = array();
152
153 // Get the data out of our child elements.
154 foreach ($this->_elements as $element){
155 $thisexport = $element->exportValue($submitValues[$this->getName()], true);
156 if ($thisexport != null){
157 $valuearray += $thisexport;
158 }
159 }
160
161 // Get any manually typed tags.
162 $tags = array();
163 if ($this->_options['display'] != MoodleQuickForm_tags::ONLYOFFICIAL &&
164 !empty($valuearray['othertags'])) {
165 $rawtags = explode(',', clean_param($valuearray['othertags'], PARAM_NOTAGS));
166 foreach ($rawtags as $tag) {
167 $tags[] = trim($tag);
168 }
169 }
170
171 // Add any official tags that were selected.
172 if ($this->_options['display'] != MoodleQuickForm_tags::NOOFFICIAL &&
173 !empty($valuearray['officialtags'])) {
174 $tags = array_unique(array_merge($tags, $valuearray['officialtags']));
175 }
176
177 return array($this->getName() => $tags);
178 }
179}
180?>