Merge branch 'MDL-58030_32' of git://github.com/aolley/moodle into MOODLE_32_STABLE
[moodle.git] / blocks / online_users / block_online_users.php
blob9b2e19e3749037498d8a5e5afc858c90e0233266
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.
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/>.
17 /**
18 * Online users block.
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
25 use block_online_users\fetcher;
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.
32 class block_online_users extends block_base {
33 function init() {
34 $this->title = get_string('pluginname','block_online_users');
37 function has_config() {
38 return true;
41 function get_content() {
42 global $USER, $CFG, $DB, $OUTPUT;
44 if ($this->content !== NULL) {
45 return $this->content;
48 $this->content = new stdClass;
49 $this->content->text = '';
50 $this->content->footer = '';
52 if (empty($this->instance)) {
53 return $this->content;
56 $timetoshowusers = 300; //Seconds default
57 if (isset($CFG->block_online_users_timetosee)) {
58 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
60 $now = time();
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));
67 //Get the user current group
68 $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL;
70 $sitelevel = $this->page->course->id == SITEID || $this->page->context->contextlevel < CONTEXT_COURSE;
72 $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $this->page->context,
73 $sitelevel, $this->page->course->id);
75 //Calculate minutes
76 $minutes = floor($timetoshowusers/60);
78 // Verify if we can see the list of users, if not just print number of users
79 if (!has_capability('block/online_users:viewlist', $this->page->context)) {
80 if (!$usercount = $onlineusers->count_users()) {
81 $usercount = get_string("none");
83 $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
84 return $this->content;
86 $userlimit = 50; // We'll just take the most recent 50 maximum.
87 if ($users = $onlineusers->get_users($userlimit)) {
88 foreach ($users as $user) {
89 $users[$user->id]->fullname = fullname($user);
91 } else {
92 $users = array();
95 $usercount = $onlineusers->count_users();
96 $usercount = ": $usercount";
98 $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes)."$usercount)</div>";
100 //Now, we have in users, the list of users to show
101 //Because they are online
102 if (!empty($users)) {
103 //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
104 //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
105 $this->content->text .= "<ul class='list'>\n";
106 if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context)
107 && !empty($CFG->messaging) && !isguestuser()) {
108 $canshowicon = true;
109 } else {
110 $canshowicon = false;
112 foreach ($users as $user) {
113 $this->content->text .= '<li class="listentry">';
114 $timeago = format_time($now - $user->lastaccess); //bruno to calculate correctly on frontpage
116 if (isguestuser($user)) {
117 $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false));
118 $this->content->text .= get_string('guestuser').'</div>';
120 } else {
121 $this->content->text .= '<div class="user">';
122 $this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->page->course->id.'" title="'.$timeago.'">';
123 $this->content->text .= $OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false, 'link'=>false)) .$user->fullname.'</a></div>';
125 if ($canshowicon and ($USER->id != $user->id) and !isguestuser($user)) { // Only when logged in and messaging active etc
126 $anchortagcontents = '<img class="iconsmall" src="'.$OUTPUT->pix_url('t/message') . '" alt="'. get_string('messageselectadd') .'" />';
127 $anchorurl = new moodle_url('/message/index.php', array('id' => $user->id));
128 $anchortag = html_writer::link($anchorurl, $anchortagcontents,
129 array('title' => get_string('messageselectadd')));
131 $this->content->text .= '<div class="message">'.$anchortag.'</div>';
133 $this->content->text .= "</li>\n";
135 $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
136 } else {
137 $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
140 return $this->content;