Skip to content

Commit

Permalink
Merge branch 'MDL-40742-master' of git://github.com/ankitagarwal/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Sep 10, 2013
2 parents b3602b3 + 32dea43 commit 214816f
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 18 deletions.
19 changes: 14 additions & 5 deletions blog/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,15 @@ public function add() {
/**
* Updates this entry in the database. Access control checks must be done by calling code.
*
* @param mform $form Used for attachments
* @param array $params Entry parameters.
* @param moodleform $form Used for attachments.
* @param array $summaryoptions Summary options.
* @param array $attachmentoptions Attachment options.
*
* @return void
*/
public function edit($params=array(), $form=null, $summaryoptions=array(), $attachmentoptions=array()) {
global $CFG, $USER, $DB, $PAGE;
global $CFG, $DB;

$sitecontext = context_system::instance();
$entry = $this;
Expand All @@ -298,12 +302,17 @@ public function edit($params=array(), $form=null, $summaryoptions=array(), $atta

$entry->lastmodified = time();

// Update record
// Update record.
$DB->update_record('post', $entry);
tag_set('post', $entry->id, $entry->tags);

add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$USER->id.'&entryid='.$entry->id, $entry->subject);
events_trigger('blog_entry_edited', $entry);
$event = \core\event\blog_entry_updated::create(array(
'objectid' => $entry->id,
'relateduserid' => $entry->userid,
'other' => array('subject' => $entry->subject)
));
$event->set_custom_data($entry);
$event->trigger();
}

/**
Expand Down
73 changes: 61 additions & 12 deletions blog/tests/bloglib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
require_once($CFG->dirroot . '/blog/locallib.php');
require_once($CFG->dirroot . '/blog/lib.php');


/**
* Test functions that rely on the DB tables
*/
Expand Down Expand Up @@ -154,23 +153,22 @@ public function test_blog_get_headers_case_10() {
/**
* Test various blog related events.
*/
public function test_blog_entry_events() {
global $USER, $DB;
public function test_blog_entry_created_event() {
global $USER;

$this->setAdminUser();
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();

// Create a blog entry for another user as Admin.
$sink = $this->redirectEvents();
$blog = new blog_entry();
$blog->userid = $user->id;
$blog->summary = "This is summary of blog";
$blog->subject = "Subject of blog";
$blog->userid = $this->userid;
$states = blog_entry::get_applicable_publish_states();
$blog->publishstate = reset($states);
$sink = $this->redirectEvents();
$blog->add();
$events = $sink->get_events();
$sink->close();
$event = reset($events);
$sitecontext = context_system::instance();

Expand All @@ -179,27 +177,78 @@ public function test_blog_entry_events() {
$this->assertEquals($sitecontext->id, $event->contextid);
$this->assertEquals($blog->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($user->id, $event->relateduserid);
$this->assertEquals($this->userid, $event->relateduserid);
$this->assertEquals("post", $event->objecttable);
$arr = array(SITEID, 'blog', 'add', 'index.php?userid=' . $user->id . '&entryid=' . $blog->id, $blog->subject);
$arr = array(SITEID, 'blog', 'add', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id, $blog->subject);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEquals("blog_entry_added", $event->get_legacy_eventname());
$this->assertEventLegacyData($blog, $event);
}

/**
* Tests for event blog_entry_updated.
*/
public function test_blog_entry_updated_event() {
global $USER;

$this->setAdminUser();
$this->resetAfterTest();
$sitecontext = context_system::instance();

// Edit a blog entry as Admin.
$blog = new blog_entry($this->postid);
$sink = $this->redirectEvents();
$blog->summary_editor = array('text' => 'Something', 'format' => FORMAT_MOODLE);
$blog->edit(array(), null, array(), array());
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();

// Validate event data.
$this->assertInstanceOf('\core\event\blog_entry_updated', $event);
$this->assertEquals($sitecontext->id, $event->contextid);
$this->assertEquals($blog->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($this->userid, $event->relateduserid);
$this->assertEquals("post", $event->objecttable);
$this->assertEquals("blog_entry_edited", $event->get_legacy_eventname());
$this->assertEventLegacyData($blog, $event);
$arr = array (SITEID, 'blog', 'update', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id, $blog->subject);
$this->assertEventLegacyLogData($arr, $event);
}

/**
* Tests for event blog_entry_deleted.
*/
public function test_blog_entry_deleted_event() {
global $USER, $DB;

$this->setAdminUser();
$this->resetAfterTest();
$sitecontext = context_system::instance();

// Delete a user blog entry as Admin.
$blog = new blog_entry($this->postid);
$sink = $this->redirectEvents();
$record = $DB->get_record('post', array('id' => $blog->id));
$blog->delete();
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();

// Validate event data.
$this->assertInstanceOf('\core\event\blog_entry_deleted', $event);
$this->assertEquals(context_system::instance()->id, $event->contextid);
$this->assertEquals($sitecontext->id, $event->contextid);
$this->assertEquals($blog->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($user->id, $event->relateduserid);
$this->assertEquals($this->userid, $event->relateduserid);
$this->assertEquals("post", $event->objecttable);
$this->assertEquals($record, $event->get_record_snapshot("post", $blog->id));
$this->assertSame('blog_entry_deleted', $event->get_legacy_eventname());
$arr = array(SITEID, 'blog', 'delete', 'index.php?userid=' . $user->id, 'deleted blog entry with entry id# '. $blog->id);
$arr = array(SITEID, 'blog', 'delete', 'index.php?userid=' . $blog->userid, 'deleted blog entry with entry id# ' .
$blog->id);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventLegacyData($blog, $event);
}
}

2 changes: 1 addition & 1 deletion lang/en/blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@
$string['entryerrornotyours'] = 'This entry is not yours';
$string['entrysaved'] = 'Your entry has been saved';
$string['entrytitle'] = 'Entry title';
$string['entryupdated'] = 'Blog entry updated';
$string['evententryadded'] = 'Blog entry added';
$string['evententrydeleted'] = 'Blog entry deleted';
$string['evententryupdated'] = 'Blog entry updated';
$string['externalblogcrontime'] = 'External blog cron schedule';
$string['externalblogdeleteconfirm'] = 'Unregister this external blog?';
$string['externalblogdeleted'] = 'External blog unregistered';
Expand Down
114 changes: 114 additions & 0 deletions lib/classes/event/blog_entry_updated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?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/>.

/**
* Event to be triggered when a blog entry is updated.
*
* @package core
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\event;
defined('MOODLE_INTERNAL') || die();

/**
* Class blog_entry_updated
*
* Event to be triggered when a blog entry is updated.
*
* @package core
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class blog_entry_updated extends base {

/** @var \blog_entry A reference to the active blog_entry object. */
protected $customobject;

/**
* Set basic event properties.
*/
protected function init() {
$this->context = \context_system::instance();
$this->data['objecttable'] = 'post';
$this->data['crud'] = 'u';
$this->data['level'] = self::LEVEL_PARTICIPATING;
}

/**
* Set custom data of the event.
*
* @param \blog_entry $data A reference to the active blog_entry object.
*/
public function set_custom_data($data) {
$this->customobject = $data;
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('evententryupdated', 'core_blog');
}

/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return 'User with id {$this->userid} updated blog entry {$this->other["subject"]';
}

/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/blog/index.php', array('entryid' => $this->objectid, 'userid' => $this->userid));
}

/**
* Legacy event data if get_legacy_eventname() is not empty.
*
* @return \blog_entry
*/
protected function get_legacy_eventdata() {
return $this->customobject;
}

/**
* Legacy event name.
*
* @return string legacy event name
*/
public static function get_legacy_eventname() {
return 'blog_entry_edited';
}

/**
* Replace legacy add_to_log() statement.
*
* @return array of parameters to be passed to legacy add_to_log() function.
*/
protected function get_legacy_logdata() {
return array(SITEID, 'blog', 'update', 'index.php?userid=' . $this->relateduserid . '&entryid=' . $this->objectid,
$this->other['subject']);
}
}

0 comments on commit 214816f

Please sign in to comment.