From 86b0e3b308457908675ac761a571e80a96a9f977 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Mon, 2 Dec 2013 10:59:30 +0800 Subject: [PATCH] MDL-40058 Events: Replaced add_to_log for page comments to event --- comment/lib.php | 28 +++++++ lang/en/moodle.php | 3 + lib/classes/event/comment_created.php | 81 ++++++++++++++++++++ lib/classes/event/comment_deleted.php | 81 ++++++++++++++++++++ lib/classes/event/comments_viewed.php | 68 +++++++++++++++++ mod/wiki/classes/event/comment_created.php | 55 ++++++++++++++ mod/wiki/classes/event/comment_deleted.php | 55 ++++++++++++++ mod/wiki/classes/event/comments_viewed.php | 87 ++++++++++++++++++++++ mod/wiki/comments.php | 11 ++- mod/wiki/instancecomments.php | 1 - 10 files changed, 468 insertions(+), 2 deletions(-) create mode 100644 lib/classes/event/comment_created.php create mode 100644 lib/classes/event/comment_deleted.php create mode 100644 lib/classes/event/comments_viewed.php create mode 100644 mod/wiki/classes/event/comment_created.php create mode 100644 mod/wiki/classes/event/comment_deleted.php create mode 100644 mod/wiki/classes/event/comments_viewed.php diff --git a/comment/lib.php b/comment/lib.php index 5381866c424..8033ac0eaf7 100644 --- a/comment/lib.php +++ b/comment/lib.php @@ -651,6 +651,20 @@ class comment { } $newcmt->time = userdate($newcmt->timecreated, $newcmt->strftimeformat); + // Trigger comment created event. + $eventclassname = '\\' . $this->component . '\\event\comment_created'; + if (class_exists($eventclassname)) { + $event = $eventclassname::create( + array( + 'context' => $this->context, + 'objectid' => $newcmt->id, + 'other' => array( + 'itemid' => $this->itemid + ) + )); + $event->trigger(); + } + return $newcmt; } else { throw new comment_exception('dbupdatefailed'); @@ -709,6 +723,20 @@ class comment { throw new comment_exception('nopermissiontocomment'); } $DB->delete_records('comments', array('id'=>$commentid)); + // Trigger comment delete event. + $eventclassname = '\\' . $this->component . '\\event\comment_deleted'; + if (class_exists($eventclassname)) { + $event = $eventclassname::create( + array( + 'context' => $this->context, + 'objectid' => $commentid, + 'other' => array( + 'itemid' => $this->itemid + ) + )); + $event->add_record_snapshot('comments', $comment); + $event->trigger(); + } return true; } diff --git a/lang/en/moodle.php b/lang/en/moodle.php index d20f174203d..f2f102a3b33 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -710,6 +710,9 @@ $string['errorcreatingactivity'] = 'Unable to create an instance of activity \'{ $string['errorfiletoobig'] = 'The file was bigger than the limit of {$a} bytes'; $string['errornouploadrepo'] = 'There is no upload repository enabled for this site'; $string['errorwhenconfirming'] = 'You are not confirmed yet because an error occurred. If you clicked on a link in an email to get here, make sure that the line in your email wasn\'t broken or wrapped. You may have to use cut and paste to reconstruct the link properly.'; +$string['eventcommentcreated'] = 'Comment created'; +$string['eventcommentdeleted'] = 'Comment deleted'; +$string['eventcommentsviewed'] = 'Comments viewed'; $string['eventcoursecategorydeleted'] = 'Category deleted'; $string['eventcoursecontentdeleted'] = 'Course content deleted'; $string['eventcoursecreated'] = 'Course created'; diff --git a/lib/classes/event/comment_created.php b/lib/classes/event/comment_created.php new file mode 100644 index 00000000000..abee52e0e93 --- /dev/null +++ b/lib/classes/event/comment_created.php @@ -0,0 +1,81 @@ +. + +/** + * Abstract comment created event. + * + * @package core + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Abstract comment created event class. + * + * This class has to be extended by any event which is triggred while creating new comment. + * + * @package core + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class comment_created extends \core\event\base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'c'; + $this->data['level'] = self::LEVEL_PARTICIPATING; + $this->data['objecttable'] = 'comments'; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcommentcreated', 'moodle'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return 'User with id '. $this->userid . ' added comment for ' . $this->component . ' with instance id ' . + $this->contextinstanceid; + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + if (!isset($this->other['itemid'])) { + throw new \coding_exception('The itemid needs to be set in $other'); + } + } +} diff --git a/lib/classes/event/comment_deleted.php b/lib/classes/event/comment_deleted.php new file mode 100644 index 00000000000..f6a20040c39 --- /dev/null +++ b/lib/classes/event/comment_deleted.php @@ -0,0 +1,81 @@ +. + +/** + * Abstract comment deleted event. + * + * @package core + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Abstract comment deleted event class. + * + * This class has to be extended by any event which is triggred while deleting comment. + * + * @package core + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class comment_deleted extends \core\event\base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'd'; + $this->data['level'] = self::LEVEL_PARTICIPATING; + $this->data['objecttable'] = 'comments'; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcommentdeleted', 'moodle'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return 'User with id '. $this->userid . ' deleted comment for ' . $this->component . ' with instance id ' . + $this->contextinstanceid; + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + if (!isset($this->other['itemid'])) { + throw new \coding_exception('The itemid needs to be set in $other'); + } + } +} diff --git a/lib/classes/event/comments_viewed.php b/lib/classes/event/comments_viewed.php new file mode 100644 index 00000000000..1f6aaded8d1 --- /dev/null +++ b/lib/classes/event/comments_viewed.php @@ -0,0 +1,68 @@ +. + +/** + * Abstract comments viewed event. + * + * @package core + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Abstract comments viewed event class. + * + * This class has to be extended by any event which is triggred while viewing comment. + * + * @package core + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class comments_viewed extends \core\event\base { + + /** + * Init method. + * + * @return void + */ + protected function init() { + $this->data['crud'] = 'r'; + $this->data['level'] = self::LEVEL_PARTICIPATING; + } + + /** + * Return localised event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcommentsviewed', 'moodle'); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return 'User with id '. $this->userid . ' viewed comments for ' . $this->component . ' with instance id ' . + $this->objectid; + } +} diff --git a/mod/wiki/classes/event/comment_created.php b/mod/wiki/classes/event/comment_created.php new file mode 100644 index 00000000000..bbc4a583d28 --- /dev/null +++ b/mod/wiki/classes/event/comment_created.php @@ -0,0 +1,55 @@ +. + +/** + * mod_wiki comment created event. + * + * @package mod_wiki + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_wiki\event; +defined('MOODLE_INTERNAL') || die(); + +/** + * mod_wiki comment created event. + * + * @package mod_wiki + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class comment_created extends \core\event\comment_created { + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/wiki/comments.php', array('pageid' => $this->other['itemid'])); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return 'User with id '. $this->userid . ' added comment for ' . $this->component . ' with page id ' . + $this->other['itemid']; + } +} diff --git a/mod/wiki/classes/event/comment_deleted.php b/mod/wiki/classes/event/comment_deleted.php new file mode 100644 index 00000000000..1e3c757e2eb --- /dev/null +++ b/mod/wiki/classes/event/comment_deleted.php @@ -0,0 +1,55 @@ +. + +/** + * mod_wiki comment deleted event. + * + * @package mod_wiki + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_wiki\event; +defined('MOODLE_INTERNAL') || die(); + +/** + * mod_wiki comment deleted event. + * + * @package mod_wiki + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class comment_deleted extends \core\event\comment_deleted { + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/wiki/comments.php', array('pageid' => $this->other['itemid'])); + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return 'User with id '. $this->userid . ' deleted comment for ' . $this->component . ' with page id ' . + $this->other['itemid']; + } +} diff --git a/mod/wiki/classes/event/comments_viewed.php b/mod/wiki/classes/event/comments_viewed.php new file mode 100644 index 00000000000..2e7274da417 --- /dev/null +++ b/mod/wiki/classes/event/comments_viewed.php @@ -0,0 +1,87 @@ +. + +/** + * mod_wiki comments viewed event. + * + * @package mod_wiki + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_wiki\event; +defined('MOODLE_INTERNAL') || die(); + +/** + * mod_wiki comments viewed event. + * + * @package mod_wiki + * @copyright 2013 Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class comments_viewed extends \core\event\comments_viewed { + + /** + * Init method. + * + * @return void + */ + protected function init() { + parent::init(); + $this->data['objecttable'] = 'wiki_pages'; + } + + /** + * Returns description of what happened. + * + * @return string + */ + public function get_description() { + return 'User with id '. $this->userid . ' viewed comments for wiki page id ' . + $this->objectid; + } + + /** + * Return the legacy event log data. + * + * @return array + */ + protected function get_legacy_logdata() { + return(array($this->courseid, 'wiki', 'comments', + 'comments.php?pageid=' . $this->objectid, $this->objectid, $this->context->instanceid)); + } + + /** + * Get URL related to the action. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/mod/wiki/comments.php', array('pageid' => $this->objectid)); + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + if (empty($this->objectid) || empty($this->objecttable)) { + throw new \coding_exception('The objectid and objecttable need to be set in $other'); + } + } +} diff --git a/mod/wiki/comments.php b/mod/wiki/comments.php index f10fccb6e5d..a1a38ac10ba 100644 --- a/mod/wiki/comments.php +++ b/mod/wiki/comments.php @@ -59,7 +59,16 @@ $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST) require_login($course, true, $cm); -add_to_log($course->id, 'wiki', 'comments', "comments.php?pageid=".$pageid, $pageid, $cm->id); +// Trigger comment viewed event. +$event = \mod_wiki\event\comments_viewed::create( + array( + 'context' => context_module::instance($cm->id), + 'objectid' => $pageid + )); +$event->add_record_snapshot('wiki_pages', $page); +$event->add_record_snapshot('wiki', $wiki); +$event->add_record_snapshot('wiki_subwikis', $subwiki); +$event->trigger(); /// Print the page header $wikipage = new page_wiki_comments($wiki, $subwiki, $cm); diff --git a/mod/wiki/instancecomments.php b/mod/wiki/instancecomments.php index 1294c826c3a..56898c61320 100644 --- a/mod/wiki/instancecomments.php +++ b/mod/wiki/instancecomments.php @@ -99,7 +99,6 @@ if ($action == 'delete') { $comm->set_action($action, 0, $content); } } -add_to_log($course->id, 'wiki', 'comment', "comments.php?pageid=".$pageid, $pageid, $cm->id); $comm->print_header(); $comm->print_content();