Skip to content

Commit 96c2e42

Browse files
committed
issue 195: fix SQL for checking recent logins
1 parent 77c99ab commit 96c2e42

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

cli/clean.php

+6-4
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

+32-36
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@
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

26+
<<<<<<< HEAD
2427
defined('MOODLE_INTERNAL') || die();
28+
=======
29+
use local_datacleaner\clean;
30+
>>>>>>> ce6bb45 (issue 195: coding style fixes)
2531

2632
/**
2733
* Print a message to the terminal.
@@ -89,43 +95,33 @@ function safety_checks($dryrun) {
8995
$minutes = $timetoshowusers / 60;
9096
$now = time();
9197
$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";
98+
$params = ['timefrom' => $timefrom];
99+
100+
$namefields = "u." . implode(', u.', \core_user\fields::get_name_fields());
101+
102+
$sql = "SELECT u.id, u.username, {$namefields}
103+
FROM {user} u
104+
WHERE u.lastaccess > :timefrom
105+
AND u.deleted = 0
106+
ORDER BY lastaccess DESC ";
107+
$users = $DB->get_records_sql($sql, $params);
108+
109+
$message = "The following users have logged in within the last {$minutes} minutes:\n";
110+
$nonadmins = 0;
111+
foreach ($users as $user) {
112+
$message .= ' - ' . fullname($user) . ' (' . $user->username . ')';
113+
if (is_siteadmin($user)) {
114+
$message .= ' (siteadmin)';
115+
} else {
116+
$nonadmins++;
122117
}
118+
$message .= "\n";
119+
}
123120

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-
}
121+
if ($nonadmins) {
122+
abort_message($abort, $message);
123+
abort_message($abort, "There are non site-administrators in the list of recent users.", true);
124+
$willdie = true;
129125
}
130126

131127
// 3. Has cron run recently?

0 commit comments

Comments
 (0)