<?php 

error_reporting(E_ALL); # DEBUG
ini_set('display_errors', '1');

function parseSubmission($name, &$assignment, &$time, &$type) {
   $res = preg_match("/^assignment_(\\d\\d\\d)__(\\d+.\\d+|\\d+).(lyx|pdf)\$/",
          $name, $a);
   if (! $res) return false;

   $assignment = $a[1];
   $time = $a[2];
   $type = $a[3];
   return true;
}
 
$path = "/home/zunino/stuff/uploads";
chdir($path);

$q = NULL;
if (isset($_GET["q"]))     $q = $_GET["q"];
$fetch = NULL;
if (isset($_GET["fetch"])) $fetch = $_GET["fetch"];
$select = NULL;
if (isset($_GET["select"])) $select = $_GET["select"];

# sanity checks
if ($q) {
   $res = parseSubmission($q, $assignment, $time, $type); 
   if (! $res) { die("wrong submission name"); }
}

if ($q and $fetch) {
   # file fetch query
   if ($type == "pdf") {
      header("Content-type: application/pdf");
      header("Content-Disposition: inline; filename=" . $q);
   } else if ($type == "lyx") {
      header("Content-type: application/x-lyx");
      header("Content-Disposition: inline; filename=" . $q);
   } else {
      die("wrong submission type");
   }
   readfile($q); # forward file
   exit(0);
}

# browsing submissions
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>Submissions List</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Submitted Assignments</h1>
<?php

if ($q) {
   echo "<p>Showing only the requested file. " .
        "(<a href=\"view.php\">show all</a>). PDFs may appear only after " .
        "several days.</p>";
} else {
   echo "<p>Showing all anonymous submissions. PDFs may require several " .
        "days to appear -- be patient.</p>";
}


function decodePDF($name) {
   $pdffile = preg_replace("/\\.lyx\$/", ".pdf", $name);
   if (is_file($pdffile)) {
      return " <a href=\"view.php?q=" . urlencode($pdffile) .
             "&amp;fetch=1\">PDF</a>";
   } else {
      return " (no PDF has been generated, yet)";
   }
}

function getMark($name) {
   $eval = preg_replace("/\\.lyx\$/", ".eval", $name);
   if (is_file($eval)) {
     $s = trim(file_get_contents($eval));
     if ($s == "a" or $s == "A") return "correct";
     if ($s == "b" or $s == "B") return "partially correct";
     if ($s == "c" or $s == "C") return "wrong";
     if ($s == "") return "(not checked)";
     return htmlspecialchars($s);
   } else {
     return "(not checked)";
   }
}


#function decodeMark($mark) {
#   if ($mark == "a" or $mark == "A") return " correct";
#   if ($mark == "b" or $mark == "B") return " partially correct";
#   if ($mark == "c" or $mark == "C") return " wrong";
#   return " (not yet checked)";
#}

# echo "<p>select=" . $select . "</p>\n";

echo "<table class=\"results\">\n" .
     "<tr><th>Assignment</th><th>Submission time</th><th>PDF</th></tr>";

$found = false;
$a = glob("assignment_*.lyx");
foreach ($a as $filename) {
   $filename = basename($filename);

   $res = parseSubmission($filename, $a2, $t2, $ty2);
   if (! $res) {
      echo "<tr><td>Assignment unknown " . $filename . "</td></tr>\n";
   } else if ((! $q and ($select==NULL or $select == $a2)) or 
              ($q and ($assignment == $a2 and $time == $t2 and $type == $ty2))) {
      $found = true;
      echo "<tr><td><a href=\"view.php?q=" . urlencode($filename) .
           "&amp;fetch=1\">Assignment " . ($a2+0) . "</a></td>" .
           "<td>" . htmlspecialchars(date("c", $t2)) . "</td>" .
       #   "<td>" . getMark($filename) . "</td>" . 
           "<td>" . decodePDF($filename) . "</td></tr>\n";
   }
}

echo "</table>\n";

if (! $found) {
   echo "<p>No result found.</p>";
}
?>

<p>
<a href="../../..">Home</a> -
<a href="../..">Teaching</a> -
<a href="..">Computability</a> -
<a href=".">Assignments</a>
</p>
<hr />
<p><span style="float:right">
<a href="http://jigsaw.w3.org/css-validator/check/referer"><img 
   src="../vcss.png" 
   alt="Valid CSS" height="31" width="88" /></a>
<a href="http://validator.w3.org/check/referer"><img 
   src="../valid-xhtml11.png"
   alt="Valid XHTML 1.1" height="31" width="88" 
   /></a>
</span><a href="mailto:roberto.zunino@unitn.it">Roberto Zunino</a>, 2013
</p>


</body>
</html>
