Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.

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