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 * stats report
19 *
20 * @package    report
21 * @subpackage stats
22 * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26use core\report_helper;
27
28require_once('../../config.php');
29require_once($CFG->dirroot.'/report/stats/locallib.php');
30require_once($CFG->libdir.'/adminlib.php');
31
32$courseid = optional_param('course', SITEID, PARAM_INT);
33$report   = optional_param('report', 0, PARAM_INT);
34$time     = optional_param('time', 0, PARAM_INT);
35$mode     = optional_param('mode', STATS_MODE_GENERAL, PARAM_INT);
36$userid   = optional_param('userid', 0, PARAM_INT);
37$roleid   = 0;
38
39if ($report > 50) {
40    $roleid = substr($report,1);
41    $report = 5;
42}
43
44if ($report == STATS_REPORT_USER_LOGINS) {
45    $courseid = SITEID; //override
46}
47
48if ($mode == STATS_MODE_RANKED) {
49    redirect($CFG->wwwroot.'/report/stats/index.php?time='.$time);
50}
51
52if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
53    print_error("invalidcourseid");
54}
55
56if (!empty($userid)) {
57    $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
58} else {
59    $user = null;
60}
61
62require_login($course);
63$context = context_course::instance($course->id);
64require_capability('report/stats:view', $context);
65
66$PAGE->set_url(new moodle_url('/report/stats/index.php', array('course' => $course->id,
67                                                               'report' => $report,
68                                                               'time'   => $time,
69                                                               'mode'   => $mode,
70                                                               'userid' => $userid)));
71navigation_node::override_active_url(new moodle_url('/report/stats/index.php', array('course' => $course->id)));
72
73// Trigger a content view event.
74$event = \report_stats\event\report_viewed::create(array('context' => $context, 'relateduserid' => $userid,
75        'other' => array('report' => $report, 'time' => $time, 'mode' => $mode)));
76$event->trigger();
77stats_check_uptodate($course->id);
78
79$url = new moodle_url('/report/stats/index.php', ['course' => $course->id]);
80report_helper::save_selected_report($courseid, $url);
81
82if ($course->id == SITEID) {
83    admin_externalpage_setup('reportstats', '', null, '', array('pagelayout'=>'report'));
84    echo $OUTPUT->header();
85} else {
86    $strreports = get_string("reports");
87    $strstats = get_string('stats');
88
89    $PAGE->set_title("$course->shortname: $strstats");
90    $PAGE->set_heading($course->fullname);
91    $PAGE->set_pagelayout('report');
92    $PAGE->set_headingmenu(report_stats_mode_menu($course, $mode, $time, "$CFG->wwwroot/report/stats/index.php"));
93    echo $OUTPUT->header();
94
95    // Print the selected dropdown.
96    $pluginname = get_string('pluginname', 'report_stats');
97    report_helper::print_report_selector($pluginname);
98}
99
100report_stats_report($course, $report, $mode, $user, $roleid, $time);
101
102if (empty($CFG->enablestats)) {
103    if (has_capability('moodle/site:config', context_system::instance())) {
104        redirect("$CFG->wwwroot/$CFG->admin/settings.php?section=stats", get_string('mustenablestats', 'admin'), 3);
105    } else {
106        print_error('statsdisable');
107    }
108}
109
110echo $OUTPUT->footer();
111
112
113