-
Notifications
You must be signed in to change notification settings - Fork 1
/
lectio.php
340 lines (286 loc) · 12.2 KB
/
lectio.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/*
Uofficielt API til Lectio
Baseret på simple_html_dom.php
Lavet af Henrik Pedersen og Daniel Poulsen
Opdateret og vedligeholdt af Krede
*/
class lectio {
const LECTIO_URL = "https://www.lectio.dk/lectio/";
function __construct($path = 'simple_html_dom.php') {
if (is_file($path) && !class_exists('simple_html_dom')) {
require_once($path);
}
}
private function get_html($url) {
$options = array(
'http' => array(
'method' => "GET",
'header' => "Accept-language: en\r\n".
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13\r\n"
)
);
$context = stream_context_create($options);
$html = file_get_contents($url, false, $context);
if ($html == null) {
return "Lectio Error: Couldn't load";
}
return $html;
}
private function nice_string($text) {
return trim(str_replace("'", "'", $text));
}
private function get_list($url, $rex) {
$list = array();
$html = $this->get_html($url);
$dom = new simple_html_dom();
$dom->load($html);
$anchors = $dom->find('a');
foreach ($anchors as $anchor) {
$href = $anchor->getAttribute("href");
if (preg_match($rex, $href, $matches)) { // Regex
$name = trim(html_entity_decode($anchor->plaintext));
$list[$name] = $matches[1];
}
}
ksort( $list, SORT_NATURAL ); // Sorter liste
return $list;
}
public function get_schools() {
$list = $this->get_list(self::LECTIO_URL."login_list.aspx", "/\/lectio\/(\d*)\/default\.aspx/");
return $list;
}
public function get_classes($id) {
$list = $this->get_list(self::LECTIO_URL.$id."/FindSkema.aspx?type=stamklasse", "/\/lectio\/\d*\/SkemaNy\.aspx\?type=stamklasse&klasseid=(\d*)/");
return $list;
}
public function get_teachers($id) {
$list = $this->get_list(self::LECTIO_URL.$id."/FindSkema.aspx?type=laerer", "/\/lectio\/\d*\/SkemaNy\.aspx\?type=laerer&laererid=(\d*)/");
return $list;
}
public function get_students_page($url) {
$list = $this->get_list($url, "/\/lectio\/\d*\/SkemaNy\.aspx\?type=elev&elevid=(\d*)/");
return $list;
}
public function get_students($id) {
$url = self::LECTIO_URL.$id.'/FindSkema.aspx?type=elev&forbogstav=';
$students = array();
$alphabet = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'Æ', 'Ø', 'Å', '?');
foreach ($alphabet as $key) {
$students = array_merge($students, $this->get_students_page($url.$key));
}
ksort( $students, SORT_NATURAL ); // Sorter eleverne da de kan været placeret tilfældigt
return $students;
}
public function get_rooms($id) {
$list = $this->get_list(self::LECTIO_URL.$id."/FindSkema.aspx?type=lokale", "/\/lectio\/\d*\/SkemaNy\.aspx\?type=lokale&nosubnav=1&id=(\d*)/");
return $list;
}
private function is_room_empty($url, $time) {
$html = $this->get_html($url);
$dom = new simple_html_dom();
$dom->load($html);
$day = null;
$headers = $dom->find('.s2dayHeader td'); // Vi starter iterationer i 1 fordi vi springer den første over (altid tom)
$max = count($headers);
for ($i = 1; $i < $max; $i++) {
if (preg_match("/[a-z]* \(".date('j\\\/n', $time)."\)/i", $headers[$i]->plaintext)) {
$day = $i;
break;
}
}
if ($day != null) {
$collection = $dom->find('.s2skemabrikcontainer');
$dom->load($collection[$day + $max - 1]->innertext); // Skip noter
$day_info = $dom->find('.s2skemabrik'); // Søg efter alle skemabrikkerne. Indeholder modul information
for ($i = 0; $i < count($day_info); $i++) {
$info = $day_info[$i]->getAttribute('data-additionalinfo');
if (substr($info, 0, 7) == "Aflyst!") continue; // Modulet er aflyst, fjern
if (preg_match("/(\d\d:\d\d) til (\d\d:\d\d)/", $info, $matches)) {
$start = strtotime($matches[1], $time);
$end = strtotime($matches[2], $time);
if ($time >= $start && $time <= $end) {
return false;
}
}
}
}
return true;
}
public function get_empty_rooms($id, $t = null) { // Kan caches, men ødelægger selve ideen med funktionen
date_default_timezone_set('Europe/Copenhagen');
$time = isset($t) ? $t : time();
$list = $this->get_rooms($id);
$rooms = array();
$week = date('WY', $time);
foreach ($list as $name => $room) {
$url = self::LECTIO_URL.$id."/SkemaNy.aspx?type=lokale&nosubnav=1&id=".$room."&week=".$week;
$empty = $this->is_room_empty($url, $time);
if ($empty) {
$rooms[$name] = $room;
}
}
return $rooms;
}
public function get_schedule($url, $date = null) {
date_default_timezone_set('Europe/Copenhagen');
$schedule = array();
$schedule['title'] = '';
$schedule['week'] = 0;
$schedule['year'] = 0;
$schedule['day'] = '';
$schedule['weekdays'] = array();
$schedule['schedule'] = array();
$schedule['dayschedule'] = array();
$finalrex = "(?=Aktiviteten har en præsentation.|Lærere:|Lærer:|Lokale:|Lokaler:|Note:|Lektier:|Øvrigt indhold:|$)";
$html = $this->get_html($url);
$dom = new simple_html_dom();
$dom->load($html);
$schedule['title'] = $dom->find('.s2weekHeader td', 0)->plaintext;
if (preg_match("/Uge (\d*) - (\d*)/", $schedule['title'], $matches)) { // Find uge og år
$schedule['week'] = floatval($matches[1]);
$schedule['year'] = floatval($matches[2]);
}
$headers = $dom->find('.s2dayHeader td'); // Vi starter iterationer i 1 fordi vi springer den første over (altid tom)
for ($i = 1; $i < count($headers); $i++) {
$name = $headers[$i]->plaintext;
$schedule['weekdays'][] = $name;
$schedule['schedule'][$name] = array(
'notes' => array(),
'lessons' => array()
);
}
$collection = $dom->find('.s2skemabrikcontainer');
$info_container = new simple_html_dom(); // Definer den her for genbrugens skyld da den tømmes efter hver iteration
$max = count($schedule['weekdays']);
for ($i = 0; $i < $max; $i++) { // Iterer alle noterne i "toppen" af ugedagene
$info_container->load($collection[$i]->innertext);
$noter = $info_container->find('.s2skemabrikcontent');
foreach($noter as $note) {
$info = trim(html_entity_decode($note->plaintext));
$info = preg_replace("/\s+/u", " ", $info);
$schedule['schedule'][$schedule['weekdays'][$i]]['notes'][] = $info; // Tilføj til liste over noter for dagen
}
}
for ($i = 0; $i < $max; $i++) {
$info_container->load($collection[$i + $max + 1]->innertext); // Skip noter + 1
$day_info = $info_container->find('.s2skemabrik'); // Søg efter alle skemabrikkerne. Indeholder modul information
$day = $schedule['weekdays'][$i];
$lessons = array();
for ($y = 0; $y < count($day_info); $y++) {
$info = preg_replace('/\s+/u', ' ', $day_info[$y]->getAttribute('data-additionalinfo'));
if (substr($info, 0, 7) == "Aflyst!") continue; // Modulet er aflyst, fjern
$keyname = 'NA:NA';
$changed = false;
$presentation = false;
$time = array();
$title = '';
$team = '';
$teachers = array();
$room = array();
$homework = array();
$additional = array();
$note = '';
if (substr($info, 0, 8) == "Ændret!") { // Status
$changed = true;
$info = substr($info, 8);
}
if (preg_match("/Aktiviteten har en præsentation./", $info, $matches)) { // Præsentation
$presentation = true;
}
if (preg_match("/(\d\d:\d\d) til (\d\d:\d\d)/", $info, $matches)) { // Find tid
$keyname = $matches[1];
$time = array($matches[1], $matches[2]);
}
if (preg_match("/(?<=Hold:)(.*?)".$finalrex."/", $info, $matches)) { // Hold
$team = $this->nice_string($matches[1]);
}
if (preg_match("/(\d*\/\d*-\d*)/", $info, $matches) && strpos($info, $matches[1])) { // Titel
$title = $this->nice_string(substr($info, 0, strpos($info, $matches[1])));
}
if (preg_match("/Lærer: (.*?) \((\S*)\)/", $info, $matches)) { // En lærer
$teachers[] = $matches[2];
} elseif (preg_match("/(?<=Lærere:)(.*?)".$finalrex."/", $info, $matches)) { // Flere lærere
foreach (explode(', ', $matches[1]) as $name) {
$teachers[] = $this->nice_string($name);
}
}
if (preg_match("/(?<=Lokale:|Lokaler:)(.*?)".$finalrex."/", $info, $matches)) { // Lokaler
foreach (explode(', ', $matches[1]) as $name) {
if (trim($name) == "") continue;
$room[] = $this->nice_string($name);
}
}
if (preg_match("/(?<=Lektier:)(.*?)".$finalrex."/", $info, $matches)) { // Lektier
foreach (explode('[...]', $matches[1]) as $name) {
if (trim($name) == "") continue;
$homework[] = $this->nice_string(substr($name, 2));
}
}
if (preg_match("/(?<=Øvrigt indhold:)(.*?)".$finalrex."/", $info, $matches)) { // Øvrigt indhold
foreach (explode('[...]', $matches[1]) as $name) {
if (trim($name) == "") continue;
$additional[] = $this->nice_string(substr($name, 2));
}
}
if (preg_match("/(?<=Note:)(.*?)".$finalrex."/", $info, $matches)) { // Note
$note = $this->nice_string($matches[1]);
}
$key = $keyname;
$in = 0;
while (isset($lessons[$key])) { // Brikken findes allerede, inkrementer for ikke at override
$in++;
$key = $keyname.'('.$in.')';
}
$lessons[$key] = array(
'changed' => $changed,
'presentation' => $presentation,
'time' => $time,
'title' => $title,
'team' => $team,
'teachers' => $teachers,
'classroom' => $room,
'homework' => $homework,
'additional' => $additional,
'note' => $note,
'all' => $info
);
}
ksort($lessons, SORT_NATURAL); // Sorter dagene da brikkerne kan være placeret tilfældigt
foreach ($lessons as $lesson) {
$schedule['schedule'][$day]['lessons'][] = $lesson;
}
}
if ($date != null) {
foreach ($schedule['schedule'] as $day => $sched) {
if (preg_match("/[a-z]* \(".$date."\)/i", $day)) {
$schedule['day'] = $day;
$schedule['dayschedule'] = $sched;
break;
}
}
}
return $schedule;
}
public function get_schedule_class($id, $lectio_id, $t = null) {
date_default_timezone_set('Europe/Copenhagen');
$time = isset($t) ? $t : time();
$url = 'https://www.lectio.dk/lectio/'.$id.'/SkemaNy.aspx?type=stamklasse&klasseid='.$lectio_id.'&week='.date('WY', $time);
return $this->get_schedule($url, date('j\\\/n', $time));
}
public function get_schedule_student($id, $lectio_id, $t = null) {
date_default_timezone_set('Europe/Copenhagen');
$time = isset($t) ? $t : time();
$url = 'https://www.lectio.dk/lectio/'.$id.'/SkemaNy.aspx?type=elev&elevid='.$lectio_id.'&week='.date('WY', $time);
return $this->get_schedule($url, date('j\\\/n', $time));
}
public function get_schedule_teacher($id, $lectio_id, $t = null) {
date_default_timezone_set('Europe/Copenhagen');
$time = isset($t) ? $t : time();
$url = 'https://www.lectio.dk/lectio/'.$id.'/SkemaNy.aspx?type=laerer&laererid='.$lectio_id.'&week='.date('WY', $time);
return $this->get_schedule($url, date('j\\\/n', $time));
}
}
?>