MDL-45890 Blog: add additional events
authorStephen Bourget <steve.bourget@sau19.org>
Sun, 25 Sep 2016 19:06:54 +0000 (15:06 -0400)
committerStephen Bourget <steve.bourget@sau19.org>
Sun, 25 Sep 2016 19:06:54 +0000 (15:06 -0400)
12 files changed:
blog/external_blog_edit.php
blog/external_blogs.php
blog/locallib.php
blog/tests/events_test.php [new file with mode: 0644]
blog/tests/lib_test.php
lang/en/blog.php
lib/classes/event/blog_association_deleted.php [new file with mode: 0644]
lib/classes/event/blog_external_added.php [new file with mode: 0644]
lib/classes/event/blog_external_removed.php [new file with mode: 0644]
lib/classes/event/blog_external_updated.php [new file with mode: 0644]
lib/classes/event/blog_external_viewed.php [new file with mode: 0644]
version.php

index 3f615bd..5d69243 100644 (file)
@@ -88,6 +88,13 @@ if ($externalblogform->is_cancelled()) {
                     context_user::instance($newexternal->userid), $data->autotags);
             blog_sync_external_entries($newexternal);
 
                     context_user::instance($newexternal->userid), $data->autotags);
             blog_sync_external_entries($newexternal);
 
+            // Log this action.
+            $eventparms = array('context' => $context,
+                'objectid' => $newexternal->id,
+                'other' => array('url' => $newexternal->url));
+            $event = \core\event\blog_external_added::create($eventparms);
+            $event->trigger();
+
             break;
 
         case 'edit':
             break;
 
         case 'edit':
@@ -104,6 +111,14 @@ if ($externalblogform->is_cancelled()) {
                 $external->timemodified = time();
 
                 $DB->update_record('blog_external', $external);
                 $external->timemodified = time();
 
                 $DB->update_record('blog_external', $external);
+
+                // Log this action.
+                $eventparms = array('context' => $context,
+                    'objectid' => $external->id,
+                    'other' => array('url' => $external->url));
+                $event = \core\event\blog_external_updated::create($eventparms);
+                $event->trigger();
+
                 core_tag_tag::set_item_tags('core', 'blog_external', $external->id,
                         context_user::instance($external->userid), $data->autotags);
             } else {
                 core_tag_tag::set_item_tags('core', 'blog_external', $external->id,
                         context_user::instance($external->userid), $data->autotags);
             } else {
index 89d9acb..996ee7a 100644 (file)
@@ -41,8 +41,8 @@ $strblogs = get_string('blogs', 'blog');
 $message = null;
 
 if ($delete && confirm_sesskey()) {
 $message = null;
 
 if ($delete && confirm_sesskey()) {
-    $externalbloguserid = $DB->get_field('blog_external', 'userid', array('id' => $delete));
-    if ($externalbloguserid == $USER->id) {
+    $externalblog = $DB->get_record('blog_external', array('id' => $delete));
+    if ($externalblog->userid == $USER->id) {
         // Delete the external blog.
         $DB->delete_records('blog_external', array('id' => $delete));
 
         // Delete the external blog.
         $DB->delete_records('blog_external', array('id' => $delete));
 
@@ -55,6 +55,11 @@ if ($delete && confirm_sesskey()) {
                                                                'userid' => $USER->id,
                                                                'delete' => $delete));
 
                                                                'userid' => $USER->id,
                                                                'delete' => $delete));
 
+        // Log this action.
+        $eventparms = array('context' => $context, 'objectid' => $delete);
+        $event = \core\event\blog_external_removed::create($eventparms);
+        $event->add_record_snapshot('blog_external', $externalblog);
+        $event->trigger();
         $message = get_string('externalblogdeleted', 'blog');
     }
 }
         $message = get_string('externalblogdeleted', 'blog');
     }
 }
@@ -111,4 +116,8 @@ if (!empty($blogs)) {
 $newexternalurl = new moodle_url('/blog/external_blog_edit.php');
 echo html_writer::link($newexternalurl, $straddnewexternalblog);
 echo $OUTPUT->box_end();
 $newexternalurl = new moodle_url('/blog/external_blog_edit.php');
 echo html_writer::link($newexternalurl, $straddnewexternalblog);
 echo $OUTPUT->box_end();
+
+// Log this page.
+$event = \core\event\blog_external_viewed::create(array('context' => $context));
+$event->trigger();
 echo $OUTPUT->footer();
 echo $OUTPUT->footer();
index af1b72e..40af88d 100644 (file)
@@ -403,11 +403,29 @@ class blog_entry implements renderable {
 
     /**
      * remove all associations for a blog entry
 
     /**
      * remove all associations for a blog entry
-     * @return voic
+     *
+     * @return void
      */
     public function remove_associations() {
         global $DB;
      */
     public function remove_associations() {
         global $DB;
-        $DB->delete_records('blog_association', array('blogid' => $this->id));
+
+        $associations = $DB->get_records('blog_association', array('blogid' => $this->id));
+        foreach ($associations as $association) {
+
+            // Trigger an association deleted event.
+            $context = context::instance_by_id($association->contextid);
+            $eventparam = array(
+                'objectid' => $this->id,
+                'other' => array('subject' => $this->subject, 'blogid' => $this->id),
+                'relateduserid' => $this->userid
+            );
+            $event = \core\event\blog_association_deleted::create($eventparam);
+            $event->add_record_snapshot('blog_association', $association);
+            $event->trigger();
+
+            // Now remove the association.
+            $DB->delete_records('blog_association', array('id' => $association->id));
+        }
     }
 
     /**
     }
 
     /**
diff --git a/blog/tests/events_test.php b/blog/tests/events_test.php
new file mode 100644 (file)
index 0000000..87f265d
--- /dev/null
@@ -0,0 +1,584 @@
+<?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
+ * @category   test
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+require_once($CFG->dirroot . '/blog/locallib.php');
+require_once($CFG->dirroot . '/blog/lib.php');
+
+/**
+ * Unit tests for the blog events.
+ *
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class core_blog_events_testcase extends advanced_testcase {
+
+    /** @var $courseid */
+    private $courseid;
+
+    /** @var $cmid */
+    private $cmid;
+
+    /** @var $groupid */
+    private $groupid;
+
+    /** @var $userid */
+    private $userid;
+
+    /** @var $tagid */
+    private $tagid;
+
+    /** @var $postid */
+    private $postid;
+
+    /**
+     * Setup the tests.
+     */
+    protected function setUp() {
+        global $DB;
+        parent::setUp();
+
+        $this->resetAfterTest();
+
+        // Create default course.
+        $course = $this->getDataGenerator()->create_course(array('category' => 1, 'shortname' => 'ANON'));
+        $this->assertNotEmpty($course);
+        $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
+        $this->assertNotEmpty($page);
+
+        // Create default group.
+        $group = new stdClass();
+        $group->courseid = $course->id;
+        $group->name = 'ANON';
+        $group->id = $DB->insert_record('groups', $group);
+
+        // Create default user.
+        $user = $this->getDataGenerator()->create_user(array(
+                'username' => 'testuser',
+                'firstname' => 'Jimmy',
+                'lastname' => 'Kinnon'
+        ));
+
+        // Create default tag.
+        $tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id,
+            'rawname' => 'Testtagname', 'isstandard' => 1));
+
+        // Create default post.
+        $post = new stdClass();
+        $post->userid = $user->id;
+        $post->groupid = $group->id;
+        $post->content = 'test post content text';
+        $post->module = 'blog';
+        $post->id = $DB->insert_record('post', $post);
+
+        // Grab important ids.
+        $this->courseid = $course->id;
+        $this->cmid = $page->cmid;
+        $this->groupid  = $group->id;
+        $this->userid  = $user->id;
+        $this->tagid  = $tag->id;
+        $this->postid = $post->id;
+    }
+
+    /**
+     * Test various blog related events.
+     */
+    public function test_blog_entry_created_event() {
+        global $USER;
+
+        $this->setAdminUser();
+        $this->resetAfterTest();
+
+        // Create a blog entry for another user as Admin.
+        $sink = $this->redirectEvents();
+        $blog = new blog_entry();
+        $blog->subject = "Subject of blog";
+        $blog->userid = $this->userid;
+        $states = blog_entry::get_applicable_publish_states();
+        $blog->publishstate = reset($states);
+        $blog->add();
+        $events = $sink->get_events();
+        $sink->close();
+        $event = reset($events);
+        $sitecontext = context_system::instance();
+
+        // Validate event data.
+        $this->assertInstanceOf('\core\event\blog_entry_created', $event);
+        $url = new moodle_url('/blog/index.php', array('entryid' => $event->objectid));
+        $this->assertEquals($url, $event->get_url());
+        $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);
+        $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);
+        $this->assertEventContextNotUsed($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);
+        $url = new moodle_url('/blog/index.php', array('entryid' => $event->objectid));
+        $this->assertEquals($url, $event->get_url());
+        $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);
+        $this->assertEventContextNotUsed($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(null, $event->get_url());
+        $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($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=' . $blog->userid, 'deleted blog entry with entry id# ' .
+                $blog->id);
+        $this->assertEventLegacyLogData($arr, $event);
+        $this->assertEventLegacyData($blog, $event);
+        $this->assertEventContextNotUsed($event);
+    }
+
+    /**
+     * Tests for event blog_association_deleted.
+     */
+    public function test_blog_association_deleted_event() {
+        global $USER;
+
+        $this->setAdminUser();
+        $this->resetAfterTest();
+        $sitecontext = context_system::instance();
+        $coursecontext = context_course::instance($this->courseid);
+        $contextmodule = context_module::instance($this->cmid);
+
+        // Add blog associations with a course.
+        $blog = new blog_entry($this->postid);
+        $blog->add_association($coursecontext->id);
+
+        $sink = $this->redirectEvents();
+        $blog->remove_associations();
+        $events = $sink->get_events();
+        $event = reset($events);
+        $sink->close();
+
+        // Validate event data.
+        $this->assertInstanceOf('\core\event\blog_association_deleted', $event);
+        $this->assertEquals($sitecontext->id, $event->contextid);
+        $this->assertEquals($blog->id, $event->other['blogid']);
+        $this->assertEquals($USER->id, $event->userid);
+        $this->assertEquals($this->userid, $event->relateduserid);
+        $this->assertEquals('blog_association', $event->objecttable);
+
+        // Add blog associations with a module.
+        $blog = new blog_entry($this->postid);
+        $blog->add_association($contextmodule->id);
+        $sink = $this->redirectEvents();
+        $blog->remove_associations();
+        $events = $sink->get_events();
+        $event = reset($events);
+        $sink->close();
+
+        // Validate event data.
+        $this->assertEquals($blog->id, $event->other['blogid']);
+        $this->assertEquals($USER->id, $event->userid);
+        $this->assertEventContextNotUsed($event);
+    }
+
+    /**
+     * Tests for event blog_association_created.
+     */
+    public function test_blog_association_created_event() {
+        global $USER;
+
+        $this->setAdminUser();
+        $this->resetAfterTest();
+        $sitecontext = context_system::instance();
+        $coursecontext = context_course::instance($this->courseid);
+        $contextmodule = context_module::instance($this->cmid);
+
+        // Add blog associations with a course.
+        $blog = new blog_entry($this->postid);
+        $sink = $this->redirectEvents();
+        $blog->add_association($coursecontext->id);
+        $events = $sink->get_events();
+        $event = reset($events);
+        $sink->close();
+
+        // Validate event data.
+        $this->assertInstanceOf('\core\event\blog_association_created', $event);
+        $this->assertEquals($sitecontext->id, $event->contextid);
+        $url = new moodle_url('/blog/index.php', array('entryid' => $event->other['blogid']));
+        $this->assertEquals($url, $event->get_url());
+        $this->assertEquals($blog->id, $event->other['blogid']);
+        $this->assertEquals($this->courseid, $event->other['associateid']);
+        $this->assertEquals('course', $event->other['associatetype']);
+        $this->assertEquals($blog->subject, $event->other['subject']);
+        $this->assertEquals($USER->id, $event->userid);
+        $this->assertEquals($this->userid, $event->relateduserid);
+        $this->assertEquals('blog_association', $event->objecttable);
+        $arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
+                     $blog->subject, 0, $this->userid);
+        $this->assertEventLegacyLogData($arr, $event);
+
+        // Add blog associations with a module.
+        $blog = new blog_entry($this->postid);
+        $sink = $this->redirectEvents();
+        $blog->add_association($contextmodule->id);
+        $events = $sink->get_events();
+        $event = reset($events);
+        $sink->close();
+
+        // Validate event data.
+        $this->assertEquals($blog->id, $event->other['blogid']);
+        $this->assertEquals($this->cmid, $event->other['associateid']);
+        $this->assertEquals('coursemodule', $event->other['associatetype']);
+        $arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
+                     $blog->subject, $this->cmid, $this->userid);
+        $this->assertEventLegacyLogData($arr, $event);
+        $this->assertEventContextNotUsed($event);
+    }
+
+    /**
+     * Tests for event blog_association_created validations.
+     */
+    public function test_blog_association_created_event_validations() {
+
+        $this->resetAfterTest();
+
+         // Make sure associatetype validations work.
+        try {
+            \core\event\blog_association_created::create(array(
+                'contextid' => 1,
+                'objectid' => 3,
+                'relateduserid' => 2,
+                'other' => array('associateid' => 2 , 'blogid' => 3, 'subject' => 'blog subject')));
+        } catch (coding_exception $e) {
+            $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
+        }
+        try {
+            \core\event\blog_association_created::create(array(
+                'contextid' => 1,
+                'objectid' => 3,
+                'relateduserid' => 2,
+                'other' => array('associateid' => 2 , 'blogid' => 3, 'associatetype' => 'random', 'subject' => 'blog subject')));
+        } catch (coding_exception $e) {
+            $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
+        }
+        // Make sure associateid validations work.
+        try {
+            \core\event\blog_association_created::create(array(
+                'contextid' => 1,
+                'objectid' => 3,
+                'relateduserid' => 2,
+                'other' => array('blogid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
+        } catch (coding_exception $e) {
+            $this->assertContains('The \'associateid\' value must be set in other.', $e->getMessage());
+        }
+        // Make sure blogid validations work.
+        try {
+            \core\event\blog_association_created::create(array(
+                'contextid' => 1,
+                'objectid' => 3,
+                'relateduserid' => 2,
+                'other' => array('associateid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
+        } catch (coding_exception $e) {
+            $this->assertContains('The \'blogid\' value must be set in other.', $e->getMessage());
+        }
+        // Make sure blogid validations work.
+        try {
+            \core\event\blog_association_created::create(array(
+                'contextid' => 1,
+                'objectid' => 3,
+                'relateduserid' => 2,
+                'other' => array('blogid' => 3, 'associateid' => 3, 'associatetype' => 'course')));
+        } catch (coding_exception $e) {
+            $this->assertContains('The \'subject\' value must be set in other.', $e->getMessage());
+        }
+    }
+
+    /**
+     * Tests for event blog_entries_viewed.
+     */
+    public function test_blog_entries_viewed_event() {
+
+        $this->setAdminUser();
+
+        $other = array('entryid' => $this->postid, 'tagid' => $this->tagid, 'userid' => $this->userid, 'modid' => $this->cmid,
+                       'groupid' => $this->groupid, 'courseid' => $this->courseid, 'search' => 'search', 'fromstart' => 2);
+
+        // Trigger event.
+        $sink = $this->redirectEvents();
+        $eventparams = array('other' => $other);
+        $eventinst = \core\event\blog_entries_viewed::create($eventparams);
+        $eventinst->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+        $sink->close();
+
+        // Validate event data.
+        $url = new moodle_url('/blog/index.php', $other);
+        $url2 = new moodle_url('index.php', $other);
+        $this->assertEquals($url, $event->get_url());
+        $arr = array(SITEID, 'blog', 'view', $url2->out(), 'view blog entry');
+        $this->assertEventLegacyLogData($arr, $event);
+        $this->assertEventContextNotUsed($event);
+    }
+
+    /**
+     * Test comment_created event.
+     */
+    public function test_blog_comment_created_event() {
+        global $USER, $CFG;
+
+        $this->setAdminUser();
+
+        require_once($CFG->dirroot . '/comment/lib.php');
+        $context = context_user::instance($USER->id);
+
+        $cmt = new stdClass();
+        $cmt->context = $context;
+        $cmt->courseid = $this->courseid;
+        $cmt->area = 'format_blog';
+        $cmt->itemid = $this->postid;
+        $cmt->showcount = 1;
+        $cmt->component = 'blog';
+        $manager = new comment($cmt);
+
+        // Triggering and capturing the event.
+        $sink = $this->redirectEvents();
+        $manager->add("New comment");
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\core\event\blog_comment_created', $event);
+        $this->assertEquals($context, $event->get_context());
+        $this->assertEquals($this->postid, $event->other['itemid']);
+        $url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
+        $this->assertEquals($url, $event->get_url());
+        $this->assertEventContextNotUsed($event);
+    }
+
+    /**
+     * Test comment_deleted event.
+     */
+    public function test_blog_comment_deleted_event() {
+        global $USER, $CFG;
+
+        $this->setAdminUser();
+
+        require_once($CFG->dirroot . '/comment/lib.php');
+        $context = context_user::instance($USER->id);
+
+        $cmt = new stdClass();
+        $cmt->context = $context;
+        $cmt->courseid = $this->courseid;
+        $cmt->area = 'format_blog';
+        $cmt->itemid = $this->postid;
+        $cmt->showcount = 1;
+        $cmt->component = 'blog';
+        $manager = new comment($cmt);
+        $newcomment = $manager->add("New comment");
+
+        // Triggering and capturing the event.
+        $sink = $this->redirectEvents();
+        $manager->delete($newcomment->id);
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = reset($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\core\event\blog_comment_deleted', $event);
+        $this->assertEquals($context, $event->get_context());
+        $this->assertEquals($this->postid, $event->other['itemid']);
+        $url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
+        $this->assertEquals($url, $event->get_url());
+        $this->assertEventContextNotUsed($event);
+    }
+
+    /**
+     * Test external blog added event.
+     *
+     * There is no external API for this, so the unit test will simply
+     * create and trigger the event and ensure data is returned as expected.
+     */
+    public function test_external_blog_added_event() {
+
+        // Trigger an event: external blog added.
+        $eventparams = array(
+            'context' => $context = context_system::instance(),
+            'objectid' => 1001,
+            'other' => array('url' => 'http://moodle.org')
+        );
+
+        $event = \core\event\blog_external_added::create($eventparams);
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\blog_external_added', $event);
+        $this->assertEquals(1001, $event->objectid);
+        $this->assertEquals('http://moodle.org', $event->other['url']);
+        $this->assertDebuggingNotCalled();
+    }
+
+    /**
+     * Test external blog updated event.
+     *
+     * There is no external API for this, so the unit test will simply
+     * create and trigger the event and ensure data is returned as expected.
+     */
+    public function test_external_blog_updated_event() {
+
+        // Trigger an event: external blog updated.
+        $eventparams = array(
+            'context' => $context = context_system::instance(),
+            'objectid' => 1001,
+            'other' => array('url' => 'http://moodle.org')
+        );
+
+        $event = \core\event\blog_external_updated::create($eventparams);
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\blog_external_updated', $event);
+        $this->assertEquals(1001, $event->objectid);
+        $this->assertEquals('http://moodle.org', $event->other['url']);
+        $this->assertDebuggingNotCalled();
+    }
+
+    /**
+     * Test external blog removed event.
+     *
+     * There is no external API for this, so the unit test will simply
+     * create and trigger the event and ensure data is returned as expected.
+     */
+    public function test_external_blog_removed_event() {
+
+        // Trigger an event: external blog removed.
+        $eventparams = array(
+            'context' => $context = context_system::instance(),
+            'objectid' => 1001,
+        );
+
+        $event = \core\event\blog_external_removed::create($eventparams);
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\blog_external_removed', $event);
+        $this->assertEquals(1001, $event->objectid);
+        $this->assertDebuggingNotCalled();
+    }
+
+    /**
+     * Test external blogs viewed event.
+     *
+     * There is no external API for this, so the unit test will simply
+     * create and trigger the event and ensure data is returned as expected.
+     */
+    public function test_external_blogs_viewed_event() {
+
+        // Trigger an event: external blogs viewed.
+        $eventparams = array(
+            'context' => $context = context_system::instance(),
+        );
+
+        $event = \core\event\blog_external_viewed::create($eventparams);
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\blog_external_viewed', $event);
+        $this->assertDebuggingNotCalled();
+    }
+}
+
index 80c3be5..a690e63 100644 (file)
@@ -23,6 +23,8 @@
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
+defined('MOODLE_INTERNAL') || die();
+
 global $CFG;
 require_once($CFG->dirroot . '/blog/locallib.php');
 require_once($CFG->dirroot . '/blog/lib.php');
 global $CFG;
 require_once($CFG->dirroot . '/blog/locallib.php');
 require_once($CFG->dirroot . '/blog/lib.php');
@@ -151,329 +153,6 @@ class core_blog_lib_testcase extends advanced_testcase {
         $this->assertNotEquals($blogheaders['heading'], '');
     }
 
         $this->assertNotEquals($blogheaders['heading'], '');
     }
 
-    /**
-     * Test various blog related events.
-     */
-    public function test_blog_entry_created_event() {
-        global $USER;
-
-        $this->setAdminUser();
-        $this->resetAfterTest();
-
-        // Create a blog entry for another user as Admin.
-        $sink = $this->redirectEvents();
-        $blog = new blog_entry();
-        $blog->subject = "Subject of blog";
-        $blog->userid = $this->userid;
-        $states = blog_entry::get_applicable_publish_states();
-        $blog->publishstate = reset($states);
-        $blog->add();
-        $events = $sink->get_events();
-        $sink->close();
-        $event = reset($events);
-        $sitecontext = context_system::instance();
-
-        // Validate event data.
-        $this->assertInstanceOf('\core\event\blog_entry_created', $event);
-        $url = new moodle_url('/blog/index.php', array('entryid' => $event->objectid));
-        $this->assertEquals($url, $event->get_url());
-        $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);
-        $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);
-        $this->assertEventContextNotUsed($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);
-        $url = new moodle_url('/blog/index.php', array('entryid' => $event->objectid));
-        $this->assertEquals($url, $event->get_url());
-        $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);
-        $this->assertEventContextNotUsed($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(null, $event->get_url());
-        $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($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=' . $blog->userid, 'deleted blog entry with entry id# ' .
-                $blog->id);
-        $this->assertEventLegacyLogData($arr, $event);
-        $this->assertEventLegacyData($blog, $event);
-        $this->assertEventContextNotUsed($event);
-    }
-
-
-    /**
-     * Tests for event blog_association_created.
-     */
-    public function test_blog_association_created_event() {
-        global $USER;
-
-        $this->setAdminUser();
-        $this->resetAfterTest();
-        $sitecontext = context_system::instance();
-        $coursecontext = context_course::instance($this->courseid);
-        $contextmodule = context_module::instance($this->cmid);
-
-        // Add blog associations with a course.
-        $blog = new blog_entry($this->postid);
-        $sink = $this->redirectEvents();
-        $blog->add_association($coursecontext->id);
-        $events = $sink->get_events();
-        $event = reset($events);
-        $sink->close();
-
-        // Validate event data.
-        $this->assertInstanceOf('\core\event\blog_association_created', $event);
-        $this->assertEquals($sitecontext->id, $event->contextid);
-        $url = new moodle_url('/blog/index.php', array('entryid' => $event->other['blogid']));
-        $this->assertEquals($url, $event->get_url());
-        $this->assertEquals($blog->id, $event->other['blogid']);
-        $this->assertEquals($this->courseid, $event->other['associateid']);
-        $this->assertEquals('course', $event->other['associatetype']);
-        $this->assertEquals($blog->subject, $event->other['subject']);
-        $this->assertEquals($USER->id, $event->userid);
-        $this->assertEquals($this->userid, $event->relateduserid);
-        $this->assertEquals('blog_association', $event->objecttable);
-        $arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
-                     $blog->subject, 0, $this->userid);
-        $this->assertEventLegacyLogData($arr, $event);
-
-        // Add blog associations with a module.
-        $blog = new blog_entry($this->postid);
-        $sink = $this->redirectEvents();
-        $blog->add_association($contextmodule->id);
-        $events = $sink->get_events();
-        $event = reset($events);
-        $sink->close();
-
-        // Validate event data.
-        $this->assertEquals($blog->id, $event->other['blogid']);
-        $this->assertEquals($this->cmid, $event->other['associateid']);
-        $this->assertEquals('coursemodule', $event->other['associatetype']);
-        $arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
-                     $blog->subject, $this->cmid, $this->userid);
-        $this->assertEventLegacyLogData($arr, $event);
-        $this->assertEventContextNotUsed($event);
-    }
-
-    /**
-     * Tests for event blog_association_created validations.
-     */
-    public function test_blog_association_created_event_validations() {
-
-        $this->resetAfterTest();
-
-         // Make sure associatetype validations work.
-        try {
-            \core\event\blog_association_created::create(array(
-                'contextid' => 1,
-                'objectid' => 3,
-                'relateduserid' => 2,
-                'other' => array('associateid' => 2 , 'blogid' => 3, 'subject' => 'blog subject')));
-        } catch (coding_exception $e) {
-            $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
-        }
-        try {
-            \core\event\blog_association_created::create(array(
-                'contextid' => 1,
-                'objectid' => 3,
-                'relateduserid' => 2,
-                'other' => array('associateid' => 2 , 'blogid' => 3, 'associatetype' => 'random', 'subject' => 'blog subject')));
-        } catch (coding_exception $e) {
-            $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
-        }
-        // Make sure associateid validations work.
-        try {
-            \core\event\blog_association_created::create(array(
-                'contextid' => 1,
-                'objectid' => 3,
-                'relateduserid' => 2,
-                'other' => array('blogid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
-        } catch (coding_exception $e) {
-            $this->assertContains('The \'associateid\' value must be set in other.', $e->getMessage());
-        }
-        // Make sure blogid validations work.
-        try {
-            \core\event\blog_association_created::create(array(
-                'contextid' => 1,
-                'objectid' => 3,
-                'relateduserid' => 2,
-                'other' => array('associateid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
-        } catch (coding_exception $e) {
-            $this->assertContains('The \'blogid\' value must be set in other.', $e->getMessage());
-        }
-        // Make sure blogid validations work.
-        try {
-            \core\event\blog_association_created::create(array(
-                'contextid' => 1,
-                'objectid' => 3,
-                'relateduserid' => 2,
-                'other' => array('blogid' => 3, 'associateid' => 3, 'associatetype' => 'course')));
-        } catch (coding_exception $e) {
-            $this->assertContains('The \'subject\' value must be set in other.', $e->getMessage());
-        }
-    }
-
-    /**
-     * Tests for event blog_entries_viewed.
-     */
-    public function test_blog_entries_viewed_event() {
-
-        $this->setAdminUser();
-
-        $other = array('entryid' => $this->postid, 'tagid' => $this->tagid, 'userid' => $this->userid, 'modid' => $this->cmid,
-                       'groupid' => $this->groupid, 'courseid' => $this->courseid, 'search' => 'search', 'fromstart' => 2);
-
-        // Trigger event.
-        $sink = $this->redirectEvents();
-        $eventparams = array('other' => $other);
-        $eventinst = \core\event\blog_entries_viewed::create($eventparams);
-        $eventinst->trigger();
-        $events = $sink->get_events();
-        $event = reset($events);
-        $sink->close();
-
-        // Validate event data.
-        $url = new moodle_url('/blog/index.php', $other);
-        $url2 = new moodle_url('index.php', $other);
-        $this->assertEquals($url, $event->get_url());
-        $arr = array(SITEID, 'blog', 'view', $url2->out(), 'view blog entry');
-        $this->assertEventLegacyLogData($arr, $event);
-        $this->assertEventContextNotUsed($event);
-    }
-
-    /**
-     * Test comment_created event.
-     */
-    public function test_blog_comment_created_event() {
-        global $USER, $CFG;
-
-        $this->setAdminUser();
-
-        require_once($CFG->dirroot . '/comment/lib.php');
-        $context = context_user::instance($USER->id);
-
-        $cmt = new stdClass();
-        $cmt->context = $context;
-        $cmt->courseid = $this->courseid;
-        $cmt->area = 'format_blog';
-        $cmt->itemid = $this->postid;
-        $cmt->showcount = 1;
-        $cmt->component = 'blog';
-        $manager = new comment($cmt);
-
-        // Triggering and capturing the event.
-        $sink = $this->redirectEvents();
-        $manager->add("New comment");
-        $events = $sink->get_events();
-        $this->assertCount(1, $events);
-        $event = reset($events);
-
-        // Checking that the event contains the expected values.
-        $this->assertInstanceOf('\core\event\blog_comment_created', $event);
-        $this->assertEquals($context, $event->get_context());
-        $this->assertEquals($this->postid, $event->other['itemid']);
-        $url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
-        $this->assertEquals($url, $event->get_url());
-        $this->assertEventContextNotUsed($event);
-    }
-
-    /**
-     * Test comment_deleted event.
-     */
-    public function test_blog_comment_deleted_event() {
-        global $USER, $CFG;
-
-        $this->setAdminUser();
-
-        require_once($CFG->dirroot . '/comment/lib.php');
-        $context = context_user::instance($USER->id);
-
-        $cmt = new stdClass();
-        $cmt->context = $context;
-        $cmt->courseid = $this->courseid;
-        $cmt->area = 'format_blog';
-        $cmt->itemid = $this->postid;
-        $cmt->showcount = 1;
-        $cmt->component = 'blog';
-        $manager = new comment($cmt);
-        $newcomment = $manager->add("New comment");
-
-        // Triggering and capturing the event.
-        $sink = $this->redirectEvents();
-        $manager->delete($newcomment->id);
-        $events = $sink->get_events();
-        $this->assertCount(1, $events);
-        $event = reset($events);
-
-        // Checking that the event contains the expected values.
-        $this->assertInstanceOf('\core\event\blog_comment_deleted', $event);
-        $this->assertEquals($context, $event->get_context());
-        $this->assertEquals($this->postid, $event->other['itemid']);
-        $url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
-        $this->assertEquals($url, $event->get_url());
-        $this->assertEventContextNotUsed($event);
-    }
-
     /**
      * Tests the core_blog_myprofile_navigation() function.
      */
     /**
      * Tests the core_blog_myprofile_navigation() function.
      */
index 24f4eee..2f5c5c6 100644 (file)
@@ -87,6 +87,10 @@ $string['entrysaved'] = 'Your entry has been saved';
 $string['entrytitle'] = 'Entry title';
 $string['eventblogentriesviewed'] = 'Blog entries viewed';
 $string['eventblogassociationadded'] = 'Blog association created';
 $string['entrytitle'] = 'Entry title';
 $string['eventblogentriesviewed'] = 'Blog entries viewed';
 $string['eventblogassociationadded'] = 'Blog association created';
+$string['eventblogassociationdeleted'] = 'Blog association deleted';
+$string['eventblogexternaladded'] = 'External blog registered';
+$string['eventblogexternalremoved'] = 'External blog unregistered';
+$string['eventblogexternalupdated'] = 'External blog updated';
 $string['evententryadded'] = 'Blog entry added';
 $string['evententrydeleted'] = 'Blog entry deleted';
 $string['evententryupdated'] = 'Blog entry updated';
 $string['evententryadded'] = 'Blog entry added';
 $string['evententrydeleted'] = 'Blog entry deleted';
 $string['evententryupdated'] = 'Blog entry updated';
@@ -94,6 +98,7 @@ $string['externalblogcrontime'] = 'External blog cron schedule';
 $string['externalblogdeleteconfirm'] = 'Unregister this external blog?';
 $string['externalblogdeleted'] = 'External blog unregistered';
 $string['externalblogs'] = 'External blogs';
 $string['externalblogdeleteconfirm'] = 'Unregister this external blog?';
 $string['externalblogdeleted'] = 'External blog unregistered';
 $string['externalblogs'] = 'External blogs';
+$string['eventexternalblogsviewed'] = 'External registered blogs viewed';
 $string['feedisinvalid'] = 'This feed is invalid';
 $string['feedisvalid'] = 'This feed is valid';
 $string['filterblogsby'] = 'Filter entries by...';
 $string['feedisinvalid'] = 'This feed is invalid';
 $string['feedisvalid'] = 'This feed is valid';
 $string['filterblogsby'] = 'Filter entries by...';
diff --git a/lib/classes/event/blog_association_deleted.php b/lib/classes/event/blog_association_deleted.php
new file mode 100644 (file)
index 0000000..95ad335
--- /dev/null
@@ -0,0 +1,120 @@
+<?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 for when a new blog entry is associated with a context.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace core\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Class for event to be triggered when a new blog entry is deleted with a context.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - int blogid: id of blog.
+ * }
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class blog_association_deleted extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->context = \context_system::instance();
+        $this->data['objecttable'] = 'blog_association';
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventblogassociationdeleted', 'core_blog');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' removed the associations from the blog entry with id "
+            . "'{$this->other['blogid']}'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/blog/index.php', array('entryid' => $this->other['blogid']));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception when validation fails.
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+
+        if (!isset($this->other['blogid'])) {
+            throw new \coding_exception('The \'blogid\' value must be set in other.');
+        }
+    }
+
+    /**
+     * Used for restore of objectid.
+     *
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        // Blogs are not included in backups, so no mapping required for restore.
+        return array('db' => 'blog_association', 'restore' => base::NOT_MAPPED);
+    }
+
+    /**
+     * Used for mappings of "other" data on restore.
+     *
+     * @return array
+     */
+    public static function get_other_mapping() {
+        // Blogs are not included in backups, so no mapping required for restore.
+        $othermapped = array();
+        $othermapped['blogid'] = array('db' => 'post', 'restore' => base::NOT_MAPPED);
+
+        return $othermapped;
+    }
+}
diff --git a/lib/classes/event/blog_external_added.php b/lib/classes/event/blog_external_added.php
new file mode 100644 (file)
index 0000000..e19a38f
--- /dev/null
@@ -0,0 +1,104 @@
+<?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 for when a new blog entry is associated with a context.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace core\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Class for event to be triggered when an external blog is added to moodle.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - string url: web address of the external blog added.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class blog_external_added extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'blog_external';
+        $this->data['crud'] = 'c';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventblogexternaladded', 'core_blog');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' added the external blog with the id '{$this->objectid}'" .
+            " from the web address '{$this->other['url']}'.";
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception when validation fails.
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->other['url'])) {
+            throw new \coding_exception('The \'url\' value must be set in other.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     *
+     * @return bool
+     */
+    public static function get_other_mapping() {
+        // No mapping required.
+        return false;
+    }
+
+    /**
+     * Used for restore of events.
+     *
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        // Blogs are not backed up, so no mapping required for restore.
+        return array('db' => 'blog_external', 'restore' => base::NOT_MAPPED);
+    }
+
+}
diff --git a/lib/classes/event/blog_external_removed.php b/lib/classes/event/blog_external_removed.php
new file mode 100644 (file)
index 0000000..1bab441
--- /dev/null
@@ -0,0 +1,73 @@
+<?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 for when a new blog entry is associated with a context.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace core\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Class for event to be triggered when an external blog is removed from moodle.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class blog_external_removed extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'blog_external';
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventblogexternalremoved', 'core_blog');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' removed the external blog with the id '{$this->objectid}'";
+    }
+
+    /**
+     * Used for restore of events.
+     *
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        // Blogs are not backed up, so no mapping required for restore.
+        return array('db' => 'blog_external', 'restore' => base::NOT_MAPPED);
+    }
+}
diff --git a/lib/classes/event/blog_external_updated.php b/lib/classes/event/blog_external_updated.php
new file mode 100644 (file)
index 0000000..56fb4df
--- /dev/null
@@ -0,0 +1,104 @@
+<?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 for when a new blog entry is associated with a context.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace core\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Class for event to be triggered when an external blog is updated in moodle.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - string url: web address of the external blog.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class blog_external_updated extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'blog_external';
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventblogexternalupdated', 'core_blog');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' added the external blog with the id '{$this->objectid}'" .
+            " with the web address '{$this->other['url']}'.";
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception when validation fails.
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->other['url'])) {
+            throw new \coding_exception('The \'url\' value must be set in other.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     *
+     * @return bool
+     */
+    public static function get_other_mapping() {
+        // No mapping required.
+        return false;
+    }
+
+    /**
+     * Used for restore of events.
+     *
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        // Blogs are not backed up, so no mapping required for restore.
+        return array('db' => 'blog_external', 'restore' => base::NOT_MAPPED);
+    }
+}
+
diff --git a/lib/classes/event/blog_external_viewed.php b/lib/classes/event/blog_external_viewed.php
new file mode 100644 (file)
index 0000000..77e391a
--- /dev/null
@@ -0,0 +1,76 @@
+<?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 for when a new blog entry is associated with a context.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace core\event;
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Class for event to be triggered when an external blog is viewed to moodle.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class blog_external_viewed extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['crud'] = 'r';
+        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventexternalblogsviewed', 'core_blog');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' viewed their registered external blogs";
+    }
+
+    /**
+     * Used for backup / restore of events.
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        // Blogs are not backed up, so no mapping required for restore.
+        return array('db' => 'blog_external', 'restore' => base::NOT_MAPPED);
+    }
+}
+
+
index a965a61..dbe9e01 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2016092300.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2016092300.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.