weekly release 2.3dev
[moodle.git] / tag / lib.php
CommitLineData
6bfe7aac 1<?php
4d4dac81 2
000ebd5c
DM
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
c933a060 18/**
4a0daed7 19 * Moodle tag library
db32b054 20 *
986dca60 21 * Tag strings : you can use any character in tags, except the comma (which is
22 * the separator) and the '\' (backslash). Note that many spaces (or other
23 * blank characters) will get "compressed" into one. A tag string is always a
4a0daed7 24 * rawurlencode'd string. This is the same behavior as http://del.icio.us.
db32b054 25 *
986dca60 26 * A "record" is a php array (note that an object will work too) that contains
27 * the following variables :
db32b054 28 * - type: the table containing the record that we are tagging (eg: for a
29 * blog, this is table 'post', and for a user it is 'user')
986dca60 30 * - id: the id of the record
d4be9d6d 31 *
986dca60 32 * TODO: turn this into a full-fledged categorization system. This could start
33 * by modifying (removing, probably) the 'tag type' to use another table
34 * describing the relationship between tags (parents, sibling, etc.), which
db32b054 35 * could then be merged with the 'course categorization' system...
c933a060 36 *
986dca60 37 * BASIC INSTRUCTIONS :
38 * - to "tag a blog post" (for example):
4a0daed7 39 * tag_set('post', $blog_post->id, $array_of_tags);
d4be9d6d 40 *
db32b054 41 * - to "remove all the tags on a blog post":
4a0daed7 42 * tag_set('post', $blog_post->id, array());
43 *
44 * Tag set will create tags that need to be created.
d4be9d6d 45 *
000ebd5c 46 * @package core
4a0daed7 47 * @subpackage tag
48 * @see http://www.php.net/manual/en/function.urlencode.php
000ebd5c
DM
49 * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
50 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
c933a060 51 */
52
db32b054 53define('TAG_RETURN_ARRAY', 0);
54define('TAG_RETURN_OBJECT', 1);
55define('TAG_RETURN_TEXT', 2);
56define('TAG_RETURN_HTML', 3);
c933a060 57
db32b054 58define('TAG_CASE_LOWER', 0);
59define('TAG_CASE_ORIGINAL', 1);
d4be9d6d 60
537aa65c 61define('TAG_RELATED_ALL', 0);
62define('TAG_RELATED_MANUAL', 1);
63define('TAG_RELATED_CORRELATED', 2);
64
db32b054 65///////////////////////////////////////////////////////
66/////////////////// PUBLIC TAG API ////////////////////
c933a060 67
8479c2e0 68/// Functions for settings tags //////////////////////
69
c933a060 70/**
8479c2e0 71 * Set the tags assigned to a record. This overwrites the current tags.
986dca60 72 *
73 * This function is meant to be fed the string coming up from the user
8479c2e0 74 * interface, which contains all tags assigned to a record.
75 *
986dca60 76 * @param string $record_type the type of record to tag ('post' for blogs,
8479c2e0 77 * 'user' for users, 'tag' for tags, etc.
78 * @param int $record_id the id of the record to tag
986dca60 79 * @param array $tags the array of tags to set on the record. If
8479c2e0 80 * given an empty array, all tags will be removed.
986dca60 81 * @return void
c933a060 82 */
b91de8a5 83function tag_set($record_type, $record_id, $tags) {
3d436d8a 84
28f64040 85 static $in_recursion_semaphore = false; // this is to prevent loops when tagging a tag
86 if ( $record_type == 'tag' && !$in_recursion_semaphore) {
87 $current_tagged_tag_name = tag_get_name($record_id);
88 }
8479c2e0 89
90 $tags_ids = tag_get_id($tags, TAG_RETURN_ARRAY); // force an array, even if we only have one tag.
91 $cleaned_tags = tag_normalize($tags);
92 //echo 'tags-in-tag_set'; var_dump($tags); var_dump($tags_ids); var_dump($cleaned_tags);
93
28f64040 94 $current_ids = tag_get_tags_ids($record_type, $record_id);
986dca60 95 //var_dump($current_ids);
8479c2e0 96
97 // for data coherence reasons, it's better to remove deleted tags
98 // before adding new data: ordering could be duplicated.
99 foreach($current_ids as $current_id) {
100 if (!in_array($current_id, $tags_ids)) {
28f64040 101 tag_delete_instance($record_type, $record_id, $current_id);
102 if ( $record_type == 'tag' && !$in_recursion_semaphore) {
986dca60 103 // if we are removing a tag-on-a-tag (manually related tag),
28f64040 104 // we need to remove the opposite relationship as well.
105 tag_delete_instance('tag', $current_id, $record_id);
106 }
8479c2e0 107 }
c933a060 108 }
3d436d8a 109
0f097239 110 if (empty($tags)) {
111 return true;
112 }
113
8479c2e0 114 foreach($tags as $ordering => $tag) {
115 $tag = trim($tag);
116 if (!$tag) {
db32b054 117 continue;
118 }
8479c2e0 119
120 $clean_tag = $cleaned_tags[$tag];
121 $tag_current_id = $tags_ids[$clean_tag];
986dca60 122
8479c2e0 123 if ( is_null($tag_current_id) ) {
124 // create new tags
125 //echo "call to add tag $tag\n";
126 $new_tag = tag_add($tag);
28f64040 127 $tag_current_id = $new_tag[$clean_tag];
128 }
129
b91de8a5 130 tag_assign($record_type, $record_id, $tag_current_id, $ordering);
28f64040 131
132 // if we are tagging a tag (adding a manually-assigned related tag), we
133 // need to create the opposite relationship as well.
134 if ( $record_type == 'tag' && !$in_recursion_semaphore) {
135 $in_recursion_semaphore = true;
136 tag_set_add('tag', $tag_current_id, $current_tagged_tag_name);
137 $in_recursion_semaphore = false;
db32b054 138 }
139 }
8479c2e0 140}
3d436d8a 141
8479c2e0 142/**
143 * Adds a tag to a record, without overwriting the current tags.
986dca60 144 *
145 * @param string $record_type the type of record to tag ('post' for blogs,
8479c2e0 146 * 'user' for users, etc.
147 * @param int $record_id the id of the record to tag
148 * @param string $tag the tag to add
149 * @return void
150 */
b91de8a5 151function tag_set_add($record_type, $record_id, $tag) {
8479c2e0 152
153 $new_tags = array();
b91de8a5 154 foreach( tag_get_tags($record_type, $record_id) as $current_tag ) {
8479c2e0 155 $new_tags[] = $current_tag->rawname;
156 }
157 $new_tags[] = $tag;
986dca60 158
b91de8a5 159 return tag_set($record_type, $record_id, $new_tags);
c933a060 160}
161
162/**
8479c2e0 163 * Removes a tag from a record, without overwriting other current tags.
986dca60 164 *
165 * @param string $record_type the type of record to tag ('post' for blogs,
8479c2e0 166 * 'user' for users, etc.
167 * @param int $record_id the id of the record to tag
168 * @param string $tag the tag to delete
169 * @return void
c933a060 170 */
8479c2e0 171function tag_set_delete($record_type, $record_id, $tag) {
70e9b59d 172
8479c2e0 173 $new_tags = array();
174 foreach( tag_get_tags($record_type, $record_id) as $current_tag ) {
175 if ($current_tag->name != $tag) { // Keep all tags but the one specified
176 $new_tags[] = $current_tag->name;
db32b054 177 }
c933a060 178 }
28f64040 179
8479c2e0 180 return tag_set($record_type, $record_id, $new_tags);
181}
182
183/**
184 * Set the type of a tag. At this time (version 1.9) the possible values
185 * are 'default' or 'official'. Official tags will be displayed separately "at
186 * tagging time" (while selecting the tags to apply to a record).
187 *
188 * @param string $tagid tagid to modify
189 * @param string $type either 'default' or 'official'
190 * @return true on success, false otherwise
191 */
192function tag_type_set($tagid, $type) {
f7ed2687 193 global $DB;
194
195 if ($tag = $DB->get_record('tag', array('id'=>$tagid), 'id')) {
196 $tag->tagtype = $type;
8479c2e0 197 $tag->timemodified = time();
f7ed2687 198 return $DB->update_record('tag', $tag);
8479c2e0 199 }
200 return false;
c933a060 201}
202
8479c2e0 203
986dca60 204/**
b50d2245 205 * Set the description of a tag
986dca60 206 *
b50d2245 207 * @param int $tagid the id of the tag
208 * @param string $description the description
209 * @param int $descriptionformat the moodle text format of the description
986dca60 210 * @return true on success, false otherwise
b50d2245 211 */
212function tag_description_set($tagid, $description, $descriptionformat) {
f7ed2687 213 global $DB;
214
215 if ($tag = $DB->get_record('tag', array('id'=>$tagid),'id')) {
216 $tag->description = $description;
217 $tag->descriptionformat = $descriptionformat;
b50d2245 218 $tag->timemodified = time();
f7ed2687 219 return $DB->update_record('tag', $tag);
b50d2245 220 }
221 return false;
222}
223
3d436d8a 224
6361e02a 225
db32b054 226
c933a060 227
c933a060 228
8479c2e0 229/// Functions for getting information about tags //////
2f4b82f4 230
231/**
8479c2e0 232 * Simple function to just return a single tag object when you know the name or something
2f4b82f4 233 *
234 * @param string $field which field do we use to identify the tag: id, name or rawname
235 * @param string $value the required value of the aforementioned field
236 * @param string $returnfields which fields do we want returned?
237 * @return tag object
238 *
239 **/
240function tag_get($field, $value, $returnfields='id, name, rawname') {
f7ed2687 241 global $DB;
242
8479c2e0 243 if ($field == 'name') {
f7ed2687 244 $value = moodle_strtolower($value); // To cope with input that might just be wrong case
8479c2e0 245 }
f7ed2687 246 return $DB->get_record('tag', array($field=>$value), $returnfields);
2f4b82f4 247}
248
249
c933a060 250/**
986dca60 251 * Get the array of db record of tags associated to a record (instances). Use
db32b054 252 * tag_get_tags_csv to get the same information in a comma-separated string.
638cf615 253 * This should really be called tag_get_tag_instances()
c933a060 254 *
986dca60 255 * @param string $record_type the record type for which we want to get the tags
256 * @param int $record_id the record id for which we want to get the tags
db32b054 257 * @param string $type the tag type (either 'default' or 'official'). By default,
258 * all tags are returned.
38fb8190 259 * @param int $userid optional only required for course tagging
db32b054 260 * @return array the array of tags
c933a060 261 */
38fb8190 262function tag_get_tags($record_type, $record_id, $type=null, $userid=0) {
f7ed2687 263 global $CFG, $DB;
264
265 $params = array();
3d436d8a 266
db32b054 267 if ($type) {
cda19b7a 268 $sql_type = "AND tg.tagtype = :type";
f7ed2687 269 $params['type'] = $type;
270 } else {
cda19b7a 271 $sql_type = '';
db32b054 272 }
537aa65c 273
38fb8190 274 $u = null;
275 if ($userid) {
276 $u = "AND ti.tiuserid = :userid ";
277 $params['userid'] = $userid;
278 }
279
f7ed2687 280 $sql = "SELECT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering
638cf615
AD
281 FROM {tag_instance} ti
282 JOIN {tag} tg ON tg.id = ti.tagid
38fb8190 283 WHERE ti.itemtype = :recordtype AND ti.itemid = :recordid $u $sql_type
f7ed2687 284 ORDER BY ti.ordering ASC";
285 $params['recordtype'] = $record_type;
286 $params['recordid'] = $record_id;
287
73c32e86 288 // if the fields in this query are changed, you need to do the same changes in tag_get_correlated_tags
f7ed2687 289 return $DB->get_records_sql($sql, $params);
986dca60 290 // This version of the query, reversing the ON clause, "correctly" returns
291 // a row with NULL values for instances that are still in the DB even though
292 // the tag has been deleted. This shouldn't happen, but if it did, using
db32b054 293 // this query could help "clean it up". This causes bugs at this time.
f7ed2687 294 //$tags = $DB->get_records_sql("SELECT ti.tagid, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering ".
5183f0cc 295 // "FROM {tag_instance} ti LEFT JOIN {tag} tg ON ti.tagid = tg.id ".
537aa65c 296 // "WHERE ti.itemtype = '{$record_type}' AND ti.itemid = '{$record_id}' {$type} ".
2f9ba531 297 // "ORDER BY ti.ordering ASC");
db32b054 298}
299
300/**
301 * Get the array of tags display names, indexed by id.
986dca60 302 *
537aa65c 303 * @param string $record_type the record type for which we want to get the tags
304 * @param int $record_id the record id for which we want to get the tags
db32b054 305 * @param string $type the tag type (either 'default' or 'official'). By default,
306 * all tags are returned.
307 * @return array the array of tags (with the value returned by tag_display_name), indexed by id
308 */
537aa65c 309function tag_get_tags_array($record_type, $record_id, $type=null) {
db32b054 310 $tags = array();
537aa65c 311 foreach(tag_get_tags($record_type, $record_id, $type) as $tag) {
db32b054 312 $tags[$tag->id] = tag_display_name($tag);
4f81c51d 313 }
db32b054 314 return $tags;
c933a060 315}
316
317/**
db32b054 318 * Get a comma-separated string of tags associated to a record. Use tag_get_tags
319 * to get the same information in an array.
c933a060 320 *
537aa65c 321 * @param string $record_type the record type for which we want to get the tags
322 * @param int $record_id the record id for which we want to get the tags
db32b054 323 * @param int $html either TAG_RETURN_HTML or TAG_RETURN_TEXT, depending
324 * on the type of output desired
325 * @param string $type either 'official' or 'default', if null, all tags are
326 * returned
327 * @return string the comma-separated list of tags.
c933a060 328 */
537aa65c 329function tag_get_tags_csv($record_type, $record_id, $html=TAG_RETURN_HTML, $type=null) {
db32b054 330 global $CFG;
c933a060 331
332 $tags_names = array();
8479c2e0 333 foreach(tag_get_tags($record_type, $record_id, $type) as $tag) {
db32b054 334 if ($html == TAG_RETURN_TEXT) {
e7134583 335 $tags_names[] = tag_display_name($tag, TAG_RETURN_TEXT);
db32b054 336 } else { // TAG_RETURN_HTML
337 $tags_names[] = '<a href="'. $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name) .'">'. tag_display_name($tag) .'</a>';
338 }
c933a060 339 }
db32b054 340 return implode(', ', $tags_names);
c933a060 341}
342
4f81c51d 343/**
db32b054 344 * Get an array of tag ids associated to a record.
d4be9d6d 345 *
28f64040 346 * @param string $record_type the record type for which we want to get the tags
537aa65c 347 * @param int $record_id the record id for which we want to get the tags
db32b054 348 * @return array of tag ids, indexed and sorted by 'ordering'
4f81c51d 349 */
537aa65c 350function tag_get_tags_ids($record_type, $record_id) {
986dca60 351
db32b054 352 $tag_ids = array();
8479c2e0 353 foreach (tag_get_tags($record_type, $record_id) as $tag) {
28f64040 354 if ( array_key_exists($tag->ordering, $tag_ids) ) {
355 // until we can add a unique constraint, in table tag_instance,
356 // on (itemtype, itemid, ordering), this is needed to prevent a bug
357 // TODO : modify database in 2.0
358 $tag->ordering++;
359 }
db32b054 360 $tag_ids[$tag->ordering] = $tag->id;
361 }
362 ksort($tag_ids);
363 return $tag_ids;
364}
3d436d8a 365
986dca60 366/**
db32b054 367 * Returns the database ID of a set of tags.
986dca60 368 *
db32b054 369 * @param mixed $tags one tag, or array of tags, to look for.
986dca60 370 * @param bool $return_value specify the type of the returned value. Either
371 * TAG_RETURN_OBJECT, or TAG_RETURN_ARRAY (default). If TAG_RETURN_ARRAY
372 * is specified, an array will be returned even if only one tag was
dee71a16 373 * passed in $tags.
986dca60 374 * @return mixed tag-indexed array of ids (or objects, if second parameter is
375 * TAG_RETURN_OBJECT), or only an int, if only one tag is given *and* the
dee71a16 376 * second parameter is null. No value for a key means the tag wasn't found.
db32b054 377 */
378function tag_get_id($tags, $return_value=null) {
f7ed2687 379 global $CFG, $DB;
380
db32b054 381 static $tag_id_cache = array();
3d436d8a 382
db32b054 383 $return_an_int = false;
384 if (!is_array($tags)) {
385 if(is_null($return_value) || $return_value == TAG_RETURN_OBJECT) {
986dca60 386 $return_an_int = true;
db32b054 387 }
388 $tags = array($tags);
389 }
986dca60 390
db32b054 391 $result = array();
986dca60 392
db32b054 393 //TODO: test this and see if it helps performance without breaking anything
394 //foreach($tags as $key => $tag) {
395 // $clean_tag = moodle_strtolower($tag);
396 // if ( array_key_exists($clean_tag), $tag_id_cache) ) {
397 // $result[$clean_tag] = $tag_id_cache[$clean_tag];
398 // $tags[$key] = ''; // prevent further processing for this one.
399 // }
400 //}
401
402 $tags = array_values(tag_normalize($tags));
403 foreach($tags as $key => $tag) {
986dca60 404 $tags[$key] = moodle_strtolower($tag);
db32b054 405 $result[moodle_strtolower($tag)] = null; // key must exists : no value for a key means the tag wasn't found.
406 }
db32b054 407
f7ed2687 408 if (empty($tags)) {
409 return array();
410 }
411
412 list($tag_string, $params) = $DB->get_in_or_equal($tags);
413
cd5fdfd8
EL
414 $rs = $DB->get_recordset_sql("SELECT * FROM {tag} WHERE name $tag_string ORDER BY name", $params);
415 foreach ($rs as $record) {
416 if ($return_value == TAG_RETURN_OBJECT) {
417 $result[$record->name] = $record;
418 } else { // TAG_RETURN_ARRAY
419 $result[$record->name] = $record->id;
db32b054 420 }
4f81c51d 421 }
cd5fdfd8 422 $rs->close();
3d436d8a 423
db32b054 424 if ($return_an_int) {
425 return array_pop($result);
426 }
3d436d8a 427
db32b054 428 return $result;
c933a060 429}
430
c933a060 431
432/**
db32b054 433 * Returns tags related to a tag
d4be9d6d 434 *
db32b054 435 * Related tags of a tag come from two sources:
436 * - manually added related tags, which are tag_instance entries for that tag
437 * - correlated tags, which are a calculated
d4be9d6d 438 *
986dca60 439 * @param string $tag_name_or_id is a single **normalized** tag name or the id
537aa65c 440 * of a tag
986dca60 441 * @param int $type the function will return either manually
442 * (TAG_RELATED_MANUAL) related tags or correlated (TAG_RELATED_CORRELATED)
537aa65c 443 * tags. Default is TAG_RELATED_ALL, which returns everything.
986dca60 444 * @param int $limitnum return a subset comprising this many records (optional,
537aa65c 445 * default is 10)
db32b054 446 * @return array an array of tag objects
c933a060 447 */
537aa65c 448function tag_get_related_tags($tagid, $type=TAG_RELATED_ALL, $limitnum=10) {
c933a060 449
537aa65c 450 $related_tags = array();
451
452 if ( $type == TAG_RELATED_ALL || $type == TAG_RELATED_MANUAL) {
453 //gets the manually added related tags
454 $related_tags = tag_get_tags('tag', $tagid);
c933a060 455 }
456
537aa65c 457 if ( $type == TAG_RELATED_ALL || $type == TAG_RELATED_CORRELATED ) {
458 //gets the correlated tags
459 $automatic_related_tags = tag_get_correlated($tagid, $limitnum);
460 if (is_array($automatic_related_tags)) {
461 $related_tags = array_merge($related_tags, $automatic_related_tags);
462 }
db32b054 463 }
3d436d8a 464
db32b054 465 return array_slice(object_array_unique($related_tags), 0 , $limitnum);
c933a060 466}
467
986dca60 468/**
db32b054 469 * Get a comma-separated list of tags related to another tag.
d4be9d6d 470 *
db32b054 471 * @param array $related_tags the array returned by tag_get_related_tags
472 * @param int $html either TAG_RETURN_HTML (default) or TAG_RETURN_TEXT : return html links, or just text.
473 * @return string comma-separated list
c933a060 474 */
db32b054 475function tag_get_related_tags_csv($related_tags, $html=TAG_RETURN_HTML) {
476 global $CFG;
c933a060 477
db32b054 478 $tags_names = array();
479 foreach($related_tags as $tag) {
480 if ( $html == TAG_RETURN_TEXT) {
e7134583 481 $tags_names[] = tag_display_name($tag, TAG_RETURN_TEXT);
db32b054 482 } else {
483 // TAG_RETURN_HTML
9fccb889 484 $tags_names[] = '<a href="'. $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name) .'">'. tag_display_name($tag) .'</a>';
c933a060 485 }
c933a060 486 }
b50d2245 487
db32b054 488 return implode(', ', $tags_names);
c933a060 489}
490
3a597ace 491/**
db32b054 492 * Change the "value" of a tag, and update the associated 'name'.
3a597ace 493 *
db32b054 494 * @param int $tagid the id of the tag to modify
73c32e86 495 * @param string $newtag the new rawname
db32b054 496 * @return bool true on success, false otherwise
3a597ace 497 */
73c32e86 498function tag_rename($tagid, $newrawname) {
f7ed2687 499 global $DB;
3a597ace 500
73c32e86 501 if (! $newrawname_clean = array_shift(tag_normalize($newrawname, TAG_CASE_ORIGINAL)) ) {
db32b054 502 return false;
503 }
3a597ace 504
2f4b82f4 505 if (! $newname_clean = moodle_strtolower($newrawname_clean)) {
506 return false;
507 }
508
509 // Prevent the rename if a tag with that name already exists
510 if ($existing = tag_get('name', $newname_clean, 'id, name, rawname')) {
511 if ($existing->id != $tagid) { // Another tag already exists with this name
986dca60 512 return false;
2f4b82f4 513 }
db32b054 514 }
3a597ace 515
2f4b82f4 516 if ($tag = tag_get('id', $tagid, 'id, name, rawname')) {
986dca60 517 $tag->rawname = $newrawname_clean;
518 $tag->name = $newname_clean;
db32b054 519 $tag->timemodified = time();
f7ed2687 520 return $DB->update_record('tag', $tag);
db32b054 521 }
522 return false;
3a597ace 523}
524
8479c2e0 525
c933a060 526/**
8479c2e0 527 * Delete one or more tag, and all their instances if there are any left.
986dca60 528 *
8479c2e0 529 * @param mixed $tagids one tagid (int), or one array of tagids to delete
986dca60 530 * @return bool true on success, false otherwise
c933a060 531 */
8479c2e0 532function tag_delete($tagids) {
f7ed2687 533 global $DB;
3d436d8a 534
8479c2e0 535 if (!is_array($tagids)) {
536 $tagids = array($tagids);
db32b054 537 }
3d436d8a 538
8479c2e0 539 $success = true;
8bdc9cac 540 $context = get_context_instance(CONTEXT_SYSTEM);
e2a64d83 541 foreach ($tagids as $tagid) {
8479c2e0 542 if (is_null($tagid)) { // can happen if tag doesn't exists
db32b054 543 continue;
26d8649f 544 }
986dca60 545 // only delete the main entry if there were no problems deleting all the
546 // instances - that (and the fact we won't often delete lots of tags)
8618fd2a 547 // is the reason for not using $DB->delete_records_select()
e2a64d83 548 if ($DB->delete_records('tag_instance', array('tagid'=>$tagid)) && $DB->delete_records('tag_correlation', array('tagid' => $tagid))) {
f7ed2687 549 $success &= (bool) $DB->delete_records('tag', array('id'=>$tagid));
8bdc9cac
SH
550 // Delete all files associated with this tag
551 $fs = get_file_storage();
64f93798 552 $files = $fs->get_area_files($context->id, 'tag', 'description', $tagid);
8bdc9cac
SH
553 foreach ($files as $file) {
554 $file->delete();
555 }
1acd661d 556 }
c933a060 557 }
8479c2e0 558
559 return $success;
db32b054 560}
c933a060 561
db32b054 562/**
8479c2e0 563 * Delete one instance of a tag. If the last instance was deleted, it will
a72529c7 564 * also delete the tag, unless its type is 'official'.
8479c2e0 565 *
28f64040 566 * @param string $record_type the type of the record for which to remove the instance
567 * @param int $record_id the id of the record for which to remove the instance
8479c2e0 568 * @param int $tagid the tagid that needs to be removed
569 * @return bool true on success, false otherwise
db32b054 570 */
28f64040 571function tag_delete_instance($record_type, $record_id, $tagid) {
f7ed2687 572 global $CFG, $DB;
573
574 if ($DB->delete_records('tag_instance', array('tagid'=>$tagid, 'itemtype'=>$record_type, 'itemid'=>$record_id))) {
a72529c7 575 if (!$DB->record_exists_sql("SELECT * ".
576 "FROM {tag} tg ".
577 "WHERE tg.id = ? AND ( tg.tagtype = 'official' OR ".
b91de8a5 578 "EXISTS (SELECT 1
579 FROM {tag_instance} ti
580 WHERE ti.tagid = ?) )",
581 array($tagid, $tagid))) {
8479c2e0 582 return tag_delete($tagid);
583 }
584 } else {
585 return false;
db32b054 586 }
4a0daed7 587
588 return true;
db32b054 589}
c933a060 590
8479c2e0 591
3627b0e3 592/**
8479c2e0 593 * Function that returns the name that should be displayed for a specific tag
594 *
595 * @param object $tag_object a line out of tag table, as returned by the adobd functions
e7134583 596 * @param int $html TAG_RETURN_HTML (default) will return htmlspecialchars encoded string, TAG_RETURN_TEXT will not encode.
8479c2e0 597 * @return string
3627b0e3 598 */
e7134583 599function tag_display_name($tagobject, $html=TAG_RETURN_HTML) {
3627b0e3 600
8479c2e0 601 global $CFG;
602
34b93e39 603 if (!isset($tagobject->name)) {
8479c2e0 604 return '';
605 }
606
607 if (empty($CFG->keeptagnamecase)) {
608 //this is the normalized tag name
609 $textlib = textlib_get_instance();
e7134583 610 $tagname = $textlib->strtotitle($tagobject->name);
8479c2e0 611 } else {
612 //original casing of the tag name
e7134583 613 $tagname = $tagobject->rawname;
3627b0e3 614 }
e7134583 615
34b93e39
PS
616 // clean up a bit just in case the rules change again
617 $tagname = clean_param($tagname, PARAM_TAG);
618
e7134583 619 if ($html == TAG_RETURN_TEXT) {
620 return $tagname;
621 } else { // TAG_RETURN_HTML
622 return htmlspecialchars($tagname);
b91de8a5 623 }
3627b0e3 624}
625
db32b054 626/**
8479c2e0 627 * Find all records tagged with a tag of a given type ('post', 'user', etc.)
db32b054 628 *
8479c2e0 629 * @param string $tag tag to look for
630 * @param string $type type to restrict search to. If null, every matching
631 * record will be returned
632 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
633 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
634 * @return array of matching objects, indexed by record id, from the table containing the type requested
db32b054 635 */
8479c2e0 636function tag_find_records($tag, $type, $limitfrom='', $limitnum='') {
f7ed2687 637 global $CFG, $DB;
8479c2e0 638
639 if (!$tag || !$type) {
640 return array();
db32b054 641 }
8479c2e0 642
643 $tagid = tag_get_id($tag);
644
f7ed2687 645 $query = "SELECT it.*
646 FROM {".$type."} it INNER JOIN {tag_instance} tt ON it.id = tt.itemid
647 WHERE tt.itemtype = ? AND tt.tagid = ?";
648 $params = array($type, $tagid);
986dca60 649
650 return $DB->get_records_sql($query, $params, $limitfrom, $limitnum);
c933a060 651}
652
8479c2e0 653
654
655
db32b054 656///////////////////////////////////////////////////////
657/////////////////// PRIVATE TAG API ///////////////////
c933a060 658
659/**
986dca60 660 * Adds one or more tag in the database. This function should not be called
db32b054 661 * directly : you should use tag_set.
d4be9d6d 662 *
db32b054 663 * @param mixed $tags one tag, or an array of tags, to be created
986dca60 664 * @param string $type type of tag to be created ("default" is the default
db32b054 665 * value and "official" is the only other supported value at this time). An
666 * official tag is kept even if there are no records tagged with it.
986dca60 667 * @return an array of tags ids, indexed by their lowercase normalized names.
db32b054 668 * Any boolean false in the array indicates an error while adding the tag.
c933a060 669 */
db32b054 670function tag_add($tags, $type="default") {
f7ed2687 671 global $USER, $DB;
c933a060 672
db32b054 673 if (!is_array($tags)) {
674 $tags = array($tags);
c933a060 675 }
3d436d8a 676
db32b054 677 $tag_object = new StdClass;
f7ed2687 678 $tag_object->tagtype = $type;
679 $tag_object->userid = $USER->id;
680 $tag_object->timemodified = time();
c933a060 681
db32b054 682 $clean_tags = tag_normalize($tags, TAG_CASE_ORIGINAL);
c933a060 683
db32b054 684 $tags_ids = array();
685 foreach($clean_tags as $tag) {
686 $tag = trim($tag);
687 if (!$tag) {
688 $tags_ids[$tag] = false;
689 } else {
986dca60 690 // note that the difference between rawname and name is only
691 // capitalization : the rawname is NOT the same at the rawtag.
692 $tag_object->rawname = $tag;
f7ed2687 693 $tag_name_lc = moodle_strtolower($tag);
694 $tag_object->name = $tag_name_lc;
db32b054 695 //var_dump($tag_object);
f7ed2687 696 $tags_ids[$tag_name_lc] = $DB->insert_record('tag', $tag_object);
db32b054 697 }
698 }
c933a060 699
db32b054 700 return $tags_ids;
c933a060 701}
702
703/**
db32b054 704 * Assigns a tag to a record: if the record already exists, the time and
705 * ordering will be updated.
986dca60 706 *
28f64040 707 * @param string $record_type the type of the record that will be tagged
708 * @param int $record_id the id of the record that will be tagged
986dca60 709 * @param string $tagid the tag id to set on the record.
db32b054 710 * @param int $ordering the order of the instance for this record
38fb8190 711 * @param int $userid optional only required for course tagging
db32b054 712 * @return bool true on success, false otherwise
c933a060 713 */
38fb8190 714function tag_assign($record_type, $record_id, $tagid, $ordering, $userid = 0) {
f7ed2687 715 global $DB;
c933a060 716
38fb8190 717 if ( $tag_instance_object = $DB->get_record('tag_instance', array('tagid'=>$tagid, 'itemtype'=>$record_type, 'itemid'=>$record_id, 'tiuserid'=>$userid), 'id')) {
f7ed2687 718 $tag_instance_object->ordering = $ordering;
db32b054 719 $tag_instance_object->timemodified = time();
f7ed2687 720 return $DB->update_record('tag_instance', $tag_instance_object);
986dca60 721 } else {
db32b054 722 $tag_instance_object = new StdClass;
f7ed2687 723 $tag_instance_object->tagid = $tagid;
724 $tag_instance_object->itemid = $record_id;
725 $tag_instance_object->itemtype = $record_type;
726 $tag_instance_object->ordering = $ordering;
db32b054 727 $tag_instance_object->timemodified = time();
38fb8190 728 $tag_instance_object->tiuserid = $userid;
f7ed2687 729 return $DB->insert_record('tag_instance', $tag_instance_object);
c933a060 730 }
c933a060 731}
732
733/**
db32b054 734 * Function that returns tags that start with some text, for use by the autocomplete feature
d4be9d6d 735 *
db32b054 736 * @param string $text string that the tag names will be matched against
c933a060 737 * @return mixed an array of objects, or false if no records were found or an error occured.
738 */
db32b054 739function tag_autocomplete($text) {
f7ed2687 740 global $DB;
741 return $DB->get_records_sql("SELECT tg.id, tg.name, tg.rawname
742 FROM {tag} tg
743 WHERE tg.name LIKE ?", array(moodle_strtolower($text)."%"));
c933a060 744}
745
986dca60 746/**
9545cdba 747 * Clean up the tag tables, making sure all tagged object still exists.
748 *
749 * This should normally not be necessary, but in case related tags are not deleted
750 * when the tagged record is removed, this should be done once in a while, perhaps on
986dca60 751 * an occasional cron run. On a site with lots of tags, this could become an expensive
9545cdba 752 * function to call: don't run at peak time.
753 */
754function tag_cleanup() {
f7ed2687 755 global $DB;
9545cdba 756
f7ed2687 757 $instances = $DB->get_recordset('tag_instance');
9545cdba 758
084426c5 759 // cleanup tag instances
f7ed2687 760 foreach ($instances as $instance) {
9545cdba 761 $delete = false;
986dca60 762
f7ed2687 763 if (!$DB->record_exists('tag', array('id'=>$instance->tagid))) {
9545cdba 764 // if the tag has been removed, instance should be deleted.
765 $delete = true;
766 } else {
767 switch ($instance->itemtype) {
768 case 'user': // users are marked as deleted, but not actually deleted
f7ed2687 769 if ($DB->record_exists('user', array('id'=>$instance->itemid, 'deleted'=>1))) {
9545cdba 770 $delete = true;
771 }
772 break;
773 default: // anything else, if the instance is not there, delete.
f7ed2687 774 if (!$DB->record_exists($instance->itemtype, array('id'=>$instance->itemid))) {
9545cdba 775 $delete = true;
776 }
777 break;
778 }
779 }
780 if ($delete) {
781 tag_delete_instance($instance->itemtype, $instance->itemid, $instance->tagid);
782 //debugging('deleting tag_instance #'. $instance->id .', linked to tag id #'. $instance->tagid, DEBUG_DEVELOPER);
783 }
784 }
f7ed2687 785 $instances->close();
9545cdba 786
787 // TODO: this will only clean tags of type 'default'. This is good as
788 // it won't delete 'official' tags, but the day we get more than two
789 // types, we need to fix this.
f7ed2687 790 $unused_tags = $DB->get_recordset_sql("SELECT tg.id
791 FROM {tag} tg
792 WHERE tg.tagtype = 'default'
793 AND NOT EXISTS (
794 SELECT 'x'
795 FROM {tag_instance} ti
796 WHERE ti.tagid = tg.id
797 )");
9545cdba 798
084426c5 799 // cleanup tags
f7ed2687 800 foreach ($unused_tags as $unused_tag) {
084426c5 801 tag_delete($unused_tag->id);
802 //debugging('deleting unused tag #'. $unused_tag->id, DEBUG_DEVELOPER);
9545cdba 803 }
f7ed2687 804 $unused_tags->close();
9545cdba 805}
806
c933a060 807/**
db32b054 808 * Calculates and stores the correlated tags of all tags.
809 * The correlations are stored in the 'tag_correlation' table.
810 *
811 * Two tags are correlated if they appear together a lot.
812 * Ex.: Users tagged with "computers" will probably also be tagged with "algorithms".
d4be9d6d 813 *
db32b054 814 * The rationale for the 'tag_correlation' table is performance.
815 * It works as a cache for a potentially heavy load query done at the 'tag_instance' table.
816 * So, the 'tag_correlation' table stores redundant information derived from the 'tag_instance' table.
d4be9d6d 817 *
78defd5c
SH
818 * @global moodle_database $DB
819 * @param int $mincorrelation Only tags with more than $mincorrelation correlations will
820 * be identified.
821 * @return void
c933a060 822 */
78defd5c 823function tag_compute_correlations($mincorrelation = 2) {
f7ed2687 824 global $DB;
3d436d8a 825
78defd5c
SH
826 // This mighty one line query fetches a row from the database for every
827 // individual tag correlation. We then need to process the rows collecting
828 // the correlations for each tag id.
829 // The fields used by this query are as follows:
830 // tagid : This is the tag id, there should be at least $mincorrelation
831 // rows for each tag id.
832 // correlation : This is the tag id that correlates to the above tagid field.
833 // correlationid : This is the id of the row in the tag_correlation table that
834 // relates to the tagid field and will be NULL if there are no
835 // existing correlations
2650c547 836 $sql = 'SELECT pairs.tagid, pairs.correlation, pairs.ocurrences, co.id AS correlationid
1c2cfff0
EL
837 FROM (
838 SELECT ta.tagid, tb.tagid AS correlation, COUNT(*) AS ocurrences
839 FROM {tag_instance} ta
840 JOIN {tag_instance} tb ON (ta.itemtype = tb.itemtype AND ta.itemid = tb.itemid AND ta.tagid <> tb.tagid)
841 GROUP BY ta.tagid, tb.tagid
842 HAVING COUNT(*) > :mincorrelation
843 ) pairs
844 LEFT JOIN {tag_correlation} co ON co.tagid = pairs.tagid
845 ORDER BY pairs.tagid ASC, pairs.ocurrences DESC, pairs.correlation ASC';
78defd5c
SH
846 $rs = $DB->get_recordset_sql($sql, array('mincorrelation' => $mincorrelation));
847
848 // Set up an empty tag correlation object
849 $tagcorrelation = new stdClass;
850 $tagcorrelation->id = null;
851 $tagcorrelation->tagid = null;
852 $tagcorrelation->correlatedtags = array();
853
638cf615 854 // We store each correlation id in this array so we can remove any correlations
2650c547
SH
855 // that no longer exist.
856 $correlations = array();
857
78defd5c 858 // Iterate each row of the result set and build them into tag correlations.
638cf615
AD
859 // We add all of a tag's correlations to $tagcorrelation->correlatedtags[]
860 // then save the $tagcorrelation object
78defd5c
SH
861 foreach ($rs as $row) {
862 if ($row->tagid != $tagcorrelation->tagid) {
638cf615 863 // The tag id has changed so we have all of the correlations for this tag
2650c547
SH
864 $tagcorrelationid = tag_process_computed_correlation($tagcorrelation);
865 if ($tagcorrelationid) {
866 $correlations[] = $tagcorrelationid;
867 }
78defd5c
SH
868 // Now we reset the tag correlation object so we can reuse it and set it
869 // up for the current record.
870 $tagcorrelation = new stdClass;
871 $tagcorrelation->id = $row->correlationid;
872 $tagcorrelation->tagid = $row->tagid;
873 $tagcorrelation->correlatedtags = array();
db32b054 874 }
638cf615 875 //Save the correlation on the tag correlation object
78defd5c
SH
876 $tagcorrelation->correlatedtags[] = $row->correlation;
877 }
878 // Update the current correlation after the last record.
2650c547
SH
879 $tagcorrelationid = tag_process_computed_correlation($tagcorrelation);
880 if ($tagcorrelationid) {
881 $correlations[] = $tagcorrelationid;
882 }
883
c933a060 884
78defd5c
SH
885 // Close the recordset
886 $rs->close();
2650c547
SH
887
888 // Remove any correlations that weren't just identified
638cf615
AD
889 if (empty($correlations)) {
890 //there are no tag correlations
891 $DB->delete_records('tag_correlation');
892 } else {
893 list($sql, $params) = $DB->get_in_or_equal($correlations, SQL_PARAMS_NAMED, 'param0000', false);
894 $DB->delete_records_select('tag_correlation', 'id '.$sql, $params);
895 }
78defd5c 896}
844f9c15 897
78defd5c
SH
898/**
899 * This function processes a tag correlation and makes changes in the database
900 * as required.
901 *
902 * The tag correlation object needs have both a tagid property and a correlatedtags
903 * property that is an array.
904 *
905 * @param stdClass $tagcorrelation
2650c547 906 * @return int The id of the tag correlation that was just processed.
78defd5c
SH
907 */
908function tag_process_computed_correlation(stdClass $tagcorrelation) {
909 global $DB;
db32b054 910
78defd5c
SH
911 // You must provide a tagid and correlatedtags must be set and be an array
912 if (empty($tagcorrelation->tagid) || !isset($tagcorrelation->correlatedtags) || !is_array($tagcorrelation->correlatedtags)) {
913 return false;
3d436d8a 914 }
78defd5c 915
78defd5c
SH
916 $tagcorrelation->correlatedtags = join(',', $tagcorrelation->correlatedtags);
917 if (!empty($tagcorrelation->id)) {
918 // The tag correlation already exists so update it
919 $DB->update_record('tag_correlation', $tagcorrelation);
920 } else {
921 // This is a new correlation to insert
2650c547 922 $tagcorrelation->id = $DB->insert_record('tag_correlation', $tagcorrelation);
78defd5c 923 }
2650c547 924 return $tagcorrelation->id;
db32b054 925}
926
927/**
928 * Tasks that should be performed at cron time
929 */
930function tag_cron() {
931 tag_compute_correlations();
0f5e5f5c 932 tag_cleanup();
db32b054 933}
934
0f039283 935/**
936 * Search for tags with names that match some text
937 *
938 * @param string $text escaped string that the tag names will be matched against
939 * @param boolean $ordered If true, tags are ordered by their popularity. If false, no ordering.
940 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
941 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
942 * @return mixed an array of objects, or false if no records were found or an error occured.
943 */
944function tag_find_tags($text, $ordered=true, $limitfrom='', $limitnum='') {
f7ed2687 945 global $DB;
0f039283 946
947 $text = array_shift(tag_normalize($text, TAG_CASE_LOWER));
948
949 if ($ordered) {
f7ed2687 950 $query = "SELECT tg.id, tg.name, tg.rawname, COUNT(ti.id) AS count
951 FROM {tag} tg LEFT JOIN {tag_instance} ti ON tg.id = ti.tagid
952 WHERE tg.name LIKE ?
953 GROUP BY tg.id, tg.name, tg.rawname
954 ORDER BY count DESC";
0f039283 955 } else {
f7ed2687 956 $query = "SELECT tg.id, tg.name, tg.rawname
957 FROM {tag} tg
958 WHERE tg.name LIKE ?";
0f039283 959 }
f7ed2687 960 $params = array("%{$text}%");
961 return $DB->get_records_sql($query, $params, $limitfrom , $limitnum);
0f039283 962}
963
986dca60 964/**
db32b054 965 * Get the name of a tag
986dca60 966 *
db32b054 967 * @param mixed $tagids the id of the tag, or an array of ids
968 * @return mixed string name of one tag, or id-indexed array of strings
969 */
970function tag_get_name($tagids) {
f7ed2687 971 global $DB;
3d436d8a 972
f7ed2687 973 if (!is_array($tagids)) {
974 if ($tag = $DB->get_record('tag', array('id'=>$tagids))) {
975 return $tag->name;
976 }
977 return false;
db32b054 978 }
c933a060 979
db32b054 980 $tag_names = array();
986dca60 981 foreach($DB->get_records_list('tag', 'id', $tagids) as $tag) {
db32b054 982 $tag_names[$tag->id] = $tag->name;
983 }
c933a060 984
db32b054 985 return $tag_names;
c933a060 986}
987
988/**
db32b054 989 * Returns the correlated tags of a tag, retrieved from the tag_correlation
986dca60 990 * table. Make sure cron runs, otherwise the table will be empty and this
db32b054 991 * function won't return anything.
c933a060 992 *
db32b054 993 * @param int $tag_id is a single tag id
994 * @return array an array of tag objects, empty if no correlated tags are found
c933a060 995 */
db32b054 996function tag_get_correlated($tag_id, $limitnum=null) {
f7ed2687 997 global $DB;
3d436d8a 998
f7ed2687 999 $tag_correlation = $DB->get_record('tag_correlation', array('tagid'=>$tag_id));
c933a060 1000
db32b054 1001 if (!$tag_correlation || empty($tag_correlation->correlatedtags)) {
1002 return array();
c933a060 1003 }
986dca60 1004
73c32e86 1005 // this is (and has to) return the same fields as the query in tag_get_tags
638cf615 1006 $sql = "SELECT DISTINCT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering
e2a64d83 1007 FROM {tag} tg
638cf615 1008 INNER JOIN {tag_instance} ti ON tg.id = ti.tagid
e2a64d83
SH
1009 WHERE tg.id IN ({$tag_correlation->correlatedtags})";
1010 $result = $DB->get_records_sql($sql);
1011 if (!$result) {
db32b054 1012 return array();
c933a060 1013 }
986dca60 1014
db32b054 1015 return $result;
c933a060 1016}
1017
c933a060 1018/**
db32b054 1019 * Function that normalizes a list of tag names.
c933a060 1020 *
db32b054 1021 * @param mixed $tags array of tags, or a single tag.
986dca60 1022 * @param int $case case to use for returned value (default: lower case).
0f039283 1023 * Either TAG_CASE_LOWER (default) or TAG_CASE_ORIGINAL
986dca60 1024 * @return array of lowercased normalized tags, indexed by the normalized tag,
0f039283 1025 * in the same order as the original array. (Eg: 'Banana' => 'banana').
c933a060 1026 */
db32b054 1027function tag_normalize($rawtags, $case = TAG_CASE_LOWER) {
1028
0f039283 1029 // cache normalized tags, to prevent costly repeated calls to clean_param
db32b054 1030 static $cleaned_tags_lc = array(); // lower case - use for comparison
1031 static $cleaned_tags_mc = array(); // mixed case - use for saving to database
c933a060 1032
db32b054 1033 if ( !is_array($rawtags) ) {
1034 $rawtags = array($rawtags);
1035 }
c933a060 1036
db32b054 1037 $result = array();
1038 foreach($rawtags as $rawtag) {
1039 $rawtag = trim($rawtag);
1040 if (!$rawtag) {
1041 continue;
1042 }
1043 if ( !array_key_exists($rawtag, $cleaned_tags_lc) ) {
1044 $cleaned_tags_lc[$rawtag] = moodle_strtolower( clean_param($rawtag, PARAM_TAG) );
1045 $cleaned_tags_mc[$rawtag] = clean_param($rawtag, PARAM_TAG);
1046 }
986dca60 1047 if ( $case == TAG_CASE_LOWER ) {
db32b054 1048 $result[$rawtag] = $cleaned_tags_lc[$rawtag];
1049 } else { // TAG_CASE_ORIGINAL
1050 $result[$rawtag] = $cleaned_tags_mc[$rawtag];
1051 }
1052 }
986dca60 1053
db32b054 1054 return $result;
c933a060 1055}
1056
1057/**
0f039283 1058 * Count how many records are tagged with a specific tag,
c933a060 1059 *
0f039283 1060 * @param string $record record to look for ('post', 'user', etc.)
1061 * @param int $tag is a single tag id
1062 * @return int number of mathing tags.
c933a060 1063 */
0f039283 1064function tag_record_count($record_type, $tagid) {
f7ed2687 1065 global $DB;
1066 return $DB->count_records('tag_instance', array('itemtype'=>$record_type, 'tagid'=>$tagid));
0f039283 1067}
c933a060 1068
0f039283 1069/**
986dca60 1070 * Determine if a record is tagged with a specific tag
0f039283 1071 *
1072 * @param string $record_type the record type to look for
1073 * @param int $record_id the record id to look for
1074 * @param string $tag a tag name
1075 * @return bool true if it is tagged, false otherwise
1076 */
1077function tag_record_tagged_with($record_type, $record_id, $tag) {
f7ed2687 1078 global $DB;
0f039283 1079 if ($tagid = tag_get_id($tag)) {
f7ed2687 1080 return $DB->count_records('tag_instance', array('itemtype'=>$record_type, 'itemid'=>$record_id, 'tagid'=>$tagid));
c933a060 1081 } else {
0f039283 1082 return 0; // tag doesn't exist
c933a060 1083 }
c933a060 1084}
1085
1086/**
db32b054 1087 * Flag a tag as inapropriate
986dca60 1088 *
db32b054 1089 * @param mixed $tagids one (int) tagid, or an array of tagids
1090 * @return void
c933a060 1091 */
db32b054 1092function tag_set_flag($tagids) {
f7ed2687 1093 global $DB;
1094
1095 $tagids = (array)$tagids;
db32b054 1096 foreach ($tagids as $tagid) {
f7ed2687 1097 $tag = $DB->get_record('tag', array('id'=>$tagid), 'id, flag');
c933a060 1098 $tag->flag++;
1099 $tag->timemodified = time();
f7ed2687 1100 $DB->update_record('tag', $tag);
c933a060 1101 }
1102}
1103
986dca60 1104/**
db32b054 1105 * Remove the inapropriate flag on a tag
986dca60 1106 *
db32b054 1107 * @param mixed $tagids one (int) tagid, or an array of tagids
1108 * @return bool true if function succeeds, false otherwise
c933a060 1109 */
db32b054 1110function tag_unset_flag($tagids) {
f7ed2687 1111 global $DB;
d4be9d6d 1112
db32b054 1113 if ( is_array($tagids) ) {
1114 $tagids = implode(',', $tagids);
ab86d9c7 1115 }
db32b054 1116 $timemodified = time();
cef53045 1117 return $DB->execute("UPDATE {tag} SET flag = 0, timemodified = ? WHERE id IN ($tagids)", array($timemodified));
c933a060 1118}
b1627a92
DC
1119
1120
1121/**
1122 * Return a list of page types
1123 * @param string $pagetype current page type
1124 * @param stdClass $parentcontext Block's parent context
1125 * @param stdClass $currentcontext Current context of block
1126 */
b38e2e28 1127function tag_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
1128 return array(
1129 'tag-*'=>get_string('page-tag-x', 'tag'),
1130 'tag-index'=>get_string('page-tag-index', 'tag'),
1131 'tag-search'=>get_string('page-tag-search', 'tag'),
1132 'tag-manage'=>get_string('page-tag-manage', 'tag')
1133 );
1134}