-
Notifications
You must be signed in to change notification settings - Fork 0
/
moulinette.php
200 lines (158 loc) · 6.44 KB
/
moulinette.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
class DB extends PDO {
public function __construct () {
try { parent::__construct('mysql'.':host=localhost; dbname=emploidutemps; charset=utf8', 'root', 'root', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC)); }
catch (PDOException $e) { DB::meurt('__construct', $e); }
}
public function execute($s, $p) {
try { return $s->execute($p); }
catch (PDOException $e) { DB::meurt('execute', $e); }
}
public function exec($p) {
try { return parent::exec($p); }
catch (PDOException $e) { DB::meurt('exec', $e); }
}
public function query($p) {
try { return parent::query($p); }
catch (PDOException $e) { DB::meurt('query', $e); }
}
public function request($req, $args = array(), $types = array()) {
try {
$query = parent::prepare($req);
if ($args != array()) {
foreach ($args as $key => $arg) {
if (isset($types[$key+1]))
$query->bindParam($key+1, $args[$key], $types[$key+1]);
else
$query->bindParam($key+1, $args[$key]);
}
}
$query->execute();
return $query;
}
catch (PDOException $e) { DB::meurt('request', $e); }
}
private static function meurt($type, PDOException $e) {
echo $e->getMessage();
}
}
function insertSalle($salle, $type, $jour, $debut, $fin) {
$debutDispo = array(8 => '08:00', 9 => '09:00', 10 => '10:15', 11 => '11:15', 12 => '12:15', 13 => '13:15', 14 => '14:15', 15 => '15:15', 16 => '16:30', 17 => '17:30', 18=> '18:30', 19 => '19:30');
$finDispo = array(8 => '08:00', 9 => '09:00', 10 => '10:00', 11 => '11:15', 12 => '12:15', 13 => '13:15', 14 => '14:15', 15 => '15:15', 16 => '16:15', 17 => '17:30', 18 => '18:30', 19 => '19:30', 20 => '20:30', 21 => '21:00');
$debutArray = array_map('intval', explode(':', $debut, 2));
$debut = round(($debutArray[0] * 60 + $debutArray[1]) / 60);
$finArray = array_map('intval', explode(':', $fin, 2));
$fin = floor(($finArray[0] * 60 + $finArray[1]) / 60);
$ecart = $fin - $debut;
if ($ecart >= 1 && $debut < 20) {
$insert = $GLOBALS['db']->prepare('INSERT INTO uvs_rooms(room, type, day, begin, end, gap) VALUES(?, ?, ?, ?, ?, ?)');
$GLOBALS['db']->execute($insert, array($salle, $type, $jour, $debutDispo[$debut], $finDispo[$fin], $ecart));
}
}
function insertSalles() {
$query = $GLOBALS['db']->prepare('SELECT room, type FROM uvs WHERE room != "" AND type != "T" GROUP BY room');
$GLOBALS['db']->execute($query, array());
$salles = $query->fetchAll();
$query = $GLOBALS['db']->prepare('SELECT begin, end FROM uvs WHERE room = ? AND day = ? ORDER BY begin, end');
foreach ($salles as $salle) {
for ($jour = 0; $jour < 5; $jour++) { // On compte que la semaine, le week-end on considère tout fermé
$debutDispo = '08:00';
$finDispo = '21:00';
$GLOBALS['db']->execute($query, array($salle['room'], $jour));
if ($query->rowCount() == 0) {
$insert = $GLOBALS['db']->prepare('INSERT INTO uvs_rooms(room, type, day, begin, end, gap) VALUES(?, ?, ?, ?, ?, ?)');
$GLOBALS['db']->execute($insert, array($salle['room'], $salle['type'], $jour, '00:00', '24:00', 24));
}
else {
$infos = $query->fetchAll();
foreach ($infos as $info) {
insertSalle($salle['room'], $salle['type'], $jour, $debutDispo, $info['begin']);
$debutDispo = $info['end'];
}
$fin = $info['end'][0] * 60 + $info['end'][1];
insertSalle($salle['room'], $salle['type'], $jour, $infos[count($infos) - 1]['end'], $finDispo);
}
}
}
}
$colors = array('#7DC779', '#82A1CA', '#F2D41F', '#457293', '#AB7AC6', '#DF6F53', '#B0CEE9', '#AAAAAA', '#1C704E');
function getARandomColor() {
return $GLOBALS['colors'][mt_rand(1, count($GLOBALS['colors'])) - 1];
}
$db = new DB();
$personne = include('off.php');
$days = ['LUNDI', 'MARDI', 'MERCREDI', 'JEUDI', 'VENDREDI', 'SAMEDI', 'DIMANCHE'];
$nbr = count($personne);
foreach ($personne as $key => $elem) {
$login = $elem['login'];
try {
$uvs = json_decode(file_get_contents("$WS_DSI?login=$login"));
$s = array_unique(array_map(function ($uv) {
return $uv->uv;
}, $uvs));
$c = count($s);
echo "Get $key/$nbr: $login with $c uvs\n";
} catch (Exception $e) {
echo "Error for $login\n";
}
if (count($uvs)) {
$ginger = json_decode(file_get_contents("$LOVELY_GINGER/$login?key=$KEY"));
$db->request('INSERT INTO students(login, surname, firstname, email, semester, uvs, nbrUV) VALUES(?, ?, ?, ?, "N/A", ?, ?)', [
$login, $ginger->nom, $ginger->prenom, $ginger->mail, implode(', ', $s), count($s)
]);
}
foreach ($uvs as $uv) {
$name = $uv->uv;
switch ($uv->type) {
case 'Cours':
$type = 'C';
break;
case 'TD':
$type = 'D';
break;
case 'TP':
$type = 'P';
break;
}
$day = \array_search($uv->day, $days);
$room = $uv->room;
preg_match('/Groupe ?(\d+)( \(semaine ([A-Z])\))?/', $uv->group, $matches);
$group = $matches[1];
if (count($matches) === 4) {
$week = $matches[3];
$frequency = 2;
} else {
$week = null;
$frequency = 1;
}
$r = $db->request('SELECT * FROM uvs WHERE uv = ? AND type = ? AND uvs.group = ?', [
$name, $type, $group,
]);
if ($r->rowCount() === 0) {
$db->request('INSERT INTO uvs(uv, type, group, day, begin, end, room, frequency, week) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [
$name, $type, $group, $day, $uv->begin, $uv->end, $room, $frequency, $week
]);
$r = $db->request('SELECT * FROM uvs WHERE uv = ? AND type = ? AND uvs.group = ?', [
$name, $type, $group,
]);
$id = $r->fetch()['id'];
$r = $db->request('SELECT * FROM uvs_colors WHERE uv = ?', [
$name
]);
if ($r->rowCount() === 0) {
$db->request('INSERT INTO uvs_colors(uv, color) VALUES(?, ?)', [
$name, getARandomColor()
]);
}
} else {
$fetch = $r->fetch();
$id = $fetch['id'];
$db->request('UPDATE uvs SET nbrEtu = ? WHERE id = ?', [$fetch['nbrEtu'] + 1, $id]);
}
$db->request('INSERT INTO uvs_followed(idUV, login, color, enabled, exchanged) VALUES(?, ?, null, 1, 0)', [
$id, $login
]);
}
}
echo "Inserting rooms\n";
insertSalles();