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 * Displays different views of the logs.
19 *
20 * @package    report_log
21 * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25use core\report_helper;
26
27require('../../config.php');
28require_once($CFG->dirroot.'/course/lib.php');
29require_once($CFG->dirroot.'/report/log/locallib.php');
30require_once($CFG->libdir.'/adminlib.php');
31require_once($CFG->dirroot.'/lib/tablelib.php');
32
33$id          = optional_param('id', 0, PARAM_INT);// Course ID.
34$group       = optional_param('group', 0, PARAM_INT); // Group to display.
35$user        = optional_param('user', 0, PARAM_INT); // User to display.
36$date        = optional_param('date', 0, PARAM_INT); // Date to display.
37$modid       = optional_param('modid', 0, PARAM_ALPHANUMEXT); // Module id or 'site_errors'.
38$modaction   = optional_param('modaction', '', PARAM_ALPHAEXT); // An action as recorded in the logs.
39$page        = optional_param('page', '0', PARAM_INT);     // Which page to show.
40$perpage     = optional_param('perpage', '100', PARAM_INT); // How many per page.
41$showcourses = optional_param('showcourses', false, PARAM_BOOL); // Whether to show courses if we're over our limit.
42$showusers   = optional_param('showusers', false, PARAM_BOOL); // Whether to show users if we're over our limit.
43$chooselog   = optional_param('chooselog', false, PARAM_BOOL);
44$logformat   = optional_param('download', '', PARAM_ALPHA);
45$logreader      = optional_param('logreader', '', PARAM_COMPONENT); // Reader which will be used for displaying logs.
46$edulevel    = optional_param('edulevel', -1, PARAM_INT); // Educational level.
47$origin      = optional_param('origin', '', PARAM_TEXT); // Event origin.
48
49$params = array();
50if (!empty($id)) {
51    $params['id'] = $id;
52} else {
53    $site = get_site();
54    $id = $site->id;
55}
56if ($group !== 0) {
57    $params['group'] = $group;
58}
59if ($user !== 0) {
60    $params['user'] = $user;
61}
62if ($date !== 0) {
63    $params['date'] = $date;
64}
65if ($modid !== 0) {
66    $params['modid'] = $modid;
67}
68if ($modaction !== '') {
69    $params['modaction'] = $modaction;
70}
71if ($page !== '0') {
72    $params['page'] = $page;
73}
74if ($perpage !== '100') {
75    $params['perpage'] = $perpage;
76}
77if ($showcourses) {
78    $params['showcourses'] = $showcourses;
79}
80if ($showusers) {
81    $params['showusers'] = $showusers;
82}
83if ($chooselog) {
84    $params['chooselog'] = $chooselog;
85}
86if ($logformat !== '') {
87    $params['download'] = $logformat;
88}
89if ($logreader !== '') {
90    $params['logreader'] = $logreader;
91}
92if (($edulevel != -1)) {
93    $params['edulevel'] = $edulevel;
94}
95if ($origin !== '') {
96    $params['origin'] = $origin;
97}
98// Legacy store hack, as edulevel is not supported.
99if ($logreader == 'logstore_legacy') {
100    $params['edulevel'] = -1;
101    $edulevel = -1;
102}
103$url = new moodle_url("/report/log/index.php", $params);
104
105$PAGE->set_url('/report/log/index.php', array('id' => $id));
106$PAGE->set_pagelayout('report');
107
108report_helper::save_selected_report($id, $url);
109
110// Get course details.
111$course = null;
112if ($id) {
113    $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
114    require_login($course);
115    $context = context_course::instance($course->id);
116} else {
117    require_login();
118    $context = context_system::instance();
119    $PAGE->set_context($context);
120}
121
122require_capability('report/log:view', $context);
123
124// When user choose to view logs then only trigger event.
125if ($chooselog) {
126    // Trigger a report viewed event.
127    $event = \report_log\event\report_viewed::create(array('context' => $context, 'relateduserid' => $user,
128            'other' => array('groupid' => $group, 'date' => $date, 'modid' => $modid, 'modaction' => $modaction,
129            'logformat' => $logformat)));
130    $event->trigger();
131}
132
133if (!empty($page)) {
134    $strlogs = get_string('logs'). ": ". get_string('page', 'report_log', $page + 1);
135} else {
136    $strlogs = get_string('logs');
137}
138$stradministration = get_string('administration');
139$strreports = get_string('reports');
140
141// Before we close session, make sure we have editing information in session.
142$adminediting = optional_param('adminedit', -1, PARAM_BOOL);
143if ($PAGE->user_allowed_editing() && $adminediting != -1) {
144    $USER->editing = $adminediting;
145}
146
147if (empty($course) || ($course->id == $SITE->id)) {
148    admin_externalpage_setup('reportlog', '', null, '', array('pagelayout' => 'report'));
149    $PAGE->set_title($SITE->shortname .': '. $strlogs);
150} else {
151    $PAGE->set_title($course->shortname .': '. $strlogs);
152    $PAGE->set_heading($course->fullname);
153}
154
155$reportlog = new report_log_renderable($logreader, $course, $user, $modid, $modaction, $group, $edulevel, $showcourses, $showusers,
156        $chooselog, true, $url, $date, $logformat, $page, $perpage, 'timecreated DESC', $origin);
157$readers = $reportlog->get_readers();
158$output = $PAGE->get_renderer('report_log');
159
160if (empty($readers)) {
161    echo $output->header();
162    echo $output->heading(get_string('nologreaderenabled', 'report_log'));
163} else {
164    if (!empty($chooselog)) {
165        // Delay creation of table, till called by user with filter.
166        $reportlog->setup_table();
167
168        if (empty($logformat)) {
169            echo $output->header();
170            // Print selector dropdown.
171            $pluginname = get_string('pluginname', 'report_log');
172            report_helper::print_report_selector($pluginname);
173            $userinfo = get_string('allparticipants');
174            $dateinfo = get_string('alldays');
175
176            if ($user) {
177                $u = $DB->get_record('user', array('id' => $user, 'deleted' => 0), '*', MUST_EXIST);
178                $userinfo = fullname($u, has_capability('moodle/site:viewfullnames', $context));
179            }
180            if ($date) {
181                $dateinfo = userdate($date, get_string('strftimedaydate'));
182            }
183            if (!empty($course) && ($course->id != SITEID)) {
184                $PAGE->navbar->add("$userinfo, $dateinfo");
185            }
186            echo $output->render($reportlog);
187        } else {
188            \core\session\manager::write_close();
189            $reportlog->download();
190            exit();
191        }
192    } else {
193        echo $output->header();
194        // Print selector dropdown.
195        $pluginname = get_string('pluginname', 'report_log');
196        report_helper::print_report_selector($pluginname);
197        echo $output->heading(get_string('chooselogs') .':');
198        echo $output->render($reportlog);
199    }
200}
201
202echo $output->footer();
203