Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * View model insights.
  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  use core\report_helper;
  26  
  27  require_once(__DIR__ . '/../../config.php');
  28  require_once($CFG->libdir . '/adminlib.php');
  29  
  30  $contextid = required_param('contextid', PARAM_INT);
  31  $modelid = optional_param('modelid', false, PARAM_INT);
  32  $page = optional_param('page', 0, PARAM_INT);
  33  $perpage = optional_param('perpage', 100, PARAM_INT);
  34  
  35  if ($perpage > 1000) {
  36      $perpage = 1000;
  37  }
  38  
  39  list($context, $course, $cm) = get_context_info_array($contextid);
  40  require_login($course, false, $cm);
  41  if ($context->contextlevel < CONTEXT_COURSE) {
  42      // Only for higher levels than course.
  43      $PAGE->set_context($context);
  44  }
  45  
  46  if (!\core_analytics\manager::is_analytics_enabled()) {
  47      $renderer = $PAGE->get_renderer('report_insights');
  48      echo $renderer->render_analytics_disabled();
  49      exit(0);
  50  }
  51  
  52  \core_analytics\manager::check_can_list_insights($context);
  53  
  54  // Get all models that are enabled, trained and have predictions at this context.
  55  $othermodels = \core_analytics\manager::get_all_models(true, true, $context);
  56  array_filter($othermodels, function($model) use ($context) {
  57  
  58      // Discard insights that are not linked unless you are a manager.
  59      if (!$model->get_target()->link_insights_report()) {
  60          try {
  61              \core_analytics\manager::check_can_manage_models();
  62          } catch (\required_capability_exception $e) {
  63              return false;
  64          }
  65      }
  66      return true;
  67  });
  68  
  69  if (!$modelid && count($othermodels)) {
  70      // Autoselect the only available model.
  71      $model = reset($othermodels);
  72      $modelid = $model->get_id();
  73  }
  74  if ($modelid) {
  75      unset($othermodels[$modelid]);
  76  }
  77  
  78  // The URL in navigation only contains the contextid.
  79  $params = array('contextid' => $contextid);
  80  $navurl = new \moodle_url('/report/insights/insights.php', $params);
  81  
  82  // This is the real page url, we need it to include the modelid so pagination and
  83  // other stuff works as expected.
  84  $url = clone $navurl;
  85  if ($modelid) {
  86      $url->param('modelid', $modelid);
  87  }
  88  
  89  $PAGE->set_url($url);
  90  $PAGE->set_pagelayout('report');
  91  
  92  if ($context->contextlevel === CONTEXT_SYSTEM) {
  93      admin_externalpage_setup('reportinsights', '', $url->params(), $url->out(false), array('pagelayout' => 'report'));
  94  } else if ($context->contextlevel === CONTEXT_USER) {
  95      $user = \core_user::get_user($context->instanceid, '*', MUST_EXIST);
  96      $PAGE->navigation->extend_for_user($user);
  97      $PAGE->add_report_nodes($user->id, array(
  98          'name' => get_string('insights', 'report_insights'),
  99          'url' => $url
 100      ));
 101  }
 102  $PAGE->navigation->override_active_url($navurl);
 103  
 104  $renderer = $PAGE->get_renderer('report_insights');
 105  
 106  // No models with insights available at this context level.
 107  if (!$modelid) {
 108      echo $renderer->render_no_insights($context);
 109      exit(0);
 110  }
 111  
 112  $model = new \core_analytics\model($modelid);
 113  
 114  if (!$model->get_target()->link_insights_report()) {
 115  
 116      // Only manager access if this target does not link the insights report.
 117      \core_analytics\manager::check_can_manage_models();
 118  }
 119  
 120  $insightinfo = new stdClass();
 121  $insightinfo->contextname = $context->get_context_name();
 122  $insightinfo->insightname = $model->get_target()->get_name();
 123  
 124  if (!$model->is_enabled()) {
 125      echo $renderer->render_model_disabled($insightinfo);
 126      exit(0);
 127  }
 128  
 129  if (!$model->uses_insights()) {
 130      echo $renderer->render_no_insights_model($context);
 131      exit(0);
 132  }
 133  
 134  if ($context->id == SYSCONTEXTID) {
 135      $PAGE->set_heading(get_site()->shortname);
 136  } else {
 137      $PAGE->set_heading($insightinfo->contextname);
 138  }
 139  $PAGE->set_title($insightinfo->insightname);
 140  
 141  // Some models generate one single prediction per context. We can directly show the prediction details in this case.
 142  if ($model->get_analyser()::one_sample_per_analysable()) {
 143  
 144      // Param $perpage to 2 so we can detect if this model's analyser is using one_sample_per_analysable incorrectly.
 145      $predictionsdata = $model->get_predictions($context, true, 0, 2);
 146      if ($predictionsdata) {
 147          list($total, $predictions) = $predictionsdata;
 148          if ($total > 1) {
 149              throw new \coding_exception('This model\'s analyser processed more than one sample for a single analysable element.' .
 150                  'Therefore, the analyser\'s one_sample_per_analysable() method should return false.');
 151          }
 152          $prediction = reset($predictions);
 153          $redirecturl = new \moodle_url('/report/insights/prediction.php', ['id' => $prediction->get_prediction_data()->id]);
 154          redirect($redirecturl);
 155      }
 156  }
 157  
 158  echo $OUTPUT->header();
 159  
 160  if ($course) {
 161      report_helper::save_selected_report($course->id, $url);
 162      // Print selected drop down.
 163      $pluginname = get_string('pluginname', 'report_insights');
 164      report_helper::print_report_selector($pluginname);
 165  }
 166  
 167  $renderable = new \report_insights\output\insights_list($model, $context, $othermodels, $page, $perpage);
 168  echo $renderer->render($renderable);
 169  
 170  $eventdata = array (
 171      'context' => $context,
 172      'other' => array('modelid' => $model->get_id())
 173  );
 174  \core\event\insights_viewed::create($eventdata)->trigger();
 175  
 176  echo $OUTPUT->footer();