MDL-46346 blog: coding style fixes
[moodle.git] / blog / renderer.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 /**
29  * Blog renderer
30  */
31 class core_blog_renderer extends plugin_renderer_base {
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) {
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;
54         }
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');
83         }
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;
110         }
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);
118             }
119         }
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();
131             }
132         }
134         // Links to tags.
135         $officialtags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'official');
136         $defaulttags = tag_get_tags_csv('post', $entry->id, TAG_RETURN_HTML, 'default');
138         if (!empty($CFG->usetags) && ($officialtags || $defaulttags) ) {
139             $o .= $this->output->container_start('tags');
141             if ($officialtags) {
142                 $o .= get_string('tags', 'tag') .': '. $this->output->container($officialtags, 'officialblogtags');
143                 if ($defaulttags) {
144                     $o .=  ', ';
145                 }
146             }
147             $o .=  $defaulttags;
148             $o .= $this->output->container_end();
149         }
151         // Add associations.
152         if (!empty($CFG->useblogassociations) && !empty($entry->renderable->blogassociations)) {
154             // First find and show the associated course.
155             $assocstr = '';
156             $coursesarray = array();
157             foreach ($entry->renderable->blogassociations as $assocrec) {
158                 if ($assocrec->contextlevel ==  CONTEXT_COURSE) {
159                     $coursesarray[] = $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
160                 }
161             }
162             if (!empty($coursesarray)) {
163                 $assocstr .= get_string('associated', 'blog', get_string('course')) . ': ' . implode(', ', $coursesarray);
164             }
166             // Now show mod association.
167             $modulesarray = array();
168             foreach ($entry->renderable->blogassociations as $assocrec) {
169                 if ($assocrec->contextlevel ==  CONTEXT_MODULE) {
170                     $str = get_string('associated', 'blog', $assocrec->type) . ': ';
171                     $str .= $this->output->action_icon($assocrec->url, $assocrec->icon, null, array(), true);
172                     $modulesarray[] = $str;
173                 }
174             }
175             if (!empty($modulesarray)) {
176                 if (!empty($coursesarray)) {
177                     $assocstr .= '<br/>';
178                 }
179                 $assocstr .= implode('<br/>', $modulesarray);
180             }
182             // Adding the asociations to the output.
183             $o .= $this->output->container($assocstr, 'tags');
184         }
186         if ($entry->renderable->unassociatedentry) {
187             $o .= $this->output->container(get_string('associationunviewable', 'blog'), 'noticebox');
188         }
190         // Commands.
191         $o .= $this->output->container_start('commands');
192         if ($entry->renderable->usercanedit) {
194             // External blog entries should not be edited.
195             if (empty($entry->uniquehash)) {
196                 $o .= html_writer::link(new moodle_url('/blog/edit.php',
197                                                         array('action' => 'edit', 'entryid' => $entry->id)),
198                                                         $stredit) . ' | ';
199             }
200             $o .= html_writer::link(new moodle_url('/blog/edit.php',
201                                                     array('action' => 'delete', 'entryid' => $entry->id)),
202                                                     $strdelete) . ' | ';
203         }
205         $entryurl = new moodle_url('/blog/index.php', array('entryid' => $entry->id));
206         $o .= html_writer::link($entryurl, get_string('permalink', 'blog'));
208         $o .= $this->output->container_end();
210         // Last modification.
211         if ($entry->created != $entry->lastmodified) {
212             $o .= $this->output->container(' [ '.get_string('modified').': '.userdate($entry->lastmodified).' ]');
213         }
215         // Comments.
216         if (!empty($entry->renderable->comment)) {
217             $o .= $entry->renderable->comment->output(true);
218         }
220         $o .= $this->output->container_end();
222         // Closing maincontent div.
223         $o .= $this->output->container('&nbsp;', 'side options');
224         $o .= $this->output->container_end();
226         $o .= $this->output->container_end();
228         return $o;
229     }
231     /**
232      * Renders an entry attachment
233      *
234      * Print link for non-images and returns images as HTML
235      *
236      * @param blog_entry_attachment $attachment
237      * @return string List of attachments depending on the $return input
238      */
239     public function render_blog_entry_attachment(blog_entry_attachment $attachment) {
241         $syscontext = context_system::instance();
243         // Image attachments don't get printed as links.
244         if (file_mimetype_in_typegroup($attachment->file->get_mimetype(), 'web_image')) {
245             $attrs = array('src' => $attachment->url, 'alt' => '');
246             $o = html_writer::empty_tag('img', $attrs);
247             $class = 'attachedimages';
248         } else {
249             $image = $this->output->pix_icon(file_file_icon($attachment->file),
250                                              $attachment->filename,
251                                              'moodle',
252                                              array('class' => 'icon'));
253             $o = html_writer::link($attachment->url, $image);
254             $o .= format_text(html_writer::link($attachment->url, $attachment->filename),
255                               FORMAT_HTML,
256                               array('context' => $syscontext));
257             $class = 'attachments';
258         }
260         return $this->output->container($o, $class);
261     }