MDL-11515:
[moodle-linuxchix.git] / blocks / online_users / block_online_users.php
blob06bec16b732e8b4db1f10da717e55666e3d94708
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 = 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 foreach ($pusers as $puser) {
88 // if current user can't view hidden role assignment in this context and
89 // user has a hidden role assigned at this context or any parent contexts,
90 // ignore this user
92 $SQL = "SELECT id,id FROM {$CFG->prefix}role_assignments
93 WHERE userid = $puser->id
94 AND contextid $pcontext
95 AND hidden = 1";
97 if (!has_capability('moodle/role:viewhiddenassigns', $context) && record_exists_sql($SQL)) {
98 // can't see this user as the current user has no capability
99 // and this user has a hidden assignment at this context or higher
100 continue;
103 $puser->fullname = fullname($puser);
104 $users[$puser->id] = $puser;
108 //Calculate minutes
109 $minutes = floor($timetoshowusers/60);
111 $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes).")</div>";
113 //Now, we have in users, the list of users to show
114 //Because they are online
115 if (!empty($users)) {
116 //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
117 //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
118 $this->content->text .= "<ul class='list'>\n";
119 foreach ($users as $user) {
120 $this->content->text .= '<li class="listentry">';
121 $timeago = format_time(time() - $user->lastaccess); //bruno to calculate correctly on frontpage
122 if ($user->username == 'guest') {
123 $this->content->text .= '<div class="user">'.print_user_picture($user->id, $COURSE->id, $user->picture, 16, true, false, '', false);
124 $this->content->text .= get_string('guestuser').'</div>';
126 } else {
127 $this->content->text .= '<div class="user"><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$COURSE->id.'" title="'.$timeago.'">';
128 $this->content->text .= print_user_picture($user->id, $COURSE->id, $user->picture, 16, true, false, '', false);
129 $this->content->text .= $user->fullname.'</a></div>';
131 if (!empty($USER->id) and ($USER->id != $user->id) and !empty($CFG->messaging) and
132 !isguest() and $user->username != 'guest') { // Only when logged in and messaging active etc
133 $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);">'
134 .'<img class="iconsmall" src="'.$CFG->pixpath.'/t/message.gif" alt="'. get_string('messageselectadd') .'" /></a></div>';
136 $this->content->text .= "</li>\n";
138 $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
139 } else {
140 $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
143 return $this->content;