-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_check_users.php
More file actions
147 lines (119 loc) · 4.83 KB
/
Copy pathscript_check_users.php
File metadata and controls
147 lines (119 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
// Compare students.csv (from students_from_xlsx.py) with the accounts that exist on the site.
// Read-only. Writes students_new.csv next to the input: the rows that are safe to feed to
// admin/tool/uploaduser/cli/uploaduser.php. Anything doubtful is reported and left out.
define('CLI_SCRIPT', true);
$configFile = file_exists(__DIR__ . '/config.php') ? __DIR__ . '/config.php' : getenv('MOODLE_ROOT') . '/config.php';
require_once($configFile);
/* -- Cli -- */
$file = $argv[1] ?? '';
if ($file === '' || !is_readable($file))
{
echo "Usage : sudo -u www-data php script_check_users.php students.csv\n";
echo "Colonnes attendues : username,email,firstname,lastname[,auth]\n";
exit(1);
}
/* -- Functions -- */
// Comparaison de noms insensible à la casse et aux accents (Schönmann = Schonmann).
function normalizeName($name)
{
$ascii = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $name);
return preg_replace('/[^a-z]/', '', core_text::strtolower($ascii !== false ? $ascii : $name));
}
function readCsv($file)
{
$fh = fopen($file, 'r');
$header = fgetcsv($fh, 0, ',', '"', '');
$header[0] = preg_replace('/^\xEF\xBB\xBF/', '', $header[0]); // BOM
$rows = [];
while (($line = fgetcsv($fh, 0, ',', '"', '')) !== false)
{
if (count($line) === 1 && $line[0] === null) continue;
$rows[] = array_combine($header, array_map('trim', $line));
}
fclose($fh);
return [$header, $rows];
}
/* -- Lookup -- */
list($header, $students) = readCsv($file);
foreach (['username', 'email', 'firstname', 'lastname'] as $required)
{
if (!in_array($required, $header))
{
echo "[ERREUR] colonne '{$required}' absente de {$file}\n";
exit(1);
}
}
// Tous les comptes actifs, indexés par email et par nom normalisé, pour repérer les homonymes.
$active = $DB->get_records_select('user', 'deleted = 0', [], 'id', 'id, username, email, firstname, lastname, auth, suspended');
$byEmail = [];
$byName = [];
foreach ($active as $u)
{
$byEmail[core_text::strtolower($u->email)] = $u;
$byName[normalizeName($u->lastname) . '|' . normalizeName($u->firstname)][] = $u;
}
$new = [];
$existing = [];
$arbitrate = [];
foreach ($students as $s)
{
$email = core_text::strtolower($s['email']);
$username = core_text::strtolower($s['username']);
$label = "{$s['lastname']} {$s['firstname']} <{$email}>";
$user = $DB->get_record('user', ['username' => $username, 'deleted' => 0])
?: ($byEmail[$email] ?? false);
if ($user)
{
$notes = [];
if ($user->suspended) $notes[] = 'compte suspendu';
if ($user->auth !== ($s['auth'] ?? $user->auth)) $notes[] = "auth={$user->auth}";
if (normalizeName($user->firstname) !== normalizeName($s['firstname']) || normalizeName($user->lastname) !== normalizeName($s['lastname']))
{
$notes[] = "nom Moodle : {$user->lastname} {$user->firstname}";
}
if ($user->suspended)
{
$arbitrate[] = "{$label} : existe (id {$user->id}) mais " . implode(', ', $notes);
}
else
{
$existing[] = "{$label} : id {$user->id}" . ($notes ? ' — ' . implode(', ', $notes) : '');
}
continue;
}
// Homonyme actif avec une autre adresse : jamais créé automatiquement.
$sameName = $byName[normalizeName($s['lastname']) . '|' . normalizeName($s['firstname'])] ?? [];
if ($sameName)
{
$others = array_map(fn($u) => "{$u->email} (id {$u->id}, {$u->auth})", $sameName);
$arbitrate[] = "{$label} : homonyme déjà présent avec une autre adresse : " . implode(', ', $others);
continue;
}
// Compte supprimé (Moodle garde la ligne avec deleted=1 et un email réécrit).
$deleted = $DB->get_records_select('user', 'deleted = 1 AND (username = :u OR ' . $DB->sql_like('email', ':e', false) . ')',
['u' => $username, 'e' => $email . '%']);
if ($deleted)
{
$ids = implode(', ', array_keys($deleted));
$arbitrate[] = "{$label} : un compte supprimé porte cette adresse (id {$ids}) — l'import en créera un nouveau ; vérifier que c'est voulu";
continue;
}
$new[] = $s;
}
/* -- Report -- */
echo "\n==== Nouveaux (" . count($new) . ") ====\n\n";
foreach ($new as $s) echo " {$s['lastname']} {$s['firstname']} <{$s['email']}>\n";
echo "\n==== Déjà présents (" . count($existing) . ") ====\n\n";
foreach ($existing as $line) echo " {$line}\n";
echo "\n==== À arbitrer (" . count($arbitrate) . ") — hors de students_new.csv ====\n\n";
foreach ($arbitrate as $line) echo " {$line}\n";
/* -- Save -- */
$out = dirname($file) . '/students_new.csv';
$fh = fopen($out, 'w');
fputcsv($fh, $header, ',', '"', '');
foreach ($new as $s) fputcsv($fh, array_values($s), ',', '"', '');
fclose($fh);
echo "\n" . count($new) . " lignes écrites dans {$out}\n";
echo "Import : sudo -u www-data php public/admin/tool/uploaduser/cli/uploaduser.php --file={$out} --delimiter_name=comma --uutype=0 --uuforcepasswordchange=0 --uupasswordnew=1\n";
exit($arbitrate ? 2 : 0);