weekly release 3.9.12+
[moodle.git] / report / usersessions / user.php
blob52e2d856a7f8d55204538980f1ce4d648363ce51
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.
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 * Listing of all sessions for current user.
20 * @package report_usersessions
21 * @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @author Petr Skoda <petr.skoda@totaralms.com>
26 require(__DIR__ . '/../../config.php');
27 require_once(__DIR__ . '/locallib.php');
29 require_login(null, false);
31 if (isguestuser()) {
32 // No guests here!
33 redirect(new moodle_url('/'));
34 die;
36 if (\core\session\manager::is_loggedinas()) {
37 // No login-as users.
38 redirect(new moodle_url('/user/index.php'));
39 die;
42 $context = context_user::instance($USER->id);
43 require_capability('report/usersessions:manageownsessions', $context);
45 $delete = optional_param('delete', 0, PARAM_INT);
47 $PAGE->set_url('/report/usersessions/user.php');
48 $PAGE->set_context($context);
49 $PAGE->set_title(get_string('navigationlink', 'report_usersessions'));
50 $PAGE->set_heading(fullname($USER));
51 $PAGE->set_pagelayout('admin');
53 if ($delete and confirm_sesskey()) {
54 report_usersessions_kill_session($delete);
55 redirect($PAGE->url);
58 // Create the breadcrumb.
59 $PAGE->add_report_nodes($USER->id, array(
60 'name' => get_string('navigationlink', 'report_usersessions'),
61 'url' => new moodle_url('/report/usersessions/user.php')
62 ));
64 echo $OUTPUT->header();
65 echo $OUTPUT->heading(get_string('mysessions', 'report_usersessions'));
67 $data = array();
68 $sql = "SELECT id, timecreated, timemodified, firstip, lastip, sid
69 FROM {sessions}
70 WHERE userid = :userid
71 ORDER BY timemodified DESC";
72 $params = array('userid' => $USER->id, 'sid' => session_id());
74 $sessions = $DB->get_records_sql($sql, $params);
75 foreach ($sessions as $session) {
76 if ($session->sid === $params['sid']) {
77 $lastaccess = get_string('thissession', 'report_usersessions');
78 $deletelink = '';
80 } else {
81 $lastaccess = report_usersessions_format_duration(time() - $session->timemodified);
82 $url = new moodle_url($PAGE->url, array('delete' => $session->id, 'sesskey' => sesskey()));
83 $deletelink = html_writer::link($url, get_string('logout'));
85 $data[] = array(userdate($session->timecreated), $lastaccess, report_usersessions_format_ip($session->lastip), $deletelink);
88 $table = new html_table();
89 $table->head = array(get_string('login'), get_string('lastaccess'), get_string('lastip'), get_string('action'));
90 $table->align = array('left', 'left', 'left', 'right');
91 $table->data = $data;
92 echo html_writer::table($table);
94 echo $OUTPUT->footer();