MDL-80653 h5p: handle content type creation errors in test.
[moodle.git] / report / insights / insights.php
blob4dc839d467d07672a37514ea190be2efb2fad4c4
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 insights.
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 use core\report_helper;
27 require_once(__DIR__ . '/../../config.php');
28 require_once($CFG->libdir . '/adminlib.php');
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);
35 if ($perpage > 1000) {
36 $perpage = 1000;
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);
46 if (!\core_analytics\manager::is_analytics_enabled()) {
47 $renderer = $PAGE->get_renderer('report_insights');
48 echo $renderer->render_analytics_disabled();
49 exit(0);
52 \core_analytics\manager::check_can_list_insights($context);
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) {
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;
66 return true;
67 });
69 if (!$modelid && count($othermodels)) {
70 // Autoselect the only available model.
71 $model = reset($othermodels);
72 $modelid = $model->get_id();
74 if ($modelid) {
75 unset($othermodels[$modelid]);
78 // The URL in navigation only contains the contextid.
79 $params = array('contextid' => $contextid);
80 $navurl = new \moodle_url('/report/insights/insights.php', $params);
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);
89 $PAGE->set_url($url);
90 $PAGE->set_pagelayout('report');
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
102 $PAGE->navigation->override_active_url($navurl);
104 $renderer = $PAGE->get_renderer('report_insights');
106 // No models with insights available at this context level.
107 if (!$modelid) {
108 echo $renderer->render_no_insights($context);
109 exit(0);
112 $model = new \core_analytics\model($modelid);
114 if (!$model->get_target()->link_insights_report()) {
116 // Only manager access if this target does not link the insights report.
117 \core_analytics\manager::check_can_manage_models();
120 $insightinfo = new stdClass();
121 // Don't show prefix for course-level context.
122 $withprefix = $context->contextlevel <> CONTEXT_COURSE;
123 $insightinfo->contextname = $context->get_context_name($withprefix);
124 $insightinfo->insightname = $model->get_target()->get_name();
126 if (!$model->is_enabled()) {
127 echo $renderer->render_model_disabled($insightinfo);
128 exit(0);
131 if (!$model->uses_insights()) {
132 echo $renderer->render_no_insights_model($context);
133 exit(0);
136 if ($context->id == SYSCONTEXTID) {
137 $PAGE->set_heading(get_site()->shortname);
138 } else {
139 $PAGE->set_heading($insightinfo->contextname);
141 $PAGE->set_title($insightinfo->insightname);
143 // Some models generate one single prediction per context. We can directly show the prediction details in this case.
144 if ($model->get_analyser()::one_sample_per_analysable()) {
146 // Param $perpage to 2 so we can detect if this model's analyser is using one_sample_per_analysable incorrectly.
147 $predictionsdata = $model->get_predictions($context, true, 0, 2);
148 if ($predictionsdata) {
149 list($total, $predictions) = $predictionsdata;
150 if ($total > 1) {
151 throw new \coding_exception('This model\'s analyser processed more than one sample for a single analysable element.' .
152 'Therefore, the analyser\'s one_sample_per_analysable() method should return false.');
154 $prediction = reset($predictions);
155 $redirecturl = new \moodle_url('/report/insights/prediction.php', ['id' => $prediction->get_prediction_data()->id]);
156 redirect($redirecturl);
160 echo $OUTPUT->header();
162 if ($course) {
163 // Print selected drop down.
164 $pluginname = get_string('pluginname', 'report_insights');
165 report_helper::print_report_selector($pluginname);
168 $renderable = new \report_insights\output\insights_list($model, $context, $othermodels, $page, $perpage);
169 echo $renderer->render($renderable);
171 $eventdata = array (
172 'context' => $context,
173 'other' => array('modelid' => $model->get_id())
175 \core\event\insights_viewed::create($eventdata)->trigger();
177 echo $OUTPUT->footer();