<?php
  abstract class cTeacherPage extends cPage
  {
    protected $bLoggedInAsTeacher;
    protected $bLoggedInAsProctor;
    protected $bAccessDenied;
    public $bAllowDraw;
    
    function __construct( $smarty, $db, $action, $postParams, $getParams )
    {
      parent::__construct( $smarty, $db, $action, $postParams, $getParams );
    	$this->bLoggedInAsTeacher = $this->LoggedAs("teacher");
    	$this->bLoggedInAsProctor = $this->LoggedAs("proctor");
    	$this->bAccessDenied=!($this->bLoggedInAsTeacher || $this->bLoggedInAsProctor);
    	$this->bAllowDraw=true;
    }
    
    function IsTeacherToCourse()
    {
      //funkcia vrati true, ak ucitel je ucitelom v aktualnom predmete, inak vrati false
      if (isset($this->getParams["courseID"]) && ($this->getParams["courseID"]!==""))
      {
        $this->getParams["courseID"]=mysql_real_escape_string($this->getParams["courseID"]);
        $query = "SELECT tU.id FROM tUser AS tU, tCourseToUser AS tCTU, tCourse AS tC, tTeachers AS tT ".
                 "WHERE tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND tU.id=tT.id AND ".
                 "tU.id=".$_SESSION["userID"]." AND tC.id=".$this->getParams["courseID"].";";
                 
        $result = $this->db->QueryToRow($query);
        if ( $result != 0 )
        {
          return true;
        }
        else
        {
          return false;
        }
      }
      else
      {
        return false;
      }
    }
    
    function IsProctorToCourse()
    {
      //funkcia vrati true, ak proktor je proktorom v aktualnom predmete, inak vrati false
      if (isset($this->getParams["courseID"]) && ($this->getParams["courseID"]!==""))
      {
        $this->getParams["courseID"]=mysql_real_escape_string($this->getParams["courseID"]);
        $query = "SELECT tU.id FROM tUser AS tU, tTeachers AS tT, tProctorToTeacher AS tPTT, ".
                 "tCourse AS tC, tCourseToUser AS tCTU ".
                 "WHERE tU.id=tT.id AND tCTU.id_user=tU.id AND tCTU.id_course=tC.id AND ".
                 "tU.id=tPTT.id_teacher AND tPTT.id_proctor=".$_SESSION["userID"]." AND ".
                 "tC.id=".$this->getParams["courseID"].";";
                 
        $result = $this->db->QueryToRow($query);
        if ( $result != 0 )
        {
          return true;
        }
        else
        {
          return false;
        }
      }
      else
      {
        return false;
      }
    }
    
    function IsStudentToCourse()
    {
      //funkcia vrati true, ak student je studentom v aktualnom predmete, inak vrati false
      if (isset($this->getParams["courseID"]) && ($this->getParams["courseID"]!=="") && isset($this->getParams["userID"]) && ($this->getParams["userID"]!==""))
      {
        $this->getParams["courseID"]=mysql_real_escape_string($this->getParams["courseID"]);
        $this->getParams["userID"]=mysql_real_escape_string($this->getParams["userID"]);
        $query = "SELECT tU.id FROM tUser AS tU, tCourseToUser AS tCTU, tCourse AS tC, tStudents AS tS ".
                 "WHERE tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND tU.id=tS.id AND ".
                 "tU.id=".$this->getParams["userID"]." AND tC.id=".$this->getParams["courseID"].";";
                 
        $result = $this->db->QueryToRow($query);
        if ( $result != 0 )
        {
          return true;
        }
        else
        {
          return false;
        }
      }
      else
      {
        return false;
      }
    }
    
    function IsCourseToExam()
    {
      //funkcia vrati true, ak zvoleny termin skusky patri k zvolenemu predmetu, inak vrati false
      if (isset($this->getParams["courseID"]) && ($this->getParams["courseID"]!==""))
      {
        $this->getParams["courseID"]=mysql_real_escape_string($this->getParams["courseID"]);
        if (isset($this->getParams["examID"]) && ($this->getParams["examID"]!==""))
        {
          $this->getParams["examID"]=mysql_real_escape_string($this->getParams["examID"]);
          $query = "SELECT tE.id FROM tCourse AS tC, tExam AS tE ".
                   "WHERE tE.id_course=tC.id AND tE.id=".$this->getParams["examID"]." ".
                   "AND tC.id=".$this->getParams["courseID"].";";
                   
          $result = $this->db->QueryToRow($query);
          if ( $result != 0 )
          {
            return true;
          }
          else
          {
            return false;
          }
        }
        else
        {
          return false;
        }
      }
      else
      {
        return false;
      }
    }
    
    function IsCourseToDomain()
    {
      //funkcia vrati true, ak zvoleny okruh tem patri k zvolenemu predmetu, inak vrati false
      if (isset($this->getParams["courseID"]) && ($this->getParams["courseID"]!==""))
      {
        $this->getParams["courseID"]=mysql_real_escape_string($this->getParams["courseID"]);
        if (isset($this->getParams["domainID"]) && ($this->getParams["domainID"]!==""))
        {
          if ($this->getParams["domainID"]==0)
          {
            return true;
          }
          else
          {
            $this->getParams["domainID"]=mysql_real_escape_string($this->getParams["domainID"]);
            $query = "SELECT tD.id FROM tCourse AS tC, tDomain AS tD ".
                     "WHERE tD.id_course=tC.id AND tD.id=".$this->getParams["domainID"]." ".
                     "AND tC.id=".$this->getParams["courseID"].";";
                     
            $result = $this->db->QueryToRow($query);
            if ( $result != 0 )
            {
              return true;
            }
            else
            {
              return false;
            }
          }
        }
        else
        {
          return false;
        }
      }
      else
      {
        return false;
      }
    }
    
    function IsDomainToQuestion()
    {
      //funkcia vrati true, ak zvolena otazka patri k zvolenemu okruhu tem, inak vrati false
      if (isset($this->getParams["domainID"]) && ($this->getParams["domainID"]!==""))
      {
        $this->getParams["domainID"]=mysql_real_escape_string($this->getParams["domainID"]);
        if (isset($this->getParams["questionID"]) && ($this->getParams["questionID"]!==""))
        {
          $this->getParams["questionID"]=mysql_real_escape_string($this->getParams["questionID"]);
          $query = "SELECT tQ.id FROM tDomain AS tD, tQuestion AS tQ ".
                   "WHERE tQ.id_domain=tD.id AND tD.id=".$this->getParams["domainID"]." ".
                   "AND tQ.id=".$this->getParams["questionID"].";";
                   
          $result = $this->db->QueryToRow($query);
          if ( $result != 0 )
          {
            return true;
          }
          else
          {
            return false;
          }
        }
        else
        {
          return false;
        }
      }
      else
      {
        return false;
      }
    }
    
    function IsQuestionToAnswerSet()
    {
      //funkcia vrati true, ak zvolena sada odpovedi patri k k zvolenej otazke, inak vrati false
      if (isset($this->getParams["questionID"]) && ($this->getParams["questionID"]!==""))
      {
        $this->getParams["questionID"]=mysql_real_escape_string($this->getParams["questionID"]);
        if (isset($this->getParams["answerSetID"]) && ($this->getParams["answerSetID"]!==""))
        {
          $this->getParams["answerSetID"]=mysql_real_escape_string($this->getParams["answerSetID"]);
          $query = "SELECT tAS.id FROM tAnswerSet AS tAS, tQuestion AS tQ ".
                   "WHERE tAS.id_question=tQ.id AND tQ.id=".$this->getParams["questionID"]." ".
                   "AND tAS.id=".$this->getParams["answerSetID"].";";
                   
          $result = $this->db->QueryToRow($query);
          if ( $result != 0 )
          {
            return true;
          }
          else
          {
            return false;
          }
        }
        else
        {
          return false;
        }
      }
      else
      {
        return false;
      }
    }
    
    function IsStudentToTest()
    {
      //funkcia vrati true, ak studentovi prislucha test
      if (isset($this->getParams["userID"]) && ($this->getParams["userID"]!=="") && isset($this->getParams["testID"]) && ($this->getParams["testID"]!==""))
      {
        $this->getParams["userID"]=mysql_real_escape_string($this->getParams["userID"]);
        $this->getParams["testID"]=mysql_real_escape_string($this->getParams["testID"]);
        $query = "SELECT tT.id FROM tTest AS tT ".
                 "WHERE tT.id=".$this->getParams["testID"]." AND tT.id_user=".$this->getParams["userID"].";";
                 
        $result = $this->db->QueryToRow($query);
        if ( $result != 0 )
        {
          return true;
        }
        else
        {
          return false;
        }
      }
      else
      {
        return false;
      }
    }
    
    function CourseName()
    {
      //funkcia vrati nazov aktualneho predmetu
      if (isset($this->getParams["courseID"]) && ($this->getParams["courseID"]!=="")) 
      {
        $this->getParams["courseID"]=mysql_real_escape_string($this->getParams["courseID"]);
        $query = "SELECT tC.name FROM tCourse AS tC WHERE tC.id=".$this->getParams["courseID"].";";
        
        $result = $this->db->QueryToRow($query);
        if ( $result != 0 )
        {
          return $result["name"];
        }
        else
        {
          return "";
        }
      }
    }
    
    function DomainName()
    {
      //funkcia vrati nazov aktualneho okruhu tem
      if (isset($this->getParams["domainID"]) && ($this->getParams["domainID"]!=="")) 
      {
        $this->getParams["domainID"]=mysql_real_escape_string($this->getParams["domainID"]);
        $query = "SELECT tD.subject FROM tDomain AS tD WHERE tD.id=".$this->getParams["domainID"].";";
        
        $result = $this->db->QueryToRow($query);
        if ( $result != 0 )
        {
          return $result["subject"];
        }
        else
        {
          return "";
        }
      }
    }
    
    //vrati true, ak user nebol prihlaseny (zobrazi prihlasovaciu stranku)
    //ak vrati false, vykreslovanie sa necha na potomka
    protected function Draw()
    {
      //potomkovia mohli urobit zmeny v menu, prirad ho az teraz
      $this->smarty->assign( "menu", $this->menu );

      if ((!$this->bLoggedInAsTeacher) && (!$this->bLoggedInAsProctor))
        $this->smarty->display("login.tpl");
      else
        if ($this->bAccessDenied) 
          $this->smarty->display("accessDenied.tpl");
        else
          $this->smarty->display("main.tpl");        
    }
    
    public function Redirect()
    {
      return isset($this->getParams["redirect"]) && ($this->getParams["redirect"]==1);
    }
  }
?>
