Permalink
Browse files

MDL-34436 - Blogs - Adding event triggers when adding a new entry, ed…

…iting an entry or remove an entry.
  • Loading branch information...
eriklundberg authored and jsnfwlr committed Jul 11, 2012
1 parent ca48fe5 commit af7e05d6ccac3b7242972c1c40a3e44c3836927e
Showing with 69 additions and 48 deletions.
  1. +63 −48 blog/locallib.php
  2. +6 −0 lib/db/events.php
View
@@ -76,7 +76,7 @@ class blog_entry implements renderable {
* @param mixed $idorparams A blog entry id if INT, or data for a new entry if array
*/
public function __construct($id=null, $params=null, $form=null) {
- global $DB, $PAGE;
+ global $DB, $PAGE, $CFG;
if (!empty($id)) {
$object = $DB->get_record('post', array('id' => $id));
@@ -89,6 +89,18 @@ public function __construct($id=null, $params=null, $form=null) {
}
}
+ if (!empty($CFG->useblogassociations)) {
+ $associations = $DB->get_records('blog_association', array('blogid' => $this->id));
+ foreach ($associations as $association) {
+ $context = context::instance_by_id($association->contextid);
+ if ($context->contextlevel == CONTEXT_COURSE) {
+ $this->courseassoc = $association->contextid;
+ } else if ($context->contextlevel == CONTEXT_MODULE) {
+ $this->modassoc = $association->contextid;
+ }
+ }
+ }
+
$this->form = $form;
}
@@ -104,44 +116,44 @@ public function prepare_render() {
$this->renderable->user = $DB->get_record('user', array('id'=>$this->userid));
- // Entry comments.
- if (!empty($CFG->usecomments) and $CFG->blogusecomments) {
+ // Entry comments.
+ if (!empty($CFG->usecomments) and $CFG->blogusecomments) {
require_once($CFG->dirroot . '/comment/lib.php');
-
- $cmt = new stdClass();
- $cmt->context = context_user::instance($this->userid);
- $cmt->courseid = $PAGE->course->id;
- $cmt->area = 'format_blog';
- $cmt->itemid = $this->id;
- $cmt->showcount = $CFG->blogshowcommentscount;
- $cmt->component = 'blog';
- $this->renderable->comment = new comment($cmt);
+
+ $cmt = new stdClass();
+ $cmt->context = context_user::instance($this->userid);
+ $cmt->courseid = $PAGE->course->id;
+ $cmt->area = 'format_blog';
+ $cmt->itemid = $this->id;
+ $cmt->showcount = $CFG->blogshowcommentscount;
+ $cmt->component = 'blog';
+ $this->renderable->comment = new comment($cmt);
}
-
- $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id);
- // External blog link.
- if ($this->uniquehash && $this->content) {
- if ($externalblog = $DB->get_record('blog_external', array('id' => $this->content))) {
- $urlparts = parse_url($externalblog->url);
+ $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id);
+
+ // External blog link.
+ if ($this->uniquehash && $this->content) {
+ if ($externalblog = $DB->get_record('blog_external', array('id' => $this->content))) {
+ $urlparts = parse_url($externalblog->url);
$this->renderable->externalblogtext = get_string('retrievedfrom', 'blog') . get_string('labelsep', 'langconfig');
- $this->renderable->externalblogtext .= html_writer::link($urlparts['scheme'] . '://'.$urlparts['host'], $externalblog->name);
- }
+ $this->renderable->externalblogtext .= html_writer::link($urlparts['scheme'] . '://'.$urlparts['host'], $externalblog->name);
+ }
}
// Retrieve associations
$this->renderable->unassociatedentry = false;
if (!empty($CFG->useblogassociations)) {
-
+
// Adding the entry associations data.
if ($associations = $associations = $DB->get_records('blog_association', array('blogid' => $this->id))) {
-
- // Check to see if the entry is unassociated with group/course level access.
- if ($this->publishstate == 'group' || $this->publishstate == 'course') {
- $this->renderable->unassociatedentry = true;
+
+ // Check to see if the entry is unassociated with group/course level access.
+ if ($this->publishstate == 'group' || $this->publishstate == 'course') {
+ $this->renderable->unassociatedentry = true;
}
-
- foreach ($associations as $key => $assocrec) {
+
+ foreach ($associations as $key => $assocrec) {
if (!$context = context::instance_by_id($assocrec->contextid, IGNORE_MISSING)) {
unset($associations[$key]);
@@ -150,14 +162,14 @@ public function prepare_render() {
// The renderer will need the contextlevel of the association.
$associations[$key]->contextlevel = $context->contextlevel;
-
- // Course associations.
+
+ // Course associations.
if ($context->contextlevel == CONTEXT_COURSE) {
$instancename = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); //TODO: performance!!!!
$associations[$key]->url = $assocurl = new moodle_url('/course/view.php', array('id' => $context->instanceid));
$associations[$key]->text = $instancename;
- $associations[$key]->icon = new pix_icon('i/course', $associations[$key]->text);
+ $associations[$key]->icon = new pix_icon('i/course', $associations[$key]->text);
}
// Mod associations.
@@ -174,11 +186,11 @@ public function prepare_render() {
$associations[$key]->url = new moodle_url('/mod/' . $modinfo->name . '/view.php', array('id' => $context->instanceid));
$associations[$key]->text = $instancename;
$associations[$key]->icon = new pix_icon('icon', $associations[$key]->text, $modinfo->name);
- }
+ }
}
}
- $this->renderable->blogassociations = $associations;
- }
+ $this->renderable->blogassociations = $associations;
+ }
// Entry attachments.
$this->renderable->attachments = $this->get_attachments();
@@ -193,23 +205,23 @@ public function prepare_render() {
*/
function get_attachments() {
- global $CFG;
-
- require_once($CFG->libdir.'/filelib.php');
-
+ global $CFG;
+
+ require_once($CFG->libdir.'/filelib.php');
+
$syscontext = context_system::instance();
-
+
$fs = get_file_storage();
- $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id);
+ $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id);
// Adding a blog_entry_attachment for each non-directory file.
- $attachments = array();
- foreach ($files as $file) {
- if ($file->is_directory()) {
- continue;
+ $attachments = array();
+ foreach ($files as $file) {
+ if ($file->is_directory()) {
+ continue;
}
- $attachments[] = new blog_entry_attachment($file, $this->id);
- }
+ $attachments[] = new blog_entry_attachment($file, $this->id);
+ }
return $attachments;
}
@@ -251,6 +263,7 @@ public function add() {
}
tag_set('post', $this->id, $this->tags);
+ events_trigger('blog_entry_added', $this);
}
/**
@@ -284,6 +297,7 @@ public function edit($params=array(), $form=null, $summaryoptions=array(), $atta
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);
}
/**
@@ -301,6 +315,7 @@ public function delete() {
tag_set('post', $this->id, array());
add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $this->userid, 'deleted blog entry with entry id# '. $this->id);
+ events_trigger('blog_entry_deleted', $this);
}
/**
@@ -636,7 +651,7 @@ public function print_entries() {
global $CFG, $USER, $DB, $OUTPUT, $PAGE;
$sitecontext = context_system::instance();
- // Blog renderer
+ // Blog renderer
$output = $PAGE->get_renderer('blog');
$page = optional_param('blogpage', 0, PARAM_INT);
@@ -1030,8 +1045,8 @@ public function __construct($file, $entryid) {
global $CFG;
$this->file = $file;
- $this->filename = $file->get_filename();
- $this->url = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/attachment/'.$entryid.'/'.$this->filename);
+ $this->filename = $file->get_filename();
+ $this->url = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/attachment/'.$entryid.'/'.$this->filename);
}
}
View
@@ -121,4 +121,10 @@
mod_created - int courseid, int cmid, text modulename - happens when a module is created
mod_updated - int courseid, int cmid, text modulename - happens when a module is updated
+=== blog events
+
+blog_entry_added - blog post object
+blog_entry_edited - blog post object
+blog_entry_deleteded - blog post object
+
*/

0 comments on commit af7e05d

Please sign in to comment.