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