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 = time()-$timetoshowusers;
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_current_group($COURSE->id
) : 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 .= 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,
77 if ($pusers = get_records_sql($SQL, 0, 20)) {
78 foreach ($pusers as $puser) {
79 $puser->fullname
= fullname($puser);
80 $users[$puser->id
] = $puser;
85 $minutes = floor($timetoshowusers/60);
87 $this->content
->text
= "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes).")</div>";
89 //Now, we have in users, the list of users to show
90 //Because they are online
92 //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
93 //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
94 $this->content
->text
.= "<ul class='list'>\n";
95 foreach ($users as $user) {
96 $this->content
->text
.= '<li class="listentry">';
97 $timeago = format_time(time() - max($user->timeaccess
, $user->lastaccess
)); //bruno to calculate correctly on frontpage
98 if ($user->username
== 'guest') {
99 $this->content
->text
.= '<div class="user">'.print_user_picture($user->id
, $COURSE->id
, $user->picture
, 16, true, false, '', false);
100 $this->content
->text
.= get_string('guestuser').'</div>';
103 $this->content
->text
.= '<div class="user"><a href="'.$CFG->wwwroot
.'/user/view.php?id='.$user->id
.'&course='.$COURSE->id
.'" title="'.$timeago.'">';
104 $this->content
->text
.= print_user_picture($user->id
, $COURSE->id
, $user->picture
, 16, true, false, '', false);
105 $this->content
->text
.= $user->fullname
.'</a></div>';
107 if (!empty($USER->id
) and ($USER->id
!= $user->id
) and !empty($CFG->messaging
) and
108 !isguest() and $user->username
!= 'guest') { // Only when logged in and messaging active etc
109 $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);">'
110 .'<img class="iconsmall" src="'.$CFG->pixpath
.'/t/message.gif" alt="'. get_string('messageselectadd') .'" /></a></div>';
112 $this->content
->text
.= "</li>\n";
114 $this->content
->text
.= '</ul><div class="clearer"><!-- --></div>';
116 $this->content
->text
.= "<div class=\"info\">".get_string("none")."</div>";
119 return $this->content
;