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('pluginname','block_online_users');
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;
22 $this->content = new stdClass;
23 $this->content->text = '';
24 $this->content->footer = '';
26 if (empty($this->instance)) {
27 return $this->content;
30 $timetoshowusers = 300; //Seconds default
31 if (isset($CFG->block_online_users_timetosee)) {
32 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
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;
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;
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
65 ORDER BY lastaccess DESC ";
67 $csql = "SELECT COUNT(u.id), u.id
68 FROM {user} u $groupmembers
69 WHERE u.lastaccess > $timefrom
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
85 AND ul.courseid = :courseid
88 ORDER BY lastaccess DESC";
91 FROM {user_lastaccess} ul, {user} u $groupmembers $rafrom
92 JOIN ($esqljoin) euj ON euj.id = u.id
93 WHERE ul.timeaccess > $timefrom
95 AND ul.courseid = :courseid
99 $params['courseid'] = $this->page->course->id;
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");
110 $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
111 return $this->content;
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);
122 if (count($users) < 50) {
125 $usercount = $DB->count_records_sql($csql, $params);
126 $usercount = ": $usercount";
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()) {
141 $canshowicon = false;
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>';
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.'&course='.$this->page->course->id.'" title="'.$timeago.'">'.$user->fullname.'</a></div>';
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>';
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;