Merge branch 'MDL-73993-master' of https://github.com/golenkovm/moodle
[moodle.git] / blocks / accessreview / block_accessreview.php
blob1743b06b336980d9fed47a33f130edf92db1b764
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 use tool_brickfield\accessibility;
18 use tool_brickfield\analysis;
19 use tool_brickfield\area_base;
20 use tool_brickfield\local\tool\filter;
21 use tool_brickfield\manager;
22 use tool_brickfield\registration;
23 use tool_brickfield\scheduler;
24 use tool_brickfield\sitedata;
26 /**
27 * Definition of the accessreview block.
29 * @package block_accessreview
30 * @copyright 2019 Karen Holland LTS.ie
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class block_accessreview extends block_base {
34 /**
35 * Sets the block title.
37 public function init(): void {
38 $this->title = get_string('errorssummary', 'block_accessreview');
41 /**
42 * Defines where the block can be added.
44 * @return array
46 public function applicable_formats(): array {
47 return [
48 'course-view' => true,
49 'site' => true,
50 'mod' => false,
51 'my' => false,
55 /**
56 * Controls global configurability of block.
58 * @return bool
60 public function has_config(): bool {
61 return true;
64 /**
65 * Controls whether multiple block instances are allowed.
67 * @return bool
69 public function instance_allow_multiple(): bool {
70 return false;
73 /**
74 * Creates the block's main content
76 * @return string|stdClass
78 public function get_content() {
79 global $COURSE, $OUTPUT;
81 // If Brickfield accessibility toolkit has been disabled, do nothing.
82 if (!accessibility::is_accessibility_enabled()) {
83 return '';
86 if (isset($this->content)) {
87 return $this->content;
90 $this->content = new stdClass;
91 $this->content->text = '';
93 // Check to see user can view/use the accessmap.
94 $context = context_course::instance($COURSE->id);
95 if (!isloggedin() || isguestuser() || !has_capability('block/accessreview:view', $context)) {
96 return $this->content;
99 // Check for valid registration.
100 if (!(new registration())->toolkit_is_active()) {
101 $this->content->text = manager::registration_message();
102 } else if (scheduler::is_course_analyzed($COURSE->id)) {
103 // Build error data table.
104 $table = new html_table();
105 $table->head = [
106 get_string('checktypes', 'block_accessreview'), get_string('errors', 'block_accessreview')
108 $table->align = ['left', 'center'];
109 $tabledata = $this->get_table_data($COURSE->id);
110 // Handling no data.
111 if ($tabledata === null) {
112 $this->content->text = get_string('nodata', 'block_accessreview');
113 return $this->content;
115 $table->data = $tabledata;
116 $table->attributes['class'] = 'generaltable table-sm block_accessreview_table';
117 $this->content->text .= html_writer::table($table, true);
119 // Check for compatible course formats for highlighting.
120 $showhighlighting = false;
121 switch ($COURSE->format) {
122 case accessibility::TOOL_BRICKFIELD_FORMAT_TOPIC:
123 case accessibility::TOOL_BRICKFIELD_FORMAT_WEEKLY:
124 $showhighlighting = true;
125 break;
128 // Toggle overlay link.
129 $toggle = ($showhighlighting) ? $this->get_toggle_link() : '';
130 // Report download link.
131 $download = $this->get_download_link($context);
132 // Report view link.
133 $view = $this->get_report_link($context);
135 $this->content->text .= html_writer::tag('div', $toggle . $view . $download, [
136 'class' => 'block_accessreview_links'
140 if ($showhighlighting) {
141 // Setting up AMD module.
142 $whattoshow = get_config('block_accessreview', 'whattoshow');
143 $toggled = get_user_preferences('block_accessreviewtogglestate', true);
144 $arguments = [$toggled, $whattoshow, $COURSE->id];
145 $this->page->requires->js_call_amd('block_accessreview/module', 'init', $arguments);
147 } else if (scheduler::is_course_in_schedule($COURSE->id)) {
148 // Display a message that the course is awaiting analysis.
149 $this->content->text = get_string('schedule:scheduled', manager::PLUGINNAME);
150 } else if (!analysis::is_enabled()) {
151 $this->content->text = get_string('analysistypedisabled', manager::PLUGINNAME);
152 } else {
153 // Display a button to request analysis.
154 $this->content->text = get_string('schedule:blocknotscheduled', manager::PLUGINNAME, manager::get_helpurl());
156 $button = new single_button(
157 new moodle_url(accessibility::get_plugin_url(), ['action' => 'requestanalysis', 'courseid' => $COURSE->id]),
158 get_string('schedule:requestanalysis', manager::PLUGINNAME), 'post', true,
159 ['class' => 'block_accessreview_analysisbutton']);
160 $this->content->text .= html_writer::tag('div', $OUTPUT->render($button),
161 ['class' => 'block_accessreview_analysisbutton']);
164 return $this->content;
168 * This block shouldn't be added to a page if the accessibility tools setting is disabled.
170 * @param moodle_page $page
171 * @return bool
173 public function can_block_be_added(moodle_page $page): bool {
174 global $CFG;
176 return $CFG->enableaccessibilitytools;
180 * Fetches and groups the relevent error data for the table to display.
181 * @param int $courseid The ID of the course.
182 * @return array The data required by the table.
183 * @throws coding_exception
184 * @throws moodle_exception
186 protected function get_table_data($courseid): array {
187 global $OUTPUT;
188 $datafilters = new filter($courseid, 0);
189 $errordisplay = get_config('block_accessreview', 'errordisplay');
190 $summarydata = (new sitedata())->get_checkgroup_data($datafilters);
191 $data = [];
192 $count = 0;
193 for ($i = 1; $count < $summarydata[0]->groupcount; $i++) {
194 if (isset($summarydata[0]->{'componentlabel' . $i})) {
195 $data[$i] = $summarydata[0]->{'errorsvalue' . $i};
196 $count++;
199 $files = [
200 'form' => '',
201 'image' => '231/',
202 'layout' => '234/',
203 'link' => '237/',
204 'media' => '240/',
205 'table' => '243/',
206 'text' => '246/',
208 // Populating table data.
209 $tabledata = [];
210 foreach ($data as $key => $total) {
211 // If the total is empty it means there is no results yet in the table.
212 if ($total === null) {
213 continue;
215 $type = area_base::checkgroup_name($key);
216 // Error display data.
217 $display = $total;
218 // Icons.
219 $typeicon = $OUTPUT->pix_icon('f/' . $type, '', 'block_accessreview');
220 if ($errordisplay == 'showicon') {
221 $thistype = $total == 0 ? 'smile' : 'frown';
222 $display = $OUTPUT->pix_icon($thistype,
223 get_string($thistype, 'block_accessreview'), 'block_accessreview'
225 } else if ($errordisplay == 'showpercent') {
226 $display = round(($total), 1) . '%';
228 $tabledata[] = [$typeicon . get_string('checktype:' . $type, manager::PLUGINNAME), $display];
230 return $tabledata;
234 * Get the link to toggle the heatmap.
236 * @return string
237 * @throws coding_exception
239 protected function get_toggle_link(): string {
240 global $OUTPUT;
242 if (get_user_preferences('block_accessreviewtogglestate')) {
243 $icon = 't/hide';
244 } else {
245 $icon = 't/show';
248 // Toggle overlay link.
249 return html_writer::link(
250 '#',
251 $OUTPUT->pix_icon($icon, get_string('togglealt', 'block_accessreview'), 'moodle', ['class' => 'icon-accessmap']),
253 'title' => get_string('togglealt', 'block_accessreview'),
254 'style' => 'cursor: pointer;',
255 'id' => 'toggle-accessmap',
256 'class' => 'block_accessreview_link',
262 * Get the link to download a report for the specified context.
264 * @param context $context
265 * @return string
266 * @throws coding_exception
267 * @throws moodle_exception
269 protected function get_download_link(context $context): string {
270 global $OUTPUT, $COURSE;
272 if (has_capability(accessibility::get_capability_name('viewcoursetools'), $context)) {
273 return html_writer::link(
274 new moodle_url(accessibility::get_plugin_url(),
276 'courseid' => $COURSE->id,
277 'tab' => 'printable',
278 'target' => 'pdf',
281 $OUTPUT->pix_icon('a/download_all', get_string('downloadreportalt', 'block_accessreview')),
283 'title' => get_string('downloadreportalt', 'block_accessreview'),
284 'class' => 'block_accessreview_link download-accessmap',
287 } else {
288 return '';
293 * Get the report link for the specified context.
295 * @param context $context
296 * @return string
297 * @throws coding_exception
298 * @throws dml_exception
299 * @throws moodle_exception
301 protected function get_report_link(context $context): string {
302 global $OUTPUT, $COURSE;
304 if (has_capability(accessibility::get_capability_name('viewcoursetools'), $context)) {
305 return html_writer::link(
306 new moodle_url(accessibility::get_plugin_url(),
308 'courseid' => $COURSE->id,
309 'tab' => get_config('block_accessreview', 'toolpage'),
312 $OUTPUT->pix_icon('f/find', get_string('viewreportalt', 'block_accessreview'), 'block_accessreview'),
314 'title' => get_string('viewreportalt', 'block_accessreview'),
315 'class' => 'block_accessreview_link report-accessmap',
318 } else {
319 return '';