2 // This file is part of Moodle - http://moodle.org/
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.
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
;
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
{
35 * Sets the block title.
37 public function init(): void
{
38 $this->title
= get_string('errorssummary', 'block_accessreview');
42 * Defines where the block can be added.
46 public function applicable_formats(): array {
48 'course-view' => true,
56 * Controls global configurability of block.
60 public function has_config(): bool {
65 * Controls whether multiple block instances are allowed.
69 public function instance_allow_multiple(): bool {
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()) {
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();
106 get_string('checktypes', 'block_accessreview'), get_string('errors', 'block_accessreview')
108 $table->align
= ['left', 'center'];
109 $tabledata = $this->get_table_data($COURSE->id
);
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;
128 // Toggle overlay link.
129 $toggle = ($showhighlighting) ?
$this->get_toggle_link() : '';
130 // Report download link.
131 $download = $this->get_download_link($context);
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
);
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
173 public function can_block_be_added(moodle_page
$page): bool {
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 {
188 $datafilters = new filter($courseid, 0);
189 $errordisplay = get_config('block_accessreview', 'errordisplay');
190 $summarydata = (new sitedata())->get_checkgroup_data($datafilters);
193 for ($i = 1; $count < $summarydata[0]->groupcount
; $i++
) {
194 if (isset($summarydata[0]->{'componentlabel' . $i})) {
195 $data[$i] = $summarydata[0]->{'errorsvalue' . $i};
208 // Populating table data.
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) {
215 $type = area_base
::checkgroup_name($key);
216 // Error display data.
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];
234 * Get the link to toggle the heatmap.
237 * @throws coding_exception
239 protected function get_toggle_link(): string {
242 if (get_user_preferences('block_accessreviewtogglestate')) {
248 // Toggle overlay link.
249 return html_writer
::link(
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
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',
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',
293 * Get the report link for the specified context.
295 * @param context $context
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',