MDL-44316 core_tag: changed the API to accept a contextid and component
[moodle.git] / blog / renderer.php
blobaf1724de1ab57bf384b7449dd3084e94816eca99
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', array('entryid' => $entry->id)), format_string($entry->subject));
67 $o .= $this->output->container($titlelink, 'subject');
69 // Post by.
70 $by = new stdClass();
71 $fullname = fullname($entry->renderable->user, has_capability('moodle/site:viewfullnames', $syscontext));
72 $userurlparams = array('id' => $entry->renderable->user->id, 'course' => $this->page->course->id);
73 $by->name = html_writer::link(new moodle_url('/user/view.php', $userurlparams), $fullname);
75 $by->date = userdate($entry->created);
76 $o .= $this->output->container(get_string('bynameondate', 'forum', $by), 'author');
78 // Adding external blog link.
79 if (!empty($entry->renderable->externalblogtext)) {
80 $o .= $this->output->container($entry->renderable->externalblogtext, 'externalblog');
83 // Closing subject tag and header tag.
84 $o .= $this->output->container_end();
85 $o .= $this->output->container_end();
87 // Post content.
88 $o .= $this->output->container_start('row maincontent clearfix');
90 // Entry.
91 $o .= $this->output->container_start('no-overflow content ');
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;
109 $o .= $this->output->container($blogtype, 'audience');
111 // Attachments.
112 $attachmentsoutputs = array();
113 if ($entry->renderable->attachments) {
114 foreach ($entry->renderable->attachments as $attachment) {
115 $o .= $this->render($attachment, false);
119 // Body.
120 $o .= format_text($entry->summary, $entry->summaryformat, array('overflowdiv' => true));
122 if (!empty($entry->uniquehash)) {
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();
132 // Links to tags.
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');
136 if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) {
137 $o .= $this->output->container_start('tags');
139 if ($officialtags) {
140 $o .= get_string('tags', 'tag') .': '. $this->output->container($officialtags, 'officialblogtags');
141 if ($defaulttags) {
142 $o .= ', ';
145 $o .= $defaulttags;
146 $o .= $this->output->container_end();
149 // Add associations.
150 if (!empty($CFG->useblogassociations) && !empty($entry->renderable->blogassociations)) {
152 // First find and show the associated course.
153 $assocstr = '';
154 $coursesarray = array();
155 foreach ($entry->renderable->blogassociations as $assocrec) {
156 if ($assocrec->contextlevel == CONTEXT_COURSE) {
157 $coursesarray[] = $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
160 if (!empty($coursesarray)) {
161 $assocstr .= get_string('associated', 'blog', get_string('course')) . ': ' . implode(', ', $coursesarray);
164 // Now show mod association.
165 $modulesarray = array();
166 foreach ($entry->renderable->blogassociations as $assocrec) {
167 if ($assocrec->contextlevel == CONTEXT_MODULE) {
168 $str = get_string('associated', 'blog', $assocrec->type) . ': ';
169 $str .= $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
170 $modulesarray[] = $str;
173 if (!empty($modulesarray)) {
174 if (!empty($coursesarray)) {
175 $assocstr .= '<br/>';
177 $assocstr .= implode('<br/>', $modulesarray);
180 // Adding the asociations to the output.
181 $o .= $this->output->container($assocstr, 'tags');
184 if ($entry->renderable->unassociatedentry) {
185 $o .= $this->output->container(get_string('associationunviewable', 'blog'), 'noticebox');
188 // Commands.
189 $o .= $this->output->container_start('commands');
190 if ($entry->renderable->usercanedit) {
192 // External blog entries should not be edited.
193 if (empty($entry->uniquehash)) {
194 $o .= html_writer::link(new moodle_url('/blog/edit.php',
195 array('action' => 'edit', 'entryid' => $entry->id)),
196 $stredit) . ' | ';
198 $o .= html_writer::link(new moodle_url('/blog/edit.php',
199 array('action' => 'delete', 'entryid' => $entry->id)),
200 $strdelete) . ' | ';
203 $entryurl = new moodle_url('/blog/index.php', array('entryid' => $entry->id));
204 $o .= html_writer::link($entryurl, get_string('permalink', 'blog'));
206 $o .= $this->output->container_end();
208 // Last modification.
209 if ($entry->created != $entry->lastmodified) {
210 $o .= $this->output->container(' [ '.get_string('modified').': '.userdate($entry->lastmodified).' ]');
213 // Comments.
214 if (!empty($entry->renderable->comment)) {
215 $o .= $entry->renderable->comment->output(true);
218 $o .= $this->output->container_end();
220 // Closing maincontent div.
221 $o .= $this->output->container('&nbsp;', 'side options');
222 $o .= $this->output->container_end();
224 $o .= $this->output->container_end();
226 return $o;
230 * Renders an entry attachment
232 * Print link for non-images and returns images as HTML
234 * @param blog_entry_attachment $attachment
235 * @return string List of attachments depending on the $return input
237 public function render_blog_entry_attachment(blog_entry_attachment $attachment) {
239 $syscontext = context_system::instance();
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';
246 } else {
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';
253 return $this->output->container($o, $class);