timeline: if a section is set to hidden and the user is not capable of editing a...
[moodle-blog-course-format.git] / blocks / online_users / block_online_users.php
blobdfa1b92927efc6bbef36b9543db1f33bfd29e0ee
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 = 2007101510;
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 if (empty($this->instance->pinned)) {
41 $blockcontext = get_context_instance(CONTEXT_BLOCK, $this->instance->id);
42 } else {
43 $blockcontext = get_context_instance(CONTEXT_SYSTEM); // pinned blocks do not have own context
46 //Calculate if we are in separate groups
47 $isseparategroups = ($COURSE->groupmode == SEPARATEGROUPS
48 && $COURSE->groupmodeforce
49 && !has_capability('moodle/site:accessallgroups', $context));
51 //Get the user current group
52 $currentgroup = $isseparategroups ? groups_get_course_group($COURSE) : NULL;
54 $groupmembers = "";
55 $groupselect = "";
56 $rafrom = "";
57 $rawhere = "";
59 //Add this to the SQL to show only group users
60 if ($currentgroup !== NULL) {
61 $groupmembers = ", {$CFG->prefix}groups_members gm ";
62 $groupselect = " AND u.id = gm.userid AND gm.groupid = '$currentgroup'";
65 if ($COURSE->id == SITEID) { // Site-level
66 $select = "SELECT u.id, u.username, u.firstname, u.lastname, u.picture, max(u.lastaccess) as lastaccess ";
67 $from = "FROM {$CFG->prefix}user u
68 $groupmembers ";
69 $where = "WHERE u.lastaccess > $timefrom
70 $groupselect ";
71 $order = "ORDER BY lastaccess DESC ";
73 } else { // Course-level
74 if (!has_capability('moodle/role:viewhiddenassigns', $context)) {
75 $pcontext = get_related_contexts_string($context);
76 $rafrom = ", {$CFG->prefix}role_assignments ra";
77 $rawhere = " AND ra.userid = u.id AND ra.contextid $pcontext AND ra.hidden = 0";
80 $courseselect = "AND ul.courseid = '".$COURSE->id."'";
81 $select = "SELECT u.id, u.username, u.firstname, u.lastname, u.picture, max(ul.timeaccess) as lastaccess ";
82 $from = "FROM {$CFG->prefix}user_lastaccess ul,
83 {$CFG->prefix}user u
84 $groupmembers $rafrom ";
85 $where = "WHERE ul.timeaccess > $timefrom
86 AND u.id = ul.userid
87 AND ul.courseid = $COURSE->id
88 $groupselect $rawhere ";
89 $order = "ORDER BY lastaccess DESC ";
92 $groupby = "GROUP BY u.id, u.username, u.firstname, u.lastname, u.picture ";
94 //Calculate minutes
95 $minutes = floor($timetoshowusers/60);
97 // Verify if we can see the list of users, if not just print number of users
98 if (!has_capability('block/online_users:viewlist', $blockcontext)) {
99 if (!$usercount = count_records_sql("SELECT COUNT(DISTINCT(u.id)) $from $where")) {
100 $usercount = get_string("none");
102 $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
103 return $this->content;
106 $SQL = $select . $from . $where . $groupby . $order;
108 if ($users = get_records_sql($SQL, 0, 50)) { // We'll just take the most recent 50 maximum
109 foreach ($users as $user) {
110 $users[$user->id]->fullname = fullname($user);
112 } else {
113 $users = array();
116 if (count($users) < 50) {
117 $usercount = "";
118 } else {
119 $usercount = count_records_sql("SELECT COUNT(DISTINCT(u.id)) $from $where");
120 $usercount = ": $usercount";
123 $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes)."$usercount)</div>";
125 //Now, we have in users, the list of users to show
126 //Because they are online
127 if (!empty($users)) {
128 //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
129 //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
130 $this->content->text .= "<ul class='list'>\n";
131 if (!empty($USER->id) && has_capability('moodle/site:sendmessage', $context)
132 && !empty($CFG->messaging) && !isguest()) {
133 $canshowicon = true;
134 } else {
135 $canshowicon = false;
137 foreach ($users as $user) {
138 $this->content->text .= '<li class="listentry">';
139 $timeago = format_time(time() - $user->lastaccess); //bruno to calculate correctly on frontpage
140 if ($user->username == 'guest') {
141 $this->content->text .= '<div class="user">'.print_user_picture($user->id, $COURSE->id, $user->picture, 16, true, false, '', false);
142 $this->content->text .= get_string('guestuser').'</div>';
144 } else {
145 $this->content->text .= '<div class="user"><a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$COURSE->id.'" title="'.$timeago.'">';
146 $this->content->text .= print_user_picture($user->id, $COURSE->id, $user->picture, 16, true, false, '', false);
147 $this->content->text .= $user->fullname.'</a></div>';
149 if ($canshowicon and ($USER->id != $user->id) and $user->username != 'guest') { // Only when logged in and messaging active etc
150 $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);">'
151 .'<img class="iconsmall" src="'.$CFG->pixpath.'/t/message.gif" alt="'. get_string('messageselectadd') .'" /></a></div>';
153 $this->content->text .= "</li>\n";
155 $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
156 } else {
157 $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
160 return $this->content;