Skip to content

Commit 5dba6fb

Browse files
committed
Revert "issue 195: fix SQL for checking recent logins"
This reverts commit 96c2e42. Pushed in error. Should be a in a pull request.
1 parent 96c2e42 commit 5dba6fb

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

cli/clean.php

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

1717
/**
18-
* CLI script to perform a data wash.
19-
*
2018
* @package local_datacleaner
2119
* @copyright 2015 Brendan Heywood <[email protected]>
2220
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -30,7 +28,7 @@
3028

3129
// Now get cli options.
3230
list($options, $unrecognized) = cli_get_params(
33-
[
31+
array(
3432
'help' => false,
3533
'force' => false,
3634
'filter' => false,
@@ -40,11 +38,11 @@
4038
'dryrun' => false,
4139
'verbose' => false,
4240
'reset' => false,
43-
],
44-
[
41+
),
42+
array(
4543
'h' => 'help',
4644
'v' => 'verbose',
47-
]
45+
)
4846
);
4947

5048
if ($unrecognized) {

cli/lib.php

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

1717
/**
18-
* Library functions for data cleaner.
19-
*
20-
* @package local_datacleaner
18+
* @package cleaner
2119
* @copyright 2015 Catalyst IT
2220
* @author Nigel Cunningham <[email protected]>
2321
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2422
*/
2523

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

3226
/**
3327
* Print a message to the terminal.
@@ -95,33 +89,43 @@ function safety_checks($dryrun) {
9589
$minutes = $timetoshowusers / 60;
9690
$now = time();
9791
$timefrom = $now - $timetoshowusers; // Unlike original code, don't care about caches for this.
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++;
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";
117122
}
118-
$message .= "\n";
119-
}
120123

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;
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+
}
125129
}
126130

127131
// 3. Has cron run recently?

0 commit comments

Comments
 (0)