<?php
	class cInformationPage extends cTeacherPage
	{
    	 
    function __construct( $smarty, $db, $action, $postParams, $getParams )
    {
      parent::__construct( $smarty, $db, $action, $postParams, $getParams );
      if ($this->bLoggedInAsTeacher)
        $this->menu=new cMainTeacherMenu;
      else if ($this->bLoggedInAsProctor)
        $this->menu=new cMainProctorMenu;
      if (!empty($this->menu)) 
      {
        $this->menu->GenerateMenu("");
        $menuItem=$this->menu->GetItemByLabel("informácie");
        $menuItem->bSelected=true;
      }
      $this->smarty->assign('badLogin',false);
    }
		
    function Process()
    {
      if ( $this->action === "login" )
      {
        $this->postParams["p1"]=mysql_real_escape_string($this->postParams["p1"]);
        $this->postParams["p2"]=mysql_real_escape_string($this->postParams["p2"]);
        $query = "SELECT tU.id FROM tUser AS tU, tTeachers AS tT WHERE tU.id = tT.id AND ".
                 "tU.login='".$this->postParams["p1"]."' AND ".
                 "tU.password=MD5(CONCAT(tU.passPrefix,'".$this->postParams["p2"]."'));";
        
        $result = $this->db->QueryToRow($query);
        if ( $result != 0 )
        {
          $_SESSION["userID"] = $result["id"];
          $_SESSION["userType"] = "teacher";
          $_SESSION["examTermsDate"] = date("Y-m-d");
          $_SESSION["statisticsDateFrom"] = date("Y")-1;
          $_SESSION["statisticsDateFrom"]=$_SESSION["statisticsDateFrom"].date("-m-d");
          $_SESSION["statisticsDateTo"] = date("Y-m-d");
          $this->bAccessDenied=false;
          $this->bLoggedInAsTeacher = true;
          $this->menu=new cMainTeacherMenu;
          $this->menu->GenerateMenu("");
          $menuItem=$this->menu->GetItemByLabel("informácie");
          $menuItem->bSelected=true;
        }
        else
        {
          $query = "SELECT tU.id FROM tUser AS tU, tProctors AS tP WHERE tU.id = tP.id AND ".
                   "tU.login='".$this->postParams["p1"]."' AND ".
                   "tU.password=MD5(CONCAT(tU.passPrefix,'".$this->postParams["p2"]."'));";
          
          $result = $this->db->QueryToRow($query);
          if ( $result != 0 )
          {
            $_SESSION["userID"] = $result["id"];
            $_SESSION["userType"] = "proctor";
            $_SESSION["examTermsDate"] = date("Y-m-d");
            $this->bAccessDenied=false;
            $this->bLoggedInAsProctor = true;
            $this->menu=new cMainProctorMenu;
            $this->menu->GenerateMenu("");
            $menuItem=$this->menu->GetItemByLabel("informácie");
            $menuItem->bSelected=true;
          }
          else
          {
            $this->smarty->assign('error',"Zadali ste zlé prihlasovacie údaje");
          }
        }
      }
      else if ( $this->action === "logout" )
      {
          unset( $_SESSION["userID"] );
          unset( $_SESSION["userType"] );
          unset( $_SESSION["examTermsDate"] );
          unset( $_SESSION["statisticsDateFrom"] );
          unset( $_SESSION["statisticsDateTo"] );
          $this->bLoggedInAsTeacher = false;
          $this->bLoggedInAsProctor=false;
      }
      else if ( $this->action === "e_import" )
      {
        $query = "SELECT ISIC FROM tUser WHERE id=".$_SESSION["userID"].";";
      
        $result = $this->db->QueryToRow($query);
        if ( $result != 0 )
        {
          $exchangeMan = new cExchangeManager($this->db);
          $error = $exchangeMan->ImportExams($result["ISIC"]);
          if (!empty($error))
            return false;
          $this->smarty->assign("actionInfo","Import termínov skúšok prebehol úspešne.");
        }
      }
      return true;
    }
    
    function Draw()
    {
      if ($this->bLoggedInAsTeacher) 
      {
        $this->smarty->assign("loggedInAs","učiteľ");
        $query = "SELECT tE.date, tE.id, tE.id_course, tC.name FROM tExam AS tE, tCourse AS tC, tUser AS tU, tCourseToUser AS ".
                 "tCTU WHERE tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND tC.id=tE.id_course AND ".
                 "tU.id=".$_SESSION["userID"]." AND tE.state='scheduled' ORDER BY tE.date ASC;";
        
        $result = $this->db->QueryToArray($query);
        
        $query2= "SELECT tE.date, tE.id, tE.id_course, tC.name FROM tExam AS tE, tCourse AS tC, tUser AS tU, tCourseToUser AS ".
                 "tCTU, tProctorToTeacher AS tPTT WHERE tU.id=tCTU.id_user AND ".
                 "tCTU.id_course=tC.id AND tC.id=tE.id_course AND tPTT.id_proctor=".$_SESSION["userID"]." AND ".
                 "tPTT.id_teacher=tU.id AND tE.state='scheduled' ORDER BY tE.date ASC;";
        
        $result2 = $this->db->QueryToArray($query2);
        if ( $result != 0 )
        {
          foreach($result as $key=>$value)
          {
            $result[$key]["proctor"]=false;
          }
          if ( $result2 != 0 )
          {
            foreach($result2 as $key=>$value)
            {
              $value["proctor"]=true;
              array_push($result,$value);
            }
          }
          uasort($result,"CmpDateAscending");
          foreach($result as $key=>$value)
          {
            $result[$key]["date"]=substr($result[$key]["date"],0,16);
          }
          $this->smarty->assign("listOfScheduledExams",$result);
        }
        
        $query = "SELECT tE.date, tE.id, tE.id_course, tC.name FROM tExam AS tE, tCourse AS tC, tUser AS tU, tCourseToUser AS ".
                 "tCTU WHERE tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND tC.id=tE.id_course AND ".
                 "tU.id=".$_SESSION["userID"]." AND tE.state='progress' ORDER BY tE.date ASC;";
        
        $result = $this->db->QueryToArray($query);
        
        $query2= "SELECT tE.date, tE.id, tE.id_course, tC.name FROM tExam AS tE, tCourse AS tC, tUser AS tU, tCourseToUser AS ".
                 "tCTU, tProctorToTeacher AS tPTT WHERE tU.id=tCTU.id_user AND ".
                 "tCTU.id_course=tC.id AND tC.id=tE.id_course AND tPTT.id_proctor=".$_SESSION["userID"]." AND ".
                 "tPTT.id_teacher=tU.id AND tE.state='progress' ORDER BY tE.date ASC;";
        
        $result2 = $this->db->QueryToArray($query2);
        if ( $result != 0 )
        {
          foreach($result as $key=>$value)
          {
            $result[$key]["proctor"]=false;
          }
          if ( $result2 != 0 )
          {
            foreach($result2 as $key=>$value)
            {
              $value["proctor"]=true;
              array_push($result,$value);
            }
          }
          uasort($result,"CmpDateAscending");
          foreach($result as $key=>$value)
          {
            $result[$key]["date"]=substr($result[$key]["date"],0,16);
          }
          $this->smarty->assign("listOfProgressExams",$result);
          $this->smarty->assign("numProgressExams",count($result));
        }
      }
      elseif ($this->bLoggedInAsProctor)
      {
        $this->smarty->assign("loggedInAs","proktor");
        $query = "SELECT tE.date, tE.id, tE.id_course, tC.name FROM tExam AS tE, tCourse AS tC, tUser AS tU, tCourseToUser AS ".
                 "tCTU, tProctorToTeacher AS tPTT WHERE tU.id=tCTU.id_user AND ".
                 "tCTU.id_course=tC.id AND tC.id=tE.id_course AND tPTT.id_proctor=".$_SESSION["userID"]." AND ".
                 "tPTT.id_teacher=tU.id AND tE.state='scheduled' ORDER BY tE.date ASC;";
        
        $result = $this->db->QueryToArray($query);
        if ( $result != 0 )
        {
          foreach($result as $key=>$value)
          {
            $result[$key]["proctor"]=true;
            $result[$key]["date"]=substr($result[$key]["date"],0,16);
          }
          $this->smarty->assign("listOfScheduledExams",$result);
        }
        
        $query = "SELECT tE.date, tE.id, tE.id_course, tC.name FROM tExam AS tE, tCourse AS tC, tUser AS tU, tCourseToUser AS ".
                 "tCTU, tProctorToTeacher AS tPTT WHERE tU.id=tCTU.id_user AND ".
                 "tCTU.id_course=tC.id AND tC.id=tE.id_course AND tPTT.id_proctor=".$_SESSION["userID"]." AND ".
                 "tPTT.id_teacher=tU.id AND tE.state='progress' ORDER BY tE.date ASC;";
        
        $result = $this->db->QueryToArray($query);
        if ( $result != 0 )
        {
          foreach($result as $key=>$value)
          {
            $result[$key]["proctor"]=true;
            $result[$key]["date"]=substr($result[$key]["date"],0,16);
          }
          $this->smarty->assign("listOfProgressExams",$result);
          $this->smarty->assign("numProgressExams",count($result));
        }
      }
      
      $query = "SELECT CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name ".
               "FROM tUser AS tU WHERE tU.id=".$_SESSION["userID"].";";
      
      $result = $this->db->QueryToRow($query);
      if ( $result != 0 )
      {
        $this->smarty->assign("userName",$result["name"]);
      }
      $this->smarty->assign("content","information.tpl");
      parent::Draw();
    }
	}
?>
