MDL-30798 Blog: link param for entries about this course link is fixed
[moodle.git] / blocks / blog_recent / block_blog_recent.php
CommitLineData
cae83708 1<?php
cae83708 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
cae83708 17/**
18 * Recent Blog Entries Block page.
19 *
fd85ceac
PS
20 * @package block
21 * @subpackage blog_recent
cae83708 22 * @copyright 2009 Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
fd85ceac 26defined('MOODLE_INTERNAL') || die();
cae83708 27
28/**
29 * This block simply outputs a list of links to recent blog entries, depending on
30 * the context of the current page.
31 */
32class block_blog_recent extends block_base {
33
34 function init() {
34ac830c 35 $this->title = get_string('pluginname', 'block_blog_recent');
cae83708 36 $this->content_type = BLOCK_TYPE_TEXT;
cae83708 37 }
38
dc01eb84
NC
39 function applicable_formats() {
40 return array('all' => true, 'my' => false, 'tag' => false);
41 }
42
dc01eb84
NC
43 function instance_allow_config() {
44 return true;
45 }
46
cae83708 47 function get_content() {
fd85ceac 48 global $CFG;
cae83708 49
fd85ceac
PS
50 if ($this->content !== NULL) {
51 return $this->content;
cae83708 52 }
53
fd85ceac
PS
54 // verify blog is enabled
55 if (empty($CFG->bloglevel)) {
56 $this->content = new stdClass();
dc01eb84
NC
57 $this->content->text = '';
58 if ($this->page->user_is_editing()) {
59 $this->content->text = get_string('blogdisable', 'blog');
60 }
61 return $this->content;
fd85ceac
PS
62
63 } else if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL and (!isloggedin() or isguestuser())) {
64 $this->content = new stdClass();
65 $this->content->text = '';
66 return $this->content;
67 }
68
69 require_once($CFG->dirroot .'/blog/lib.php');
70 require_once($CFG->dirroot .'/blog/locallib.php');
71
15ea1c91
DP
72 if (empty($this->config)) {
73 $this->config = new stdClass();
74 }
75
fd85ceac
PS
76 if (empty($this->config->recentbloginterval)) {
77 $this->config->recentbloginterval = 8400;
78 }
79
80 if (empty($this->config->numberofrecentblogentries)) {
81 $this->config->numberofrecentblogentries = 4;
cae83708 82 }
83
dc01eb84
NC
84 $this->content = new stdClass();
85 $this->content->footer = '';
cae83708 86
8eaf1ba1
SH
87 $context = $this->page->context;
88
021bea12 89 $url = new moodle_url('/blog/index.php');
8eaf1ba1
SH
90 $filter = array();
91 if ($context->contextlevel == CONTEXT_MODULE) {
92 $filter['module'] = $context->instanceid;
93 $a = new stdClass;
d86b7d5a 94 $a->type = get_string('modulename', $this->page->cm->modname);
8eaf1ba1 95 $strview = get_string('viewallmodentries', 'blog', $a);
021bea12 96 $url->param('modid', $context->instanceid);
8eaf1ba1
SH
97 } else if ($context->contextlevel == CONTEXT_COURSE) {
98 $filter['course'] = $context->instanceid;
99 $a = new stdClass;
100 $a->type = get_string('course');
101 $strview = get_string('viewblogentries', 'blog', $a);
021bea12 102 $url->param('courseid', $context->instanceid);
8eaf1ba1
SH
103 } else {
104 $strview = get_string('viewsiteentries', 'blog');
cae83708 105 }
8eaf1ba1 106 $filter['since'] = $this->config->recentbloginterval;
cae83708 107
8eaf1ba1 108 $bloglisting = new blog_listing($filter);
dc01eb84 109 $entries = $bloglisting->get_entries(0, $this->config->numberofrecentblogentries, 4);
cae83708 110
dc01eb84 111 if (!empty($entries)) {
5be262b6 112 $entrieslist = array();
a6855934 113 $viewblogurl = new moodle_url('/blog/index.php');
cae83708 114
dc01eb84
NC
115 foreach ($entries as $entryid => $entry) {
116 $viewblogurl->param('entryid', $entryid);
0f4c64b7 117 $entrylink = html_writer::link($viewblogurl, shorten_text($entry->subject));
5be262b6 118 $entrieslist[] = $entrylink;
dc01eb84 119 }
cae83708 120
5be262b6 121 $this->content->text .= html_writer::alist($entrieslist, array('class'=>'list'));
8eaf1ba1 122 $viewallentrieslink = html_writer::link($url, $strview);
0f4c64b7 123 $this->content->text .= $viewallentrieslink;
dc01eb84
NC
124 } else {
125 $this->content->text .= get_string('norecentblogentries', 'block_blog_recent');
126 }
cae83708 127 }
128}