<?php
die(getcwd());
$row_labels = array(
  "Week",
  "Date",
  "Title",
);
$rows = array(
  array("Week 3","1/23/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-3-learning-by-designing' target='new'>Week 3: Learning by Designing</a>"),
  array("Week 4","1/30/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-4-studio-1-studio-2-scratch-gallery' target='new'>Week 4: Studio 2 – Scratch Gallery</a>"),
  array("Week 5","2/6/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-5-defining-new-media-interactivity-immersion-telepresence' target='new'>Week 5: Defining New Media: Interactivity, Immersion & Telepresence</a>"),
  array("Week 6","2/13/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-6-studio-3-your-choice' target='new'>Week 6: Studio 3 – Your Choice</a>"),
  array("Week 7","2/20/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-7-web-2-0-learning-in-a-participatory-culture' target='new'>Week 7: Web 2.0 & Learning in a Participatory Culture</a>"),
  array("Week 8","2/27/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-8-studio-4-your-choice' target='new'>Week 8: Studio 4 – Your Choice</a>"),
  array("Week 10","3/20/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-9-the-arts-computation-digital-creativity' target='new'>Week 10: The Arts, Computation, & Digital Creativity</a>"),
  array("Week 11","3/27/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-10-diy-project' target='new'>Week 11: Studio 5 – DIY Project</a>"),
  array("Week 12","4/3/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-11-from-atoms-to-bits-and-back-again' target='new'>Week 12: From Atoms to Bits and back again</a>"),
  array("Week 13","4/10/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-12-studio-6-diy-project' target='new'>Week 13: Studio 6 – DIY Project</a>"),
  array("Week 14","4/17/11","<a href='http://www.iu.edu/~lsmt/peppler-p574/?session=week-13-informal-learning-diy-practice' target='new'>Week 14: IN|FORMAL Learning & DIY Practice</a>"),
);
$cols = array(
  "Moderator 1",
  "Moderator 2",
);

$cachefile = "../../website_db/moderator_form_cache.txt";

// attempt to load the cache file
$data = false;
if(file_exists($cachefile)) $data = file_get_contents($cachefile);
if($data === false || strlen($data) == 0) $data = array();
else $data = unserialize($data);

// if this is a submit, save any changes
$saved = false;
if($_SERVER['REQUEST_METHOD'] == "POST") {
  foreach($_POST as $key => $value)
    if(substr($key,0,5) == "data_" && strlen(trim($value)) && (!isset($data[$key]) || empty($data[$key])))
      $data[$key] = trim($value);
  $saved = file_put_contents($cachefile,serialize($data));
  if($saved === false) echo "<p style='color:red'>FAILED TO SAVE CHANGES, the server failed to save changes to the data file.</p>";
}

if($_SERVER['REQUEST_METHOD'] == "POST" && $saved)
  echo "<p style='color:green'>Your submission has been saved. Thank you!</p>";

echo "<form action='" . $_SERVER['REQUEST_URI'] . "' method='POST'>";
echo "<p><input type='submit' value='Update'></p>";
echo "<table cellspacing='1' cellpadding='4' border='1'>";
echo "<thead><tr><td>".implode("</td><td>",$row_labels)."</td><td>".implode("</td><td>",$cols)."</td></tr></thead>";
echo "<tbody>";
for($x = 0; sizeof($rows) > $x; $x++) {
  echo "<tr><td>".implode("</td><td>",$rows[$x])."</td>";
  for($y = 0; sizeof($cols) > $y; $y++)
    echo "<td><input type='text' name='data_".$x."_".$y."' value='".(isset($data['data_'.$x.'_'.$y]) && !empty($data['data_'.$x.'_'.$y]) ? htmlspecialchars($data['data_'.$x.'_'.$y],ENT_QUOTES)."' disabled" : "")."' size='30'></td>";
  echo "</tr>";
}
echo "</tbody></table>";
echo "<p><input type='submit' value='Update'></p>";
echo "</form>";
?>
