block_online_users.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Online users block.
  18. *
  19. * @package block_online_users
  20. * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. use block_online_users\fetcher;
  24. /**
  25. * This block needs to be reworked.
  26. * The new roles system does away with the concepts of rigid student and
  27. * teacher roles.
  28. */
  29. class block_online_users extends block_base {
  30. function init() {
  31. $this->title = get_string('pluginname','block_online_users');
  32. }
  33. function has_config() {
  34. return true;
  35. }
  36. function get_content() {
  37. global $USER, $CFG, $DB, $OUTPUT, $PAGE;
  38. if ($this->content !== NULL) {
  39. return $this->content;
  40. }
  41. $this->content = new stdClass;
  42. $this->content->text = '';
  43. $this->content->footer = '';
  44. if (empty($this->instance)) {
  45. return $this->content;
  46. }
  47. $timetoshowusers = 300; //Seconds default
  48. if (isset($CFG->block_online_users_timetosee)) {
  49. $timetoshowusers = $CFG->block_online_users_timetosee * 60;
  50. }
  51. $now = time();
  52. //Calculate if we are in separate groups
  53. $isseparategroups = ($this->page->course->groupmode == SEPARATEGROUPS
  54. && $this->page->course->groupmodeforce
  55. && !has_capability('moodle/site:accessallgroups', $this->page->context));
  56. //Get the user current group
  57. $currentgroup = $isseparategroups ? groups_get_course_group($this->page->course) : NULL;
  58. $sitelevel = $this->page->course->id == SITEID || $this->page->context->contextlevel < CONTEXT_COURSE;
  59. $onlineusers = new fetcher($currentgroup, $now, $timetoshowusers, $this->page->context,
  60. $sitelevel, $this->page->course->id);
  61. //Calculate minutes
  62. $minutes = floor($timetoshowusers/60);
  63. // Verify if we can see the list of users, if not just print number of users
  64. if (!has_capability('block/online_users:viewlist', $this->page->context)) {
  65. if (!$usercount = $onlineusers->count_users()) {
  66. $usercount = get_string("none");
  67. }
  68. $this->content->text = "<div class=\"info\">".get_string("periodnminutes","block_online_users",$minutes).": $usercount</div>";
  69. return $this->content;
  70. }
  71. $userlimit = 50; // We'll just take the most recent 50 maximum.
  72. if ($users = $onlineusers->get_users($userlimit)) {
  73. foreach ($users as $user) {
  74. $users[$user->id]->fullname = fullname($user);
  75. }
  76. } else {
  77. $users = array();
  78. }
  79. $usercount = $onlineusers->count_users();
  80. $usercount = ": $usercount";
  81. $this->content->text = "<div class=\"info\">(".get_string("periodnminutes","block_online_users",$minutes)."$usercount)</div>";
  82. //Now, we have in users, the list of users to show
  83. //Because they are online
  84. if (!empty($users)) {
  85. //Accessibility: Don't want 'Alt' text for the user picture; DO want it for the envelope/message link (existing lang string).
  86. //Accessibility: Converted <div> to <ul>, inherit existing classes & styles.
  87. $this->content->text .= "<ul class='list'>\n";
  88. if (isloggedin() && has_capability('moodle/site:sendmessage', $this->page->context)
  89. && !empty($CFG->messaging) && !isguestuser()) {
  90. $canshowicon = true;
  91. message_messenger_requirejs();
  92. } else {
  93. $canshowicon = false;
  94. }
  95. foreach ($users as $user) {
  96. $this->content->text .= '<li class="listentry">';
  97. $timeago = format_time($now - $user->lastaccess); //bruno to calculate correctly on frontpage
  98. if (isguestuser($user)) {
  99. $this->content->text .= '<div class="user">'.$OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false));
  100. $this->content->text .= get_string('guestuser').'</div>';
  101. } else {
  102. $this->content->text .= '<div class="user">';
  103. $this->content->text .= '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$this->page->course->id.'" title="'.$timeago.'">';
  104. $this->content->text .= $OUTPUT->user_picture($user, array('size'=>16, 'alttext'=>false, 'link'=>false)) .$user->fullname.'</a></div>';
  105. }
  106. if ($canshowicon and ($USER->id != $user->id) and !isguestuser($user)) { // Only when logged in and messaging active etc
  107. $anchortagcontents = '<img class="iconsmall" src="'.$OUTPUT->pix_url('t/message') . '" alt="'. get_string('messageselectadd') .'" />';
  108. $anchorurl = new moodle_url('/message/index.php', array('id' => $user->id));
  109. $anchortag = html_writer::link($anchorurl, $anchortagcontents, array_merge(
  110. message_messenger_sendmessage_link_params($user),
  111. array('title' => get_string('messageselectadd'))
  112. ));
  113. $this->content->text .= '<div class="message">'.$anchortag.'</div>';
  114. }
  115. $this->content->text .= "</li>\n";
  116. }
  117. $this->content->text .= '</ul><div class="clearer"><!-- --></div>';
  118. } else {
  119. $this->content->text .= "<div class=\"info\">".get_string("none")."</div>";
  120. }
  121. return $this->content;
  122. }
  123. }