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
= 2006030100;
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 ?
get_and_set_current_group($COURSE, groupmode($COURSE)) : NULL;
51 //Add this to the SQL to show only group users
52 if ($currentgroup !== NULL) {
53 $groupmembers = ', '.groups_members_from_sql(); //TODO: ", {$CFG->prefix}groups_members gm ";
54 $groupselect = ' AND '.groups_members_where_sql($currentgroup, 'u.id'); //" AND u.id = gm.userid AND gm.groupid = '$currentgroup'";
57 if ($COURSE->id
== SITEID
) { // Site-level
59 $timeselect = "AND (ul.timeaccess > $timefrom OR u.lastaccess > $timefrom)";
61 $courseselect = "AND ul.courseid = '".$COURSE->id
."'";
62 $timeselect = "AND ul.timeaccess > $timefrom";
67 $SQL = "SELECT u.id, u.username, u.firstname, u.lastname, u.picture, u.lastaccess, ul.timeaccess
68 FROM {$CFG->prefix}user_lastaccess ul,
76 GROUP BY u.id, u.username, u.firstname, u.lastname, u.picture, u.lastaccess, ul.timeaccess
77 ORDER BY ul.timeaccess DESC";
81 $pcontext = get_related_contexts_string($context);
83 if ($pusers = get_records_sql($SQL, 0, 50)) { // We'll just take the most recent 50 maximum
84 foreach ($pusers as $puser) {
86 // if current user can't view hidden role assignment in this context and
87 // user has a hidden role assigned at this context or any parent contexts,
90 $SQL = "SELECT id,id FROM {$CFG->prefix}role_assignments
91 WHERE userid = $puser->id
92 AND contextid $pcontext
95 if (!has_capability('moodle/role:viewhiddenassigns', $context) && record_exists_sql($SQL)) {
96 // can't see this user as the current user has no capability
97 // and this user has a hidden assignment at this context or higher
101 $puser->fullname
= fullname($puser);
102 $users[$puser->id
] = $puser;
107 $minutes = floor($timetoshowusers/60);
109 $this->content
->text
= "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes).")</div>";
111 //Now, we have in users, the list of users to show
112 //Because they are online
113 if (!empty($users)) {
114 //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
115 //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
116 $this->content
->text
.= "<ul class='list'>\n";
117 foreach ($users as $user) {
118 $this->content
->text
.= '<li class="listentry">';
119 $timeago = format_time(time() - max($user->timeaccess
, $user->lastaccess
)); //bruno to calculate correctly on frontpage
120 if ($user->username
== 'guest') {
121 $this->content
->text
.= '<div class="user">'.print_user_picture($user->id
, $COURSE->id
, $user->picture
, 16, true, false, '', false);
122 $this->content
->text
.= get_string('guestuser').'</div>';
125 $this->content
->text
.= '<div class="user"><a href="'.$CFG->wwwroot
.'/user/view.php?id='.$user->id
.'&course='.$COURSE->id
.'" title="'.$timeago.'">';
126 $this->content
->text
.= print_user_picture($user->id
, $COURSE->id
, $user->picture
, 16, true, false, '', false);
127 $this->content
->text
.= $user->fullname
.'</a></div>';
129 if (!empty($USER->id
) and ($USER->id
!= $user->id
) and !empty($CFG->messaging
) and
130 !isguest() and $user->username
!= 'guest') { // Only when logged in and messaging active etc
131 $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);">'
132 .'<img class="iconsmall" src="'.$CFG->pixpath
.'/t/message.gif" alt="'. get_string('messageselectadd') .'" /></a></div>';
134 $this->content
->text
.= "</li>\n";
136 $this->content
->text
.= '</ul><div class="clearer"><!-- --></div>';
138 $this->content
->text
.= "<div class=\"info\">".get_string("none")."</div>";
141 return $this->content
;