<?php
	class cExamTermsPage extends cTeacherPage
	{
    	 
    function __construct( $smarty, $db, $action, $postParams, $getParams )
    {
      parent::__construct( $smarty, $db, $action, $postParams, $getParams );
      $this->menu=new cCourseTeacherMenu;
      if (isset($this->getParams["courseID"]) && ($this->getParams["courseID"]!=="")) 
      {
        $this->menu->GenerateMenu("&amp;courseID=".$this->getParams["courseID"]);
      }
      else
      {
        $this->menu->GenerateMenu("");
      }
      $menuItem=$this->menu->GetItemByLabel("termíny skúšok");
      $menuItem->bSelected=true;
    }
		
    function Process()
    {
      if ($this->bLoggedInAsTeacher && $this->IsTeacherToCourse())
      {
        if ($this->IsCourseToExam()) 
        {
          $this->getParams["examID"]=mysql_real_escape_string($this->getParams["examID"]);
          $this->getParams["courseID"]=mysql_real_escape_string($this->getParams["courseID"]);
          if (isset($this->getParams["studentID"]) && ($this->getParams["studentID"]!==""))
          {
            $this->getParams["studentID"]=mysql_real_escape_string($this->getParams["studentID"]);
            if ($this->action==="addStudentToList")
            {
              $query = "SELECT tU.id FROM tUser AS tU, tCourseToUser AS tCTU, tCourse as tC, tStudents AS tS, tExam AS tE ".
                       "WHERE tU.id=".$this->getParams["studentID"]." AND tS.id=tU.id AND tU.id=tCTU.id_user ".
                       "AND tCTU.id_course=tC.id AND tC.id=".$this->getParams["courseID"]." ".
                       "AND tE.id_course=tC.id AND tE.id=".$this->getParams["examID"]." AND tE.state='scheduled';";
              
              $result = $this->db->QueryToRow($query);
              if ( $result != 0 )
              {
                $query = "INSERT INTO tExamToUser (id_user, id_exam) VALUES (".
                         $this->getParams["studentID"].", ".$this->getParams["examID"].");";
                
                $result = $this->db->Query($query);
              }
              $this->action="listOfStudents";
            }
            elseif ($this->action==="deleteStudentFromList")
            {
              $query = "SELECT tU.id FROM tUser AS tU, tCourseToUser AS tCTU, tCourse as tC, tStudents AS tS, tExam AS tE ".
                       "WHERE tU.id=".$this->getParams["studentID"]." AND tS.id=tU.id AND tU.id=tCTU.id_user ".
                       "AND tCTU.id_course=tC.id AND tC.id=".$this->getParams["courseID"]." AND tE.id_course=tC.id AND ".
                       "tE.id=".$this->getParams["examID"]." AND tE.state='scheduled';";
              
              $result = $this->db->QueryToRow($query);
              if ( $result != 0 )
              {
                $query = "DELETE FROM tExamToUser WHERE id_user=".$this->getParams["studentID"]." AND ".
                         "id_exam=".$this->getParams["examID"].";";
                $result = $this->db->Query($query);
              }
              $this->action="listOfStudents";
            }
          }
        }
        else
        {
          if ($this->action==="setExamTermsDate")
          {
            if (isset($this->postParams["examTermsDate"]) && ($this->postParams["examTermsDate"]!==""))
            {
              $this->postParams["examTermsDate"]=mysql_real_escape_string($this->postParams["examTermsDate"]);
              $this->postParams["examTermsDate"]=trim($this->postParams["examTermsDate"]);
              if (IsDateType($this->postParams["examTermsDate"]))
              {
                $_SESSION["examTermsDate"]=$this->postParams["examTermsDate"];
              }
            }
          }
        }
      }
      return true;
    }
    
    function Draw()
    {
      if ($this->bLoggedInAsTeacher && ($this->IsTeacherToCourse())) 
      {
        if ($this->action==="listOfStudents")
        {
          if ($this->IsCourseToExam())
          {
            $this->getParams["examID"]=mysql_real_escape_string($this->getParams["examID"]);
            $query = "SELECT tE.date, tE.state FROM tExam AS tE WHERE tE.id=".$this->getParams["examID"].";";
            $result = $this->db->QueryToRow($query);
            if ( $result != 0 )
            {
              $this->smarty->assign("date",substr($result["date"],0,16));
              $this->smarty->assign("state",$result["state"]);
            }            
            
            $query2 = "SELECT CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name, tU.id ".
                      "FROM tUser AS tU, tStudents AS tS, tExamToUser AS tETU, tExam AS tE ".
                      "WHERE tU.id=tS.id AND tU.id=tETU.id_user AND tETU.id_exam=tE.id AND tE.id=".
                      $this->getParams["examID"]." ORDER BY tU.surname ASC, tU.name ASC;";
            $result2 = $this->db->QueryToArray($query2);
            $this->smarty->assign("numRows",count($result2));
            
            $query3 = "SELECT tT.id, tT.id_user ".
                      "FROM tUser AS tU, tStudents AS tS, tExamToUser AS tETU, tExam AS tE, tTest AS tT ".
                      "WHERE tU.id=tS.id AND tU.id=tETU.id_user AND tETU.id_exam=tE.id AND tE.id=".
                      $this->getParams["examID"]." AND tT.id_exam=tE.id AND tT.id_user=tU.id;";
            $result3 = $this->db->QueryToArray($query3);
            if ( $result2 != 0 )
            {
              if ($result3!=0)
              {
                foreach ($result2 as $key2=>$value2)
                {
                  $result2[$key2]["testID"]=0;
                  foreach ($result3 as $key3=>$value3)
                  {
                    if ($value3["id_user"]==$value2["id"])
                    {
                      $result2[$key2]["testID"]=$value3["id"];
                    }
                  }
                }
              }
              $this->smarty->assign("listOfStudents",$result2);
            }
            $this->smarty->assign("examID",$this->getParams["examID"]);
          }
          else
          {
            $this->bAccessDenied=true;
          }
        }
        elseif ($this->action==="browseNewStudentsList")
        {
          if ($this->IsCourseToExam())
          {
            $this->getParams["examID"]=mysql_real_escape_string($this->getParams["examID"]);
            $this->getParams["courseID"]=mysql_real_escape_string($this->getParams["courseID"]);
            $query = "SELECT tE.date, tE.state FROM tExam AS tE WHERE tE.id=".$this->getParams["examID"].";";
            $result = $this->db->QueryToRow($query);
            if ( $result != 0 )
            {
              $this->smarty->assign("date",substr($result["date"],0,16));
              $this->smarty->assign("state",$result["state"]);
            }
            
            $query2 = "SELECT CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name, tU.id ".
                      "FROM tUser AS tU, tStudents AS tS, tCourseToUser AS tCTU, tCourse AS tC ".
                      "WHERE tU.id=tS.id AND tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND tC.id=".
                      $this->getParams["courseID"]." AND NOT EXISTS(SELECT * FROM tExamToUser AS tETU ".
                      "WHERE tETU.id_user=tU.id AND tETU.id_exam=".$this->getParams["examID"].") ".
                      "ORDER BY tU.surname ASC, tU.name ASC;";
            
            $result2 = $this->db->QueryToArray($query2);
            $this->smarty->assign("numRows",count($result2));
            if ( $result2 != 0 )
            {
              $this->smarty->assign("listOfStudents",$result2);
            }
            $this->smarty->assign("examID",$this->getParams["examID"]);
          }
          else
          {
            $this->bAccessDenied=true;
          }
        }
        else
        {
          $this->getParams["examID"]=mysql_real_escape_string($this->getParams["courseID"]);
          $query = "SELECT tC.name, tE.date, tE.state, tE.id FROM tExam AS tE, tCourse AS tC ".
                   "WHERE tE.id_course=tC.id AND tC.id=".$this->getParams["courseID"]." ".
                   "AND tE.date>='".$_SESSION["examTermsDate"]." 00:00:00' ORDER BY tE.date ASC;";
                   
          $result = $this->db->QueryToArray($query);
          if ( $result != 0 )
          {
            foreach($result as $key=>$value)
            {
              $result[$key]["date"]=substr($result[$key]["date"],0,16);
            }
            $this->smarty->assign("listOfExams",$result);
          }
          $this->smarty->assign("examTermsDate",$_SESSION["examTermsDate"]);
        }
      }
      else
      {
        $this->bAccessDenied=true;
      }
      $this->smarty->assign("action", $this->action);
      $this->smarty->assign("course", $this->CourseName());
      $this->smarty->assign("courseID",$this->getParams["courseID"]);
      $this->smarty->assign("content","examTerms.tpl");
      parent::Draw();
    }
	}
?>
