block-blog_recent MDL-22942 Fixed up block config function, blog_recent doesn't have...
[moodle.git] / blocks / blog_recent / block_blog_recent.php
CommitLineData
cae83708 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Recent Blog Entries Block page.
21 *
22 * @package moodlecore
23 * @subpackage blog
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28require_once($CFG->dirroot .'/blog/lib.php');
29require_once($CFG->dirroot .'/blog/locallib.php');
30
31/**
32 * This block simply outputs a list of links to recent blog entries, depending on
33 * the context of the current page.
34 */
35class block_blog_recent extends block_base {
36
37 function init() {
34ac830c 38 $this->title = get_string('pluginname', 'block_blog_recent');
cae83708 39 $this->content_type = BLOCK_TYPE_TEXT;
cae83708 40 }
41
dc01eb84
NC
42 function applicable_formats() {
43 return array('all' => true, 'my' => false, 'tag' => false);
44 }
45
dc01eb84
NC
46 function instance_allow_config() {
47 return true;
48 }
49
cae83708 50 function get_content() {
dc01eb84 51 global $CFG, $USER, $PAGE, $DB, $OUTPUT;
cae83708 52
dc01eb84
NC
53 if (empty($this->config->recentbloginterval)) {
54 $this->config->recentbloginterval = 8400;
55 }
cae83708 56
dc01eb84
NC
57 if (empty($this->config->numberofrecentblogentries)) {
58 $this->config->numberofrecentblogentries = 4;
cae83708 59 }
60
dc01eb84
NC
61 if (empty($CFG->bloglevel) || ($CFG->bloglevel < BLOG_GLOBAL_LEVEL && !(isloggedin() && !isguestuser()))) {
62 $this->content->text = '';
63 if ($this->page->user_is_editing()) {
64 $this->content->text = get_string('blogdisable', 'blog');
65 }
66 return $this->content;
cae83708 67 }
68
dc01eb84
NC
69 $this->content = new stdClass();
70 $this->content->footer = '';
cae83708 71
8eaf1ba1
SH
72 $context = $this->page->context;
73
74 $filter = array();
75 if ($context->contextlevel == CONTEXT_MODULE) {
76 $filter['module'] = $context->instanceid;
77 $a = new stdClass;
d86b7d5a 78 $a->type = get_string('modulename', $this->page->cm->modname);
8eaf1ba1
SH
79 $strview = get_string('viewallmodentries', 'blog', $a);
80 } else if ($context->contextlevel == CONTEXT_COURSE) {
81 $filter['course'] = $context->instanceid;
82 $a = new stdClass;
83 $a->type = get_string('course');
84 $strview = get_string('viewblogentries', 'blog', $a);
85 } else {
86 $strview = get_string('viewsiteentries', 'blog');
cae83708 87 }
8eaf1ba1 88 $filter['since'] = $this->config->recentbloginterval;
cae83708 89
8eaf1ba1 90 $bloglisting = new blog_listing($filter);
dc01eb84 91 $entries = $bloglisting->get_entries(0, $this->config->numberofrecentblogentries, 4);
8eaf1ba1 92 $url = new moodle_url('/blog/index.php', $filter);
cae83708 93
dc01eb84 94 if (!empty($entries)) {
5be262b6 95 $entrieslist = array();
a6855934 96 $viewblogurl = new moodle_url('/blog/index.php');
cae83708 97
dc01eb84
NC
98 foreach ($entries as $entryid => $entry) {
99 $viewblogurl->param('entryid', $entryid);
0f4c64b7 100 $entrylink = html_writer::link($viewblogurl, shorten_text($entry->subject));
5be262b6 101 $entrieslist[] = $entrylink;
dc01eb84 102 }
cae83708 103
5be262b6 104 $this->content->text .= html_writer::alist($entrieslist, array('class'=>'list'));
8eaf1ba1 105 $viewallentrieslink = html_writer::link($url, $strview);
0f4c64b7 106 $this->content->text .= $viewallentrieslink;
dc01eb84
NC
107 } else {
108 $this->content->text .= get_string('norecentblogentries', 'block_blog_recent');
109 }
cae83708 110 }
111}