Mercurial > hg > lukkari
diff beta.php @ 1:21fde93375e9
Add beta code.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 11 Jan 2011 20:43:12 +0200 |
parents | |
children | 2471bb891299 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/beta.php Tue Jan 11 20:43:12 2011 +0200 @@ -0,0 +1,251 @@ +<? +$pageCSS = array("http://tnsp.org/docs1.css", "luk.css"); +$pageCharset = "iso-8859-15"; +$luokkaDefault = "TTE9SNO"; +$mapFile = "kartta.png"; +$classFile = "luokkatilat.txt"; +$cacheFile = "coursecache.txt"; +$baseURI = "http://tnsp.org/luk/?"; +$infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&koodi1="; + +$showDays = 6; +$dayNames = array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai"); + + +require "mcommon.inc.php"; +require "merrors.inc.php"; + +function getHour($hour) +{ + return "<br />".($hour + 8).":15 - ".($hour + 9).":00<br /><br />"; +} + +function checkClassID(&$id) +{ + global $luokkaDefault; + if (!preg_match("#^[A-Z]{3}\d\S+#", $id)) { + errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioavoa <b>".$luokkaDefault."</b>."); + $id = $luokkaDefault; + return FALSE; + } else + return TRUE; +} + + +// Check given parameters +if (isset($_GET["luokka"])) { + $luokka = $_GET["luokka"]; + if (checkClassID($luokka)) { + setcookie("lukluokka", $luokka, time() + 365*24*60*60); // expire in a year + } +} else +if (isset($_COOKIE["lukluokka"])) { + $luokka = $_COOKIE["lukluokka"]; + checkClassID($luokka); +} else { + errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa <b>".$luokkaDefault."</b>."); + $luokka = $luokkaDefault; +} + + +if (isset($_GET["tila"])) { + $tila = $_GET["tila"]; + echo "Luokkatilan näyttömoodi ei vielä tuettu.<br />\n"; + exit; + + $fp = @fopen($classFile, "rb"); + if ($fp) { + fclose($fp); + } +} + + +function matchCourse($id) +{ + global $cache, $infoURI, $cacheDirty; + + // Check if course exists in cache + if (!isset($cache[$id])) { + $cacheDirty = TRUE; + // Not cached, try to fetch data + $data = @file_get_contents($infoURI.$id); + if ($data !== FALSE) { + if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*op\s*</strong></td>#", $data, $m)) { + // Add data to cache + $cache[$id] = array("desc" => $m[1], "op" => intval($m[2])); + } + } + } + + if (isset($cache[$id])) + return "<a target=\"_blank\" title=\"".htmlentities($id." - ".$cache[$id]["op"]." op"). + "\" href=\"".htmlentities($infoURI.$id)."\">".htmlentities($cache[$id]["desc"])."</a>"; + else + return htmlentities($id); +} + + +function matchClass($matches) +{ + global $baseURI; + return "<b><a href=\"".$baseURI."tila=".$matches[1]."\">".$matches[1]."</a></b> ".$matches[2]; +} + + +// Global cache for course data +$cache = array(); +$cacheDirty = FALSE; + + +// Try to read cachefile, if we can get file lock on it +$fp = @fopen($cacheFile, "rb"); +if ($fp) { + if (flock($fp, LOCK_SH)) { + require($cacheFile); + flock($fp, LOCK_UN); + } + fclose($fp); +} + + +if (file_exists($luokka.".data")) { + require($luokka.".data"); +} else { + errorMsg("Luokan ".htmlentities($luokka)." datatiedostoa ei löytynyt!"); +} + +$totalHours = 0; +$totalGrouped = 0; +$dayHours = array(); +for ($day = 0; $day < $showDays; $day++) + $dayHours[$day]["total"] = $dayHours[$day]["grouped"] = 0; + +foreach ($classDefs as $id => $data) { + $h = $data["hours"]; + + $totalHours += $h; + $dayHours[$data["day"]]["total"] += $h; + + if (!$data["grouped"]) { + $totalGrouped += $h; + $dayHours[$data["day"]]["grouped"] += $h; + } +} + +for ($day = $showDays - 1; $day >= 0; $day--) { + if ($dayHours[$day]["total"] == 0) + $showDays--; + else + break; +} + + + +$pageTitle = $luokka." / ".join("; ", $classInfo["info"]); +printPageHeader($pageTitle); +echo "<h1>".$pageTitle."</h1>\n". +"<p>".join("; ", $classInfo["general"])."</p>\n"; + +echo "<table class=\"timetable\">". +" <tr>\n <th></th>\n"; +for ($day = 0; $day < $showDays; $day++) { + echo " <th class=\"days\">".$dayNames[$day]."</th>\n"; +} +echo " </tr>\n"; + +for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { + echo " <tr>\n"; + echo " <th class=\"hours\">".getHour($hour)."</th>\n"; + for ($day = 0; $day < $showDays; $day++) { + if (isset($classHourTable[$hour][$day])) { + $h = $classHourTable[$hour][$day]; + + if ($h < 1) { + if ($h == 0) { + $n = 0; + for ($i = $hour; $i < $classInfo["maxhours"]; $i++) + if ($classHourTable[$i][$day] == $h) { + $classHourTable[$i][$day] = -1; + $n++; + } else + break; + echo " <td rowspan=\"".$n."\" class=\"clnothing\"></td>\n"; + } + } else + if (isset($classDefs[$h])) { + if (!isset($classDefs[$h]["done"])) { + $classDefs[$h]["done"] = true; + $i = $classDefs[$h]; + $d = $classDefs[$h]["data"]; + + $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]); + + echo " <td rowspan=\"".$i["hours"]. + "\" class=\"".($isSplit || $i["grouped"] ? "clgrouped" : "clnormal")."\">"; + + if ($isSplit) { + echo "<table>". + "<tr><td>".matchCourse($d[0])."</td><td>".matchCourse($d[1])."</td></tr>"; + for ($j = 2; $j < count($d); $j += 2) + echo "<tr><td>".htmlentities($d[$j])."</td><td>".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."</td></tr>"; + echo "</table>"; + } else { + echo matchCourse($d[0])."<br />"; + for ($j = 1; $j < count($d); $j++) + echo htmlentities($d[$j])."<br />"; + } + echo "</td>\n"; + } + } else + errorMsg("Internal error cell $hour / $day : hour id $h does not exist!"); + } else + errorMsg("Internal error, cell $hour / $day does not exist."); + } + echo " </tr>\n"; +} + +echo " <tr>\n <td>Tunteja (<b>ryhmä</b>)</td>\n"; +for ($day = 0; $day < $showDays; $day++) { + echo " <td>".$dayHours[$day]["total"]."h (<b>".$dayHours[$day]["grouped"]."h</b>)</td>\n"; +} +echo " </tr>\n"; + +echo "</table>"; + + +// Show error messages +if ($errorSet) { + echo "<ul>\n"; + foreach ($errorMsgs as $msg) + echo "<li>$msg</li>\n"; + echo "</ul>\n"; +} + +printPageFooter(); + + +// Dump the course data cache, but only if it has changed +if ($cacheDirty) { + // First try append mode + $fp = @fopen($cacheFile, "rb+"); + + // If file didn't exist, try write mode + if (!$fp) + $fp = @fopen($cacheFile, "wb"); + + if ($fp) { + // Use locking to prevent concurrent access and dump data + if (flock($fp, LOCK_EX)) { + ftruncate($fp, 0); + fwrite($fp, "<?\n\$cache = array(\n"); + foreach ($cache as $id => $data) { + fwrite($fp, " \"".addslashes($id)."\" => array(\"desc\" => \"". + addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n"); + } + fwrite($fp, ");\n?>"); + } + fclose($fp); + } +} + +?> \ No newline at end of file