MDL-58859 report_insights: Added to core
[moodle.git] / report / insights / lib.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/**
18 * This page lists public api for tool_monitor plugin.
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
25defined('MOODLE_INTERNAL') || die;
26
27/**
28 * This function extends the navigation with the tool items
29 *
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 */
34function report_insights_extend_navigation_course($navigation, $course, $context) {
35 global $DB;
36
37 if (has_capability('moodle/analytics:listinsights', $context)) {
38
39 $cache = \cache::make('core', 'modelswithpredictions');
40 $models = $cache->get($context->id);
41 if ($models === false) {
42 // Fill the cache.
43 $models = \core_analytics\manager::get_all_models(true, true, $context);
44 $modelids = array_keys($models);
45 $cache->set($context->id, $modelids);
46 }
47
48 if (!empty($modelids)) {
49 $url = new moodle_url('/report/insights/insights.php', array('contextid' => $context->id));
50 $settingsnode = navigation_node::create(get_string('insights', 'report_insights'), $url, navigation_node::TYPE_SETTING,
51 null, null, new pix_icon('i/settings', ''));
52 $reportnode = $navigation->get('coursereports');
53 if (isset($settingsnode) && !empty($reportnode)) {
54 $reportnode->add_node($settingsnode);
55 }
56 }
57 }
58}