<?php
require_once(LIB_PATH . DS . 'database.php');

class StudentAcct extends DatabaseObject
{
    protected static $table_name = "student_account";
    public $id;
    public $surname;
    public $othernames;

    public $email;
    public $password;
    public $department;

    public $level;
    public $progtype;
    public $token;
    public $activated;
    public $date;
    public $acc_id;
    
    
    


 public static  function checkStu($email='', $password='')
    {
        global $database;

        $email = $database->escape_value($email);
        $password = $database->escape_value($password);
       

         $sql = "SELECT * FROM ". static::$table_name ." " ;
         $sql .= " WHERE email='{$email}' AND password='{$password}' ";
 
       return $result_array = self::find_by_sql($sql);

        //return !empty($result_array) ? array_shift($result_array) : false;


         
         

    } 



        public static  function accidbyemail($email,$token)
    {
        global $database;

        $email = $database->escape_value($email);
        
        $token = $database->escape_value($token);
       

         $sql = "SELECT * FROM ". static::$table_name ." " ;
         $sql .= " WHERE email='{$email}' AND token='{$token}' LIMIT 1 ";
 
        $result_array = self::find_by_sql($sql);

        return !empty($result_array) ? array_shift($result_array) : false;


         
         

    } 
            
            
       public static  function existingAcct($email)
    {
        global $database;

        $email = $database->escape_value($email);
        
        
       

         $sql =" SELECT s.*,b.acc_id FROM student_account s " ;
         $sql .=" INNER JOIN form_data b ON s.id=b.acc_id ";
         $sql .=" WHERE s.email='{$email}' ";
 
        return $result_array = self::find_by_sql($sql);

        //return !empty($result_array) ? array_shift($result_array) : false;


         
         

    } 
            
            
    public static  function accid($id)
    {
        global $database;

        $id = $database->escape_value($id);
       

         $sql = "SELECT * FROM ". static::$table_name ." " ;
         $sql .= " WHERE id='{$id}' AND activated='Y' LIMIT 1 ";
 
        $result_array = self::find_by_sql($sql);

        return !empty($result_array) ? array_shift($result_array) : false;


         
         

    } 

    

  public static  function StdupdateAcc($email='',$token='')
    {
        global $database; $connection='';

        $email = $database->escape_value($email);
        $token = $database->escape_value($token);


         $sql = "UPDATE  " . self::$table_name . " SET activated='Y' WHERE email='{$email}' AND token='{$token}' LIMIT 1";
        
      if ($database->query($connection,$sql)) {
           
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }

    } 

    
    
public static  function Stdsave($table, $surname='', $othernames='',$email='',$password='',$department='',$level='',$lev='',$token='')
    {
        global $database;$connection='';
        $surname = $database->escape_value($surname);
        $othernames = $database->escape_value($othernames);
        
        $email = $database->escape_value($email);
        $password = $database->escape_value($password);
        $department = $database->escape_value($department);
        $level = $database->escape_value($level);
        $progtype = $database->escape_value($lev);
        $token = $database->escape_value($token);
      

       $sql = "INSERT INTO $table (";
        $sql .= "surname,othernames,email,password, department, level,progtype, token ";

        $sql .= ") VALUES ('";
        $sql .= $surname . "', '";
        $sql .= $othernames . "', '";
        $sql .= $email . "', '";
        $sql .= $password . "', '";
        $sql .= $department . "', '";
        $sql .= $level . "', '";
        $sql .= $progtype . "', '";
        $sql .= $token . "'
              
            )";

        if ($database->query($connection,$sql)) {
            //$this->id=$database->insert_id();
            return true;
          // return 'Success';
        } else {
           return false;
            // return 'Error'; 
        }
    }



   
//Common database Methods

}
 $account = new StudentAcct();
?>