2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
21 * Tag strings : you can use any character in tags, except the comma (which is the separator) and
22 * the '\' (backslash). Note that many spaces (or other blank characters) will get "compressed"
23 * into one. A tag string is always a rawurlencode'd string. This is the same behavior as
26 * A "record" is a php array (note that an object will work too) that contains the following
28 * - type: The database table containing the record that we are tagging (eg: for a blog, this is
29 * the table named 'post', and for a user it is the table name 'user')
30 * - id: The id of the record
32 * BASIC INSTRUCTIONS :
33 * - to "tag a blog post" (for example):
34 * tag_set('post', $blog_post->id, $array_of_tags, 'core', $thecontext);
36 * - to "remove all the tags on a blog post":
37 * tag_set('post', $blog_post->id, array(), 'core', $thecontext);
39 * Tag set will create tags that need to be created.
43 * @todo MDL-31090 turn this into a full-fledged categorization system. This could start by
44 * modifying (removing, probably) the 'tag type' to use another table describing the
45 * relationship between tags (parents, sibling, etc.), which could then be merged with
46 * the 'course categorization' system.
47 * @see http://www.php.net/manual/en/function.urlencode.php
48 * @copyright 2007 Luiz Cruz <luiz.laydner@gmail.com>
49 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
53 * Used to require that the return value from a function is an array.
56 define('TAG_RETURN_ARRAY', 0);
58 * Used to require that the return value from a function is an object.
61 define('TAG_RETURN_OBJECT', 1);
63 * Use to specify that HTML free text is expected to be returned from a function.
64 * @see tag_display_name()
66 define('TAG_RETURN_TEXT', 2);
68 * Use to specify that encoded HTML is expected to be returned from a function.
69 * @see tag_display_name()
71 define('TAG_RETURN_HTML', 3);
74 * Used to specify that we wish a lowercased string to be returned
77 define('TAG_CASE_LOWER', 0);
79 * Used to specify that we do not wish the case of the returned string to change
82 define('TAG_CASE_ORIGINAL', 1);
85 * Used to specify that we want all related tags returned, no matter how they are related.
86 * @see tag_get_related_tags()
88 define('TAG_RELATED_ALL', 0);
90 * Used to specify that we only want back tags that were manually related.
91 * @see tag_get_related_tags()
93 define('TAG_RELATED_MANUAL', 1);
95 * Used to specify that we only want back tags where the relationship was automatically correlated.
96 * @see tag_get_related_tags()
98 define('TAG_RELATED_CORRELATED', 2);
100 ///////////////////////////////////////////////////////
101 /////////////////// PUBLIC TAG API ////////////////////
103 /// Functions for settings tags //////////////////////
106 * Set the tags assigned to a record. This overwrites the current tags.
108 * This function is meant to be fed the string coming up from the user interface, which contains all tags assigned to a record.
113 * @param string $record_type the type of record to tag ('post' for blogs, 'user' for users, 'tag' for tags, etc.)
114 * @param int $record_id the id of the record to tag
115 * @param array $tags the array of tags to set on the record. If given an empty array, all tags will be removed.
116 * @param string|null $component the component that was tagged
117 * @param int|null $contextid the context id of where this tag was assigned
120 function tag_set($record_type, $record_id, $tags, $component = null, $contextid = null) {
122 static $in_recursion_semaphore = false; // this is to prevent loops when tagging a tag
124 if ( $record_type == 'tag' && !$in_recursion_semaphore) {
125 $current_tagged_tag_name = tag_get_name($record_id);
128 $tags_ids = tag_get_id($tags, TAG_RETURN_ARRAY
); // force an array, even if we only have one tag.
129 $cleaned_tags = tag_normalize($tags);
130 //echo 'tags-in-tag_set'; var_dump($tags); var_dump($tags_ids); var_dump($cleaned_tags);
132 $current_ids = tag_get_tags_ids($record_type, $record_id);
133 //var_dump($current_ids);
135 // for data coherence reasons, it's better to remove deleted tags
136 // before adding new data: ordering could be duplicated.
137 foreach($current_ids as $current_id) {
138 if (!in_array($current_id, $tags_ids)) {
139 tag_delete_instance($record_type, $record_id, $current_id);
140 if ( $record_type == 'tag' && !$in_recursion_semaphore) {
141 // if we are removing a tag-on-a-tag (manually related tag),
142 // we need to remove the opposite relationship as well.
143 tag_delete_instance('tag', $current_id, $record_id);
152 foreach($tags as $ordering => $tag) {
158 $clean_tag = $cleaned_tags[$tag];
159 $tag_current_id = $tags_ids[$clean_tag];
161 if ( is_null($tag_current_id) ) {
163 //echo "call to add tag $tag\n";
164 $new_tag = tag_add($tag);
165 $tag_current_id = $new_tag[$clean_tag];
168 tag_assign($record_type, $record_id, $tag_current_id, $ordering, 0, $component, $contextid);
170 // if we are tagging a tag (adding a manually-assigned related tag), we
171 // need to create the opposite relationship as well.
172 if ( $record_type == 'tag' && !$in_recursion_semaphore) {
173 $in_recursion_semaphore = true;
174 tag_set_add('tag', $tag_current_id, $current_tagged_tag_name, $component, $contextid);
175 $in_recursion_semaphore = false;
181 * Adds a tag to a record, without overwriting the current tags.
186 * @param string $record_type the type of record to tag ('post' for blogs, 'user' for users, etc.)
187 * @param int $record_id the id of the record to tag
188 * @param string $tag the tag to add
189 * @param string|null $component the component that was tagged
190 * @param int|null $contextid the context id of where this tag was assigned
193 function tag_set_add($record_type, $record_id, $tag, $component = null, $contextid = null) {
196 foreach( tag_get_tags($record_type, $record_id) as $current_tag ) {
197 $new_tags[] = $current_tag->rawname
;
201 return tag_set($record_type, $record_id, $new_tags, $component, $contextid);
205 * Removes a tag from a record, without overwriting other current tags.
210 * @param string $record_type the type of record to tag ('post' for blogs, 'user' for users, etc.)
211 * @param int $record_id the id of the record to tag
212 * @param string $tag the tag to delete
213 * @param string|null $component the component that was tagged
214 * @param int|null $contextid the context id of where this tag was assigned
217 function tag_set_delete($record_type, $record_id, $tag, $component = null, $contextid = null) {
220 foreach( tag_get_tags($record_type, $record_id) as $current_tag ) {
221 if ($current_tag->name
!= $tag) { // Keep all tags but the one specified
222 $new_tags[] = $current_tag->name
;
226 return tag_set($record_type, $record_id, $new_tags, $component, $contextid);
230 * Set the type of a tag. At this time (version 2.2) the possible values are 'default' or 'official'. Official tags will be
231 * displayed separately "at tagging time" (while selecting the tags to apply to a record).
236 * @param string $tagid tagid to modify
237 * @param string $type either 'default' or 'official'
238 * @return bool true on success, false otherwise
240 function tag_type_set($tagid, $type) {
243 if ($tag = $DB->get_record('tag', array('id' => $tagid), 'id, userid, name, rawname')) {
244 $tag->tagtype
= $type;
245 $tag->timemodified
= time();
246 $DB->update_record('tag', $tag);
248 $event = \core\event\tag_updated
::create(array(
249 'objectid' => $tag->id
,
250 'relateduserid' => $tag->userid
,
251 'context' => context_system
::instance(),
253 'name' => $tag->name
,
254 'rawname' => $tag->rawname
265 * Set the description of a tag
270 * @param int $tagid the id of the tag
271 * @param string $description the tag's description string to be set
272 * @param int $descriptionformat the moodle text format of the description
273 * {@link http://docs.moodle.org/dev/Text_formats_2.0#Database_structure}
274 * @return bool true on success, false otherwise
276 function tag_description_set($tagid, $description, $descriptionformat) {
279 if ($tag = $DB->get_record('tag', array('id' => $tagid), 'id, userid, name, rawname')) {
280 $tag->description
= $description;
281 $tag->descriptionformat
= $descriptionformat;
282 $tag->timemodified
= time();
283 $DB->update_record('tag', $tag);
285 $event = \core\event\tag_updated
::create(array(
286 'objectid' => $tag->id
,
287 'relateduserid' => $tag->userid
,
288 'context' => context_system
::instance(),
290 'name' => $tag->name
,
291 'rawname' => $tag->rawname
307 /// Functions for getting information about tags //////
310 * Simple function to just return a single tag object when you know the name or something
315 * @param string $field which field do we use to identify the tag: id, name or rawname
316 * @param string $value the required value of the aforementioned field
317 * @param string $returnfields which fields do we want returned. This is a comma seperated string containing any combination of
318 * 'id', 'name', 'rawname' or '*' to include all fields.
319 * @return mixed tag object
321 function tag_get($field, $value, $returnfields='id, name, rawname') {
324 if ($field == 'name') {
325 $value = core_text
::strtolower($value); // To cope with input that might just be wrong case
327 return $DB->get_record('tag', array($field=>$value), $returnfields);
332 * Get the array of db record of tags associated to a record (instances). Use {@see tag_get_tags_csv()} if you wish to get the same
333 * data in a comma-separated string, for instances such as needing to simply display a list of tags to the end user. This should
334 * really be called tag_get_tag_instances().
339 * @param string $record_type the record type for which we want to get the tags
340 * @param int $record_id the record id for which we want to get the tags
341 * @param string $type the tag type (either 'default' or 'official'). By default, all tags are returned.
342 * @param int $userid (optional) only required for course tagging
343 * @return array the array of tags
345 function tag_get_tags($record_type, $record_id, $type=null, $userid=0) {
351 $sql_type = "AND tg.tagtype = :type";
352 $params['type'] = $type;
359 $u = "AND ti.tiuserid = :userid ";
360 $params['userid'] = $userid;
363 $sql = "SELECT ti.id AS taginstanceid, tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering
364 FROM {tag_instance} ti
365 JOIN {tag} tg ON tg.id = ti.tagid
366 WHERE ti.itemtype = :recordtype AND ti.itemid = :recordid $u $sql_type
367 ORDER BY ti.ordering ASC";
368 $params['recordtype'] = $record_type;
369 $params['recordid'] = $record_id;
371 // if the fields in this query are changed, you need to do the same changes in tag_get_correlated_tags
372 return $DB->get_records_sql($sql, $params);
373 // This version of the query, reversing the ON clause, "correctly" returns
374 // a row with NULL values for instances that are still in the DB even though
375 // the tag has been deleted. This shouldn't happen, but if it did, using
376 // this query could help "clean it up". This causes bugs at this time.
377 //$tags = $DB->get_records_sql("SELECT ti.tagid, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering ".
378 // "FROM {tag_instance} ti LEFT JOIN {tag} tg ON ti.tagid = tg.id ".
379 // "WHERE ti.itemtype = '{$record_type}' AND ti.itemid = '{$record_id}' {$type} ".
380 // "ORDER BY ti.ordering ASC");
384 * Get the array of tags display names, indexed by id.
389 * @param string $record_type the record type for which we want to get the tags
390 * @param int $record_id the record id for which we want to get the tags
391 * @param string $type the tag type (either 'default' or 'official'). By default, all tags are returned.
392 * @return array the array of tags (with the value returned by tag_display_name), indexed by id
394 function tag_get_tags_array($record_type, $record_id, $type=null) {
396 foreach(tag_get_tags($record_type, $record_id, $type) as $tag) {
397 $tags[$tag->id
] = tag_display_name($tag);
403 * Get a comma-separated string of tags associated to a record. Use {@see tag_get_tags()} to get the same information in an array.
408 * @param string $record_type the record type for which we want to get the tags
409 * @param int $record_id the record id for which we want to get the tags
410 * @param int $html either TAG_RETURN_HTML or TAG_RETURN_TEXT, depending on the type of output desired
411 * @param string $type either 'official' or 'default', if null, all tags are returned
412 * @return string the comma-separated list of tags.
414 function tag_get_tags_csv($record_type, $record_id, $html=TAG_RETURN_HTML
, $type=null) {
417 $tags_names = array();
418 foreach(tag_get_tags($record_type, $record_id, $type) as $tag) {
419 if ($html == TAG_RETURN_TEXT
) {
420 $tags_names[] = tag_display_name($tag, TAG_RETURN_TEXT
);
421 } else { // TAG_RETURN_HTML
422 $tags_names[] = '<a href="'. $CFG->wwwroot
.'/tag/index.php?tag='. rawurlencode($tag->name
) .'">'. tag_display_name($tag) .'</a>';
425 return implode(', ', $tags_names);
429 * Get an array of tag ids associated to a record.
434 * @todo MDL-31150 Update ordering property
435 * @param string $record_type the record type for which we want to get the tags
436 * @param int $record_id the record id for which we want to get the tags
437 * @return array tag ids, indexed and sorted by 'ordering'
439 function tag_get_tags_ids($record_type, $record_id) {
441 foreach (tag_get_tags($record_type, $record_id) as $tag) {
442 if ( array_key_exists($tag->ordering
, $tag_ids) ) {
443 // until we can add a unique constraint, in table tag_instance,
444 // on (itemtype, itemid, ordering), this is needed to prevent a bug
445 // TODO MDL-31150 modify database in 2.0
448 $tag_ids[$tag->ordering
] = $tag->id
;
455 * Returns the database ID of a set of tags.
460 * @todo MDL-31152 Test the commented MDL-31152 todo in this function to see if it helps performance
461 * without breaking anything.
462 * @param mixed $tags one tag, or array of tags, to look for.
463 * @param bool $return_value specify the type of the returned value. Either TAG_RETURN_OBJECT, or TAG_RETURN_ARRAY (default).
464 * If TAG_RETURN_ARRAY is specified, an array will be returned even if only one tag was passed in $tags.
465 * @return mixed tag-indexed array of ids (or objects, if second parameter is TAG_RETURN_OBJECT), or only an int, if only one tag
466 * is given *and* the second parameter is null. No value for a key means the tag wasn't found.
468 function tag_get_id($tags, $return_value=null) {
471 static $tag_id_cache = array();
473 $return_an_int = false;
474 if (!is_array($tags)) {
475 if(is_null($return_value) ||
$return_value == TAG_RETURN_OBJECT
) {
476 $return_an_int = true;
478 $tags = array($tags);
483 //TODO MDL-31152 test this and see if it helps performance without breaking anything
484 //foreach($tags as $key => $tag) {
485 // $clean_tag = core_text::strtolower($tag);
486 // if ( array_key_exists($clean_tag), $tag_id_cache) ) {
487 // $result[$clean_tag] = $tag_id_cache[$clean_tag];
488 // $tags[$key] = ''; // prevent further processing for this one.
492 $tags = array_values(tag_normalize($tags));
493 foreach($tags as $key => $tag) {
494 $tags[$key] = core_text
::strtolower($tag);
495 $result[core_text
::strtolower($tag)] = null; // key must exists : no value for a key means the tag wasn't found.
502 list($tag_string, $params) = $DB->get_in_or_equal($tags);
504 $rs = $DB->get_recordset_sql("SELECT * FROM {tag} WHERE name $tag_string ORDER BY name", $params);
505 foreach ($rs as $record) {
506 if ($return_value == TAG_RETURN_OBJECT
) {
507 $result[$record->name
] = $record;
508 } else { // TAG_RETURN_ARRAY
509 $result[$record->name
] = $record->id
;
514 if ($return_an_int) {
515 return array_pop($result);
523 * Returns tags related to a tag
525 * Related tags of a tag come from two sources:
526 * - manually added related tags, which are tag_instance entries for that tag
527 * - correlated tags, which are calculated
532 * @param string $tagid is a single **normalized** tag name or the id of a tag
533 * @param int $type the function will return either manually (TAG_RELATED_MANUAL) related tags or correlated
534 * (TAG_RELATED_CORRELATED) tags. Default is TAG_RELATED_ALL, which returns everything.
535 * @param int $limitnum (optional) return a subset comprising this many records, the default is 10
536 * @return array an array of tag objects
538 function tag_get_related_tags($tagid, $type=TAG_RELATED_ALL
, $limitnum=10) {
540 $related_tags = array();
542 if ( $type == TAG_RELATED_ALL ||
$type == TAG_RELATED_MANUAL
) {
543 //gets the manually added related tags
544 $related_tags = tag_get_tags('tag', $tagid);
547 if ( $type == TAG_RELATED_ALL ||
$type == TAG_RELATED_CORRELATED
) {
548 //gets the correlated tags
549 $automatic_related_tags = tag_get_correlated($tagid);
550 $related_tags = array_merge($related_tags, $automatic_related_tags);
553 // Remove duplicated tags (multiple instances of the same tag).
555 foreach ($related_tags as $instance => $tag) {
556 if (isset($seen[$tag->id
])) {
557 unset($related_tags[$instance]);
563 return array_slice($related_tags, 0 , $limitnum);
567 * Get a comma-separated list of tags related to another tag.
572 * @param array $related_tags the array returned by tag_get_related_tags
573 * @param int $html either TAG_RETURN_HTML (default) or TAG_RETURN_TEXT : return html links, or just text.
574 * @return string comma-separated list
576 function tag_get_related_tags_csv($related_tags, $html=TAG_RETURN_HTML
) {
579 $tags_names = array();
580 foreach($related_tags as $tag) {
581 if ( $html == TAG_RETURN_TEXT
) {
582 $tags_names[] = tag_display_name($tag, TAG_RETURN_TEXT
);
585 $tags_names[] = '<a href="'. $CFG->wwwroot
.'/tag/index.php?tag='. rawurlencode($tag->name
) .'">'. tag_display_name($tag) .'</a>';
589 return implode(', ', $tags_names);
593 * Change the "value" of a tag, and update the associated 'name'.
598 * @param int $tagid the id of the tag to modify
599 * @param string $newrawname the new rawname
600 * @return bool true on success, false otherwise
602 function tag_rename($tagid, $newrawname) {
605 $norm = tag_normalize($newrawname, TAG_CASE_ORIGINAL
);
606 if (! $newrawname_clean = array_shift($norm) ) {
610 if (! $newname_clean = core_text
::strtolower($newrawname_clean)) {
614 // Prevent the rename if a tag with that name already exists
615 if ($existing = tag_get('name', $newname_clean, 'id, name, rawname')) {
616 if ($existing->id
!= $tagid) { // Another tag already exists with this name
621 if ($tag = tag_get('id', $tagid, 'id, userid, name, rawname')) {
622 // Store the name before we change it.
623 $oldname = $tag->name
;
625 $tag->rawname
= $newrawname_clean;
626 $tag->name
= $newname_clean;
627 $tag->timemodified
= time();
628 $DB->update_record('tag', $tag);
630 $event = \core\event\tag_updated
::create(array(
631 'objectid' => $tag->id
,
632 'relateduserid' => $tag->userid
,
633 'context' => context_system
::instance(),
635 'name' => $newname_clean,
636 'rawname' => $newrawname_clean
639 $event->set_legacy_logdata(array($COURSE->id
, 'tag', 'update', 'index.php?id='. $tag->id
, $oldname . '->'. $tag->name
));
649 * Delete one or more tag, and all their instances if there are any left.
654 * @param mixed $tagids one tagid (int), or one array of tagids to delete
655 * @return bool true on success, false otherwise
657 function tag_delete($tagids) {
660 if (!is_array($tagids)) {
661 $tagids = array($tagids);
664 // Use the tagids to create a select statement to be used later.
665 list($tagsql, $tagparams) = $DB->get_in_or_equal($tagids);
667 // Store the tags and tag instances we are going to delete.
668 $tags = $DB->get_records_select('tag', 'id ' . $tagsql, $tagparams);
669 $taginstances = $DB->get_records_select('tag_instance', 'tagid ' . $tagsql, $tagparams);
671 // Delete all the tag instances.
672 $select = 'WHERE tagid ' . $tagsql;
673 $sql = "DELETE FROM {tag_instance} $select";
674 $DB->execute($sql, $tagparams);
676 // Delete all the tag correlations.
677 $sql = "DELETE FROM {tag_correlation} $select";
678 $DB->execute($sql, $tagparams);
680 // Delete all the tags.
681 $select = 'WHERE id ' . $tagsql;
682 $sql = "DELETE FROM {tag} $select";
683 $DB->execute($sql, $tagparams);
685 // Fire an event that these items were untagged.
687 // Save the system context in case the 'contextid' column in the 'tag_instance' table is null.
688 $syscontextid = context_system
::instance()->id
;
689 // Loop through the tag instances and fire a 'tag_removed'' event.
690 foreach ($taginstances as $taginstance) {
691 // We can not fire an event with 'null' as the contextid.
692 if (is_null($taginstance->contextid
)) {
693 $taginstance->contextid
= $syscontextid;
696 // Trigger tag removed event.
697 $event = \core\event\tag_removed
::create(array(
698 'objectid' => $taginstance->id
,
699 'contextid' => $taginstance->contextid
,
701 'tagid' => $taginstance->tagid
,
702 'tagname' => $tags[$taginstance->tagid
]->name
,
703 'tagrawname' => $tags[$taginstance->tagid
]->rawname
,
704 'itemid' => $taginstance->itemid
,
705 'itemtype' => $taginstance->itemtype
708 $event->add_record_snapshot('tag_instance', $taginstance);
713 // Fire an event that these tags were deleted.
715 $context = context_system
::instance();
716 foreach ($tags as $tag) {
717 // Delete all files associated with this tag
718 $fs = get_file_storage();
719 $files = $fs->get_area_files($context->id
, 'tag', 'description', $tag->id
);
720 foreach ($files as $file) {
724 // Trigger an event for deleting this tag.
725 $event = \core\event\tag_deleted
::create(array(
726 'objectid' => $tag->id
,
727 'relateduserid' => $tag->userid
,
728 'context' => $context,
730 'name' => $tag->name
,
731 'rawname' => $tag->rawname
734 $event->add_record_snapshot('tag', $tag);
743 * Deletes all the tag instances given a component and an optional contextid.
745 * @param string $component
746 * @param int $contextid if null, then we delete all tag instances for the $component
748 function tag_delete_instances($component, $contextid = null) {
751 $sql = "SELECT ti.*, t.name, t.rawname
752 FROM {tag_instance} ti
754 ON ti.tagid = t.id ";
755 if (is_null($contextid)) {
756 $params = array('component' => $component);
757 $sql .= "WHERE ti.component = :component";
759 $params = array('component' => $component, 'contextid' => $contextid);
760 $sql .= "WHERE ti.component = :component
761 AND ti.contextid = :contextid";
763 if ($taginstances = $DB->get_records_sql($sql, $params)) {
764 // Now remove all the tag instances.
765 $DB->delete_records('tag_instance',$params);
766 // Save the system context in case the 'contextid' column in the 'tag_instance' table is null.
767 $syscontextid = context_system
::instance()->id
;
768 // Loop through the tag instances and fire an 'tag_removed' event.
769 foreach ($taginstances as $taginstance) {
770 // We can not fire an event with 'null' as the contextid.
771 if (is_null($taginstance->contextid
)) {
772 $taginstance->contextid
= $syscontextid;
775 // Trigger tag removed event.
776 $event = \core\event\tag_removed
::create(array(
777 'objectid' => $taginstance->id
,
778 'contextid' => $taginstance->contextid
,
780 'tagid' => $taginstance->tagid
,
781 'tagname' => $taginstance->name
,
782 'tagrawname' => $taginstance->rawname
,
783 'itemid' => $taginstance->itemid
,
784 'itemtype' => $taginstance->itemtype
787 $event->add_record_snapshot('tag_instance', $taginstance);
794 * Delete one instance of a tag. If the last instance was deleted, it will also delete the tag, unless its type is 'official'.
799 * @param string $record_type the type of the record for which to remove the instance
800 * @param int $record_id the id of the record for which to remove the instance
801 * @param int $tagid the tagid that needs to be removed
802 * @param int $userid (optional) the userid
803 * @return bool true on success, false otherwise
805 function tag_delete_instance($record_type, $record_id, $tagid, $userid = null) {
808 if (is_null($userid)) {
809 $taginstance = $DB->get_record('tag_instance', array('tagid' => $tagid, 'itemtype' => $record_type, 'itemid' => $record_id));
811 $taginstance = $DB->get_record('tag_instance', array('tagid' => $tagid, 'itemtype' => $record_type, 'itemid' => $record_id,
812 'tiuserid' => $userid));
816 $tag = $DB->get_record('tag', array('id' => $tagid));
818 $DB->delete_records('tag_instance', array('id' => $taginstance->id
));
820 // We can not fire an event with 'null' as the contextid.
821 if (is_null($taginstance->contextid
)) {
822 $taginstance->contextid
= context_system
::instance()->id
;
825 // Trigger tag removed event.
826 $event = \core\event\tag_removed
::create(array(
827 'objectid' => $taginstance->id
,
828 'contextid' => $taginstance->contextid
,
831 'tagname' => $tag->name
,
832 'tagrawname' => $tag->rawname
,
833 'itemid' => $taginstance->itemid
,
834 'itemtype' => $taginstance->itemtype
837 $event->add_record_snapshot('tag_instance', $taginstance);
840 // If there are no other instances of the tag then consider deleting the tag as well.
841 if (!$DB->record_exists('tag_instance', array('tagid' => $tagid))) {
842 // If the tag is a personal tag then delete it - don't delete official tags.
843 if ($tag->tagtype
== 'default') {
856 * Function that returns the name that should be displayed for a specific tag
861 * @param object $tagobject a line out of tag table, as returned by the adobd functions
862 * @param int $html TAG_RETURN_HTML (default) will return htmlspecialchars encoded string, TAG_RETURN_TEXT will not encode.
865 function tag_display_name($tagobject, $html=TAG_RETURN_HTML
) {
868 if (!isset($tagobject->name
)) {
872 if (empty($CFG->keeptagnamecase
)) {
873 //this is the normalized tag name
874 $tagname = core_text
::strtotitle($tagobject->name
);
876 //original casing of the tag name
877 $tagname = $tagobject->rawname
;
880 // clean up a bit just in case the rules change again
881 $tagname = clean_param($tagname, PARAM_TAG
);
883 if ($html == TAG_RETURN_TEXT
) {
885 } else { // TAG_RETURN_HTML
886 return htmlspecialchars($tagname);
891 * Find all records tagged with a tag of a given type ('post', 'user', etc.)
896 * @param string $tag tag to look for
897 * @param string $type type to restrict search to. If null, every matching record will be returned
898 * @param int $limitfrom (optional, required if $limitnum is set) return a subset of records, starting at this point.
899 * @param int $limitnum (optional, required if $limitfrom is set) return a subset comprising this many records.
900 * @return array of matching objects, indexed by record id, from the table containing the type requested
902 function tag_find_records($tag, $type, $limitfrom='', $limitnum='') {
905 if (!$tag ||
!$type) {
909 $tagid = tag_get_id($tag);
911 $query = "SELECT it.*
912 FROM {".$type."} it INNER JOIN {tag_instance} tt ON it.id = tt.itemid
913 WHERE tt.itemtype = ? AND tt.tagid = ?";
914 $params = array($type, $tagid);
916 return $DB->get_records_sql($query, $params, $limitfrom, $limitnum);
922 ///////////////////////////////////////////////////////
923 /////////////////// PRIVATE TAG API ///////////////////
926 * Adds one or more tag in the database. This function should not be called directly : you should
931 * @param mixed $tags one tag, or an array of tags, to be created
932 * @param string $type type of tag to be created ("default" is the default value and "official" is the only other supported
933 * value at this time). An official tag is kept even if there are no records tagged with it.
934 * @return array $tags ids indexed by their lowercase normalized names. Any boolean false in the array indicates an error while
937 function tag_add($tags, $type="default") {
940 if (!is_array($tags)) {
941 $tags = array($tags);
944 $tag_object = new StdClass
;
945 $tag_object->tagtype
= $type;
946 $tag_object->userid
= $USER->id
;
947 $tag_object->timemodified
= time();
949 $clean_tags = tag_normalize($tags, TAG_CASE_ORIGINAL
);
952 foreach($clean_tags as $tag) {
955 $tags_ids[$tag] = false;
957 // note that the difference between rawname and name is only
958 // capitalization : the rawname is NOT the same at the rawtag.
959 $tag_object->rawname
= $tag;
960 $tag_name_lc = core_text
::strtolower($tag);
961 $tag_object->name
= $tag_name_lc;
962 //var_dump($tag_object);
963 $tags_ids[$tag_name_lc] = $DB->insert_record('tag', $tag_object);
965 $event = \core\event\tag_created
::create(array(
966 'objectid' => $tags_ids[$tag_name_lc],
967 'relateduserid' => $tag_object->userid
,
968 'context' => context_system
::instance(),
970 'name' => $tag_object->name
,
971 'rawname' => $tag_object->rawname
982 * Assigns a tag to a record; if the record already exists, the time and ordering will be updated.
986 * @param string $record_type the type of the record that will be tagged
987 * @param int $record_id the id of the record that will be tagged
988 * @param string $tagid the tag id to set on the record.
989 * @param int $ordering the order of the instance for this record
990 * @param int $userid (optional) only required for course tagging
991 * @param string|null $component the component that was tagged
992 * @param int|null $contextid the context id of where this tag was assigned
993 * @return bool true on success, false otherwise
995 function tag_assign($record_type, $record_id, $tagid, $ordering, $userid = 0, $component = null, $contextid = null) {
998 if ($component === null ||
$contextid === null) {
999 debugging('You should specify the component and contextid of the item being tagged in your call to tag_assign.',
1004 $tag = $DB->get_record('tag', array('id' => $tagid), 'name, rawname', MUST_EXIST
);
1006 if ( $tag_instance_object = $DB->get_record('tag_instance', array('tagid'=>$tagid, 'itemtype'=>$record_type, 'itemid'=>$record_id, 'tiuserid'=>$userid), 'id')) {
1007 $tag_instance_object->ordering
= $ordering;
1008 $tag_instance_object->timemodified
= time();
1010 $DB->update_record('tag_instance', $tag_instance_object);
1012 $tag_instance_object = new StdClass
;
1013 $tag_instance_object->tagid
= $tagid;
1014 $tag_instance_object->component
= $component;
1015 $tag_instance_object->itemid
= $record_id;
1016 $tag_instance_object->itemtype
= $record_type;
1017 $tag_instance_object->contextid
= $contextid;
1018 $tag_instance_object->ordering
= $ordering;
1019 $tag_instance_object->timecreated
= time();
1020 $tag_instance_object->timemodified
= $tag_instance_object->timecreated
;
1021 $tag_instance_object->tiuserid
= $userid;
1023 $tag_instance_object->id
= $DB->insert_record('tag_instance', $tag_instance_object);
1026 // We can not fire an event with 'null' as the contextid.
1027 if (is_null($contextid)) {
1028 $contextid = context_system
::instance()->id
;
1031 // Trigger tag added event.
1032 $event = \core\event\tag_added
::create(array(
1033 'objectid' => $tag_instance_object->id
,
1034 'contextid' => $contextid,
1037 'tagname' => $tag->name
,
1038 'tagrawname' => $tag->rawname
,
1039 'itemid' => $record_id,
1040 'itemtype' => $record_type
1049 * Function that returns tags that start with some text, for use by the autocomplete feature
1053 * @param string $text string that the tag names will be matched against
1054 * @return mixed an array of objects, or false if no records were found or an error occured.
1056 function tag_autocomplete($text) {
1058 return $DB->get_records_sql("SELECT tg.id, tg.name, tg.rawname
1060 WHERE tg.name LIKE ?", array(core_text
::strtolower($text)."%"));
1064 * Clean up the tag tables, making sure all tagged object still exists.
1066 * This should normally not be necessary, but in case related tags are not deleted when the tagged record is removed, this should be
1067 * done once in a while, perhaps on an occasional cron run. On a site with lots of tags, this could become an expensive function to
1068 * call: don't run at peak time.
1072 * @todo MDL-31212 Update tag cleanup sql so that it supports multiple types of tags
1074 function tag_cleanup() {
1077 // Get ids to delete from instances where the tag has been deleted. This should never happen apparently.
1078 $sql = "SELECT ti.id
1079 FROM {tag_instance} ti
1080 LEFT JOIN {tag} t ON t.id = ti.tagid
1081 WHERE t.id IS null";
1082 $tagids = $DB->get_records_sql($sql);
1083 $tagarray = array();
1084 foreach ($tagids as $tagid) {
1085 $tagarray[] = $tagid->id
;
1088 // Next get ids from instances that have an owner that has been deleted.
1089 $sql = "SELECT ti.id
1090 FROM {tag_instance} ti, {user} u
1091 WHERE ti.itemid = u.id
1092 AND ti.itemtype = 'user'
1094 $tagids = $DB->get_records_sql($sql);
1095 foreach ($tagids as $tagid) {
1096 $tagarray[] = $tagid->id
;
1099 // Get the other itemtypes.
1100 $sql = "SELECT itemtype
1102 WHERE itemtype <> 'user'
1104 $tagitemtypes = $DB->get_records_sql($sql);
1105 foreach ($tagitemtypes as $key => $notused) {
1106 $sql = 'SELECT ti.id
1107 FROM {tag_instance} ti
1108 LEFT JOIN {' . $key . '} it ON it.id = ti.itemid
1110 AND ti.itemtype = \'' . $key . '\'';
1111 $tagids = $DB->get_records_sql($sql);
1112 foreach ($tagids as $tagid) {
1113 $tagarray[] = $tagid->id
;
1117 // Get instances for each of the ids to be deleted.
1118 if (count($tagarray) > 0) {
1119 list($sqlin, $params) = $DB->get_in_or_equal($tagarray);
1120 $sql = "SELECT ti.*, COALESCE(t.name, 'deleted') AS name, COALESCE(t.rawname, 'deleted') AS rawname
1121 FROM {tag_instance} ti
1122 LEFT JOIN {tag} t ON t.id = ti.tagid
1123 WHERE ti.id $sqlin";
1124 $instances = $DB->get_records_sql($sql, $params);
1125 tag_bulk_delete_instances($instances);
1128 // TODO MDL-31212 this will only clean tags of type 'default'. This is good as
1129 // it won't delete 'official' tags, but the day we get more than two
1130 // types, we need to fix this.
1131 $unused_tags = $DB->get_recordset_sql("SELECT tg.id
1133 WHERE tg.tagtype = 'default'
1136 FROM {tag_instance} ti
1137 WHERE ti.tagid = tg.id
1141 foreach ($unused_tags as $unused_tag) {
1142 tag_delete($unused_tag->id
);
1143 //debugging('deleting unused tag #'. $unused_tag->id, DEBUG_DEVELOPER);
1145 $unused_tags->close();
1149 * This function will delete numerous tag instances efficiently.
1150 * This removes tag instances only. It doesn't check to see if it is the last use of a tag.
1152 * @param array $instances An array of tag instance objects with the addition of the tagname and tagrawname
1153 * (used for recording a delete event).
1155 function tag_bulk_delete_instances($instances) {
1158 $instanceids = array();
1159 foreach ($instances as $instance) {
1160 $instanceids[] = $instance->id
;
1163 // This is a multi db compatible method of creating the correct sql when using the 'IN' value.
1164 // $insql is the sql statement, $params are the id numbers.
1165 list($insql, $params) = $DB->get_in_or_equal($instanceids);
1166 $sql = 'id ' . $insql;
1167 $DB->delete_records_select('tag_instance', $sql, $params);
1169 // Now go through and record each tag individually with the event system.
1170 foreach ($instances as $instance) {
1171 // Trigger tag removed event (i.e. The tag instance has been removed).
1172 $event = \core\event\tag_removed
::create(array(
1173 'objectid' => $instance->id
,
1174 'contextid' => $instance->contextid
,
1176 'tagid' => $instance->tagid
,
1177 'tagname' => $instance->name
,
1178 'tagrawname' => $instance->rawname
,
1179 'itemid' => $instance->itemid
,
1180 'itemtype' => $instance->itemtype
1183 unset($instance->name
);
1184 unset($instance->rawname
);
1185 $event->add_record_snapshot('tag_instance', $instance);
1191 * Calculates and stores the correlated tags of all tags. The correlations are stored in the 'tag_correlation' table.
1193 * Two tags are correlated if they appear together a lot. Ex.: Users tagged with "computers" will probably also be tagged with "algorithms".
1195 * The rationale for the 'tag_correlation' table is performance. It works as a cache for a potentially heavy load query done at the
1196 * 'tag_instance' table. So, the 'tag_correlation' table stores redundant information derived from the 'tag_instance' table.
1200 * @param int $mincorrelation Only tags with more than $mincorrelation correlations will be identified.
1202 function tag_compute_correlations($mincorrelation = 2) {
1205 // This mighty one line query fetches a row from the database for every
1206 // individual tag correlation. We then need to process the rows collecting
1207 // the correlations for each tag id.
1208 // The fields used by this query are as follows:
1209 // tagid : This is the tag id, there should be at least $mincorrelation
1210 // rows for each tag id.
1211 // correlation : This is the tag id that correlates to the above tagid field.
1212 // correlationid : This is the id of the row in the tag_correlation table that
1213 // relates to the tagid field and will be NULL if there are no
1214 // existing correlations
1215 $sql = 'SELECT pairs.tagid, pairs.correlation, pairs.ocurrences, co.id AS correlationid
1217 SELECT ta.tagid, tb.tagid AS correlation, COUNT(*) AS ocurrences
1218 FROM {tag_instance} ta
1219 JOIN {tag_instance} tb ON (ta.itemtype = tb.itemtype AND ta.itemid = tb.itemid AND ta.tagid <> tb.tagid)
1220 GROUP BY ta.tagid, tb.tagid
1221 HAVING COUNT(*) > :mincorrelation
1223 LEFT JOIN {tag_correlation} co ON co.tagid = pairs.tagid
1224 ORDER BY pairs.tagid ASC, pairs.ocurrences DESC, pairs.correlation ASC';
1225 $rs = $DB->get_recordset_sql($sql, array('mincorrelation' => $mincorrelation));
1227 // Set up an empty tag correlation object
1228 $tagcorrelation = new stdClass
;
1229 $tagcorrelation->id
= null;
1230 $tagcorrelation->tagid
= null;
1231 $tagcorrelation->correlatedtags
= array();
1233 // We store each correlation id in this array so we can remove any correlations
1234 // that no longer exist.
1235 $correlations = array();
1237 // Iterate each row of the result set and build them into tag correlations.
1238 // We add all of a tag's correlations to $tagcorrelation->correlatedtags[]
1239 // then save the $tagcorrelation object
1240 foreach ($rs as $row) {
1241 if ($row->tagid
!= $tagcorrelation->tagid
) {
1242 // The tag id has changed so we have all of the correlations for this tag
1243 $tagcorrelationid = tag_process_computed_correlation($tagcorrelation);
1244 if ($tagcorrelationid) {
1245 $correlations[] = $tagcorrelationid;
1247 // Now we reset the tag correlation object so we can reuse it and set it
1248 // up for the current record.
1249 $tagcorrelation = new stdClass
;
1250 $tagcorrelation->id
= $row->correlationid
;
1251 $tagcorrelation->tagid
= $row->tagid
;
1252 $tagcorrelation->correlatedtags
= array();
1254 //Save the correlation on the tag correlation object
1255 $tagcorrelation->correlatedtags
[] = $row->correlation
;
1257 // Update the current correlation after the last record.
1258 $tagcorrelationid = tag_process_computed_correlation($tagcorrelation);
1259 if ($tagcorrelationid) {
1260 $correlations[] = $tagcorrelationid;
1264 // Close the recordset
1267 // Remove any correlations that weren't just identified
1268 if (empty($correlations)) {
1269 //there are no tag correlations
1270 $DB->delete_records('tag_correlation');
1272 list($sql, $params) = $DB->get_in_or_equal($correlations, SQL_PARAMS_NAMED
, 'param0000', false);
1273 $DB->delete_records_select('tag_correlation', 'id '.$sql, $params);
1278 * This function processes a tag correlation and makes changes in the database as required.
1280 * The tag correlation object needs have both a tagid property and a correlatedtags property that is an array.
1284 * @param stdClass $tagcorrelation
1285 * @return int/bool The id of the tag correlation that was just processed or false.
1287 function tag_process_computed_correlation(stdClass
$tagcorrelation) {
1290 // You must provide a tagid and correlatedtags must be set and be an array
1291 if (empty($tagcorrelation->tagid
) ||
!isset($tagcorrelation->correlatedtags
) ||
!is_array($tagcorrelation->correlatedtags
)) {
1295 $tagcorrelation->correlatedtags
= join(',', $tagcorrelation->correlatedtags
);
1296 if (!empty($tagcorrelation->id
)) {
1297 // The tag correlation already exists so update it
1298 $DB->update_record('tag_correlation', $tagcorrelation);
1300 // This is a new correlation to insert
1301 $tagcorrelation->id
= $DB->insert_record('tag_correlation', $tagcorrelation);
1303 return $tagcorrelation->id
;
1307 * Tasks that should be performed at cron time
1312 function tag_cron() {
1313 tag_compute_correlations();
1318 * Search for tags with names that match some text
1322 * @param string $text escaped string that the tag names will be matched against
1323 * @param bool $ordered If true, tags are ordered by their popularity. If false, no ordering.
1324 * @param int/string $limitfrom (optional, required if $limitnum is set) return a subset of records, starting at this point.
1325 * @param int/string $limitnum (optional, required if $limitfrom is set) return a subset comprising this many records.
1326 * @return array/boolean an array of objects, or false if no records were found or an error occured.
1328 function tag_find_tags($text, $ordered=true, $limitfrom='', $limitnum='') {
1331 $norm = tag_normalize($text, TAG_CASE_LOWER
);
1332 $text = array_shift($norm);
1335 $query = "SELECT tg.id, tg.name, tg.rawname, COUNT(ti.id) AS count
1336 FROM {tag} tg LEFT JOIN {tag_instance} ti ON tg.id = ti.tagid
1337 WHERE tg.name LIKE ?
1338 GROUP BY tg.id, tg.name, tg.rawname
1339 ORDER BY count DESC";
1341 $query = "SELECT tg.id, tg.name, tg.rawname
1343 WHERE tg.name LIKE ?";
1345 $params = array("%{$text}%");
1346 return $DB->get_records_sql($query, $params, $limitfrom , $limitnum);
1350 * Get the name of a tag
1354 * @param mixed $tagids the id of the tag, or an array of ids
1355 * @return mixed string name of one tag, or id-indexed array of strings
1357 function tag_get_name($tagids) {
1360 if (!is_array($tagids)) {
1361 if ($tag = $DB->get_record('tag', array('id'=>$tagids))) {
1367 $tag_names = array();
1368 foreach($DB->get_records_list('tag', 'id', $tagids) as $tag) {
1369 $tag_names[$tag->id
] = $tag->name
;
1376 * Returns the correlated tags of a tag, retrieved from the tag_correlation table. Make sure cron runs, otherwise the table will be
1377 * empty and this function won't return anything.
1379 * Correlated tags are calculated in cron based on existing tag instances.
1381 * This function will return as many entries as there are existing tag instances,
1382 * which means that there will be duplicates for each tag.
1384 * If you need only one record for each correlated tag please call:
1385 * tag_get_related_tags($tag_id, TAG_RELATED_CORRELATED);
1389 * @param int $tag_id is a single tag id
1390 * @param int $notused this argument is no longer used
1391 * @return array an array of tag objects or an empty if no correlated tags are found
1393 function tag_get_correlated($tag_id, $notused = null) {
1396 $tag_correlation = $DB->get_record('tag_correlation', array('tagid'=>$tag_id));
1398 if (!$tag_correlation ||
empty($tag_correlation->correlatedtags
)) {
1402 // this is (and has to) return the same fields as the query in tag_get_tags
1403 $sql = "SELECT ti.id AS taginstanceid, tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering
1405 INNER JOIN {tag_instance} ti ON tg.id = ti.tagid
1406 WHERE tg.id IN ({$tag_correlation->correlatedtags})
1407 ORDER BY ti.ordering ASC";
1408 return $DB->get_records_sql($sql);
1412 * Function that normalizes a list of tag names.
1416 * @param array/string $rawtags array of tags, or a single tag.
1417 * @param int $case case to use for returned value (default: lower case). Either TAG_CASE_LOWER (default) or TAG_CASE_ORIGINAL
1418 * @return array lowercased normalized tags, indexed by the normalized tag, in the same order as the original array.
1419 * (Eg: 'Banana' => 'banana').
1421 function tag_normalize($rawtags, $case = TAG_CASE_LOWER
) {
1423 // cache normalized tags, to prevent costly repeated calls to clean_param
1424 static $cleaned_tags_lc = array(); // lower case - use for comparison
1425 static $cleaned_tags_mc = array(); // mixed case - use for saving to database
1427 if ( !is_array($rawtags) ) {
1428 $rawtags = array($rawtags);
1432 foreach($rawtags as $rawtag) {
1433 $rawtag = trim($rawtag);
1437 if ( !array_key_exists($rawtag, $cleaned_tags_lc) ) {
1438 $cleaned_tags_lc[$rawtag] = core_text
::strtolower( clean_param($rawtag, PARAM_TAG
) );
1439 $cleaned_tags_mc[$rawtag] = clean_param($rawtag, PARAM_TAG
);
1441 if ( $case == TAG_CASE_LOWER
) {
1442 $result[$rawtag] = $cleaned_tags_lc[$rawtag];
1443 } else { // TAG_CASE_ORIGINAL
1444 $result[$rawtag] = $cleaned_tags_mc[$rawtag];
1452 * Count how many records are tagged with a specific tag.
1456 * @param string $record_type record to look for ('post', 'user', etc.)
1457 * @param int $tagid is a single tag id
1458 * @return int number of mathing tags.
1460 function tag_record_count($record_type, $tagid) {
1462 return $DB->count_records('tag_instance', array('itemtype'=>$record_type, 'tagid'=>$tagid));
1466 * Determine if a record is tagged with a specific tag
1470 * @param string $record_type the record type to look for
1471 * @param int $record_id the record id to look for
1472 * @param string $tag a tag name
1473 * @return bool/int true if it is tagged, 0 (false) otherwise
1475 function tag_record_tagged_with($record_type, $record_id, $tag) {
1477 if ($tagid = tag_get_id($tag)) {
1478 return $DB->count_records('tag_instance', array('itemtype'=>$record_type, 'itemid'=>$record_id, 'tagid'=>$tagid));
1480 return 0; // tag doesn't exist
1485 * Flag a tag as inappropriate.
1487 * @param int|array $tagids a single tagid, or an array of tagids
1489 function tag_set_flag($tagids) {
1492 $tagids = (array) $tagids;
1494 // Use the tagids to create a select statement to be used later.
1495 list($tagsql, $tagparams) = $DB->get_in_or_equal($tagids, SQL_PARAMS_NAMED
);
1497 // Update all the tags to flagged.
1498 $sql = "UPDATE {tag}
1499 SET flag = flag + 1, timemodified = :time
1502 // Update all the tags.
1503 $DB->execute($sql, array_merge(array('time' => time()), $tagparams));
1505 // Get all the tags.
1506 if ($tags = $DB->get_records_select('tag', 'id '. $tagsql, $tagparams, 'id ASC')) {
1507 // Loop through and fire an event for each tag that it was flagged.
1508 foreach ($tags as $tag) {
1509 $event = \core\event\tag_flagged
::create(array(
1510 'objectid' => $tag->id
,
1511 'relateduserid' => $tag->userid
,
1512 'context' => context_system
::instance(),
1514 'name' => $tag->name
,
1515 'rawname' => $tag->rawname
1519 $event->add_record_snapshot('tag', $tag);
1526 * Remove the inappropriate flag on a tag.
1528 * @param int|array $tagids a single tagid, or an array of tagids
1530 function tag_unset_flag($tagids) {
1533 $tagids = (array) $tagids;
1535 // Use the tagids to create a select statement to be used later.
1536 list($tagsql, $tagparams) = $DB->get_in_or_equal($tagids, SQL_PARAMS_NAMED
);
1538 // Update all the tags to unflagged.
1539 $sql = "UPDATE {tag}
1540 SET flag = 0, timemodified = :time
1543 // Update all the tags.
1544 $DB->execute($sql, array_merge(array('time' => time()), $tagparams));
1546 // Get all the tags.
1547 if ($tags = $DB->get_records_select('tag', 'id '. $tagsql, $tagparams, 'id ASC')) {
1548 // Loop through and fire an event for each tag that it was unflagged.
1549 foreach ($tags as $tag) {
1550 $event = \core\event\tag_unflagged
::create(array(
1551 'objectid' => $tag->id
,
1552 'relateduserid' => $tag->userid
,
1553 'context' => context_system
::instance(),
1555 'name' => $tag->name
,
1556 'rawname' => $tag->rawname
1559 $event->add_record_snapshot('tag', $tag);
1566 * Return a list of page types
1570 * @param string $pagetype current page type
1571 * @param stdClass $parentcontext Block's parent context
1572 * @param stdClass $currentcontext Current context of block
1574 function tag_page_type_list($pagetype, $parentcontext, $currentcontext) {
1576 'tag-*'=>get_string('page-tag-x', 'tag'),
1577 'tag-index'=>get_string('page-tag-index', 'tag'),
1578 'tag-search'=>get_string('page-tag-search', 'tag'),
1579 'tag-manage'=>get_string('page-tag-manage', 'tag')