Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * Listing of all sessions for current user.
19
 *
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>
24
 */
25
 
26
require(__DIR__ . '/../../config.php');
27
require_once(__DIR__ . '/locallib.php');
28
 
29
require_login(null, false);
30
 
31
if (isguestuser()) {
32
    // No guests here!
33
    redirect(new moodle_url('/'));
34
    die;
35
}
36
if (\core\session\manager::is_loggedinas()) {
37
    // No login-as users.
38
    redirect(new moodle_url('/user/index.php'));
39
    die;
40
}
41
 
42
$context = context_user::instance($USER->id);
43
require_capability('report/usersessions:manageownsessions', $context);
44
 
45
$delete = optional_param('delete', 0, PARAM_INT);
1441 ariadna 46
$deleteall = optional_param('deleteall', false, PARAM_BOOL);
47
$lastip = cleanremoteaddr(optional_param('lastip', '', PARAM_TEXT));
1 efrain 48
 
49
$PAGE->set_url('/report/usersessions/user.php');
50
$PAGE->set_context($context);
51
$PAGE->set_title(get_string('navigationlink', 'report_usersessions'));
52
$PAGE->set_heading(fullname($USER));
53
$PAGE->set_pagelayout('admin');
54
 
1441 ariadna 55
// Delete a specific session.
56
if ($delete && confirm_sesskey()) {
1 efrain 57
    report_usersessions_kill_session($delete);
1441 ariadna 58
    redirect(
59
        url: $PAGE->url,
60
        message: get_string('logoutsinglesessionsuccess', 'report_usersessions', $lastip),
61
        messagetype: \core\output\notification::NOTIFY_SUCCESS,
62
    );
1 efrain 63
}
64
 
1441 ariadna 65
// Delete all sessions except current.
66
if ($deleteall && confirm_sesskey()) {
67
    \core\session\manager::destroy_user_sessions($USER->id, session_id());
68
    redirect(
69
        url: $PAGE->url,
70
        message: get_string('logoutothersessionssuccess', 'report_usersessions'),
71
        messagetype: \core\output\notification::NOTIFY_SUCCESS,
72
    );
73
}
74
 
1 efrain 75
// Create the breadcrumb.
76
$PAGE->add_report_nodes($USER->id, array(
77
        'name' => get_string('navigationlink', 'report_usersessions'),
78
        'url' => new moodle_url('/report/usersessions/user.php')
79
    ));
80
 
81
echo $OUTPUT->header();
82
echo $OUTPUT->heading(get_string('mysessions', 'report_usersessions'));
83
 
84
$data = array();
1441 ariadna 85
$sessions = \core\session\manager::get_sessions_by_userid($USER->id);
86
// Order records by timemodified DESC.
87
usort($sessions, function($a, $b){
88
    return $b->timemodified <=> $a->timemodified;
89
});
1 efrain 90
foreach ($sessions as $session) {
1441 ariadna 91
    if ($session->sid === session_id()) {
1 efrain 92
        $lastaccess = get_string('thissession', 'report_usersessions');
93
        $deletelink = '';
94
 
95
    } else {
96
        $lastaccess = report_usersessions_format_duration(time() - $session->timemodified);
1441 ariadna 97
        $url = new moodle_url($PAGE->url, ['delete' => $session->id, 'sesskey' => sesskey(), 'lastip' => $session->lastip]);
1 efrain 98
        $deletelink = html_writer::link($url, get_string('logout'));
99
    }
100
    $data[] = array(userdate($session->timecreated), $lastaccess, report_usersessions_format_ip($session->lastip), $deletelink);
101
}
102
 
103
$table = new html_table();
104
$table->head  = array(get_string('login'), get_string('lastaccess'), get_string('lastip'), get_string('action'));
105
$table->align = array('left', 'left', 'left', 'right');
106
$table->data  = $data;
107
echo html_writer::table($table);
108
 
1441 ariadna 109
// Provide button to log out all other sessions.
110
if (count($sessions) > 1) {
111
    $url = new moodle_url($PAGE->url, ['deleteall' => true]);
112
    echo $OUTPUT->single_button($url, get_string('logoutothersessions', 'report_usersessions'));
113
}
114
 
1 efrain 115
echo $OUTPUT->footer();