MDL-80880 quiz: change display of previous attempts summary
[moodle.git] / blocks / accessreview / block_accessreview.php
blobff552b5d65bdb09f7ee9a502ee613dec400705b1
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', single_button::BUTTON_PRIMARY,
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 return accessibility::is_accessibility_enabled();
178 * Fetches and groups the relevent error data for the table to display.
179 * @param int $courseid The ID of the course.
180 * @return array The data required by the table.
181 * @throws coding_exception
182 * @throws moodle_exception
184 protected function get_table_data($courseid): array {
185 global $OUTPUT;
186 $datafilters = new filter($courseid, 0);
187 $errordisplay = get_config('block_accessreview', 'errordisplay');
188 $summarydata = (new sitedata())->get_checkgroup_data($datafilters);
189 $data = [];
190 $count = 0;
191 for ($i = 1; $count < $summarydata[0]->groupcount; $i++) {
192 if (isset($summarydata[0]->{'componentlabel' . $i})) {
193 $data[$i] = $summarydata[0]->{'errorsvalue' . $i};
194 $count++;
197 $files = [
198 'form' => '',
199 'image' => '231/',
200 'layout' => '234/',
201 'link' => '237/',
202 'media' => '240/',
203 'table' => '243/',
204 'text' => '246/',
206 // Populating table data.
207 $tabledata = [];
208 foreach ($data as $key => $total) {
209 // If the total is empty it means there is no results yet in the table.
210 if ($total === null) {
211 continue;
213 $type = area_base::checkgroup_name($key);
214 // Error display data.
215 $display = $total;
216 // Icons.
217 $typeicon = $OUTPUT->pix_icon('f/' . $type, '', 'block_accessreview');
218 if ($errordisplay == 'showicon') {
219 $thistype = $total == 0 ? 'smile' : 'frown';
220 $display = $OUTPUT->pix_icon($thistype,
221 get_string($thistype, 'block_accessreview'), 'block_accessreview'
223 } else if ($errordisplay == 'showpercent') {
224 $display = round(($total), 1) . '%';
226 $tabledata[] = [$typeicon . get_string('checktype:' . $type, manager::PLUGINNAME), $display];
228 return $tabledata;
232 * Get the link to toggle the heatmap.
234 * @return string
235 * @throws coding_exception
237 protected function get_toggle_link(): string {
238 global $OUTPUT;
240 if (get_user_preferences('block_accessreviewtogglestate')) {
241 $icon = 't/hide';
242 } else {
243 $icon = 't/show';
246 // Toggle overlay link.
247 return html_writer::link(
248 '#',
249 $OUTPUT->pix_icon($icon, get_string('togglealt', 'block_accessreview'), 'moodle', ['class' => 'icon-accessmap']),
251 'title' => get_string('togglealt', 'block_accessreview'),
252 'style' => 'cursor: pointer;',
253 'id' => 'toggle-accessmap',
254 'class' => 'block_accessreview_link',
260 * Get the link to download a report for the specified context.
262 * @param context $context
263 * @return string
264 * @throws coding_exception
265 * @throws moodle_exception
267 protected function get_download_link(context $context): string {
268 global $OUTPUT, $COURSE;
270 if (has_capability(accessibility::get_capability_name('viewcoursetools'), $context)) {
271 return html_writer::link(
272 new moodle_url(accessibility::get_plugin_url(),
274 'courseid' => $COURSE->id,
275 'tab' => 'printable',
276 'target' => 'pdf',
279 $OUTPUT->pix_icon('a/download_all', get_string('downloadreportalt', 'block_accessreview')),
281 'title' => get_string('downloadreportalt', 'block_accessreview'),
282 'class' => 'block_accessreview_link download-accessmap',
285 } else {
286 return '';
291 * Get the report link for the specified context.
293 * @param context $context
294 * @return string
295 * @throws coding_exception
296 * @throws dml_exception
297 * @throws moodle_exception
299 protected function get_report_link(context $context): string {
300 global $OUTPUT, $COURSE;
302 if (has_capability(accessibility::get_capability_name('viewcoursetools'), $context)) {
303 return html_writer::link(
304 new moodle_url(accessibility::get_plugin_url(),
306 'courseid' => $COURSE->id,
307 'tab' => get_config('block_accessreview', 'toolpage'),
310 $OUTPUT->pix_icon('f/find', get_string('viewreportalt', 'block_accessreview'), 'block_accessreview'),
312 'title' => get_string('viewreportalt', 'block_accessreview'),
313 'class' => 'block_accessreview_link report-accessmap',
316 } else {
317 return '';