block_blog_recent.php 3.91 KB
Newer Older
nicolasconnault's avatar
nicolasconnault committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.


/**
 * Recent Blog Entries Block page.
 *
 * @package    moodlecore
 * @subpackage blog
 * @copyright  2009 Nicolas Connault
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once($CFG->dirroot .'/blog/lib.php');
require_once($CFG->dirroot .'/blog/locallib.php');

/**
 * This block simply outputs a list of links to recent blog entries, depending on
 * the context of the current page.
 */
class block_blog_recent extends block_base {

    function init() {
38
        $this->title = get_string('pluginname', 'block_blog_recent');
nicolasconnault's avatar
nicolasconnault committed
39
40
41
        $this->content_type = BLOCK_TYPE_TEXT;
    }

42
43
44
45
46
47
48
49
50
51
52
53
    function applicable_formats() {
        return array('all' => true, 'my' => false, 'tag' => false);
    }

    function has_config() {
        return true;
    }

    function instance_allow_config() {
        return true;
    }

nicolasconnault's avatar
nicolasconnault committed
54
    function get_content() {
55
        global $CFG, $USER, $PAGE, $DB, $OUTPUT;
nicolasconnault's avatar
nicolasconnault committed
56

57
58
59
        if (empty($this->config->recentbloginterval)) {
            $this->config->recentbloginterval = 8400;
        }
nicolasconnault's avatar
nicolasconnault committed
60

61
62
        if (empty($this->config->numberofrecentblogentries)) {
            $this->config->numberofrecentblogentries = 4;
nicolasconnault's avatar
nicolasconnault committed
63
64
        }

65
66
67
68
69
70
        if (empty($CFG->bloglevel) || ($CFG->bloglevel < BLOG_GLOBAL_LEVEL && !(isloggedin() && !isguestuser()))) {
            $this->content->text = '';
            if ($this->page->user_is_editing()) {
                $this->content->text = get_string('blogdisable', 'blog');
            }
            return $this->content;
nicolasconnault's avatar
nicolasconnault committed
71
72
        }

73
74
        $this->content = new stdClass();
        $this->content->footer = '';
nicolasconnault's avatar
nicolasconnault committed
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        $context = $this->page->context;

        $filter = array();
        if ($context->contextlevel == CONTEXT_MODULE) {
            $filter['module'] = $context->instanceid;
            $a = new stdClass;
            $a->type = get_string('modulename', $page->cm->modname);
            $strview = get_string('viewallmodentries', 'blog', $a);
        } else if ($context->contextlevel == CONTEXT_COURSE) {
            $filter['course'] = $context->instanceid;
            $a = new stdClass;
            $a->type = get_string('course');
            $strview = get_string('viewblogentries', 'blog', $a);
        } else {
            $strview = get_string('viewsiteentries', 'blog');
nicolasconnault's avatar
nicolasconnault committed
91
        }
92
        $filter['since'] = $this->config->recentbloginterval;
nicolasconnault's avatar
nicolasconnault committed
93

94
        $bloglisting = new blog_listing($filter);
95
        $entries = $bloglisting->get_entries(0, $this->config->numberofrecentblogentries, 4);
96
        $url = new moodle_url('/blog/index.php', $filter);
nicolasconnault's avatar
nicolasconnault committed
97

98
        if (!empty($entries)) {
Petr Skoda's avatar
Petr Skoda committed
99
            $entrieslist = array();
100
            $viewblogurl = new moodle_url('/blog/index.php');
nicolasconnault's avatar
nicolasconnault committed
101

102
103
            foreach ($entries as $entryid => $entry) {
                $viewblogurl->param('entryid', $entryid);
104
                $entrylink = html_writer::link($viewblogurl, shorten_text($entry->subject));
Petr Skoda's avatar
Petr Skoda committed
105
                $entrieslist[] = $entrylink;
106
            }
nicolasconnault's avatar
nicolasconnault committed
107

Petr Skoda's avatar
Petr Skoda committed
108
            $this->content->text .= html_writer::alist($entrieslist, array('class'=>'list'));
109
            $viewallentrieslink = html_writer::link($url, $strview);
110
            $this->content->text .= $viewallentrieslink;
111
112
113
        } else {
            $this->content->text .= get_string('norecentblogentries', 'block_blog_recent');
        }
nicolasconnault's avatar
nicolasconnault committed
114
115
    }
}