<?php
	class cSearchPage 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("vyhľadávanie");
      $menuItem->bSelected=true;
    }
		
    function Process()
    {
      if ($this->bLoggedInAsTeacher && $this->IsTeacherToCourse())
      {
        if ($this->action==="") 
        {
          
        }
      }
      return true;
    }
    
    function Draw()
    {
      if ($this->bLoggedInAsTeacher && ($this->IsTeacherToCourse())) 
      {
        if ($this->action==="search")
        {
          if (isset($this->getParams["searchText"]) && isset($this->getParams["searchWhere"]) && isset($this->getParams["sortType"]))
          {
            $this->smarty->assign("searchText", $this->getParams["searchText"]);
            $this->smarty->assign("searchWhere", $this->getParams["searchWhere"]);
            $this->smarty->assign("sortType", $this->getParams["sortType"]);
            
            $this->getParams["searchText"]=strtolower($this->getParams["searchText"]);
            $this->getParams["searchText"]=mysql_real_escape_string($this->getParams["searchText"]);
            if ($this->getParams["sortType"]==="descending")
            {
              $sort=" DESC";
            }
            else
            {
              $sort=" ASC";
            }
            
            if ($this->getParams["searchWhere"]==="answerSets")
            {
              $query = "SELECT tA.id_answerSet AS id, tAS.obsolete, tA.text, tAS.id_question, tQ.id_domain FROM ".
                       "tAnswer AS tA, tAnswerSet AS tAS, tQuestion AS tQ, tDomain AS tD WHERE ".
                       "tA.id_answerSet=tAS.id AND tAS.id_question=tQ.id AND tQ.id_domain=tD.id AND ".
                       "tD.id_course=".$this->getParams["courseID"]." AND NOT tAS.obsolete AND EXISTS(SELECT tA2.id FROM tAnswer AS tA2 ".
                       "WHERE tA2.id_answerSet=tA.id_answerSet AND LCASE(tA2.text) LIKE '%".$this->getParams["searchText"]."%') ".
                       "ORDER BY tA.text".$sort.";";
              $result = $this->db->QueryToArray($query);
              $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"];
                  $answerSets[$value["id"]]["obsolete"]=$value["obsolete"];
                  $answerSets[$value["id"]]["id_question"]=$value["id_question"];
                  $answerSets[$value["id"]]["id_domain"]=$value["id_domain"];
                }
              }
              foreach ($answerSets as $key=>$value)
              {
                $answerSets[$key]["text"]=ShortenText($answerSets[$key]["text"]);
                }
              
              $this->smarty->assign("answerSetList", $answerSets);
            }
            elseif ($this->getParams["searchWhere"]==="questions")
            {
              $query = "SELECT tQ.id, tQ.question, tQ.obsolete, tD.id AS id_domain FROM tQuestion AS tQ, ".
                       "tDomain AS tD WHERE tQ.id_domain=tD.id AND tD.id_course=".$this->getParams["courseID"]." AND ".
                       "LCASE(tQ.question) LIKE '%".$this->getParams["searchText"]."%' AND NOT tQ.obsolete ORDER BY tQ.question".$sort.";";
              $result = $this->db->QueryToArray($query);
              foreach ($result as $key=>$value)
              {
                $result[$key]["question"]=ShortenText($value["question"]);
              }
              
              $this->smarty->assign("questionList", $result);
            }
            elseif ($this->getParams["searchWhere"]==="domains")
            {
              $query = "SELECT tD.id, tD.subject, tD.obsolete FROM tDomain as tD WHERE tD.id_course=".
                       $this->getParams["courseID"]." AND LCASE(CONCAT(tD.subject, tD.description)) LIKE '%".
                       $this->getParams["searchText"]."%' AND NOT tD.obsolete ORDER BY tD.subject".$sort.";";
              $result = $this->db->QueryToArray($query);
              foreach ($result as $key=>$value)
              {
                $result[$key]["subject"]=ShortenText($value["subject"]);
              }
              
              $this->smarty->assign("domainList", $result);
            }
            elseif ($this->getParams["searchWhere"]==="students")
            {
              $query = "SELECT tU.id, CONCAT(tU.namePrefix, ' ', tU.surname, ', ', tU.name, ' ', tU.nameSuffix) AS name ".
                       "FROM tUser AS tU, tStudents AS tS, tCourseToUser AS tCTU, tCourse AS tC ".
                       "WHERE tU.id=tS.id AND tU.id=tCTU.id_user AND tCTU.id_course=tC.id AND tC.id=".
                       $this->getParams["courseID"]." AND LCASE(CONCAT(tU.namePrefix, tU.name, tU.surname, tU.nameSuffix)) ".
                       "LIKE '%".$this->getParams["searchText"]."%' ORDER BY tU.surname".$sort.";";
              $result = $this->db->QueryToArray($query);

              $this->smarty->assign("studentList", $result);
            }
            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("content","search.tpl");
      parent::Draw();
    }
	}
?>
