[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/blocks/online_users/ -> block_online_users.php (source)

   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  /**
  26   * This block needs to be reworked.
  27   * The new roles system does away with the concepts of rigid student and
  28   * teacher roles.
  29   */
  30  class block_online_users extends block_base {
  31      function init() {
  32          $this->title = get_string('pluginname','block_online_users');
  33      }
  34  
  35      function has_config() {
  36          return true;
  37      }
  38  
  39      function get_content() {
  40          global $USER, $CFG, $DB, $OUTPUT;
  41  
  42          if ($this->content !== NULL) {
  43              return $this->content;
  44          }
  45  
  46          $this->content = new stdClass;
  47          $this->content->text = '';
  48          $this->content->footer = '';
  49  
  50          if (empty($this->instance)) {
  51              return $this->content;
  52          }
  53  
  54          $timetoshowusers = 300; //Seconds default
  55          if (isset($CFG->block_online_users_timetosee)) {
  56              $timetoshowusers = $CFG->block_online_users_timetosee * 60;
  57          }
  58          $now = time();
  59          $timefrom = 100 * floor(($now - $timetoshowusers) / 100); // Round to nearest 100 seconds for better query cache
  60  
  61          //Calculate if we are in separate groups
  62          $isseparategroups = ($this->page->course->groupmode == SEPARATEGROUPS
  63                               && $this->page->course->groupmodeforce
  64                               && !has_capability('moodle/site:accessallgroups', $this->page->context));
  65  
  66          //Get the user current group
  67          $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL;
  68  
  69          $groupmembers = "";
  70          $groupselect  = "";
  71          $params = array();
  72  
  73          //Add this to the SQL to show only group users
  74          if ($currentgroup !== NULL) {
  75              $groupmembers = ", {groups_members} gm";
  76              $groupselect = "AND u.id = gm.userid AND gm.groupid = :currentgroup";
  77              $params['currentgroup'] = $currentgroup;
  78          }
  79  
  80          $userfields = user_picture::fields('u', array('username'));
  81          $params['now'] = $now;
  82          $params['timefrom'] = $timefrom;
  83          if ($this->page->course->id == SITEID or $this->page->context->contextlevel < CONTEXT_COURSE) {  // Site-level
  84              $sql = "SELECT $userfields, MAX(u.lastaccess) AS lastaccess
  85                        FROM {user} u $groupmembers
  86                       WHERE u.lastaccess > :timefrom
  87                             AND u.lastaccess <= :now
  88                             AND u.deleted = 0
  89                             $groupselect
  90                    GROUP BY $userfields
  91                    ORDER BY lastaccess DESC ";
  92  
  93             $csql = "SELECT COUNT(u.id)
  94                        FROM {user} u $groupmembers
  95                       WHERE u.lastaccess > :timefrom
  96                             AND u.lastaccess <= :now
  97                             AND u.deleted = 0
  98                             $groupselect";
  99  
 100          } else {
 101              // Course level - show only enrolled users for now
 102              // TODO: add a new capability for viewing of all users (guests+enrolled+viewing)
 103  
 104              list($esqljoin, $eparams) = get_enrolled_sql($this->page->context);
 105              $params = array_merge($params, $eparams);
 106  
 107              $sql = "SELECT $userfields, MAX(ul.timeaccess) AS lastaccess
 108                        FROM {user_lastaccess} ul $groupmembers, {user} u
 109                        JOIN ($esqljoin) euj ON euj.id = u.id
 110                       WHERE ul.timeaccess > :timefrom
 111                             AND u.id = ul.userid
 112                             AND ul.courseid = :courseid
 113                             AND ul.timeaccess <= :now
 114                             AND u.deleted = 0
 115                             $groupselect
 116                    GROUP BY $userfields
 117                    ORDER BY lastaccess DESC";
 118  
 119             $csql = "SELECT COUNT(u.id)
 120                        FROM {user_lastaccess} ul $groupmembers, {user} u
 121                        JOIN ($esqljoin) euj ON euj.id = u.id
 122                       WHERE ul.timeaccess > :timefrom
 123                             AND u.id = ul.userid
 124                             AND ul.courseid = :courseid
 125                             AND ul.timeaccess <= :now
 126                             AND u.deleted = 0
 127                             $groupselect";
 128  
 129              $params['courseid'] = $this->page->course->id;
 130          }
 131  
 132          //Calculate minutes
 133          $minutes  = floor($timetoshowusers/60);
 134  
 135          // Verify if we can see the list of users, if not just print number of users
 136          if (!has_capability('block/online_users:viewlist', $this->page->context)) {
 137              if (!$usercount = $DB->count_records_sql($csql, $params)) {
 138                  $usercount = get_string("none");
 139              }
 140              $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
 141              return $this->content;
 142          }
 143  
 144          if ($users = $DB->get_records_sql($sql, $params, 0, 50)) {   // We'll just take the most recent 50 maximum
 145              foreach ($users as $user) {
 146                  $users[$user->id]->fullname = fullname($user);
 147              }
 148          } else {
 149              $users = array();
 150          }
 151  
 152          if (count($users) < 50) {
 153              $usercount = "";
 154          } else {
 155              $usercount = $DB->count_records_sql($csql, $params);
 156              $usercount = ": $usercount";
 157          }
 158  
 159          $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes)."$usercount)</div>";
 160  
 161          //Now, we have in users, the list of users to show
 162          //Because they are online
 163          if (!empty($users)) {
 164              //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
 165              //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
 166              $this->content->text .= "<ul class='list'>\n";
 167              if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context)
 168                             && !empty($CFG->messaging) && !isguestuser()) {
 169                  $canshowicon = true;
 170              } else {
 171                  $canshowicon = false;
 172              }
 173              foreach ($users as $user) {
 174                  $this->content->text .= '<li class="listentry">';
 175                  $timeago = format_time($now - $user->lastaccess); //bruno to calculate correctly on frontpage
 176  
 177                  if (isguestuser($user)) {
 178                      $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false));
 179                      $this->content->text .= get_string('guestuser').'</div>';
 180  
 181                  } else {
 182                      $this->content->text .= '<div class="user">';
 183                      $this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->page->course->id.'" title="'.$timeago.'">';
 184                      $this->content->text .= $OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false, 'link'=>false)) .$user->fullname.'</a></div>';
 185                  }
 186                  if ($canshowicon and ($USER->id != $user->id) and !isguestuser($user)) {  // Only when logged in and messaging active etc
 187                      $anchortagcontents = '<img class="iconsmall" src="'.$OUTPUT->pix_url('t/message') . '" alt="'. get_string('messageselectadd') .'" />';
 188                      $anchortag = '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'" title="'.get_string('messageselectadd').'">'.$anchortagcontents .'</a>';
 189  
 190                      $this->content->text .= '<div class="message">'.$anchortag.'</div>';
 191                  }
 192                  $this->content->text .= "</li>\n";
 193              }
 194              $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
 195          } else {
 196              $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
 197          }
 198  
 199          return $this->content;
 200      }
 201  }
 202  
 203  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1