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