| 1 | efrain | 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 |  * Display user activity reports for a course (totals)
 | 
        
           |  |  | 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 |   | 
        
           |  |  | 25 | require('../../config.php');
 | 
        
           |  |  | 26 | require_once($CFG->dirroot.'/report/log/locallib.php');
 | 
        
           |  |  | 27 | require_once($CFG->dirroot.'/lib/tablelib.php');
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | $userid   = required_param('id', PARAM_INT);
 | 
        
           |  |  | 30 | $courseid = required_param('course', PARAM_INT);
 | 
        
           |  |  | 31 | $mode     = optional_param('mode', 'today', PARAM_ALPHA);
 | 
        
           |  |  | 32 | $page     = optional_param('page', 0, PARAM_INT);
 | 
        
           |  |  | 33 | $perpage  = optional_param('perpage', 100, PARAM_INT);
 | 
        
           |  |  | 34 | $logreader   = optional_param('logreader', '', PARAM_COMPONENT); // Log reader which will be used for displaying logs.
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 | if ($mode !== 'today' and $mode !== 'all') {
 | 
        
           |  |  | 37 |     $mode = 'today';
 | 
        
           |  |  | 38 | }
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 | $user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), '*', MUST_EXIST);
 | 
        
           | 1441 | ariadna | 41 | $course = $DB->get_record('course', ['id' => $courseid], '*');
 | 
        
           |  |  | 42 | $sitecoursefilter = 0;
 | 
        
           |  |  | 43 | if (!$course) {
 | 
        
           |  |  | 44 |     // Missing courses may have be deleted, so display them in site context.
 | 
        
           |  |  | 45 |     $course = $SITE;
 | 
        
           |  |  | 46 |     $sitecoursefilter = $courseid;
 | 
        
           |  |  | 47 | }
 | 
        
           | 1 | efrain | 48 |   | 
        
           |  |  | 49 | $coursecontext   = context_course::instance($course->id);
 | 
        
           |  |  | 50 | $personalcontext = context_user::instance($user->id);
 | 
        
           |  |  | 51 |   | 
        
           | 1441 | ariadna | 52 | if ($course->id == SITEID) {
 | 
        
           | 1 | efrain | 53 |     $PAGE->set_context($personalcontext);
 | 
        
           |  |  | 54 | }
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 | if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
 | 
        
           |  |  | 57 |         and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
 | 
        
           |  |  | 58 |     //TODO: do not require parents to be enrolled in courses - this is a hack!
 | 
        
           |  |  | 59 |     require_login();
 | 
        
           |  |  | 60 |     $PAGE->set_course($course);
 | 
        
           |  |  | 61 | } else {
 | 
        
           |  |  | 62 |     require_login($course);
 | 
        
           |  |  | 63 | }
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 | list($all, $today) = report_log_can_access_user_report($user, $course);
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 | if (!$today && !$all) {
 | 
        
           |  |  | 68 |     throw new \moodle_exception('nocapability', 'report_log');
 | 
        
           |  |  | 69 | }
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 | if ($mode === 'today') {
 | 
        
           |  |  | 72 |     if (!$today) {
 | 
        
           |  |  | 73 |         require_capability('report/log:viewtoday', $coursecontext);
 | 
        
           |  |  | 74 |     }
 | 
        
           |  |  | 75 | } else {
 | 
        
           |  |  | 76 |     if (!$all) {
 | 
        
           |  |  | 77 |         require_capability('report/log:view', $coursecontext);
 | 
        
           |  |  | 78 |     }
 | 
        
           |  |  | 79 | }
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 | $stractivityreport = get_string('activityreport');
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 | $PAGE->set_pagelayout('report');
 | 
        
           | 1441 | ariadna | 84 | $PAGE->set_url('/report/log/user.php', ['id' => $user->id, 'course' => $courseid, 'mode' => $mode]);
 | 
        
           | 1 | efrain | 85 | $PAGE->navigation->extend_for_user($user);
 | 
        
           |  |  | 86 | $PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
 | 
        
           |  |  | 87 | $PAGE->set_title("$course->shortname: $stractivityreport");
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 | // Create the appropriate breadcrumb.
 | 
        
           |  |  | 90 | $navigationnode = array(
 | 
        
           | 1441 | ariadna | 91 |         'url' => new moodle_url('/report/log/user.php', ['id' => $user->id, 'course' => $courseid, 'mode' => $mode]),
 | 
        
           | 1 | efrain | 92 |     );
 | 
        
           |  |  | 93 | if ($mode === 'today') {
 | 
        
           |  |  | 94 |     $navigationnode['name'] = get_string('todaylogs');
 | 
        
           |  |  | 95 | } else {
 | 
        
           |  |  | 96 |     $navigationnode['name'] = get_string('alllogs');
 | 
        
           |  |  | 97 | }
 | 
        
           |  |  | 98 | $PAGE->add_report_nodes($user->id, $navigationnode);
 | 
        
           |  |  | 99 |   | 
        
           | 1441 | ariadna | 100 | if ($course->id == SITEID) {
 | 
        
           | 1 | efrain | 101 |     $PAGE->set_heading(fullname($user, has_capability('moodle/site:viewfullnames', $PAGE->context)));
 | 
        
           |  |  | 102 | } else {
 | 
        
           |  |  | 103 |     $PAGE->set_heading($course->fullname);
 | 
        
           |  |  | 104 | }
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 | // Trigger a user logs viewed event.
 | 
        
           |  |  | 107 | $event = \report_log\event\user_report_viewed::create(array('context' => $coursecontext, 'relateduserid' => $userid,
 | 
        
           |  |  | 108 |         'other' => array('mode' => $mode)));
 | 
        
           |  |  | 109 | $event->trigger();
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 | echo $OUTPUT->header();
 | 
        
           | 1441 | ariadna | 112 | if ($course->id != SITEID) {
 | 
        
           | 1 | efrain | 113 |     $userheading = array(
 | 
        
           |  |  | 114 |             'heading' => fullname($user, has_capability('moodle/site:viewfullnames', $PAGE->context)),
 | 
        
           |  |  | 115 |             'user' => $user,
 | 
        
           |  |  | 116 |             'usercontext' => $personalcontext,
 | 
        
           |  |  | 117 |         );
 | 
        
           |  |  | 118 |     echo $OUTPUT->context_header($userheading, 2);
 | 
        
           |  |  | 119 |     if ($mode === 'today') {
 | 
        
           |  |  | 120 |         echo $OUTPUT->heading(get_string('todaylogs', 'moodle'), 2, 'main mt-4 mb-4');
 | 
        
           |  |  | 121 |     } else {
 | 
        
           |  |  | 122 |         echo $OUTPUT->heading(get_string('alllogs', 'moodle'), 2, 'main mt-4 mb-4');
 | 
        
           |  |  | 123 |     }
 | 
        
           |  |  | 124 | }
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 | // Time to filter records from.
 | 
        
           |  |  | 127 | if ($mode === 'today') {
 | 
        
           |  |  | 128 |     $timefrom = usergetmidnight(time());
 | 
        
           |  |  | 129 | } else {
 | 
        
           |  |  | 130 |     $timefrom = 0;
 | 
        
           |  |  | 131 | }
 | 
        
           |  |  | 132 |   | 
        
           |  |  | 133 | $output = $PAGE->get_renderer('report_log');
 | 
        
           |  |  | 134 | $reportlog = new report_log_renderable($logreader, $course, $user->id, 0, '', -1, -1, false, false, true, false, $PAGE->url,
 | 
        
           | 1441 | ariadna | 135 |         $timefrom, '', $page, $perpage, 'timecreated DESC', '' , $sitecoursefilter);
 | 
        
           | 1 | efrain | 136 |   | 
        
           |  |  | 137 | // Setup table if log reader is enabled.
 | 
        
           |  |  | 138 | if (!empty($reportlog->selectedlogreader)) {
 | 
        
           |  |  | 139 |     $reportlog->setup_table();
 | 
        
           |  |  | 140 |     $reportlog->tablelog->is_downloadable(false);
 | 
        
           |  |  | 141 | }
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 | echo $output->reader_selector($reportlog);
 | 
        
           |  |  | 144 |   | 
        
           |  |  | 145 | // Print the graphic chart accordingly to the mode (all, today).
 | 
        
           |  |  | 146 | echo '<div class="graph">';
 | 
        
           | 1441 | ariadna | 147 | report_log_print_graph($course, $user, $mode, 0, $logreader, $sitecoursefilter);
 | 
        
           | 1 | efrain | 148 | echo '</div>';
 | 
        
           |  |  | 149 |   | 
        
           |  |  | 150 | echo $output->render($reportlog);
 | 
        
           |  |  | 151 | echo $OUTPUT->footer();
 |