mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 04:22:07 +02:00
MDL-40908 core_tag: replaced 'flag' add_to_log call with an event
This commit is contained in:
parent
cfa2c6e198
commit
6298494d73
@ -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';
|
||||
|
91
lib/classes/event/tag_flagged.php
Normal file
91
lib/classes/event/tag_flagged.php
Normal file
@ -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
tag/lib.php
42
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user