<?php
	class cDemoTestPage 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("testy");
      $menuItem->bExpanded=true;
      $menuItem=$menuItem->GetItemByLabel("demo test");
      $menuItem->bSelected=true;
    }
		
    function Process()
    {
      if ($this->bLoggedInAsTeacher && $this->IsTeacherToCourse())
      {
        if ($this->action==="delete") 
        {
          $query = "SELECT tC.id_demoTest FROM tCourse AS tC ".
                   "WHERE tC.id=".$this->getParams["courseID"].";";
          $demoTestID = $this->db->QueryToRow($query);
          if ( $demoTestID != 0 )
          {
            if ($demoTestID["id_demoTest"]!=0)
            {
              $query = "DELETE FROM tTestItems ".
                       "WHERE id_testTemplate=".$demoTestID["id_demoTest"].";";
              $result = $this->db->Query($query);
              
              $query = "DELETE FROM tTestTemplate ".
                       "WHERE id=".$demoTestID["id_demoTest"].";";
              $result = $this->db->Query($query);
              
              $query = "UPDATE tCourse SET id_demoTest=0 ".
                       "WHERE id=".$this->getParams["courseID"].";";
              $result = $this->db->Query($query);
            }
          }
          $this->action="";
        }
      }
      return true;
    }
    
    function Draw()
    {
      if ($this->bLoggedInAsTeacher && ($this->IsTeacherToCourse())) 
      {
        if ($this->action==="")
        {
          $query = "SELECT tQ.id AS id_question, tQ.question, tA.text ".
                   "FROM tCourse AS tC, tTestTemplate AS tTT, tTestItems as tTI, tAnswerSet AS tAS, ".
                   "tQuestion AS tQ, tAnswer AS tA ".
                   "WHERE tTT.id=tC.id_demoTest 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 tC.id=".$this->getParams["courseID"]." ".
                   "ORDER BY tQ.id;";
          $result = $this->db->QueryToArray($query);
          if ( count($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"]]["answers"]=array();
              }
              array_push($questions[$value["id_question"]]["answers"], array("text"=>$value["text"]));
            }
            $i=0;
            foreach ($questions as $key=>$value)
            {
              $i++;
              $questions[$key]["number"]=$i;
            }
            $this->smarty->assign("questions",$questions);
          }
          else
          {
            $this->smarty->assign("noDemoTest",true);
          }
        }
        else
        {
          $this->bAccessDenied=true;
        }
      }
      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","demoTest.tpl");
      parent::Draw();
    }
	}
?>
