mirror of
https://github.com/moodle/moodle.git
synced 2025-04-14 13:02:07 +02:00
MDL-40908 core_tag: replaced 'update' add_to_log calls with an event
This commit is contained in:
parent
1a727e121e
commit
09fce43469
@ -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';
|
||||
|
107
lib/classes/event/tag_updated.php
Normal file
107
lib/classes/event/tag_updated.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?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 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 <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_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.');
|
||||
}
|
||||
}
|
||||
}
|
23
tag/edit.php
23
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();
|
||||
|
62
tag/lib.php
62
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;
|
||||
}
|
||||
|
96
tag/tests/events_test.php
Normal file
96
tag/tests/events_test.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Events tests.
|
||||
*
|
||||
* @package core_tag
|
||||
* @category test
|
||||
* @copyright 2014 Mark Nelson <markn@moodle.com>
|
||||
* @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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user