Example #1
0
 function get_content()
 {
     global $USER, $CFG, $DB, $OUTPUT;
     if ($this->content !== NULL) {
         return $this->content;
     }
     $this->content = new stdClass();
     $this->content->text = '';
     $this->content->footer = '';
     if (empty($this->instance)) {
         return $this->content;
     }
     $timetoshowusers = 300;
     //Seconds default
     if (isset($CFG->block_online_users_timetosee)) {
         $timetoshowusers = $CFG->block_online_users_timetosee * 60;
     }
     $now = time();
     //Calculate if we are in separate groups
     $isseparategroups = $this->page->course->groupmode == SEPARATEGROUPS && $this->page->course->groupmodeforce && !has_capability('moodle/site:accessallgroups', $this->page->context);
     //Get the user current group
     $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL;
     $sitelevel = $this->page->course->id == SITEID || $this->page->context->contextlevel < CONTEXT_COURSE;
     $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $this->page->context, $sitelevel, $this->page->course->id);
     //Calculate minutes
     $minutes = floor($timetoshowusers / 60);
     // Verify if we can see the list of users, if not just print number of users
     if (!has_capability('block/online_users:viewlist', $this->page->context)) {
         if (!($usercount = $onlineusers->count_users())) {
             $usercount = get_string("none");
         }
         $this->content->text = "<div class=\"info\">" . get_string("periodnminutes", "block_online_users", $minutes) . ": {$usercount}</div>";
         return $this->content;
     }
     $userlimit = 50;
     // We'll just take the most recent 50 maximum.
     if ($users = $onlineusers->get_users($userlimit)) {
         foreach ($users as $user) {
             $users[$user->id]->fullname = fullname($user);
         }
     } else {
         $users = array();
     }
     $usercount = $onlineusers->count_users();
     $usercount = ": {$usercount}";
     $this->content->text = "<div class=\"info\">(" . get_string("periodnminutes", "block_online_users", $minutes) . "{$usercount})</div>";
     //Now, we have in users, the list of users to show
     //Because they are online
     if (!empty($users)) {
         //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
         //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
         $this->content->text .= "<ul class='list'>\n";
         if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context) && !empty($CFG->messaging) && !isguestuser()) {
             $canshowicon = true;
         } else {
             $canshowicon = false;
         }
         foreach ($users as $user) {
             $this->content->text .= '<li class="listentry">';
             $timeago = format_time($now - $user->lastaccess);
             //bruno to calculate correctly on frontpage
             if (isguestuser($user)) {
                 $this->content->text .= '<div class="user">' . $OUTPUT->user_picture($user, array('size' => 16, 'alttext' => false));
                 $this->content->text .= get_string('guestuser') . '</div>';
             } else {
                 $this->content->text .= '<div class="user">';
                 $this->content->text .= '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&amp;course=' . $this->page->course->id . '" title="' . $timeago . '">';
                 $this->content->text .= $OUTPUT->user_picture($user, array('size' => 16, 'alttext' => false, 'link' => false)) . $user->fullname . '</a></div>';
             }
             if ($canshowicon and $USER->id != $user->id and !isguestuser($user)) {
                 // Only when logged in and messaging active etc
                 $anchortagcontents = '<img class="iconsmall" src="' . $OUTPUT->pix_url('t/message') . '" alt="' . get_string('messageselectadd') . '" />';
                 $anchorurl = new moodle_url('/message/index.php', array('id' => $user->id));
                 $anchortag = html_writer::link($anchorurl, $anchortagcontents, array('title' => get_string('messageselectadd')));
                 $this->content->text .= '<div class="message">' . $anchortag . '</div>';
             }
             $this->content->text .= "</li>\n";
         }
         $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
     } else {
         $this->content->text .= "<div class=\"info\">" . get_string("none") . "</div>";
     }
     return $this->content;
 }
Example #2
0
 /**
  * Check logged in at the site level (should be 12).
  *
  * @param int $now Current Unix timestamp
  * @param int $timetoshowusers The time window (in seconds) to check for the latest logged in users
  */
 public function test_fetcher_sitelevel()
 {
     global $CFG;
     $currentgroup = null;
     $now = time();
     $timetoshowusers = $CFG->block_online_users_timetosee * 60;
     $context = context_system::instance();
     $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $context, true);
     $usercount = $onlineusers->count_users();
     $users = $onlineusers->get_users();
     $this->assertEquals($usercount, count($users), 'There was a problem counting the number of online users at site level');
     $this->assertEquals(12, $usercount, 'There was a problem counting the number of online users at site level');
 }