MDL-8857
[moodle-linuxchix.git] / blocks / online_users / block_online_users.php
blobccac195c53689f9a3c602b14143a8e1466820c93
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 = 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;
48 $groupmembers = "";
49 $groupselect = "";
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
58 $courseselect = '';
59 $timeselect = "AND (ul.timeaccess > $timefrom OR u.lastaccess > $timefrom)";
60 } else {
61 $courseselect = "AND ul.courseid = '".$COURSE->id."'";
62 $timeselect = "AND ul.timeaccess > $timefrom";
65 $users = array();
67 $SQL = "SELECT u.id, u.username, u.firstname, u.lastname, u.picture, u.lastaccess, ul.timeaccess
68 FROM {$CFG->prefix}user_lastaccess ul,
69 {$CFG->prefix}user u
70 $groupmembers
71 WHERE
72 ul.userid = u.id
73 $courseselect
74 $timeselect
75 $groupselect ";
77 if ($pusers = get_records_sql($SQL, 0, 20)) {
78 foreach ($pusers as $puser) {
79 $puser->fullname = fullname($puser);
80 $users[$puser->id] = $puser;
84 //Calculate minutes
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
91 if (!empty($users)) {
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>';
102 } else {
103 $this->content->text .= '<div class="user"><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;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>';
115 } else {
116 $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
119 return $this->content;