<?php
	class cChangePasswordPage extends cTeacherPage
	{
    private $menuItem;

    function __construct( $smarty, $db, $action, $postParams, $getParams )
    {
      parent::__construct( $smarty, $db, $action, $postParams, $getParams );
      if ($this->bLoggedInAsTeacher)
        $this->menu=new cMainTeacherMenu;
      else
        $this->menu=new cMainProctorMenu;
      $this->menu->GenerateMenu("");
      $menuItem=$this->menu->GetItemByLabel("zmena hesla");
      $menuItem->bSelected=true;
   }
		
    function Process()
    {
      if ($this->bLoggedInAsTeacher || $this->bLoggedInAsProctor)
      {
        if ( $this->action === "change")
        {
          //changing password
          //p1 - stare heslo, p2 - nove heslo, p3 - zopakovane heslo
          if ( empty( $this->postParams["p1"] ) )
          {
            $this->smarty->assign( "error", "Musíte zadať staré heslo!" );        
          }          
          else if ( empty( $this->postParams["p2"] ) )
          {
            $this->smarty->assign( "error", "Musíte zadať nové heslo!" );        
          }          
          else if ( $this->postParams["p2"] != $this->postParams["p3"] )
          {
            $this->smarty->assign( "error", "Zle zopakované heslo!" );        
          }
          else
          { 
            $this->postParams["p1"]=mysql_real_escape_string($this->postParams["p1"]);
            $this->postParams["p2"]=mysql_real_escape_string($this->postParams["p2"]);
            $this->postParams["p3"]=mysql_real_escape_string($this->postParams["p3"]);
            $query = "SELECT password, passPrefix FROM tUser WHERE id=".$_SESSION["userID"].";";
            $result = $this->db->QueryToRow($query);
            
            if ( $result == 0 )
            {
              $this->smarty->assign( "error", $this->db->GetError() );
            }                                
            else if ( md5($result["passPrefix"].$this->postParams["p1"]) == $result["password"] )
            {
              $query = "UPDATE tUser SET password=MD5(CONCAT(passPrefix,'".$this->postParams["p2"]."')) ".
                       "WHERE id=".$_SESSION["userID"].";";
            
              $result = $this->db->Query($query);
              if ( $result )
                $this->smarty->assign( "actionResult", "Heslo bolo úspešne zmenené." );
              else
                $this->smarty->assign( "error", $this->db->GetError() );
            }
            else
            {              
              $this->smarty->assign( "error", "Nesprávne zadané heslo!" );
            }
          }
        }
      }
      return true;
    }
    
    function Draw()
    {
      $this->smarty->assign("content", "changePassword.tpl");
      parent::Draw();
    }
	}
?>
