MDL-41623 ensure all rss links are valid urls.
[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
122 // Uniquehash is used as a link to an external blog.
2591c7ae 123 if (!empty($entry->uniquehash)) {
f8133217
DM
124 $o .= $this->output->container_start('externalblog');
125 $o .= html_writer::link($entry->uniquehash, get_string('linktooriginalentry', 'blog'));
126 $o .= $this->output->container_end();
2591c7ae
DM
127 }
128
f8133217 129 // Links to tags.
2591c7ae
DM
130 $officialtags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'official');
131 $defaulttags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'default');
132
133 if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) {
f8133217 134 $o .= $this->output->container_start('tags');
2591c7ae
DM
135
136 if ($officialtags) {
f8133217 137 $o .= get_string('tags', 'tag') .': '. $this->output->container($officialtags, 'officialblogtags');
2591c7ae 138 if ($defaulttags) {
f8133217 139 $o .= ', ';
2591c7ae
DM
140 }
141 }
f8133217
DM
142 $o .= $defaulttags;
143 $o .= $this->output->container_end();
2591c7ae
DM
144 }
145
f8133217 146 // Add associations.
2591c7ae 147 if (!empty($CFG->useblogassociations) && !empty($entry->renderable->blogassociations)) {
f8133217
DM
148
149 // First find and show the associated course.
150 $assocstr = '';
2591c7ae
DM
151 $coursesarray = array();
152 foreach ($entry->renderable->blogassociations as $assocrec) {
f8133217 153 if ($assocrec->contextlevel == CONTEXT_COURSE) {
2591c7ae
DM
154 $coursesarray[] = $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
155 }
156 }
157 if (!empty($coursesarray)) {
f8133217 158 $assocstr .= get_string('associated', 'blog', get_string('course')) . ': ' . implode(', ', $coursesarray);
2591c7ae
DM
159 }
160
f8133217 161 // Now show mod association.
2591c7ae
DM
162 $modulesarray = array();
163 foreach ($entry->renderable->blogassociations as $assocrec) {
f8133217 164 if ($assocrec->contextlevel == CONTEXT_MODULE) {
2591c7ae
DM
165 $str = get_string('associated', 'blog', $assocrec->type) . ': ';
166 $str .= $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
167 $modulesarray[] = $str;
168 }
169 }
170 if (!empty($modulesarray)) {
171 if (!empty($coursesarray)) {
f8133217 172 $assocstr .= '<br/>';
2591c7ae 173 }
f8133217 174 $assocstr .= implode('<br/>', $modulesarray);
2591c7ae
DM
175 }
176
f8133217
DM
177 // Adding the asociations to the output.
178 $o .= $this->output->container($assocstr, 'tags');
2591c7ae
DM
179 }
180
181 if ($entry->renderable->unassociatedentry) {
f8133217 182 $o .= $this->output->container(get_string('associationunviewable', 'blog'), 'noticebox');
2591c7ae
DM
183 }
184
f8133217
DM
185 // Commands.
186 $o .= $this->output->container_start('commands');
2591c7ae 187 if ($entry->renderable->usercanedit) {
f8133217
DM
188
189 // External blog entries should not be edited.
2591c7ae 190 if (empty($entry->uniquehash)) {
f8133217 191 $o .= html_writer::link(new moodle_url('/blog/edit.php',
2591c7ae
DM
192 array('action' => 'edit', 'entryid' => $entry->id)),
193 $stredit) . ' | ';
194 }
f8133217 195 $o .= html_writer::link(new moodle_url('/blog/edit.php',
2591c7ae
DM
196 array('action' => 'delete', 'entryid' => $entry->id)),
197 $strdelete) . ' | ';
198 }
199
200 $entryurl = new moodle_url('/blog/index.php', array('entryid' => $entry->id));
f8133217 201 $o .= html_writer::link($entryurl, get_string('permalink', 'blog'));
2591c7ae 202
f8133217 203 $o .= $this->output->container_end();
2591c7ae 204
f8133217
DM
205 // Last modification.
206 if ($entry->created != $entry->lastmodified) {
207 $o .= $this->output->container(' [ '.get_string('modified').': '.userdate($entry->lastmodified).' ]');
2591c7ae
DM
208 }
209
f8133217 210 // Comments.
2591c7ae 211 if (!empty($entry->renderable->comment)) {
f8133217 212 $o .= $entry->renderable->comment->output(true);
2591c7ae
DM
213 }
214
f8133217
DM
215 $o .= $this->output->container_end();
216
217 // Closing maincontent div.
218 $o .= $this->output->container('&nbsp;', 'side options');
219 $o .= $this->output->container_end();
220
221 $o .= $this->output->container_end();
2591c7ae 222
f8133217 223 return $o;
2591c7ae
DM
224 }
225
226 /**
227 * Renders an entry attachment
228 *
f8133217 229 * Print link for non-images and returns images as HTML
2591c7ae
DM
230 *
231 * @param blog_entry_attachment $attachment
2591c7ae
DM
232 * @return string List of attachments depending on the $return input
233 */
f8133217 234 public function render_blog_entry_attachment(blog_entry_attachment $attachment) {
2591c7ae 235
f8133217 236 $syscontext = context_system::instance();
2591c7ae 237
f8133217
DM
238 // Image attachments don't get printed as links.
239 if (file_mimetype_in_typegroup($attachment->file->get_mimetype(), 'web_image')) {
240 $attrs = array('src' => $attachment->url, 'alt' => '');
241 $o = html_writer::empty_tag('img', $attrs);
242 $class = 'attachedimages';
2591c7ae 243 } else {
f8133217
DM
244 $image = $this->output->pix_icon(file_file_icon($attachment->file), $attachment->filename, 'moodle', array('class'=>'icon'));
245 $o = html_writer::link($attachment->url, $image);
246 $o .= format_text(html_writer::link($attachment->url, $attachment->filename), FORMAT_HTML, array('context' => $syscontext));
247 $class = 'attachments';
2591c7ae 248 }
f8133217
DM
249
250 return $this->output->container($o, $class);
2591c7ae
DM
251 }
252}