MDL-25637 Fixed whitespace
[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         $rafrom       = "";
47         $rawhere      = "";
48         $params = array();
50         //Add this to the SQL to show only group users
51         if ($currentgroup !== NULL) {
52             $groupmembers = ", {groups_members} gm";
53             $groupselect = "AND u.id = gm.userid AND gm.groupid = :currentgroup";
54             $params['currentgroup'] = $currentgroup;
55         }
57         $userfields = user_picture::fields('u', array('username'));
59         if ($this->page->course->id == SITEID) {  // Site-level
60             $sql = "SELECT $userfields, MAX(u.lastaccess) AS lastaccess
61                       FROM {user} u $groupmembers
62                      WHERE u.lastaccess > $timefrom
63                            $groupselect
64                   GROUP BY $userfields
65                   ORDER BY lastaccess DESC ";
67            $csql = "SELECT COUNT(u.id), u.id
68                       FROM {user} u $groupmembers
69                      WHERE u.lastaccess > $timefrom
70                            $groupselect
71                   GROUP BY u.id";
73         } else {
74             // Course level - show only enrolled users for now
75             // TODO: add a new capability for viewing of all users (guests+enrolled+viewing)
77             list($esqljoin, $eparams) = get_enrolled_sql($this->page->context);
78             $params = array_merge($params, $eparams);
80             $sql = "SELECT $userfields, MAX(ul.timeaccess) AS lastaccess
81                       FROM {user_lastaccess} ul, {user} u $groupmembers $rafrom
82                       JOIN ($esqljoin) euj ON euj.id = u.id
83                      WHERE ul.timeaccess > $timefrom
84                            AND u.id = ul.userid
85                            AND ul.courseid = :courseid
86                            $groupselect $rawhere
87                   GROUP BY $userfields
88                   ORDER BY lastaccess DESC";
90            $csql = "SELECT u.id
91                       FROM {user_lastaccess} ul, {user} u $groupmembers $rafrom
92                       JOIN ($esqljoin) euj ON euj.id = u.id
93                      WHERE ul.timeaccess > $timefrom
94                            AND u.id = ul.userid
95                            AND ul.courseid = :courseid
96                            $groupselect $rawhere
97                   GROUP BY u.id";
99             $params['courseid'] = $this->page->course->id;
100         }
102         //Calculate minutes
103         $minutes  = floor($timetoshowusers/60);
105         // Verify if we can see the list of users, if not just print number of users
106         if (!has_capability('block/online_users:viewlist', $this->page->context)) {
107             if (!$usercount = $DB->count_records_sql($csql, $params)) {
108                 $usercount = get_string("none");
109             }
110             $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
111             return $this->content;
112         }
114         if ($users = $DB->get_records_sql($sql, $params, 0, 50)) {   // We'll just take the most recent 50 maximum
115             foreach ($users as $user) {
116                 $users[$user->id]->fullname = fullname($user);
117             }
118         } else {
119             $users = array();
120         }
122         if (count($users) < 50) {
123             $usercount = "";
124         } else {
125             $usercount = $DB->count_records_sql($csql, $params);
126             $usercount = ": $usercount";
127         }
129         $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes)."$usercount)</div>";
131         //Now, we have in users, the list of users to show
132         //Because they are online
133         if (!empty($users)) {
134             //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
135             //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
136             $this->content->text .= "<ul class='list'>\n";
137             if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context)
138                            && !empty($CFG->messaging) && !isguestuser()) {
139                 $canshowicon = true;
140             } else {
141                 $canshowicon = false;
142             }
143             foreach ($users as $user) {
144                 $this->content->text .= '<li class="listentry">';
145                 $timeago = format_time(time() - $user->lastaccess); //bruno to calculate correctly on frontpage
147                 if (isguestuser($user)) {
148                     $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16));
149                     $this->content->text .= get_string('guestuser').'</div>';
151                 } else {
152                     $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16));
153                     $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>';
154                 }
155                 if ($canshowicon and ($USER->id != $user->id) and !isguestuser($user)) {  // Only when logged in and messaging active etc
156                     $anchortagcontents = '<img class="iconsmall" src="'.$OUTPUT->pix_url('t/message') . '" alt="'. get_string('messageselectadd') .'" />';
157                     $anchortag = '<a href="'.$CFG->wwwroot.'/message/index.php?id='.$user->id.'" title="'.get_string('messageselectadd').'">'.$anchortagcontents .'</a>';
159                     $this->content->text .= '<div class="message">'.$anchortag.'</div>';
160                 }
161                 $this->content->text .= "</li>\n";
162             }
163             $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
164         } else {
165             $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
166         }
168         return $this->content;
169     }