Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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   * View an insight.
  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  $predictionid = required_param('id', PARAM_INT);
  29  
  30  if (!\core_analytics\manager::is_analytics_enabled()) {
  31      $PAGE->set_context(\context_system::instance());
  32      $renderer = $PAGE->get_renderer('report_insights');
  33      echo $renderer->render_analytics_disabled();
  34      exit(0);
  35  }
  36  
  37  list($model, $prediction, $context) = \core_analytics\manager::get_prediction($predictionid, true);
  38  if ($context->contextlevel < CONTEXT_COURSE) {
  39      // Only for higher levels than course.
  40      $PAGE->set_context($context);
  41  }
  42  
  43  $params = array('id' => $prediction->get_prediction_data()->id);
  44  $url = new \moodle_url('/report/insights/prediction.php', $params);
  45  $PAGE->set_url($url);
  46  $PAGE->set_pagelayout('report');
  47  
  48  $navurl = new \moodle_url('/report/insights/insights.php', array('contextid' => $context->id));
  49  if ($context->contextlevel === CONTEXT_SYSTEM) {
  50      admin_externalpage_setup('reportinsights', '', null, '', array('pagelayout' => 'report'));
  51  } else if ($context->contextlevel === CONTEXT_USER) {
  52      $user = \core_user::get_user($context->instanceid, '*', MUST_EXIST);
  53      $PAGE->navigation->extend_for_user($user);
  54  
  55      $modelinsightsurl = clone $navurl;
  56      $modelinsightsurl->param('modelid', $model->get_id());
  57      $PAGE->add_report_nodes($user->id, array(
  58          'name' => get_string('insights', 'report_insights'),
  59          'url' => $url
  60      ));
  61  }
  62  $PAGE->navigation->override_active_url($navurl);
  63  
  64  $renderer = $PAGE->get_renderer('report_insights');
  65  
  66  $insightinfo = new stdClass();
  67  $insightinfo->contextname = $context->get_context_name();
  68  $insightinfo->insightname = $model->get_target()->get_name();
  69  
  70  $modelready = $model->is_enabled() && $model->is_trained() && $model->predictions_exist($context);
  71  if (!$modelready) {
  72      echo $renderer->render_model_disabled($insightinfo);
  73      exit(0);
  74  }
  75  
  76  if (!$model->uses_insights()) {
  77      echo $renderer->render_no_insights_model($context);
  78      exit(0);
  79  }
  80  
  81  if ($context->id == SYSCONTEXTID) {
  82      $PAGE->set_heading(get_site()->shortname);
  83  } else {
  84      $PAGE->set_heading($insightinfo->contextname);
  85  }
  86  $PAGE->set_title($insightinfo->insightname);
  87  
  88  echo $OUTPUT->header();
  89  
  90  $renderable = new \report_insights\output\insight($prediction, $model, false, $context);
  91  echo $renderer->render($renderable);
  92  
  93  echo $OUTPUT->footer();