-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroup-maker.php
79 lines (64 loc) · 1.6 KB
/
group-maker.php
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
#!/usr/bin/env php
<?php
/*
* This file is part of the exam-order package.
*
* (c) Benjamin Georgeault
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
if (3 !== count($argv)) {
echo 'This script need only 2 arguments:';
echo "\n";
echo 'Usage: php group-maker.php [perGroupNumber] [seed]';
echo "\n";
exit;
}
if (2 > $perGroupNumber = (int) $argv[1]) {
throw new RuntimeException(sprintf(
'Set more then 1 person per group.'
));
}
$seed = (int) hexdec($argv[2]);
mt_srand($seed);
if (!file_exists($file = __DIR__ . '/names.json')) {
throw new RuntimeException(sprintf(
'The file "%s" does not exist.',
$file
));
}
if (false === $string = file_get_contents($file)) {
throw new RuntimeException(sprintf(
'Cannot read content from file "%s"',
$file
));
}
$json = json_decode($string, true);
foreach ($json as $key => $name) {
if (!is_string($name)) {
throw new RuntimeException(sprintf(
'The name given in file "%s" at position %d is not a string.',
$file,
$key
));
}
}
$namesOrder = [];
for ($i = 0; $i < count($json); $i++) {
$namesOrder[] = rand();
}
asort($namesOrder);
$groupCounter = 0;
foreach (array_keys($namesOrder) as $i => $order) {
if (
0 === $i % $perGroupNumber &&
($i !== count($namesOrder) - 1)
) {
if ($i !== 0) {
echo "\n";
}
echo 'Group n°' . ++$groupCounter . ":\n";
}
echo $json[$order] . "\n";
}