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