From af7e05d6ccac3b7242972c1c40a3e44c3836927e Mon Sep 17 00:00:00 2001 From: Erik Lundberg Date: Wed, 11 Jul 2012 14:26:44 +0200 Subject: [PATCH] MDL-34436 - Blogs - Adding event triggers when adding a new entry, editing an entry or remove an entry. --- blog/locallib.php | 113 ++++++++++++++++++++++++++-------------------- lib/db/events.php | 6 +++ 2 files changed, 70 insertions(+), 49 deletions(-) diff --git a/blog/locallib.php b/blog/locallib.php index 304b3da4191..67fa66d71a0 100644 --- a/blog/locallib.php +++ b/blog/locallib.php @@ -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 @@ class blog_entry implements renderable { } } + 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 @@ class blog_entry implements renderable { $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); - } - - $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); + $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->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 @@ class blog_entry implements renderable { // 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 @@ class blog_entry implements renderable { $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 @@ class blog_entry implements renderable { */ 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 @@ class blog_entry implements renderable { } tag_set('post', $this->id, $this->tags); + events_trigger('blog_entry_added', $this); } /** @@ -284,6 +297,7 @@ class blog_entry implements renderable { 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 @@ class blog_entry implements renderable { 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 @@ class blog_listing { 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 @@ class blog_entry_attachment implements renderable { 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); } } diff --git a/lib/db/events.php b/lib/db/events.php index 51a8958c849..769ad0b18a5 100644 --- a/lib/db/events.php +++ b/lib/db/events.php @@ -121,4 +121,10 @@ mod_deleted - int courseid, int cmid, text modulename - happens when a module is 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 + */