Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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.
  13  //
  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/>.
  16  
  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;
  25  
  26  /**
  27   * Definition of the accessreview block.
  28   *
  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
  32   */
  33  class block_accessreview extends block_base {
  34      /**
  35       * Sets the block title.
  36       */
  37      public function init(): void {
  38          $this->title = get_string('errorssummary', 'block_accessreview');
  39      }
  40  
  41      /**
  42       * Defines where the block can be added.
  43       *
  44       * @return array
  45       */
  46      public function applicable_formats(): array {
  47          return [
  48              'course-view' => true,
  49              'site' => true,
  50              'mod' => false,
  51              'my' => false,
  52          ];
  53      }
  54  
  55      /**
  56       * Controls global configurability of block.
  57       *
  58       * @return bool
  59       */
  60      public function has_config(): bool {
  61          return true;
  62      }
  63  
  64      /**
  65       * Controls whether multiple block instances are allowed.
  66       *
  67       * @return bool
  68       */
  69      public function instance_allow_multiple(): bool {
  70          return false;
  71      }
  72  
  73      /**
  74       * Creates the block's main content
  75       *
  76       * @return string|stdClass
  77       */
  78      public function get_content() {
  79          global $COURSE, $OUTPUT;
  80  
  81          // If Brickfield accessibility toolkit has been disabled, do nothing.
  82          if (!accessibility::is_accessibility_enabled()) {
  83              return '';
  84          }
  85  
  86          if (isset($this->content)) {
  87              return $this->content;
  88          }
  89  
  90          $this->content = new stdClass;
  91          $this->content->text = '';
  92  
  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;
  97          }
  98  
  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')
 107              ];
 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;
 114              }
 115              $table->data = $tabledata;
 116              $table->attributes['class'] = 'generaltable table-sm block_accessreview_table';
 117              $this->content->text .= html_writer::table($table, true);
 118  
 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;
 126              }
 127  
 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);
 134  
 135              $this->content->text .= html_writer::tag('div', $toggle . $view . $download, [
 136                      'class' => 'block_accessreview_links'
 137                  ]
 138              );
 139  
 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);
 146              }
 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());
 155  
 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']);
 162          }
 163  
 164          return $this->content;
 165      }
 166  
 167      /**
 168       * This block shouldn't be added to a page if the accessibility tools setting is disabled.
 169       *
 170       * @param moodle_page $page
 171       * @return bool
 172       */
 173      public function can_block_be_added(moodle_page $page): bool {
 174          return accessibility::is_accessibility_enabled();
 175      }
 176  
 177      /**
 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
 183       */
 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++;
 195              }
 196          }
 197          $files = [
 198              'form' => '',
 199              'image' => '231/',
 200              'layout' => '234/',
 201              'link' => '237/',
 202              'media' => '240/',
 203              'table' => '243/',
 204              'text' => '246/',
 205          ];
 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;
 212              }
 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'
 222                  );
 223              } else if ($errordisplay == 'showpercent') {
 224                  $display = round(($total), 1) . '%';
 225              }
 226              $tabledata[] = [$typeicon . get_string('checktype:' . $type, manager::PLUGINNAME), $display];
 227          }
 228          return $tabledata;
 229      }
 230  
 231      /**
 232       * Get the link to toggle the heatmap.
 233       *
 234       * @return string
 235       * @throws coding_exception
 236       */
 237      protected function get_toggle_link(): string {
 238          global $OUTPUT;
 239  
 240          if (get_user_preferences('block_accessreviewtogglestate')) {
 241              $icon = 't/hide';
 242          } else {
 243              $icon = 't/show';
 244          }
 245  
 246          // Toggle overlay link.
 247          return html_writer::link(
 248              '#',
 249              $OUTPUT->pix_icon($icon, get_string('togglealt', 'block_accessreview'), 'moodle', ['class' => 'icon-accessmap']),
 250              [
 251                  'title' => get_string('togglealt', 'block_accessreview'),
 252                  'style' => 'cursor: pointer;',
 253                  'id' => 'toggle-accessmap',
 254                  'class' => 'block_accessreview_link',
 255              ]
 256          );
 257      }
 258  
 259      /**
 260       * Get the link to download a report for the specified context.
 261       *
 262       * @param context $context
 263       * @return string
 264       * @throws coding_exception
 265       * @throws moodle_exception
 266       */
 267      protected function get_download_link(context $context): string {
 268          global $OUTPUT, $COURSE;
 269  
 270          if (has_capability(accessibility::get_capability_name('viewcoursetools'), $context)) {
 271              return html_writer::link(
 272                  new moodle_url(accessibility::get_plugin_url(),
 273                      [
 274                          'courseid' => $COURSE->id,
 275                          'tab' => 'printable',
 276                          'target' => 'pdf',
 277                      ]
 278                  ),
 279                  $OUTPUT->pix_icon('a/download_all', get_string('downloadreportalt', 'block_accessreview')),
 280                  [
 281                      'title' => get_string('downloadreportalt', 'block_accessreview'),
 282                      'class' => 'block_accessreview_link download-accessmap',
 283                  ]
 284              );
 285          } else {
 286              return '';
 287          }
 288      }
 289  
 290      /**
 291       * Get the report link for the specified context.
 292       *
 293       * @param context $context
 294       * @return string
 295       * @throws coding_exception
 296       * @throws dml_exception
 297       * @throws moodle_exception
 298       */
 299      protected function get_report_link(context $context): string {
 300          global $OUTPUT, $COURSE;
 301  
 302          if (has_capability(accessibility::get_capability_name('viewcoursetools'), $context)) {
 303              return html_writer::link(
 304                  new moodle_url(accessibility::get_plugin_url(),
 305                      [
 306                          'courseid' => $COURSE->id,
 307                          'tab' => get_config('block_accessreview', 'toolpage'),
 308                      ]
 309                  ),
 310                  $OUTPUT->pix_icon('f/find', get_string('viewreportalt', 'block_accessreview'), 'block_accessreview'),
 311                  [
 312                      'title' => get_string('viewreportalt', 'block_accessreview'),
 313                      'class' => 'block_accessreview_link report-accessmap',
 314                  ]
 315              );
 316          } else {
 317              return '';
 318          }
 319      }
 320  }