Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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);
  46  
  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');
  52  
  53  if ($delete and confirm_sesskey()) {
  54      report_usersessions_kill_session($delete);
  55      redirect($PAGE->url);
  56  }
  57  
  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      ));
  63  
  64  echo $OUTPUT->header();
  65  echo $OUTPUT->heading(get_string('mysessions', 'report_usersessions'));
  66  
  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());
  73  
  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 = '';
  79  
  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'));
  84      }
  85      $data[] = array(userdate($session->timecreated), $lastaccess, report_usersessions_format_ip($session->lastip), $deletelink);
  86  }
  87  
  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);
  93  
  94  echo $OUTPUT->footer();
  95