forked from ned-code/moodle-block_sidebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_side_bar.php
364 lines (315 loc) · 14.9 KB
/
block_side_bar.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?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/>.
/**
* Allows for arbitrarily adding resources or activities to extra (non-standard) course sections with instance
* configuration for the block title.
*
* @package block_side_bar
* @see block_site_main_menu
* @author Justin Filip <[email protected]>
* @copyright 2011 onwards Justin Filip
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class block_side_bar extends block_list {
/**
* Setup the title for this block
*/
public function init() {
$this->title = get_string('pluginname', 'block_side_bar');
}
/**
* Parent class version of this function simply returns NULL This should be implemented by
* the derived class to return the content object.
*
* @return object The content object
*/
public function get_content() {
global $USER, $CFG, $DB, $OUTPUT;
require_once($CFG->dirroot.'/mod/hsuforum/lib.php');
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass();
$this->content->items = array();
$this->content->icons = array();
$this->content->footer = '';
if (empty($this->instance)) {
if (!isset($this->content)) {
$this->content = new stdClass();
}
return $this->content;
}
$locationmethod = 'number';
if (isset($this->config->location_method)) {
$locationmethod = $this->config->location_method;
}
$sectionname = '';
if (isset($this->config->section_name)) {
$sectionname = $this->config->section_name;
}
if (!isset($this->config->title)) {
if (!isset($this->config)) {
$this->config = new stdClass();
}
$this->config->title = '';
}
$course = $this->page->course;
require_once($CFG->dirroot.'/course/lib.php');
$context = context_course::instance($course->id);
$isediting = $this->page->user_is_editing() && has_capability('moodle/course:manageactivities', $context);
if (!empty($locationmethod) && !empty($sectionname)) {
if ($locationmethod == 'name') {
if ($section = $DB->get_record('course_sections',
array('course' => $course->id, 'name' => $sectionname))) {
$this->config->section_id = $section->id;
} else {
if (preg_match_all('/\d+/', $sectionname, $numbers)) {
if ($section = $DB->get_record('course_sections',
array('course' => $course->id, 'section' => end($numbers[0])))) {
$this->config->section_id = $section->id;
}
}
}
}
}
// Create a new section for this block (if necessary).
if (empty($this->config->section_id)) {
require_once($CFG->dirroot.'/blocks/side_bar/locallib.php');
if (null == ($section = block_side_bar_create_section($course))) {
return $this->content;
}
$this->config->section_id = $section->id;
parent::instance_config_commit();
} else {
$section = $DB->get_record('course_sections', array('id' => $this->config->section_id));
if (empty($section)) {
require_once($CFG->dirroot.'/blocks/side_bar/locallib.php');
if (null == ($section = block_side_bar_create_section($course))) {
return $this->content;
}
$this->config->section_id = $section->id;
parent::instance_config_commit();
}
}
// Extra fast view mode.
$modinfo = get_fast_modinfo($course);
if (!$isediting) {
if (!empty($modinfo->sections[$section->section])) {
$options = array('overflowdiv' => true);
foreach ($modinfo->sections[$section->section] as $cmid) {
$cm = $modinfo->cms[$cmid];
$count = hsuforum_count_forum_unread_posts($cm, $course);
if (!$cm->uservisible) {
continue;
}
$content = $cm->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
$instancename = $cm->get_formatted_name();
if (!($url = $cm->url)) {
$this->content->items[] = $content;
$this->content->icons[] = '';
} else {
$linkcss = $cm->visible ? '' : ' class="dimmed" ';
// Accessibility: incidental image - should be empty Alt text.
// Moving elements around and wrapping icon in its own span so I can manipulate it better.
$icon = '<span><img src="'.$cm->get_icon_url().'" class="icon" alt="" /></span>';
if ($count) {
$this->content->items[] = $icon . '<a title="' . $cm->modplural . '" ' . $linkcss . ' ' .
$cm->extra . ' href="' . $url . '">' . $instancename . '</a><a href="' . $url . '" title="'.
$instancename.' count"><span class="badge-side-bar badge-primary">' . $count . '</span</a>';
continue;
}
$this->content->items[] = $icon . '<a title="'.$cm->modplural.'"'.$linkcss.' '.$cm->extra.' href="'.
$url.'">'.$instancename.'</a>';
}
}
}
return $this->content;
}
// Editing on quick link btn.
$sectionurl = new moodle_url('/course/view.php',
array('id' => $course->id, 'section' => $section->section)
);
// Slow & hacky editing mode.
$courserenderer = $this->page->get_renderer('core', 'course');
$ismoving = ismoving($course->id);
if (!$cs = $DB->get_record('course_sections', array('section' => $section->section, 'course' => $course->id))) {
debugging('Could not get course section record for section '.$section->section, DEBUG_DEVELOPER);
return $this->content;
}
$modinfo = get_fast_modinfo($course);
$section = $modinfo->get_section_info($section->section);
$modnames = get_module_types_names();
$groupbuttons = $course->groupmode;
$groupbuttonslink = (!$course->groupmodeforce);
if ($ismoving) {
$strmovehere = get_string('movehere');
$strmovefull = strip_tags(get_string('movefull', '', "'$USER->activitycopyname'"));
$strcancel = get_string('cancel');
$stractivityclipboard = $USER->activitycopyname;
} else {
$strmove = get_string('move');
}
// Casting $course->modinfo to string prevents one notice when the field is null.
$editbuttons = '';
if ($ismoving) {
$this->content->icons[] = '<img src="'.$OUTPUT->pix_url('t/move') . '" class="iconsmall" alt="" />';
$this->content->items[] = $USER->activitycopyname.' (<a href="'.$CFG->wwwroot.
'/course/mod.php?cancelcopy=true&sesskey='.sesskey().'">'.$strcancel.'</a>)';
}
if (!empty($modinfo->sections[$section->section])) {
$options = array('overflowdiv' => true);
foreach ($modinfo->sections[$section->section] as $modnumber) {
$mod = $modinfo->cms[$modnumber];
if (!$mod->uservisible) {
continue;
}
if (!$ismoving) {
$actions = course_get_cm_edit_actions($mod, -1);
// Prepend list of actions with the 'move' action.
$actions = array('move' => new action_menu_link_primary(
new moodle_url('/course/mod.php', array('sesskey' => sesskey(), 'copy' => $mod->id)),
new pix_icon('t/move', $strmove, 'moodle', array('class' => 'iconsmall', 'title' => '')),
$strmove
)) + $actions;
$editactions = $courserenderer->course_section_cm_edit_actions($actions, $mod, array('donotenhance' => true));
$editbuttons = html_writer::tag('div', $editactions, array('class' => 'buttons'));
} else {
$editbuttons = '';
}
if ($mod->visible || has_capability('moodle/course:viewhiddenactivities', $context)) {
if ($ismoving) {
if ($mod->id == $USER->activitycopy) {
continue;
}
$this->content->items[] = '<a title="'.$strmovefull.'" href="'.$CFG->wwwroot.'/course/mod.php'.
'?moveto='.$mod->id.'&sesskey='.sesskey().'"><img style="height:16px; width:80px; border:0px"'.
' src="'.$OUTPUT->pix_url('movehere').'" alt="'.$strmovehere.'" /></a>';
$this->content->icons[] = '';
}
$content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true));
$instancename = $mod->get_formatted_name();
$linkcss = $mod->visible ? '' : ' class="dimmed" ';
if (!($url = $mod->url)) {
$this->content->items[] = $content.$editbuttons;
$this->content->icons[] = '';
} else {
// Accessibility: incidental image - should be empty Alt text.
$icon = '<img src="'.$mod->get_icon_url().'" class="icon" alt="" /> ';
$this->content->items[] = '<a title="'.$mod->modfullname.'" '.$linkcss.' '.$mod->extra.
' href="'.$url.'">'.$icon.$instancename.'</a>'.$editbuttons;
}
}
}
}
if ($ismoving) {
$this->content->items[] = '<a title="'.$strmovefull.'" href="'.$CFG->wwwroot.'/course/mod.php?'.
'movetosection='.$section->id.'&sesskey='.sesskey().'"><img style="height'.
':16px; width:80px; border:0px" src="'.$OUTPUT->pix_url('movehere').'" alt="'.
$strmovehere.'" /></a>';
$this->content->icons[] = '';
}
$this->content->items[] = '<hr />';
$this->content->icons[] = '';
$this->content->items[] = '<div class="section-quick-link"><a class="btn" href="'.
$sectionurl->out().'">'.get_string('gotosection', 'block_side_bar', $section->section).'</a></div>';
$this->content->icons[] = '';
$this->content->footer = $courserenderer->course_section_add_cm_control($course, $section->section,
null, array('inblock' => true));
// Replace modchooser with dropdown.
$this->content->footer = str_replace(
'hiddenifjs addresourcedropdown', 'visibleifjs addresourcedropdown', $this->content->footer
);
$this->content->footer = str_replace(
'visibleifjs addresourcemodchooser', 'hiddenifjs addresourcemodchooser', $this->content->footer
);
return $this->content;
}
/**
* This function is called on your subclass right after an instance is loaded. Use this function to act on instance data just
* after it's loaded and before anything else is done. For instance: if your block will have different title's depending on
* location (site, course, blog, etc)
*
* @throws dml_exception
* @return void
*/
public function specialization() {
global $OUTPUT, $DB;
if (!empty($this->config->title)) {
$this->title = '<span><img src="'.$OUTPUT->image_url('monologo', 'hsuforum').
'" class="icon" alt="" /></span> ' . $this->config->title;
}
$this->instance->weight = -10;
$this->instance->region = 'content';
$this->instance->defaultweight = -10;
$this->instance->defaultregion = 'content';
$DB->update_record('block_instances', $this->instance);
// Cause the block to recalculate its content immediately.
$this->refresh_content();
}
/**
* Subclasses should override this and return true if the subclass block has a config_global.html file.
*
* @return bool
*/
public function has_config() {
return false;
}
/**
* Are you going to allow multiple instances of each block? If yes, then it is assumed that the block
* WILL USE per-instance configuration.
* @return bool
*/
public function instance_allow_multiple() {
return true;
}
/**
* Which page types this block may appear on.
*
* The information returned here is processed by the
* {@link blocks_name_allowed_in_format()} function. Look there if you need
* to know exactly how this works.
*
* Default case: everything except mod and tag.
*
* @return array page-type prefix => true/false.
*/
public function applicable_formats() {
return array(
'site-index' => true,
'course-view' => true,
'blocks-side_bar' => true
);
}
/**
* Code executed after a course is restored with this block present in the restore data. Allows for setting the new course
* section ID for the restored course as part of this block's instance configuration.
*
* @return bool Status indicator
*/
public function after_restore() {
global $DB;
// Get the correct course_sections record ID for the new course.
$section = $DB->get_record('course_sections',
array('course' => $this->instance->pageid, 'section' => $this->config->section_id)
);
if (!empty($section->id)) {
$this->config->section_id = $section->id;
parent::instance_config_commit();
}
return true;
}
}