MDL-40908 core_tag: created a 'tag_deleted' event

This commit is contained in:
Mark Nelson 2014-01-20 22:08:50 -08:00
parent 097bd4cdae
commit db456ec1a5
5 changed files with 239 additions and 44 deletions

View File

@ -38,6 +38,7 @@ $string['errordeleting'] = 'Error deleting tag with id {$a}, please report to yo
$string['errortagfrontpage'] = 'Tagging the site main page is not allowed';
$string['errorupdatingrecord'] = 'Error updating tag record';
$string['eventitemtagged'] = 'Item tagged';
$string['eventtagdeleted'] = 'Tag deleted';
$string['eventtagflagged'] = 'Tag flagged';
$string['eventtagunflagged'] = 'Tag unflagged';
$string['eventtagupdated'] = 'Tag updated';

View File

@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tag deleted event.
*
* @property-read array $other {
* Extra information about event.
*
* - string name: the name of the tag.
* - string rawname: the raw name of the tag.
* }
*
* @package core
* @copyright 2014 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();
class tag_deleted extends base {
/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = 'tag';
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventtagdeleted', 'tag');
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return 'The tag with the id ' . $this->objectid . ' was deleted by the user with the id ' . $this->userid;
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['name'])) {
throw new \coding_exception('The name must be set in $other.');
}
if (!isset($this->other['rawname'])) {
throw new \coding_exception('The rawname must be set in $other.');
}
}
}

View File

@ -267,32 +267,30 @@ function coursetag_store_keywords($tags, $courseid, $userid=0, $tagtype='officia
* @param int $courseid the course that the tag is associated with
*/
function coursetag_delete_keyword($tagid, $userid, $courseid) {
global $DB;
global $CFG, $DB;
$sql = "SELECT COUNT(*)
FROM {tag_instance}
WHERE tagid = $tagid
AND tiuserid = $userid
AND itemtype = 'course'
AND itemid = $courseid";
if ($DB->count_records_sql($sql) == 1) {
$sql = "tagid = $tagid
AND tiuserid = $userid
AND itemtype = 'course'
AND itemid = $courseid";
$DB->delete_records_select('tag_instance', $sql);
// if there are no other instances of the tag then consider deleting the tag as well
$sql = "SELECT *
FROM {tag_instance}
WHERE tagid = :tagid
AND tiuserid = :userid
AND itemtype = 'course'
AND itemid = :courseid";
if ($DB->record_exists_sql($sql, array('tagid' => $tagid, 'userid' => $userid, 'courseid' => $courseid))) {
$sql = "tagid = :tagid
AND tiuserid = :userid
AND itemtype = 'course'
AND itemid = :courseid";
$DB->delete_records_select('tag_instance', $sql, array('tagid' => $tagid, 'userid' => $userid, 'courseid' => $courseid));
// If there are no other instances of the tag then consider deleting the tag as well.
if (!$DB->record_exists('tag_instance', array('tagid' => $tagid))) {
// if the tag is a personal tag then delete it - don't do official tags
if ($DB->record_exists('tag', array('id' => $tagid, 'tagtype' => 'default'))) {
$DB->delete_records('tag', array('id' => $tagid, 'tagtype' => 'default'));
// If the tag is a personal tag then delete it - don't do official tags.
if ($tag = $DB->get_record('tag', array('id' => $tagid, 'tagtype' => 'default'))) {
tag_delete($tagid);
}
}
} else {
print_error("errordeleting", 'tag', '', $tagid);
}
}
/**
@ -340,18 +338,10 @@ function coursetag_get_tagged_courses($tagid) {
function coursetag_delete_course_tags($courseid, $showfeedback=false) {
global $DB, $OUTPUT;
if ($tags = $DB->get_records_select('tag_instance', "itemtype='course' AND itemid=:courseid", array('courseid'=>$courseid))) {
foreach ($tags as $tag) {
//delete the course tag instance record
$DB->delete_records('tag_instance', array('tagid'=>$tag->tagid, 'itemtype'=>'course', 'itemid'=> $courseid));
// delete tag if there are no other tag_instance entries now
if (!($DB->record_exists('tag_instance', array('tagid'=>$tag->tagid)))) {
$DB->delete_records('tag', array('id'=>$tag->tagid));
// Delete files
$fs = get_file_storage();
$fs->delete_area_files(context_system::instance()->id, 'tag', 'description', $tag->tagid);
}
}
if ($taginstances = $DB->get_fieldset_select('tag_instance', 'tagid', "itemtype = 'course' AND itemid = :courseid",
array('courseid' => $courseid))) {
tag_delete(array_values($taginstances));
}
if ($showfeedback) {

View File

@ -653,27 +653,53 @@ function tag_delete($tagids) {
$tagids = array($tagids);
}
$success = true;
$context = context_system::instance();
foreach ($tagids as $tagid) {
if (is_null($tagid)) { // can happen if tag doesn't exists
continue;
}
// only delete the main entry if there were no problems deleting all the
// instances - that (and the fact we won't often delete lots of tags)
// is the reason for not using $DB->delete_records_select()
if ($DB->delete_records('tag_instance', array('tagid'=>$tagid)) && $DB->delete_records('tag_correlation', array('tagid' => $tagid))) {
$success &= (bool) $DB->delete_records('tag', array('id'=>$tagid));
// Use the tagids to create a select statement to be used later.
list($tagsql, $tagparams) = $DB->get_in_or_equal($tagids);
// Store the tags we are going to delete.
$tags = $DB->get_records_select('tag', 'id ' . $tagsql, $tagparams);
// Delete all the tag instances.
$select = 'WHERE tagid ' . $tagsql;
$sql = "DELETE FROM {tag_instance} $select";
$DB->execute($sql, $tagparams);
// Delete all the tag correlations.
$sql = "DELETE FROM {tag_correlation} $select";
$DB->execute($sql, $tagparams);
// Delete all the tags.
$select = 'WHERE id ' . $tagsql;
$sql = "DELETE FROM {tag} $select";
$DB->execute($sql, $tagparams);
// Fire an event that these tags were deleted.
if ($tags) {
$context = context_system::instance();
foreach ($tags as $tag) {
// Delete all files associated with this tag
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'tag', 'description', $tagid);
$files = $fs->get_area_files($context->id, 'tag', 'description', $tag->id);
foreach ($files as $file) {
$file->delete();
}
// Trigger an event for deleting this tag.
$event = \core\event\tag_deleted::create(array(
'objectid' => $tag->id,
'relateduserid' => $tag->userid,
'context' => $context,
'other' => array(
'name' => $tag->name,
'rawname' => $tag->rawname
)
));
$event->add_record_snapshot('tag', $tag);
$event->trigger();
}
}
return $success;
return true;
}
/**

View File

@ -245,4 +245,100 @@ class core_tag_events_testcase extends advanced_testcase {
$this->assertInstanceOf('\core\event\tag_unflagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
/**
* Test the tag deleted event.
*/
public function test_tag_deleted() {
global $DB;
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create tag we are going to delete.
$tag = $this->getDataGenerator()->create_tag();
// Trigger and capture the event for deleting a tag.
$sink = $this->redirectEvents();
tag_delete($tag->id);
$events = $sink->get_events();
$event = reset($events);
// Check that the tag was deleted and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
// Create two tags we are going to delete to ensure passing multiple tags work.
$tag = $this->getDataGenerator()->create_tag();
$tag2 = $this->getDataGenerator()->create_tag();
// Trigger and capture the events for deleting multiple tags.
$sink = $this->redirectEvents();
tag_delete(array($tag->id, $tag2->id));
$events = $sink->get_events();
// Check that the tags were deleted and the events data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
foreach ($events as $event) {
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
// Create another tag to delete.
$tag = $this->getDataGenerator()->create_tag();
// Add a tag instance to a course.
tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
// Trigger and capture the event for deleting a personal tag for a user for a course.
$sink = $this->redirectEvents();
coursetag_delete_keyword($tag->id, 2, $course->id);
$events = $sink->get_events();
$event = reset($events);
// Check that the tag was deleted and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
// Create a new tag we are going to delete.
$tag = $this->getDataGenerator()->create_tag();
// Add the tag instance to the course again as it was deleted.
tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
// Trigger and capture the event for deleting all tags in a course.
$sink = $this->redirectEvents();
coursetag_delete_course_tags($course->id);
$events = $sink->get_events();
$event = reset($events);
// Check that the tag was deleted and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
// Create two tags we are going to delete to ensure passing multiple tags work.
$tag = $this->getDataGenerator()->create_tag();
$tag2 = $this->getDataGenerator()->create_tag();
// Add multiple tag instances now and check that it still works.
tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
tag_assign('course', $course->id, $tag2->id, 0, 2, 'course', context_course::instance($course->id)->id);
// Trigger and capture the event for deleting all tags in a course.
$sink = $this->redirectEvents();
coursetag_delete_course_tags($course->id);
$events = $sink->get_events();
// Check that the tags were deleted and the events data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
foreach ($events as $event) {
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
}
}