Merge branch 'MDL-65515-master' of git://github.com/mihailges/moodle
[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.
abea2c5d 135 $o .= $this->output->tag_list(core_tag_tag::get_item_tags('core', 'post', $entry->id));
2591c7ae 136
f8133217 137 // Add associations.
2591c7ae 138 if (!empty($CFG->useblogassociations) && !empty($entry->renderable->blogassociations)) {
f8133217
DM
139
140 // First find and show the associated course.
141 $assocstr = '';
2591c7ae
DM
142 $coursesarray = array();
143 foreach ($entry->renderable->blogassociations as $assocrec) {
0e32a565 144 if ($assocrec->contextlevel == CONTEXT_COURSE) {
2591c7ae
DM
145 $coursesarray[] = $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
146 }
147 }
148 if (!empty($coursesarray)) {
f8133217 149 $assocstr .= get_string('associated', 'blog', get_string('course')) . ': ' . implode(', ', $coursesarray);
2591c7ae
DM
150 }
151
f8133217 152 // Now show mod association.
2591c7ae
DM
153 $modulesarray = array();
154 foreach ($entry->renderable->blogassociations as $assocrec) {
0e32a565 155 if ($assocrec->contextlevel == CONTEXT_MODULE) {
2591c7ae
DM
156 $str = get_string('associated', 'blog', $assocrec->type) . ': ';
157 $str .= $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
158 $modulesarray[] = $str;
159 }
160 }
161 if (!empty($modulesarray)) {
162 if (!empty($coursesarray)) {
f8133217 163 $assocstr .= '<br/>';
2591c7ae 164 }
f8133217 165 $assocstr .= implode('<br/>', $modulesarray);
2591c7ae
DM
166 }
167
f8133217
DM
168 // Adding the asociations to the output.
169 $o .= $this->output->container($assocstr, 'tags');
2591c7ae
DM
170 }
171
172 if ($entry->renderable->unassociatedentry) {
f8133217 173 $o .= $this->output->container(get_string('associationunviewable', 'blog'), 'noticebox');
2591c7ae
DM
174 }
175
f8133217
DM
176 // Commands.
177 $o .= $this->output->container_start('commands');
2591c7ae 178 if ($entry->renderable->usercanedit) {
f8133217
DM
179
180 // External blog entries should not be edited.
2591c7ae 181 if (empty($entry->uniquehash)) {
f8133217 182 $o .= html_writer::link(new moodle_url('/blog/edit.php',
2591c7ae
DM
183 array('action' => 'edit', 'entryid' => $entry->id)),
184 $stredit) . ' | ';
185 }
f8133217 186 $o .= html_writer::link(new moodle_url('/blog/edit.php',
2591c7ae
DM
187 array('action' => 'delete', 'entryid' => $entry->id)),
188 $strdelete) . ' | ';
189 }
190
191 $entryurl = new moodle_url('/blog/index.php', array('entryid' => $entry->id));
f8133217 192 $o .= html_writer::link($entryurl, get_string('permalink', 'blog'));
2591c7ae 193
f8133217 194 $o .= $this->output->container_end();
2591c7ae 195
f8133217
DM
196 // Last modification.
197 if ($entry->created != $entry->lastmodified) {
198 $o .= $this->output->container(' [ '.get_string('modified').': '.userdate($entry->lastmodified).' ]');
2591c7ae
DM
199 }
200
f8133217 201 // Comments.
2591c7ae 202 if (!empty($entry->renderable->comment)) {
f8133217 203 $o .= $entry->renderable->comment->output(true);
2591c7ae
DM
204 }
205
f8133217
DM
206 $o .= $this->output->container_end();
207
208 // Closing maincontent div.
209 $o .= $this->output->container('&nbsp;', 'side options');
210 $o .= $this->output->container_end();
211
212 $o .= $this->output->container_end();
2591c7ae 213
f8133217 214 return $o;
2591c7ae
DM
215 }
216
217 /**
218 * Renders an entry attachment
219 *
f8133217 220 * Print link for non-images and returns images as HTML
2591c7ae
DM
221 *
222 * @param blog_entry_attachment $attachment
2591c7ae
DM
223 * @return string List of attachments depending on the $return input
224 */
f8133217 225 public function render_blog_entry_attachment(blog_entry_attachment $attachment) {
2591c7ae 226
f8133217 227 $syscontext = context_system::instance();
2591c7ae 228
f8133217
DM
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';
2591c7ae 234 } else {
2b6e53e8
AD
235 $image = $this->output->pix_icon(file_file_icon($attachment->file),
236 $attachment->filename,
237 'moodle',
238 array('class' => 'icon'));
f8133217 239 $o = html_writer::link($attachment->url, $image);
2b6e53e8
AD
240 $o .= format_text(html_writer::link($attachment->url, $attachment->filename),
241 FORMAT_HTML,
242 array('context' => $syscontext));
f8133217 243 $class = 'attachments';
2591c7ae 244 }
f8133217
DM
245
246 return $this->output->container($o, $class);
2591c7ae
DM
247 }
248}