changeset 5:3d6a83eaa2ba

Improvements.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 11 Jan 2011 21:58:35 +0200
parents dd2bce7ec0c8
children 7fca87c41e17
files beta.php luk.css
diffstat 2 files changed, 113 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/beta.php	Tue Jan 11 20:55:09 2011 +0200
+++ b/beta.php	Tue Jan 11 21:58:35 2011 +0200
@@ -2,26 +2,23 @@
 $pageCSS = array("http://tnsp.org/docs1.css", "luk.css");
 $pageCharset = "iso-8859-15";
 $luokkaDefault = "TTE9SNO";
+$dataDir = "cache/";
+$classFile = "luokat.txt";
 $mapFile = "kartta.png";
-$classFile = "luokkatilat.txt";
+$roomFile = "luokkatilat.txt";
 $cacheFile = "coursecache.txt";
-$baseURI = "http://tnsp.org/luk/?";
+$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=";
 
 $showDays = 6;
 $dayNames = array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai");
 
-
+// Include framework
 require "mcommon.inc.php";
 require "merrors.inc.php";
 
 
-function getHour($hour)
-{
-  return "<br />".($hour + 8).":15 - ".($hour + 9).":00<br /><br />";
-}
-
-
+// Helper functions
 function checkClassID(&$id)
 {
   global $luokkaDefault;
@@ -55,13 +52,19 @@
   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);
   }
 }
 
   
+function getHour($hour)
+{
+  return "<br />".($hour + 8).":15 - ".($hour + 9).":00<br /><br />";
+}
+
+
 function matchCourse($id)
 {
   global $cache, $infoURI, $cacheDirty;
@@ -90,7 +93,7 @@
 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];
 }
 
 
@@ -109,55 +112,80 @@
   fclose($fp);
 }
 
-
-if (file_exists($luokka.".data")) {
-  require($luokka.".data");
-} else {
-  errorMsg("Luokan ".htmlentities($luokka)." datatiedostoa ei löytynyt!");
+$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);
 }
 
-$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;
-  }
+if (!file_exists($dataDir.$luokka.".data")) {
+  errorMsg("Luokan ".htmlentities($luokka)." datatiedostoa ei löytynyt!");
+  $haveData = FALSE;
+} else {
+  require($dataDir.$luokka.".data");
+  $haveData = isset($classDefs);
 }
 
-for ($day = $showDays - 1; $day >= 0; $day--) {
-  if ($dayHours[$day]["total"] == 0)
-    $showDays--;
-  else
-    break;
+
+function printHourInfo($dayHours, $showDays)
+{
+  $out = " <tr>\n  <td>Tunteja (<b>ryhmä</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";
 }
 
 
+if ($haveData) {
+  $totalHours = 0;
+  $totalGrouped = 0;
+  $dayHours = array();
+  for ($day = 0; $day < $showDays; $day++) 
+    $dayHours[$day]["total"] = $dayHours[$day]["grouped"] = 0;
 
-$pageTitle = $luokka." / ".join("; ", $classInfo["info"]);
-printPageHeader($pageTitle);
-echo "<h1>".$pageTitle."</h1>\n".
-"<p>".join("; ", $classInfo["general"])."</p>\n";
+  foreach ($classDefs as $id => $data) {
+    $h = $data["hours"];
+  
+    $totalHours += $h;
+    $dayHours[$data["day"]]["total"] += $h;
 
-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";
+    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;
+  }
+
+
+  // Create the timetable table
+  $out =
+  "<p>".join("; ", $classInfo["general"])."</p>\n".
+  "<table class=\"timetable\">".
+  " <tr>\n  <th></th>\n";
+  for ($day = 0; $day < $showDays; $day++) {
+    $out .=  "  <th class=\"days\">".$dayNames[$day]."</th>\n";
+  }
+  $out .= " </tr>\n";
 
 for ($hour = 0; $hour < $classInfo["maxhours"]; $hour++) {
-  echo " <tr>\n";
-  echo "  <th class=\"hours\">".getHour($hour)."</th>\n";
+  $out .= " <tr>\n".
+  "  <th class=\"hours\">".getHour($hour)."</th>\n";
   for ($day = 0; $day < $showDays; $day++) {
     if (isset($classHourTable[$hour][$day])) {
       $h = $classHourTable[$hour][$day];
@@ -171,7 +199,7 @@
             $n++;
           } else
             break;
-          echo "  <td rowspan=\"".$n."\" class=\"clnothing\"></td>\n";
+          $out .= "  <td rowspan=\"".$n."\" class=\"clnothing\"></td>\n";
         }
       } else
       if (isset($classDefs[$h])) {
@@ -182,38 +210,47 @@
           
           $isSplit = preg_match("/^[A-Z]\d{6}$/", $d[1]);
           
-          echo "  <td rowspan=\"".$i["hours"].
+          $out .= "  <td rowspan=\"".$i["hours"].
           "\" class=\"".($isSplit || $i["grouped"] ? "clgrouped" : "clnormal")."\">";
           
           if ($isSplit) {
-            echo "<table>".
+            $out .= "<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>";
+              $out .= "<tr><td>".htmlentities($d[$j])."</td><td>".(isset($d[$j+1]) ? htmlentities($d[$j+1]) : "")."</td></tr>";
+            $out .= "</table>";
           } else {
-            echo matchCourse($d[0])."<br />";
+            $out .= matchCourse($d[0])."<br />";
             for ($j = 1; $j < count($d); $j++)
-              echo htmlentities($d[$j])."<br />";
+              $out .= htmlentities($d[$j])."<br />";
           }
-          echo "</td>\n";
+          $out .= "<div class=\"nhours\"><span>".$i["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.");
   }
-  echo " </tr>\n";
+  $out .= " </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";
+  $out .= printHourInfo($dayHours, $showDays).
+  "</table>";
+
+} // haveData
+
+
+$pageTitle = $haveData ? $luokka." / ".join("; ", $classInfo["info"]) : $luokka;
+printPageHeader($pageTitle);
+echo "<h1>".$pageTitle."</h1>\n";
+
+echo "<form action=\"".$baseURI."\" method=\"get\">\n".
+" <select name=\"luokka\">\n";
+
+foreach ($classes as $class) {
+  echo "  <option value=\"".$class."\">".htmlentities($class)."</option>\n";
 }
-echo " </tr>\n";
-
-echo "</table>";
-
+echo " </select>\n <input type=\"submit\" value=\" Vaihda \" />\n</form>\n";
 
 // Show error messages
 if ($errorSet) {
@@ -223,6 +260,8 @@
   echo "</ul>\n";
 }
 
+echo $out;
+
 printPageFooter();
 
 
--- a/luk.css	Tue Jan 11 20:55:09 2011 +0200
+++ b/luk.css	Tue Jan 11 21:58:35 2011 +0200
@@ -32,3 +32,13 @@
 
 td.clnormal {
 }
+
+div.nhours {
+	text-align: right;
+}
+
+div.nhours span {
+	background: black;
+	padding: 2pt;
+	border: 1px solid gray;
+}