info343/feedback.php

<?php
   header('Content-Type: text/plain');
   
   $assignmenttype = $_REQUEST['assignmenttype'];
   $assignmentid = $_REQUEST['assignmentid'];
   
   if (isset($_REQUEST['username'])) {
      date_default_timezone_set('America/Los_Angeles');
      $timestamp = date("c", time());
      
      $ip = $_SERVER['REMOTE_ADDR'];
      $username = $_REQUEST['username'];

      $file = "submissions/$assignmenttype/$assignmentid/$username/feedback.txt";

      if (isset($_REQUEST['rating']) || isset($_REQUEST['time']) || isset($_REQUEST['tags']) || isset($_REQUEST['comments'])) {
         $rating = isset($_REQUEST['rating']) ? $_REQUEST['rating'] : '';
         $time = isset($_REQUEST['time']) ? $_REQUEST['time'] : '';
         $comments = isset($_REQUEST['comments']) ? $_REQUEST['comments'] : '';
         $tags = isset($_REQUEST['tags']) ? implode(', ', $_REQUEST['tags']) : '';

         $contents = implode("\n", array($rating, $time, $tags, $comments));
         file_put_contents($file, $contents);
         file_put_contents("submissions/master.log", "$timestamp $username@$ip: Received feedback for '$assignmentid'.\n", FILE_APPEND);
      }
      if (file_exists($file)) {
         echo file_get_contents($file);
      }
   } else {
      // summary mode!
      $total_submissions = count(explode("\n", trim(`ls -1d submissions/$assignmenttype/$assignmentid/*`)));
      $files = explode("\n", trim(`ls -1 submissions/$assignmenttype/$assignmentid/*/feedback.txt`));
      $ratings = array();
      $times = array();
      $tags = array();
      foreach (explode("\n", file_get_contents('tags.txt')) as $tag) {
         $tags[$tag] = 0;
      }
      $comments = array();
      
      foreach ($files as $file) {
         list($r, $t, $tgs, $cmnt) = explode("\n", file_get_contents($file), 4);
         if ($r) {
            array_push($ratings, $r);
         }
         if ($t) {
            array_push($times, $t);
         }
         foreach (preg_split("/, ?/", $tgs) as $tg) {
            $tags[$tg]++;
         }
         if ($cmnt) {
            array_push($comments, $cmnt);
         }
      }
      
      arsort($tags);
      
      function tagcombine($tag, $count) {
         return "$tag ($count)";
      };
      
      $ratings_report = count($ratings) ? implode(", ", $ratings) . " (total " . count($ratings) . ", avg. " . (array_sum($ratings) / count($ratings)) . ")" : 'None yet';
      $times_report = count($times) ? implode(", ", $times) . " (total " . count($times) . ", avg. " . (array_sum($times) / count($times)) . ")" : 'None yet';
      ?>
Total submissions: <?= $total_submissions ?> 
Ratings: <?= $ratings_report ?> 
Time: <?= $times_report ?> 
Tags: <?= implode(", ", array_map('tagcombine', array_keys($tags), array_values($tags))) ?> 
Comments:
<?= implode("\n----------\n", $comments) ?>


<?php
      
      // foreach ($files as $file) {
      //    echo "$file:\n";
      //    echo "[" . file_get_contents($file) . "]\n\n";
      // }
   }
?>