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.
   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   * Renderer.
  19   *
  20   * @package    tool_analytics
  21   * @copyright  2016 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  use plugin_renderer_base;
  30  
  31  
  32  /**
  33   * Renderer class.
  34   *
  35   * @package    tool_analytics
  36   * @copyright  2016 David Monllao {@link http://www.davidmonllao.com}
  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       * Defer to template.
  43       *
  44       * @param \tool_analytics\output\models_list $modelslist
  45       * @return string HTML
  46       */
  47      protected function render_models_list(\tool_analytics\output\models_list $modelslist) {
  48          $data = $modelslist->export_for_template($this);
  49          return parent::render_from_template('tool_analytics/models_list', $data);
  50      }
  51  
  52      /**
  53       * Renders a table.
  54       *
  55       * @param \table_sql $table
  56       * @return string HTML
  57       */
  58      public function render_table(\table_sql $table) {
  59  
  60          ob_start();
  61          $table->out(10, true);
  62          $output = ob_get_contents();
  63          ob_end_clean();
  64  
  65          return $output;
  66      }
  67  
  68      /**
  69       * Web interface evaluate results.
  70       *
  71       * @param \stdClass[] $results
  72       * @param string[] $logs
  73       * @return string HTML
  74       */
  75      public function render_evaluate_results($results, $logs = array()) {
  76          $output = '';
  77  
  78          foreach ($results as $timesplittingid => $result) {
  79  
  80              if (!CLI_SCRIPT) {
  81                  $output .= $this->output->box_start('generalbox mb-3');
  82              }
  83  
  84              // Check that the array key is a string, not all results depend on time splitting methods (e.g. general errors).
  85              if (!is_numeric($timesplittingid)) {
  86                  $timesplitting = \core_analytics\manager::get_time_splitting($timesplittingid);
  87                  $langstrdata = (object)array('name' => $timesplitting->get_name(), 'id' => $timesplittingid);
  88  
  89                  if (CLI_SCRIPT) {
  90                      $output .= $this->output->heading(get_string('scheduledanalysisresultscli', 'tool_analytics', $langstrdata), 3);
  91                  } else {
  92                      $output .= $this->output->heading(get_string('scheduledanalysisresults', 'tool_analytics', $langstrdata), 3);
  93                  }
  94              }
  95  
  96              if ($result->status == 0) {
  97                  $output .= $this->output->notification(get_string('goodmodel', 'tool_analytics'),
  98                      \core\output\notification::NOTIFY_SUCCESS);
  99              } else if ($result->status === \core_analytics\model::NO_DATASET) {
 100                  $output .= $this->output->notification(get_string('nodatatoevaluate', 'tool_analytics'),
 101                      \core\output\notification::NOTIFY_WARNING);
 102              }
 103  
 104              if (isset($result->score)) {
 105                  // Score.
 106                  $output .= $this->output->heading(get_string('accuracy', 'tool_analytics') . ': ' .
 107                      round(floatval($result->score), 4) * 100  . '%', 4);
 108              }
 109  
 110              if (!empty($result->info)) {
 111                  foreach ($result->info as $message) {
 112                      $output .= $this->output->notification($message, \core\output\notification::NOTIFY_WARNING);
 113                  }
 114              }
 115  
 116              if (!CLI_SCRIPT) {
 117                  $output .= $this->output->box_end();
 118              }
 119          }
 120  
 121          // Info logged during evaluation.
 122          if (!empty($logs) && debugging()) {
 123              $output .= $this->output->heading(get_string('extrainfo', 'tool_analytics'), 3);
 124              foreach ($logs as $log) {
 125                  $output .= $this->output->notification($log, \core\output\notification::NOTIFY_WARNING);
 126              }
 127          }
 128  
 129          if (!CLI_SCRIPT) {
 130              $output .= $this->output->single_button(new \moodle_url('/admin/tool/analytics/index.php'),
 131                      get_string('continue'), 'get');
 132          }
 133  
 134          return $output;
 135      }
 136  
 137  
 138      /**
 139       * Web interface training & prediction results.
 140       *
 141       * @param \stdClass|false $trainresults
 142       * @param string[] $trainlogs
 143       * @param \stdClass|false $predictresults
 144       * @param string[] $predictlogs
 145       * @return string HTML
 146       */
 147      public function render_get_predictions_results($trainresults = false, $trainlogs = array(), $predictresults = false, $predictlogs = array()) {
 148          $output = '';
 149  
 150          if ($trainresults || (!empty($trainlogs) && debugging())) {
 151              $output .= $this->output->heading(get_string('trainingresults', 'tool_analytics'), 3);
 152          }
 153  
 154          if ($trainresults) {
 155              if ($trainresults->status == 0) {
 156                  $output .= $this->output->notification(
 157                          get_string('trainingprocessfinished', 'tool_analytics'),
 158                      \core\output\notification::NOTIFY_SUCCESS);
 159              } else if ($trainresults->status === \core_analytics\model::NO_DATASET ||
 160                      $trainresults->status === \core_analytics\model::NOT_ENOUGH_DATA) {
 161                  $output .= $this->output->notification(
 162                          get_string('nodatatotrain', 'tool_analytics'),
 163                      \core\output\notification::NOTIFY_WARNING);
 164              } else {
 165                  $output .= $this->output->notification(
 166                          get_string('generalerror', 'tool_analytics', $trainresults->status),
 167                      \core\output\notification::NOTIFY_ERROR);
 168              }
 169          }
 170  
 171          if (!empty($trainlogs) && debugging()) {
 172              $output .= $this->output->heading(get_string('extrainfo', 'tool_analytics'), 4);
 173              foreach ($trainlogs as $log) {
 174                  $output .= $this->output->notification($log, \core\output\notification::NOTIFY_WARNING);
 175              }
 176          }
 177  
 178          if ($predictresults || (!empty($predictlogs) && debugging())) {
 179              $output .= $this->output->heading(
 180                      get_string('predictionresults', 'tool_analytics'), 3, 'main mt-3');
 181          }
 182  
 183          if ($predictresults) {
 184              if ($predictresults->status == 0) {
 185                  $output .= $this->output->notification(
 186                          get_string('predictionprocessfinished', 'tool_analytics'),
 187                      \core\output\notification::NOTIFY_SUCCESS);
 188              } else if ($predictresults->status === \core_analytics\model::NO_DATASET ||
 189                      $predictresults->status === \core_analytics\model::NOT_ENOUGH_DATA) {
 190                  $output .= $this->output->notification(
 191                          get_string('nodatatopredict', 'tool_analytics'),
 192                      \core\output\notification::NOTIFY_WARNING);
 193              } else {
 194                  $output .= $this->output->notification(
 195                          get_string('generalerror', 'tool_analytics', $predictresults->status),
 196                      \core\output\notification::NOTIFY_ERROR);
 197              }
 198          }
 199  
 200          if (!empty($predictlogs) && debugging()) {
 201              $output .= $this->output->heading(get_string('extrainfo', 'tool_analytics'), 4);
 202              foreach ($predictlogs as $log) {
 203                  $output .= $this->output->notification($log, \core\output\notification::NOTIFY_WARNING);
 204              }
 205          }
 206  
 207          if (!CLI_SCRIPT) {
 208              $output .= $this->output->single_button(new \moodle_url('/admin/tool/analytics/index.php'),
 209                      get_string('continue'), 'get');
 210          }
 211  
 212          return $output;
 213      }
 214  
 215      /**
 216       * Defer to template.
 217       *
 218       * @param \tool_analytics\output\insights_report $insightsreport
 219       * @return string HTML
 220       */
 221      protected function render_insights_report(\tool_analytics\output\insights_report $insightsreport): string {
 222          $data = $insightsreport->export_for_template($this);
 223          return parent::render_from_template('tool_analytics/insights_report', $data);
 224      }
 225  
 226      /**
 227       * Defer to template.
 228       *
 229       * @param \tool_analytics\output\invalid_analysables $invalidanalysables
 230       * @return string HTML
 231       */
 232      protected function render_invalid_analysables(\tool_analytics\output\invalid_analysables $invalidanalysables) {
 233          $data = $invalidanalysables->export_for_template($this);
 234          return parent::render_from_template('tool_analytics/invalid_analysables', $data);
 235      }
 236  
 237      /**
 238       * Renders an analytics disabled notification.
 239       *
 240       * @return string HTML
 241       */
 242      public function render_analytics_disabled() {
 243          global $FULLME;
 244  
 245          $this->page->set_url($FULLME);
 246          $this->page->set_title(get_string('pluginname', 'tool_analytics'));
 247          $this->page->set_heading(get_string('pluginname', 'tool_analytics'));
 248  
 249          $output = $this->output->header();
 250          $output .= $this->output->notification(get_string('analyticsdisabled', 'analytics'),
 251                  \core\output\notification::NOTIFY_INFO);
 252          $output .= \html_writer::tag('a', get_string('continue'), ['class' => 'btn btn-primary',
 253              'href' => (new \moodle_url('/'))->out()]);
 254          $output .= $this->output->footer();
 255  
 256          return $output;
 257      }
 258  
 259  }