Mercurial > hg > lukkari
annotate index.php @ 27:7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 05 Sep 2011 10:54:47 +0300 |
parents | b4b86915ae2c |
children | ab06d838d916 |
rev | line source |
---|---|
1 | 1 <? |
2 $pageCSS = array("http://tnsp.org/docs1.css", "luk.css"); | |
3 $pageCharset = "iso-8859-15"; | |
4 $luokkaDefault = "TTE9SNO"; | |
5 | 5 $dataDir = "cache/"; |
6 $classFile = "luokat.txt"; | |
1 | 7 $mapFile = "kartta.png"; |
5 | 8 $roomFile = "luokkatilat.txt"; |
1 | 9 $cacheFile = "coursecache.txt"; |
16
ea9558465ad6
Add link to original data, fix left-over beta stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
10 $baseURI = "http://tnsp.org/luk/"; |
1 | 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 | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
14 $hourStamps = array( |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
15 array( 8, 15), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
16 array( 9, 15), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
17 array(10, 15), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
18 array(11, 15), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
19 array(12, 15), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
20 array(13, 15), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
21 array(14, 15), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
22 array(15, 15), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
23 array(16, 15), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
24 array(17, 5), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
25 array(18, 5), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
26 array(19, 0), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
27 array(20, 45), |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
28 ); |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
29 |
5 | 30 // Include framework |
1 | 31 require "mcommon.inc.php"; |
32 require "merrors.inc.php"; | |
33 | |
2 | 34 |
5 | 35 // Helper functions |
1 | 36 function checkClassID(&$id) |
37 { | |
38 global $luokkaDefault; | |
23
b4b86915ae2c
Better input validation for $_GET arguments, thanks to Hjalmarr for pointing out the error.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
39 if (preg_match("#^([A-Z]{3}\d[A-Za-z0-9_]{1,6}|ccr|Ryh_[A-Z]{3}\d[A-Za-z0-9_]{1,6})$#", $id, $m)) { |
b4b86915ae2c
Better input validation for $_GET arguments, thanks to Hjalmarr for pointing out the error.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
40 $id = $m[1]; |
b4b86915ae2c
Better input validation for $_GET arguments, thanks to Hjalmarr for pointing out the error.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
41 return TRUE; |
b4b86915ae2c
Better input validation for $_GET arguments, thanks to Hjalmarr for pointing out the error.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
42 } else { |
18 | 43 errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioarvoa <b>".$luokkaDefault."</b>."); |
1 | 44 $id = $luokkaDefault; |
45 return FALSE; | |
23
b4b86915ae2c
Better input validation for $_GET arguments, thanks to Hjalmarr for pointing out the error.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
46 } |
1 | 47 } |
48 | |
17 | 49 // Check for mini-info mode |
50 $miniMode = isset($_GET["nyt"]); | |
1 | 51 |
52 // Check given parameters | |
53 if (isset($_GET["luokka"])) { | |
54 $luokka = $_GET["luokka"]; | |
17 | 55 if (checkClassID($luokka) && !$miniMode) { |
1 | 56 setcookie("lukluokka", $luokka, time() + 365*24*60*60); // expire in a year |
57 } | |
58 } else | |
59 if (isset($_COOKIE["lukluokka"])) { | |
60 $luokka = $_COOKIE["lukluokka"]; | |
61 checkClassID($luokka); | |
62 } else { | |
63 errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa <b>".$luokkaDefault."</b>."); | |
64 $luokka = $luokkaDefault; | |
65 } | |
66 | |
67 | |
68 if (isset($_GET["tila"])) { | |
69 $tila = $_GET["tila"]; | |
70 echo "Luokkatilan näyttömoodi ei vielä tuettu.<br />\n"; | |
71 exit; | |
72 | |
5 | 73 $fp = @fopen($roomFile, "rb"); |
1 | 74 if ($fp) { |
75 fclose($fp); | |
76 } | |
77 } | |
78 | |
20 | 79 |
80 function getHourStamp($hour, $sec = 0, $mon = 0, $day = 0, $year = 0) | |
5 | 81 { |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
82 global $hourStamps; |
17 | 83 if (isset($hourStamps[$hour])) |
20 | 84 return mktime($hourStamps[$hour][0], $hourStamps[$hour][1], $sec, $mon, $year); |
17 | 85 else |
20 | 86 return mktime($hour + 8, 0, $sec, $mon, $year); |
87 } | |
88 | |
17 | 89 |
20 | 90 function getHour($hour) |
91 { | |
92 $stamp = getHourStamp($hour); | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
93 return "<br />".date("H:i", $stamp)." - ".date("H:i", $stamp + 45 * 60)."<br /><br />"; |
5 | 94 } |
95 | |
96 | |
1 | 97 function matchCourse($id) |
98 { | |
99 global $cache, $infoURI, $cacheDirty; | |
100 | |
101 // Check if course exists in cache | |
102 if (!isset($cache[$id])) { | |
103 $cacheDirty = TRUE; | |
104 // Not cached, try to fetch data | |
105 $data = @file_get_contents($infoURI.$id); | |
106 if ($data !== FALSE) { | |
107 if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*op\s*</strong></td>#", $data, $m)) { | |
108 // Add data to cache | |
109 $cache[$id] = array("desc" => $m[1], "op" => intval($m[2])); | |
110 } | |
111 } | |
112 } | |
113 | |
114 if (isset($cache[$id])) | |
115 return "<a target=\"_blank\" title=\"".htmlentities($id." - ".$cache[$id]["op"]." op"). | |
116 "\" href=\"".htmlentities($infoURI.$id)."\">".htmlentities($cache[$id]["desc"])."</a>"; | |
117 else | |
118 return htmlentities($id); | |
119 } | |
120 | |
121 | |
122 function matchClass($matches) | |
123 { | |
124 global $baseURI; | |
5 | 125 return "<b><a href=\"".$baseURI."?tila=".$matches[1]."\">".$matches[1]."</a></b> ".$matches[2]; |
1 | 126 } |
127 | |
128 | |
129 // Global cache for course data | |
130 $cache = array(); | |
131 $cacheDirty = FALSE; | |
132 | |
133 | |
134 // Try to read cachefile, if we can get file lock on it | |
135 $fp = @fopen($cacheFile, "rb"); | |
136 if ($fp) { | |
137 if (flock($fp, LOCK_SH)) { | |
138 require($cacheFile); | |
139 flock($fp, LOCK_UN); | |
140 } | |
141 fclose($fp); | |
142 } | |
143 | |
5 | 144 $fp = @fopen($classFile, "rb"); |
145 $classes = array(); | |
146 if ($fp) { | |
147 if (flock($fp, LOCK_SH)) { | |
148 while (!feof($fp)) { | |
149 $str = trim(fgets($fp, 128)); | |
150 if ($str[0] != "#" && strlen($str) > 2) | |
151 $classes[] = $str; | |
152 } | |
153 flock($fp, LOCK_UN); | |
154 } | |
155 fclose($fp); | |
17 | 156 } else { |
157 errorMsg("Luokkien listaa ei löytynyt. Kokeile ladata sivu uudelleen hetken kuluttua."); | |
1 | 158 } |
159 | |
160 | |
5 | 161 if (!file_exists($dataDir.$luokka.".data")) { |
17 | 162 errorMsg("Luokan ".htmlentities($luokka)." tietoja ei löytynyt! Jos luokkakoodi on uusi, ilmestyy se järjestelmään seuraavan päivityksen aikana. Luokkatiedot päivitetään 2 kertaa vuorokaudessa."); |
5 | 163 $haveData = FALSE; |
164 } else { | |
165 require($dataDir.$luokka.".data"); | |
166 $haveData = isset($classDefs); | |
1 | 167 } |
168 | |
5 | 169 |
170 function printHourInfo($dayHours, $showDays) | |
171 { | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
172 $out = " <tr>\n <td>Tunteja<br />(<b>ryhmä/vv</b>)</td>\n"; |
5 | 173 for ($day = 0; $day < $showDays; $day++) { |
174 $out .= " <td>".$dayHours[$day]["total"]."h (<b>".$dayHours[$day]["grouped"]."h</b>)</td>\n"; | |
175 } | |
176 return $out." </tr>\n"; | |
1 | 177 } |
178 | |
179 | |
12
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
180 function findHours($start, $day, $cmp, $mark = FALSE) |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
181 { |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
182 global $classHourTable, $classInfo; |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
183 $n = 0; |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
184 for ($i = $start; $i < $classInfo["maxhours"]; $i++) |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
185 if ($classHourTable[$i][$day] == $cmp) { |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
186 if ($mark) $classHourTable[$i][$day] = -9999; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
187 $n++; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
188 } else |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
189 break; |
12
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
190 return $n; |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
191 } |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
192 |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
193 |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
194 function findNextHour(&$j, &$i, $day, $hour, $chk) |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
195 { |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
196 global $classInfo, $classHourTable; |
20 | 197 // for ($j = $day; $j < $classInfo["maxdays"]; $j++) |
198 $j = $day; | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
199 for ($i = $hour; $i < $classInfo["maxhours"]; $i++) { |
20 | 200 if ($chk > 0 && $classHourTable[$i][$j] != $chk) |
201 return $classHourTable[$i][$j]; | |
202 | |
203 if ($classHourTable[$i][$j] > 0 && $classHourTable[$i][$j] != $chk) | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
204 return $classHourTable[$i][$j]; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
205 } |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
206 |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
207 return -1; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
208 } |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
209 |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
210 |
19 | 211 function getHourInfo($isSplit, $d) |
212 { | |
213 if ($isSplit) { | |
214 $out = "<table>". | |
215 "<tr><td>".matchCourse($d[0])."</td><td>".matchCourse($d[1])."</td></tr>"; | |
216 | |
217 for ($j = 2; $j < count($d); $j += 2) | |
218 $out .= "<tr><td>".htmlentities($d[$j])."</td><td>".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."</td></tr>"; | |
219 | |
220 return $out."</table>"; | |
221 } else { | |
222 $out = matchCourse($d[0])."<br />"; | |
223 | |
224 for ($j = 1; $j < count($d); $j++) | |
225 $out .= htmlentities($d[$j])."<br />"; | |
226 | |
227 return $out; | |
228 } | |
229 } | |
230 | |
231 | |
232 function getHourInfoData($id) | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
233 { |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
234 global $classDefs; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
235 |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
236 if (isset($id)) { |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
237 if ($id >= 0 && isset($classDefs[$id])) { |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
238 $i = $classDefs[$id]; |
19 | 239 $isSplit = preg_match("/^[A-Z]\d{6}$/", $i["data"][1]); |
240 return getHourInfo($isSplit, $i["data"]); | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
241 } else |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
242 return "<p>Ei tunteja.</p>"; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
243 } else |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
244 return "<p>Ei mitään.</p>"; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
245 |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
246 return ""; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
247 } |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
248 |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
249 |
17 | 250 if ($miniMode) { |
20 | 251 $stamp = time(); |
252 $aika = getdate($stamp); | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
253 $day = $aika["wday"] - 1; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
254 if ($day < 0) $day = 7; |
20 | 255 $hour = $aika["hours"] - 8; |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
256 |
20 | 257 $id = $classHourTable[$hour][$day]; |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
258 |
18 | 259 $out = "<div class=\"content\">\n". |
20 | 260 "<h2>".$dayNames[$day]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n". |
261 getHourInfoData($id)."\n"; | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
262 |
20 | 263 |
264 if (isset($id) && $id >= 0 && isset($classDefs[$id])) { | |
265 $i = $classDefs[$id]; | |
266 $start = getHourStamp($i["start"]); | |
267 } | |
268 | |
269 // echo "day=$day, hour=$hour, id=$id<br />\n"; | |
270 | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
271 if ($day >= $classInfo["maxdays"]) { |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
272 $day = $hour = 0; |
20 | 273 $id = -1; |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
274 } else |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
275 if ($hour < 0) { |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
276 $hour = 0; |
20 | 277 $id = -1; |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
278 } else |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
279 if ($hour >= $classInfo["maxhours"]) { |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
280 $day++; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
281 $hour = 0; |
20 | 282 $id = -1; |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
283 } |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
284 |
20 | 285 // echo "day=$day, hour=$hour, id=$id<br />\n"; |
286 $found = findNextHour($nday, $nhour, $day, $hour, $id); | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
287 |
20 | 288 // echo "nday=$nday, nhour=$nhour, id=$found<br />\n"; |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
289 |
20 | 290 $out .= "<h2>Seuraavaksi"; |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
291 if ($found > 0) { |
20 | 292 $stamp = getHourStamp($nhour, 0, 0, $aika["mon"], $aika["mday"] + $nday - $day, $aika["year"]); |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
293 $aika = getdate($stamp); |
20 | 294 $out .= ": ".$dayNames[$nday]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n". |
295 getHourInfoData($found)."\n"; | |
296 } else { | |
297 $out .= "</h2>\n". | |
298 "<p>Ei mitään</p>\n"; | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
299 } |
20 | 300 |
301 $out .= "</div>\n"; | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
302 } |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
303 else |
5 | 304 if ($haveData) { |
305 $totalHours = 0; | |
306 $totalGrouped = 0; | |
307 $dayHours = array(); | |
7 | 308 |
11 | 309 for ($day = 0; $day < $classInfo["maxdays"]; $day++) { |
5 | 310 $dayHours[$day]["total"] = $dayHours[$day]["grouped"] = 0; |
1 | 311 |
11 | 312 for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { |
313 $id = $classHourTable[$hour][$day]; | |
1 | 314 |
11 | 315 if ($id > 0) { |
316 $totalHours++; | |
317 $dayHours[$day]["total"]++; | |
318 | |
319 if ($classDefs[$id]["grouped"]) { | |
320 $totalGrouped++; | |
321 $dayHours[$day]["grouped"]++; | |
322 } | |
323 } | |
5 | 324 } |
325 } | |
326 | |
327 // Create the timetable table | |
328 $out = | |
329 "<p>".join("; ", $classInfo["general"])."</p>\n". | |
11 | 330 "<div>Viikossa yhteensä <b>".$totalHours."</b> tuntia, joista <b>".$totalGrouped."</b> ryhmissä tai vuoroviikoin.</div>\n". |
5 | 331 "<table class=\"timetable\">". |
332 " <tr>\n <th></th>\n"; | |
7 | 333 for ($day = 0; $day < $classInfo["maxdays"]; $day++) { |
18 | 334 $out .= " <th style=\"width: ".(100 / $classInfo["maxdays"]). |
335 "%;\" class=\"days\">".$dayNames[$day]."</th>\n"; | |
5 | 336 } |
337 $out .= " </tr>\n"; | |
1 | 338 |
339 for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { | |
5 | 340 $out .= " <tr>\n". |
341 " <th class=\"hours\">".getHour($hour)."</th>\n"; | |
7 | 342 for ($day = 0; $day < $classInfo["maxdays"]; $day++) { |
1 | 343 if (isset($classHourTable[$hour][$day])) { |
344 $h = $classHourTable[$hour][$day]; | |
345 | |
346 if ($h < 1) { | |
10
6272ca0cacea
Make editing of custom timetable easier.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
347 if ($h > -9999) { |
12
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
348 $n = findHours($hour, $day, $h, TRUE); |
5 | 349 $out .= " <td rowspan=\"".$n."\" class=\"clnothing\"></td>\n"; |
1 | 350 } |
351 } else | |
352 if (isset($classDefs[$h])) { | |
353 if (!isset($classDefs[$h]["done"])) { | |
354 $classDefs[$h]["done"] = true; | |
355 $i = $classDefs[$h]; | |
356 $d = $classDefs[$h]["data"]; | |
357 | |
358 $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]); | |
359 | |
12
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
360 $hours = findHours($hour, $day, $h, FALSE); |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
361 if ($hours != $i["hours"]) { |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
362 errorMsg("Internal error, cell $hour / $day : hour id $h hours ($hours) do not match ".$i["hours"]."!"); |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
363 } |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
364 |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
365 $out .= " <td rowspan=\"".$hours. |
19 | 366 "\" class=\"".($isSplit || $i["grouped"] ? "clgrouped" : "clnormal")."\">". |
367 getHourInfo($isSplit, $d). | |
368 "<div class=\"nhours\"><span>".$hours."h</span></div></td>\n"; | |
1 | 369 } |
370 } else | |
371 errorMsg("Internal error cell $hour / $day : hour id $h does not exist!"); | |
372 } else | |
373 errorMsg("Internal error, cell $hour / $day does not exist."); | |
374 } | |
5 | 375 $out .= " </tr>\n"; |
1 | 376 } |
377 | |
7 | 378 $out .= printHourInfo($dayHours, $classInfo["maxdays"]). |
14 | 379 "</table>\n"; |
5 | 380 |
381 } // haveData | |
382 | |
383 | |
384 $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka; | |
385 printPageHeader($pageTitle); | |
18 | 386 |
387 if ($miniMode) echo "<div id=\"mini\">\n"; | |
5 | 388 |
18 | 389 echo |
390 "<form id=\"controls\" action=\"".$baseURI."\" method=\"get\">\n". | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
391 " <table>\n". |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
392 " <tr>\n". |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
393 " <td>\n". |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
394 " <select name=\"luokka\">\n"; |
5 | 395 |
396 foreach ($classes as $class) { | |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
397 echo " <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".htmlentities($class)."</option>\n"; |
1 | 398 } |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
399 echo |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
400 " </select>\n". |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
401 " </td>\n". |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
402 " <td><input type=\"submit\" value=\" Vaihda \" /></td>\n". |
17 | 403 " <td>[<a href=\"".$baseURI."?luokka=".$luokka; |
404 | |
405 if ($miniMode) | |
406 echo "\">Normaali"; | |
407 else | |
408 echo "&nyt\">Mini-info"; | |
409 | |
410 echo "</a>]</td>\n". | |
16
ea9558465ad6
Add link to original data, fix left-over beta stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
411 " <td>[<a href=\"http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_".$luokka.".htm\">Alkuperäinen</a>]</td>\n". |
13
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
412 " </tr>\n". |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
413 " </table>\n". |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
414 "</form>\n"; |
57dd1db922c2
Fix hours, add mini-info mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
415 |
18 | 416 echo "<h1>".$pageTitle."</h1>\n"; |
1 | 417 |
418 // Show error messages | |
419 if ($errorSet) { | |
420 echo "<ul>\n"; | |
421 foreach ($errorMsgs as $msg) | |
422 echo "<li>$msg</li>\n"; | |
423 echo "</ul>\n"; | |
424 } | |
425 | |
5 | 426 echo $out; |
427 | |
17 | 428 echo "<div style=\"position: relative; top: 5em; font-size: 5pt;\"><hr />Yhteydenotot <b>ccr @ IRCNet</b> tai <b>ccr at tnsp.org</b>, en vastaa mahdollisista virheistä tiedoissa.</div>\n"; |
18 | 429 |
430 if ($miniMode) echo "</div>\n"; | |
1 | 431 printPageFooter(); |
432 | |
433 | |
434 // Dump the course data cache, but only if it has changed | |
435 if ($cacheDirty) { | |
27
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
436 // Create string containing the data |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
437 $str = "<?\n\$cache = array(\n"; |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
438 foreach ($cache as $id => $data) { |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
439 $str .= " \"".addslashes($id)."\" => array(\"desc\" => \"". |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
440 addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n"; |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
441 } |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
442 $str .= ");\n?>"; |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
443 |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
444 // Use locking to prevent concurrent access and dump data |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
445 if (file_put_contents($cacheFile, $str, LOCK_EX) === FALSE) { |
7655db03ea60
Use different strategy of saving course cache, by using file_put_contents() with locking instead of using separate writes.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
446 // Can't do much anything here .. |
1 | 447 } |
448 } | |
449 | |
450 ?> |