Differences Between: [Versions 310 and 403] [Versions 311 and 403] [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 use core\report_helper; 26 27 require_once(__DIR__ . '/../../config.php'); 28 require_once($CFG->libdir . '/adminlib.php'); 29 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); 34 35 if ($perpage > 1000) { 36 $perpage = 1000; 37 } 38 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); 44 } 45 46 if (!\core_analytics\manager::is_analytics_enabled()) { 47 $renderer = $PAGE->get_renderer('report_insights'); 48 echo $renderer->render_analytics_disabled(); 49 exit(0); 50 } 51 52 \core_analytics\manager::check_can_list_insights($context); 53 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) { 57 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; 64 } 65 } 66 return true; 67 }); 68 69 if (!$modelid && count($othermodels)) { 70 // Autoselect the only available model. 71 $model = reset($othermodels); 72 $modelid = $model->get_id(); 73 } 74 if ($modelid) { 75 unset($othermodels[$modelid]); 76 } 77 78 // The URL in navigation only contains the contextid. 79 $params = array('contextid' => $contextid); 80 $navurl = new \moodle_url('/report/insights/insights.php', $params); 81 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); 87 } 88 89 $PAGE->set_url($url); 90 $PAGE->set_pagelayout('report'); 91 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 100 )); 101 } 102 $PAGE->navigation->override_active_url($navurl); 103 104 $renderer = $PAGE->get_renderer('report_insights'); 105 106 // No models with insights available at this context level. 107 if (!$modelid) { 108 echo $renderer->render_no_insights($context); 109 exit(0); 110 } 111 112 $model = new \core_analytics\model($modelid); 113 114 if (!$model->get_target()->link_insights_report()) { 115 116 // Only manager access if this target does not link the insights report. 117 \core_analytics\manager::check_can_manage_models(); 118 } 119 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(); 125 126 if (!$model->is_enabled()) { 127 echo $renderer->render_model_disabled($insightinfo); 128 exit(0); 129 } 130 131 if (!$model->uses_insights()) { 132 echo $renderer->render_no_insights_model($context); 133 exit(0); 134 } 135 136 if ($context->id == SYSCONTEXTID) { 137 $PAGE->set_heading(get_site()->shortname); 138 } else { 139 $PAGE->set_heading($insightinfo->contextname); 140 } 141 $PAGE->set_title($insightinfo->insightname); 142 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()) { 145 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.'); 153 } 154 $prediction = reset($predictions); 155 $redirecturl = new \moodle_url('/report/insights/prediction.php', ['id' => $prediction->get_prediction_data()->id]); 156 redirect($redirecturl); 157 } 158 } 159 160 echo $OUTPUT->header(); 161 162 if ($course) { 163 // Print selected drop down. 164 $pluginname = get_string('pluginname', 'report_insights'); 165 report_helper::print_report_selector($pluginname); 166 } 167 168 $renderable = new \report_insights\output\insights_list($model, $context, $othermodels, $page, $perpage); 169 echo $renderer->render($renderable); 170 171 $eventdata = array ( 172 'context' => $context, 173 'other' => array('modelid' => $model->get_id()) 174 ); 175 \core\event\insights_viewed::create($eventdata)->trigger(); 176 177 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body