<?php
	class cUserInformationPage extends cTeacherPage
	{
    	 
    function __construct( $smarty, $db, $action, $postParams, $getParams )
    {
      parent::__construct( $smarty, $db, $action, $postParams, $getParams );
      if ($this->bLoggedInAsTeacher)
      {
        //ucitel
        if (isset($this->getParams["menuSet"]))
        {
          if ($this->getParams["menuSet"]==1)
          {
            $this->menu=new cMainTeacherMenu;
          }
          else
          {
            $this->menu=new cCourseTeacherMenu;
          }
        }
        else
        {
          $this->menu=new cMainTeacherMenu;
        }
      }
      else
      {
        //proktor
        $this->menu=new cMainProctorMenu;
      }
      if (!isset($this->getParams["menuIndex"]))
      {
        $this->getParams["menuIndex"]=1;
      }
      if (isset($this->getParams["courseID"]) && ($this->getParams["courseID"]!=="")) 
      {
        $this->menu->GenerateMenu("&amp;courseID=".$this->getParams["courseID"]);
      }
      else
      {
        $this->menu->GenerateMenu("");
      }
      $menuItem=$this->menu->GetItemByIndex($this->getParams["menuIndex"]);
      $menuItem->bSelected=true;
    }
		
    function Process()
    {
      if ($this->bLoggedInAsTeacher) 
        {
        if ($this->action==="saveComment")
        {
          if ($this->IsTeacherToCourse() && $this->IsStudentToCourse() && $this->IsStudentToTest())
          {
            if (isset($this->postParams["comment"]))
            {
              $this->postParams["comment"]=mysql_real_escape_string($this->postParams["comment"]);
              $query = "UPDATE tTest SET comment='".htmlspecialchars($this->postParams["comment"])."' WHERE id=".$this->getParams["testID"].";";
              $result = $this->db->Query($query);
              
              $this->action="studentDetailTest";
            }
          }
        }
        elseif ($this->action==="savePoints")
        {
          if ($this->IsTeacherToCourse() && $this->IsStudentToCourse() && $this->IsStudentToTest())
          {
            foreach ($this->postParams as $key=>$value)
            {
              if (strlen($key)>12)
              {
                if (substr($key,0,12)==="testAnswerID")
                {
                  $id=substr($key,12,strlen($key)-12);
                  $id=mysql_real_escape_string($id);
                  $value=mysql_real_escape_string($value);
                  //kontrola, ci dana testovacia odpoved patri pod test
                  $query = "SELECT tTA.id FROM tTestAnswer AS tTA WHERE tTA.id=".$id." AND ".
                           "tTA.id_test=".$this->getParams["testID"].";";
                  $result = $this->db->QueryToRow($query);
                  if ($result!=0)
                  {
                    //zmen body
                    
                    $query = "UPDATE tTestAnswer SET points=".htmlspecialchars($value)." ".
                             "WHERE id=".$id.";";
                    $result = $this->db->Query($query);
                  }
                }
              }
            }
            //prepocitaj body z celeho testu a prirad znamku
            $query = "SELECT SUM(tTA.points) AS points FROM tTest AS tT, tTestAnswer AS tTA ".
                     "WHERE tTA.id_test=tT.id AND tT.id=".$this->getParams["testID"].";";
            $sum = $this->db->QueryToRow($query);
            
            $query = "SELECT tS.* FROM tScale AS tS, tCourse AS tC WHERE tC.id_scale=tS.id AND ".
                     "tC.id=".$this->getParams["courseID"].";";
            $scale = $this->db->QueryToRow($query);
            if (($sum!=0) && ($scale!=0))
            {
              $mark="Fx";
              if ($sum["points"]>=$scale["E"]) $mark="E";
              if ($sum["points"]>=$scale["D"]) $mark="D";
              if ($sum["points"]>=$scale["C"]) $mark="C";
              if ($sum["points"]>=$scale["B"]) $mark="B";
              if ($sum["points"]>=$scale["A"]) $mark="A";
              $query = "UPDATE tTest SET points=".$sum["points"].", mark='".$mark."' ".
                       "WHERE id=".$this->getParams["testID"].";";
              $result = $this->db->Query($query);
            }
            
            $this->action="studentDetailTest";
          }
        }
      }
      return true;
    }
    
    function Draw()
    {
      if (($this->bLoggedInAsTeacher) || ($this->bLoggedInAsProctor)) 
      {
        if (isset($this->getParams["userID"]) && ($this->getParams["userID"]!==""))
        {
          $this->getParams["userID"]=mysql_real_escape_string($this->getParams["userID"]);
          if ($this->action==="showStudent")
          {
            $query = "SELECT CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name, tU.ISIC ".
                     "FROM tUser AS tU, tStudents AS tS WHERE tU.id=".$this->getParams["userID"]." ".
                     "AND tU.id=tS.id;";
                     
            $result = $this->db->QueryToRow($query);
            if ( $result != 0 )
            {
              $this->smarty->assign("userInfo",$result);
              $this->smarty->assign("userID",$this->getParams["userID"]);
              
              //zisti vsetky zapisane predmety o danom studentovi
              $query = "SELECT tC.id, tC.name FROM tUser AS tU, tCourseToUser AS tCTU, tCourse AS tC ".
                       "WHERE tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND ".
                       "tU.id=".$this->getParams["userID"]." ORDER BY tC.name ASC;";
              $studentCourses = $this->db->QueryToArray($query);
              
              //zisti vsetky predmety, ktore uci prihlaseny ucitel
              $query = "SELECT tC.id FROM tUser AS tU, tCourseToUser AS tCTU, tCourse AS tC ".
                       "WHERE tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND ".
                       "tU.id=".$_SESSION["userID"]." ORDER BY tC.name ASC;";
              $teacherCourses = $this->db->QueryToArray($query);
              
              foreach ($studentCourses as $keyStudent=>$valueStudent)
              {
                $b=false;
                foreach ($teacherCourses as $keyTeacher=>$valueTeacher)
                {
                  if ($studentCourses[$keyStudent]["id"]==$teacherCourses[$keyTeacher]["id"])
                  {
                    $b=true;
                  }
                }
                if ($b)
                {
                  $studentCourses[$keyStudent]["link"]=true;
                }
                else
                {
                  $studentCourses[$keyStudent]["link"]=false;
                }
              }
              $this->smarty->assign("listOfCourses",$studentCourses);
            }
            else
            {
              $this->bAccessDenied=true;
            }
          }
          elseif ($this->action==="showProctor")
          {
            $query = "SELECT CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name, tU.ISIC ".
                     "FROM tUser AS tU WHERE tU.id=".$this->getParams["userID"]." ".
                     "AND NOT EXISTS(SELECT tS.id FROM tStudents AS tS WHERE tS.id=tU.id);";
                     
            $result = $this->db->QueryToRow($query);
            if ( $result != 0 )
            {
              $this->smarty->assign("userInfo",$result);
              
              //zisti predmety, kde je uzivatel ucitelom
              $query = "SELECT tC.name FROM tUser AS tU, tTeachers AS tT, tCourseToUser AS tCTU, tCourse AS tC ".
                       "WHERE tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND tT.id=tU.id AND ".
                       "tU.id=".$this->getParams["userID"]." ORDER BY tC.name ASC;";
              $teacherCourses = $this->db->QueryToArray($query);
              
              //zisti predmety, kde je uzivatel proktorom
              $query = "SELECT tC.name FROM tUser AS tU, tTeachers AS tT, tProctorToTeacher AS tPTT, ".
                       "tCourseToUser AS tCTU, tCourse AS tC ".
                       "WHERE tU.id=tT.id AND tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND ".
                       "tPTT.id_teacher=tU.id AND tPTT.id_proctor=".$this->getParams["userID"]." ".
                       "ORDER BY tC.name ASC;";
              $proctorCourses = $this->db->QueryToArray($query);
              
              
              $this->smarty->assign("teacherCourses",$teacherCourses);
              $this->smarty->assign("proctorCourses",$proctorCourses);
            }
            else
            {
              $this->bAccessDenied=true;
            }
          }
          elseif ($this->action==="studentDetailCourse")
          {
            if ((($this->bLoggedInAsTeacher && $this->IsTeacherToCourse()) || 
                 ($this->bLoggedInAsProctor && $this->IsProctorToCourse())) && ($this->IsStudentToCourse()))
            {
              $query = "SELECT CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name ".
                       "FROM tUser AS tU, tStudents AS tS WHERE tU.id=".$this->getParams["userID"]." ".
                       "AND tU.id=tS.id;";
              $result = $this->db->QueryToRow($query);
              if ( $result != 0 )
              {
                $this->smarty->assign("userInfo",$result);
              }
              
              $query = "SELECT tC.name FROM tCourse AS tC WHERE tC.id=".$this->getParams["courseID"].";";
              $result = $this->db->QueryToRow($query);
              if ( $result != 0 )
              {
                $this->smarty->assign("courseName",$result["name"]);
              }
              
              $query = "SELECT tE.date, tT.mark, tT.id FROM tTest AS tT, tExam AS tE WHERE ".
                       "tT.id_exam=tE.id AND tT.id_user=".$this->getParams["userID"]." AND ".
                       "tE.id_course=".$this->getParams["courseID"]." ORDER BY tE.date DESC;";
              $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("tests",$result);
              }
              
              $this->smarty->assign("courseID", $this->getParams["courseID"]);
            }
            else
            {
              $this->bAccessDenied=true;
            }
          }
          elseif ($this->action==="studentDetailTest")
          {
            if ((($this->bLoggedInAsTeacher && $this->IsTeacherToCourse()) || 
                 ($this->bLoggedInAsProctor && $this->IsProctorToCourse())) && 
                 ($this->IsStudentToCourse()) && ($this->IsStudentToTest()))
            {
              $query = "SELECT CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name ".
                       "FROM tUser AS tU, tStudents AS tS WHERE tU.id=".$this->getParams["userID"]." ".
                       "AND tU.id=tS.id;";
              $result = $this->db->QueryToRow($query);
              if ( $result != 0 )
              {
                $this->smarty->assign("userInfo",$result);
              }
              
              $this->smarty->assign("courseName",$this->CourseName());
              
              $query = "SELECT tE.date, tT.comment, tT.mark, tT.points FROM tTest AS tT, ".
                       "tExam AS tE WHERE tT.id=".$this->getParams["testID"]." AND tT.id_exam=tE.id;";
              $result = $this->db->QueryToRow($query);
              if ( $result != 0 )
              {
                $result["date"]=substr($result["date"],0,16);
                $this->smarty->assign("examDate",$result["date"]);
                $this->smarty->assign("testComment",$result["comment"]);
                $this->smarty->assign("testMark",$result["mark"]);
                $this->smarty->assign("testPoints",$result["points"]);
              }
              
              $query = "SELECT tQ.id AS id_question, tQ.question, tA.text, tTATA.value, tA.correct, ".
                       "tTA.comment, tTA.points, tTA.id AS id_testAnswer ".
                       "FROM tTest AS tT, tTestTemplate AS tTT, tTestItems as tTI, tAnswerSet AS tAS, ".
                       "tQuestion AS tQ, tAnswer AS tA, tTestAnswerToAnswer AS tTATA, tTestAnswer AS tTA ".
                       "WHERE tT.id=".$this->getParams["testID"]." AND tT.id_user=".$this->getParams["userID"]." AND ".
                       "tT.id_testTemplate=tTT.id AND tTI.id_testTemplate=tTT.id AND ".
                       "tTI.id_answerSet=tAS.id AND tAS.id_question=tQ.id AND ".
                       "tA.id_answerSet=tAS.id AND tTATA.id_answer=tA.id AND ".
                       "tTATA.id_testAnswer=tTA.id AND tTA.id_test=tT.id ".
                       "ORDER BY tQ.id;";
              $result = $this->db->QueryToArray($query);
              if ( $result != 0 )
              {
                $questions=array();
                foreach ($result as $key=>$value)
                {
                  if (empty($questions[$value["id_question"]]))
                  {
                    $questions[$value["id_question"]]["question"]=$value["question"];
                    $questions[$value["id_question"]]["id_testAnswer"]=$value["id_testAnswer"];
                    $questions[$value["id_question"]]["comment"]=$value["comment"];
                    $questions[$value["id_question"]]["points"]=$value["points"];
                    $questions[$value["id_question"]]["answers"]=array();
                  }
                  array_push($questions[$value["id_question"]]["answers"], array("text"=>$value["text"], "value"=>$value["value"], "correct"=>$value["correct"]));
                }
                $i=0;
                foreach ($questions as $key=>$value)
                {
                  $i++;
                  $questions[$key]["number"]=$i;
                }
                $this->smarty->assign("questions",$questions);
              }
              
              $this->smarty->assign("courseID", $this->getParams["courseID"]);
              $this->smarty->assign("testID", $this->getParams["testID"]);
            }
            else
            {
              $this->bAccessDenied=true;
            }
          }
          else
          {
            $this->bAccessDenied=true;
          }
        }
        else
        {
          $this->bAccessDenied=true;
        }
      }
      else
      {
        $this->bAccessDenied=true;
      }
      $this->smarty->assign("action", $this->action);
      $this->smarty->assign("course", $this->CourseName());
      $this->smarty->assign("userID", $this->getParams["userID"]);
      $this->smarty->assign("menuSet", $this->getParams["menuSet"]);
      $this->smarty->assign("menuIndex", $this->getParams["menuIndex"]);
      $this->smarty->assign("content","userInformation.tpl");
      parent::Draw();
    }
	}
?>
