MDL-41944 block_onlines_users: Improve performance
[moodle.git] / blocks / online_users / block_online_users.php
CommitLineData
4ca6cfbf 1<?php
f25a6839
SH
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 */
0f3fe4b6 24
0964aa33
BO
25use block_online_users\fetcher;
26
01199ea9 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 */
e89d741a 32class block_online_users extends block_base {
9b4b78fd 33 function init() {
4bbe3bd8 34 $this->title = get_string('pluginname','block_online_users');
89adb174 35 }
36
bfe20414
MN
37 function has_config() {
38 return true;
39 }
0f3fe4b6 40
0f3fe4b6 41 function get_content() {
cf4a17cb 42 global $USER, $CFG, $DB, $OUTPUT, $PAGE;
0f3fe4b6 43
675dbdd9 44 if ($this->content !== NULL) {
0f3fe4b6 45 return $this->content;
46 }
47
9b4b78fd 48 $this->content = new stdClass;
5ef50b77 49 $this->content->text = '';
50 $this->content->footer = '';
283ebb71 51
9b4b78fd 52 if (empty($this->instance)) {
675dbdd9 53 return $this->content;
54 }
01199ea9 55
076cc57c 56 $timetoshowusers = 300; //Seconds default
57 if (isset($CFG->block_online_users_timetosee)) {
58 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
59 }
2628edb1 60 $now = time();
0f3fe4b6 61
c7756c73 62 //Calculate if we are in separate groups
cb640229 63 $isseparategroups = ($this->page->course->groupmode == SEPARATEGROUPS
64 && $this->page->course->groupmodeforce
e92c286c 65 && !has_capability('moodle/site:accessallgroups', $this->page->context));
0f3fe4b6 66
c7756c73 67 //Get the user current group
cb640229 68 $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL;
0f3fe4b6 69
0964aa33 70 $sitelevel = $this->page->course->id == SITEID || $this->page->context->contextlevel < CONTEXT_COURSE;
2d90bdf0 71
0964aa33
BO
72 $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $sitelevel,
73 $this->page->context, $this->page->course->id);
dd97c328 74
283ebb71 75 //Calculate minutes
76 $minutes = floor($timetoshowusers/60);
dd97c328 77
283ebb71 78 // Verify if we can see the list of users, if not just print number of users
e92c286c 79 if (!has_capability('block/online_users:viewlist', $this->page->context)) {
0964aa33 80 if (!$usercount = $onlineusers->count_users()) {
283ebb71 81 $usercount = get_string("none");
82 }
83 $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
84 return $this->content;
85 }
0964aa33
BO
86 $userlimit = 50; // We'll just take the most recent 50 maximum.
87 if ($users = $onlineusers->get_users($userlimit)) {
283ebb71 88 foreach ($users as $user) {
89 $users[$user->id]->fullname = fullname($user);
174526f4 90 }
283ebb71 91 } else {
92 $users = array();
93 }
94
0964aa33 95 if (count($users) < $userlimit) {
283ebb71 96 $usercount = "";
97 } else {
0964aa33 98 $usercount = $onlineusers->count_users();
283ebb71 99 $usercount = ": $usercount";
100 }
0f3fe4b6 101
283ebb71 102 $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes)."$usercount)</div>";
0f3fe4b6 103
104 //Now, we have in users, the list of users to show
105 //Because they are online
076cc57c 106 if (!empty($users)) {
450a0a7d 107 //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
108 //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
109 $this->content->text .= "<ul class='list'>\n";
4f0c2d00 110 if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context)
4cdb8d70 111 && !empty($CFG->messaging) && !isguestuser()) {
59eaa9ac 112 $canshowicon = true;
cf4a17cb 113 message_messenger_requirejs();
59eaa9ac 114 } else {
115 $canshowicon = false;
116 }
0f3fe4b6 117 foreach ($users as $user) {
450a0a7d 118 $this->content->text .= '<li class="listentry">';
2628edb1 119 $timeago = format_time($now - $user->lastaccess); //bruno to calculate correctly on frontpage
4ca6cfbf 120
629e12fd 121 if (isguestuser($user)) {
4ff849b7 122 $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false));
440d1648 123 $this->content->text .= get_string('guestuser').'</div>';
9d5210af 124
125 } else {
4ff849b7
JF
126 $this->content->text .= '<div class="user">';
127 $this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->page->course->id.'" title="'.$timeago.'">';
128 $this->content->text .= $OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false, 'link'=>false)) .$user->fullname.'</a></div>';
9d5210af 129 }
629e12fd 130 if ($canshowicon and ($USER->id != $user->id) and !isguestuser($user)) { // Only when logged in and messaging active etc
eaed2fd1 131 $anchortagcontents = '<img class="iconsmall" src="'.$OUTPUT->pix_url('t/message') . '" alt="'. get_string('messageselectadd') .'" />';
cf4a17cb
FM
132 $anchorurl = new moodle_url('/message/index.php', array('id' => $user->id));
133 $anchortag = html_writer::link($anchorurl, $anchortagcontents, array_merge(
134 message_messenger_sendmessage_link_params($user),
135 array('title' => get_string('messageselectadd'))
136 ));
eaed2fd1
AD
137
138 $this->content->text .= '<div class="message">'.$anchortag.'</div>';
4b22e391 139 }
73bb9da7 140 $this->content->text .= "</li>\n";
0f3fe4b6 141 }
440d1648 142 $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
0f3fe4b6 143 } else {
440d1648 144 $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
0f3fe4b6 145 }
146
147 return $this->content;
148 }
149}
150
4ca6cfbf 151