changeset 59:aa201ddd33dd

Cleaned up, v2.0 beta.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 24 Oct 2012 01:31:10 +0300
parents 119f0cef6498
children cdc04572800c
files index.php
diffstat 1 files changed, 198 insertions(+), 246 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
- <?php
+<?php
 //
 // Yes, this is horrible. :|
 //
@@ -7,7 +7,7 @@
 $luokkaDefault = "TTE9SNO";
 $classFile = "luokat.txt";
 $cacheFile = "coursecache.txt";
-$baseURI = "http://tnsp.org/luk/";
+$baseURI = "http://tnsp.org/luk2/";
 //$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");
@@ -15,22 +15,6 @@
 $pageCSSData = array("cookie" => "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
+      "<br />".
+      lukGetHourStamp($classHourTimes[$hour]["start"]).
+      " - ".
+      lukGetHourStamp($classHourTimes[$hour]["end"]).
+      "<br /><br />";
+  }
   else
-    return mktime($hour + 8, 0, $sec, $mon, $year);
-}
-  
-
-function getHour($hour)
-{
-  $stamp = lukMakeHourStamp($hour);
-  return "<br />".date("H:i", $stamp)." - ".date("H:i", $stamp + 45 * 60)."<br /><br />";
+    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("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*op\s*</strong></td>#", $data, $m)) {
+      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]));
       }
@@ -93,36 +95,112 @@
   }
   
   if (isset($cache[$id]))
+  {
     return "<a target=\"_blank\" title=\"".lukChEntities($id." - ".$cache[$id]["op"]." op").
     "\" href=\"".lukChEntities($infoURI.$id)."\">".lukChEntities($cache[$id]["desc"])."</a>";
+  }
   else
     return lukChEntities($id);
 }
 
 
+function lukGetClassInfo($isSplit, $data)
+{
+  if ($isSplit)
+  {
+    $out = "<table>".
+    "<tr><td>".lukMatchCourse($data[0])."</td><td>".lukMatchCourse($data[1])."</td></tr>";
+
+    for ($i = 2; $i < count($data); $i += 2)
+    {
+      $out .= "<tr><td>".lukChEntities($data[$i])."</td><td>".
+        (isset($data[$i+1]) ? lukChEntities($data[$i+1]) : "")."</td></tr>";
+    }
+    
+    return $out."</table>";
+  }
+  else
+  {
+    $out = lukMatchCourse($data[0])."<br />";
+
+    for ($i = 1; $i < count($data); $i++)
+    {
+      $out .= lukChEntities($data[$i])."<br />";
+    }
+
+    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 "<p>Ei tunteja.</p>";
+  }
+  else
+    return "<p>Ei mitään.</p>";
+}
+
+
+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 <b>".$luokkaDefault."</b>.");
   $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 = "<table>".
-    "<tr><td>".lukMatchCourse($d[0])."</td><td>".lukMatchCourse($d[1])."</td></tr>";
-
-    for ($i = 2; $i < count($d); $i += 2)
-      $out .= "<tr><td>".lukChEntities($d[$i])."</td><td>".(isset($d[$i+1]) ? lukChEntities($d[$i+1]) : "")."</td></tr>";
-    
-    return $out."</table>";
-  } else {
-    $out = lukMatchCourse($d[0])."<br />";
-
-    for ($i = 1; $i < count($d); $i++)
-      $out .= lukChEntities($d[$i])."<br />";
-
-    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 "<p>Ei tunteja.</p>";
-  } else
-    return "<p>Ei mitään.</p>";
-
-  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 = "<div class=\"content\">\n";
- 
-  if ($hour >= 0 && $day >= 0 && $hour < sizeof($classHourTable))
-  {
-    $id = $classHourTable[$hour][$day];
-    $out .=
-    "<h2>".$dayNames[$day]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\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<br />\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<br />\n";
-  $found = lukFindNextActiveHour($nday, $nhour, $day, $hour, $id);
-
-//  echo "nday=$nday, nhour=$nhour, id=$found<br />\n";
-
-  $out .= "<h2>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)."</h2>\n".
-    lukGetHourInfoData($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"]++;
-        }
-      }
-    }
-  }
-
+//  $currStamp = time() + 9 * 60 * 60 + 18 * 60;
+  $currStamp = time();
+  $currTime = $currStamp - mktime(0, 0, 0);
+  $currDay = lukGetDayFromTimestamp($currStamp);
+  
   // 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".
+  "<div>Viikossa yhteensä <b>".$classInfo["totalHours"]."</b> tuntia.</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";
+  $cellWidth = 100 / $classInfo["maxDays"];
+  if ($cellWidth > 25) $cellWidth = 25;
+  for ($day = 0; $day < $classInfo["maxDays"]; $day++)
+  {
+    $out .=  "  <th style=\"width: ".$cellWidth."%;\" class=\"days\">".$dayNames[$day]."</th>\n";
   }
   $out .= " </tr>\n";
 
-  for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) {
+  for ($hour = $classInfo["firstHour"]; $hour < $classInfo["lastHour"]; $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];
+    "  <th class=\"hours\">".lukGetHourStr($hour)."</th>\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 .= "  <td rowspan=\"".$class["hours"].
+          "\" class=\"".($class["grouped"] ? "clgrouped" : ($isActive ? "clactive" : "clnormal"))."\">";
+
+//          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 .= "  <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 = 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 .= "  <td rowspan=\"".$hours.
-            "\" class=\"".($isSplit || $i["grouped"] ? "clgrouped" : "clnormal")."\">".
-            lukGetHourInfo($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 .=
+          lukGetClassInfo($isSplit, $class["data"]).
+          "</td>\n";
+        }
+      }
+      else
+      {
+        $out .= "  <td class=\"clnothing\"></td>\n";
+      }
     }
     $out .= " </tr>\n";
   }
 
-  $out .= "</table>\n";
-
-} // haveData
+  return $out."</table>\n";
+}
 
 
+//
+//
+//
 $pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka;
 printPageHeader($pageTitle);
 
-echo
-"<form id=\"controls\" action=\"".$baseURI."\" method=\"get\">
+echo "<form id=\"controls\" action=\"".$baseURI."\" method=\"get\">
  <table>
   <tr>
    <th>
@@ -418,19 +365,24 @@
 echo "<h1>".$pageTitle."</h1>\n";
 
 // Show error messages
-if ($errorSet) {
+if ($errorSet)
+{
   echo "<ul>\n";
   foreach ($errorMsgs as $msg)
     echo "<li>$msg</li>\n";
   echo "</ul>\n";
 }
 
-echo $out;
+if ($haveData)
+{
+  echo lukNormalTimeTable();
+}
 
 echo "<div id=\"footer\">
 Yhteydenotot <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. En vastaa mahdollisista virheistä tiedoissa!
 </div>
 <div id=\"csssel\">
+<div id=\"ctitle\">Lukkari v2.0 beta</div>
 Style: ";
 foreach ($pageCSSAlts as $name => $id)
   echo "<a href=\"?css=".$id."\">".$name."</a>";