MDL-58859 report_insights: Added to core
[moodle.git] / report / insights / insights.php
blob085fb1bb0cf698702257889f7ebb3268be4b8a73
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.
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/>.
17 /**
18 * View model predictions.
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
25 require_once(__DIR__ . '/../../../config.php');
27 $contextid = required_param('contextid', PARAM_INT);
28 $modelid = optional_param('modelid', false, PARAM_INT);
30 $context = context::instance_by_id($contextid);
32 if ($context->contextlevel === CONTEXT_MODULE) {
33 list($course, $cm) = get_module_from_cmid($context->instanceid);
34 require_login($course, true, $cm);
35 } else if ($context->contextlevel >= CONTEXT_COURSE) {
36 $coursecontext = $context->get_course_context(true);
37 require_login($coursecontext->instanceid);
38 } else {
39 require_login();
40 $PAGE->set_context($context);
43 require_capability('moodle/analytics:listinsights', $context);
45 // Get all models that are enabled, trained and have predictions at this context.
46 $othermodels = \core_analytics\manager::get_all_models(true, true, $context);
47 if (!$modelid && count($othermodels)) {
48 // Autoselect the only available model.
49 $model = reset($othermodels);
50 $modelid = $model->get_id();
52 if ($modelid) {
53 unset($othermodels[$modelid]);
56 $params = array('contextid' => $contextid);
57 $url = new \moodle_url('/report/insights/insights.php', $params);
58 if ($modelid) {
59 $url->param('modelid', $modelid);
62 $PAGE->set_url($url);
63 $PAGE->set_pagelayout('report');
65 $renderer = $PAGE->get_renderer('report_insights');
67 // No models with predictions available at this context level.
68 if (!$modelid) {
69 echo $renderer->render_no_predictions($context);
70 exit(0);
73 $model = new \core_analytics\model($modelid);
75 $insightinfo = new stdClass();
76 $insightinfo->contextname = $context->get_context_name();
77 $insightinfo->insightname = $model->get_target()->get_name();
78 $title = get_string('insightinfo', 'report_insights', $insightinfo);
81 if (!$model->is_enabled() && !has_capability('moodle/analytics:managemodels', $context)) {
82 echo $renderer->render_model_disabled($insightinfo);
83 exit(0);
86 $PAGE->set_title($title);
87 $PAGE->set_heading($title);
89 echo $OUTPUT->header();
91 $renderable = new \core_analytics\output\predictions_list($model, $context, $othermodels);
92 echo $renderer->render($renderable);
94 echo $OUTPUT->footer();