Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * Online users block.
  19   *
  20   * @package    block_online_users
  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  use block_online_users\fetcher;
  26  
  27  /**
  28   * This block needs to be reworked.
  29   * The new roles system does away with the concepts of rigid student and
  30   * teacher roles.
  31   */
  32  class block_online_users extends block_base {
  33      function init() {
  34          $this->title = get_string('pluginname','block_online_users');
  35      }
  36  
  37      function has_config() {
  38          return true;
  39      }
  40  
  41      function get_content() {
  42          global $USER, $CFG, $DB, $OUTPUT;
  43  
  44          if ($this->content !== NULL) {
  45              return $this->content;
  46          }
  47  
  48          $this->content = new stdClass;
  49          $this->content->text = '';
  50          $this->content->footer = '';
  51  
  52          if (empty($this->instance)) {
  53              return $this->content;
  54          }
  55  
  56          $timetoshowusers = 300; //Seconds default
  57          if (isset($CFG->block_online_users_timetosee)) {
  58              $timetoshowusers = $CFG->block_online_users_timetosee * 60;
  59          }
  60          $now = time();
  61  
  62          //Calculate if we are in separate groups
  63          $isseparategroups = ($this->page->course->groupmode == SEPARATEGROUPS
  64                               && $this->page->course->groupmodeforce
  65                               && !has_capability('moodle/site:accessallgroups', $this->page->context));
  66  
  67          //Get the user current group
  68          $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL;
  69  
  70          $sitelevel = $this->page->course->id == SITEID || $this->page->context->contextlevel < CONTEXT_COURSE;
  71  
  72          $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $this->page->context,
  73                  $sitelevel, $this->page->course->id);
  74  
  75          //Calculate minutes
  76          $minutes  = floor($timetoshowusers/60);
  77          $periodminutes = get_string('periodnminutes', 'block_online_users', $minutes);
  78  
  79          // Count users.
  80          $usercount = $onlineusers->count_users();
  81          if ($usercount === 0) {
  82              $usercount = get_string('nouser', 'block_online_users');
  83          } else if ($usercount === 1) {
  84              $usercount = get_string('numuser', 'block_online_users', $usercount);
  85          } else {
  86              $usercount = get_string('numusers', 'block_online_users', $usercount);
  87          }
  88  
  89          $this->content->text = '<div class="info">'.$usercount.' ('.$periodminutes.')</div>';
  90  
  91          // Verify if we can see the list of users, if not just print number of users.
  92          // If the current user is not logged in OR it's a guest then don't show any users.
  93          if (!has_capability('block/online_users:viewlist', $this->page->context)
  94                  || isguestuser() || !isloggedin()) {
  95              return $this->content;
  96          }
  97  
  98          $userlimit = 50; // We'll just take the most recent 50 maximum.
  99          $initialcount = 0;
 100          if ($users = $onlineusers->get_users($userlimit)) {
 101              require_once($CFG->dirroot . '/user/lib.php');
 102              $initialcount = count($users);
 103              foreach ($users as $user) {
 104                  if (!user_can_view_profile($user)) {
 105                      unset($users[$user->id]);
 106                      continue;
 107                  }
 108                  $users[$user->id]->fullname = fullname($user);
 109              }
 110          } else {
 111              $users = array();
 112          }
 113  
 114          //Now, we have in users, the list of users to show
 115          //Because they are online
 116          if (!empty($users)) {
 117              $this->page->requires->js_call_amd('block_online_users/change_user_visibility', 'init');
 118              //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
 119              //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
 120              $this->content->text .= "<ul class='list'>\n";
 121              if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context)
 122                             && !empty($CFG->messaging) && !isguestuser()) {
 123                  $canshowicon = true;
 124              } else {
 125                  $canshowicon = false;
 126              }
 127              foreach ($users as $user) {
 128                  $this->content->text .= '<li class="listentry">';
 129                  $timeago = format_time($now - $user->lastaccess); //bruno to calculate correctly on frontpage
 130  
 131                  if (isguestuser($user)) {
 132                      $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false));
 133                      $this->content->text .= get_string('guestuser').'</div>';
 134  
 135                  } else { // Not a guest user.
 136                      $this->content->text .= '<div class="user">';
 137                      $this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->page->course->id.'" title="'.$timeago.'">';
 138                      $avataroptions = ['size' => 16, 'visibletoscreenreaders' => false, 'link' => false];
 139                      $this->content->text .= $OUTPUT->user_picture($user, $avataroptions) . $user->fullname . '</a></div>';
 140  
 141                      if ($USER->id == $user->id) {
 142                          if ($CFG->block_online_users_onlinestatushiding) {
 143                              $action = ($user->uservisibility != null && $user->uservisibility == 0) ? 'show' : 'hide';
 144                              $anchortagcontents = $OUTPUT->pix_icon('t/' . $action,
 145                                  get_string('online_status:' . $action, 'block_online_users'));
 146                              $anchortag = html_writer::link("", $anchortagcontents,
 147                                  array('title' => get_string('online_status:' . $action, 'block_online_users'),
 148                                      'data-action' => $action, 'data-userid' => $user->id, 'id' => 'change-user-visibility'));
 149  
 150                              $this->content->text .= '<div class="uservisibility">' . $anchortag . '</div>';
 151                          }
 152                      } else {
 153                          if ($canshowicon) {  // Only when logged in and messaging active etc.
 154                              $anchortagcontents = $OUTPUT->pix_icon('t/message', get_string('messageselectadd'));
 155                              $anchorurl = new moodle_url('/message/index.php', array('id' => $user->id));
 156                              $anchortag = html_writer::link($anchorurl, $anchortagcontents,
 157                                  array('title' => get_string('messageselectadd')));
 158  
 159                              $this->content->text .= '<div class="message">'.$anchortag.'</div>';
 160                          }
 161                      }
 162                  }
 163                  $this->content->text .= "</li>\n";
 164              }
 165              if ($initialcount - count($users) > 0) {
 166                  $this->content->text .= '<li class="listentry"><div class="otherusers">';
 167                  $this->content->text .= html_writer::span(
 168                      get_string('otherusers', 'block_online_users', $initialcount - count($users))
 169                  );
 170                  $this->content->text .= "</div>";
 171                  $this->content->text .= "</li>\n";
 172              }
 173              $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
 174          }
 175  
 176          return $this->content;
 177      }
 178  
 179      /**
 180       * Return the plugin config settings for external functions.
 181       *
 182       * @return stdClass the configs for both the block instance and plugin
 183       * @since Moodle 3.8
 184       */
 185      public function get_config_for_external() {
 186          global $CFG;
 187  
 188          // Return all settings for all users since it is safe (no private keys, etc..).
 189          $configs = (object) [
 190              'timetosee' => $CFG->block_online_users_timetosee,
 191              'onlinestatushiding' => $CFG->block_online_users_onlinestatushiding
 192          ];
 193  
 194          return (object) [
 195              'instance' => new stdClass(),
 196              'plugin' => $configs,
 197          ];
 198      }
 199  }
 200  
 201