MDL-45774 navigation: Added general reports on a site level
[moodle.git] / report / log / lib.php
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/>.
17 /**
18  * Public API of the log report.
19  *
20  * Defines the APIs used by log reports
21  *
22  * @package    report_log
23  * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die;
29 /**
30  * This function extends the navigation with the report items
31  *
32  * @param navigation_node $navigation The navigation node to extend
33  * @param stdClass $course The course to object for the report
34  * @param stdClass $context The context of the course
35  */
36 function report_log_extend_navigation_course($navigation, $course, $context) {
37     if (has_capability('report/log:view', $context)) {
38         $url = new moodle_url('/report/log/index.php', array('id'=>$course->id));
39         $navigation->add(get_string('pluginname', 'report_log'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
40     }
41 }
43 /**
44  * Callback to verify if the given instance of store is supported by this report or not.
45  *
46  * @param string $instance store instance.
47  *
48  * @return bool returns true if the store is supported by the report, false otherwise.
49  */
50 function report_log_supports_logstore($instance) {
51     if ($instance instanceof \core\log\sql_reader) {
52         return true;
53     }
54     return false;
55 }
57 /**
58  * This function extends the course navigation with the report items
59  *
60  * @param navigation_node $navigation The navigation node to extend
61  * @param stdClass $user
62  * @param stdClass $course The course to object for the report
63  */
64 function report_log_extend_navigation_user($navigation, $user, $course) {
65     list($all, $today) = report_log_can_access_user_report($user, $course);
67     if ($today) {
68         $url = new moodle_url('/report/log/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>'today'));
69         $navigation->add(get_string('todaylogs'), $url);
70     }
71     if ($all) {
72         $url = new moodle_url('/report/log/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>'all'));
73         $navigation->add(get_string('alllogs'), $url);
74     }
75 }
77 /**
78  * Is current user allowed to access this report
79  *
80  * @access private defined in lib.php for performance reasons
81  * @global stdClass $USER
82  * @param stdClass $user
83  * @param stdClass $course
84  * @return array with two elements $all, $today
85  */
86 function report_log_can_access_user_report($user, $course) {
87     global $USER;
89     $coursecontext = context_course::instance($course->id);
90     $personalcontext = context_user::instance($user->id);
92     $today = false;
93     $all = false;
95     if (has_capability('report/log:view', $coursecontext)) {
96         $today = true;
97     }
98     if (has_capability('report/log:viewtoday', $coursecontext)) {
99         $all = true;
100     }
102     if ($today and $all) {
103         return array(true, true);
104     }
106     if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)) {
107         if ($course->showreports and (is_viewing($coursecontext, $user) or is_enrolled($coursecontext, $user))) {
108             return array(true, true);
109         }
111     } else if ($user->id == $USER->id) {
112         if ($course->showreports and (is_viewing($coursecontext, $USER) or is_enrolled($coursecontext, $USER))) {
113             return array(true, true);
114         }
115     }
117     return array($all, $today);
120 /**
121  * This function extends the module navigation with the report items
122  *
123  * @param navigation_node $navigation The navigation node to extend
124  * @param stdClass $cm
125  */
126 function report_log_extend_navigation_module($navigation, $cm) {
127     if (has_capability('report/log:view', context_course::instance($cm->course))) {
128         $url = new moodle_url('/report/log/index.php', array('chooselog'=>'1','id'=>$cm->course,'modid'=>$cm->id));
129         $navigation->add(get_string('logs'), $url, navigation_node::TYPE_SETTING, null, 'logreport');
130     }
133 /**
134  * Return a list of page types
135  *
136  * @param string $pagetype current page type
137  * @param stdClass $parentcontext Block's parent context
138  * @param stdClass $currentcontext Current context of block
139  * @return array a list of page types
140  */
141 function report_log_page_type_list($pagetype, $parentcontext, $currentcontext) {
142     $array = array(
143         '*'                => get_string('page-x', 'pagetype'),
144         'report-*'         => get_string('page-report-x', 'pagetype'),
145         'report-log-*'     => get_string('page-report-log-x',  'report_log'),
146         'report-log-index' => get_string('page-report-log-index',  'report_log'),
147         'report-log-user'  => get_string('page-report-log-user',  'report_log')
148     );
149     return $array;
152 /**
153  * Add nodes to myprofile page.
154  *
155  * @param \core_user\output\myprofile\tree $tree Tree object
156  * @param stdClass $user user object
157  * @param bool $iscurrentuser
158  * @param stdClass $course Course object
159  *
160  * @return bool
161  */
162 function report_log_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
163     if (empty($course)) {
164         // We want to display these reports under the site context.
165         $course = get_fast_modinfo(SITEID)->get_course();
166     }
167     list($all, $today) = report_log_can_access_user_report($user, $course);
168     if ($today) {
169         // Today's log.
170         $url = new moodle_url('/report/log/user.php',
171             array('id' => $user->id, 'course' => $course->id, 'mode' => 'today'));
172         $node = new core_user\output\myprofile\node('reports', 'todayslogs', get_string('todaylogs'), null, $url);
173         $tree->add_node($node);
174     }
176     if ($all) {
177         // All logs.
178         $url = new moodle_url('/report/log/user.php',
179             array('id' => $user->id, 'course' => $course->id, 'mode' => 'all'));
180         $node = new core_user\output\myprofile\node('reports', 'alllogs', get_string('alllogs'), null, $url);
181         $tree->add_node($node);
182     }
183     return true;