Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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  /**
  18   * Invalid analysables renderable.
  19   *
  20   * @package    tool_analytics
  21   * @copyright  2017 David Monllao {@link http://www.davidmonllao.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_analytics\output;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  /**
  30   * Invalid analysables renderable.
  31   *
  32   * @package    tool_analytics
  33   * @copyright  2017 David Monllao {@link http://www.davidmonllao.com}
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class invalid_analysables implements \renderable, \templatable {
  37  
  38      /**
  39       * @var \core_analytics\model
  40       */
  41      protected $model = null;
  42  
  43      /**
  44       * @var int
  45       */
  46      protected $page = 0;
  47  
  48      /**
  49       * @var int
  50       */
  51      protected $perpage = 0;
  52  
  53      /**
  54       * Inits the invalid analysables renderable.
  55       *
  56       * @param \core_analytics\model $model
  57       * @param int $page
  58       * @param int $perpage
  59       * @return \stdClass
  60       */
  61      public function __construct(\core_analytics\model $model, $page, $perpage) {
  62  
  63          $this->model = $model;
  64          $this->page = $page;
  65          $this->perpage = $perpage;
  66      }
  67  
  68      /**
  69       * Export the data.
  70       *
  71       * @param \renderer_base $output
  72       * @return \stdClass
  73       */
  74      public function export_for_template(\renderer_base $output) {
  75          global $PAGE;
  76  
  77          $offset = $this->page * $this->perpage;
  78  
  79          $contexts = $this->model->get_contexts();
  80          $analysables = $this->model->get_analyser(['notimesplitting' => true])->get_analysables_iterator(null, $contexts);
  81  
  82          $skipped = 0;
  83          $enoughresults = false;
  84          $morepages = false;
  85          $results = array();
  86          foreach ($analysables as $analysable) {
  87  
  88              if (!$analysable) {
  89                  continue;
  90              }
  91  
  92              $validtraining = $this->model->get_target()->is_valid_analysable($analysable, true);
  93              if ($validtraining === true) {
  94                  if ($this->model->is_static()) {
  95                      // We still want to show this analysable if it is not valid to get predictions.
  96                      $validtraining = get_string('notrainingbasedassumptions', 'analytics');
  97                  } else {
  98                      // We skip analysables that are valid for training or valid for prediction.
  99                      continue;
 100                  }
 101              }
 102  
 103              $validprediction = $this->model->get_target()->is_valid_analysable($analysable, false);
 104              if ($validprediction === true) {
 105                  // We skip analysables that are valid for training or valid for prediction.
 106                  continue;
 107              }
 108  
 109              if ($offset && $skipped < $offset) {
 110                  $skipped++;
 111                  continue;
 112              }
 113  
 114              // Add a new results if we don't have enough yet.
 115              if (!$enoughresults) {
 116                  $results[$analysable->get_id()] = array($analysable, $validtraining, $validprediction);
 117                  if ($this->perpage && count($results) === $this->perpage) {
 118                      $enoughresults = true;
 119                  }
 120              } else {
 121                  // Confirmed that we have results we can not fit into this page.
 122                  $morepages = true;
 123                  break;
 124              }
 125          }
 126  
 127          // Prepare the context object.
 128          $data = new \stdClass();
 129          $data->modelname = $this->model->get_name();
 130  
 131          if ($this->page > 0) {
 132              $prev = clone $PAGE->url;
 133              $prev->param('page', $this->page - 1);
 134              $button = new \single_button($prev, get_string('previouspage', 'tool_analytics'), 'get');
 135              $data->prev = $button->export_for_template($output);
 136          }
 137          if ($morepages) {
 138              $next = clone $PAGE->url;
 139              $next->param('page', $this->page + 1);
 140              $button = new \single_button($next, get_string('nextpage', 'tool_analytics'), 'get');
 141              $data->next = $button->export_for_template($output);
 142          }
 143  
 144          $data->analysables = [];
 145          foreach ($results as list($analysable, $validtraining, $validprediction)) {
 146              $obj = new \stdClass();
 147              $obj->url = \html_writer::link($analysable->get_context()->get_url(), $analysable->get_name(),
 148                  array('target' => '_blank'));
 149  
 150              if ($validtraining !== true) {
 151                  $obj->validtraining = $validtraining;
 152              }
 153              if ($validprediction !== true) {
 154                  $obj->validprediction = $validprediction;
 155              }
 156              $data->analysables[] = $obj;
 157          }
 158  
 159          if (empty($data->analysables)) {
 160              $data->noanalysables = [
 161                  'message' => get_string('noinvalidanalysables', 'tool_analytics'),
 162                  'announce' => true,
 163              ];
 164          }
 165          return $data;
 166      }
 167  }