Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Insights list page.
  19   *
  20   * @package    report_insights
  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 report_insights\output;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Shows report_insights insights list.
  31   *
  32   * @package    report_insights
  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 insights_list implements \renderable, \templatable {
  37  
  38      /**
  39       * @var \core_analytics\model
  40       */
  41      protected $model;
  42  
  43      /**
  44       * @var \context
  45       */
  46      protected $context;
  47  
  48      /**
  49       * @var \core_analytics\model[]
  50       */
  51      protected $othermodels;
  52  
  53      /**
  54       * @var int
  55       */
  56      protected $page;
  57  
  58      /**
  59       * @var int
  60       */
  61      protected $perpage;
  62  
  63      /**
  64       * Constructor
  65       *
  66       * @param \core_analytics\model $model
  67       * @param \context $context
  68       * @param \core_analytics\model[] $othermodels
  69       * @param int $page
  70       * @param int $perpage The max number of results to fetch
  71       * @return void
  72       */
  73      public function __construct(\core_analytics\model $model, \context $context, $othermodels, $page = 0, $perpage = 100) {
  74          $this->model = $model;
  75          $this->context = $context;
  76          $this->othermodels = $othermodels;
  77          $this->page = $page;
  78          $this->perpage = $perpage;
  79      }
  80  
  81      /**
  82       * Exports the data.
  83       *
  84       * @param \renderer_base $output
  85       * @return \stdClass
  86       */
  87      public function export_for_template(\renderer_base $output) {
  88          global $PAGE;
  89  
  90          $target = $this->model->get_target();
  91  
  92          $data = new \stdClass();
  93          $data->modelid = $this->model->get_id();
  94          $data->contextid = $this->context->id;
  95  
  96          $targetname = $target->get_name();
  97          $data->insightname = format_string($targetname);
  98  
  99          $targetinfostr = $targetname->get_identifier() . 'info';
 100          if (get_string_manager()->string_exists($targetinfostr, $targetname->get_component())) {
 101              $data->insightdescription = get_string($targetinfostr, $targetname->get_component());
 102          }
 103  
 104          $data->showpredictionheading = true;
 105          if (!$target->is_linear()) {
 106              $nclasses = count($target::get_classes());
 107              $nignoredclasses = count($target->ignored_predicted_classes());
 108              if ($nclasses - $nignoredclasses <= 1) {
 109                  // Hide the prediction heading if there is only 1 class displayed. Otherwise it is redundant with the insight name.
 110                  $data->showpredictionheading = false;
 111              }
 112          }
 113  
 114          $total = 0;
 115  
 116          if ($this->model->uses_insights()) {
 117  
 118              $target->add_bulk_actions_js();
 119  
 120              $predictionsdata = $this->model->get_predictions($this->context, true, $this->page, $this->perpage);
 121  
 122              if (!$this->model->is_static()) {
 123                  $notification = new \core\output\notification(get_string('justpredictions', 'report_insights'));
 124                  $data->nostaticmodelnotification = $notification->export_for_template($output);
 125              }
 126  
 127              $data->predictions = array();
 128              $predictionvalues = array();
 129              $insights = array();
 130              if ($predictionsdata) {
 131                  list($total, $predictions) = $predictionsdata;
 132  
 133                  if ($predictions) {
 134                      // No bulk actions if no predictions.
 135                      $data->bulkactions = actions_exporter::add_bulk_actions($target, $output, $predictions, $this->context);
 136                  }
 137  
 138                  $data->multiplepredictions = count($predictions) > 1 ? true : false;
 139  
 140                  foreach ($predictions as $prediction) {
 141                      $predictedvalue = $prediction->get_prediction_data()->prediction;
 142  
 143                      // Only need to fill this data once.
 144                      if (!isset($predictionvalues[$predictedvalue])) {
 145                          $preddata = array();
 146                          $preddata['predictiondisplayvalue'] = $target->get_display_value($predictedvalue);
 147                          list($preddata['style'], $preddata['outcomeicon']) =
 148                              insight::get_calculation_display($target, floatval($predictedvalue), $output);
 149                          $predictionvalues[$predictedvalue] = $preddata;
 150                      }
 151  
 152                      $insightrenderable = new \report_insights\output\insight($prediction, $this->model, true, $this->context);
 153                      $insights[$predictedvalue][] = $insightrenderable->export_for_template($output);
 154                  }
 155  
 156                  // Order predicted values.
 157                  if ($target->is_linear()) {
 158                      // During regression what we will be interested on most of the time is in low values so let's show them first.
 159                      ksort($predictionvalues);
 160                  } else {
 161                      // During classification targets flag "not that important" samples as 0 so let's show them at the end.
 162                      krsort($predictionvalues);
 163                  }
 164  
 165                  // Ok, now we have all the data we want, put it into a format that mustache can handle.
 166                  foreach ($predictionvalues as $key => $prediction) {
 167                      if (isset($insights[$key])) {
 168  
 169                          $toggleall = new \core\output\checkbox_toggleall('insight-bulk-action-' . $key, true, [
 170                              'id' => 'id-toggle-all-' . $key,
 171                              'name' => 'toggle-all-' . $key,
 172                              'label' => get_string('selectall'),
 173                              'labelclasses' => 'sr-only',
 174                              'checked' => false
 175                          ]);
 176                          $prediction['checkboxtoggleall'] = $output->render($toggleall);
 177  
 178                          $prediction['predictedvalue'] = $key;
 179                          $prediction['insights'] = $insights[$key];
 180                      }
 181  
 182                      $data->predictions[] = $prediction;
 183                  }
 184              }
 185  
 186              if (empty($insights) && $this->page == 0) {
 187                  if ($this->model->any_prediction_obtained()) {
 188                      $data->noinsights = get_string('noinsights', 'analytics');
 189                  } else {
 190                      $data->noinsights = get_string('nopredictionsyet', 'analytics');
 191                  }
 192              }
 193          } else {
 194              $data->noinsights = get_string('noinsights', 'analytics');
 195          }
 196  
 197          if (!empty($data->noinsights)) {
 198              $notification = new \core\output\notification($data->noinsights);
 199              $data->noinsights = $notification->export_for_template($output);
 200          }
 201  
 202          $url = $PAGE->url;
 203  
 204          if ($this->othermodels) {
 205  
 206              $options = array();
 207              foreach ($this->othermodels as $model) {
 208                  $options[$model->get_id()] = $model->get_target()->get_name();
 209              }
 210  
 211              // New moodle_url instance returned by magic_get_url.
 212              $url->remove_params('modelid');
 213              $modelselector = new \single_select($url, 'modelid', $options, '',
 214                  array('' => get_string('selectotherinsights', 'report_insights')));
 215              $data->modelselector = $modelselector->export_for_template($output);
 216          }
 217  
 218          // Add the 'perpage' parameter to the url which is later used to generate the pagination links.
 219          $url->param('perpage', $this->perpage);
 220          $data->pagingbar = $output->render(new \paging_bar($total, $this->page, $this->perpage, $url));
 221  
 222          return $data;
 223      }
 224  }