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 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  namespace tool_brickfield\output;
  18  
  19  use tool_brickfield\accessibility;
  20  use plugin_renderer_base;
  21  use moodle_url;
  22  use tabobject;
  23  use tabtree;
  24  use html_writer;
  25  use tool_brickfield\analysis;
  26  use tool_brickfield\local\tool\filter;
  27  use tool_brickfield\local\tool\tool;
  28  use tool_brickfield\manager;
  29  use tool_brickfield\scheduler;
  30  
  31  /**
  32   * tool_brickfield renderer
  33   *
  34   * @package    tool_brickfield
  35   * @copyright  2020 onward: Brickfield Education Labs, https://www.brickfield.ie
  36   * @author     Bas Brands
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class renderer extends plugin_renderer_base {
  40  
  41      /**
  42       * Render the page containing the tool report.
  43       *
  44       * @param \stdClass $data Report data.
  45       * @param filter $filter Display filters.
  46       * @return String HTML showing charts.
  47       */
  48      public function display(\stdClass $data, filter $filter): string {
  49          $component = 'tool_brickfield';
  50          $subtype = $filter->tab;
  51          $toolrenderer = $this->page->get_renderer($component, $subtype);
  52          if (!empty($toolrenderer)) {
  53              return $toolrenderer->display($data, $filter);
  54          }
  55      }
  56  
  57      /**
  58       * Render the valid tabs.
  59       *
  60       * @param filter $filter
  61       * @param array $tools
  62       * @return string
  63       * @throws \moodle_exception
  64       */
  65      public function tabs(filter $filter, array $tools): string {
  66          $idprefix = 'tab_';
  67          $tabs = [];
  68          foreach ($tools as $toolname => $tool) {
  69              $link = new moodle_url(
  70                  accessibility::get_plugin_url(),
  71                  array_merge(['tab' => $toolname, ], $tool->toplevel_arguments($filter))
  72              );
  73              if (isset($altlabel[$toolname])) {
  74                  $label = $altlabel[$toolname];
  75              } else {
  76                  $label = $tool->get_toolshortname();
  77              }
  78              $tab = new tabobject($idprefix . $toolname, $link, $label);
  79              $tab->extraclass = isset($extraclass[$toolname]) ? $extraclass[$toolname] : null;
  80              $tabs[] = $tab;
  81          }
  82          return $this->render(new tabtree($tabs, $idprefix . $filter->tab));
  83      }
  84  
  85      /**
  86       * Renders tabtree
  87       *
  88       * @param tabtree $tabtree
  89       * @return string
  90       * @throws \moodle_exception
  91       */
  92      protected function render_tabtree(tabtree $tabtree): string {
  93          if (empty($tabtree->subtree)) {
  94              return '';
  95          }
  96          $data = $tabtree->export_for_template($this);
  97          foreach ($data->tabs as $idx => $tab) {
  98              if (isset($tabtree->subtree[$idx]->extraclass)) {
  99                  $data->tabs[$idx]->extraclass = $tabtree->subtree[$idx]->extraclass;
 100              }
 101          }
 102          return $this->render_from_template(manager::PLUGINNAME . '/tabtree', $data);
 103      }
 104  
 105      /**
 106       * Render the cache alert message.
 107       *
 108       * @return string
 109       * @throws \coding_exception
 110       * @throws \dml_exception
 111       */
 112      public function cachealert(): string {
 113          $html = '';
 114          if (!analysis::is_enabled()) {
 115              $html = \html_writer::div(get_string('analysistypedisabled', manager::PLUGINNAME),
 116                  '', ['class' => 'alert alert-primary']);
 117          }
 118          return $html;
 119      }
 120  
 121      /**
 122       * This function assumes that 'scheduler::is_analysed' has already failed.
 123       * @param int $courseid
 124       * @return string
 125       * @throws \coding_exception
 126       * @throws \moodle_exception
 127       */
 128      public function analysisalert(int $courseid): string {
 129          $siteorcourse = ($courseid == SITEID) ? 'site' : '';
 130          if (scheduler::is_course_in_schedule($courseid)) {
 131              $html = \html_writer::div(get_string('schedule:' . $siteorcourse . 'scheduled', manager::PLUGINNAME),
 132                  '', ['class' => 'alert alert-primary']);
 133          } else {
 134              $html = \html_writer::div(
 135                  get_string('schedule:' . $siteorcourse . 'notscheduled', manager::PLUGINNAME, manager::get_helpurl()),
 136                  '', ['class' => 'alert alert-primary']
 137              );
 138              $html .= $this->analysisbutton($courseid);
 139          }
 140          return $html;
 141      }
 142  
 143      /**
 144       * Render the "not validated" alert message.
 145       *
 146       * @return string
 147       * @throws \coding_exception
 148       */
 149      public function notvalidatedalert(): string {
 150          return \html_writer::div(get_string('notvalidated', manager::PLUGINNAME), '', ['class' => 'alert alert-primary']);
 151      }
 152  
 153      /**
 154       * Render the analysis request button.
 155       *
 156       * @param int $courseid
 157       * @return string
 158       * @throws \coding_exception
 159       * @throws \moodle_exception
 160       */
 161      public function analysisbutton(int $courseid) : string {
 162          $link = new moodle_url(accessibility::get_plugin_url(), [
 163              'action' => 'requestanalysis',
 164              'courseid' => $courseid
 165          ]);
 166  
 167          $classname = manager::PLUGINNAME . '_analysisbutton';
 168  
 169          $button = new \single_button(
 170              $link,
 171              get_string('schedule:requestanalysis', manager::PLUGINNAME),
 172              'post',
 173              true,
 174              ['class' => $classname]
 175          );
 176  
 177          return html_writer::tag('div', $this->render($button), ['class' => $classname]);
 178      }
 179  }