Merge branch 'wip-MDL-59775-fix' of https://github.com/marinaglancy/moodle
[moodle.git] / report / insights / prediction.php
CommitLineData
373b1de1
DM
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/**
f9e7447f 18 * View an insight.
373b1de1
DM
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
6ec2ae0f 25require_once(__DIR__ . '/../../config.php');
1d5b1d01 26require_once($CFG->libdir . '/adminlib.php');
373b1de1
DM
27
28$predictionid = required_param('id', PARAM_INT);
29
1611308b
DM
30list($model, $prediction, $context) = \core_analytics\manager::get_prediction($predictionid, true);
31if ($context->contextlevel < CONTEXT_COURSE) {
32 // Only for higher levels than course.
373b1de1
DM
33 $PAGE->set_context($context);
34}
35
1611308b 36$params = array('id' => $prediction->get_prediction_data()->id);
6ec2ae0f 37$url = new \moodle_url('/report/insights/prediction.php', $params);
373b1de1
DM
38$PAGE->set_url($url);
39$PAGE->set_pagelayout('report');
40
1d5b1d01
DM
41$navurl = new \moodle_url('/report/insights/insights.php', array('contextid' => $context->id));
42if ($context->contextlevel === CONTEXT_SYSTEM) {
43 admin_externalpage_setup('reportinsights', '', null, '', array('pagelayout' => 'report'));
44} else if ($context->contextlevel === CONTEXT_USER) {
45 $user = \core_user::get_user($context->instanceid, '*', MUST_EXIST);
46 $PAGE->navigation->extend_for_user($user);
47
48 $modelinsightsurl = clone $navurl;
49 $modelinsightsurl->param('modelid', $model->get_id());
50 $PAGE->add_report_nodes($user->id, array(
51 'name' => get_string('insights', 'report_insights'),
52 'url' => $url
53 ));
54}
55$PAGE->navigation->override_active_url($navurl);
56
373b1de1
DM
57$renderer = $PAGE->get_renderer('report_insights');
58
373b1de1
DM
59$insightinfo = new stdClass();
60$insightinfo->contextname = $context->get_context_name();
61$insightinfo->insightname = $model->get_target()->get_name();
373b1de1 62
6ec2ae0f 63$modelready = $model->is_enabled() && $model->is_trained() && $model->predictions_exist($context);
3e0f33aa 64if (!$modelready) {
373b1de1
DM
65 echo $renderer->render_model_disabled($insightinfo);
66 exit(0);
67}
68
f9e7447f
DM
69if (!$model->uses_insights()) {
70 echo $renderer->render_no_insights_model($context);
71 exit(0);
72}
73
1d5b1d01
DM
74$PAGE->set_title($insightinfo->insightname);
75$PAGE->set_heading($insightinfo->contextname);
373b1de1
DM
76
77echo $OUTPUT->header();
78
1611308b 79$renderable = new \report_insights\output\insight($prediction, $model, false);
373b1de1
DM
80echo $renderer->render($renderable);
81
82echo $OUTPUT->footer();