MDL-65308 core: change a couple of unsafe preg_quote() cases
[moodle.git] / blog / renderer.php
blob640686571c245e000032175ba52937fb7879a63f
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.
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/>.
17 /**
18 * Renderers for outputting blog data
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
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Blog renderer
31 class core_blog_renderer extends plugin_renderer_base {
33 /**
34 * Renders a blog entry
36 * @param blog_entry $entry
37 * @return string The table HTML
39 public function render_blog_entry(blog_entry $entry) {
41 global $CFG;
43 $syscontext = context_system::instance();
45 $stredit = get_string('edit');
46 $strdelete = get_string('delete');
48 // Header.
49 $mainclass = 'forumpost blog_entry blog clearfix ';
50 if ($entry->renderable->unassociatedentry) {
51 $mainclass .= 'draft';
52 } else {
53 $mainclass .= $entry->publishstate;
55 $o = $this->output->container_start($mainclass, 'b' . $entry->id);
56 $o .= $this->output->container_start('row header clearfix');
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();
63 $o .= $this->output->container_start('topic starter header clearfix');
65 // Title.
66 $titlelink = html_writer::link(new moodle_url('/blog/index.php',
67 array('entryid' => $entry->id)),
68 format_string($entry->subject));
69 $o .= $this->output->container($titlelink, 'subject');
71 // Post by.
72 $by = new stdClass();
73 $fullname = fullname($entry->renderable->user, has_capability('moodle/site:viewfullnames', $syscontext));
74 $userurlparams = array('id' => $entry->renderable->user->id, 'course' => $this->page->course->id);
75 $by->name = html_writer::link(new moodle_url('/user/view.php', $userurlparams), $fullname);
77 $by->date = userdate($entry->created);
78 $o .= $this->output->container(get_string('bynameondate', 'forum', $by), 'author');
80 // Adding external blog link.
81 if (!empty($entry->renderable->externalblogtext)) {
82 $o .= $this->output->container($entry->renderable->externalblogtext, 'externalblog');
85 // Closing subject tag and header tag.
86 $o .= $this->output->container_end();
87 $o .= $this->output->container_end();
89 // Post content.
90 $o .= $this->output->container_start('row maincontent clearfix');
92 // Entry.
93 $o .= $this->output->container_start('no-overflow content ');
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;
111 $o .= $this->output->container($blogtype, 'audience');
113 // Attachments.
114 $attachmentsoutputs = array();
115 if ($entry->renderable->attachments) {
116 foreach ($entry->renderable->attachments as $attachment) {
117 $o .= $this->render($attachment, false);
121 // Body.
122 $o .= format_text($entry->summary, $entry->summaryformat, array('overflowdiv' => true));
124 if (!empty($entry->uniquehash)) {
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();
134 // Links to tags.
135 $o .= $this->output->tag_list(core_tag_tag::get_item_tags('core', 'post', $entry->id));
137 // Add associations.
138 if (!empty($CFG->useblogassociations) && !empty($entry->renderable->blogassociations)) {
140 // First find and show the associated course.
141 $assocstr = '';
142 $coursesarray = array();
143 foreach ($entry->renderable->blogassociations as $assocrec) {
144 if ($assocrec->contextlevel == CONTEXT_COURSE) {
145 $coursesarray[] = $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
148 if (!empty($coursesarray)) {
149 $assocstr .= get_string('associated', 'blog', get_string('course')) . ': ' . implode(', ', $coursesarray);
152 // Now show mod association.
153 $modulesarray = array();
154 foreach ($entry->renderable->blogassociations as $assocrec) {
155 if ($assocrec->contextlevel == CONTEXT_MODULE) {
156 $str = get_string('associated', 'blog', $assocrec->type) . ': ';
157 $str .= $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
158 $modulesarray[] = $str;
161 if (!empty($modulesarray)) {
162 if (!empty($coursesarray)) {
163 $assocstr .= '<br/>';
165 $assocstr .= implode('<br/>', $modulesarray);
168 // Adding the asociations to the output.
169 $o .= $this->output->container($assocstr, 'tags');
172 if ($entry->renderable->unassociatedentry) {
173 $o .= $this->output->container(get_string('associationunviewable', 'blog'), 'noticebox');
176 // Commands.
177 $o .= $this->output->container_start('commands');
178 if ($entry->renderable->usercanedit) {
180 // External blog entries should not be edited.
181 if (empty($entry->uniquehash)) {
182 $o .= html_writer::link(new moodle_url('/blog/edit.php',
183 array('action' => 'edit', 'entryid' => $entry->id)),
184 $stredit) . ' | ';
186 $o .= html_writer::link(new moodle_url('/blog/edit.php',
187 array('action' => 'delete', 'entryid' => $entry->id)),
188 $strdelete) . ' | ';
191 $entryurl = new moodle_url('/blog/index.php', array('entryid' => $entry->id));
192 $o .= html_writer::link($entryurl, get_string('permalink', 'blog'));
194 $o .= $this->output->container_end();
196 // Last modification.
197 if ($entry->created != $entry->lastmodified) {
198 $o .= $this->output->container(' [ '.get_string('modified').': '.userdate($entry->lastmodified).' ]');
201 // Comments.
202 if (!empty($entry->renderable->comment)) {
203 $o .= $entry->renderable->comment->output(true);
206 $o .= $this->output->container_end();
208 // Closing maincontent div.
209 $o .= $this->output->container('&nbsp;', 'side options');
210 $o .= $this->output->container_end();
212 $o .= $this->output->container_end();
214 return $o;
218 * Renders an entry attachment
220 * Print link for non-images and returns images as HTML
222 * @param blog_entry_attachment $attachment
223 * @return string List of attachments depending on the $return input
225 public function render_blog_entry_attachment(blog_entry_attachment $attachment) {
227 $syscontext = context_system::instance();
229 // Image attachments don't get printed as links.
230 if (file_mimetype_in_typegroup($attachment->file->get_mimetype(), 'web_image')) {
231 $attrs = array('src' => $attachment->url, 'alt' => '');
232 $o = html_writer::empty_tag('img', $attrs);
233 $class = 'attachedimages';
234 } else {
235 $image = $this->output->pix_icon(file_file_icon($attachment->file),
236 $attachment->filename,
237 'moodle',
238 array('class' => 'icon'));
239 $o = html_writer::link($attachment->url, $image);
240 $o .= format_text(html_writer::link($attachment->url, $attachment->filename),
241 FORMAT_HTML,
242 array('context' => $syscontext));
243 $class = 'attachments';
246 return $this->output->container($o, $class);