4 * This block needs to be reworked.
5 * The new roles system does away with the concepts of rigid student and
8 class block_online_users extends block_base {
10 $this->title = get_string('blockname','block_online_users');
11 $this->version = 2007101510;
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;
23 $this->content = new stdClass;
24 $this->content->text = '';
25 $this->content->footer = '';
27 if (empty($this->instance)) {
28 return $this->content;
31 $timetoshowusers = 300; //Seconds default
32 if (isset($CFG->block_online_users_timetosee)) {
33 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
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;
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;
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
66 ORDER BY lastaccess DESC ";
68 $csql = "SELECT COUNT(u.id), u.id
69 FROM {user} u $groupmembers
70 WHERE u.lastaccess > $timefrom
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
86 AND ul.courseid = :courseid
89 ORDER BY lastaccess DESC";
92 FROM {user_lastaccess} ul, {user} u $groupmembers $rafrom
93 JOIN ($esqljoin) euj ON euj.id = u.id
94 WHERE ul.timeaccess > $timefrom
96 AND ul.courseid = :courseid
100 $params['courseid'] = $this->page->course->id;
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");
111 $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
112 return $this->content;
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);
123 if (count($users) < 50) {
126 $usercount = $DB->count_records_sql($csql, $params);
127 $usercount = ": $usercount";
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()) {
142 $canshowicon = false;
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>';
153 $this->content->text .= '<div class="user"><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&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>';
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>';
161 $this->content->text .= "</li>\n";
163 $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
165 $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
168 return $this->content;