<?php
  require 'include/head.inc';
  require 'include/db.inc';
  require 'include/common.inc'; 

  if(isset($_POST['submit']) && $_POST['stime']!="" && $_POST['score']!="") {
    $db->query("INSERT INTO exams (id,score) VALUES (" . strtotime(trim($_POST['stime'])) . ",'" . $_POST['score'] . "')");
    block("lightgreen", "added exam");
  }

  if(isset($_GET['delete'])) {
    $db->query("DELETE FROM exams WHERE id=" . $_GET['delete']);
    block("red", "deleted exam");
  }

  require 'include/exams.inc';

  block("black", "add exam");
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <table>
    <tr>  
      <th align="left">Time:</th> <th><input type="text" name="stime"></th><th></th><th>(e.g. 10.9.2012 10:30)</th>
    </tr>
    <tr>  
      <th align="left">Score:</th> <th><input type="text" name="score"></th>
      <th><input type="submit" name="submit" value="Add"></th><th>(e.g. 4,5,5)</th>
    </tr>
  </table>
</form>

<?php
  if ( $EXAMS_n == 0 ) {
    block("red", "no exams defined");
    exit;
  }

  block("black", "exam overview");

  print "<table>";
  foreach ($EXAMS as $exam) {
    print "<tr>";
    print "<td>" . $exam['id'] . "&nbsp;&nbsp;</td>";
    print "<td>" . $exam['timestr'] . "&nbsp;&nbsp;</td>";
    print "<td>" . $exam['scorestr'] . "(" . $exam['nquests'] . ")" . "&nbsp;&nbsp;</td>";
    print "<td>" . $exam['totscore'] . "&nbsp;&nbsp;</td>";
    if ($exam['nresults'] == 0) {
      print "<td><a href='exams.php?delete=" . $exam['id']  . "'>delete</a></td>";
    }
    else { print "<td>locked</td>"; }
    print "</tr>";
  }
  print "</table>";
     
?>