7cc7556bc859254113157dc3a7c78ededfbd7adc
[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('blockname','block_online_users');
11         $this->version = 2007101510;
12     }
14     function has_config() {return true;}
16     function get_content() {
17         global $USER, $CFG, $DB, $OUTPUT;
19         if ($this->content !== NULL) {
20             return $this->content;
21         }
23         $this->content = new stdClass;
24         $this->content->text = '';
25         $this->content->footer = '';
27         if (empty($this->instance)) {
28             return $this->content;
29         }
31         $timetoshowusers = 300; //Seconds default
32         if (isset($CFG->block_online_users_timetosee)) {
33             $timetoshowusers = $CFG->block_online_users_timetosee * 60;
34         }
35         $timefrom = 100 * floor((time()-$timetoshowusers) / 100); // Round to nearest 100 seconds for better query cache
37         //Calculate if we are in separate groups
38         $isseparategroups = ($this->page->course->groupmode == SEPARATEGROUPS
39                              && $this->page->course->groupmodeforce
40                              && !has_capability('moodle/site:accessallgroups', $this->page->context));
42         //Get the user current group
43         $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL;
45         $groupmembers = "";
46         $groupselect  = "";
47         $rafrom       = "";
48         $rawhere      = "";
49         $params = array();
51         //Add this to the SQL to show only group users
52         if ($currentgroup !== NULL) {
53             $groupmembers = ", {groups_members} gm";
54             $groupselect = "AND u.id = gm.userid AND gm.groupid = :currentgroup";
55             $params['currentgroup'] = $currentgroup;
56         }
58         $userfields = user_picture::fields('u').', username';
60         if ($this->page->course->id == SITEID) {  // Site-level
61             $sql = "SELECT $userfields, MAX(u.lastaccess) AS lastaccess
62                       FROM {user} u $groupmembers
63                      WHERE u.lastaccess > $timefrom
64                            $groupselect
65                   GROUP BY $userfields
66                   ORDER BY lastaccess DESC ";
68            $csql = "SELECT COUNT(u.id), u.id
69                       FROM {user} u $groupmembers
70                      WHERE u.lastaccess > $timefrom
71                            $groupselect
72                   GROUP BY u.id";
74         } else {
75             // Course level - show only enrolled users for now
76             // TODO: add a new capability for viewing of all users (guests+enrolled+viewing)
78             list($esqljoin, $eparams) = get_enrolled_sql($this->page->context);
79             $params = array_merge($params, $eparams);
81             $sql = "SELECT $userfields, MAX(ul.timeaccess) AS lastaccess
82                       FROM {user_lastaccess} ul, {user} u $groupmembers $rafrom
83                       JOIN ($esqljoin) euj ON euj.id = u.id     
84                      WHERE ul.timeaccess > $timefrom
85                            AND u.id = ul.userid
86                            AND ul.courseid = :courseid
87                            $groupselect $rawhere
88                   GROUP BY $userfields
89                   ORDER BY lastaccess DESC";
91            $csql = "SELECT u.id
92                       FROM {user_lastaccess} ul, {user} u $groupmembers $rafrom
93                       JOIN ($esqljoin) euj ON euj.id = u.id
94                      WHERE ul.timeaccess > $timefrom
95                            AND u.id = ul.userid
96                            AND ul.courseid = :courseid
97                            $groupselect $rawhere
98                   GROUP BY u.id";
100             $params['courseid'] = $this->page->course->id;
101         }
103         //Calculate minutes
104         $minutes  = floor($timetoshowusers/60);
106         // Verify if we can see the list of users, if not just print number of users
107         if (!has_capability('block/online_users:viewlist', $this->page->context)) {
108             if (!$usercount = $DB->count_records_sql($csql, $params)) {
109                 $usercount = get_string("none");
110             }
111             $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
112             return $this->content;
113         }
115         if ($users = $DB->get_records_sql($sql, $params, 0, 50)) {   // We'll just take the most recent 50 maximum
116             foreach ($users as $user) {
117                 $users[$user->id]->fullname = fullname($user);
118             }
119         } else {
120             $users = array();
121         }
123         if (count($users) < 50) {
124             $usercount = "";
125         } else {
126             $usercount = $DB->count_records_sql($csql, $params);
127             $usercount = ": $usercount";
128         }
130         $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes)."$usercount)</div>";
132         //Now, we have in users, the list of users to show
133         //Because they are online
134         if (!empty($users)) {
135             //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
136             //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
137             $this->content->text .= "<ul class='list'>\n";
138             if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context)
139                            && !empty($CFG->messaging) && !isguestuser()) {
140                 $canshowicon = true;
141             } else {
142                 $canshowicon = false;
143             }
144             foreach ($users as $user) {
145                 $this->content->text .= '<li class="listentry">';
146                 $timeago = format_time(time() - $user->lastaccess); //bruno to calculate correctly on frontpage
148                 if ($user->username == 'guest') {
149                     $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16));
150                     $this->content->text .= get_string('guestuser').'</div>';
152                 } else {
153                     $this->content->text .= '<div class="user"><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->page->course->id.'" title="'.$timeago.'">';
154                     $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16));
155                     $this->content->text .= $user->fullname.'</a></div>';
156                 }
157                 if ($canshowicon and ($USER->id != $user->id) and  $user->username != 'guest') {  // Only when logged in and messaging active etc
158                     $this->content->text .= '<div class="message"><a title="'.get_string('messageselectadd').'" href="'.$CFG->wwwroot.'/message/discussion.php?id='.$user->id.'" onclick="this.target=\'message_'.$user->id.'\';return openpopup(\'/message/discussion.php?id='.$user->id.'\', \'message_'.$user->id.'\', \'menubar=0,location=0,scrollbars,status,resizable,width=400,height=500\', 0);">'
159                         .'<img class="iconsmall" src="'.$OUTPUT->pix_url('t/message') . '" alt="'. get_string('messageselectadd') .'" /></a></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     }