2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
26 * This block needs to be reworked.
27 * The new roles system does away with the concepts of rigid student and
30 class block_online_users
extends block_base
{
32 $this->title
= get_string('pluginname','block_online_users');
35 function has_config() {
39 function get_content() {
40 global $USER, $CFG, $DB, $OUTPUT;
42 if ($this->content
!== NULL) {
43 return $this->content
;
46 $this->content
= new stdClass
;
47 $this->content
->text
= '';
48 $this->content
->footer
= '';
50 if (empty($this->instance
)) {
51 return $this->content
;
54 $timetoshowusers = 300; //Seconds default
55 if (isset($CFG->block_online_users_timetosee
)) {
56 $timetoshowusers = $CFG->block_online_users_timetosee
* 60;
59 $timefrom = 100 * floor(($now - $timetoshowusers) / 100); // Round to nearest 100 seconds for better query cache
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
));
66 //Get the user current group
67 $currentgroup = $isseparategroups ?
groups_get_course_group($this->page
->course
) : NULL;
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;
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
91 ORDER BY lastaccess DESC ";
93 $csql = "SELECT COUNT(u.id)
94 FROM {user} u $groupmembers
95 WHERE u.lastaccess > :timefrom
96 AND u.lastaccess <= :now
101 // Course level - show only enrolled users for now
102 // TODO: add a new capability for viewing of all users (guests+enrolled+viewing)
104 list($esqljoin, $eparams) = get_enrolled_sql($this->page
->context
);
105 $params = array_merge($params, $eparams);
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
112 AND ul.courseid = :courseid
113 AND ul.timeaccess <= :now
117 ORDER BY lastaccess DESC";
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
124 AND ul.courseid = :courseid
125 AND ul.timeaccess <= :now
129 $params['courseid'] = $this->page
->course
->id
;
133 $minutes = floor($timetoshowusers/60);
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");
140 $this->content
->text
= "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
141 return $this->content
;
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);
152 if (count($users) < 50) {
155 $usercount = $DB->count_records_sql($csql, $params);
156 $usercount = ": $usercount";
159 $this->content
->text
= "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes)."$usercount)</div>";
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()) {
171 $canshowicon = false;
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
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>';
182 $this->content
->text
.= '<div class="user">';
183 $this->content
->text
.= '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$user->id
.'&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>';
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>';
190 $this->content
->text
.= '<div class="message">'.$anchortag.'</div>';
192 $this->content
->text
.= "</li>\n";
194 $this->content
->text
.= '</ul><div class="clearer"><!-- --></div>';
196 $this->content
->text
.= "<div class=\"info\">".get_string("none")."</div>";
199 return $this->content
;