Skip to content

Commit

Permalink
MDL-40908 core_tag: created a 'tag_unflagged' event
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Apr 4, 2014
1 parent 6298494 commit 097bd4c
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 10 deletions.
1 change: 1 addition & 0 deletions lang/en/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
$string['errorupdatingrecord'] = 'Error updating tag record';
$string['eventitemtagged'] = 'Item tagged';
$string['eventtagflagged'] = 'Tag flagged';
$string['eventtagunflagged'] = 'Tag unflagged';
$string['eventtagupdated'] = 'Tag updated';
$string['flag'] = 'Flag';
$string['flagasinappropriate'] = 'Flag as inappropriate';
Expand Down
82 changes: 82 additions & 0 deletions lib/classes/event/tag_unflagged.php
Original file line number Diff line number Diff line change
@@ -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 unflagged 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_unflagged 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('eventtagunflagged', 'tag');
}

/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return 'The tag with the id ' . $this->objectid . ' was unflagged 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.');
}
}
}
41 changes: 31 additions & 10 deletions tag/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1301,24 +1301,45 @@ function tag_set_flag($tagids) {
}

/**
* Remove the inapropriate flag on a tag
* Remove the inappropriate flag on a tag.
*
* @package core_tag
* @access private
* @param int|array $tagids a single tagid, or an array of tagids
* @return bool true if function succeeds, false otherwise
* @param int|array $tagids a single tagid, or an array of tagids
*/
function tag_unset_flag($tagids) {
global $DB;

if ( is_array($tagids) ) {
$tagids = implode(',', $tagids);
$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 unflagged.
$sql = "UPDATE {tag}
SET flag = 0, 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 unflagged.
foreach ($tags as $tag) {
$event = \core\event\tag_unflagged::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();
}
}
$timemodified = time();
return $DB->execute("UPDATE {tag} SET flag = 0, timemodified = ? WHERE id IN ($tagids)", array($timemodified));
}


/**
* Return a list of page types
*
Expand Down
53 changes: 53 additions & 0 deletions tag/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,57 @@ public function test_tag_flagged() {
$expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag2->id, $tag2->id, '', '2');
$this->assertEventLegacyLogData($expected, $event);
}

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

$this->setAdminUser();

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

// Flag it.
tag_set_flag($tag->id);

// Trigger and capture the event for unsetting the flag of a tag.
$sink = $this->redirectEvents();
tag_unset_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(0, $tag->flag);

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\tag_unflagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());

// Set the flag back for both.
tag_set_flag(array($tag->id, $tag2->id));

// Trigger and capture the event for unsetting the flag for multiple tags.
$sink = $this->redirectEvents();
tag_unset_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(0, $tag->flag);
$tag2 = $DB->get_record('tag', array('id' => $tag2->id));
$this->assertEquals(0, $tag2->flag);

// Confirm the events.
$event = $events[0];
$this->assertInstanceOf('\core\event\tag_unflagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());

$event = $events[1];
$this->assertInstanceOf('\core\event\tag_unflagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
}

0 comments on commit 097bd4c

Please sign in to comment.