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 file contains public API of outline report
19 *
20 * @package    report
21 * @subpackage outline
22 * @copyright  2009 Sam Hemelryk
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die;
27
28/**
29 * This function extends the course navigation with the report items
30 *
31 * @param navigation_node $navigation The navigation node to extend
32 * @param stdClass $course The course to object for the report
33 * @param stdClass $context The context of the course
34 */
35function report_outline_extend_navigation_course($navigation, $course, $context) {
36    if (has_capability('report/outline:view', $context)) {
37        $url = new moodle_url('/report/outline/index.php', array('id'=>$course->id));
38        $navigation->add(get_string('pluginname', 'report_outline'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
39    }
40}
41
42/**
43 * This function extends the course navigation with the report items
44 *
45 * @param navigation_node $navigation The navigation node to extend
46 * @param stdClass $user
47 * @param stdClass $course The course to object for the report
48 */
49function report_outline_extend_navigation_user($navigation, $user, $course) {
50    if (report_outline_can_access_user_report($user, $course)) {
51        $url = new moodle_url('/report/outline/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>'outline'));
52        $navigation->add(get_string('outlinereport'), $url);
53        $url = new moodle_url('/report/outline/user.php', array('id'=>$user->id, 'course'=>$course->id, 'mode'=>'complete'));
54        $navigation->add(get_string('completereport'), $url);
55    }
56}
57
58/**
59 * Is current user allowed to access this report
60 *
61 * @private defined in lib.php for performance reasons
62 *
63 * @param stdClass $user
64 * @param stdClass $course
65 * @return bool
66 */
67function report_outline_can_access_user_report($user, $course) {
68    global $USER;
69
70    $coursecontext = context_course::instance($course->id);
71    $personalcontext = context_user::instance($user->id);
72
73    if ($user->id == $USER->id) {
74        if ($course->showreports and (is_viewing($coursecontext, $USER) or is_enrolled($coursecontext, $USER))) {
75            return true;
76        }
77    } else if (has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)) {
78        if ($course->showreports and (is_viewing($coursecontext, $user) or is_enrolled($coursecontext, $user))) {
79            return true;
80        }
81
82    }
83
84    // Check if $USER shares group with $user (in case separated groups are enabled and 'moodle/site:accessallgroups' is disabled).
85    if (!groups_user_groups_visible($course, $user->id)) {
86        return false;
87    }
88
89    if (has_capability('report/outline:viewuserreport', $coursecontext)) {
90        return true;
91    }
92
93    return false;
94}
95
96/**
97 * Return a list of page types
98 * @param string $pagetype current page type
99 * @param stdClass $parentcontext Block's parent context
100 * @param stdClass $currentcontext Current context of block
101 * @return array
102 */
103function report_outline_page_type_list($pagetype, $parentcontext, $currentcontext) {
104    $array = array(
105        '*'                    => get_string('page-x', 'pagetype'),
106        'report-*'             => get_string('page-report-x', 'pagetype'),
107        'report-outline-*'     => get_string('page-report-outline-x',  'report_outline'),
108        'report-outline-index' => get_string('page-report-outline-index',  'report_outline'),
109        'report-outline-user'  => get_string('page-report-outline-user',  'report_outline')
110    );
111    return $array;
112}
113
114/**
115 * Callback to verify if the given instance of store is supported by this report or not.
116 *
117 * @param string $instance store instance.
118 *
119 * @return bool returns true if the store is supported by the report, false otherwise.
120 */
121function report_outline_supports_logstore($instance) {
122    if ($instance instanceof \core\log\sql_internal_table_reader || $instance instanceof \logstore_legacy\log\store) {
123        return true;
124    }
125    return false;
126}
127
128/**
129 * Add nodes to myprofile page.
130 *
131 * @param \core_user\output\myprofile\tree $tree Tree object
132 * @param stdClass $user user object
133 * @param bool $iscurrentuser
134 * @param stdClass $course Course object
135 *
136 * @return bool
137 */
138function report_outline_myprofile_navigation(core_user\output\myprofile\tree $tree, $user, $iscurrentuser, $course) {
139    if (empty($course)) {
140        // We want to display these reports under the site context.
141        $course = get_fast_modinfo(SITEID)->get_course();
142    }
143    if (report_outline_can_access_user_report($user, $course)) {
144        $url = new moodle_url('/report/outline/user.php',
145                array('id' => $user->id, 'course' => $course->id, 'mode' => 'outline'));
146        $node = new core_user\output\myprofile\node('reports', 'outline', get_string('outlinereport'), null, $url);
147        $tree->add_node($node);
148        $url = new moodle_url('/report/outline/user.php',
149            array('id' => $user->id, 'course' => $course->id, 'mode' => 'complete'));
150        $node = new core_user\output\myprofile\node('reports', 'complete', get_string('completereport'), null, $url);
151        $tree->add_node($node);
152    }
153}
154