diff --git a/blog/index.php b/blog/index.php index 6b3d5db9d31..efa99ea1373 100644 --- a/blog/index.php +++ b/blog/index.php @@ -223,5 +223,13 @@ $bloglisting = new blog_listing($blogheaders['filters']); $bloglisting->print_entries(); echo $OUTPUT->footer(); - -add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&tagid='.@$tagid.'&tag='.$tag, 'view blog entry'); +$eventparams = array( + 'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid, + 'search' => $search, 'fromstart' => $start) +); +if (!empty($userid)) { + $eventparams['relateduserid'] = $userid; +} +$eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid; +$event = \core\event\blog_entries_viewed::create($eventparams); +$event->trigger(); diff --git a/blog/locallib.php b/blog/locallib.php index a4e42105b9c..c6a1084c8e9 100644 --- a/blog/locallib.php +++ b/blog/locallib.php @@ -339,46 +339,62 @@ class blog_entry implements renderable { } /** - * function to add all context associations to an entry - * @param int entry - data object processed to include all 'entry' fields and extra data from the edit_form object + * Function to add all context associations to an entry. + * TODO : Remove $action in 2.9 (MDL-41330) + * + * @param string $action - This does nothing, do not use it. This is present only for Backward compatibility. */ - public function add_associations($action='add') { - global $DB, $USER; + public function add_associations($action = null) { + + if (!empty($action)) { + debugging('blog_entry->add_associations() does not accept any argument', DEBUG_DEVELOPER); + } $this->remove_associations(); if (!empty($this->courseassoc)) { - $this->add_association($this->courseassoc, $action); + $this->add_association($this->courseassoc); } if (!empty($this->modassoc)) { - $this->add_association($this->modassoc, $action); + $this->add_association($this->modassoc); } } /** - * add a single association for a blog entry - * @param int contextid - id of context to associate with the blog entry + * Add a single association for a blog entry + * TODO : Remove $action in 2.9 (MDL-41330) + * + * @param int $contextid - id of context to associate with the blog entry. + * @param string $action - This does nothing, do not use it. This is present only for Backward compatibility. */ - public function add_association($contextid, $action='add') { - global $DB, $USER; + public function add_association($contextid, $action = null) { + global $DB; + + if (!empty($action)) { + debugging('blog_entry->add_association() accepts only one argument', DEBUG_DEVELOPER); + } $assocobject = new StdClass; $assocobject->contextid = $contextid; $assocobject->blogid = $this->id; - $DB->insert_record('blog_association', $assocobject); + $id = $DB->insert_record('blog_association', $assocobject); + // Trigger an association created event. $context = context::instance_by_id($contextid); - $courseid = null; - + $eventparam = array( + 'objectid' => $id, + 'other' => array('associateid' => $context->instanceid, 'subject' => $this->subject, 'blogid' => $this->id), + 'relateduserid' => $this->userid + ); if ($context->contextlevel == CONTEXT_COURSE) { - $courseid = $context->instanceid; - add_to_log($courseid, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject); + $eventparam['other']['associatetype'] = 'course'; + } else if ($context->contextlevel == CONTEXT_MODULE) { - $cm = $DB->get_record('course_modules', array('id' => $context->instanceid)); - $modulename = $DB->get_field('modules', 'name', array('id' => $cm->module)); - add_to_log($cm->course, 'blog', $action, 'index.php?userid='.$this->userid.'&entryid='.$this->id, $this->subject, $cm->id, $this->userid); + $eventparam['other']['associatetype'] = 'coursemodule'; } + $event = \core\event\blog_association_created::create($eventparam); + $event->trigger(); } /** diff --git a/blog/tests/bloglib_test.php b/blog/tests/bloglib_test.php index 689e3bef6f0..47c8a299163 100644 --- a/blog/tests/bloglib_test.php +++ b/blog/tests/bloglib_test.php @@ -250,5 +250,137 @@ class core_bloglib_testcase extends advanced_testcase { $this->assertEventLegacyLogData($arr, $event); $this->assertEventLegacyData($blog, $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); + $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); + } + + /** + * 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, + 'other' => array('associateid' => 2 , 'blogid' => 3, 'subject' => 'blog subject'))); + } catch (coding_exception $e) { + $this->assertContains('Invalid associatetype', $e->getMessage()); + } + try { + \core\event\blog_association_created::create(array( + 'contextid' => 1, + 'objectid' => 3, + 'other' => array('associateid' => 2 , 'blogid' => 3, 'associatetype' => 'random', 'subject' => 'blog subject'))); + } catch (coding_exception $e) { + $this->assertContains('Invalid associatetype', $e->getMessage()); + } + // Make sure associateid validations work. + try { + \core\event\blog_association_created::create(array( + 'contextid' => 1, + 'objectid' => 3, + 'other' => array('blogid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject'))); + } catch (coding_exception $e) { + $this->assertContains('Associate id must be set', $e->getMessage()); + } + // Make sure blogid validations work. + try { + \core\event\blog_association_created::create(array( + 'contextid' => 1, + 'objectid' => 3, + 'other' => array('associateid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject'))); + } catch (coding_exception $e) { + $this->assertContains('Blog id must be set', $e->getMessage()); + } + // Make sure blogid validations work. + try { + \core\event\blog_association_created::create(array( + 'contextid' => 1, + 'objectid' => 3, + 'other' => array('blogid' => 3, 'associateid' => 3, 'associatetype' => 'course'))); + } catch (coding_exception $e) { + $this->assertContains('Subject must be set', $e->getMessage()); + } + } + + /** + * Tests for event blog_entries_viewed. + */ + public function test_blog_entries_viewed_event() { + + $this->setAdminUser(); + $this->resetAfterTest(); + $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); + } } diff --git a/blog/upgrade.txt b/blog/upgrade.txt new file mode 100644 index 00000000000..8695b956bc1 --- /dev/null +++ b/blog/upgrade.txt @@ -0,0 +1,4 @@ +=== 2.7 === + +* blog_entry->add_association() does not accept any params. +* blog_entry->add_associations() accepts only one param. \ No newline at end of file diff --git a/lang/en/blog.php b/lang/en/blog.php index 593785977ff..a7d5d29c6d5 100644 --- a/lang/en/blog.php +++ b/lang/en/blog.php @@ -86,6 +86,8 @@ $string['entrybodyonlydesc'] = 'Entry description'; $string['entryerrornotyours'] = 'This entry is not yours'; $string['entrysaved'] = 'Your entry has been saved'; $string['entrytitle'] = 'Entry title'; +$string['eventblogentriesviewed'] = 'Blog entries viewed'; +$string['eventblogassociationcreated'] = 'Blog association created'; $string['evententryadded'] = 'Blog entry added'; $string['evententrydeleted'] = 'Blog entry deleted'; $string['evententryupdated'] = 'Blog entry updated'; diff --git a/lib/classes/event/blog_association_created.php b/lib/classes/event/blog_association_created.php new file mode 100644 index 00000000000..5670f52c1f6 --- /dev/null +++ b/lib/classes/event/blog_association_created.php @@ -0,0 +1,108 @@ +. +/** + * Event for when a new blog entry is associated with a context. + * + * @package core + * @copyright 2013 onwards Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * blog_association_created + * + * Class for event to be triggered when a new blog entry is associated with a context. + * + * @package core + * @copyright 2013 onwards Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class blog_association_created extends \core\event\base { + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->context = \context_system::instance(); + $this->data['objecttable'] = 'blog_association'; + $this->data['crud'] = 'c'; + $this->data['level'] = self::LEVEL_PARTICIPATING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventblogassociationadded', 'core_blog'); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @return string + */ + public function get_description() { + return "Blog association added between entry id $this->other['blogid'] and $this->other['associatetype'] with id + $this->other['associateid']"; + } + + /** + * Returns relevant URL. + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/blog/index.php', array('entryid' => $this->objectid, 'userid' => $this->userid)); + } + + /** + * replace add_to_log() statement. + * + * @return array of parameters to be passed to legacy add_to_log() function. + */ + protected function get_legacy_logdata() { + if ($this->other['associatetype'] === 'course') { + return array (SITEID, 'blog', 'add association', 'index.php?userid=' . $this->relateduserid. '&entryid=' . + $this->other['blogid'], $this->other['subject'], 0, $this->relateduserid); + } else { + return array (SITEID, 'blog', 'add association', 'index.php?userid=' . $this->relateduserid. '&entryid=' . + $this->other['blogid'], $this->other['subject'], $this->other['associateid'], $this->relateduserid); + } + } + + /** + * Custom validations. + * + * @throws \coding_exception when validation fails. + * @return void + */ + protected function validate_data() { + if (empty($this->other['associatetype']) || ($this->other['associatetype'] !== 'course' + && $this->other['associatetype'] !== 'coursemodule')) { + throw new \coding_exception('Invalid associatetype in event blog_association_created.'); + } else if (!isset($this->other['blogid'])) { + throw new \coding_exception('Blog id must be set in event blog_association_created.'); + } else if (!isset($this->other['associateid'])) { + throw new \coding_exception('Associate id must be set in event blog_association_created.'); + } else if (!isset($this->other['subject'])) { + throw new \coding_exception('Subject must be set in event blog_association_created.'); + } + } +} diff --git a/lib/classes/event/blog_entries_viewed.php b/lib/classes/event/blog_entries_viewed.php new file mode 100644 index 00000000000..bf4dbafa066 --- /dev/null +++ b/lib/classes/event/blog_entries_viewed.php @@ -0,0 +1,97 @@ +. +/** + * Event for when blog entries are viewed. + * + * @package core + * @copyright 2013 onwards Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * blog_entries_viewed + * + * Class for event to be triggered when blog entries are viewed. + * + * @package core + * @copyright 2013 onwards Ankit Agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class blog_entries_viewed extends base { + + /** @var array List of url params accepted*/ + private $validparams = array('entryid', 'tagid', 'userid', 'modid', 'groupid', 'courseid', 'search', 'fromstart'); + + /** + * Set basic properties for the event. + */ + protected function init() { + $this->context = \context_system::instance(); + $this->data['crud'] = 'r'; + $this->data['level'] = self::LEVEL_PARTICIPATING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventblogentriesviewed', 'core_blog'); + } + + /** + * Returns non-localised event description with id's for admin use only. + * + * @return string + */ + public function get_description() { + return 'Blog entries viewed'; + } + + /** + * Returns relevant URL. + * @return \moodle_url + */ + public function get_url() { + $params = array(); + foreach ($this->validparams as $param) { + if (!empty($this->other[$param])) { + $params[$param] = $this->other[$param]; + } + } + return new \moodle_url('/blog/index.php', $params); + } + + /** + * replace add_to_log() statement. + * + * @return array of parameters to be passed to legacy add_to_log() function. + */ + protected function get_legacy_logdata() { + $params = array(); + foreach ($this->validparams as $param) { + if (!empty($this->other[$param])) { + $params[$param] = $this->other[$param]; + } + } + $url = new \moodle_url('index.php', $params); + return array (SITEID, 'blog', 'view', $url->out(), 'view blog entry'); + } +}