$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';
--- /dev/null
+<?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.');
+ }
+ }
+}
}
/**
- * 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
*
$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());
+ }
}