diff --git a/lang/en/tag.php b/lang/en/tag.php index d2ec5fa9d41..c3e65678db7 100644 --- a/lang/en/tag.php +++ b/lang/en/tag.php @@ -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['eventtagflagged'] = 'Tag flagged'; $string['eventtagupdated'] = 'Tag updated'; $string['flag'] = 'Flag'; $string['flagasinappropriate'] = 'Flag as inappropriate'; diff --git a/lib/classes/event/tag_flagged.php b/lib/classes/event/tag_flagged.php new file mode 100644 index 00000000000..18adc67e26a --- /dev/null +++ b/lib/classes/event/tag_flagged.php @@ -0,0 +1,91 @@ +. + +/** + * 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 + * @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.'); + } + } +} diff --git a/tag/lib.php b/tag/lib.php index b9d8660c897..5e467e52c03 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -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(); + } } } diff --git a/tag/tests/events_test.php b/tag/tests/events_test.php index 2f0902c73e7..29d16452356 100644 --- a/tag/tests/events_test.php +++ b/tag/tests/events_test.php @@ -136,4 +136,60 @@ class core_tag_events_testcase extends advanced_testcase { $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); + } } diff --git a/tag/user.php b/tag/user.php index f6840f701f9..983fdb95a20 100644 --- a/tag/user.php +++ b/tag/user.php @@ -47,8 +47,6 @@ switch ($action) { 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);