See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 39 and 401]
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 /** 18 * File containing onlineusers class. 19 * 20 * @package block_online_users 21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace block_online_users; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Class used to list and count online users 31 * 32 * @package block_online_users 33 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class fetcher { 37 38 /** @var string The SQL query for retrieving a list of online users */ 39 public $sql; 40 /** @var string The SQL query for counting the number of online users */ 41 public $csql; 42 /** @var string The params for the SQL queries */ 43 public $params; 44 45 /** 46 * Class constructor 47 * 48 * @param int $currentgroup The group (if any) to filter on 49 * @param int $now Time now 50 * @param int $timetoshowusers Number of seconds to show online users 51 * @param context $context Context object used to generate the sql for users enrolled in a specific course 52 * @param bool $sitelevel Whether to check online users at site level. 53 * @param int $courseid The course id to check 54 */ 55 public function __construct($currentgroup, $now, $timetoshowusers, $context, $sitelevel = true, $courseid = null) { 56 $this->set_sql($currentgroup, $now, $timetoshowusers, $context, $sitelevel, $courseid); 57 } 58 59 /** 60 * Store the SQL queries & params for listing online users 61 * 62 * @param int $currentgroup The group (if any) to filter on 63 * @param int $now Time now 64 * @param int $timetoshowusers Number of seconds to show online users 65 * @param context $context Context object used to generate the sql for users enrolled in a specific course 66 * @param bool $sitelevel Whether to check online users at site level. 67 * @param int $courseid The course id to check 68 */ 69 protected function set_sql($currentgroup, $now, $timetoshowusers, $context, $sitelevel, $courseid) { 70 global $USER, $DB, $CFG; 71 72 $timefrom = 100 * floor(($now - $timetoshowusers) / 100); // Round to nearest 100 seconds for better query cache. 73 74 $groupmembers = ""; 75 $groupselect = ""; 76 $groupby = ""; 77 $lastaccess = ", lastaccess"; 78 $timeaccess = ", ul.timeaccess AS lastaccess"; 79 $uservisibility = ""; 80 $uservisibilityselect = ""; 81 if ($CFG->block_online_users_onlinestatushiding) { 82 $uservisibility = ", up.value AS uservisibility"; 83 $uservisibilityselect = "AND (" . $DB->sql_cast_char2int('up.value') . " = 1 84 OR up.value IS NULL 85 OR u.id = :userid)"; 86 } 87 $params = array(); 88 89 $userfieldsapi = \core_user\fields::for_userpic()->including('username', 'deleted'); 90 $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects; 91 92 // Add this to the SQL to show only group users. 93 if ($currentgroup !== null) { 94 $groupmembers = ", {groups_members} gm"; 95 $groupselect = "AND u.id = gm.userid AND gm.groupid = :currentgroup"; 96 $groupby = "GROUP BY $userfields"; 97 $lastaccess = ", MAX(u.lastaccess) AS lastaccess"; 98 $timeaccess = ", MAX(ul.timeaccess) AS lastaccess"; 99 if ($CFG->block_online_users_onlinestatushiding) { 100 $uservisibility = ", MAX(up.value) AS uservisibility"; 101 } 102 $params['currentgroup'] = $currentgroup; 103 } 104 105 $params['now'] = $now; 106 $params['timefrom'] = $timefrom; 107 $params['userid'] = $USER->id; 108 $params['name'] = 'block_online_users_uservisibility'; 109 110 if ($sitelevel) { 111 $sql = "SELECT $userfields $lastaccess $uservisibility 112 FROM {user} u $groupmembers 113 LEFT JOIN {user_preferences} up ON up.userid = u.id 114 AND up.name = :name 115 WHERE u.lastaccess > :timefrom 116 AND u.lastaccess <= :now 117 AND u.deleted = 0 118 $uservisibilityselect 119 $groupselect $groupby 120 ORDER BY lastaccess DESC "; 121 122 $csql = "SELECT COUNT(u.id) 123 FROM {user} u $groupmembers 124 LEFT JOIN {user_preferences} up ON up.userid = u.id 125 AND up.name = :name 126 WHERE u.lastaccess > :timefrom 127 AND u.lastaccess <= :now 128 AND u.deleted = 0 129 $uservisibilityselect 130 $groupselect"; 131 } else { 132 // Course level - show only enrolled users for now. 133 // TODO: add a new capability for viewing of all users (guests+enrolled+viewing). 134 list($esqljoin, $eparams) = get_enrolled_sql($context); 135 $params = array_merge($params, $eparams); 136 137 $sql = "SELECT $userfields $timeaccess $uservisibility 138 FROM {user_lastaccess} ul $groupmembers, {user} u 139 JOIN ($esqljoin) euj ON euj.id = u.id 140 LEFT JOIN {user_preferences} up ON up.userid = u.id 141 AND up.name = :name 142 WHERE ul.timeaccess > :timefrom 143 AND u.id = ul.userid 144 AND ul.courseid = :courseid 145 AND ul.timeaccess <= :now 146 AND u.deleted = 0 147 $uservisibilityselect 148 $groupselect $groupby 149 ORDER BY lastaccess DESC"; 150 151 $csql = "SELECT COUNT(u.id) 152 FROM {user_lastaccess} ul $groupmembers, {user} u 153 JOIN ($esqljoin) euj ON euj.id = u.id 154 LEFT JOIN {user_preferences} up ON up.userid = u.id 155 AND up.name = :name 156 WHERE ul.timeaccess > :timefrom 157 AND u.id = ul.userid 158 AND ul.courseid = :courseid 159 AND ul.timeaccess <= :now 160 AND u.deleted = 0 161 $uservisibilityselect 162 $groupselect"; 163 164 $params['courseid'] = $courseid; 165 } 166 $this->sql = $sql; 167 $this->csql = $csql; 168 $this->params = $params; 169 } 170 171 /** 172 * Get a list of the most recent online users 173 * 174 * @param int $userlimit The maximum number of users that will be returned (optional, unlimited if not set) 175 * @return array 176 */ 177 public function get_users($userlimit = 0) { 178 global $DB; 179 $users = $DB->get_records_sql($this->sql, $this->params, 0, $userlimit); 180 return $users; 181 } 182 183 /** 184 * Count the number of online users 185 * 186 * @return int 187 */ 188 public function count_users() { 189 global $DB; 190 return $DB->count_records_sql($this->csql, $this->params); 191 } 192 193 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body