forked from at-tools/moodle-block_massaction
-
Notifications
You must be signed in to change notification settings - Fork 14
/
block_massaction.php
202 lines (182 loc) · 8.69 KB
/
block_massaction.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
<?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/>.
/**
* Primary block class.
*
* @package block_massaction
* @copyright 2013 University of Minnesota
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use block_massaction\hook\filter_sections_different_course;
use block_massaction\hook\filter_sections_same_course;
/**
* Configures and displays the block.
*
* @copyright 2013 University of Minnesota
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_massaction extends block_base {
/**
* Initialize the plugin. This method is being called by the parent constructor by default.
*/
public function init() {
$this->title = get_string('blocktitle', 'block_massaction');
}
/**
* Which page types this block may appear on.
*
* The information returned here is processed by the
* blocks_name_allowed_in_format() function. Look there if you need
* to know exactly how this works.
*
* @return array page-type prefix => true/false.
* @throws dml_exception
*/
public function applicable_formats(): array {
$applicableformats['site-index'] = false;
$formats = explode(',', get_config('block_massaction', 'applicablecourseformats'));
foreach ($formats as $pluginname) {
$applicableformats['course-view-' . $pluginname] = true;
}
return $applicableformats;
}
/**
* No need to have multiple blocks to perform the same functionality
*/
public function instance_allow_multiple(): bool {
return false;
}
/**
* Has config function.
*
* @see block_base::has_config()
*/
public function has_config() {
return true;
}
/**
* Sets up the content of the block for display to the user.
*
* @return stdClass The HTML content of the block.
* @throws coding_exception
* @throws moodle_exception
*/
public function get_content(): stdClass {
global $CFG, $COURSE, $OUTPUT;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass();
$this->content->text = '';
$this->content->footer = '';
if ($this->page->user_is_editing()) {
$blockcontext = context_block::instance($this->instance->id);
if (!has_capability('block/massaction:use', $blockcontext)) {
$this->content->text = get_string('nopermissions', 'error', get_string('massaction:use', 'block_massaction'));
return $this->content;
}
$applicableformatkey = 'course-view-' . $COURSE->format;
$iscoursecompatible = in_array($applicableformatkey, array_keys($this->applicable_formats()))
&& $this->applicable_formats()[$applicableformatkey];
if (!$iscoursecompatible) {
$this->content = new stdClass();
$this->content->text = get_string('unusable', 'block_massaction');
$this->content->footer = '';
return $this->content;
}
// Check for double instances. This usually should not be an issue, but in rare cases users manage to add
// two blocks to the site.
$instancecounter = 0;
foreach ($this->page->blocks->get_regions() as $region) {
foreach ($this->page->blocks->get_blocks_for_region($region) as $block) {
if ($block instanceof block_massaction) {
$instancecounter++;
}
if ($instancecounter > 1) {
$this->content = new stdClass();
$this->content->text = get_string('multipleinstances', 'block_massaction');
$this->content->footer = '';
return $this->content;
}
}
}
$modinfo = get_fast_modinfo($COURSE->id);
$filtersectionshook = new filter_sections_same_course($COURSE->id, array_keys($modinfo->get_section_info_all()));
\core\di::get(\core\hook\manager::class)->dispatch($filtersectionshook);
$sectionsavailable = $filtersectionshook->get_sectionnums();
// Initialize the JS module.
$this->page->requires->js_call_amd('block_massaction/massactionblock', 'init');
$context = context_course::instance($COURSE->id);
// Actions to be rendered later on.
$actionicons = [];
if (has_capability('moodle/course:activityvisibility', $context)
&& has_capability('block/massaction:activityshowhide', $blockcontext)) {
// As we want to use this symbol for the *operation*, not the state, we switch the icons hide/show.
$actionicons['show'] = 't/hide';
$actionicons['hide'] = 't/show';
if (!empty($CFG->allowstealth)) {
$actionicons['makeavailable'] = 't/block';
}
}
if (has_capability('moodle/backup:backuptargetimport', $context)
&& has_capability('moodle/restore:restoretargetimport', $context)
&& has_capability('block/massaction:duplicate', $blockcontext)) {
$actionicons['duplicate'] = 't/copy';
}
if (has_capability('moodle/backup:backuptargetimport', $context)
&& has_capability('block/massaction:duplicatetocourse', $blockcontext)) {
$actionicons['duplicatetocourse'] = 't/copy';
}
if (has_capability('moodle/course:manageactivities', $context)) {
if (has_capability('block/massaction:delete', $blockcontext)) {
$actionicons['delete'] = 't/delete';
}
if (course_get_format($COURSE->id)->uses_indentation()
&& has_capability('block/massaction:indent', $blockcontext)) {
// From Moodle 4.0 on the course format has to declare if it supports indentation or not.
$actionicons['moveright'] = 't/right';
$actionicons['moveleft'] = 't/left';
}
if (has_capability('block/massaction:descriptionshowhide', $blockcontext)) {
$actionicons['showdescription'] = 't/more';
$actionicons['hidedescription'] = 't/less';
}
}
if (has_capability('block/massaction:sendcontentchangednotifications', $blockcontext)) {
$actionicons['contentchangednotification'] = 't/email';
}
$actions = [];
foreach ($actionicons as $action => $iconpath) {
$actions[] = ['action' => $action, 'icon' => $iconpath,
'actiontext' => get_string('action_' . $action, 'block_massaction')];
}
$this->content->text = $OUTPUT->render_from_template('block_massaction/block_massaction',
['actions' => $actions,
'formaction' => $CFG->wwwroot . '/blocks/massaction/action.php',
'instanceid' => $this->instance->id, 'requesturi' => $_SERVER['REQUEST_URI'],
'helpicon' => $OUTPUT->help_icon('usage', 'block_massaction'),
'show_moveto_select' => (has_capability('moodle/course:manageactivities', $context) &&
has_capability('block/massaction:movetosection', $context)),
'show_duplicateto_select' => (has_capability('moodle/backup:backuptargetimport', $context) &&
has_capability('moodle/restore:restoretargetimport', $context) &&
has_capability('block/massaction:movetosection', $context)),
'sectionselecthelpicon' => $OUTPUT->help_icon('sectionselect', 'block_massaction'),
'availabletargetsections' => implode(',', $sectionsavailable),
]);
}
return $this->content;
}
}