changeset 15:45987abe0d10

Move beta code into production. :P
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 12 Jan 2011 01:03:36 +0200
parents a2c61b010680
children ea9558465ad6
files beta.php index.php
diffstat 2 files changed, 343 insertions(+), 470 deletions(-) [+]
line wrap: on
line diff
--- a/beta.php	Wed Jan 12 01:02:28 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,418 +0,0 @@
-<?
-$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/beta.php";
-$infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&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\S+|ccr#", $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) && !isset($_["nyt"])) {
-    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&auml;ytt&ouml;moodi ei viel&auml; tuettu.<br />\n";
-  exit;
-
-  $fp = @fopen($roomFile, "rb");
-  if ($fp) {
-    fclose($fp);
-  }
-}
-
-  
-function getHour($hour)
-{
-  global $hourStamps;
-  $stamp = mktime($hourStamps[$hour][0], $hourStamps[$hour][1], 0);
-  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);
-}
-
-$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);
-}
-
-
-if (!file_exists($dataDir.$luokka.".data")) {
-  errorMsg("Luokan ".htmlentities($luokka)." datatiedostoa ei löytynyt!");
-  $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++)
-    for ($i = $hour; $i < $classInfo["maxhours"]; $i++) {
-//      echo "chk=$chk - i=$i / j=$j : ht=".$classHourTable[$i][$j]."<br />\n";
-      if (($chk > 0 && $classHourTable[$i][$j] != $chk) || $classHourTable[$i][$j] > 0)
-        return $classHourTable[$i][$j];
-    }
-
-  return -1;
-}
-
-
-function getHourInfo($id)
-{
-  global $classDefs;
-
-  if (isset($id)) {
-    if ($id >= 0 && isset($classDefs[$id])) {
-      $i = $classDefs[$id];
-      $d = $i["data"];
-      $out = "";
-      
-      $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]);
-      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>";
-            $out .= "</table>";
-          } else {
-            $out .= matchCourse($d[0])."<br />";
-            for ($j = 1; $j < count($d); $j++)
-              $out .= htmlentities($d[$j])."<br />";
-          }
-      
-      return $out;
-    } else
-      return "<p>Ei tunteja.</p>";
-  } else
-    return "<p>Ei mitään.</p>";
-
-  return "";
-}
-
-
-if (isset($_GET["nyt"])) {
-  $aika = getdate();
-  $hour = $aika["hours"] - 8;
-  $day = $aika["wday"] - 1;
-  if ($day < 0) $day = 7;
-
-  $chk = $classHourTable[$hour][$day];
-
-  $out = "<h2>".$dayNames[$day]." ".$aika["mday"].".".$aika["mon"].".".$aika["year"]." - klo ".$aika["hours"].":".$aika["minutes"]."</h2>\n".
-  getHourInfo($chk)."\n";
-  
-//  $out .= "hour=".$hour."/day=".$day."<br />\n";
-  if ($day >= $classInfo["maxdays"]) {
-    $day = $hour = 0;
-    $chk = -1;
-  } else
-  if ($hour < 0) {
-    $hour = 0;
-    $chk = -1;
-  } else
-  if ($hour >= $classInfo["maxhours"]) {
-    $day++;
-    $hour = 0;
-    $chk = -1;
-  }
-//  $out .= "hour=".$hour."/day=".$day."<br />\n";
-
-  $found = findNextHour($nday, $nhour, $day, $hour, $chk);
-
-//  $out .= "hour=".$nhour."/day=".$nday."/found=".$found."<br />\n";
-
-  if ($found > 0) {
-    $stamp = mktime(8 + $nhour, 15, 0, $aika["mon"], $aika["mday"] + $nday - $day, $aika["year"]);
-    $aika = getdate($stamp);
-    $hour = $aika["hours"] - 8;
-    $day = $aika["wday"] - 1;
-    if ($day < 0) $day = 7;
-    $out .= "<h2>Seuraavaksi: ".$dayNames[$nday]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n".
-    getHourInfo($found)."\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 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")."\">";
-          
-          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>";
-            $out .= "</table>";
-          } else {
-            $out .= matchCourse($d[0])."<br />";
-            for ($j = 1; $j < count($d); $j++)
-              $out .= htmlentities($d[$j])."<br />";
-          }
-          $out .= "<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);
-echo "<h1>".$pageTitle."</h1>\n";
-
-echo "<form action=\"".$baseURI."\" method=\"get\">\n".
-" <table>\n".
-"  <tr>\n".
-"   <td>\n".
-"    <select name=\"luokka\">\n";
-
-foreach ($classes as $class) {
-  echo "     <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".htmlentities($class)."</option>\n";
-}
-echo
-"    </select>\n".
-"   </td>\n".
-"   <td><input type=\"submit\" value=\" Vaihda \" /></td>\n".
-"   <td>[<a href=\"".$baseURI."?luokka=".$luokka."&amp;nyt\">Mini-info moodi</a>]</td>\n".
-"  </tr>\n".
-" </table>\n".
-"</form>\n";
-
-
-// Show error messages
-if ($errorSet) {
-  echo "<ul>\n";
-  foreach ($errorMsgs as $msg)
-    echo "<li>$msg</li>\n";
-  echo "</ul>\n";
-}
-
-echo $out;
-
-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
--- a/index.php	Wed Jan 12 01:02:28 2011 +0200
+++ b/index.php	Wed Jan 12 01:03:36 2011 +0200
@@ -1,57 +1,93 @@
 <?
+$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/beta.php";
+$infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&koodi1=";
+$dayNames = array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai");
 
-$mapfile = "kartta.png";
-$classfile = "luokkatilat.txt";
-$cachefile = "coursecache.txt";
-$baseURI = "http://tnsp.org/luk/?";
-$lukuURI = "http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_";
-$infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&koodi1=";
+$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\S+|ccr#", $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
-$luokka = isset($_GET["luokka"]) ? $_GET["luokka"] : "TTE9SNO";
+if (isset($_GET["luokka"])) {
+  $luokka = $_GET["luokka"];
+  if (checkClassID($luokka) && !isset($_["nyt"])) {
+    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 (!preg_match("#^[A-Z]{3}\d\S+#", $luokka)) {
-  echo "Virhe! Luokan t&auml;ytyy olla muotoa XXXnXXX.<br />\n";
-  exit;
-}
 
 if (isset($_GET["tila"])) {
   $tila = $_GET["tila"];
   echo "Luokkatilan n&auml;ytt&ouml;moodi ei viel&auml; tuettu.<br />\n";
   exit;
 
-  $fp = @fopen($classfile, "rb");
+  $fp = @fopen($roomFile, "rb");
   if ($fp) {
     fclose($fp);
   }
 }
 
   
-// Global cache for course data
-$cache = array();
-$dirty = 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);
+function getHour($hour)
+{
+  global $hourStamps;
+  $stamp = mktime($hourStamps[$hour][0], $hourStamps[$hour][1], 0);
+  return "<br />".date("H:i", $stamp)." - ".date("H:i", $stamp + 45 * 60)."<br /><br />";
 }
 
 
-function match_course($matches)
+function matchCourse($id)
 {
-  global $cache, $infoURI, $dirty;
-
-  $id = $matches[1];
+  global $cache, $infoURI, $cacheDirty;
 
   // Check if course exists in cache
   if (!isset($cache[$id])) {
-    $dirty = TRUE;
+    $cacheDirty = TRUE;
     // Not cached, try to fetch data
     $data = @file_get_contents($infoURI.$id);
     if ($data !== FALSE) {
@@ -70,44 +106,299 @@
 }
 
 
-function match_class($matches)
+function matchClass($matches)
 {
   global $baseURI;
-  return "<b><a href=\"".$baseURI."tila=".$matches[1]."\">".$matches[1]."</a></b> ".$matches[2];
+  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);
+}
+
+$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);
+}
+
+
+if (!file_exists($dataDir.$luokka.".data")) {
+  errorMsg("Luokan ".htmlentities($luokka)." datatiedostoa ei löytynyt!");
+  $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++)
+    for ($i = $hour; $i < $classInfo["maxhours"]; $i++) {
+//      echo "chk=$chk - i=$i / j=$j : ht=".$classHourTable[$i][$j]."<br />\n";
+      if (($chk > 0 && $classHourTable[$i][$j] != $chk) || $classHourTable[$i][$j] > 0)
+        return $classHourTable[$i][$j];
+    }
+
+  return -1;
+}
+
+
+function getHourInfo($id)
+{
+  global $classDefs;
+
+  if (isset($id)) {
+    if ($id >= 0 && isset($classDefs[$id])) {
+      $i = $classDefs[$id];
+      $d = $i["data"];
+      $out = "";
+      
+      $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]);
+      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>";
+            $out .= "</table>";
+          } else {
+            $out .= matchCourse($d[0])."<br />";
+            for ($j = 1; $j < count($d); $j++)
+              $out .= htmlentities($d[$j])."<br />";
+          }
+      
+      return $out;
+    } else
+      return "<p>Ei tunteja.</p>";
+  } else
+    return "<p>Ei mitään.</p>";
+
+  return "";
 }
 
 
-/* Fetch HTML data and replace occurances of course IDs
- * with information and a link
- */
-if (file_exists("OR_".$luokka.".htm"))
-  $data = @file_get_contents("OR_".$luokka.".htm");
-else
-  $data = @file_get_contents($lukuURI.$luokka.".htm");
+if (isset($_GET["nyt"])) {
+  $aika = getdate();
+  $hour = $aika["hours"] - 8;
+  $day = $aika["wday"] - 1;
+  if ($day < 0) $day = 7;
 
-if ($data !== FALSE) {
-  $data = preg_replace_callback("#<B>([A-Z]\d+)\.?</B>#", match_course, $data);
+  $chk = $classHourTable[$hour][$day];
+
+  $out = "<h2>".$dayNames[$day]." ".$aika["mday"].".".$aika["mon"].".".$aika["year"]." - klo ".$aika["hours"].":".$aika["minutes"]."</h2>\n".
+  getHourInfo($chk)."\n";
   
-  $data = preg_replace_callback("#<B>(\d{4}|ATK\d+|[A-Z]{2,3}LAB\d+|[A-Z]{2,3}LAB|Audit\d+)(\(\d+\))?</B>#", match_class, $data);
+//  $out .= "hour=".$hour."/day=".$day."<br />\n";
+  if ($day >= $classInfo["maxdays"]) {
+    $day = $hour = 0;
+    $chk = -1;
+  } else
+  if ($hour < 0) {
+    $hour = 0;
+    $chk = -1;
+  } else
+  if ($hour >= $classInfo["maxhours"]) {
+    $day++;
+    $hour = 0;
+    $chk = -1;
+  }
+//  $out .= "hour=".$hour."/day=".$day."<br />\n";
+
+  $found = findNextHour($nday, $nhour, $day, $hour, $chk);
+
+//  $out .= "hour=".$nhour."/day=".$nday."/found=".$found."<br />\n";
+
+  if ($found > 0) {
+    $stamp = mktime(8 + $nhour, 15, 0, $aika["mon"], $aika["mday"] + $nday - $day, $aika["year"]);
+    $aika = getdate($stamp);
+    $hour = $aika["hours"] - 8;
+    $day = $aika["wday"] - 1;
+    if ($day < 0) $day = 7;
+    $out .= "<h2>Seuraavaksi: ".$dayNames[$nday]." ".date("d.n.Y", $stamp)." - klo ".date("H:i", $stamp)."</h2>\n".
+    getHourInfo($found)."\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 class=\"days\">".$dayNames[$day]."</th>\n";
+  }
+  $out .= " </tr>\n";
 
-  $data = preg_replace("#<body class=tt>#", "<body class=tt>\n".
-  "<a href=\"".$lukuURI.$luokka.".htm\">[orig]</a>\n, Luokat: ".
-  "<a href=\"".$baseURI."luokka=TTE0SNO"."\">[TTE0SNO]</a>\n".
-  "<a href=\"".$baseURI."luokka=TTE9SNO"."\">[TTE9SNO]</a>\n", $data);
-  echo $data;
-} else {
-  echo "Luokan datatiedostoa <b>".htmlentities($lukuURI.$luokka.".htm")."</b> ei saatu.<br />\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")."\">";
+          
+          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>";
+            $out .= "</table>";
+          } else {
+            $out .= matchCourse($d[0])."<br />";
+            for ($j = 1; $j < count($d); $j++)
+              $out .= htmlentities($d[$j])."<br />";
+          }
+          $out .= "<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);
+echo "<h1>".$pageTitle."</h1>\n";
+
+echo "<form action=\"".$baseURI."\" method=\"get\">\n".
+" <table>\n".
+"  <tr>\n".
+"   <td>\n".
+"    <select name=\"luokka\">\n";
+
+foreach ($classes as $class) {
+  echo "     <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".htmlentities($class)."</option>\n";
+}
+echo
+"    </select>\n".
+"   </td>\n".
+"   <td><input type=\"submit\" value=\" Vaihda \" /></td>\n".
+"   <td>[<a href=\"".$baseURI."?luokka=".$luokka."&amp;nyt\">Mini-info moodi</a>]</td>\n".
+"  </tr>\n".
+" </table>\n".
+"</form>\n";
+
+
+// Show error messages
+if ($errorSet) {
+  echo "<ul>\n";
+  foreach ($errorMsgs as $msg)
+    echo "<li>$msg</li>\n";
+  echo "</ul>\n";
+}
+
+echo $out;
+
+printPageFooter();
+
 
 // Dump the course data cache, but only if it has changed
-if ($dirty) {
+if ($cacheDirty) {
   // First try append mode
-  $fp = @fopen($cachefile, "rb+");
+  $fp = @fopen($cacheFile, "rb+");
 
   // If file didn't exist, try write mode
   if (!$fp)
-    $fp = @fopen($cachefile, "wb");
+    $fp = @fopen($cacheFile, "wb");
 
   if ($fp) {
     // Use locking to prevent concurrent access and dump data