Mercurial > hg > lukkari
annotate beta.php @ 12:306028aa2253
Fix hour counts when table has been modified.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 11 Jan 2011 23:05:49 +0200 |
parents | 03c393c347c7 |
children | 57dd1db922c2 |
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"; |
5 | 10 $baseURI = "http://tnsp.org/luk/beta.php"; |
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 | |
5 | 14 // Include framework |
1 | 15 require "mcommon.inc.php"; |
16 require "merrors.inc.php"; | |
17 | |
2 | 18 |
5 | 19 // Helper functions |
1 | 20 function checkClassID(&$id) |
21 { | |
22 global $luokkaDefault; | |
10
6272ca0cacea
Make editing of custom timetable easier.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
23 if (!preg_match("#^[A-Z]{3}\d\S+|ccr#", $id)) { |
1 | 24 errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioavoa <b>".$luokkaDefault."</b>."); |
25 $id = $luokkaDefault; | |
26 return FALSE; | |
27 } else | |
28 return TRUE; | |
29 } | |
30 | |
31 | |
32 // Check given parameters | |
33 if (isset($_GET["luokka"])) { | |
34 $luokka = $_GET["luokka"]; | |
35 if (checkClassID($luokka)) { | |
36 setcookie("lukluokka", $luokka, time() + 365*24*60*60); // expire in a year | |
37 } | |
38 } else | |
39 if (isset($_COOKIE["lukluokka"])) { | |
40 $luokka = $_COOKIE["lukluokka"]; | |
41 checkClassID($luokka); | |
42 } else { | |
43 errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa <b>".$luokkaDefault."</b>."); | |
44 $luokka = $luokkaDefault; | |
45 } | |
46 | |
47 | |
48 if (isset($_GET["tila"])) { | |
49 $tila = $_GET["tila"]; | |
50 echo "Luokkatilan näyttömoodi ei vielä tuettu.<br />\n"; | |
51 exit; | |
52 | |
5 | 53 $fp = @fopen($roomFile, "rb"); |
1 | 54 if ($fp) { |
55 fclose($fp); | |
56 } | |
57 } | |
58 | |
59 | |
5 | 60 function getHour($hour) |
61 { | |
62 return "<br />".($hour + 8).":15 - ".($hour + 9).":00<br /><br />"; | |
63 } | |
64 | |
65 | |
1 | 66 function matchCourse($id) |
67 { | |
68 global $cache, $infoURI, $cacheDirty; | |
69 | |
70 // Check if course exists in cache | |
71 if (!isset($cache[$id])) { | |
72 $cacheDirty = TRUE; | |
73 // Not cached, try to fetch data | |
74 $data = @file_get_contents($infoURI.$id); | |
75 if ($data !== FALSE) { | |
76 if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*op\s*</strong></td>#", $data, $m)) { | |
77 // Add data to cache | |
78 $cache[$id] = array("desc" => $m[1], "op" => intval($m[2])); | |
79 } | |
80 } | |
81 } | |
82 | |
83 if (isset($cache[$id])) | |
84 return "<a target=\"_blank\" title=\"".htmlentities($id." - ".$cache[$id]["op"]." op"). | |
85 "\" href=\"".htmlentities($infoURI.$id)."\">".htmlentities($cache[$id]["desc"])."</a>"; | |
86 else | |
87 return htmlentities($id); | |
88 } | |
89 | |
90 | |
91 function matchClass($matches) | |
92 { | |
93 global $baseURI; | |
5 | 94 return "<b><a href=\"".$baseURI."?tila=".$matches[1]."\">".$matches[1]."</a></b> ".$matches[2]; |
1 | 95 } |
96 | |
97 | |
98 // Global cache for course data | |
99 $cache = array(); | |
100 $cacheDirty = FALSE; | |
101 | |
102 | |
103 // Try to read cachefile, if we can get file lock on it | |
104 $fp = @fopen($cacheFile, "rb"); | |
105 if ($fp) { | |
106 if (flock($fp, LOCK_SH)) { | |
107 require($cacheFile); | |
108 flock($fp, LOCK_UN); | |
109 } | |
110 fclose($fp); | |
111 } | |
112 | |
5 | 113 $fp = @fopen($classFile, "rb"); |
114 $classes = array(); | |
115 if ($fp) { | |
116 if (flock($fp, LOCK_SH)) { | |
117 while (!feof($fp)) { | |
118 $str = trim(fgets($fp, 128)); | |
119 if ($str[0] != "#" && strlen($str) > 2) | |
120 $classes[] = $str; | |
121 } | |
122 flock($fp, LOCK_UN); | |
123 } | |
124 fclose($fp); | |
1 | 125 } |
126 | |
127 | |
5 | 128 if (!file_exists($dataDir.$luokka.".data")) { |
129 errorMsg("Luokan ".htmlentities($luokka)." datatiedostoa ei löytynyt!"); | |
130 $haveData = FALSE; | |
131 } else { | |
132 require($dataDir.$luokka.".data"); | |
133 $haveData = isset($classDefs); | |
1 | 134 } |
135 | |
5 | 136 |
137 function printHourInfo($dayHours, $showDays) | |
138 { | |
139 $out = " <tr>\n <td>Tunteja (<b>ryhmä</b>)</td>\n"; | |
140 for ($day = 0; $day < $showDays; $day++) { | |
141 $out .= " <td>".$dayHours[$day]["total"]."h (<b>".$dayHours[$day]["grouped"]."h</b>)</td>\n"; | |
142 } | |
143 return $out." </tr>\n"; | |
1 | 144 } |
145 | |
146 | |
12
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
147 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
|
148 { |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
149 global $classHourTable, $classInfo; |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
150 $n = 0; |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
151 for ($i = $start; $i < $classInfo["maxhours"]; $i++) |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
152 if ($classHourTable[$i][$day] == $cmp) { |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
153 if ($mark) $classHourTable[$i][$day] = -9999; |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
154 $n++; |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
155 } else |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
156 break; |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
157 return $n; |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
158 } |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
159 |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
160 |
5 | 161 if ($haveData) { |
162 $totalHours = 0; | |
163 $totalGrouped = 0; | |
164 $dayHours = array(); | |
7 | 165 |
11 | 166 for ($day = 0; $day < $classInfo["maxdays"]; $day++) { |
5 | 167 $dayHours[$day]["total"] = $dayHours[$day]["grouped"] = 0; |
1 | 168 |
11 | 169 for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { |
170 $id = $classHourTable[$hour][$day]; | |
1 | 171 |
11 | 172 if ($id > 0) { |
173 $totalHours++; | |
174 $dayHours[$day]["total"]++; | |
175 | |
176 if ($classDefs[$id]["grouped"]) { | |
177 $totalGrouped++; | |
178 $dayHours[$day]["grouped"]++; | |
179 } | |
180 } | |
5 | 181 } |
182 } | |
183 | |
184 // Create the timetable table | |
185 $out = | |
186 "<p>".join("; ", $classInfo["general"])."</p>\n". | |
11 | 187 "<div>Viikossa yhteensä <b>".$totalHours."</b> tuntia, joista <b>".$totalGrouped."</b> ryhmissä tai vuoroviikoin.</div>\n". |
5 | 188 "<table class=\"timetable\">". |
189 " <tr>\n <th></th>\n"; | |
7 | 190 for ($day = 0; $day < $classInfo["maxdays"]; $day++) { |
5 | 191 $out .= " <th class=\"days\">".$dayNames[$day]."</th>\n"; |
192 } | |
193 $out .= " </tr>\n"; | |
1 | 194 |
195 for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { | |
5 | 196 $out .= " <tr>\n". |
197 " <th class=\"hours\">".getHour($hour)."</th>\n"; | |
7 | 198 for ($day = 0; $day < $classInfo["maxdays"]; $day++) { |
1 | 199 if (isset($classHourTable[$hour][$day])) { |
200 $h = $classHourTable[$hour][$day]; | |
201 | |
202 if ($h < 1) { | |
10
6272ca0cacea
Make editing of custom timetable easier.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
203 if ($h > -9999) { |
12
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
204 $n = findHours($hour, $day, $h, TRUE); |
5 | 205 $out .= " <td rowspan=\"".$n."\" class=\"clnothing\"></td>\n"; |
1 | 206 } |
207 } else | |
208 if (isset($classDefs[$h])) { | |
209 if (!isset($classDefs[$h]["done"])) { | |
210 $classDefs[$h]["done"] = true; | |
211 $i = $classDefs[$h]; | |
212 $d = $classDefs[$h]["data"]; | |
213 | |
214 $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]); | |
215 | |
12
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
216 $hours = findHours($hour, $day, $h, FALSE); |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
217 if ($hours != $i["hours"]) { |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
218 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
|
219 } |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
220 |
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
221 $out .= " <td rowspan=\"".$hours. |
1 | 222 "\" class=\"".($isSplit || $i["grouped"] ? "clgrouped" : "clnormal")."\">"; |
223 | |
224 if ($isSplit) { | |
5 | 225 $out .= "<table>". |
1 | 226 "<tr><td>".matchCourse($d[0])."</td><td>".matchCourse($d[1])."</td></tr>"; |
227 for ($j = 2; $j < count($d); $j += 2) | |
5 | 228 $out .= "<tr><td>".htmlentities($d[$j])."</td><td>".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."</td></tr>"; |
229 $out .= "</table>"; | |
1 | 230 } else { |
5 | 231 $out .= matchCourse($d[0])."<br />"; |
1 | 232 for ($j = 1; $j < count($d); $j++) |
5 | 233 $out .= htmlentities($d[$j])."<br />"; |
1 | 234 } |
12
306028aa2253
Fix hour counts when table has been modified.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
235 $out .= "<div class=\"nhours\"><span>".$hours."h</span></div></td>\n"; |
1 | 236 } |
237 } else | |
238 errorMsg("Internal error cell $hour / $day : hour id $h does not exist!"); | |
239 } else | |
240 errorMsg("Internal error, cell $hour / $day does not exist."); | |
241 } | |
5 | 242 $out .= " </tr>\n"; |
1 | 243 } |
244 | |
7 | 245 $out .= printHourInfo($dayHours, $classInfo["maxdays"]). |
5 | 246 "</table>"; |
247 | |
248 } // haveData | |
249 | |
250 | |
251 $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka; | |
252 printPageHeader($pageTitle); | |
253 echo "<h1>".$pageTitle."</h1>\n"; | |
254 | |
255 echo "<form action=\"".$baseURI."\" method=\"get\">\n". | |
256 " <select name=\"luokka\">\n"; | |
257 | |
258 foreach ($classes as $class) { | |
259 echo " <option value=\"".$class."\">".htmlentities($class)."</option>\n"; | |
1 | 260 } |
5 | 261 echo " </select>\n <input type=\"submit\" value=\" Vaihda \" />\n</form>\n"; |
1 | 262 |
263 // Show error messages | |
264 if ($errorSet) { | |
265 echo "<ul>\n"; | |
266 foreach ($errorMsgs as $msg) | |
267 echo "<li>$msg</li>\n"; | |
268 echo "</ul>\n"; | |
269 } | |
270 | |
5 | 271 echo $out; |
272 | |
1 | 273 printPageFooter(); |
274 | |
275 | |
276 // Dump the course data cache, but only if it has changed | |
277 if ($cacheDirty) { | |
278 // First try append mode | |
279 $fp = @fopen($cacheFile, "rb+"); | |
280 | |
281 // If file didn't exist, try write mode | |
282 if (!$fp) | |
283 $fp = @fopen($cacheFile, "wb"); | |
284 | |
285 if ($fp) { | |
286 // Use locking to prevent concurrent access and dump data | |
287 if (flock($fp, LOCK_EX)) { | |
288 ftruncate($fp, 0); | |
289 fwrite($fp, "<?\n\$cache = array(\n"); | |
290 foreach ($cache as $id => $data) { | |
291 fwrite($fp, " \"".addslashes($id)."\" => array(\"desc\" => \"". | |
292 addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n"); | |
293 } | |
294 fwrite($fp, ");\n?>"); | |
295 } | |
296 fclose($fp); | |
297 } | |
298 } | |
299 | |
300 ?> |