MDL-61804 admin: Add setting for course visibility sorting
[moodle.git] / admin / tool / monitor / lib.php
CommitLineData
3883eee0
AA
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 tool_monitor
21 * @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.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 tool_monitor_extend_navigation_course($navigation, $course, $context) {
5ff86736 35 if (has_capability('tool/monitor:managerules', $context) && get_config('tool_monitor', 'enablemonitor')) {
3883eee0
AA
36 $url = new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $course->id));
37 $settingsnode = navigation_node::create(get_string('managerules', 'tool_monitor'), $url, navigation_node::TYPE_SETTING,
067e9599
AA
38 null, null, new pix_icon('i/settings', ''));
39 $reportnode = $navigation->get('coursereports');
40
41 if (isset($settingsnode) && !empty($reportnode)) {
42 $reportnode->add_node($settingsnode);
43 }
3883eee0 44 }
067e9599 45}
3883eee0 46
9aa9a1c7
JO
47/**
48 * This function extends the navigation with the tool items
49 *
50 * @param navigation_node $navigation The navigation node to extend
51 * @param stdClass $course The course to object for the tool
52 * @param context $context The context of the course
53 */
54function tool_monitor_extend_navigation_frontpage($navigation, $course, $context) {
55
56 if (has_capability('tool/monitor:managerules', $context)) {
57 $url = new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $course->id));
58 $settingsnode = navigation_node::create(get_string('managerules', 'tool_monitor'), $url, navigation_node::TYPE_SETTING,
59 null, null, new pix_icon('i/settings', ''));
60 $reportnode = $navigation->get('frontpagereports');
61
62 if (isset($settingsnode) && !empty($reportnode)) {
63 $reportnode->add_node($settingsnode);
64 }
65 }
66}
67
067e9599
AA
68/**
69 * This function extends the navigation with the tool items for user settings node.
70 *
71 * @param navigation_node $navigation The navigation node to extend
72 * @param stdClass $user The user object
73 * @param context $usercontext The context of the user
74 * @param stdClass $course The course to object for the tool
75 * @param context $coursecontext The context of the course
76 */
77function tool_monitor_extend_navigation_user_settings($navigation, $user, $usercontext, $course, $coursecontext) {
7c8afa8f
AG
78 global $USER, $PAGE;
79
80 // Don't bother doing needless calculations unless we are on the relevant pages.
81 $onpreferencepage = $PAGE->url->compare(new moodle_url('/user/preferences.php'), URL_MATCH_BASE);
82 $onmonitorpage = $PAGE->url->compare(new moodle_url('/admin/tool/monitor/index.php'), URL_MATCH_BASE);
83 if (!$onpreferencepage && !$onmonitorpage) {
84 return null;
85 }
3883eee0 86
6e918b64
AG
87 // Don't show the setting if the event monitor isn't turned on. No access to other peoples subscriptions.
88 if (get_config('tool_monitor', 'enablemonitor') && $USER->id == $user->id) {
0f510be7 89 // Now let's check to see if the user has any courses / site rules that they can subscribe to.
7c8afa8f
AG
90 // We skip doing a check here if we are on the event monitor page as the check is done internally on that page.
91 if ($onmonitorpage || tool_monitor_can_subscribe()) {
0f510be7
AG
92 $url = new moodle_url('/admin/tool/monitor/index.php');
93 $subsnode = navigation_node::create(get_string('managesubscriptions', 'tool_monitor'), $url,
94 navigation_node::TYPE_SETTING, null, 'monitor', new pix_icon('i/settings', ''));
25c634df 95
0f510be7
AG
96 if (isset($subsnode) && !empty($navigation)) {
97 $navigation->add_node($subsnode);
98 }
f3a493de 99 }
3883eee0 100 }
b0001c15 101}
0f510be7 102
7c8afa8f
AG
103/**
104 * Check if the user has the capacity to subscribe to an event monitor anywhere.
105 *
106 * @return bool True if a capability in a course is found. False otherwise.
107 */
108function tool_monitor_can_subscribe() {
109 if (has_capability('tool/monitor:subscribe', context_system::instance())) {
110 return true;
111 }
112 $courses = get_user_capability_course('tool/monitor:subscribe', null, true, '', '', 1);
113 return empty($courses) ? false : true;
114}
115
0f510be7
AG
116/**
117 * Get a list of courses and also include 'Site' for site wide rules.
118 *
119 * @return array|bool Returns an array of courses or false if the user has no permission to subscribe to rules.
120 */
121function tool_monitor_get_user_courses() {
8471a0db
AB
122 // Get the course sorting according to the admin settings.
123 $sort = enrol_get_courses_sortingsql();
124
0f510be7
AG
125 $options = array();
126 if (has_capability('tool/monitor:subscribe', context_system::instance())) {
127 $options[0] = get_string('site');
128 }
7c8afa8f 129
bfbd7334
AN
130 $fieldlist = array_merge(
131 [
132 'fullname',
133 'visible',
134 ],
135 array_values(context_helper::get_preload_record_columns('c'))
136 );
137
138 $fields = implode(', ', $fieldlist);
8471a0db 139 if ($courses = get_user_capability_course('tool/monitor:subscribe', null, true, $fields, $sort)) {
0f510be7 140 foreach ($courses as $course) {
7c8afa8f 141 context_helper::preload_from_record($course);
fac004e2
EL
142 $coursectx = context_course::instance($course->id);
143 if ($course->visible || has_capability('moodle/course:viewhiddencourses', $coursectx)) {
144 $options[$course->id] = format_string($course->fullname, true, array('context' => $coursectx));
b324802f 145 }
0f510be7
AG
146 }
147 }
148 // If there are no courses and there is no site permission then return false.
149 if (count($options) < 1) {
150 return false;
151 } else {
152 return $options;
153 }
154}