<?php
	class cProctorsPage extends cTeacherPage
	{
    	 
    function __construct( $smarty, $db, $action, $postParams, $getParams )
    {
      parent::__construct( $smarty, $db, $action, $postParams, $getParams );
      $this->menu=new cMainTeacherMenu;
      $this->menu->GenerateMenu("");
      $menuItem=$this->menu->GetItemByLabel("proktori");
      $menuItem->bSelected=true;
    }
		
    function Process()
    {
      if ($this->bLoggedInAsTeacher) 
      {
        if ($this->action==="addProctor")
        {
          if (isset($this->getParams["id"]) && ($this->getParams["id"]!="")) 
          {
            $this->getParams["id"]=mysql_real_escape_string($this->getParams["id"]);
            $query="INSERT INTO tProctorToTeacher (id_proctor, id_teacher) VALUES (".
            $this->getParams["id"].", ".$_SESSION["userID"].");";
            $result = $this->db->Query($query);
            $this->action="";
          }
        }
        elseif ($this->action==="deleteProctor")
        {
          if (isset($this->getParams["id"]) && ($this->getParams["id"]!="")) 
          {
            $this->getParams["id"]=mysql_real_escape_string($this->getParams["id"]);
            $query="DELETE FROM tProctorToTeacher WHERE id_proctor=".
            $this->getParams["id"]." AND id_teacher=".$_SESSION["userID"].";";
            $result = $this->db->Query($query);
            $this->action="";
          }
        }
      }
      return true;
    }
    
    function Draw()
    {
      if ($this->bLoggedInAsTeacher) 
      {
        if ($this->action==="browseList") 
        {
          $query = "SELECT tU.id, CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name FROM tUser AS tU, ".
                   "tTeachers AS tT WHERE tU.id=tT.id AND ".
                   "tU.id<>".$_SESSION["userID"]." AND NOT EXISTS(SELECT tPTT.id_proctor FROM tProctorToTeacher AS tPTT ".
                   "WHERE tPTT.id_teacher=".$_SESSION["userID"]." AND tPTT.id_proctor=tU.id) ".
                   "UNION ".
                   "SELECT tU.id, CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name FROM tUser AS tU, ".
                   "tProctors AS tP WHERE tU.id=tP.id AND ".
                   "tU.id<>".$_SESSION["userID"]." AND NOT EXISTS(SELECT tPTT.id_proctor FROM tProctorToTeacher AS tPTT ".
                   "WHERE tPTT.id_teacher=".$_SESSION["userID"]." AND tPTT.id_proctor=tU.id);";
          $result = $this->db->QueryToArray($query);
          if ( $result != 0 )
          {
            $this->smarty->assign("listOfProctors",$result);
          }
        }
        else
        {
          $query = "SELECT CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name, tU.id ".
                   "FROM tUser AS tU, tProctorToTeacher AS tPTT WHERE ".
                   "tU.id=tPTT.id_proctor AND tPTT.id_teacher=".$_SESSION["userID"]." ".
                   "ORDER BY tU.name ASC;";
          
          $result = $this->db->QueryToArray($query);
          if ( $result != 0 )
          {
            $this->smarty->assign("listOfProctors",$result);
          }
        }
      }
      else 
      {
        $this->bAccessDenied=true;
      }
      $this->smarty->assign("content","proctors.tpl");
      $this->smarty->assign("action",$this->action);
      parent::Draw();
    }
	}
?>
