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;
|
|
23 if (!preg_match("#^[A-Z]{3}\d\S+#", $id)) {
|
|
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
|
5
|
147 if ($haveData) {
|
|
148 $totalHours = 0;
|
|
149 $totalGrouped = 0;
|
|
150 $dayHours = array();
|
7
|
151
|
|
152 for ($day = 0; $day < $classInfo["maxdays"]; $day++)
|
5
|
153 $dayHours[$day]["total"] = $dayHours[$day]["grouped"] = 0;
|
1
|
154
|
5
|
155 foreach ($classDefs as $id => $data) {
|
|
156 $h = $data["hours"];
|
|
157
|
|
158 $totalHours += $h;
|
|
159 $dayHours[$data["day"]]["total"] += $h;
|
1
|
160
|
5
|
161 if (!$data["grouped"]) {
|
|
162 $totalGrouped += $h;
|
|
163 $dayHours[$data["day"]]["grouped"] += $h;
|
|
164 }
|
|
165 }
|
|
166
|
|
167 // Create the timetable table
|
|
168 $out =
|
|
169 "<p>".join("; ", $classInfo["general"])."</p>\n".
|
|
170 "<table class=\"timetable\">".
|
|
171 " <tr>\n <th></th>\n";
|
7
|
172 for ($day = 0; $day < $classInfo["maxdays"]; $day++) {
|
5
|
173 $out .= " <th class=\"days\">".$dayNames[$day]."</th>\n";
|
|
174 }
|
|
175 $out .= " </tr>\n";
|
1
|
176
|
|
177 for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) {
|
5
|
178 $out .= " <tr>\n".
|
|
179 " <th class=\"hours\">".getHour($hour)."</th>\n";
|
7
|
180 for ($day = 0; $day < $classInfo["maxdays"]; $day++) {
|
1
|
181 if (isset($classHourTable[$hour][$day])) {
|
|
182 $h = $classHourTable[$hour][$day];
|
|
183
|
|
184 if ($h < 1) {
|
|
185 if ($h == 0) {
|
|
186 $n = 0;
|
|
187 for ($i = $hour; $i < $classInfo["maxhours"]; $i++)
|
|
188 if ($classHourTable[$i][$day] == $h) {
|
|
189 $classHourTable[$i][$day] = -1;
|
|
190 $n++;
|
|
191 } else
|
|
192 break;
|
5
|
193 $out .= " <td rowspan=\"".$n."\" class=\"clnothing\"></td>\n";
|
1
|
194 }
|
|
195 } else
|
|
196 if (isset($classDefs[$h])) {
|
|
197 if (!isset($classDefs[$h]["done"])) {
|
|
198 $classDefs[$h]["done"] = true;
|
|
199 $i = $classDefs[$h];
|
|
200 $d = $classDefs[$h]["data"];
|
|
201
|
|
202 $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]);
|
|
203
|
5
|
204 $out .= " <td rowspan=\"".$i["hours"].
|
1
|
205 "\" class=\"".($isSplit || $i["grouped"] ? "clgrouped" : "clnormal")."\">";
|
|
206
|
|
207 if ($isSplit) {
|
5
|
208 $out .= "<table>".
|
1
|
209 "<tr><td>".matchCourse($d[0])."</td><td>".matchCourse($d[1])."</td></tr>";
|
|
210 for ($j = 2; $j < count($d); $j += 2)
|
5
|
211 $out .= "<tr><td>".htmlentities($d[$j])."</td><td>".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."</td></tr>";
|
|
212 $out .= "</table>";
|
1
|
213 } else {
|
5
|
214 $out .= matchCourse($d[0])."<br />";
|
1
|
215 for ($j = 1; $j < count($d); $j++)
|
5
|
216 $out .= htmlentities($d[$j])."<br />";
|
1
|
217 }
|
5
|
218 $out .= "<div class=\"nhours\"><span>".$i["hours"]."h</span></div></td>\n";
|
1
|
219 }
|
|
220 } else
|
|
221 errorMsg("Internal error cell $hour / $day : hour id $h does not exist!");
|
|
222 } else
|
|
223 errorMsg("Internal error, cell $hour / $day does not exist.");
|
|
224 }
|
5
|
225 $out .= " </tr>\n";
|
1
|
226 }
|
|
227
|
7
|
228 $out .= printHourInfo($dayHours, $classInfo["maxdays"]).
|
5
|
229 "</table>";
|
|
230
|
|
231 } // haveData
|
|
232
|
|
233
|
|
234 $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka;
|
|
235 printPageHeader($pageTitle);
|
|
236 echo "<h1>".$pageTitle."</h1>\n";
|
|
237
|
|
238 echo "<form action=\"".$baseURI."\" method=\"get\">\n".
|
|
239 " <select name=\"luokka\">\n";
|
|
240
|
|
241 foreach ($classes as $class) {
|
|
242 echo " <option value=\"".$class."\">".htmlentities($class)."</option>\n";
|
1
|
243 }
|
5
|
244 echo " </select>\n <input type=\"submit\" value=\" Vaihda \" />\n</form>\n";
|
1
|
245
|
|
246 // Show error messages
|
|
247 if ($errorSet) {
|
|
248 echo "<ul>\n";
|
|
249 foreach ($errorMsgs as $msg)
|
|
250 echo "<li>$msg</li>\n";
|
|
251 echo "</ul>\n";
|
|
252 }
|
|
253
|
5
|
254 echo $out;
|
|
255
|
1
|
256 printPageFooter();
|
|
257
|
|
258
|
|
259 // Dump the course data cache, but only if it has changed
|
|
260 if ($cacheDirty) {
|
|
261 // First try append mode
|
|
262 $fp = @fopen($cacheFile, "rb+");
|
|
263
|
|
264 // If file didn't exist, try write mode
|
|
265 if (!$fp)
|
|
266 $fp = @fopen($cacheFile, "wb");
|
|
267
|
|
268 if ($fp) {
|
|
269 // Use locking to prevent concurrent access and dump data
|
|
270 if (flock($fp, LOCK_EX)) {
|
|
271 ftruncate($fp, 0);
|
|
272 fwrite($fp, "<?\n\$cache = array(\n");
|
|
273 foreach ($cache as $id => $data) {
|
|
274 fwrite($fp, " \"".addslashes($id)."\" => array(\"desc\" => \"".
|
|
275 addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n");
|
|
276 }
|
|
277 fwrite($fp, ");\n?>");
|
|
278 }
|
|
279 fclose($fp);
|
|
280 }
|
|
281 }
|
|
282
|
|
283 ?> |