MDL-32344 blog Adding a renderer to manage entries output
authorDavid Monllao <davidm@moodle.com>
Tue, 24 Jul 2012 01:34:08 +0000 (09:34 +0800)
committerDavid Monllao <davidm@moodle.com>
Fri, 10 Aug 2012 00:51:21 +0000 (08:51 +0800)
blog/edit.php
blog/locallib.php
blog/renderer.php [new file with mode: 0644]

index 3ed9155..e09cd64 100644 (file)
@@ -105,6 +105,9 @@ if ($id) {
 }
 $returnurl->param('userid', $userid);
 
+// Blog renderer
+$output = $PAGE->get_renderer('blog');
+
 $strblogs = get_string('blogs','blog');
 
 if ($action === 'delete'){
@@ -125,7 +128,11 @@ if ($action === 'delete'){
         $PAGE->set_title("$SITE->shortname: $strblogs");
         $PAGE->set_heading($SITE->fullname);
         echo $OUTPUT->header();
-        $entry->print_html();
+
+        // Output the entry
+        $entry->prepare_render();
+        echo $output->render($entry);
+
         echo '<br />';
         echo $OUTPUT->confirm(get_string('blogdeleteconfirm', 'blog'), new moodle_url('edit.php', $optionsyes),new moodle_url( 'index.php', $optionsno));
         echo $OUTPUT->footer();
index 813b6c6..4ede567 100644 (file)
@@ -39,7 +39,7 @@ require_once($CFG->libdir . '/filelib.php');
  * @copyright  2009 Nicolas Connault
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class blog_entry {
+class blog_entry implements renderable {
     // Public Database fields
     public $id;
     public $userid;
@@ -66,6 +66,9 @@ class blog_entry {
     public $form;
     public $tags = array();
 
+    // Data needed to render the entry
+    public $renderable;
+
     // Methods
     /**
      * Constructor. If given an id, will fetch the corresponding record from the DB.
@@ -89,242 +92,130 @@ class blog_entry {
         $this->form = $form;
     }
 
+
     /**
-     * Prints or returns the HTML for this blog entry.
-     *
-     * @param bool $return
-     * @return string
+     * Gets the required data to print the entry
      */
-    public function print_html($return=false) {
+    public function prepare_render() {
 
-        global $USER, $CFG, $COURSE, $DB, $OUTPUT, $PAGE;
+        global $DB, $CFG, $PAGE;
 
-        $user = $DB->get_record('user', array('id'=>$this->userid));
-        $cmttext = '';
-        if (!empty($CFG->usecomments) and $CFG->blogusecomments) {
-            require_once($CFG->dirroot . '/comment/lib.php');
-            // Comments
-            $cmt = new stdClass();
-            $cmt->context = context_user::instance($user->id);
-            $cmt->courseid = $PAGE->course->id;
-            $cmt->area = 'format_blog';
-            $cmt->itemid = $this->id;
-            $cmt->showcount = $CFG->blogshowcommentscount;
-            $cmt->component = 'blog';
-            $comment = new comment($cmt);
-            $cmttext = $comment->output(true);
-        }
-        $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id);
+        $this->renderable = new StdClass();
 
-        $options = array('overflowdiv'=>true);
-        $template['body'] = format_text($this->summary, $this->summaryformat, $options);
-        $template['title'] = format_string($this->subject);
-        $template['userid'] = $user->id;
-        $template['author'] = fullname($user);
-        $template['created'] = userdate($this->created);
+        $this->renderable->user = $DB->get_record('user', array('id'=>$this->userid));
 
-        if ($this->created != $this->lastmodified) {
-            $template['lastmod'] = userdate($this->lastmodified);
+        // Entry comments\r
+        if (!empty($CFG->usecomments) and $CFG->blogusecomments) {\r
+            require_once($CFG->dirroot . '/comment/lib.php');
+\r
+            $cmt = new stdClass();\r
+            $cmt->context = get_context_instance(CONTEXT_USER, $this->userid);\r
+            $cmt->courseid = $PAGE->course->id;\r
+            $cmt->area = 'format_blog';\r
+            $cmt->itemid = $this->id;\r
+            $cmt->showcount = $CFG->blogshowcommentscount;\r
+            $cmt->component = 'blog';\r
+            $this->renderable->comment = new comment($cmt);\r
         }
 
-        $template['publishstate'] = $this->publishstate;
-
-        $stredit = get_string('edit');
-        $strdelete = get_string('delete');
-
-        // Check to see if the entry is unassociated with group/course level access
-        $unassociatedentry = false;
-        if (!empty($CFG->useblogassociations) && ($this->publishstate == 'group' || $this->publishstate == 'course')) {
-            if (!$DB->record_exists('blog_association', array('blogid' => $this->id))) {
-                $unassociatedentry = true;
-            }
+        // Post data\r
+        $this->summary = file_rewrite_pluginfile_urls($this->summary, 'pluginfile.php', SYSCONTEXTID, 'blog', 'post', $this->id);\r
+\r
+        $options = array('overflowdiv'=>true);\r
+        $this->renderable->body = format_text($this->summary, $this->summaryformat, $options);\r
+        $this->renderable->title = format_string($this->subject);\r
+        $this->renderable->userid = $this->userid;\r
+        $this->renderable->author = fullname($this->renderable->user);\r
+        $this->renderable->created = userdate($this->created);\r
+        if ($this->created != $this->lastmodified) {\r
+            $this->renderable->lastmod = userdate($this->lastmodified);\r
+        }\r
+        $this->renderable->publishstate = $this->publishstate;
+
+        // External blog\r
+        if ($this->uniquehash && $this->content) {\r
+            if ($externalblog = $DB->get_record('blog_external', array('id' => $this->content))) {\r
+                $urlparts = parse_url($externalblog->url);\r
+                $this->renderable->externalblogtext = get_string('retrievedfrom', 'blog').get_string('labelsep', 'langconfig').html_writer::link($urlparts['scheme'].'://'.$urlparts['host'], $externalblog->name);\r
+            }\r
         }
-
-        // Start printing of the blog
-        $table = new html_table();
-        $table->cellspacing = 0;
-        $table->attributes['class'] = 'forumpost blog_entry blog'. ($unassociatedentry ? 'draft' : $template['publishstate']);
-        $table->attributes['id'] = 'b'.$this->id;
-        $table->width = '100%';
-
-        $picturecell = new html_table_cell();
-        $picturecell->attributes['class'] = 'picture left';
-        $picturecell->text = $OUTPUT->user_picture($user);
-
-        $table->head[] = $picturecell;
-
-        $topiccell = new html_table_cell();
-        $topiccell->attributes['class'] = 'topic starter';
-        $titlelink =  html_writer::link(new moodle_url('/blog/index.php', array('entryid' => $this->id)), $template['title']);
-        $topiccell->text = $OUTPUT->container($titlelink, 'subject');
-        $topiccell->text .= $OUTPUT->container_start('author');
-
-        $fullname = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($PAGE->course->id)));
-        $by = new stdClass();
-        $by->name =  html_writer::link(new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $PAGE->course->id)), $fullname);
-        $by->date = $template['created'];
-
-        $topiccell->text .= get_string('bynameondate', 'forum', $by);
-        $topiccell->text .= $OUTPUT->container_end();
-
-        if ($this->uniquehash && $this->content) {
-            if ($externalblog = $DB->get_record('blog_external', array('id' => $this->content))) {
-                $urlparts = parse_url($externalblog->url);
-                $topiccell->text .= $OUTPUT->container(get_string('retrievedfrom', 'blog').get_string('labelsep', 'langconfig').html_writer::link($urlparts['scheme'].'://'.$urlparts['host'], $externalblog->name), 'externalblog');
-            }
+\r
+        // Check to see if the entry is unassociated with group/course level access\r
+        $this->renderable->unassociatedentry = false;\r
+        if (!empty($CFG->useblogassociations) && ($this->publishstate == 'group' || $this->publishstate == 'course')) {\r
+            if (!$DB->record_exists('blog_association', array('blogid' => $this->id))) {\r
+                $this->renderable->unassociatedentry = true;\r
+            }\r
         }
 
-        $topiccell->header = false;
-        $table->head[] = $topiccell;
-
-        // Actual content
-        $mainrow = new html_table_row();
-
-        $leftsidecell = new html_table_cell();
-        $leftsidecell->attributes['class'] = 'left side';
-        $mainrow->cells[] = $leftsidecell;
-
-        $contentcell = new html_table_cell();
-        $contentcell->attributes['class'] = 'content';
-
-        $attachedimages = $OUTPUT->container($this->print_attachments(), 'attachments');
-
-        // retrieve associations in case they're needed early
-        $blogassociations = $DB->get_records('blog_association', array('blogid' => $this->id));
-
-        // determine text for publish state
-        switch ($template['publishstate']) {
-            case 'draft':
-                $blogtype = get_string('publishtonoone', 'blog');
-            break;
-            case 'site':
-                $blogtype = get_string('publishtosite', 'blog');
-            break;
-            case 'public':
-                $blogtype = get_string('publishtoworld', 'blog');
-            break;
-            default:
-                $blogtype = '';
-            break;
-
-        }
-
-        $contentcell->text .= $OUTPUT->container($blogtype, 'audience');
-
-        $contentcell->text .= $template['body'];
-        $contentcell->text .= $attachedimages;
-
-        // Uniquehash is used as a link to an external blog
-        if (!empty($this->uniquehash)) {
-            $contentcell->text .= $OUTPUT->container_start('externalblog');
-            $contentcell->text .= html_writer::link($this->uniquehash, get_string('linktooriginalentry', 'blog'));
-            $contentcell->text .= $OUTPUT->container_end();
-        }
-
-        // Links to tags
-        $officialtags = tag_get_tags_csv('post', $this->id, TAG_RETURN_HTML, 'official');
-        $defaulttags = tag_get_tags_csv('post', $this->id, TAG_RETURN_HTML, 'default');
-
-        if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) {
-            $contentcell->text .= $OUTPUT->container_start('tags');
-
-            if ($officialtags) {
-                $contentcell->text .= get_string('tags', 'tag') .': '. $OUTPUT->container($officialtags, 'officialblogtags');
-                if ($defaulttags) {
-                    $contentcell->text .=  ', ';
-                }
-            }
-            $contentcell->text .=  $defaulttags;
-            $contentcell->text .= $OUTPUT->container_end();
-        }
-
-        // Add associations
-        if (!empty($CFG->useblogassociations) && $blogassociations) {
-            $contentcell->text .= $OUTPUT->container_start('tags');
-            $assocstr = '';
-            $hascourseassocs = false;
-            $assoctype = '';
-
-            // First find and show the associated course
-            foreach ($blogassociations as $assocrec) {
-                $context = context::instance_by_id($assocrec->contextid);
-                if ($context->contextlevel ==  CONTEXT_COURSE) {
-                    $assocurl = new moodle_url('/course/view.php', array('id' => $context->instanceid));
-                    $text = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); //TODO: performance!!!!
-                    $assocstr .= $OUTPUT->action_icon($assocurl, new pix_icon('i/course', $text), null, array(), true);
-                    $hascourseassocs = true;
-                    $assoctype = get_string('course');
-                }
-            }
-
-            // Now show mod association
-            foreach ($blogassociations as $assocrec) {
-                $context = context::instance_by_id($assocrec->contextid);
-
-                if ($context->contextlevel ==  CONTEXT_MODULE) {
-                    if ($hascourseassocs) {
-                        $assocstr .= ', ';
-                        $hascourseassocs = false;
+        // Retrieve associations in case they're needed early
+        if (!empty($CFG->useblogassociations)) {\r
+            $associations = $DB->get_records('blog_association', array('blogid' => $this->id));
+
+            // Adding the entry associations data
+            if ($associations) {\r
+                foreach ($associations as $key => $assocrec) {\r
+                    $context = get_context_instance_by_id($assocrec->contextid);
+\r
+                    // Course associations\r
+                    if ($context->contextlevel ==  CONTEXT_COURSE) {
+                        $instancename = $DB->get_field('course', 'shortname', array('id' => $context->instanceid)); //TODO: performance!!!!
+\r
+                        $associations[$key]->type = get_string('course');
+                        $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);\r
                     }
 
-                    $modinfo = $DB->get_record('course_modules', array('id' => $context->instanceid));
-                    $modname = $DB->get_field('modules', 'name', array('id' => $modinfo->module));
+                    // Mod associations
+                    if ($context->contextlevel ==  CONTEXT_MODULE) {
 
-                    $assocurl = new moodle_url('/mod/'.$modname.'/view.php', array('id' => $modinfo->id));
-                    $text = $DB->get_field($modname, 'name', array('id' => $modinfo->instance)); //TODO: performance!!!!
-                    $assocstr .= $OUTPUT->action_icon($assocurl, new pix_icon('icon', $text, $modname), null, array(), true);
-                    $assocstr .= ', ';
-                    $assoctype = get_string('modulename', $modname);
+                        $modinfo = $DB->get_record('course_modules', array('id' => $context->instanceid));
+                        $modname = $DB->get_field('modules', 'name', array('id' => $modinfo->module));
+                        $instancename = $DB->get_field($modname, 'name', array('id' => $modinfo->instance)); //TODO: performance!!!!
 
+                        $associations[$key]->type = get_string('modulename', $modname);
+                        $associations[$key]->url = new moodle_url('/mod/'.$modname.'/view.php', array('id' => $modinfo->id));
+                        $associations[$key]->text = $instancename;
+                        $associations[$key]->icon = new pix_icon('icon', $associations[$key]->text, $modname);
+                    }\r
                 }
             }
-            $assocstr = substr($assocstr, 0, -2);
-            $contentcell->text .= get_string('associated', 'blog', $assoctype) . ': '. $assocstr;
+            $this->renderable->blogassociations = $associations;\r
+        }\r
 
-            $contentcell->text .= $OUTPUT->container_end();
-        }
+        // Entry attachments
+        $this->renderable->attachments = $this->get_attachments();
 
-        if ($unassociatedentry) {
-            $contentcell->text .= $OUTPUT->container(get_string('associationunviewable', 'blog'), 'noticebox');
-        }
+        $this->renderable->usercanedit = blog_user_can_edit_entry($this);
+    }
 
-    /// Commands
 
-        $contentcell->text .= $OUTPUT->container_start('commands');
+    /**
+     * Gets the entry attachments list
+     * @return array List of blog_entry_attachment instances
+     */
+    function get_attachments() {
+
+        global $CFG;\r
+\r
+        require_once($CFG->libdir.'/filelib.php');\r
+\r
+        $syscontext = get_context_instance(CONTEXT_SYSTEM);
+\r
+        $fs = get_file_storage();
+        $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id);\r
 
-        if (blog_user_can_edit_entry($this)) {
-            if (empty($this->uniquehash)) {
-                //External blog entries should not be edited
-                $contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php',
-                                                        array('action' => 'edit', 'entryid' => $this->id)),
-                                                        $stredit) . ' | ';
+        // Adding a blog_entry_attachment for each non-directory file
+        $attachments = array();\r
+        foreach ($files as $file) {\r
+            if ($file->is_directory()) {\r
+                continue;\r
             }
-            $contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php',
-                                                    array('action' => 'delete', 'entryid' => $this->id)),
-                                                    $strdelete) . ' | ';
-        }
-
-        $contentcell->text .= html_writer::link(new moodle_url('/blog/index.php', array('entryid' => $this->id)), get_string('permalink', 'blog'));
-
-        $contentcell->text .= $OUTPUT->container_end();
-
-        if (isset($template['lastmod']) ){
-            $contentcell->text .= '<div style="font-size: 55%;">';
-            $contentcell->text .= ' [ '.get_string('modified').': '.$template['lastmod'].' ]';
-            $contentcell->text .= '</div>';
-        }
-
-        //add comments under everything
-        $contentcell->text .= $cmttext;
-
-        $mainrow->cells[] = $contentcell;
-        $table->data = array($mainrow);
+            $attachments[] = new blog_entry_attachment($file, $this->id);\r
+        }\r
 
-        if ($return) {
-            return html_writer::table($table);
-        } else {
-            echo html_writer::table($table);
-        }
+        return $attachments;
     }
 
     /**
@@ -480,66 +371,6 @@ class blog_entry {
         $fs->delete_area_files(SYSCONTEXTID, 'blog', 'post', $this->id);
     }
 
-    /**
-     * if return=html, then return a html string.
-     * if return=text, then return a text-only string.
-     * otherwise, print HTML for non-images, and return image HTML
-     *
-     * @param bool $return Whether to return or print the generated code
-     * @return void
-     */
-    public function print_attachments($return=false) {
-        global $CFG, $OUTPUT;
-
-        require_once($CFG->libdir.'/filelib.php');
-
-        $fs = get_file_storage();
-
-        $syscontext = context_system::instance();
-
-        $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id);
-
-        $imagereturn = "";
-        $output = "";
-
-        $strattachment = get_string("attachment", "forum");
-
-        foreach ($files as $file) {
-            if ($file->is_directory()) {
-                continue;
-            }
-
-            $filename = $file->get_filename();
-            $ffurl    = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/attachment/'.$this->id.'/'.$filename);
-            $mimetype = $file->get_mimetype();
-
-            $image = $OUTPUT->pix_icon(file_file_icon($file), $filename, 'moodle', array('class'=>'icon'));
-
-            if ($return == "html") {
-                $output .= html_writer::link($ffurl, $image);
-                $output .= html_writer::link($ffurl, $filename);
-
-            } else if ($return == "text") {
-                $output .= "$strattachment $filename:\n$ffurl\n";
-
-            } else {
-                if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) {    // Image attachments don't get printed as links
-                    $imagereturn .= '<br /><img src="'.$ffurl.'" alt="" />';
-                } else {
-                    $imagereturn .= html_writer::link($ffurl, $image);
-                    $imagereturn .= format_text(html_writer::link($ffurl, $filename), FORMAT_HTML, array('context'=>$syscontext));
-                }
-            }
-        }
-
-        if ($return) {
-            return $output;
-        }
-
-        return $imagereturn;
-
-    }
-
     /**
      * function to attach tags into an entry
      * @return void
@@ -807,9 +638,12 @@ class blog_listing {
      * @return void
      */
     public function print_entries() {
-        global $CFG, $USER, $DB, $OUTPUT;
+        global $CFG, $USER, $DB, $OUTPUT, $PAGE;
         $sitecontext = context_system::instance();
 
+        // Blog renderer\r
+        $output = $PAGE->get_renderer('blog');
+
         $page  = optional_param('blogpage', 0, PARAM_INT);
         $limit = optional_param('limit', get_user_preferences('blogpagesize', 10), PARAM_INT);
         $start = $page * $limit;
@@ -860,10 +694,13 @@ class blog_listing {
 
         if ($entries) {
             $count = 0;
-
             foreach ($entries as $entry) {
                 $blogentry = new blog_entry(null, $entry);
-                $blogentry->print_html();
+
+                // Get the required blog entry data to render it
+                $blogentry->prepare_render();
+                echo $output->render($blogentry);
+
                 $count++;
             }
 
@@ -1161,3 +998,30 @@ class blog_filter_search extends blog_filter {
         $this->params[] = "%$searchterm%";
     }
 }
+
+
+/**
+ * Renderable class to represent an entry attachment
+ */
+class blog_entry_attachment implements renderable {
+
+    public $filename;
+    public $url;
+    public $file;
+
+    /**
+     * @param stored_file $file
+     * @param int $entryid Attachment entry id
+     */
+    public function __construct($file, $entryid) {
+
+        global $OUTPUT, $CFG;
+
+        $syscontext = get_context_instance(CONTEXT_SYSTEM);
+
+        $this->file = $file;
+        $this->filename = $file->get_filename();\r
+        $this->url    = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.SYSCONTEXTID.'/blog/attachment/'.$entryid.'/'.$this->filename);\r
+    }
+
+}
diff --git a/blog/renderer.php b/blog/renderer.php
new file mode 100644 (file)
index 0000000..d103f94
--- /dev/null
@@ -0,0 +1,272 @@
+<?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/>.
+
+/**
+ * Renderers for outputting blog data
+ *
+ * @package    core_blog
+ * @subpackage blog
+ * @copyright  2012 David MonllaĆ³
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+\r
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Blog renderer
+ */
+class core_blog_renderer extends plugin_renderer_base {
+
+    /**
+     * Renders a blog entry
+     *
+     * @param blog_entry $entry
+     * @return string The table HTML
+     */
+    public function render_blog_entry(blog_entry $entry) {
+
+        global $CFG;
+
+        $stredit = get_string('edit');
+        $strdelete = get_string('delete');
+
+        // Start printing of the blog
+        $table = new html_table();
+        $table->cellspacing = 0;
+        $table->attributes['class'] = 'forumpost blog_entry blog'. ($entry->renderable->unassociatedentry ? 'draft' : $entry->renderable->publishstate);
+        $table->attributes['id'] = 'b'.$entry->id;
+        $table->width = '100%';
+
+        $picturecell = new html_table_cell();
+        $picturecell->attributes['class'] = 'picture left';
+        $picturecell->text = $this->output->user_picture($entry->renderable->user);
+
+        $table->head[] = $picturecell;
+
+        $topiccell = new html_table_cell();
+        $topiccell->attributes['class'] = 'topic starter';
+        $titlelink =  html_writer::link(new moodle_url('/blog/index.php', array('entryid' => $entry->id)), $entry->renderable->title);
+        $topiccell->text = $this->output->container($titlelink, 'subject');
+        $topiccell->text .= $this->output->container_start('author');
+\r
+        // Post by
+        $by = new stdClass();
+        $coursecontext = get_context_instance(CONTEXT_COURSE, $this->page->course->id);
+        $fullname = fullname($entry->renderable->user, has_capability('moodle/site:viewfullnames', $coursecontext));
+        $userurlparams = array('id' => $entry->renderable->user->id, 'course' => $this->page->course->id);
+        $by->name =  html_writer::link(new moodle_url('/user/view.php', $userurlparams), $fullname);
+        $by->date = $entry->renderable->created;
+
+        $topiccell->text .= get_string('bynameondate', 'forum', $by);
+        $topiccell->text .= $this->output->container_end();
+
+        // Adding external blog link
+        if (!empty($entry->renderable->externalblogtext)) {
+            $topiccell->text .= $this->output->container($entry->renderable->externalblogtext, 'externalblog');
+        }
+
+        $topiccell->header = false;
+        $table->head[] = $topiccell;
+
+        // Actual content
+        $mainrow = new html_table_row();
+
+        $leftsidecell = new html_table_cell();
+        $leftsidecell->attributes['class'] = 'left side';
+        $mainrow->cells[] = $leftsidecell;
+
+        $contentcell = new html_table_cell();
+        $contentcell->attributes['class'] = 'content';
+\r
+        // Determine text for publish state\r
+        switch ($entry->renderable->publishstate) {\r
+            case 'draft':\r
+                $blogtype = get_string('publishtonoone', 'blog');\r
+                break;\r
+            case 'site':\r
+                $blogtype = get_string('publishtosite', 'blog');\r
+                break;\r
+            case 'public':\r
+                $blogtype = get_string('publishtoworld', 'blog');\r
+                break;\r
+            default:\r
+                $blogtype = '';\r
+                break;\r
+\r
+        }\r
+        $contentcell->text .= $this->output->container($blogtype, 'audience');
+
+        // Entry body\r
+        $contentcell->text .= $entry->renderable->body;
+
+        // Entry attachments\r
+        $attachmentsoutputs = array();
+        if ($entry->renderable->attachments) {
+            foreach ($entry->renderable->attachments as $attachment) {
+                $attachmentsoutputs[] = $this->render($attachment, false);
+            }
+            $contentcell->text .= $this->output->container(implode(', ', $attachmentsoutputs), 'attachments');
+        }
+
+        // Uniquehash is used as a link to an external blog
+        if (!empty($entry->uniquehash)) {
+            $contentcell->text .= $this->output->container_start('externalblog');
+            $contentcell->text .= html_writer::link($entry->uniquehash, get_string('linktooriginalentry', 'blog'));
+            $contentcell->text .= $this->output->container_end();
+        }
+
+        // Links to tags
+        $officialtags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'official');
+        $defaulttags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'default');
+
+        if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) {
+            $contentcell->text .= $this->output->container_start('tags');
+
+            if ($officialtags) {
+                $contentcell->text .= get_string('tags', 'tag') .': '. $this->output->container($officialtags, 'officialblogtags');
+                if ($defaulttags) {
+                    $contentcell->text .=  ', ';
+                }
+            }
+            $contentcell->text .=  $defaulttags;
+            $contentcell->text .= $this->output->container_end();
+        }
+
+        // Add associations
+        if (!empty($CFG->useblogassociations) && !empty($entry->renderable->blogassociations)) {
+            $contentcell->text .= $this->output->container_start('tags');
+            $hascourseassocs = false;
+\r
+            // First find and show the associated course\r
+            $coursesstr = '';
+            $coursesarray = array();
+            foreach ($entry->renderable->blogassociations as $assocrec) {
+                $context = get_context_instance_by_id($assocrec->contextid);
+                if ($context->contextlevel ==  CONTEXT_COURSE) {
+                    $coursesarray[] = $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
+                }
+            }
+            if (!empty($coursesarray)) {
+                $coursesstr .= get_string('associated', 'blog', $assocrec->type) . ': ' . implode(', ', $coursesarray);
+            }
+
+            // Now show mod association
+            $modulesstr = '';\r
+            $modulesarray = array();
+            foreach ($entry->renderable->blogassociations as $assocrec) {
+                $context = get_context_instance_by_id($assocrec->contextid);
+
+                if ($context->contextlevel ==  CONTEXT_MODULE) {
+                    $str = get_string('associated', 'blog', $assocrec->type) . ': ';
+                    $str .= $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
+                    $modulesarray[] = $str;
+                }
+            }
+            if (!empty($modulesarray)) {
+                if (!empty($coursesarray)) {
+                    $modulesstr .= '<br/>';
+                }
+                $modulesstr .= implode('<br/>', $modulesarray);
+            }
+
+            $contentcell->text .= $coursesstr.$modulesstr;
+            $contentcell->text .= $this->output->container_end();
+        }
+
+        if ($entry->renderable->unassociatedentry) {
+            $contentcell->text .= $this->output->container(get_string('associationunviewable', 'blog'), 'noticebox');
+        }
+
+        /// Commands
+        $contentcell->text .= $this->output->container_start('commands');
+        if ($entry->renderable->usercanedit) {
+            if (empty($entry->uniquehash)) {
+                //External blog entries should not be edited
+                $contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php',
+                                                        array('action' => 'edit', 'entryid' => $entry->id)),
+                                                        $stredit) . ' | ';
+            }
+            $contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php',
+                                                    array('action' => 'delete', 'entryid' => $entry->id)),
+                                                    $strdelete) . ' | ';
+        }
+
+        $entryurl = new moodle_url('/blog/index.php', array('entryid' => $entry->id));
+        $contentcell->text .= html_writer::link($entryurl, get_string('permalink', 'blog'));
+
+        $contentcell->text .= $this->output->container_end();
+
+        if (isset($entry->renderable->lastmod) ) {
+            $contentcell->text .= '<div>';
+            $contentcell->text .= ' [ '.get_string('modified').': '.$entry->renderable->lastmod.' ]';
+            $contentcell->text .= '</div>';
+        }
+
+        //add comments under everything
+        if (!empty($entry->renderable->comment)) {
+            $contentcell->text .= $entry->renderable->comment->output(true);
+        }
+
+        $mainrow->cells[] = $contentcell;
+        $table->data = array($mainrow);
+
+        return html_writer::table($table);
+    }
+
+    /**
+     * Renders an entry attachment
+     *
+     * if return=html, then return a html string.
+     * if return=text, then return a text-only string.
+     * otherwise, print HTML for non-images, and return image HTML
+     *
+     * @param blog_entry_attachment $attachment
+     * @param bool $return Whether to return or print the generated code
+     * @return string List of attachments depending on the $return input
+     */
+    public function render_blog_entry_attachment(blog_entry_attachment $attachment, $return = false) {
+
+        $syscontext = get_context_instance(CONTEXT_SYSTEM);
+        $strattachment = get_string("attachment", "forum");
+
+        $image = $this->output->pix_icon(file_file_icon($attachment->file), $attachment->filename, 'moodle', array('class'=>'icon'));
+
+        $o = '';
+        $imagereturn = '';
+
+        if ($return == "html") {
+            $o .= html_writer::link($attachment->url, $image);
+            $o .= html_writer::link($attachment->url, $attachment->filename);
+
+        } else if ($return == "text") {
+            $o .= "$strattachment $attachment->filename:\n$attachment->url\n";
+
+        } else {
+            if (file_mimetype_in_typegroup($attachment->file->get_mimetype(), 'web_image')) {    // Image attachments don't get printed as links
+                $imagereturn .= '<br /><img src="'.$attachment->url.'" alt="" />';
+            } else {
+                $imagereturn .= html_writer::link($attachment->url, $image);
+                $imagereturn .= format_text(html_writer::link($attachment->url, $attachment->filename), FORMAT_HTML, array('context'=>$syscontext));
+            }
+        }
+\r
+        if ($return) {\r
+            return $o;\r
+        }\r
+\r
+        return $imagereturn;
+    }
+}