MDL-46346 blog: coding style fixes
[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 */
f8133217 25
2591c7ae
DM
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
f8133217
DM
43 $syscontext = context_system::instance();
44
2591c7ae
DM
45 $stredit = get_string('edit');
46 $strdelete = get_string('delete');
47
f8133217
DM
48 // Header.
49 $mainclass = 'forumpost blog_entry blog clearfix ';
50 if ($entry->renderable->unassociatedentry) {
51 $mainclass .= 'draft';
52 } else {
53 $mainclass .= $entry->publishstate;
54 }
55 $o = $this->output->container_start($mainclass, 'b' . $entry->id);
56 $o .= $this->output->container_start('row header clearfix');
57
58 // User picture.
59 $o .= $this->output->container_start('left picture header');
60 $o .= $this->output->user_picture($entry->renderable->user);
61 $o .= $this->output->container_end();
62
63 $o .= $this->output->container_start('topic starter header clearfix');
64
65 // Title.
2b6e53e8
AD
66 $titlelink = html_writer::link(new moodle_url('/blog/index.php',
67 array('entryid' => $entry->id)),
68 format_string($entry->subject));
f8133217
DM
69 $o .= $this->output->container($titlelink, 'subject');
70
71 // Post by.
2591c7ae 72 $by = new stdClass();
f8133217 73 $fullname = fullname($entry->renderable->user, has_capability('moodle/site:viewfullnames', $syscontext));
2591c7ae 74 $userurlparams = array('id' => $entry->renderable->user->id, 'course' => $this->page->course->id);
f8133217 75 $by->name = html_writer::link(new moodle_url('/user/view.php', $userurlparams), $fullname);
2591c7ae 76
f8133217
DM
77 $by->date = userdate($entry->created);
78 $o .= $this->output->container(get_string('bynameondate', 'forum', $by), 'author');
2591c7ae 79
f8133217 80 // Adding external blog link.
2591c7ae 81 if (!empty($entry->renderable->externalblogtext)) {
f8133217
DM
82 $o .= $this->output->container($entry->renderable->externalblogtext, 'externalblog');
83 }
84
85 // Closing subject tag and header tag.
86 $o .= $this->output->container_end();
87 $o .= $this->output->container_end();
88
89 // Post content.
90 $o .= $this->output->container_start('row maincontent clearfix');
91
92 // Entry.
93 $o .= $this->output->container_start('no-overflow content ');
94
95 // Determine text for publish state.
96 switch ($entry->publishstate) {
97 case 'draft':
98 $blogtype = get_string('publishtonoone', 'blog');
99 break;
100 case 'site':
101 $blogtype = get_string('publishtosite', 'blog');
102 break;
103 case 'public':
104 $blogtype = get_string('publishtoworld', 'blog');
105 break;
106 default:
107 $blogtype = '';
108 break;
109
2591c7ae 110 }
f8133217 111 $o .= $this->output->container($blogtype, 'audience');
2591c7ae 112
f8133217 113 // Attachments.
2591c7ae
DM
114 $attachmentsoutputs = array();
115 if ($entry->renderable->attachments) {
116 foreach ($entry->renderable->attachments as $attachment) {
f8133217 117 $o .= $this->render($attachment, false);
2591c7ae 118 }
2591c7ae
DM
119 }
120
f8133217
DM
121 // Body.
122 $o .= format_text($entry->summary, $entry->summaryformat, array('overflowdiv' => true));
123
2591c7ae 124 if (!empty($entry->uniquehash)) {
4925b9e8
DP
125 // Uniquehash is used as a link to an external blog.
126 $url = clean_param($entry->uniquehash, PARAM_URL);
127 if (!empty($url)) {
128 $o .= $this->output->container_start('externalblog');
129 $o .= html_writer::link($url, get_string('linktooriginalentry', 'blog'));
130 $o .= $this->output->container_end();
131 }
2591c7ae
DM
132 }
133
f8133217 134 // Links to tags.
2591c7ae
DM
135 $officialtags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'official');
136 $defaulttags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'default');
137
138 if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) {
f8133217 139 $o .= $this->output->container_start('tags');
2591c7ae
DM
140
141 if ($officialtags) {
f8133217 142 $o .= get_string('tags', 'tag') .': '. $this->output->container($officialtags, 'officialblogtags');
2591c7ae 143 if ($defaulttags) {
f8133217 144 $o .= ', ';
2591c7ae
DM
145 }
146 }
f8133217
DM
147 $o .= $defaulttags;
148 $o .= $this->output->container_end();
2591c7ae
DM
149 }
150
f8133217 151 // Add associations.
2591c7ae 152 if (!empty($CFG->useblogassociations) && !empty($entry->renderable->blogassociations)) {
f8133217
DM
153
154 // First find and show the associated course.
155 $assocstr = '';
2591c7ae
DM
156 $coursesarray = array();
157 foreach ($entry->renderable->blogassociations as $assocrec) {
f8133217 158 if ($assocrec->contextlevel == CONTEXT_COURSE) {
2591c7ae
DM
159 $coursesarray[] = $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
160 }
161 }
162 if (!empty($coursesarray)) {
f8133217 163 $assocstr .= get_string('associated', 'blog', get_string('course')) . ': ' . implode(', ', $coursesarray);
2591c7ae
DM
164 }
165
f8133217 166 // Now show mod association.
2591c7ae
DM
167 $modulesarray = array();
168 foreach ($entry->renderable->blogassociations as $assocrec) {
f8133217 169 if ($assocrec->contextlevel == CONTEXT_MODULE) {
2591c7ae
DM
170 $str = get_string('associated', 'blog', $assocrec->type) . ': ';
171 $str .= $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
172 $modulesarray[] = $str;
173 }
174 }
175 if (!empty($modulesarray)) {
176 if (!empty($coursesarray)) {
f8133217 177 $assocstr .= '<br/>';
2591c7ae 178 }
f8133217 179 $assocstr .= implode('<br/>', $modulesarray);
2591c7ae
DM
180 }
181
f8133217
DM
182 // Adding the asociations to the output.
183 $o .= $this->output->container($assocstr, 'tags');
2591c7ae
DM
184 }
185
186 if ($entry->renderable->unassociatedentry) {
f8133217 187 $o .= $this->output->container(get_string('associationunviewable', 'blog'), 'noticebox');
2591c7ae
DM
188 }
189
f8133217
DM
190 // Commands.
191 $o .= $this->output->container_start('commands');
2591c7ae 192 if ($entry->renderable->usercanedit) {
f8133217
DM
193
194 // External blog entries should not be edited.
2591c7ae 195 if (empty($entry->uniquehash)) {
f8133217 196 $o .= html_writer::link(new moodle_url('/blog/edit.php',
2591c7ae
DM
197 array('action' => 'edit', 'entryid' => $entry->id)),
198 $stredit) . ' | ';
199 }
f8133217 200 $o .= html_writer::link(new moodle_url('/blog/edit.php',
2591c7ae
DM
201 array('action' => 'delete', 'entryid' => $entry->id)),
202 $strdelete) . ' | ';
203 }
204
205 $entryurl = new moodle_url('/blog/index.php', array('entryid' => $entry->id));
f8133217 206 $o .= html_writer::link($entryurl, get_string('permalink', 'blog'));
2591c7ae 207
f8133217 208 $o .= $this->output->container_end();
2591c7ae 209
f8133217
DM
210 // Last modification.
211 if ($entry->created != $entry->lastmodified) {
212 $o .= $this->output->container(' [ '.get_string('modified').': '.userdate($entry->lastmodified).' ]');
2591c7ae
DM
213 }
214
f8133217 215 // Comments.
2591c7ae 216 if (!empty($entry->renderable->comment)) {
f8133217 217 $o .= $entry->renderable->comment->output(true);
2591c7ae
DM
218 }
219
f8133217
DM
220 $o .= $this->output->container_end();
221
222 // Closing maincontent div.
223 $o .= $this->output->container('&nbsp;', 'side options');
224 $o .= $this->output->container_end();
225
226 $o .= $this->output->container_end();
2591c7ae 227
f8133217 228 return $o;
2591c7ae
DM
229 }
230
231 /**
232 * Renders an entry attachment
233 *
f8133217 234 * Print link for non-images and returns images as HTML
2591c7ae
DM
235 *
236 * @param blog_entry_attachment $attachment
2591c7ae
DM
237 * @return string List of attachments depending on the $return input
238 */
f8133217 239 public function render_blog_entry_attachment(blog_entry_attachment $attachment) {
2591c7ae 240
f8133217 241 $syscontext = context_system::instance();
2591c7ae 242
f8133217
DM
243 // Image attachments don't get printed as links.
244 if (file_mimetype_in_typegroup($attachment->file->get_mimetype(), 'web_image')) {
245 $attrs = array('src' => $attachment->url, 'alt' => '');
246 $o = html_writer::empty_tag('img', $attrs);
247 $class = 'attachedimages';
2591c7ae 248 } else {
2b6e53e8
AD
249 $image = $this->output->pix_icon(file_file_icon($attachment->file),
250 $attachment->filename,
251 'moodle',
252 array('class' => 'icon'));
f8133217 253 $o = html_writer::link($attachment->url, $image);
2b6e53e8
AD
254 $o .= format_text(html_writer::link($attachment->url, $attachment->filename),
255 FORMAT_HTML,
256 array('context' => $syscontext));
f8133217 257 $class = 'attachments';
2591c7ae 258 }
f8133217
DM
259
260 return $this->output->container($o, $class);
2591c7ae
DM
261 }
262}