<?php 
/**
 * Model for Submissions
 *
 * @author Mills ABeiku Godfred
 */
class Submission
{
	
	public $conn;
	
	function __construct()
	{
		$this->conn = Database::connect();
	}

    public function update_submission(array $key_col_vals, array $values) {
        $sql = "UPDATE submission SET ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $key_cols = array_keys($key_col_vals);
        $key_vals = array_values($key_col_vals);
        foreach ($fields as $i=>$f) {
            $fields[$i] .= " = ? ";
        }

        $sql .= implode(",", $fields);
        $sql .= " WHERE " ;
        
        foreach ($key_cols as $i=>$f) {
            $key_cols[$i] .= " = ? ";
        }
        
        $sql .= implode(" AND ", $key_cols);

        $statement = $this->conn->prepare($sql);
        foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
        }
        
        foreach ($key_vals as $i=>$v) {
           if($i==0){
               $statement->bindValue((count($fields)+$i+1), $v);
            }else{
               $statement->bindValue((count($fields)+$i), $v);
            }
        }
        
       return  $statement->execute();

	}


	public function set_log(array $data){
		$sql = "INSERT INTO submission_history ";
		$fields = array_keys($data);
		$vals = array_values($data);

		$sql .= "(".implode(",", $fields).")";
		$arr = array();
		foreach ($fields as $f) {
			$arr[] = "?";
		}
		$sql .= " VALUES(".implode(",", $arr).")";

		$statement = $this->conn->prepare($sql);

		foreach ($vals as $i => $v) {
			$statement->bindValue($i + 1, $v);
		}

		return $statement->execute();

	}

	public function add_submission(array $data){
		$sql = "INSERT INTO submission ";
		$fields = array_keys($data);
		$vals = array_values($data);

		$sql .= "(".implode(",", $fields).")";
		$arr = array();
		foreach ($fields as $f) {
			$arr[] = "?";
		}
		$sql .= " VALUES(".implode(",", $arr).")";

		$statement = $this->conn->prepare($sql);

		foreach ($vals as $i => $v) {
			$statement->bindValue($i + 1, $v);
		}

		return $statement->execute();

	}

	public function set_attempt_log(array $data){
		$sql = "INSERT INTO submission_attempts_history ";
		$fields = array_keys($data);
		$vals = array_values($data);

		$sql .= "(".implode(",", $fields).")";
		$arr = array();
		foreach ($fields as $f) {
			$arr[] = "?";
		}
		$sql .= " VALUES(".implode(",", $arr).")";

		$statement = $this->conn->prepare($sql);

		foreach ($vals as $i => $v) {
			$statement->bindValue($i + 1, $v);
		}

		return $statement->execute();

	}    
	
	public function auth($email,$phone,$key){
		$sql = "SELECT * FROM student WHERE email=:email
         AND phone=:phone  AND EXISTS (SELECT * FROM exam WHERE exam_key=:key AND status='Active')";
		$statement = $this->conn->prepare($sql);
		$statement->bindParam(":email",$email);
		$statement->bindParam(":phone",$phone);
        $statement->bindParam(":key",$key);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

    public function check($student_id,$exam_key,$exam_module){
		$sql = "SELECT * FROM submission WHERE exam_key=:exam_key 
        AND student_id=:student_id AND exam_module=:exam_module";
		$statement = $this->conn->prepare($sql);
		$statement->bindParam(":student_id",$student_id);
        $statement->bindParam(":exam_key",$exam_key);
        $statement->bindParam(":exam_module",$exam_module);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

	public function remove_employee_submission($email,$phone){
		$sql = "DELETE FROM submission WHERE email=:email || phone=:phone";
		$statement = $this->conn->prepare($sql);
		$statement->bindParam(":email",$email);
        $statement->bindParam(":phone",$phone);
		$statement->execute();
		if ($statement->rowCount() > 0) {
            $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

}