<?php
session_start();
// Copyright Andrew Gilbert
// _______Include classes needed________

//Design class: contains HTML for header and footer and various other HTML design elements.
//e.g. Output of inputboxes depending on number of runs and measurements taken.
include("design.class.php");
$design = new design;
// Essential class: contains essential functions such as checking for input, stripping input, etc. 
include("essential.class.php");
$essential = new essential;
// Data class: contains functions for actually doing stuff with data input.
include("data.class.php");
$data = new data;
// Graph class: contains functions needed to draw the actual graph
include("graph.class.php");
$graph = new graph;

// Begin actual page output.
$design->printHeader();

  // checks if input has already been passed to program or if program has already been run.
  if (!$essential->checkTwoInput($_POST['numRuns'], $_POST['numRunsDone']))
  {
  $design->printRunsInfo();
  }
  // if input on numRuns and numMeasurements has been passed,
  // print the required number of input boxes
  if ($essential->checkTwoInput($_POST['numRuns'], $_POST['numMeasurements']))
  {
  $numRuns = $_POST['numRuns'];
  $numMeasurements = $_POST['numMeasurements'];
  
  // add input that isn't needed at the moment to a session so that it can be accessed later
  $_SESSION['cal1'] = $_POST['cal1'];
  $_SESSION['read1'] = $_POST['read1'];
  $_SESSION['random1'] = $_POST['random1'];
  $_SESSION['startLength'] = $_POST['startLength'];
  $_SESSION['lengthLeft'] = $_POST['startLength'];
  $_SESSION['increments'] = $_POST['increments'];
  $_SESSION['cal2'] = $_POST['cal2'];
  $_SESSION['read2'] = $_POST['read2'];
  $_SESSION['random2'] = $_POST['random2'];
  $_SESSION['move'] = 0;
  
  
  $design->printInputboxes($numRuns, $numMeasurements);
  }
  // if all measurements have been input, will calculate all data needed.
  if ($essential->checkTwoInput($_POST['numRunsDone'], $_POST['numMeasurementsDone']))
  {
  $numRuns = $_POST['numRunsDone'];
  $numMeasurements = $_POST['numMeasurementsDone'];

  $i = 1;
  $design->printTable();
  $totalT = 0;
  $graph->drawGrid($_SESSION['startLength'], 5, $_SESSION['increments']);
    while ($i <= $numRuns)
    {
      $r = 1;
      $values = "";
      while($r <= $numMeasurements) 
      {
      $p = $r - 1;
      // creates the key to access data sent
      $key = $i . ":" . $r;
      $dataValue = $_POST[$key];
      $values .= $dataValue;
        if ($r < $numMeasurements)
        {
        // puts a comma after value so long as it isn't the last value
        $values .= ",";
        }
      $r++;
      }      
    // get important data so that it can be output in table form
    $data->getMean($values);
    $data->getAveragePeriod();
    $data->getSquarePeriod();
    $data->getPercentError(1);
    $data->getActualError();
    // add value of T^2 to total so that we can find the centroid.
    $totalT += $data->squarePeriod;
    // plot graph points
    //$graph->getPoints($_SESSION['lengthLeft'], $data->squarePeriod);
    $graph->addPoint($_SESSION['lengthLeft'], $data->squarePeriod);
    echo("<tr><td>" . $data->averagePeriod . "s</td><td>" . $data->squarePeriod ." (s^2)</td><td>" . $data->percentError ."%</td><td>+/- " . $data->percentErrorSquare ."%</td><td>+/- " . $data->actualError ." (s^2)</td></tr>");    
    
    // calculate error bars (must go after echo as percentError and actualError are changed to accomodate
    // errors in the length of string (ruler errors)
    $yError = $data->actualError;
    $data->getActualRulerError();
    $xError = $data->actualRulerError;
    //$graph->errorBars($graph->x, $graph->y, $yError, $xError);
    $graph->errorBars($_SESSION['lengthLeft'], $data->squarePeriod, $yError, $xError);
    //echo($_SESSION['lengthLeft'] . "," .$data->squarePeriod . "," . $yError . "," . $xError . " - ");
    
    // calculate next value of length
    $_SESSION['lengthLeft'] = ($_SESSION['lengthLeft'] - $_SESSION['increments']);
    $i++;    
    }
  $data->centroidX($numRuns);
  $data->centroidY = $totalT / ($numRuns + 1);
  $graph->addCentroid($data->centroidX, $data->centroidY);
  $graph->addScale($numRuns);
  //$graph->plotLine($data->centroidX, $data->centroidY, 0);
  echo("</table>");
  echo("<A href=\"plot.php?x=" . $data->centroidX . "&y=" . $data->centroidY . "&move=0\">Plot line on graph</a>");
   
  }
   
$design->printFooter();

?>