MDL-32344 blog Adding a renderer to manage entries output
[moodle.git] / blog / renderer.php
CommitLineData
2591c7ae
DM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Renderers for outputting blog data
19 *
20 * @package core_blog
21 * @subpackage blog
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25\r
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Blog renderer
30 */
31class core_blog_renderer extends plugin_renderer_base {
32
33 /**
34 * Renders a blog entry
35 *
36 * @param blog_entry $entry
37 * @return string The table HTML
38 */
39 public function render_blog_entry(blog_entry $entry) {
40
41 global $CFG;
42
43 $stredit = get_string('edit');
44 $strdelete = get_string('delete');
45
46 // Start printing of the blog
47 $table = new html_table();
48 $table->cellspacing = 0;
49 $table->attributes['class'] = 'forumpost blog_entry blog'. ($entry->renderable->unassociatedentry ? 'draft' : $entry->renderable->publishstate);
50 $table->attributes['id'] = 'b'.$entry->id;
51 $table->width = '100%';
52
53 $picturecell = new html_table_cell();
54 $picturecell->attributes['class'] = 'picture left';
55 $picturecell->text = $this->output->user_picture($entry->renderable->user);
56
57 $table->head[] = $picturecell;
58
59 $topiccell = new html_table_cell();
60 $topiccell->attributes['class'] = 'topic starter';
61 $titlelink = html_writer::link(new moodle_url('/blog/index.php', array('entryid' => $entry->id)), $entry->renderable->title);
62 $topiccell->text = $this->output->container($titlelink, 'subject');
63 $topiccell->text .= $this->output->container_start('author');
64\r
65 // Post by
66 $by = new stdClass();
67 $coursecontext = get_context_instance(CONTEXT_COURSE, $this->page->course->id);
68 $fullname = fullname($entry->renderable->user, has_capability('moodle/site:viewfullnames', $coursecontext));
69 $userurlparams = array('id' => $entry->renderable->user->id, 'course' => $this->page->course->id);
70 $by->name = html_writer::link(new moodle_url('/user/view.php', $userurlparams), $fullname);
71 $by->date = $entry->renderable->created;
72
73 $topiccell->text .= get_string('bynameondate', 'forum', $by);
74 $topiccell->text .= $this->output->container_end();
75
76 // Adding external blog link
77 if (!empty($entry->renderable->externalblogtext)) {
78 $topiccell->text .= $this->output->container($entry->renderable->externalblogtext, 'externalblog');
79 }
80
81 $topiccell->header = false;
82 $table->head[] = $topiccell;
83
84 // Actual content
85 $mainrow = new html_table_row();
86
87 $leftsidecell = new html_table_cell();
88 $leftsidecell->attributes['class'] = 'left side';
89 $mainrow->cells[] = $leftsidecell;
90
91 $contentcell = new html_table_cell();
92 $contentcell->attributes['class'] = 'content';
93\r
94 // Determine text for publish state\r
95 switch ($entry->renderable->publishstate) {\r
96 case 'draft':\r
97 $blogtype = get_string('publishtonoone', 'blog');\r
98 break;\r
99 case 'site':\r
100 $blogtype = get_string('publishtosite', 'blog');\r
101 break;\r
102 case 'public':\r
103 $blogtype = get_string('publishtoworld', 'blog');\r
104 break;\r
105 default:\r
106 $blogtype = '';\r
107 break;\r
108\r
109 }\r
110 $contentcell->text .= $this->output->container($blogtype, 'audience');
111
112 // Entry body\r
113 $contentcell->text .= $entry->renderable->body;
114
115 // Entry attachments\r
116 $attachmentsoutputs = array();
117 if ($entry->renderable->attachments) {
118 foreach ($entry->renderable->attachments as $attachment) {
119 $attachmentsoutputs[] = $this->render($attachment, false);
120 }
121 $contentcell->text .= $this->output->container(implode(', ', $attachmentsoutputs), 'attachments');
122 }
123
124 // Uniquehash is used as a link to an external blog
125 if (!empty($entry->uniquehash)) {
126 $contentcell->text .= $this->output->container_start('externalblog');
127 $contentcell->text .= html_writer::link($entry->uniquehash, get_string('linktooriginalentry', 'blog'));
128 $contentcell->text .= $this->output->container_end();
129 }
130
131 // Links to tags
132 $officialtags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'official');
133 $defaulttags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'default');
134
135 if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) {
136 $contentcell->text .= $this->output->container_start('tags');
137
138 if ($officialtags) {
139 $contentcell->text .= get_string('tags', 'tag') .': '. $this->output->container($officialtags, 'officialblogtags');
140 if ($defaulttags) {
141 $contentcell->text .= ', ';
142 }
143 }
144 $contentcell->text .= $defaulttags;
145 $contentcell->text .= $this->output->container_end();
146 }
147
148 // Add associations
149 if (!empty($CFG->useblogassociations) && !empty($entry->renderable->blogassociations)) {
150 $contentcell->text .= $this->output->container_start('tags');
151 $hascourseassocs = false;
152\r
153 // First find and show the associated course\r
154 $coursesstr = '';
155 $coursesarray = array();
156 foreach ($entry->renderable->blogassociations as $assocrec) {
157 $context = get_context_instance_by_id($assocrec->contextid);
158 if ($context->contextlevel == CONTEXT_COURSE) {
159 $coursesarray[] = $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
160 }
161 }
162 if (!empty($coursesarray)) {
163 $coursesstr .= get_string('associated', 'blog', $assocrec->type) . ': ' . implode(', ', $coursesarray);
164 }
165
166 // Now show mod association
167 $modulesstr = '';\r
168 $modulesarray = array();
169 foreach ($entry->renderable->blogassociations as $assocrec) {
170 $context = get_context_instance_by_id($assocrec->contextid);
171
172 if ($context->contextlevel == CONTEXT_MODULE) {
173 $str = get_string('associated', 'blog', $assocrec->type) . ': ';
174 $str .= $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
175 $modulesarray[] = $str;
176 }
177 }
178 if (!empty($modulesarray)) {
179 if (!empty($coursesarray)) {
180 $modulesstr .= '<br/>';
181 }
182 $modulesstr .= implode('<br/>', $modulesarray);
183 }
184
185 $contentcell->text .= $coursesstr.$modulesstr;
186 $contentcell->text .= $this->output->container_end();
187 }
188
189 if ($entry->renderable->unassociatedentry) {
190 $contentcell->text .= $this->output->container(get_string('associationunviewable', 'blog'), 'noticebox');
191 }
192
193 /// Commands
194 $contentcell->text .= $this->output->container_start('commands');
195 if ($entry->renderable->usercanedit) {
196 if (empty($entry->uniquehash)) {
197 //External blog entries should not be edited
198 $contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php',
199 array('action' => 'edit', 'entryid' => $entry->id)),
200 $stredit) . ' | ';
201 }
202 $contentcell->text .= html_writer::link(new moodle_url('/blog/edit.php',
203 array('action' => 'delete', 'entryid' => $entry->id)),
204 $strdelete) . ' | ';
205 }
206
207 $entryurl = new moodle_url('/blog/index.php', array('entryid' => $entry->id));
208 $contentcell->text .= html_writer::link($entryurl, get_string('permalink', 'blog'));
209
210 $contentcell->text .= $this->output->container_end();
211
212 if (isset($entry->renderable->lastmod) ) {
213 $contentcell->text .= '<div>';
214 $contentcell->text .= ' [ '.get_string('modified').': '.$entry->renderable->lastmod.' ]';
215 $contentcell->text .= '</div>';
216 }
217
218 //add comments under everything
219 if (!empty($entry->renderable->comment)) {
220 $contentcell->text .= $entry->renderable->comment->output(true);
221 }
222
223 $mainrow->cells[] = $contentcell;
224 $table->data = array($mainrow);
225
226 return html_writer::table($table);
227 }
228
229 /**
230 * Renders an entry attachment
231 *
232 * if return=html, then return a html string.
233 * if return=text, then return a text-only string.
234 * otherwise, print HTML for non-images, and return image HTML
235 *
236 * @param blog_entry_attachment $attachment
237 * @param bool $return Whether to return or print the generated code
238 * @return string List of attachments depending on the $return input
239 */
240 public function render_blog_entry_attachment(blog_entry_attachment $attachment, $return = false) {
241
242 $syscontext = get_context_instance(CONTEXT_SYSTEM);
243 $strattachment = get_string("attachment", "forum");
244
245 $image = $this->output->pix_icon(file_file_icon($attachment->file), $attachment->filename, 'moodle', array('class'=>'icon'));
246
247 $o = '';
248 $imagereturn = '';
249
250 if ($return == "html") {
251 $o .= html_writer::link($attachment->url, $image);
252 $o .= html_writer::link($attachment->url, $attachment->filename);
253
254 } else if ($return == "text") {
255 $o .= "$strattachment $attachment->filename:\n$attachment->url\n";
256
257 } else {
258 if (file_mimetype_in_typegroup($attachment->file->get_mimetype(), 'web_image')) { // Image attachments don't get printed as links
259 $imagereturn .= '<br /><img src="'.$attachment->url.'" alt="" />';
260 } else {
261 $imagereturn .= html_writer::link($attachment->url, $image);
262 $imagereturn .= format_text(html_writer::link($attachment->url, $attachment->filename), FORMAT_HTML, array('context'=>$syscontext));
263 }
264 }
265\r
266 if ($return) {\r
267 return $o;\r
268 }\r
269\r
270 return $imagereturn;
271 }
272}