<?php
	class cQuestionDBPage extends cTeacherPage
	{

    function GetSubdomains (&$domains,$id_domain)
    {
      $query = "SELECT tD.id FROM tDomain AS tD WHERE tD.id_parent=".$id_domain.";";
      $result = $this->db->QueryToArray($query);
      if ($result!=0)
      {
        foreach ($result as $value)
        {
          $domains[$value["id"]]=$value["id"];
          $this->GetSubdomains($domains,$value["id"]);
        }
      }
    }
    	 
    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("databáza otázok");
      $menuItem->bSelected=true;
    }
    
    function Process()
    {
      if ($this->bLoggedInAsTeacher && $this->IsTeacherToCourse())
      {
        if ($this->action==="addDomain") 
        {
          if ($this->IsCourseToDomain())
          {
            if (isset($this->postParams["subject"]) && ($this->postParams["subject"]!=="") && 
                isset($this->postParams["description"]))
            {
              $this->postParams["subject"]=mysql_real_escape_string($this->postParams["subject"]);
              $this->postParams["description"]=mysql_real_escape_string($this->postParams["description"]);
              $query = "INSERT INTO tDomain (id_parent, id_course, subject, description) ".
                       "VALUES (".$this->getParams["domainID"].", ".$this->getParams["courseID"].", '".
                       htmlspecialchars($this->postParams["subject"], ENT_QUOTES)."', '".
                       htmlspecialchars($this->postParams["description"], ENT_QUOTES)."');";
                       
              $result = $this->db->Query($query);
              $this->action="browseDomain";
            }
          }
        }
        elseif ($this->action==="editDomain")
        {
          if ($this->IsCourseToDomain())
          {
            if (isset($this->postParams["subject"]) && ($this->postParams["subject"]!=="") && 
                isset($this->postParams["description"]))
            {
              $this->postParams["subject"]=mysql_real_escape_string($this->postParams["subject"]);
              $this->postParams["description"]=mysql_real_escape_string($this->postParams["description"]);
              $query = "UPDATE tDomain SET subject='".htmlspecialchars($this->postParams["subject"], ENT_QUOTES)."', ".
                       "description='".htmlspecialchars($this->postParams["description"], ENT_QUOTES)."' ".
                       "WHERE id=".$this->getParams["domainID"].";";
                       
              $result = $this->db->Query($query);
              $query = "SELECT tD.id_parent FROM tDomain AS tD ".
                       "WHERE tD.id=".$this->getParams["domainID"].";";
                       
              $result = $this->db->QueryToRow($query);
              if ($result!=0)
              {
                $this->getParams["domainID"]=$result["id_parent"];
              }
              $this->action="browseDomain";
            }
          }
        }
        elseif ($this->action==="deleteDomain")
        {
          if ($this->IsCourseToDomain())
          {
            //zapamataj si rodicovsky okruh
            $query = "SELECT tD.id_parent FROM tDomain AS tD WHERE tD.id=".$this->getParams["domainID"].";";
            $result = $this->db->QueryToRow($query);
            if ($result != 0)
            {
              $parentDomainId=$result["id_parent"];
            }
            $domains=array($this->getParams["domainID"]=>$this->getParams["domainID"]);
            $this->GetSubdomains($domains,$this->getParams["domainID"]);
            foreach ($domains as $value)
            {
              $query = "SELECT tD.id FROM tDomain AS tD, tQuestion AS tQ, tAnswerSet AS tAS, tTestItems AS tTI ".
                       "WHERE tD.id=tQ.id_domain AND tQ.id=tAS.id_question AND tTI.id_answerSet=tAS.id AND ".
                       "tD.id=".$value.";";
  
              $result = $this->db->QueryToRow($query);
              if ($result != 0) 
              {
                //okruh nemozme odstranit, lebo nejaka jej otazka uz bola pouzita v teste
                $query = "SELECT tQ.id FROM tQuestion AS tQ WHERE tQ.id_domain=".$value.";";
                $questionsInDomain=$this->db->QueryToArray($query);
                
                //zistime, ktore otazky z okruhu tem su uz pouzite v testoch
                $query = "SELECT DISTINCT tQ.id FROM tQuestion AS tQ, tAnswerSet AS tAS, tTestItems AS tTI ".
                         "WHERE tQ.id=tAS.id_question AND tTI.id_answerSet=tAS.id AND ".
                         "tQ.id_domain=".$value.";";
    
                $questionsInTest = $this->db->QueryToArray($query);
                if ($questionsInTest !=0)
                {
                  foreach ($questionsInDomain as $questionId)
                  {
                    if (in_array($questionId, $questionsInTest))
                    {
                      //ak sa otazka pouzila v teste, otazku nemozme odstranit
                      //otazku oznac za zmazanu
                      $query = "UPDATE tQuestion SET obsolete=1 WHERE id=".$questionId["id"].";";
                      $result = $this->db->Query($query);
                      //aj sady odpovedi k otazke oznac za zmazane, kt. boli pouzite v teste
                      $query = "UPDATE tAnswerSet SET obsolete=1 WHERE id_question=".$questionId["id"]." AND ".
                               "EXISTS(SELECT tTI.id_answerSet FROM tTestItems AS tTI WHERE ".
                               "tTI.id_answerSet=id);";
                      $result = $this->db->Query($query);
                      //odpovede k sadam odpovedi, kt. neboli v teste pouzite, vymazeme
                      $query = "DELETE tA FROM tAnswer AS tA, tAnswerSet AS tAS ".
                               "WHERE tA.id_answerSet=tAS.id AND tAS.id_question=".
                               $questionId["id"]." AND ".
                               "NOT EXISTS(SELECT tTI.id_answerSet FROM tTestItems AS tTI WHERE ".
                               "tTI.id_answerSet=tAS.id);";
                      $result = $this->db->Query($query);
                      //sady odpovedi k otazke, kt. neboli pouzite v teste, vymaz
                      $query = "DELETE FROM tAnswerSet WHERE id_question=".$questionId["id"]." AND ".
                               "NOT EXISTS(SELECT tTI.id_answerSet FROM tTestItems AS tTI WHERE ".
                               "tTI.id_answerSet=id);";
                      $result = $this->db->Query($query);
                    }
                    else
                    {
                      //ak sa otazka nepouzila v teste, otazku mozme vymazat
                      //vymazeme vsetky odpovede viazane k danej otazke
                      $query = "DELETE tA FROM tAnswer AS tA, tAnswerSet AS tAS WHERE tA.id_answerSet=tAS.id AND ".
                               "tAS.id_question=".$questionId["id"].";";
                      $result = $this->db->Query($query);
                      //vymazeme vsetky sady odpovedi viazane k danej otazke
                      $query = "DELETE FROM tAnswerSet WHERE id_question=".$questionId["id"].";";
                      $result = $this->db->Query($query);
                      //vymazeme samotnu otazku
                      $query = "DELETE FROM tQuestion WHERE id=".$questionId["id"].";";
                      $result = $this->db->Query($query);
                    }
                  }
                }
                //okruh takisto oznacime za zmazany
                $query = "UPDATE tDomain SET obsolete=1, id_parent=0 WHERE id=".$value.";";
                $result = $this->db->Query($query);
              }
              else
              {
                //okruh mozme vymazat, ziadna jej otazka este nebola pouzita v teste
                //vymazeme vsetky odpovede viazane k danemu okruhu
                $query = "DELETE tA FROM tAnswer AS tA, tAnswerSet AS tAS, tQuestion AS tQ ".
                         "WHERE tA.id_answerSet=tAS.id AND tAS.id_question=tQ.id AND ".
                         "tQ.id_domain=".$value.";";
                $result = $this->db->Query($query);
                //vymazeme vsetky sady odpovedi viazane k danemu okruhu
                $query = "DELETE tAS FROM tAnswerSet AS tAS, tQuestion AS tQ ".
                         "WHERE tAS.id_question=tQ.id AND tQ.id_domain=".$value.";";
                $result = $this->db->Query($query);
                //vymazeme vsetky otazky viazane k danemu okruhu
                $query = "DELETE FROM tQuestion WHERE id_domain=".$value.";";
                $result = $this->db->Query($query);
                //vymazeme vybraty okruh tem
                $query = "DELETE FROM tDomain WHERE id=".$value.";";
                $result = $this->db->Query($query);
              }
            }
            $this->action="browseDomain";
            $this->getParams["domainID"]=$parentDomainId;
          }
        }
        elseif ($this->action==="addQuestion")
        {
          if ($this->IsCourseToDomain())
          {
            if (isset($this->postParams["question"]) && ($this->postParams["question"]!==""))
            {
              $this->postParams["question"]=mysql_real_escape_string($this->postParams["question"]);
              $query = "INSERT INTO tQuestion (id_domain, question) ".
                       "VALUES (".$this->getParams["domainID"].", '".
                       htmlspecialchars($this->postParams["question"], ENT_QUOTES)."');";

              $result = $this->db->Query($query);
              $this->action="browseDomain";
            }
          }
        }
        elseif ($this->action==="editQuestion")
        {
          if ($this->IsCourseToDomain() && $this->IsDomainToQuestion())
          {
            if (isset($this->postParams["question"]) && ($this->postParams["question"]!==""))
            {
              $this->postParams["question"]=mysql_real_escape_string($this->postParams["question"]);
              $query = "SELECT tQ.id FROM tQuestion AS tQ WHERE tQ.id=".$this->getParams["questionID"]." ".
                       "AND NOT tQ.obsolete;";
  
              $result = $this->db->QueryToRow($query);
              if ($result != 0) 
              {
                $query = "SELECT tQ.id FROM tQuestion AS tQ, tAnswerSet AS tAS, tTestItems AS tTI ".
                         "WHERE tQ.id=tAS.id_question AND tTI.id_answerSet=tAS.id AND ".
                         "tQ.id=".$this->getParams["questionID"].";";
    
                $result = $this->db->QueryToRow($query);
                if ($result != 0) 
                {
                  //otazku nemozme premenovat, lebo uz bola pouzita v teste 
                  //vytvor novu otazku
                  $query = "INSERT INTO tQuestion (id_domain,question) VALUES(".$this->getParams["domainID"].", '".
                           htmlspecialchars($this->postParams["question"])."');";
                  $result = $this->db->Query($query);
                  $newQuestionId=$this->db->InsertId();
                  
                  //skopiruj vsetky nevymazane sady odpovedi k novej otazke
                  $query = "SELECT tAS.id FROM tAnswerSet AS tAS WHERE id_question=".$this->getParams["questionID"]." ".
                           "AND NOT tAS.obsolete;";
                  $answerSets = $this->db->QueryToArray($query);
                  if ($answerSets != 0)
                  {
                    foreach ($answerSets as $answerSetId)
                    {
                      $query = "INSERT INTO tAnswerSet (id_question) VALUES(".$newQuestionId.");";
                      $result = $this->db->Query($query);
                      $newAnswerSetId=$this->db->InsertId();
                      
                      $query = "INSERT INTO tAnswer (id_answerSet, text, correct) ".
                               "SELECT ".$newAnswerSetId.", tA.text, tA.correct FROM tAnswer AS tA ".
                               "WHERE tA.id_answerSet=".$answerSetId["id"].";";
                      $result = $this->db->Query($query);
                    }
                  }
                  //staru otazku oznac za zmazanu
                  $query = "UPDATE tQuestion SET obsolete=1 WHERE id=".$this->getParams["questionID"].";";
                  $result = $this->db->Query($query);
                  //aj sady odpovedi k starej otazke oznac za zmazane, kt. boli pouzite v teste
                  $query = "UPDATE tAnswerSet SET obsolete=1 WHERE id_question=".$this->getParams["questionID"]." AND ".
                           "EXISTS(SELECT tTI.id_answerSet FROM tTestItems AS tTI WHERE ".
                           "tTI.id_answerSet=id);";
                  $result = $this->db->Query($query);
                  //odpovede k sadam odpovedi, kt. neboli v teste pouzite, vymazeme
                  $query = "DELETE tA FROM tAnswer AS tA, tAnswerSet AS tAS ".
                           "WHERE tA.id_answerSet=tAS.id AND tAS.id_question=".
                           $this->getParams["questionID"]." AND ".
                           "NOT EXISTS(SELECT tTI.id_answerSet FROM tTestItems AS tTI WHERE ".
                           "tTI.id_answerSet=tAS.id);";
                  $result = $this->db->Query($query);
                  //sady odpovedi k otazke, kt. neboli pouzite v teste, vymaz
                  $query = "DELETE FROM tAnswerSet WHERE id_question=".$this->getParams["questionID"]." AND ".
                           "NOT EXISTS(SELECT tTI.id_answerSet FROM tTestItems AS tTI WHERE ".
                           "tTI.id_answerSet=id);";
                  $result = $this->db->Query($query);
                }
                else
                {
                  //znenie otazky mozme premenovat, nebola este pouzita v teste
                  $query = "UPDATE tQuestion SET question='".htmlspecialchars($this->postParams["question"])."' WHERE id=".$this->getParams["questionID"].";";
                  $result = $this->db->Query($query);
                }
              }
              $this->action="browseDomain";
            }
          }
        }
        elseif ($this->action==="deleteQuestion")
        {
          if ($this->IsCourseToDomain() && $this->IsDomainToQuestion())
          {
            $query = "SELECT tQ.id FROM tQuestion AS tQ, tAnswerSet AS tAS, tTestItems AS tTI ".
                     "WHERE tQ.id=tAS.id_question AND tTI.id_answerSet=tAS.id AND ".
                     "tQ.id=".$this->getParams["questionID"].";";

            $result = $this->db->QueryToRow($query);
            if ($result != 0) 
            {
              //otazku nemozme odstranit, lebo uz bola pouzita v teste
              //otazku oznac za zmazanu
              $query = "UPDATE tQuestion SET obsolete=1 WHERE id=".$this->getParams["questionID"].";";
              $result = $this->db->Query($query);
              //aj sady odpovedi k otazke oznac za zmazane, kt. boli pouzite v teste
              $query = "UPDATE tAnswerSet SET obsolete=1 WHERE id_question=".$this->getParams["questionID"]." AND ".
                       "EXISTS(SELECT tTI.id_answerSet FROM tTestItems AS tTI WHERE ".
                       "tTI.id_answerSet=id);";
              $result = $this->db->Query($query);
              //odpovede k sadam odpovedi, kt. neboli v teste pouzite, vymazeme
              $query = "DELETE tA FROM tAnswer AS tA, tAnswerSet AS tAS ".
                       "WHERE tA.id_answerSet=tAS.id AND tAS.id_question=".
                       $this->getParams["questionID"]." AND ".
                       "NOT EXISTS(SELECT tTI.id_answerSet FROM tTestItems AS tTI WHERE ".
                       "tTI.id_answerSet=tAS.id);";
              $result = $this->db->Query($query);
              //sady odpovedi k otazke, kt. neboli pouzite v teste, vymaz
              $query = "DELETE FROM tAnswerSet WHERE id_question=".$this->getParams["questionID"]." AND ".
                       "NOT EXISTS(SELECT tTI.id_answerSet FROM tTestItems AS tTI WHERE ".
                       "tTI.id_answerSet=id);";
              $result = $this->db->Query($query);
            }
            else
            {
              //otazku mozme vymazat, nebola este v teste pouzita
              //vymazeme vsetky odpovede viazane k danej otazke
              $query = "DELETE tA FROM tAnswer AS tA, tAnswerSet AS tAS WHERE tA.id_answerSet=tAS.id AND ".
                       "tAS.id_question=".$this->getParams["questionID"].";";
              $result = $this->db->Query($query);
              //vymazeme vsetky sady odpovedi viazane k danej otazke
              $query = "DELETE FROM tAnswerSet WHERE id_question=".$this->getParams["questionID"].";";
              $result = $this->db->Query($query);
              //vymazeme samotnu otazku
              $query = "DELETE FROM tQuestion WHERE id=".$this->getParams["questionID"].";";
              $result = $this->db->Query($query);
            }
            $this->action="browseDomain";
          }
        }
        elseif ($this->action==="addAnswerSet")
        {
          if ($this->IsCourseToDomain() && $this->IsDomainToQuestion())
          {
            $i=0;
            $inserted=0;
            while (isset($this->postParams["answer".$i]))
            {
              if (($this->postParams["answer".$i]!==""))
              {
                $this->postParams["answer".$i]=mysql_real_escape_string($this->postParams["answer".$i]);
                if (!isset($this->postParams["correct".$i]))
                {
                  $this->postParams["correct".$i]=0;
                }
                else
                {
                  $this->postParams["correct".$i]=1;
                }
                $inserted++;
                if ($inserted==1)
                {
                  $query = "INSERT INTO tAnswerSet (id_question) VALUES(".$this->getParams["questionID"].");";
                  $result = $this->db->Query($query);
                  $answerSetId=$this->db->InsertId();
                }
                $query = "INSERT INTO tAnswer (id_answerSet, text, correct) VALUES(".$answerSetId.", '".
                         htmlspecialchars($this->postParams["answer".$i])."', ".
                         htmlspecialchars($this->postParams["correct".$i]).");";
                $result = $this->db->Query($query);
              }
              $i++;
            }
            if ($inserted!=0)
            {
              $this->action="browseAnswerSets";
            }
          }
        }
        elseif ($this->action==="deleteAnswerSet")
        {
          if ($this->IsCourseToDomain() && $this->IsDomainToQuestion() && $this->IsQuestionToAnswerSet())
          {
            $query = "SELECT tAS.id FROM tAnswerSet AS tAS, tTestItems AS tTI ".
                     "WHERE tTI.id_answerSet=tAS.id AND ".
                     "tAS.id=".$this->getParams["answerSetID"].";";

            $result = $this->db->QueryToRow($query);
            if ($result != 0) 
            {
              //sadu odpovedi nemozme odstranit, lebo uz bola pouzita v teste
              //sadu odpovedi oznac za zmazanu
              $query = "UPDATE tAnswerSet SET obsolete=1 WHERE id=".$this->getParams["answerSetID"].";";
              $result = $this->db->Query($query);
            }
            else
            {
              //sadu odpovedi mozme vymazat, nebola este v teste pouzita
              //vymazeme vsetky odpovede viazane k danej sade odpovedi
              $query = "DELETE tA FROM tAnswer AS tA, tAnswerSet AS tAS WHERE tA.id_answerSet=tAS.id AND ".
                       "tAS.id=".$this->getParams["answerSetID"].";";
              $result = $this->db->Query($query);
              //vymazeme sadu odpovedi
              $query = "DELETE FROM tAnswerSet WHERE id=".$this->getParams["answerSetID"].";";
              $result = $this->db->Query($query);
            }
            $this->action="browseAnswerSets";
          }
        }
        elseif ($this->action==="editAnswerSet")
        {
          if ($this->IsCourseToDomain() && $this->IsDomainToQuestion() && $this->IsQuestionToAnswerSet())
          {
            $query = "SELECT tAS.id FROM tAnswerSet AS tAS WHERE tAS.id=".$this->getParams["answerSetID"]." ".
                     "AND NOT tAS.obsolete;";
            
            $result = $this->db->QueryToRow($query);
            if ($result != 0) 
            {
              $i=0;
              $count=0;
              while (isset($this->postParams["answer".$i]))
              {
                if (($this->postParams["answer".$i]!==""))
                {
                  $count++;
                }
                $i++;
              }
              if ($count!=0)
              {
                $query = "SELECT tAS.id FROM tAnswerSet AS tAS, tTestItems AS tTI ".
                         "WHERE tTI.id_answerSet=tAS.id AND ".
                         "tAS.id=".$this->getParams["answerSetID"].";";
                
                $result = $this->db->QueryToRow($query);
                if ($result != 0) 
                {
                  //sadu odpovedi nemozme premenovat, lebo uz bola pouzita v teste 
                  //staru sadu odpovedi oznacime za zmazanu a vytvorime novu
                  $query = "UPDATE tAnswerSet SET obsolete=1 WHERE id=".$this->getParams["answerSetID"].";";
                  $result = $this->db->Query($query);
                  
                  $query = "INSERT INTO tAnswerSet (id_question) VALUES(".$this->getParams["questionID"].");";
                  $result = $this->db->Query($query);
                  $answerSetId=$this->db->InsertId();
                }
                else
                {
                  //povodne odpovede k aktualnej sade odpovedi zmazeme
                  $query = "DELETE FROM tAnswer WHERE id_answerSet=".$this->getParams["answerSetID"].";";
                  $result = $this->db->Query($query);
                  $answerSetId=$this->getParams["answerSetID"];
                }
                $i=0;
                while (isset($this->postParams["answer".$i]))
                {
                  if (($this->postParams["answer".$i]!==""))
                  {
                    $this->postParams["answer".$i]=mysql_real_escape_string($this->postParams["answer".$i]);
                    if (!isset($this->postParams["correct".$i]))
                    {
                      $this->postParams["correct".$i]=0;
                    }
                    else
                    {
                      $this->postParams["correct".$i]=1;
                    }
                    $query = "INSERT INTO tAnswer (id_answerSet, text, correct) VALUES(".$answerSetId.", '".
                             htmlspecialchars($this->postParams["answer".$i])."', ".
                             htmlspecialchars($this->postParams["correct".$i]).");";
                    $result = $this->db->Query($query);
                  }
                  $i++;
                }
                $this->action="browseAnswerSets";
              }
            }
          }
        }
      }
      return true;
    }
    
    function Draw()
    {
      if ($this->bLoggedInAsTeacher && ($this->IsTeacherToCourse())) 
      {
        if ($this->action==="browseDomain")
        {
          if (!isset($this->getParams["domainID"]))
          {
            $this->getParams["domainID"]=0;
          }
          if ($this->IsCourseToDomain())
          {
            $this->getParams["courseID"]=mysql_real_escape_string($this->getParams["courseID"]);
            $this->getParams["domainID"]=mysql_real_escape_string($this->getParams["domainID"]);
            $query = "SELECT tD.id, tD.subject, tD.description FROM tDomain AS tD ".
                     "WHERE tD.id_parent=".$this->getParams["domainID"]." AND ".
                     "tD.id_course=".$this->getParams["courseID"]." AND NOT tD.obsolete ".
                     " ORDER BY tD.subject ASC;";
                     
            $result = $this->db->QueryToArray($query);
            if ( $result != 0 )
            {
              foreach ($result as $key=>$value)
              {
                $result[$key]["subject"]=ShortenText($value["subject"]);
              }
              $this->smarty->assign("domainList", $result);
            }
            
            $query2 = "SELECT tQ.id, tQ.question FROM tQuestion AS tQ ".
                      "WHERE tQ.id_domain=".$this->getParams["domainID"]." AND NOT tQ.obsolete ".
                      "ORDER BY tQ.question ASC;";
                     
            $result2 = $this->db->QueryToArray($query2);
            
            if ( $result2 != 0 )
            {
              foreach ($result2 as $key=>$value)
              {
                $result2[$key]["question"]=ShortenText($value["question"]);
              }
              $this->smarty->assign("questionList", $result2);
            }
            
            if ($this->getParams["domainID"]!=0)
            {
              $query3 = "SELECT tD.id_parent FROM tDomain AS tD ".
                        "WHERE tD.id=".$this->getParams["domainID"].";";
                       
              $result3 = $this->db->QueryToRow($query3);
              if ( $result3 != 0 )
              {
                $this->smarty->assign("parentDomainID", $result3["id_parent"]);
              }
            }
            $this->smarty->assign("domain", $this->DomainName());
          }
          else
          {
            $this->bAccessDenied=true;
          }
        }
        elseif ($this->action==="addDomain")
        {
          
        }
        elseif ($this->action==="editDomain")
        {
          if ($this->IsCourseToDomain())
          {
            $query = "SELECT tD.subject, tD.description FROM tDomain AS tD ".
                     "WHERE tD.id=".$this->getParams["domainID"].";";
                     
            $result = $this->db->QueryToRow($query);
            if ( $result != 0 )
            {
              $this->smarty->assign("domain", $result);
            }
          }
          else
          {
            $this->bAccessDenied=true;
          }          
        }
        elseif ($this->action==="addQuestion")
        {
          
        }
        elseif ($this->action==="editQuestion")
        {
          if ($this->IsCourseToDomain() && $this->IsDomainToQuestion())
          {
            $query = "SELECT tQ.question FROM tQuestion AS tQ ".
                     "WHERE tQ.id=".$this->getParams["questionID"].";";
                     
            $result = $this->db->QueryToRow($query);
            if ( $result != 0 )
            {
              $result["name"]=ShortenText($result["question"]);
              $this->smarty->assign("question", $result);
              $this->smarty->assign("questionID", $this->getParams["questionID"]);
            }
          }
          else
          {
            $this->bAccessDenied=true;
          }         
        }
        elseif ($this->action==="browseAnswerSets")
        {
          if ($this->IsCourseToDomain())
          {
            if ($this->IsDomainToQuestion())
            {
              $query = "SELECT tAS.id, tA.text FROM tQuestion AS tQ, tAnswerSet AS tAS, tAnswer AS tA ".
                       "WHERE tAS.id_question=tQ.id AND tA.id_answerSet=tAS.id AND tQ.id=".
                       $this->getParams["questionID"]." AND NOT tAS.obsolete ORDER BY tA.text ASC;";
                       
              $result = $this->db->QueryToArray($query);
              if ( $result != 0 )
              {
                $answerSets=array();
                foreach ($result as $key=>$value)
                {
                  if (isset($answerSets[$value["id"]]))
                  {
                    $answerSets[$value["id"]]["text"]=$answerSets[$value["id"]]["text"].", ".$value["text"];
                  }
                  else
                  {
                    $answerSets[$value["id"]]["text"]=$value["text"];
                    $answerSets[$value["id"]]["id"]=$value["id"];
                  }
                }
                foreach ($answerSets as $key=>$value)
                {
                  $answerSets[$key]["text"]=ShortenText($answerSets[$key]["text"]);
                }
                $this->smarty->assign("answerSetList", $answerSets);
              }
              
              $query = "SELECT tQ.question FROM tQuestion AS tQ ".
                       "WHERE tQ.id=".$this->getParams["questionID"].";";
                       
              $result = $this->db->QueryToRow($query);
              if ( $result != 0 )
              {
                $this->smarty->assign("question", ShortenText($result["question"]));
              }
              $this->smarty->assign("questionID", $this->getParams["questionID"]);
            }
            else
            {
              $this->bAccessDenied=true;
            }
          }
          else
          {
            $this->bAccessDenied=true;
          }
        }
        elseif ($this->action==="addAnswerSet")
        {
          if ($this->IsCourseToDomain() && $this->IsDomainToQuestion())
          {
            $query = "SELECT tQ.question FROM tQuestion AS tQ ".
                     "WHERE tQ.id=".$this->getParams["questionID"].";";
                     
            $result = $this->db->QueryToRow($query);
            if ( $result != 0 )
            {
              $this->smarty->assign("question", $result["question"]);
            }
            $this->smarty->assign("questionID", $this->getParams["questionID"]);
          }
          else
          {
            $this->bAccessDenied=true;
          }
        }
        elseif ($this->action==="editAnswerSet")
        {
          if ($this->IsCourseToDomain() && $this->IsDomainToQuestion() && $this->IsQuestionToAnswerSet())
          {
            $query = "SELECT tQ.question FROM tQuestion AS tQ ".
                     "WHERE tQ.id=".$this->getParams["questionID"].";";
                     
            $result = $this->db->QueryToRow($query);
            if ( $result != 0 )
            {
              $this->smarty->assign("question", ShortenText($result["question"]));
            }
            
            $query = "SELECT tA.text, tA.correct FROM tAnswer AS tA ".
                     "WHERE tA.id_answerSet=".$this->getParams["answerSetID"].";";
                     
            $answerList = $this->db->QueryToArray($query);
            if ( $answerList != 0 )
            {
              foreach ($answerList as $key=>$value)
              {
                $answerList[$key]["text"]="\"".$answerList[$key]["text"]."\"";
              }
              $this->smarty->assign("answerList", $answerList);
            }
            $this->smarty->assign("questionID", $this->getParams["questionID"]);
            $this->smarty->assign("answerSetID", $this->getParams["answerSetID"]);
          }
          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("courseID",$this->getParams["courseID"]);
      $this->smarty->assign("domainID", $this->getParams["domainID"]);
      $this->smarty->assign("content","questionDB.tpl");
      parent::Draw();
    }
	}
?>
