Skip to content

Commit

Permalink
MDL-61363 tag: add get_items_tags function
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwyllie committed Mar 7, 2018
1 parent 064f6b5 commit 694710d
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions tag/classes/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,19 +638,19 @@ protected function update_instance_ordering($instanceid, $ordering) {
}

/**
* Get the array of core_tag_tag objects associated with an item (instances).
* Get the array of core_tag_tag objects associated with a list of items.
*
* Use {@link core_tag_tag::get_item_tags_array()} if you wish to get the same data as simple array.
*
* @param string $component component responsible for tagging. For BC it can be empty but in this case the
* query will be slow because DB index will not be used.
* @param string $itemtype type of the tagged item
* @param int $itemid
* @param int[] $itemids
* @param int $standardonly wether to return only standard tags or any
* @param int $tiuserid tag instance user id, only needed for tag areas with user tagging
* @return core_tag_tag[] each object contains additional fields taginstanceid, taginstancecontextid and ordering
*/
public static function get_item_tags($component, $itemtype, $itemid, $standardonly = self::BOTH_STANDARD_AND_NOT,
public static function get_items_tags($component, $itemtype, $itemids, $standardonly = self::BOTH_STANDARD_AND_NOT,
$tiuserid = 0) {
global $DB;

Expand All @@ -659,34 +659,59 @@ public static function get_item_tags($component, $itemtype, $itemid, $standardon
return array();
}

if (empty($itemids)) {
return array();
}

$standardonly = (int)$standardonly; // In case somebody passed bool.

list($idsql, $params) = $DB->get_in_or_equal($itemids, SQL_PARAMS_NAMED);
// Note: if the fields in this query are changed, you need to do the same changes in core_tag_tag::get_correlated_tags().
$sql = "SELECT ti.id AS taginstanceid, tg.id, tg.isstandard, tg.name, tg.rawname, tg.flag,
tg.tagcollid, ti.ordering, ti.contextid AS taginstancecontextid
tg.tagcollid, ti.ordering, ti.contextid AS taginstancecontextid, ti.itemid
FROM {tag_instance} ti
JOIN {tag} tg ON tg.id = ti.tagid
WHERE ti.itemtype = :itemtype AND ti.itemid = :itemid ".
WHERE ti.itemtype = :itemtype AND ti.itemid $idsql ".
($component ? "AND ti.component = :component " : "").
($tiuserid ? "AND ti.tiuserid = :tiuserid " : "").
(($standardonly == self::STANDARD_ONLY) ? "AND tg.isstandard = 1 " : "").
(($standardonly == self::NOT_STANDARD_ONLY) ? "AND tg.isstandard = 0 " : "").
"ORDER BY ti.ordering ASC, ti.id";

$params = array();
$params['itemtype'] = $itemtype;
$params['itemid'] = $itemid;
$params['component'] = $component;
$params['tiuserid'] = $tiuserid;

$records = $DB->get_records_sql($sql, $params);
$result = array();
foreach ($itemids as $itemid) {
$result[$itemid] = [];
}
foreach ($records as $id => $record) {
$result[$id] = new static($record);
$result[$record->itemid][$id] = new static($record);
}
return $result;
}

/**
* Get the array of core_tag_tag objects associated with an item (instances).
*
* Use {@link core_tag_tag::get_item_tags_array()} if you wish to get the same data as simple array.
*
* @param string $component component responsible for tagging. For BC it can be empty but in this case the
* query will be slow because DB index will not be used.
* @param string $itemtype type of the tagged item
* @param int $itemid
* @param int $standardonly wether to return only standard tags or any
* @param int $tiuserid tag instance user id, only needed for tag areas with user tagging
* @return core_tag_tag[] each object contains additional fields taginstanceid, taginstancecontextid and ordering
*/
public static function get_item_tags($component, $itemtype, $itemid, $standardonly = self::BOTH_STANDARD_AND_NOT,
$tiuserid = 0) {
$tagobjects = static::get_items_tags($component, $itemtype, [$itemid], $standardonly, $tiuserid);
return empty($tagobjects) ? [] : $tagobjects[$itemid];
}

/**
* Returns the list of display names of the tags that are associated with an item
*
Expand Down Expand Up @@ -1158,7 +1183,7 @@ public function get_correlated_tags($keepduplicates = false) {

// This is (and has to) return the same fields as the query in core_tag_tag::get_item_tags().
$sql = "SELECT ti.id AS taginstanceid, tg.id, tg.isstandard, tg.name, tg.rawname, tg.flag,
tg.tagcollid, ti.ordering, ti.contextid AS taginstancecontextid
tg.tagcollid, ti.ordering, ti.contextid AS taginstancecontextid, ti.itemid
FROM {tag} tg
INNER JOIN {tag_instance} ti ON tg.id = ti.tagid
WHERE tg.id $query AND tg.id <> ? AND tg.tagcollid = ?
Expand Down

0 comments on commit 694710d

Please sign in to comment.