-
Notifications
You must be signed in to change notification settings - Fork 2
/
block_news.php
254 lines (217 loc) · 9.7 KB
/
block_news.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?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/>.
use block_news\system;
use block_news\message;
use block_news\output\short_message;
defined('MOODLE_INTERNAL') || die;
/**
* News block main class.
*
* @package block_news
* @copyright 2012 The Open University
* @author Jon Sharp <[email protected]>
* @author OU developers
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_news extends block_base {
public $bns = '';
public function init() {
$this->title = get_string('pluginname', 'block_news');
}
/**
* multiple instance control
* note: administrator can override
*/
public function instance_allow_multiple() {
return true;
}
/**
* return true if the block has settings.php file.
*/
public function has_config() {
return true;
}
/**
* Display block content
*/
public function get_content() {
global $CFG, $PAGE, $COURSE, $USER, $SESSION;
if ($this->content !== null) {
return $this->content;
}
$blockinstanceid = $this->instance->id;
$blockcontext = context_block::instance($blockinstanceid);
$this->content = new stdClass;
$output = $PAGE->get_renderer('block_news'); // Looks for class xxx_renderer.
$this->content->footer = '';
$context = context_course::instance($this->page->course->id);
$isediting = $this->page->user_is_editing()
&& has_capability('moodle/course:manageactivities', $context)
&& strpos($this->page->bodyclasses, 'format-ousubject') !== false;
// Show Add if permittted.
if (has_capability('block/news:add', $blockcontext)) {
$this->content->footer .= $output->container_start(null, 'block_news_addmsg');
$this->content->footer .= $output->render_add($blockinstanceid);
$this->content->footer .= $output->container_end();
$canaddnews = 'blocknewscanadd';// Extra class added on some links if edit permission.
} else {
$canaddnews = null;
}
$nummsgs = $this->bns->get_nummessages();
if ($this->bns->get_displaytype() == system::DISPLAY_DEFAULT) {
$msgs = $this->bns->get_messages_limited($nummsgs);
$events = null;
} else {
$msgs = $this->bns->get_messages_limited($nummsgs, message::MESSAGETYPE_NEWS);
$events = $this->bns->get_messages_limited($nummsgs, message::MESSAGETYPE_EVENT);
}
$sumlen = $this->bns->get_summarylength();
if ($msgs || $events || $this->bns->get_displaytype() == system::DISPLAY_SEPARATE_INTO_EVENT_AND_NEWSITEMS) {
$c = 1;
$this->content->text = $output->open_news_block_custom_wrapper();
$msglistclasses = 'block_news_msglist';
if ($this->bns->get_displaytype() == system::DISPLAY_DEFAULT) {
$msglistclasses .= ' block_news_noeventlist';
if ($COURSE->format === 'ousubject') {
$this->content->text .= $output->heading(get_string('newsheading', 'block_news'), 3, 'news-heading');
}
}
$this->content->text .= $output->container_start($msglistclasses);
if ($this->bns->get_displaytype() == system::DISPLAY_SEPARATE_INTO_EVENT_AND_NEWSITEMS) {
$this->content->text .= $output->heading(get_string('newsheading', 'block_news'), 3);
}
$newmsg = false;
$thumbnails = $this->bns->get_images('thumbnail');
$images = $this->bns->get_images();
if (empty($msgs)) {
$this->content->text .= get_string('nonewsyet', 'block_news');
} else {
foreach ($msgs as $msg) {
// Check whether there are news posts the user is not likely to have seen.
if (!empty($SESSION->news_block_views) &&
!empty($SESSION->news_block_views[$msg->get_id()])
) {
$courseaccess = time();
} else {
if (isset($USER->lastcourseaccess[$COURSE->id])) {
$courseaccess = $USER->lastcourseaccess[$COURSE->id];
} else {
$courseaccess = false;
}
}
$msgdate = $msg->get_messagedate();
$msguserid = $msg->get_userid();
if ($USER->id != $msguserid && (!$courseaccess || $msgdate > $courseaccess)) {
$newmsg = true;
}
$msgwidget = new short_message($msg, $this->bns, $sumlen, $c, $thumbnails, $images);
$this->content->text .= $output->render($msgwidget);
$c++;
}
}
if ($newmsg) {
$this->content->text .= $output->render_new_icon($blockinstanceid);
}
$this->content->text .= $output->container_end();
if ($this->bns->get_displaytype() == system::DISPLAY_SEPARATE_INTO_EVENT_AND_NEWSITEMS) {
$this->content->text .= $output->container_start('block_news_eventlist');
$this->content->text .= $output->heading(get_string('eventsheading', 'block_news'), 3);
if (empty($events)) {
$this->content->text .= get_string('noeventsyet', 'block_news');
} else {
foreach ($events as $event) {
$eventwidget = new short_message($event, $this->bns, $sumlen, $c, $thumbnails, $images);
$this->content->text .= $output->render($eventwidget);
}
}
$this->content->text .= $output->container_end();
}
$this->content->text .= $output->close_news_block_custom_wrapper();
// Main footer.
$this->content->footer .= $output->container_start($canaddnews, 'block_news_viewall');
$this->content->footer .= $output->render_view_all($blockinstanceid, $this->bns->get_viewall_label());
$this->content->footer .= $output->container_end();
} else {
$this->content->text = '';
if ($this->bns->get_displaytype() == system::DISPLAY_DEFAULT &&
$COURSE->format === 'ousubject') {
$this->content->text .= $output->heading(get_string('newsheading', 'block_news'), 3, 'news-heading');
}
$this->content->text .= $output->container(
get_string('msgblocknonews', 'block_news'), null, 'msgblocknonews');
if (has_capability('block/news:viewhidden', $blockcontext)) {
$this->content->footer .= $output->container_start(null, 'block_news_viewall');
$this->content->footer .= $output->render_view_all($blockinstanceid, $this->bns->get_viewall_label());
$this->content->footer .= $output->container_end();
}
}
// If feeds allowed on site, display icon.
if (isset($CFG->enablerssfeeds) && $CFG->enablerssfeeds) {
$this->content->footer .= $output->container_start($canaddnews, 'block_news_rss');
$pi = new pix_icon('i/rss', 'RSS');
$this->content->footer .= $output->action_icon(
$this->bns->get_feed_url(), $pi, null, array('title' => 'RSS'));
$this->content->footer .= $output->container_end();
}
return $this->content;
}
/**
* instance delete
*/
public function instance_delete() {
$this->bns->delete();
}
/**
* instance settings
*/
public function specialization() {
$blockinstanceid = $this->instance->id;
$this->bns = system::get_block_settings($blockinstanceid);
// Display title of this instance on config page (its put in block header and on
// block edit page header).
$t = $this->bns->get_title();
if (!empty($t)) {
$this->title = $t;
}
}
/**
* store config data for instance
*/
public function instance_config_save($data, $nolongerused = false) {
global $DB;
$this->bns->save($data);
// Force some config data (necessary for backup/restore).
$config = new stdClass;
$config->title = $data->title;
$config->id = $this->instance->id;
$DB->set_field('block_instances', 'configdata', base64_encode(serialize($config)),
array('id' => $this->instance->id));
}
public function instance_create() {
// Force some initial data for the block.
$data = new stdClass();
$data->title = get_string('defaultblocktitle', 'block_news');
$data->feedurls = '';
$this->instance_config_save($data);
return true;
}
/**
* limit contexts from which block can be created
*/
public function applicable_formats() {
return array('course' => true, 'mod' => true, 'my' => true);
}
}