Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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 $userfields = \user_picture::fields('u', array('username', 'deleted')); 90 91 // Add this to the SQL to show only group users. 92 if ($currentgroup !== null) { 93 $groupmembers = ", {groups_members} gm"; 94 $groupselect = "AND u.id = gm.userid AND gm.groupid = :currentgroup"; 95 $groupby = "GROUP BY $userfields"; 96 $lastaccess = ", MAX(u.lastaccess) AS lastaccess"; 97 $timeaccess = ", MAX(ul.timeaccess) AS lastaccess"; 98 if ($CFG->block_online_users_onlinestatushiding) { 99 $uservisibility = ", MAX(up.value) AS uservisibility"; 100 } 101 $params['currentgroup'] = $currentgroup; 102 } 103 104 $params['now'] = $now; 105 $params['timefrom'] = $timefrom; 106 $params['userid'] = $USER->id; 107 $params['name'] = 'block_online_users_uservisibility'; 108 109 if ($sitelevel) { 110 $sql = "SELECT $userfields $lastaccess $uservisibility 111 FROM {user} u $groupmembers 112 LEFT JOIN {user_preferences} up ON up.userid = u.id 113 AND up.name = :name 114 WHERE u.lastaccess > :timefrom 115 AND u.lastaccess <= :now 116 AND u.deleted = 0 117 $uservisibilityselect 118 $groupselect $groupby 119 ORDER BY lastaccess DESC "; 120 121 $csql = "SELECT COUNT(u.id) 122 FROM {user} u $groupmembers 123 LEFT JOIN {user_preferences} up ON up.userid = u.id 124 AND up.name = :name 125 WHERE u.lastaccess > :timefrom 126 AND u.lastaccess <= :now 127 AND u.deleted = 0 128 $uservisibilityselect 129 $groupselect"; 130 } else { 131 // Course level - show only enrolled users for now. 132 // TODO: add a new capability for viewing of all users (guests+enrolled+viewing). 133 list($esqljoin, $eparams) = get_enrolled_sql($context); 134 $params = array_merge($params, $eparams); 135 136 $sql = "SELECT $userfields $timeaccess $uservisibility 137 FROM {user_lastaccess} ul $groupmembers, {user} u 138 JOIN ($esqljoin) euj ON euj.id = u.id 139 LEFT JOIN {user_preferences} up ON up.userid = u.id 140 AND up.name = :name 141 WHERE ul.timeaccess > :timefrom 142 AND u.id = ul.userid 143 AND ul.courseid = :courseid 144 AND ul.timeaccess <= :now 145 AND u.deleted = 0 146 $uservisibilityselect 147 $groupselect $groupby 148 ORDER BY lastaccess DESC"; 149 150 $csql = "SELECT COUNT(u.id) 151 FROM {user_lastaccess} ul $groupmembers, {user} u 152 JOIN ($esqljoin) euj ON euj.id = u.id 153 LEFT JOIN {user_preferences} up ON up.userid = u.id 154 AND up.name = :name 155 WHERE ul.timeaccess > :timefrom 156 AND u.id = ul.userid 157 AND ul.courseid = :courseid 158 AND ul.timeaccess <= :now 159 AND u.deleted = 0 160 $uservisibilityselect 161 $groupselect"; 162 163 $params['courseid'] = $courseid; 164 } 165 $this->sql = $sql; 166 $this->csql = $csql; 167 $this->params = $params; 168 } 169 170 /** 171 * Get a list of the most recent online users 172 * 173 * @param int $userlimit The maximum number of users that will be returned (optional, unlimited if not set) 174 * @return array 175 */ 176 public function get_users($userlimit = 0) { 177 global $DB; 178 $users = $DB->get_records_sql($this->sql, $this->params, 0, $userlimit); 179 return $users; 180 } 181 182 /** 183 * Count the number of online users 184 * 185 * @return int 186 */ 187 public function count_users() { 188 global $DB; 189 return $DB->count_records_sql($this->csql, $this->params); 190 } 191 192 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body