<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//include connection file 
include_once("db_cls_connect.php");
//include('../partials/header.php');
$db = new dbObj();
$connString = $db->getConnstring();

//$fl = new Faculty($connString);

class Faculty {

    public $facultyCode;
    public $facultyName;
    protected $conn;

    public function __construct() {
        $db = new dbObj();
        $connString = $db->getConnstring();
        $this->conn = $connString;
    }

    public function getFacultyCode() {
        return $this->facultyCode;
    }

    public function getFacultyName() {
        return $this->facultyName;
    }

    public function setFacultyCode($facultyCode) {
        $this->facultyCode = $facultyCode;
    }

    public function setFacultyName($facultyName) {
        $this->facultyName = $facultyName;
    }

    public function getAllFaculties() {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = 'SELECT * FROM faculty';

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }
    public function getDeptFacult($faculty) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM departments WHERE Faculty_FacultyCode1='$faculty'";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }
    public function checkFacCode($facultyCode) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "SELECT * FROM faculty WHERE FacultyCode='$facultyCode'";;

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        if (mysqli_num_rows($query) > 0) {
  	  return 'taken';	
  	}else{
  	  return 'not_taken';
  	}
        return "not_taken";
    }
    public function insertFaculty($facultyCode, $facultyname) {

        if (!$this->conn) {
            die('Failed to connect to MySQL: ' . mysqli_connect_error());
        }

        $sql = "INSERT INTO faculty(FacultyCode, FacultyName) VALUES ('$facultyCode', ' $facultyname')";

        $query = mysqli_query($this->conn, $sql);

        if (!$query) {
            die('SQL Error: ' . mysqli_error($this->conn));
        }
        return $query;
    }

}
