Merge branch 'MDL-40742-master' of git://github.com/ankitagarwal/moodle
authorSam Hemelryk <sam@moodle.com>
Tue, 10 Sep 2013 22:13:29 +0000 (10:13 +1200)
committerSam Hemelryk <sam@moodle.com>
Tue, 10 Sep 2013 22:13:29 +0000 (10:13 +1200)
blog/locallib.php
blog/tests/bloglib_test.php
lang/en/blog.php
lib/classes/event/blog_entry_updated.php [new file with mode: 0644]

index 3c5b102..a1a4704 100644 (file)
@@ -275,11 +275,15 @@ class blog_entry implements renderable {
     /**
      * 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;
@@ -298,12 +302,17 @@ class blog_entry implements renderable {
 
         $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();
     }
 
     /**
index 2582ac8..689e3be 100644 (file)
@@ -27,7 +27,6 @@ global $CFG;
 require_once($CFG->dirroot . '/blog/locallib.php');
 require_once($CFG->dirroot . '/blog/lib.php');
 
-
 /**
  * Test functions that rely on the DB tables
  */
@@ -154,23 +153,22 @@ class core_bloglib_testcase extends advanced_testcase {
     /**
      * 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();
 
@@ -179,27 +177,78 @@ class core_bloglib_testcase extends advanced_testcase {
         $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);
     }
 }
+
index bf1c97c..5937859 100644 (file)
@@ -86,9 +86,9 @@ $string['entrybodyonlydesc'] = 'Entry description';
 $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';
diff --git a/lib/classes/event/blog_entry_updated.php b/lib/classes/event/blog_entry_updated.php
new file mode 100644 (file)
index 0000000..5859f4b
--- /dev/null
@@ -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']);
+    }
+}
+