MDL-16221
[moodle-linuxchix.git] / blocks / online_users / block_online_users.php
blobb872f57dc2c02f7a00d9dd8af1e3bddb4207074d
1 <?php //$Id$
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 = 2007101509;
14 function has_config() {return true;}
16 function get_content() {
17 global $USER, $CFG, $COURSE;
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 // Get context so we can check capabilities.
38 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
40 //Calculate if we are in separate groups
41 $isseparategroups = ($COURSE->groupmode == SEPARATEGROUPS
42 && $COURSE->groupmodeforce
43 && !has_capability('moodle/site:accessallgroups', $context));
45 //Get the user current group
46 $currentgroup = $isseparategroups ? groups_get_course_group($COURSE) : NULL;
48 $groupmembers = "";
49 $groupselect = "";
51 //Add this to the SQL to show only group users
52 if ($currentgroup !== NULL) {
53 $groupmembers = ", {$CFG->prefix}groups_members gm ";
54 $groupselect = " AND u.id = gm.userid AND gm.groupid = '$currentgroup'";
57 if ($COURSE->id == SITEID) { // Site-level
58 $select = "SELECT u.id, u.username, u.firstname, u.lastname, u.picture, max(u.lastaccess) as lastaccess ";
59 $from = "FROM {$CFG->prefix}user u
60 $groupmembers ";
61 $where = "WHERE u.lastaccess > $timefrom
62 $groupselect ";
63 $order = "ORDER BY lastaccess DESC ";
65 } else { // Course-level
66 $courseselect = "AND ul.courseid = '".$COURSE->id."'";
67 $select = "SELECT u.id, u.username, u.firstname, u.lastname, u.picture, max(ul.timeaccess) as lastaccess ";
68 $from = "FROM {$CFG->prefix}user_lastaccess ul,
69 {$CFG->prefix}user u
70 $groupmembers ";
71 $where = "WHERE ul.timeaccess > $timefrom
72 AND u.id = ul.userid
73 AND ul.courseid = $COURSE->id
74 $groupselect ";
75 $order = "ORDER BY lastaccess DESC ";
78 $groupby = "GROUP BY u.id, u.username, u.firstname, u.lastname, u.picture ";
80 $SQL = $select . $from . $where . $groupby . $order;
82 $users = array();
83 $pcontext = get_related_contexts_string($context);
85 if ($pusers = get_records_sql($SQL, 0, 50)) { // We'll just take the most recent 50 maximum
86 $hidden = false;
88 if (!has_capability('moodle/role:viewhiddenassigns', $context)) {
89 // if current user can't view hidden role assignment in this context and
90 // user has a hidden role assigned at this context or any parent contexts,
91 // ignore this user
92 $userids = array_keys($pusers);
93 $userids = implode(',', $userids);
94 $sql = "SELECT userid
95 FROM {$CFG->prefix}role_assignments
96 WHERE userid IN ($userids) AND contextid $pcontext AND hidden = 1
97 GROUP BY userid";
98 $hidden = get_records_sql($sql);
101 foreach ($pusers as $puser) {
102 if ($hidden and isset($hidden[$puser->id])) {
103 continue;
106 $puser->fullname = fullname($puser);
107 $users[$puser->id] = $puser;
111 //Calculate minutes
112 $minutes = floor($timetoshowusers/60);
114 $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes).")</div>";
116 //Now, we have in users, the list of users to show
117 //Because they are online
118 if (!empty($users)) {
119 //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
120 //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
121 $this->content->text .= "<ul class='list'>\n";
122 foreach ($users as $user) {
123 $this->content->text .= '<li class="listentry">';
124 $timeago = format_time(time() - $user->lastaccess); //bruno to calculate correctly on frontpage
125 if ($user->username == 'guest') {
126 $this->content->text .= '<div class="user">'.print_user_picture($user->id, $COURSE->id, $user->picture, 16, true, false, '', false);
127 $this->content->text .= get_string('guestuser').'</div>';
129 } else {
130 $this->content->text .= '<div class="user"><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$COURSE->id.'" title="'.$timeago.'">';
131 $this->content->text .= print_user_picture($user->id, $COURSE->id, $user->picture, 16, true, false, '', false);
132 $this->content->text .= $user->fullname.'</a></div>';
134 if (!empty($USER->id) and ($USER->id != $user->id) and !empty($CFG->messaging) and
135 !isguest() and $user->username != 'guest') { // Only when logged in and messaging active etc
136 $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);">'
137 .'<img class="iconsmall" src="'.$CFG->pixpath.'/t/message.gif" alt="'. get_string('messageselectadd') .'" /></a></div>';
139 $this->content->text .= "</li>\n";
141 $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
142 } else {
143 $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
146 return $this->content;