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