# HG changeset patch # User Matti Hamalainen # Date 1351031470 -10800 # Node ID aa201ddd33dd8e43257f922b44821420d9479685 # Parent 119f0cef6498c3d00d5cd21238d3bdd79086ad72 Cleaned up, v2.0 beta. diff -r 119f0cef6498 -r aa201ddd33dd index.php --- a/index.php Wed Oct 24 01:30:37 2012 +0300 +++ b/index.php Wed Oct 24 01:31:10 2012 +0300 @@ -1,4 +1,4 @@ - "lukcss", "prefix" => $baseURI."luk"); $pageCSSAlts = array("blue" => "1", "old" => "2", "purple" => "3"); -$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"; @@ -57,20 +41,35 @@ } } -function lukMakeHourStamp($hour, $sec = 0, $mon = 0, $day = 0, $year = 0) + +function lukGetDayFromTimestamp($stamp) +{ + $info = getdate($stamp); + $day = $info["wday"]; + return ($day > 0) ? $day - 1 : $day + 6; +} + + +function lukGetHourStamp($stamp) { - global $hourStamps; - if (isset($hourStamps[$hour])) - return mktime($hourStamps[$hour][0], $hourStamps[$hour][1], $sec, $mon, $year); + return date("H:i", mktime(0, 0, $stamp, 0, 0, 0)); +} + + +function lukGetHourStr($hour) +{ + global $classHourTimes; + if (isset($classHourTimes[$hour])) + { + return + "
". + lukGetHourStamp($classHourTimes[$hour]["start"]). + " - ". + lukGetHourStamp($classHourTimes[$hour]["end"]). + "

"; + } else - return mktime($hour + 8, 0, $sec, $mon, $year); -} - - -function getHour($hour) -{ - $stamp = lukMakeHourStamp($hour); - return "
".date("H:i", $stamp)." - ".date("H:i", $stamp + 45 * 60)."

"; + return "ERROR"; } @@ -79,13 +78,16 @@ global $cache, $infoURI, $cacheDirty, $internalCharset; // Check if course exists in cache - if (!isset($cache[$id])) { + if (!isset($cache[$id])) + { + // Not cached, try to fetch data $cacheDirty = TRUE; - // Not cached, try to fetch data $data = @file_get_contents($infoURI.$id); - if ($data !== FALSE) { + if ($data !== FALSE) + { $data = iconv("iso-8859-1", $internalCharset, $data); - if (preg_match("#(.+?)\s+(\d+)\s*op\s*#", $data, $m)) { + if (preg_match("#(.+?)\s+(\d+)\s*op\s*#", $data, $m)) + { // Add data to cache $cache[$id] = array("desc" => $m[1], "op" => intval($m[2])); } @@ -93,36 +95,112 @@ } if (isset($cache[$id])) + { return "".lukChEntities($cache[$id]["desc"]).""; + } else return lukChEntities($id); } +function lukGetClassInfo($isSplit, $data) +{ + if ($isSplit) + { + $out = "". + ""; + + for ($i = 2; $i < count($data); $i += 2) + { + $out .= ""; + } + + return $out."
".lukMatchCourse($data[0])."".lukMatchCourse($data[1])."
".lukChEntities($data[$i])."". + (isset($data[$i+1]) ? lukChEntities($data[$i+1]) : "")."
"; + } + else + { + $out = lukMatchCourse($data[0])."
"; + + for ($i = 1; $i < count($data); $i++) + { + $out .= lukChEntities($data[$i])."
"; + } + + return $out; + } +} + + +function lukGetClassInfoData($id) +{ + global $classInfo; + + if (isset($id)) + { + if ($id >= 0 && isset($classInfo[$id])) + { + $i = $classInfo[$id]; + $isSplit = preg_match("/^[A-Z]\d{6}$/", $i["data"][1]); + return lukGetClassInfo($isSplit, $i["data"]); + } + else + return "

Ei tunteja.

"; + } + else + return "

Ei mitään.

"; +} + + +function lukFindClass($day, $hour) +{ + global $classHourDefs, $classDayTable; + if (isset($classDayTable[$day])) + { + foreach ($classDayTable[$day] as $id) + { + if ($hour >= $classHourDefs[$id]["start"] && + $hour < $classHourDefs[$id]["start"] + $classHourDefs[$id]["hours"]) + return $id; + } + } + return 0; +} + + // Check for mini-info mode $miniMode = isset($_GET["nyt"]); // Check given parameters -if (isset($_GET["luokka"])) { +if (isset($_GET["luokka"])) +{ $luokka = $_GET["luokka"]; - if (lukCheckClassID($luokka) && !$miniMode) { + if (lukCheckClassID($luokka) && !$miniMode) + { setcookie("lukluokka", $luokka, time() + 365*24*60*60); // expire in a year } -} else -if (isset($_COOKIE["lukluokka"])) { +} +else +if (isset($_COOKIE["lukluokka"])) +{ $luokka = $_COOKIE["lukluokka"]; lukCheckClassID($luokka); -} else { +} +else +{ errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa ".$luokkaDefault."."); $luokka = $luokkaDefault; } -if (isset($_GET["next"])) { +if (isset($_GET["next"])) +{ $nextPeriod = TRUE; $dataFile = "cache-next/".$luokka.".data"; -} else { +} +else +{ $nextPeriod = FALSE; $dataFile = "cache/".$luokka.".data"; } @@ -134,8 +212,10 @@ // Try to read cachefile, if we can get file lock on it $fp = @fopen($cacheFile, "rb"); -if ($fp) { - if (flock($fp, LOCK_SH)) { +if ($fp) +{ + if (flock($fp, LOCK_SH)) + { require($cacheFile); flock($fp, LOCK_UN); } @@ -144,11 +224,14 @@ // Read classfile +$classes = array(); $fp = @fopen($classFile, "rb"); -$classes = array(); -if ($fp) { - if (flock($fp, LOCK_SH)) { - while (!feof($fp)) { +if ($fp) +{ + if (flock($fp, LOCK_SH)) + { + while (!feof($fp)) + { $str = trim(fgets($fp, 128)); if (strlen($str) > 2 && $str[0] != "#") $classes[] = $str; @@ -156,236 +239,100 @@ flock($fp, LOCK_UN); } fclose($fp); -} else { +} +else +{ errorMsg("Luokkien listaa ei löytynyt. Kokeile ladata sivu uudelleen hetken kuluttua."); } -if (!file_exists($dataFile)) { +if (!file_exists($dataFile)) +{ errorMsg("Luokan ".lukChEntities($luokka)." tietoja ei löytynyt! ". "Jos luokkakoodi on uusi, ilmestyy se järjestelmään seuraavan ". "päivityksen aikana. Luokkatiedot päivitetään kerran viikossa."); $haveData = FALSE; -} else { - require($dataFile); - $haveData = isset($classDefs); } - - -function lukResolveHours($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 lukFindNextActiveHour(&$j, &$i, $day, $hour, $chk) +else { - 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 lukGetHourInfo($isSplit, $d) -{ - if ($isSplit) { - $out = "". - ""; - - for ($i = 2; $i < count($d); $i += 2) - $out .= ""; - - return $out."
".lukMatchCourse($d[0])."".lukMatchCourse($d[1])."
".lukChEntities($d[$i])."".(isset($d[$i+1]) ? lukChEntities($d[$i+1]) : "")."
"; - } else { - $out = lukMatchCourse($d[0])."
"; - - for ($i = 1; $i < count($d); $i++) - $out .= lukChEntities($d[$i])."
"; - - return $out; - } -} - - -function lukGetHourInfoData($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 lukGetHourInfo($isSplit, $i["data"]); - } else - return "

Ei tunteja.

"; - } else - return "

Ei mitään.

"; - - return ""; + require($dataFile); + $haveData = isset($classInfo); } -$out = ""; - -if ($miniMode && $haveData) { - $stamp = time(); - $aika = getdate($stamp); - $day = $aika["wday"] - 1; - if ($day < 0) $day = 7; - $hour = $aika["hours"] - 8; - - $out = "
\n"; - - if ($hour >= 0 && $day >= 0 && $hour < sizeof($classHourTable)) - { - $id = $classHourTable[$hour][$day]; - $out .= - "

".$dayNames[$day]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."

\n". - lukGetHourInfoData($id)."\n"; - } - - - if (isset($id) && $id >= 0 && isset($classDefs[$id])) { - $i = $classDefs[$id]; - $start = lukMakeHourStamp($i["start"]); - } - -// echo "day=$day, hour=$hour, id=$id
\n"; +function lukNormalTimeTable() +{ + global $classInfo, $classHourDefs, $dayNames, $classHourTimes; - 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
\n"; - $found = lukFindNextActiveHour($nday, $nhour, $day, $hour, $id); - -// echo "nday=$nday, nhour=$nhour, id=$found
\n"; - - $out .= "

Seuraavaksi"; - if ($found > 0) { - $stamp = lukMakeHourStamp($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)."

\n". - lukGetHourInfoData($found)."\n"; - } else { - $out .= "\n". - "

Ei mitään

\n"; - } - - $out .= "
\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"]++; - } - } - } - } - +// $currStamp = time() + 9 * 60 * 60 + 18 * 60; + $currStamp = time(); + $currTime = $currStamp - mktime(0, 0, 0); + $currDay = lukGetDayFromTimestamp($currStamp); + // Create the timetable table $out = "

".join("; ", $classInfo["general"])."

\n". - "
Viikossa yhteensä ".$totalHours." tuntia, joista ".$totalGrouped." ryhmissä tai vuoroviikoin.
\n". + "
Viikossa yhteensä ".$classInfo["totalHours"]." tuntia.
\n". "". " \n \n"; - for ($day = 0; $day < $classInfo["maxdays"]; $day++) { - $out .= " \n"; + $cellWidth = 100 / $classInfo["maxDays"]; + if ($cellWidth > 25) $cellWidth = 25; + for ($day = 0; $day < $classInfo["maxDays"]; $day++) + { + $out .= " \n"; } $out .= " \n"; - for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) { + for ($hour = $classInfo["firstHour"]; $hour < $classInfo["lastHour"]; $hour++) + { $out .= " \n". - " \n"; - for ($day = 0; $day < $classInfo["maxdays"]; $day++) { - if (isset($classHourTable[$hour][$day])) { - $h = $classHourTable[$hour][$day]; + " \n"; + for ($day = 0; $day < $classInfo["maxDays"]; $day++) + { + $id = lukFindClass($day, $hour); + if ($id > 0) + { + $class = &$classHourDefs[$id]; + if (!isset($class["set"])) + { + $class["set"] = TRUE; + + $isSplit = preg_match("/^[A-Z]\d{6}$/", $class["data"][1]); + + $isActive = $day == $currDay && + $currTime >= $classHourTimes[$class["start"]]["start"] && + $currTime < $classHourTimes[$class["start"] + $class["hours"] - 1]["end"]; + + $out .= " \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 = lukResolveHours($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 .= " \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 .= + lukGetClassInfo($isSplit, $class["data"]). + "\n"; + } + } + else + { + $out .= " \n"; + } } $out .= " \n"; } - $out .= "
".$dayNames[$day]."".$dayNames[$day]."
".getHour($hour)."".lukGetHourStr($hour).""; + +// if ($isActive) +// $out .= sprintf("%d : %d <> %d, %d -- ", $day, $currTime, $classHourTimes[$class["start"]]["start"], $classHourTimes[$class["start"] + $class["hours"] - 1]["end"]); - if ($h < 1) { - if ($h > -9999) { - $n = lukResolveHours($hour, $day, $h, TRUE); - $out .= " ". - lukGetHourInfo($isSplit, $d). - "
".$hours."h
\n"; - -} // haveData + return $out."\n"; +} +// +// +// $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka; printPageHeader($pageTitle); -echo -"
+echo "
@@ -418,19 +365,24 @@ echo "

".$pageTitle."

\n"; // Show error messages -if ($errorSet) { +if ($errorSet) +{ echo "
    \n"; foreach ($errorMsgs as $msg) echo "
  • $msg
  • \n"; echo "
\n"; } -echo $out; +if ($haveData) +{ + echo lukNormalTimeTable(); +} echo "
Yhteydenotot ccr @ IRCNet tai ccr at tnsp dot org. En vastaa mahdollisista virheistä tiedoissa!
+
Lukkari v2.0 beta
Style: "; foreach ($pageCSSAlts as $name => $id) echo "".$name."";