diff --git a/lang/en/tag.php b/lang/en/tag.php index eef906d2121..236a3a95ff2 100644 --- a/lang/en/tag.php +++ b/lang/en/tag.php @@ -37,6 +37,7 @@ $string['entertags'] = 'Enter tags separated by commas'; $string['errordeleting'] = 'Error deleting tag with id {$a}, please report to your system administrator.'; $string['errortagfrontpage'] = 'Tagging the site main page is not allowed'; $string['errorupdatingrecord'] = 'Error updating tag record'; +$string['eventtagupdated'] = 'Tag updated'; $string['flag'] = 'Flag'; $string['flagasinappropriate'] = 'Flag as inappropriate'; $string['helprelatedtags'] = 'Comma separated related tags'; diff --git a/lib/classes/event/tag_updated.php b/lib/classes/event/tag_updated.php new file mode 100644 index 00000000000..83810977771 --- /dev/null +++ b/lib/classes/event/tag_updated.php @@ -0,0 +1,107 @@ +. + +/** + * Tag updated 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_updated extends base { + + /** @var array The legacy log data. */ + private $legacylogdata; + + /** + * 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('eventtagupdated', 'tag'); + } + + /** + * Returns non-localised description of what happened. + * + * @return string + */ + public function get_description() { + return 'The tag with the id ' . $this->objectid . ' was updated by the user with the id ' . $this->userid; + } + + /** + * Set the legacy data used for add_to_log(). + * + * @param array $logdata + */ + public function set_legacy_logdata($logdata) { + $this->legacylogdata = $logdata; + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + if (isset($this->legacylogdata)) { + return $this->legacylogdata; + } + + return array($this->courseid, 'tag', 'update', 'index.php?id='. $this->objectid, $this->other['name']); + } + + /** + * 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/edit.php b/tag/edit.php index e9449f7c925..3277cd75dac 100644 --- a/tag/edit.php +++ b/tag/edit.php @@ -119,27 +119,22 @@ if ($tagnew = $tagform->get_data()) { $tagnew = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); - tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat); + if ($tag->description != $tagnew->description) { + tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat); + } $tagnew->timemodified = time(); if (has_capability('moodle/tag:manage', $systemcontext)) { - // rename tag - if(!tag_rename($tag->id, $tagnew->rawname)) { - print_error('errorupdatingrecord', 'tag'); + // Check if we need to rename the tag. + if (isset($tagnew->name) && ($tag->name != $tagnew->name)) { + // Rename the tag. + if (!tag_rename($tag->id, $tagnew->rawname)) { + print_error('errorupdatingrecord', 'tag'); + } } } - //log tag changes activity - //if tag name exist from form, renaming is allow. record log action as rename - //otherwise, record log action as update - if (isset($tagnew->name) && ($tag->name != $tagnew->name)){ - add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name . '->'. $tagnew->name); - - } elseif ($tag->description != $tagnew->description) { - add_to_log($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $tag->name); - } - //updated related tags tag_set('tag', $tagnew->id, explode(',', trim($tagnew->relatedtags)), 'core', $systemcontext); //print_object($tagnew); die(); diff --git a/tag/lib.php b/tag/lib.php index daedf66cd4b..7168cc35cfa 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -240,10 +240,23 @@ function tag_set_delete($record_type, $record_id, $tag, $component = null, $cont function tag_type_set($tagid, $type) { global $DB; - if ($tag = $DB->get_record('tag', array('id'=>$tagid), 'id')) { + if ($tag = $DB->get_record('tag', array('id' => $tagid), 'id, userid, name, rawname')) { $tag->tagtype = $type; $tag->timemodified = time(); - return $DB->update_record('tag', $tag); + $DB->update_record('tag', $tag); + + $event = \core\event\tag_updated::create(array( + 'objectid' => $tag->id, + 'relateduserid' => $tag->userid, + 'context' => context_system::instance(), + 'other' => array( + 'name' => $tag->name, + 'rawname' => $tag->rawname + ) + )); + $event->trigger(); + + return true; } return false; } @@ -263,12 +276,26 @@ function tag_type_set($tagid, $type) { function tag_description_set($tagid, $description, $descriptionformat) { global $DB; - if ($tag = $DB->get_record('tag', array('id'=>$tagid),'id')) { + if ($tag = $DB->get_record('tag', array('id' => $tagid), 'id, userid, name, rawname')) { $tag->description = $description; $tag->descriptionformat = $descriptionformat; $tag->timemodified = time(); - return $DB->update_record('tag', $tag); + $DB->update_record('tag', $tag); + + $event = \core\event\tag_updated::create(array( + 'objectid' => $tag->id, + 'relateduserid' => $tag->userid, + 'context' => context_system::instance(), + 'other' => array( + 'name' => $tag->name, + 'rawname' => $tag->rawname + ) + )); + $event->trigger(); + + return true; } + return false; } @@ -565,7 +592,7 @@ function tag_get_related_tags_csv($related_tags, $html=TAG_RETURN_HTML) { * @return bool true on success, false otherwise */ function tag_rename($tagid, $newrawname) { - global $DB; + global $COURSE, $DB; $norm = tag_normalize($newrawname, TAG_CASE_ORIGINAL); if (! $newrawname_clean = array_shift($norm) ) { @@ -583,11 +610,28 @@ function tag_rename($tagid, $newrawname) { } } - if ($tag = tag_get('id', $tagid, 'id, name, rawname')) { - $tag->rawname = $newrawname_clean; - $tag->name = $newname_clean; + if ($tag = tag_get('id', $tagid, 'id, userid, name, rawname')) { + // Store the name before we change it. + $oldname = $tag->name; + + $tag->rawname = $newrawname_clean; + $tag->name = $newname_clean; $tag->timemodified = time(); - return $DB->update_record('tag', $tag); + $DB->update_record('tag', $tag); + + $event = \core\event\tag_updated::create(array( + 'objectid' => $tag->id, + 'relateduserid' => $tag->userid, + 'context' => context_system::instance(), + 'other' => array( + 'name' => $newname_clean, + 'rawname' => $newrawname_clean + ) + )); + $event->set_legacy_logdata(array($COURSE->id, 'tag', 'update', 'index.php?id='. $tag->id, $oldname . '->'. $tag->name)); + $event->trigger(); + + return true; } return false; } diff --git a/tag/tests/events_test.php b/tag/tests/events_test.php new file mode 100644 index 00000000000..2a5d8f9029e --- /dev/null +++ b/tag/tests/events_test.php @@ -0,0 +1,96 @@ +. + +/** + * Events tests. + * + * @package core_tag + * @category test + * @copyright 2014 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/tag/lib.php'); + +class core_tag_events_testcase extends advanced_testcase { + + /** + * Test set up. + * + * This is executed before running any test in this file. + */ + public function setUp() { + $this->resetAfterTest(); + } + + /** + * Test the tag updated event. + */ + public function test_tag_updated() { + $this->setAdminUser(); + + // Save the system context. + $systemcontext = context_system::instance(); + + // Create a tag we are going to update. + $tag = $this->getDataGenerator()->create_tag(); + + // Store the name before we change it. + $oldname = $tag->name; + + // Trigger and capture the event when renaming a tag. + $sink = $this->redirectEvents(); + tag_rename($tag->id, 'newname'); + // Update the tag's name since we have renamed it. + $tag->name = 'newname'; + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\tag_updated', $event); + $this->assertEquals($systemcontext, $event->get_context()); + $expected = array(SITEID, 'tag', 'update', 'index.php?id=' . $tag->id, $oldname . '->'. $tag->name); + $this->assertEventLegacyLogData($expected, $event); + + // Trigger and capture the event when setting the type of a tag. + $sink = $this->redirectEvents(); + tag_type_set($tag->id, 'official'); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\tag_updated', $event); + $this->assertEquals($systemcontext, $event->get_context()); + $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name); + $this->assertEventLegacyLogData($expected, $event); + + // Trigger and capture the event for setting the description of a tag. + $sink = $this->redirectEvents(); + tag_description_set($tag->id, 'description', FORMAT_MOODLE); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the event data is valid. + $this->assertInstanceOf('\core\event\tag_updated', $event); + $this->assertEquals($systemcontext, $event->get_context()); + $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name); + $this->assertEventLegacyLogData($expected, $event); + } +}