Skip to content

Commit

Permalink
MDL-40058 Events: Replaced add_to_log for page comments to event
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja authored and marinaglancy committed Dec 11, 2013
1 parent 8b43cf2 commit 86b0e3b
Show file tree
Hide file tree
Showing 10 changed files with 468 additions and 2 deletions.
28 changes: 28 additions & 0 deletions comment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,20 @@ public function add($content, $format = FORMAT_MOODLE) {
}
$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');
Expand Down Expand Up @@ -709,6 +723,20 @@ public function delete($commentid) {
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;
}

Expand Down
3 changes: 3 additions & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,9 @@
$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';
Expand Down
81 changes: 81 additions & 0 deletions lib/classes/event/comment_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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/>.

/**
* Abstract comment created event.
*
* @package core
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @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 <rajesh@moodle.com>
* @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');
}
}
}
81 changes: 81 additions & 0 deletions lib/classes/event/comment_deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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/>.

/**
* Abstract comment deleted event.
*
* @package core
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @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 <rajesh@moodle.com>
* @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');
}
}
}
68 changes: 68 additions & 0 deletions lib/classes/event/comments_viewed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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/>.

/**
* Abstract comments viewed event.
*
* @package core
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @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 <rajesh@moodle.com>
* @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;
}
}
55 changes: 55 additions & 0 deletions mod/wiki/classes/event/comment_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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/>.

/**
* mod_wiki comment created event.
*
* @package mod_wiki
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @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 <rajesh@moodle.com>
* @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'];
}
}
55 changes: 55 additions & 0 deletions mod/wiki/classes/event/comment_deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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/>.

/**
* mod_wiki comment deleted event.
*
* @package mod_wiki
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @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 <rajesh@moodle.com>
* @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'];
}
}
Loading

0 comments on commit 86b0e3b

Please sign in to comment.