Skip to content

Commit

Permalink
MDL-61363 tag: add additional tag instance delete functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwyllie committed Mar 7, 2018
1 parent 98aaa34 commit 064f6b5
Showing 1 changed file with 60 additions and 15 deletions.
75 changes: 60 additions & 15 deletions tag/classes/tag.php
Expand Up @@ -499,6 +499,64 @@ protected function delete_instance($component, $itemtype, $itemid, $tiuserid = 0
$this->delete_instance_as_record($taginstance, true);
}

/**
* Bulk delete all tag instances.
*
* @param stdClass[] $taginstances A list of tag_instance records to delete. Each
* record must also contain the name and rawname
* columns from the related tag record.
*/
public static function delete_instances_as_record(array $taginstances) {
global $DB;

if (empty($taginstances)) {
return;
}

$taginstanceids = array_map(function($taginstance) {
return $taginstance->id;
}, $taginstances);
// Now remove all the tag instances.
$DB->delete_records_list('tag_instance', 'id', $taginstanceids);
// Save the system context in case the 'contextid' column in the 'tag_instance' table is null.
$syscontextid = context_system::instance()->id;
// Loop through the tag instances and fire an 'tag_removed' event.
foreach ($taginstances as $taginstance) {
// We can not fire an event with 'null' as the contextid.
if (is_null($taginstance->contextid)) {
$taginstance->contextid = $syscontextid;
}

// Trigger tag removed event.
\core\event\tag_removed::create_from_tag_instance($taginstance, $taginstance->name,
$taginstance->rawname, true)->trigger();
}
}

/**
* Bulk delete all tag instances by tag id.
*
* @param int[] $taginstanceids List of tag instance ids to be deleted.
*/
public static function delete_instances_by_id(array $taginstanceids) {
global $DB;

if (empty($taginstanceids)) {
return;
}

list($idsql, $params) = $DB->get_in_or_equal($taginstanceids);
$sql = "SELECT ti.*, t.name, t.rawname, t.isstandard
FROM {tag_instance} ti
JOIN {tag} t
ON ti.tagid = t.id
WHERE ti.id {$idsql}";

if ($taginstances = $DB->get_records_sql($sql, $params)) {
static::delete_instances_as_record($taginstances);
}
}

/**
* Bulk delete all tag instances for a component or tag area
*
Expand All @@ -523,22 +581,9 @@ public static function delete_instances($component, $itemtype = null, $contextid
$sql .= " AND ti.itemtype = :itemtype";
$params['itemtype'] = $itemtype;
}
if ($taginstances = $DB->get_records_sql($sql, $params)) {
// Now remove all the tag instances.
$DB->delete_records('tag_instance', $params);
// Save the system context in case the 'contextid' column in the 'tag_instance' table is null.
$syscontextid = context_system::instance()->id;
// Loop through the tag instances and fire an 'tag_removed' event.
foreach ($taginstances as $taginstance) {
// We can not fire an event with 'null' as the contextid.
if (is_null($taginstance->contextid)) {
$taginstance->contextid = $syscontextid;
}

// Trigger tag removed event.
\core\event\tag_removed::create_from_tag_instance($taginstance, $taginstance->name,
$taginstance->rawname, true)->trigger();
}
if ($taginstances = $DB->get_records_sql($sql, $params)) {
static::delete_instances_as_record($taginstances);
}
}

Expand Down

0 comments on commit 064f6b5

Please sign in to comment.