Skip to content

Commit 09f4d9e

Browse files
authored
Merge pull request #199 from catalyst/issue-195-fix-sql-for-master-branch
issue 195: fix SQL for checking recent logins
2 parents 5dba6fb + a2708d6 commit 09f4d9e

File tree

2 files changed

+34
-41
lines changed

2 files changed

+34
-41
lines changed

cli/clean.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18+
* CLI script to perform a data wash.
19+
*
1820
* @package local_datacleaner
1921
* @copyright 2015 Brendan Heywood <brendan@catalyst-au.net>
2022
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -28,7 +30,7 @@
2830

2931
// Now get cli options.
3032
list($options, $unrecognized) = cli_get_params(
31-
array(
33+
[
3234
'help' => false,
3335
'force' => false,
3436
'filter' => false,
@@ -38,11 +40,11 @@
3840
'dryrun' => false,
3941
'verbose' => false,
4042
'reset' => false,
41-
),
42-
array(
43+
],
44+
[
4345
'h' => 'help',
4446
'v' => 'verbose',
45-
)
47+
]
4648
);
4749

4850
if ($unrecognized) {

cli/lib.php

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
/**
18-
* @package cleaner
18+
* Library functions for data cleaner.
19+
*
20+
* @package local_datacleaner
1921
* @copyright 2015 Catalyst IT
2022
* @author Nigel Cunningham <nigelc@catalyst-au.net>
2123
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2224
*/
2325

24-
defined('MOODLE_INTERNAL') || die();
2526

2627
/**
2728
* Print a message to the terminal.
@@ -89,43 +90,33 @@ function safety_checks($dryrun) {
8990
$minutes = $timetoshowusers / 60;
9091
$now = time();
9192
$timefrom = $now - $timetoshowusers; // Unlike original code, don't care about caches for this.
92-
$params = array('now' => $now, 'timefrom' => $timefrom);
93-
94-
$csql = "SELECT COUNT(u.id)
95-
FROM {user} u
96-
WHERE u.lastaccess > :timefrom
97-
AND u.lastaccess <= :now
98-
AND u.deleted = 0";
99-
100-
if ($DB->count_records_sql($csql, $params)) {
101-
$namefields = "u." . implode(', u.', get_all_user_name_fields());
102-
103-
$sql = "SELECT u.id, u.username, {$namefields}
104-
FROM {user} u
105-
WHERE u.lastaccess > :timefrom
106-
AND u.lastaccess <= :now
107-
AND u.deleted = 0
108-
GROUP BY u.id
109-
ORDER BY lastaccess DESC ";
110-
$users = $DB->get_records_sql($sql, $params);
111-
112-
$message = "The following users have logged in within the last {$minutes} minutes:\n";
113-
$nonadmins = 0;
114-
foreach ($users as $user) {
115-
$message .= ' - ' . fullname($user) . ' (' . $user->username . ')';
116-
if (is_siteadmin($user)) {
117-
$message .= ' (siteadmin)';
118-
} else {
119-
$nonadmins++;
120-
}
121-
$message .= "\n";
93+
$params = ['timefrom' => $timefrom];
94+
95+
$namefields = "u." . implode(', u.', \core_user\fields::get_name_fields());
96+
97+
$sql = "SELECT u.id, u.username, {$namefields}
98+
FROM {user} u
99+
WHERE u.lastaccess > :timefrom
100+
AND u.deleted = 0
101+
ORDER BY lastaccess DESC ";
102+
$users = $DB->get_records_sql($sql, $params);
103+
104+
$message = "The following users have logged in within the last {$minutes} minutes:\n";
105+
$nonadmins = 0;
106+
foreach ($users as $user) {
107+
$message .= ' - ' . fullname($user) . ' (' . $user->username . ')';
108+
if (is_siteadmin($user)) {
109+
$message .= ' (siteadmin)';
110+
} else {
111+
$nonadmins++;
122112
}
113+
$message .= "\n";
114+
}
123115

124-
if ($nonadmins) {
125-
abort_message($abort, $message);
126-
abort_message("There are non site-administrators in the list of recent users.", true);
127-
$willdie = true;
128-
}
116+
if ($nonadmins) {
117+
abort_message($abort, $message);
118+
abort_message($abort, "There are non site-administrators in the list of recent users.", true);
119+
$willdie = true;
129120
}
130121

131122
// 3. Has cron run recently?

0 commit comments

Comments
 (0)