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