Mercurial > hg > lukkari
view index.php @ 30:6148ce4a83ed
Cleanups.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 27 Sep 2011 10:56:50 +0300 |
parents | ab06d838d916 |
children | dbe7ff545293 |
line wrap: on
line source
<? $pageCSS = array("http://tnsp.org/docs1.css", "luk.css"); $pageCharset = "iso-8859-15"; $luokkaDefault = "TTE9SNO"; $dataDir = "cache/"; $classFile = "luokat.txt"; $mapFile = "kartta.png"; $roomFile = "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="; $infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&koodi1="; $dayNames = array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai"); $hourStamps = array( array( 8, 15), array( 9, 15), array(10, 15), array(11, 15), array(12, 15), array(13, 15), array(14, 15), array(15, 15), array(16, 15), array(17, 5), array(18, 5), array(19, 0), array(20, 45), ); // Include framework require "mcommon.inc.php"; require "merrors.inc.php"; // Helper functions function checkClassID(&$id) { global $luokkaDefault; if (preg_match("#^([A-Z]{3}\d[A-Za-z0-9_]{1,6}|ccr|Ryh_[A-Z]{3}\d[A-Za-z0-9_]{1,6})$#", $id, $m)) { $id = $m[1]; return TRUE; } else { errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioarvoa <b>".$luokkaDefault."</b>."); $id = $luokkaDefault; return FALSE; } } // Check for mini-info mode $miniMode = isset($_GET["nyt"]); // Check given parameters if (isset($_GET["luokka"])) { $luokka = $_GET["luokka"]; if (checkClassID($luokka) && !$miniMode) { 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($roomFile, "rb"); if ($fp) { fclose($fp); } } function getHourStamp($hour, $sec = 0, $mon = 0, $day = 0, $year = 0) { global $hourStamps; if (isset($hourStamps[$hour])) return mktime($hourStamps[$hour][0], $hourStamps[$hour][1], $sec, $mon, $year); else return mktime($hour + 8, 0, $sec, $mon, $year); } function getHour($hour) { $stamp = getHourStamp($hour); return "<br />".date("H:i", $stamp)." - ".date("H:i", $stamp + 45 * 60)."<br /><br />"; } 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); } // Read classfile $fp = @fopen($classFile, "rb"); $classes = array(); if ($fp) { if (flock($fp, LOCK_SH)) { while (!feof($fp)) { $str = trim(fgets($fp, 128)); if ($str[0] != "#" && strlen($str) > 2) $classes[] = $str; } flock($fp, LOCK_UN); } fclose($fp); } else { errorMsg("Luokkien listaa ei löytynyt. Kokeile ladata sivu uudelleen hetken kuluttua."); } if (!file_exists($dataDir.$luokka.".data")) { errorMsg("Luokan ".htmlentities($luokka)." tietoja ei löytynyt! Jos luokkakoodi on uusi, ilmestyy se järjestelmään seuraavan päivityksen aikana. Luokkatiedot päivitetään 2 kertaa vuorokaudessa."); $haveData = FALSE; } else { require($dataDir.$luokka.".data"); $haveData = isset($classDefs); } function printHourInfo($dayHours, $showDays) { $out = " <tr>\n <td>Tunteja<br />(<b>ryhmä/vv</b>)</td>\n"; for ($day = 0; $day < $showDays; $day++) { $out .= " <td>".$dayHours[$day]["total"]."h (<b>".$dayHours[$day]["grouped"]."h</b>)</td>\n"; } return $out." </tr>\n"; } function findHours($start, $day, $cmp, $mark = FALSE) { global $classHourTable, $classInfo; $n = 0; for ($i = $start; $i < $classInfo["maxhours"]; $i++) if ($classHourTable[$i][$day] == $cmp) { if ($mark) $classHourTable[$i][$day] = -9999; $n++; } else break; return $n; } function findNextHour(&$j, &$i, $day, $hour, $chk) { global $classInfo, $classHourTable; // for ($j = $day; $j < $classInfo["maxdays"]; $j++) $j = $day; for ($i = $hour; $i < $classInfo["maxhours"]; $i++) { if ($chk > 0 && $classHourTable[$i][$j] != $chk) return $classHourTable[$i][$j]; if ($classHourTable[$i][$j] > 0 && $classHourTable[$i][$j] != $chk) return $classHourTable[$i][$j]; } return -1; } function getHourInfo($isSplit, $d) { if ($isSplit) { $out = "<table>". "<tr><td>".matchCourse($d[0])."</td><td>".matchCourse($d[1])."</td></tr>"; for ($j = 2; $j < count($d); $j += 2) $out .= "<tr><td>".htmlentities($d[$j])."</td><td>".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."</td></tr>"; return $out."</table>"; } else { $out = matchCourse($d[0])."<br />"; for ($j = 1; $j < count($d); $j++) $out .= htmlentities($d[$j])."<br />"; return $out; } } function getHourInfoData($id) { global $classDefs; if (isset($id)) { if ($id >= 0 && isset($classDefs[$id])) { $i = $classDefs[$id]; $isSplit = preg_match("/^[A-Z]\d{6}$/", $i["data"][1]); return getHourInfo($isSplit, $i["data"]); } else return "<p>Ei tunteja.</p>"; } else return "<p>Ei mitään.</p>"; return ""; } if ($miniMode) { $stamp = time(); $aika = getdate($stamp); $day = $aika["wday"] - 1; if ($day < 0) $day = 7; $hour = $aika["hours"] - 8; $id = $classHourTable[$hour][$day]; $out = "<div class=\"content\">\n". "<h2>".$dayNames[$day]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n". getHourInfoData($id)."\n"; if (isset($id) && $id >= 0 && isset($classDefs[$id])) { $i = $classDefs[$id]; $start = getHourStamp($i["start"]); } // echo "day=$day, hour=$hour, id=$id<br />\n"; if ($day >= $classInfo["maxdays"]) { $day = $hour = 0; $id = -1; } else if ($hour < 0) { $hour = 0; $id = -1; } else if ($hour >= $classInfo["maxhours"]) { $day++; $hour = 0; $id = -1; } // echo "day=$day, hour=$hour, id=$id<br />\n"; $found = findNextHour($nday, $nhour, $day, $hour, $id); // echo "nday=$nday, nhour=$nhour, id=$found<br />\n"; $out .= "<h2>Seuraavaksi"; if ($found > 0) { $stamp = getHourStamp($nhour, 0, 0, $aika["mon"], $aika["mday"] + $nday - $day, $aika["year"]); $aika = getdate($stamp); $out .= ": ".$dayNames[$nday]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n". getHourInfoData($found)."\n"; } else { $out .= "</h2>\n". "<p>Ei mitään</p>\n"; } $out .= "</div>\n"; } else if ($haveData) { $totalHours = 0; $totalGrouped = 0; $dayHours = array(); for ($day = 0; $day < $classInfo["maxdays"]; $day++) { $dayHours[$day]["total"] = $dayHours[$day]["grouped"] = 0; for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { $id = $classHourTable[$hour][$day]; if ($id > 0) { $totalHours++; $dayHours[$day]["total"]++; if ($classDefs[$id]["grouped"]) { $totalGrouped++; $dayHours[$day]["grouped"]++; } } } } // Create the timetable table $out = "<p>".join("; ", $classInfo["general"])."</p>\n". "<div>Viikossa yhteensä <b>".$totalHours."</b> tuntia, joista <b>".$totalGrouped."</b> ryhmissä tai vuoroviikoin.</div>\n". "<table class=\"timetable\">". " <tr>\n <th></th>\n"; for ($day = 0; $day < $classInfo["maxdays"]; $day++) { $out .= " <th style=\"width: ".(100 / $classInfo["maxdays"]). "%;\" class=\"days\">".$dayNames[$day]."</th>\n"; } $out .= " </tr>\n"; for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { $out .= " <tr>\n". " <th class=\"hours\">".getHour($hour)."</th>\n"; for ($day = 0; $day < $classInfo["maxdays"]; $day++) { if (isset($classHourTable[$hour][$day])) { $h = $classHourTable[$hour][$day]; if ($h < 1) { if ($h > -9999) { $n = findHours($hour, $day, $h, TRUE); $out .= " <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]); $hours = findHours($hour, $day, $h, FALSE); if ($hours != $i["hours"]) { errorMsg("Internal error, cell $hour / $day : hour id $h hours ($hours) do not match ".$i["hours"]."!"); } $out .= " <td rowspan=\"".$hours. "\" class=\"".($isSplit || $i["grouped"] ? "clgrouped" : "clnormal")."\">". getHourInfo($isSplit, $d). "<div class=\"nhours\"><span>".$hours."h</span></div></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."); } $out .= " </tr>\n"; } $out .= printHourInfo($dayHours, $classInfo["maxdays"]). "</table>\n"; } // haveData $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka; printPageHeader($pageTitle); if ($miniMode) echo "<div id=\"mini\">\n"; echo "<form id=\"controls\" action=\"".$baseURI."\" method=\"get\"> <table> <tr> <td> <select name=\"luokka\"> "; foreach ($classes as $class) { echo " <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".htmlentities($class)."</option>\n"; } echo " </select> </td> <td><input type=\"submit\" value=\" Vaihda \" /></td> <td>[<a href=\"".$baseURI."?luokka=".$luokka; if ($miniMode) echo "\">Normaali"; else echo "&nyt\">Mini-info"; echo "</a>]</td> <td>[<a href=\"http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_".$luokka.".htm\">Alkuperäinen</a>]</td> </tr> </table> </form> "; echo "<h1>".$pageTitle."</h1>\n"; // Show error messages if ($errorSet) { echo "<ul>\n"; foreach ($errorMsgs as $msg) echo "<li>$msg</li>\n"; echo "</ul>\n"; } echo $out; echo "<div style=\"position: relative; top: 5em; font-size: 5pt;\"><hr /> Yhteydenotot <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. En vastaa mahdollisista virheistä tiedoissa! </div> "; if ($miniMode) echo "</div>\n"; printPageFooter(); // Dump the course data cache, but only if it has changed if ($cacheDirty) { // Create string containing the data $str = "<?\n\$cache = array(\n"; foreach ($cache as $id => $data) { $str .= " \"".addslashes($id)."\" => array(\"desc\" => \"". addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n"; } $str .= ");\n?>"; // Use locking to prevent concurrent access and dump data if (file_put_contents($cacheFile, $str, LOCK_EX) === FALSE) { // Can't do much anything here .. } } ?>