Merge branch 'wip-MDL-60241-34' of git://github.com/marinaglancy/moodle into MOODLE_3...
[moodle.git] / report / insights / lib.php
blobe63bc79768a14910549a55f04af47960b73f3768
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 * This page lists public api for tool_monitor plugin.
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 defined('MOODLE_INTERNAL') || die;
27 /**
28 * This function extends the navigation with the tool items
30 * @param navigation_node $navigation The navigation node to extend
31 * @param stdClass $course The course to object for the tool
32 * @param context $context The context of the course
33 * @return void
35 function report_insights_extend_navigation_course($navigation, $course, $context) {
37 if (has_capability('moodle/analytics:listinsights', $context)) {
39 $modelids = report_insights_context_insights($context);
40 if (!empty($modelids)) {
41 $url = new moodle_url('/report/insights/insights.php', array('contextid' => $context->id));
42 $node = navigation_node::create(get_string('insights', 'report_insights'), $url, navigation_node::TYPE_SETTING,
43 null, null, new pix_icon('i/report', get_string('insights', 'report_insights')));
44 $navigation->add_node($node);
49 /**
50 * Add nodes to myprofile page.
52 * @param \core_user\output\myprofile\tree $tree Tree object
53 * @param stdClass $user user object
54 * @param bool $iscurrentuser
55 * @param stdClass $course Course object
57 * @return bool
59 function report_insights_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
61 $context = \context_user::instance($user->id);
62 if (has_capability('moodle/analytics:listinsights', $context)) {
64 $modelids = report_insights_context_insights($context);
65 if (!empty($modelids)) {
66 $url = new moodle_url('/report/insights/insights.php', array('contextid' => $context->id));
67 $node = new core_user\output\myprofile\node('reports', 'insights', get_string('insights', 'report_insights'),
68 null, $url);
69 $tree->add_node($node);
74 /**
75 * Adds nodes to category navigation
77 * @param navigation_node $navigation The navigation node to extend
78 * @param context $context The context of the course
79 * @return void|null return null if we don't want to display the node.
81 function report_insights_extend_navigation_category_settings($navigation, $context) {
83 if (has_capability('moodle/analytics:listinsights', $context)) {
85 $modelids = report_insights_context_insights($context);
86 if (!empty($modelids)) {
87 $url = new moodle_url('/report/insights/insights.php', array('contextid' => $context->id));
89 $node = navigation_node::create(
90 get_string('insights', 'report_insights'),
91 $url,
92 navigation_node::NODETYPE_LEAF,
93 null,
94 'insights',
95 new pix_icon('i/report', get_string('insights', 'report_insights'))
98 $navigation->add_node($node);
104 * Returns the models that generated insights in the provided context.
106 * @param \context $context
107 * @return int[]
109 function report_insights_context_insights(\context $context) {
111 $cache = \cache::make('core', 'contextwithinsights');
112 $modelids = $cache->get($context->id);
113 if ($modelids === false) {
114 // They will be full unless a model has been cleared.
115 $models = \core_analytics\manager::get_models_with_insights($context);
116 $modelids = array_keys($models);
117 $cache->set($context->id, $modelids);
119 return $modelids;