MDL-22054 converting block name strings to pluginname
[moodle.git] / blocks / online_users / block_online_users.php
CommitLineData
4ca6cfbf 1<?php
0f3fe4b6 2
01199ea9 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 */
e89d741a 8class block_online_users extends block_base {
9b4b78fd 9 function init() {
4bbe3bd8 10 $this->title = get_string('pluginname','block_online_users');
283ebb71 11 $this->version = 2007101510;
89adb174 12 }
13
0f3fe4b6 14 function has_config() {return true;}
15
0f3fe4b6 16 function get_content() {
6b608f8f 17 global $USER, $CFG, $DB, $OUTPUT;
0f3fe4b6 18
675dbdd9 19 if ($this->content !== NULL) {
0f3fe4b6 20 return $this->content;
21 }
22
9b4b78fd 23 $this->content = new stdClass;
5ef50b77 24 $this->content->text = '';
25 $this->content->footer = '';
283ebb71 26
9b4b78fd 27 if (empty($this->instance)) {
675dbdd9 28 return $this->content;
29 }
01199ea9 30
076cc57c 31 $timetoshowusers = 300; //Seconds default
32 if (isset($CFG->block_online_users_timetosee)) {
33 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
34 }
97e7e624 35 $timefrom = 100 * floor((time()-$timetoshowusers) / 100); // Round to nearest 100 seconds for better query cache
0f3fe4b6 36
c7756c73 37 //Calculate if we are in separate groups
cb640229 38 $isseparategroups = ($this->page->course->groupmode == SEPARATEGROUPS
39 && $this->page->course->groupmodeforce
e92c286c 40 && !has_capability('moodle/site:accessallgroups', $this->page->context));
0f3fe4b6 41
c7756c73 42 //Get the user current group
cb640229 43 $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL;
0f3fe4b6 44
c7756c73 45 $groupmembers = "";
283ebb71 46 $groupselect = "";
47 $rafrom = "";
48 $rawhere = "";
f28f2d90 49 $params = array();
0f3fe4b6 50
c7756c73 51 //Add this to the SQL to show only group users
52 if ($currentgroup !== NULL) {
f28f2d90 53 $groupmembers = ", {groups_members} gm";
54 $groupselect = "AND u.id = gm.userid AND gm.groupid = :currentgroup";
55 $params['currentgroup'] = $currentgroup;
c7756c73 56 }
2d90bdf0 57
4f0c2d00
PS
58 $userfields = user_picture::fields('u').', username';
59
cb640229 60 if ($this->page->course->id == SITEID) { // Site-level
4f0c2d00 61 $sql = "SELECT $userfields, MAX(u.lastaccess) AS lastaccess
f28f2d90 62 FROM {user} u $groupmembers
63 WHERE u.lastaccess > $timefrom
64 $groupselect
4f0c2d00 65 GROUP BY $userfields
f28f2d90 66 ORDER BY lastaccess DESC ";
283ebb71 67
68 $csql = "SELECT COUNT(u.id), u.id
69 FROM {user} u $groupmembers
70 WHERE u.lastaccess > $timefrom
71 $groupselect
72 GROUP BY u.id";
73
4f0c2d00
PS
74 } else {
75 // Course level - show only enrolled users for now
76 // TODO: add a new capability for viewing of all users (guests+enrolled+viewing)
77
78 list($esqljoin, $eparams) = get_enrolled_sql($this->page->context);
79 $params = array_merge($params, $eparams);
283ebb71 80
4f0c2d00 81 $sql = "SELECT $userfields, MAX(ul.timeaccess) AS lastaccess
283ebb71 82 FROM {user_lastaccess} ul, {user} u $groupmembers $rafrom
4f0c2d00 83 JOIN ($esqljoin) euj ON euj.id = u.id
f28f2d90 84 WHERE ul.timeaccess > $timefrom
85 AND u.id = ul.userid
86 AND ul.courseid = :courseid
283ebb71 87 $groupselect $rawhere
4f0c2d00 88 GROUP BY $userfields
f28f2d90 89 ORDER BY lastaccess DESC";
283ebb71 90
91 $csql = "SELECT u.id
92 FROM {user_lastaccess} ul, {user} u $groupmembers $rafrom
4f0c2d00 93 JOIN ($esqljoin) euj ON euj.id = u.id
283ebb71 94 WHERE ul.timeaccess > $timefrom
95 AND u.id = ul.userid
96 AND ul.courseid = :courseid
97 $groupselect $rawhere
98 GROUP BY u.id";
99
cb640229 100 $params['courseid'] = $this->page->course->id;
c7756c73 101 }
dd97c328 102
283ebb71 103 //Calculate minutes
104 $minutes = floor($timetoshowusers/60);
dd97c328 105
283ebb71 106 // Verify if we can see the list of users, if not just print number of users
e92c286c 107 if (!has_capability('block/online_users:viewlist', $this->page->context)) {
283ebb71 108 if (!$usercount = $DB->count_records_sql($csql, $params)) {
109 $usercount = get_string("none");
110 }
111 $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
112 return $this->content;
113 }
dd97c328 114
283ebb71 115 if ($users = $DB->get_records_sql($sql, $params, 0, 50)) { // We'll just take the most recent 50 maximum
116 foreach ($users as $user) {
117 $users[$user->id]->fullname = fullname($user);
174526f4 118 }
283ebb71 119 } else {
120 $users = array();
121 }
122
123 if (count($users) < 50) {
124 $usercount = "";
125 } else {
126 $usercount = $DB->count_records_sql($csql, $params);
127 $usercount = ": $usercount";
128 }
0f3fe4b6 129
283ebb71 130 $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes)."$usercount)</div>";
0f3fe4b6 131
132 //Now, we have in users, the list of users to show
133 //Because they are online
076cc57c 134 if (!empty($users)) {
450a0a7d 135 //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
136 //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
137 $this->content->text .= "<ul class='list'>\n";
4f0c2d00 138 if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context)
4cdb8d70 139 && !empty($CFG->messaging) && !isguestuser()) {
59eaa9ac 140 $canshowicon = true;
141 } else {
142 $canshowicon = false;
143 }
0f3fe4b6 144 foreach ($users as $user) {
450a0a7d 145 $this->content->text .= '<li class="listentry">';
283ebb71 146 $timeago = format_time(time() - $user->lastaccess); //bruno to calculate correctly on frontpage
4ca6cfbf 147
9d5210af 148 if ($user->username == 'guest') {
812dbaf7 149 $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16));
440d1648 150 $this->content->text .= get_string('guestuser').'</div>';
9d5210af 151
152 } else {
cb640229 153 $this->content->text .= '<div class="user"><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->page->course->id.'" title="'.$timeago.'">';
812dbaf7 154 $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16));
440d1648 155 $this->content->text .= $user->fullname.'</a></div>';
9d5210af 156 }
59eaa9ac 157 if ($canshowicon and ($USER->id != $user->id) and $user->username != 'guest') { // Only when logged in and messaging active etc
440d1648 158 $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);">'
b5d0cafc 159 .'<img class="iconsmall" src="'.$OUTPUT->pix_url('t/message') . '" alt="'. get_string('messageselectadd') .'" /></a></div>';
4b22e391 160 }
73bb9da7 161 $this->content->text .= "</li>\n";
0f3fe4b6 162 }
440d1648 163 $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
0f3fe4b6 164 } else {
440d1648 165 $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
0f3fe4b6 166 }
167
168 return $this->content;
169 }
170}
171
4ca6cfbf 172