forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_blog_recent.php
157 lines (132 loc) · 5.25 KB
/
block_blog_recent.php
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?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 block_blog_recent
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* 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() {
$this->title = get_string('pluginname', 'block_blog_recent');
$this->content_type = BLOCK_TYPE_TEXT;
}
function applicable_formats() {
return array('all' => true, 'my' => false, 'tag' => false);
}
function instance_allow_config() {
return true;
}
function get_content() {
global $CFG;
if ($this->content !== NULL) {
return $this->content;
}
// verify blog is enabled
if (empty($CFG->enableblogs)) {
$this->content = new stdClass();
$this->content->text = '';
if ($this->page->user_is_editing()) {
$this->content->text = get_string('blogdisable', 'blog');
}
return $this->content;
} else if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL and (!isloggedin() or isguestuser())) {
$this->content = new stdClass();
$this->content->text = '';
return $this->content;
}
require_once($CFG->dirroot .'/blog/lib.php');
require_once($CFG->dirroot .'/blog/locallib.php');
if (empty($this->config)) {
$this->config = new stdClass();
}
if (empty($this->config->recentbloginterval)) {
$this->config->recentbloginterval = 8400;
}
if (empty($this->config->numberofrecentblogentries)) {
$this->config->numberofrecentblogentries = 4;
}
$this->content = new stdClass();
$this->content->footer = '';
$this->content->text = '';
$context = $this->page->context;
$url = new moodle_url('/blog/index.php');
$filter = array();
if ($context->contextlevel == CONTEXT_MODULE) {
$filter['module'] = $context->instanceid;
$a = new stdClass;
$a->type = get_string('modulename', $this->page->cm->modname);
$strview = get_string('viewallmodentries', 'blog', $a);
$url->param('modid', $context->instanceid);
} else if ($context->contextlevel == CONTEXT_COURSE) {
$filter['course'] = $context->instanceid;
$a = new stdClass;
$a->type = get_string('course');
$strview = get_string('viewblogentries', 'blog', $a);
$url->param('courseid', $context->instanceid);
} else {
$strview = get_string('viewsiteentries', 'blog');
}
$filter['since'] = $this->config->recentbloginterval;
$bloglisting = new blog_listing($filter);
$entries = $bloglisting->get_entries(0, $this->config->numberofrecentblogentries, 4);
if (!empty($entries)) {
$entrieslist = array();
$viewblogurl = new moodle_url('/blog/index.php');
foreach ($entries as $entryid => $entry) {
$viewblogurl->param('entryid', $entryid);
$entrylink = html_writer::link($viewblogurl, shorten_text(format_string($entry->subject, true,
['context' => $context])));
$entrieslist[] = $entrylink;
}
$this->content->text .= html_writer::alist($entrieslist, array('class'=>'list'));
$viewallentrieslink = html_writer::link($url, $strview);
$this->content->text .= $viewallentrieslink;
} else {
$this->content->text .= get_string('norecentblogentries', 'block_blog_recent');
}
}
/**
* Return the plugin config settings for external functions.
*
* @return stdClass the configs for both the block instance and plugin
* @since Moodle 3.8
*/
public function get_config_for_external() {
// Return all settings for all users since it is safe (no private keys, etc..).
$configs = !empty($this->config) ? $this->config : new stdClass();
return (object) [
'instance' => $configs,
'plugin' => new stdClass(),
];
}
/**
* This block shouldn't be added to a page if the blogs advanced feature is disabled.
*
* @param moodle_page $page
* @return bool
*/
public function can_block_be_added(moodle_page $page): bool {
global $CFG;
return $CFG->enableblogs;
}
}