<?php
	class cTestingPage extends cTeacherPage
	{
    	 
    function __construct( $smarty, $db, $action, $postParams, $getParams )
    {
      parent::__construct( $smarty, $db, $action, $postParams, $getParams );
      $this->menu=new cTestingMenu;
      $this->menu->GenerateMenu("");
      $menuItem=$this->menu->GetItemByLabel("predmety");
      $menuItem->bSelected=true;
    }
		
    function Process()
    {
      if (($this->bLoggedInAsTeacher && $this->IsTeacherToCourse()) ||
          ($this->bLoggedInAsProctor && $this->IsProctorToCourse()))
      {
        if ($this->IsCourseToExam())
        {
          if ($this->action==="startTesting")
          {
          	// zmen status skuske na beziacu
            $query = "UPDATE tExam SET state='progress' WHERE id=".$this->getParams["examID"].";";
            $result = $this->db->Query($query);
            $this->action="";
          }
          elseif ($this->action==="addTime")
          {
            // oznacenym studentom pridat cas
						if (is_array($this->postParams['students']))
						{
	            foreach ($this->postParams['students'] as $student)
	            {	
	            	$student = mysql_real_escape_string($student);
	            	$time = mysql_real_escape_string($this->postParams['time']);
	            	$time = (is_numeric($time)?$time:0);
								$query = "UPDATE tTest SET totalTime=totalTime+$time WHERE id_exam='".$this->getParams["examID"]."' AND id_user='$student';";
		            $result = $this->db->Query($query);
	            }
	          }
          }
          elseif ($this->action==="closeTesting")
          {
            $query = "UPDATE tExam SET state='finished' WHERE id=".$this->getParams["examID"].";";
            $result = $this->db->Query($query);
          }
          elseif ($this->action==="openTesting")
          {
            $query = "UPDATE tExam SET state='progress' WHERE id=".$this->getParams["examID"].";";
            $result = $this->db->Query($query);
          }
          elseif ($this->action==="stopTesting")
          {
            // vsetkym studentom prihlasenym na skusku vynuluj pocitadlo

            // pripav zoznam studentov ktory pisu skusku
            $query = "SELECT tT.id
                      FROM tUser AS tU
											INNER JOIN tStudents AS tS ON tU.id=tS.id
											INNER JOIN tExamToUser AS tETU ON tU.id=tETU.id_user
											INNER JOIN tExam AS tE ON tETU.id_exam=tE.id
											INNER JOIN tTest AS tT ON (tT.id_exam=tE.id AND tT.id_user=tU.id)
                      WHERE tE.id='".$this->getParams["examID"]."'
											ORDER BY tU.surname ASC, tU.name ASC;";
		        $result = $this->db->QueryToArray($query);
		        if ($result !== 0)
		        {
							if (is_array($result))
							{
								foreach ($result as $test)
								{
									// nastav totalTime na aktualny timestamp a tym sa ukonci testovanie
									$query2 = "UPDATE tTest SET totalTime='".time()."' WHERE id=".$test['id'].";";
			            $result2 = $this->db->Query($query2);
		            }
	            }
						}
            $query = "UPDATE tExam SET state='finished' WHERE id=".$this->getParams["examID"].";";
            $result = $this->db->Query($query);
          }
        }
      }
      return true;
    }
    
    function Draw()
    {
      if (($this->bLoggedInAsTeacher && $this->IsTeacherToCourse()) ||
          ($this->bLoggedInAsProctor && $this->IsProctorToCourse()))
      {
        if ($this->IsCourseToExam())
        {
          if ($this->action!=="stopTesting")
          {
            $this->smarty->assign("testingStopped",0);
            // zisti stav
						$query = "SELECT state FROM tExam WHERE id='".$this->getParams["examID"]."';";
            $data = $this->db->QueryToRow($query);
		        $closed = ($data['state']==='finished');
						$this->smarty->assign("closed",$closed);
            
            // pripav zoznam studentov ktory pisu skusku
            $query = "SELECT CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name, tU.id, tT.totalTime - ".time()." AS totalTime
                      FROM tUser AS tU
											INNER JOIN tStudents AS tS ON tU.id=tS.id
											INNER JOIN tExamToUser AS tETU ON tU.id=tETU.id_user
											INNER JOIN tExam AS tE ON tETU.id_exam=tE.id
											INNER JOIN tTest AS tT ON (tT.id_exam=tE.id AND tT.id_user=tU.id)
                      WHERE tE.id='".$this->getParams["examID"]."'
											ORDER BY tU.surname ASC, tU.name ASC;";
		        $result = $this->db->QueryToArray($query);
		        if ($result !== 0)
		        {
		        	$this->smarty->assign("students",$result);
		        }
            
          }
          else
          {
            $this->smarty->assign("testingStopped",1);
          }
        }
	      else
	      {
	        $this->bAccessDenied=true;
	      }
      }
      else
      {
        $this->bAccessDenied=true;
      }
      $this->smarty->assign("examID", $this->getParams["examID"]);
      $this->smarty->assign("courseID", $this->getParams["courseID"]);
      $this->smarty->assign("course", $this->CourseName());
      $this->smarty->assign("content","testing.tpl");
      parent::Draw();
    }
	}
?>
