Skip to content

Commit

Permalink
MDL-40908 core_tag: replaced 'flag' add_to_log call with an event
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Apr 4, 2014
1 parent cfa2c6e commit 6298494
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 12 deletions.
1 change: 1 addition & 0 deletions lang/en/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
$string['errortagfrontpage'] = 'Tagging the site main page is not allowed';
$string['errorupdatingrecord'] = 'Error updating tag record';
$string['eventitemtagged'] = 'Item tagged';
$string['eventtagflagged'] = 'Tag flagged';
$string['eventtagupdated'] = 'Tag updated';
$string['flag'] = 'Flag';
$string['flagasinappropriate'] = 'Flag as inappropriate';
Expand Down
91 changes: 91 additions & 0 deletions lib/classes/event/tag_flagged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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 flagged 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_flagged extends base {

/**
* Initialise the event data.
*/
protected function init() {
$this->data['objecttable'] = 'tag';
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventtagflagged', 'tag');
}

/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return 'The tag with the id ' . $this->objectid . ' was flagged by the user with the id ' . $this->userid;
}

/**
* Return legacy data for add_to_log().
*
* @return array
*/
protected function get_legacy_logdata() {
return array(SITEID, 'tag', 'flag', 'index.php?id='. $this->objectid, $this->objectid, '', $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.');
}
}
}
42 changes: 32 additions & 10 deletions tag/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1260,21 +1260,43 @@ function tag_record_tagged_with($record_type, $record_id, $tag) {
}

/**
* Flag a tag as inapropriate
* Flag a tag as inappropriate.
*
* @package core_tag
* @access private
* @param int|array $tagids a single tagid, or an array of tagids
* @param int|array $tagids a single tagid, or an array of tagids
*/
function tag_set_flag($tagids) {
global $DB;

$tagids = (array)$tagids;
foreach ($tagids as $tagid) {
$tag = $DB->get_record('tag', array('id'=>$tagid), 'id, flag');
$tag->flag++;
$tag->timemodified = time();
$DB->update_record('tag', $tag);
$tagids = (array) $tagids;

// Use the tagids to create a select statement to be used later.
list($tagsql, $tagparams) = $DB->get_in_or_equal($tagids, SQL_PARAMS_NAMED);

// Update all the tags to flagged.
$sql = "UPDATE {tag}
SET flag = flag + 1, timemodified = :time
WHERE id $tagsql";

// Update all the tags.
$DB->execute($sql, array_merge(array('time' => time()), $tagparams));

// Get all the tags.
if ($tags = $DB->get_records_select('tag', 'id '. $tagsql, $tagparams, 'id ASC')) {
// Loop through and fire an event for each tag that it was flagged.
foreach ($tags as $tag) {
$event = \core\event\tag_flagged::create(array(
'objectid' => $tag->id,
'relateduserid' => $tag->userid,
'context' => context_system::instance(),
'other' => array(
'name' => $tag->name,
'rawname' => $tag->rawname
)

));
$event->add_record_snapshot('tag', $tag);
$event->trigger();
}
}
}

Expand Down
56 changes: 56 additions & 0 deletions tag/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,60 @@ public function test_item_tagged() {
$expected = null;
$this->assertEventLegacyLogData($expected, $event);
}

/**
* Test the tag flagged event.
*/
public function test_tag_flagged() {
global $DB;

$this->setAdminUser();

// Create tags we are going to flag.
$tag = $this->getDataGenerator()->create_tag();
$tag2 = $this->getDataGenerator()->create_tag();

// Trigger and capture the event for setting the flag of a tag.
$sink = $this->redirectEvents();
tag_set_flag($tag->id);
$events = $sink->get_events();
$event = reset($events);

// Check that the flag was updated.
$tag = $DB->get_record('tag', array('id' => $tag->id));
$this->assertEquals(1, $tag->flag);

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\tag_flagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
$this->assertEventLegacyLogData($expected, $event);

// Unset the flag for both (though by default tag2 should have been created with 0 already).
tag_unset_flag(array($tag->id, $tag2->id));

// Trigger and capture the event for setting the flag for multiple tags.
$sink = $this->redirectEvents();
tag_set_flag(array($tag->id, $tag2->id));
$events = $sink->get_events();

// Check that the flags were updated.
$tag = $DB->get_record('tag', array('id' => $tag->id));
$this->assertEquals(1, $tag->flag);
$tag2 = $DB->get_record('tag', array('id' => $tag2->id));
$this->assertEquals(1, $tag2->flag);

// Confirm the events.
$event = $events[0];
$this->assertInstanceOf('\core\event\tag_flagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
$this->assertEventLegacyLogData($expected, $event);

$event = $events[1];
$this->assertInstanceOf('\core\event\tag_flagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag2->id, $tag2->id, '', '2');
$this->assertEventLegacyLogData($expected, $event);
}
}
2 changes: 0 additions & 2 deletions tag/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
case 'flaginappropriate':

$tagid = tag_get_id($tag);
// Add flaging action to logs
add_to_log(SITEID, 'tag', 'flag', 'index.php?id='. $tagid, $tagid, '', $USER->id);

tag_set_flag($tagid);

Expand Down

0 comments on commit 6298494

Please sign in to comment.