comparison index.php @ 15:45987abe0d10

Move beta code into production. :P
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 12 Jan 2011 01:03:36 +0200
parents beta.php@a2c61b010680
children ea9558465ad6
comparison
equal deleted inserted replaced
14:a2c61b010680 15:45987abe0d10
1 <? 1 <?
2 2 $pageCSS = array("http://tnsp.org/docs1.css", "luk.css");
3 $mapfile = "kartta.png"; 3 $pageCharset = "iso-8859-15";
4 $classfile = "luokkatilat.txt"; 4 $luokkaDefault = "TTE9SNO";
5 $cachefile = "coursecache.txt"; 5 $dataDir = "cache/";
6 $baseURI = "http://tnsp.org/luk/?"; 6 $classFile = "luokat.txt";
7 $lukuURI = "http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_"; 7 $mapFile = "kartta.png";
8 $roomFile = "luokkatilat.txt";
9 $cacheFile = "coursecache.txt";
10 $baseURI = "http://tnsp.org/luk/beta.php";
8 $infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&koodi1="; 11 $infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&koodi1=";
12 $dayNames = array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai");
13
14 $hourStamps = array(
15 array( 8, 15),
16 array( 9, 15),
17 array(10, 15),
18 array(11, 15),
19 array(12, 15),
20 array(13, 15),
21 array(14, 15),
22 array(15, 15),
23 array(16, 15),
24 array(17, 5),
25 array(18, 5),
26 array(19, 0),
27 array(20, 45),
28 );
29
30 // Include framework
31 require "mcommon.inc.php";
32 require "merrors.inc.php";
33
34
35 // Helper functions
36 function checkClassID(&$id)
37 {
38 global $luokkaDefault;
39 if (!preg_match("#^[A-Z]{3}\d\S+|ccr#", $id)) {
40 errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioavoa <b>".$luokkaDefault."</b>.");
41 $id = $luokkaDefault;
42 return FALSE;
43 } else
44 return TRUE;
45 }
46
9 47
10 // Check given parameters 48 // Check given parameters
11 $luokka = isset($_GET["luokka"]) ? $_GET["luokka"] : "TTE9SNO"; 49 if (isset($_GET["luokka"])) {
12 50 $luokka = $_GET["luokka"];
13 if (!preg_match("#^[A-Z]{3}\d\S+#", $luokka)) { 51 if (checkClassID($luokka) && !isset($_["nyt"])) {
14 echo "Virhe! Luokan t&auml;ytyy olla muotoa XXXnXXX.<br />\n"; 52 setcookie("lukluokka", $luokka, time() + 365*24*60*60); // expire in a year
15 exit; 53 }
16 } 54 } else
55 if (isset($_COOKIE["lukluokka"])) {
56 $luokka = $_COOKIE["lukluokka"];
57 checkClassID($luokka);
58 } else {
59 errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa <b>".$luokkaDefault."</b>.");
60 $luokka = $luokkaDefault;
61 }
62
17 63
18 if (isset($_GET["tila"])) { 64 if (isset($_GET["tila"])) {
19 $tila = $_GET["tila"]; 65 $tila = $_GET["tila"];
20 echo "Luokkatilan n&auml;ytt&ouml;moodi ei viel&auml; tuettu.<br />\n"; 66 echo "Luokkatilan n&auml;ytt&ouml;moodi ei viel&auml; tuettu.<br />\n";
21 exit; 67 exit;
22 68
23 $fp = @fopen($classfile, "rb"); 69 $fp = @fopen($roomFile, "rb");
24 if ($fp) { 70 if ($fp) {
25 fclose($fp); 71 fclose($fp);
26 } 72 }
27 } 73 }
28 74
29 75
30 // Global cache for course data 76 function getHour($hour)
31 $cache = array(); 77 {
32 $dirty = FALSE; 78 global $hourStamps;
33 79 $stamp = mktime($hourStamps[$hour][0], $hourStamps[$hour][1], 0);
34 80 return "<br />".date("H:i", $stamp)." - ".date("H:i", $stamp + 45 * 60)."<br /><br />";
35 // Try to read cachefile, if we can get file lock on it 81 }
36 $fp = @fopen($cachefile, "rb"); 82
37 if ($fp) { 83
38 if (flock($fp, LOCK_SH)) { 84 function matchCourse($id)
39 require($cachefile); 85 {
40 flock($fp, LOCK_UN); 86 global $cache, $infoURI, $cacheDirty;
41 }
42 fclose($fp);
43 }
44
45
46 function match_course($matches)
47 {
48 global $cache, $infoURI, $dirty;
49
50 $id = $matches[1];
51 87
52 // Check if course exists in cache 88 // Check if course exists in cache
53 if (!isset($cache[$id])) { 89 if (!isset($cache[$id])) {
54 $dirty = TRUE; 90 $cacheDirty = TRUE;
55 // Not cached, try to fetch data 91 // Not cached, try to fetch data
56 $data = @file_get_contents($infoURI.$id); 92 $data = @file_get_contents($infoURI.$id);
57 if ($data !== FALSE) { 93 if ($data !== FALSE) {
58 if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*op\s*</strong></td>#", $data, $m)) { 94 if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*op\s*</strong></td>#", $data, $m)) {
59 // Add data to cache 95 // Add data to cache
68 else 104 else
69 return htmlentities($id); 105 return htmlentities($id);
70 } 106 }
71 107
72 108
73 function match_class($matches) 109 function matchClass($matches)
74 { 110 {
75 global $baseURI; 111 global $baseURI;
76 return "<b><a href=\"".$baseURI."tila=".$matches[1]."\">".$matches[1]."</a></b> ".$matches[2]; 112 return "<b><a href=\"".$baseURI."?tila=".$matches[1]."\">".$matches[1]."</a></b> ".$matches[2];
77 } 113 }
78 114
79 115
80 /* Fetch HTML data and replace occurances of course IDs 116 // Global cache for course data
81 * with information and a link 117 $cache = array();
82 */ 118 $cacheDirty = FALSE;
83 if (file_exists("OR_".$luokka.".htm")) 119
84 $data = @file_get_contents("OR_".$luokka.".htm"); 120
121 // Try to read cachefile, if we can get file lock on it
122 $fp = @fopen($cacheFile, "rb");
123 if ($fp) {
124 if (flock($fp, LOCK_SH)) {
125 require($cacheFile);
126 flock($fp, LOCK_UN);
127 }
128 fclose($fp);
129 }
130
131 $fp = @fopen($classFile, "rb");
132 $classes = array();
133 if ($fp) {
134 if (flock($fp, LOCK_SH)) {
135 while (!feof($fp)) {
136 $str = trim(fgets($fp, 128));
137 if ($str[0] != "#" && strlen($str) > 2)
138 $classes[] = $str;
139 }
140 flock($fp, LOCK_UN);
141 }
142 fclose($fp);
143 }
144
145
146 if (!file_exists($dataDir.$luokka.".data")) {
147 errorMsg("Luokan ".htmlentities($luokka)." datatiedostoa ei löytynyt!");
148 $haveData = FALSE;
149 } else {
150 require($dataDir.$luokka.".data");
151 $haveData = isset($classDefs);
152 }
153
154
155 function printHourInfo($dayHours, $showDays)
156 {
157 $out = " <tr>\n <td>Tunteja<br />(<b>ryhmä/vv</b>)</td>\n";
158 for ($day = 0; $day < $showDays; $day++) {
159 $out .= " <td>".$dayHours[$day]["total"]."h (<b>".$dayHours[$day]["grouped"]."h</b>)</td>\n";
160 }
161 return $out." </tr>\n";
162 }
163
164
165 function findHours($start, $day, $cmp, $mark = FALSE)
166 {
167 global $classHourTable, $classInfo;
168 $n = 0;
169 for ($i = $start; $i < $classInfo["maxhours"]; $i++)
170 if ($classHourTable[$i][$day] == $cmp) {
171 if ($mark) $classHourTable[$i][$day] = -9999;
172 $n++;
173 } else
174 break;
175 return $n;
176 }
177
178
179 function findNextHour(&$j, &$i, $day, $hour, $chk)
180 {
181 global $classInfo, $classHourTable;
182 for ($j = $day; $j < $classInfo["maxdays"]; $j++)
183 for ($i = $hour; $i < $classInfo["maxhours"]; $i++) {
184 // echo "chk=$chk - i=$i / j=$j : ht=".$classHourTable[$i][$j]."<br />\n";
185 if (($chk > 0 && $classHourTable[$i][$j] != $chk) || $classHourTable[$i][$j] > 0)
186 return $classHourTable[$i][$j];
187 }
188
189 return -1;
190 }
191
192
193 function getHourInfo($id)
194 {
195 global $classDefs;
196
197 if (isset($id)) {
198 if ($id >= 0 && isset($classDefs[$id])) {
199 $i = $classDefs[$id];
200 $d = $i["data"];
201 $out = "";
202
203 $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]);
204 if ($isSplit) {
205 $out .= "<table>".
206 "<tr><td>".matchCourse($d[0])."</td><td>".matchCourse($d[1])."</td></tr>";
207 for ($j = 2; $j < count($d); $j += 2)
208 $out .= "<tr><td>".htmlentities($d[$j])."</td><td>".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."</td></tr>";
209 $out .= "</table>";
210 } else {
211 $out .= matchCourse($d[0])."<br />";
212 for ($j = 1; $j < count($d); $j++)
213 $out .= htmlentities($d[$j])."<br />";
214 }
215
216 return $out;
217 } else
218 return "<p>Ei tunteja.</p>";
219 } else
220 return "<p>Ei mitään.</p>";
221
222 return "";
223 }
224
225
226 if (isset($_GET["nyt"])) {
227 $aika = getdate();
228 $hour = $aika["hours"] - 8;
229 $day = $aika["wday"] - 1;
230 if ($day < 0) $day = 7;
231
232 $chk = $classHourTable[$hour][$day];
233
234 $out = "<h2>".$dayNames[$day]." ".$aika["mday"].".".$aika["mon"].".".$aika["year"]." - klo ".$aika["hours"].":".$aika["minutes"]."</h2>\n".
235 getHourInfo($chk)."\n";
236
237 // $out .= "hour=".$hour."/day=".$day."<br />\n";
238 if ($day >= $classInfo["maxdays"]) {
239 $day = $hour = 0;
240 $chk = -1;
241 } else
242 if ($hour < 0) {
243 $hour = 0;
244 $chk = -1;
245 } else
246 if ($hour >= $classInfo["maxhours"]) {
247 $day++;
248 $hour = 0;
249 $chk = -1;
250 }
251 // $out .= "hour=".$hour."/day=".$day."<br />\n";
252
253 $found = findNextHour($nday, $nhour, $day, $hour, $chk);
254
255 // $out .= "hour=".$nhour."/day=".$nday."/found=".$found."<br />\n";
256
257 if ($found > 0) {
258 $stamp = mktime(8 + $nhour, 15, 0, $aika["mon"], $aika["mday"] + $nday - $day, $aika["year"]);
259 $aika = getdate($stamp);
260 $hour = $aika["hours"] - 8;
261 $day = $aika["wday"] - 1;
262 if ($day < 0) $day = 7;
263 $out .= "<h2>Seuraavaksi: ".$dayNames[$nday]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n".
264 getHourInfo($found)."\n";
265 }
266 }
85 else 267 else
86 $data = @file_get_contents($lukuURI.$luokka.".htm"); 268 if ($haveData) {
87 269 $totalHours = 0;
88 if ($data !== FALSE) { 270 $totalGrouped = 0;
89 $data = preg_replace_callback("#<B>([A-Z]\d+)\.?</B>#", match_course, $data); 271 $dayHours = array();
90 272
91 $data = preg_replace_callback("#<B>(\d{4}|ATK\d+|[A-Z]{2,3}LAB\d+|[A-Z]{2,3}LAB|Audit\d+)(\(\d+\))?</B>#", match_class, $data); 273 for ($day = 0; $day < $classInfo["maxdays"]; $day++) {
92 274 $dayHours[$day]["total"] = $dayHours[$day]["grouped"] = 0;
93 $data = preg_replace("#<body class=tt>#", "<body class=tt>\n". 275
94 "<a href=\"".$lukuURI.$luokka.".htm\">[orig]</a>\n, Luokat: ". 276 for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) {
95 "<a href=\"".$baseURI."luokka=TTE0SNO"."\">[TTE0SNO]</a>\n". 277 $id = $classHourTable[$hour][$day];
96 "<a href=\"".$baseURI."luokka=TTE9SNO"."\">[TTE9SNO]</a>\n", $data); 278
97 echo $data; 279 if ($id > 0) {
98 } else { 280 $totalHours++;
99 echo "Luokan datatiedostoa <b>".htmlentities($lukuURI.$luokka.".htm")."</b> ei saatu.<br />\n"; 281 $dayHours[$day]["total"]++;
100 } 282
283 if ($classDefs[$id]["grouped"]) {
284 $totalGrouped++;
285 $dayHours[$day]["grouped"]++;
286 }
287 }
288 }
289 }
290
291 // Create the timetable table
292 $out =
293 "<p>".join("; ", $classInfo["general"])."</p>\n".
294 "<div>Viikossa yhteensä <b>".$totalHours."</b> tuntia, joista <b>".$totalGrouped."</b> ryhmissä tai vuoroviikoin.</div>\n".
295 "<table class=\"timetable\">".
296 " <tr>\n <th></th>\n";
297 for ($day = 0; $day < $classInfo["maxdays"]; $day++) {
298 $out .= " <th class=\"days\">".$dayNames[$day]."</th>\n";
299 }
300 $out .= " </tr>\n";
301
302 for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) {
303 $out .= " <tr>\n".
304 " <th class=\"hours\">".getHour($hour)."</th>\n";
305 for ($day = 0; $day < $classInfo["maxdays"]; $day++) {
306 if (isset($classHourTable[$hour][$day])) {
307 $h = $classHourTable[$hour][$day];
308
309 if ($h < 1) {
310 if ($h > -9999) {
311 $n = findHours($hour, $day, $h, TRUE);
312 $out .= " <td rowspan=\"".$n."\" class=\"clnothing\"></td>\n";
313 }
314 } else
315 if (isset($classDefs[$h])) {
316 if (!isset($classDefs[$h]["done"])) {
317 $classDefs[$h]["done"] = true;
318 $i = $classDefs[$h];
319 $d = $classDefs[$h]["data"];
320
321 $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]);
322
323 $hours = findHours($hour, $day, $h, FALSE);
324 if ($hours != $i["hours"]) {
325 errorMsg("Internal error, cell $hour / $day : hour id $h hours ($hours) do not match ".$i["hours"]."!");
326 }
327
328 $out .= " <td rowspan=\"".$hours.
329 "\" class=\"".($isSplit || $i["grouped"] ? "clgrouped" : "clnormal")."\">";
330
331 if ($isSplit) {
332 $out .= "<table>".
333 "<tr><td>".matchCourse($d[0])."</td><td>".matchCourse($d[1])."</td></tr>";
334 for ($j = 2; $j < count($d); $j += 2)
335 $out .= "<tr><td>".htmlentities($d[$j])."</td><td>".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."</td></tr>";
336 $out .= "</table>";
337 } else {
338 $out .= matchCourse($d[0])."<br />";
339 for ($j = 1; $j < count($d); $j++)
340 $out .= htmlentities($d[$j])."<br />";
341 }
342 $out .= "<div class=\"nhours\"><span>".$hours."h</span></div></td>\n";
343 }
344 } else
345 errorMsg("Internal error cell $hour / $day : hour id $h does not exist!");
346 } else
347 errorMsg("Internal error, cell $hour / $day does not exist.");
348 }
349 $out .= " </tr>\n";
350 }
351
352 $out .= printHourInfo($dayHours, $classInfo["maxdays"]).
353 "</table>\n";
354
355 } // haveData
356
357
358 $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka;
359 printPageHeader($pageTitle);
360 echo "<h1>".$pageTitle."</h1>\n";
361
362 echo "<form action=\"".$baseURI."\" method=\"get\">\n".
363 " <table>\n".
364 " <tr>\n".
365 " <td>\n".
366 " <select name=\"luokka\">\n";
367
368 foreach ($classes as $class) {
369 echo " <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".htmlentities($class)."</option>\n";
370 }
371 echo
372 " </select>\n".
373 " </td>\n".
374 " <td><input type=\"submit\" value=\" Vaihda \" /></td>\n".
375 " <td>[<a href=\"".$baseURI."?luokka=".$luokka."&amp;nyt\">Mini-info moodi</a>]</td>\n".
376 " </tr>\n".
377 " </table>\n".
378 "</form>\n";
379
380
381 // Show error messages
382 if ($errorSet) {
383 echo "<ul>\n";
384 foreach ($errorMsgs as $msg)
385 echo "<li>$msg</li>\n";
386 echo "</ul>\n";
387 }
388
389 echo $out;
390
391 printPageFooter();
101 392
102 393
103 // Dump the course data cache, but only if it has changed 394 // Dump the course data cache, but only if it has changed
104 if ($dirty) { 395 if ($cacheDirty) {
105 // First try append mode 396 // First try append mode
106 $fp = @fopen($cachefile, "rb+"); 397 $fp = @fopen($cacheFile, "rb+");
107 398
108 // If file didn't exist, try write mode 399 // If file didn't exist, try write mode
109 if (!$fp) 400 if (!$fp)
110 $fp = @fopen($cachefile, "wb"); 401 $fp = @fopen($cacheFile, "wb");
111 402
112 if ($fp) { 403 if ($fp) {
113 // Use locking to prevent concurrent access and dump data 404 // Use locking to prevent concurrent access and dump data
114 if (flock($fp, LOCK_EX)) { 405 if (flock($fp, LOCK_EX)) {
115 ftruncate($fp, 0); 406 ftruncate($fp, 0);