[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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. 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 17 18 /** 19 * Tag autocomplete field. 20 * 21 * Contains HTML class for editing tags, both standard and not. 22 * 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 28 global $CFG; 29 require_once($CFG->libdir . '/form/autocomplete.php'); 30 31 /** 32 * Form field type for editing tags. 33 * 34 * HTML class for editing tags, both standard and not. 35 * 36 * @package core_form 37 * @copyright 2009 Tim Hunt 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete { 41 /** 42 * Inidcates that the user should be the usual interface, with the official 43 * tags listed seprately, and a text box where they can type anything. 44 * @deprecated since 3.1 45 * @var int 46 */ 47 const DEFAULTUI = 'defaultui'; 48 49 /** 50 * Indicates that the user should only be allowed to select official tags. 51 * @deprecated since 3.1 52 * @var int 53 */ 54 const ONLYOFFICIAL = 'onlyofficial'; 55 56 /** 57 * Indicates that the user should just be given a text box to type in (they 58 * can still type official tags though. 59 * @deprecated since 3.1 60 * @var int 61 */ 62 const NOOFFICIAL = 'noofficial'; 63 64 /** 65 * @var boolean $showstandard Standard tags suggested? (if not, then don't show link to manage standard tags). 66 */ 67 protected $showstandard = false; 68 69 /** 70 * Options passed when creating an element. 71 * @var array 72 */ 73 protected $tagsoptions = array(); 74 75 /** 76 * Constructor 77 * 78 * @param string $elementName Element name 79 * @param mixed $elementLabel Label(s) for an element 80 * @param array $options Options to control the element's display 81 * @param mixed $attributes Either a typical HTML attribute string or an associative array. 82 */ 83 public function __construct($elementName = null, $elementLabel = null, $options = array(), $attributes = null) { 84 $validoptions = array(); 85 86 if (!empty($options)) { 87 // Only execute it when the element was created and $options has values set by user. 88 // In onQuickFormEvent() we make sure that $options is not empty even if developer left it empty. 89 $showstandard = core_tag_tag::BOTH_STANDARD_AND_NOT; 90 if (isset($options['showstandard'])) { 91 $showstandard = $options['showstandard']; 92 } else if (isset($options['display'])) { 93 debugging('Option "display" is deprecated, each tag area can be configured to show standard tags or not ' . 94 'by admin or manager. If it is necessary for the developer to override it, please use "showstandard" option', 95 DEBUG_DEVELOPER); 96 if ($options['display'] === self::NOOFFICIAL) { 97 $showstandard = core_tag_tag::HIDE_STANDARD; 98 } else if ($options['display'] === self::ONLYOFFICIAL) { 99 $showstandard = core_tag_tag::STANDARD_ONLY; 100 } 101 } else if (!empty($options['component']) && !empty($options['itemtype'])) { 102 $showstandard = core_tag_area::get_showstandard($options['component'], $options['itemtype']); 103 } 104 105 $this->tagsoptions = $options; 106 107 $this->showstandard = ($showstandard != core_tag_tag::HIDE_STANDARD); 108 if ($this->showstandard) { 109 $validoptions = $this->load_standard_tags(); 110 } 111 // Option 'tags' allows us to type new tags. 112 $attributes['tags'] = ($showstandard != core_tag_tag::STANDARD_ONLY); 113 $attributes['multiple'] = 'multiple'; 114 $attributes['placeholder'] = get_string('entertags', 'tag'); 115 $attributes['showsuggestions'] = $this->showstandard; 116 } 117 118 parent::__construct($elementName, $elementLabel, $validoptions, $attributes); 119 } 120 121 /** 122 * Called by HTML_QuickForm whenever form event is made on this element 123 * 124 * @param string $event Name of event 125 * @param mixed $arg event arguments 126 * @param object $caller calling object 127 * @return bool 128 */ 129 public function onQuickFormEvent($event, $arg, &$caller) { 130 if ($event === 'createElement') { 131 if (!is_array($arg[2])) { 132 $arg[2] = []; 133 } 134 $arg[2] += array('itemtype' => '', 'component' => ''); 135 } 136 return parent::onQuickFormEvent($event, $arg, $caller); 137 } 138 139 /** 140 * Checks if tagging is enabled for this itemtype 141 * 142 * @return boolean 143 */ 144 protected function is_tagging_enabled() { 145 if (!empty($this->tagsoptions['itemtype']) && !empty($this->tagsoptions['component'])) { 146 $enabled = core_tag_tag::is_enabled($this->tagsoptions['component'], $this->tagsoptions['itemtype']); 147 if ($enabled === false) { 148 return false; 149 } 150 } 151 // Backward compatibility with code developed before Moodle 3.0 where itemtype/component were not specified. 152 return true; 153 } 154 155 /** 156 * Old syntax of class constructor. Deprecated in PHP7. 157 * 158 * @deprecated since Moodle 3.1 159 */ 160 public function MoodleQuickForm_tags($elementName = null, $elementLabel = null, $options = array(), $attributes = null) { 161 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER); 162 self::__construct($elementName, $elementLabel, $options, $attributes); 163 } 164 165 /** 166 * Finds the tag collection to use for standard tag selector 167 * 168 * @return int 169 */ 170 protected function get_tag_collection() { 171 if (empty($this->tagsoptions['tagcollid']) && (empty($this->tagsoptions['itemtype']) || 172 empty($this->tagsoptions['component']))) { 173 debugging('You need to specify \'itemtype\' and \'component\' of the tagged ' 174 . 'area in the tags form element options', 175 DEBUG_DEVELOPER); 176 } 177 if (!empty($this->tagsoptions['tagcollid'])) { 178 return $this->tagsoptions['tagcollid']; 179 } 180 if ($this->tagsoptions['itemtype']) { 181 $this->tagsoptions['tagcollid'] = core_tag_area::get_collection($this->tagsoptions['component'], 182 $this->tagsoptions['itemtype']); 183 } else { 184 $this->tagsoptions['tagcollid'] = core_tag_collection::get_default(); 185 } 186 return $this->tagsoptions['tagcollid']; 187 } 188 189 /** 190 * Returns HTML for select form element. 191 * 192 * @return string 193 */ 194 function toHtml(){ 195 global $OUTPUT; 196 197 $managelink = ''; 198 if (has_capability('moodle/tag:manage', context_system::instance()) && $this->showstandard) { 199 $url = new moodle_url('/tag/manage.php', array('tc' => $this->get_tag_collection())); 200 $managelink = ' ' . $OUTPUT->action_link($url, get_string('managestandardtags', 'tag')); 201 } 202 203 return parent::toHTML() . $managelink; 204 } 205 206 /** 207 * Accepts a renderer 208 * 209 * @param HTML_QuickForm_Renderer $renderer An HTML_QuickForm_Renderer object 210 * @param bool $required Whether a group is required 211 * @param string $error An error message associated with a group 212 */ 213 public function accept(&$renderer, $required = false, $error = null) { 214 if ($this->is_tagging_enabled()) { 215 $renderer->renderElement($this, $required, $error); 216 } else { 217 $renderer->renderHidden($this); 218 } 219 } 220 221 /** 222 * Internal function to load standard tags 223 */ 224 protected function load_standard_tags() { 225 global $CFG, $DB; 226 if (!$this->is_tagging_enabled()) { 227 return array(); 228 } 229 $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname'; 230 $tags = $DB->get_records_menu('tag', 231 array('isstandard' => 1, 'tagcollid' => $this->get_tag_collection()), 232 $namefield, 'id,' . $namefield); 233 return array_combine($tags, $tags); 234 } 235 236 /** 237 * Returns a 'safe' element's value 238 * 239 * @param array $submitValues array of submitted values to search 240 * @param bool $assoc whether to return the value as associative array 241 * @return mixed 242 */ 243 public function exportValue(&$submitValues, $assoc = false) { 244 if (!$this->is_tagging_enabled()) { 245 return $assoc ? array($this->getName() => array()) : array(); 246 } 247 248 return parent::exportValue($submitValues, $assoc); 249 } 250 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |