<?php
/**
 * Created by IntelliJ IDEA.
 * User: DennysO
 * Date: 6/3/2015
 * Time: 11:10 AM
 */
require_once "generic.php";

class Users extends Generic
{
	protected $primaryKey = 'id';
    protected $table = 'users';
    protected $fillable = ['id', 'email', 'pass', 'fname', 'lname', 'termid', 'statesid', 'roleid', 'projectid', 'title', 'affiliation', 'locationid', 'created', 'lastupdated'];
    private $term;

    public function Users()
    {
        parent::__construct();
        $this->term = (new Terms())->getActiveTerm();
    }

    public function role()
    {
        return $this->hasOne('Roles', 'id', 'roleid');
    }

    public function states()
    {
        return $this->hasOne('States', 'id', 'statesid');
    }

    public function project()
    {
        return $this->hasOne('Projects', 'id', 'projectid');
    }

    public function getStudentsInfo() {
        $activeTerm = (new Terms())->getActiveTerm()->id;
        $table = $this->select('users.id','fname', 'lname', 'email', 'users.termid', 'projectid', 'statesid','projects.name')
            ->join('projects', 'users.projectid', '=', 'projects.id')
            ->with('states')
            ->where('users.termid', $activeTerm)
            ->where('roleid', 1)
            ->orderBy('fname')
            ->get();

        return $table;
    }

    function getJudgesInfo()
    {
        $termid = (new Terms())->getActiveTerm()->id;
        $table = new Users();
        $table = $table->select('id', 'fname', 'lname', 'email', 'affiliation', 'title', 'statesid', 'termid')
            ->with('states')
            ->where('roleid', 2)
            ->where('users.termid', $termid)
            ->orderBy('fname')
            ->get();

        return $table;
    }

    public function getFullStudentsInfo($termid, $statename)
    {
        $table = $this->select('users.id', 'fname', 'lname', 'email', 'users.termid', 'projectid', 'projects.name',
            'states.name AS sname', 'terms.name AS tname')
        ->join('projects', 'users.projectid', '=', 'projects.id')
        ->join('states', 'users.statesid', '=', 'states.id')
        ->join('terms', 'users.termid', '=', 'terms.id')
        ->where('states.name', $statename)
        ->where('terms.id', $termid)
        ->where('users.roleid', 1)
        ->orderBy('fname')
        ->get();

        /*$table = $this
        ->join('terms', 'users.termid', '=', 'terms.id')
        ->join('states', 'users.statesid', '=', 'states.id')
        ->where('terms.id', $termid)
        ->where('states.name', $statename)
        ->where('users.roleid', 1)->get();*/

        return $table;
    }

    public function getFullJudgesInfo($termid, $statename)
    {
        $table = $this->select('users.id', 'fname', 'lname', 'email', 'users.termid', 'projectid', 'projects.name',
            'states.name AS sname', 'terms.name AS tname')
        ->join('projects', 'users.projectid', '=', 'projects.id')
        ->join('states', 'users.statesid', '=', 'states.id')
        ->join('terms', 'users.termid', '=', 'terms.id')
        ->where('states.name', $statename)
        ->where('terms.id', $termid)
        ->where('users.roleid', 2)
        ->orderBy('fname')
        ->get();

        return $table;
    }

    public function login($user, $pass){
        $activeTerm = (new Terms())->getActiveTerm();
        $admin = (new Users())->isadmin($user);
        
        if($admin)
        {
            $result = $this->select('users.id','fname', 'lname', 'email', 'pass', 'users.termid', 'projectid', 'projects.name', 'roleid', 'locationid', 'locations.name AS nameLocation')
            ->join('projects', 'users.projectid', '=', 'projects.id')
            ->join('locations', 'users.locationid', '=', 'locations.id')
            ->where('email', $user)
                ->where('pass', '=', $pass)
            ->get();
        }

        else
        {
            $result = $this->select('users.id','fname', 'lname', 'email', 'users.termid', 'projectid', 'projects.name', 'roleid', 'locationid', 'locations.name AS nameLocation')
            ->join('projects', 'users.projectid', '=', 'projects.id')
            ->join('locations', 'users.locationid', '=', 'locations.id')
            ->where('email', '=', $user)
            ->where('pass', '=', $pass)
            ->where('users.termid', '=', $activeTerm->id)
            ->get();

            $user = null;
            //var_dump($result);
            //echo sizeof($result);
            if(sizeof($result) != 0){ $user = (new Users())->getById($result[0]->id);}
            if($user != null)
            {
                if($user->roleid == 2)
                {
                    if($activeTerm->allowjudgestologin != 1)
                    {
                        $result = -1;
                    }
                    elseif($user->statesid == 5 && strtotime($activeTerm->start) < time() && strtotime($activeTerm->end) > time())
                    {
                        $user->statesid = 6;
                        $user->update();
                    }
                }
            }
        }

        return $result;
    }

    public function isadmin($user){
        $temp = new Users();
        $result = $temp->select('roleid')->where('email', $user)->first();
        $isAdmin = $result != null ? $result->roleid == 3 : false;
        return ($isAdmin);
    }

    public function idadmin($user){
        $temp = new Users();
        $result = $temp->select('roleid')->where('id', $user)->first();
        $isAdmin = $result != null ? $result->roleid == 3 : false;
        return ($isAdmin);
    }

    function getOldJudgesInfo()
    {
        $termid = (new Terms())->getActiveTerm()->id;
        $table = new Users();
        $table = $table->select('id', 'fname', 'lname', 'email', 'affiliation', 'title', 'statesid', 'termid')
        ->with('states')
        ->where('roleid', 2)
        ->where('users.termid', '<>', $termid)
        ->get();

        return $table;
    }

    function getAllJudgesInfo()
    {
        $table = new Users();
        $table = $table->select('id', 'fname', 'lname', 'email', 'affiliation', 'title', 'statesid', 'termid')
        ->with('states')
        ->where('roleid', 2)
        ->get();

        return $table;
    }

    public function getAdminHome()
    {
        $total = $this->where("termid", $this->term->id)->where("roleid", 1)->count();
        $term = (new Terms())->getActiveTerm();
        $stugrades = (new Grades())->where('termid', $term->id)->where('active', 1)->where('graded', 0)->groupBy('studentid')->get();
        $pendingGrade = sizeof($stugrades);
        $graded = (new Grades())->where('termid', $term->id)->where('active', 1)->where('graded', 1)->count();
        $notgraded = (new Grades())->where('termid', $term->id)->where('active', 1)->where('graded', 0)->count();
        $acceptedg = (new Grades())->where('termid', $term->id)->where('active', 1)->where('graded', 1)->where('accepted', 1)->count();
        $reflected = (new Grades())->where('termid', $term->id)->where('active', 1)->where('graded', 1)->where('accepted', 2)->count();

        $pro = (new Users())->where('roleid', 2)->where('termid', $term->id)->where('statesid', 1)->count();
        $inv = (new Users())->where('roleid', 2)->where('termid', $term->id)->where('statesid', 2)->count();
        $rej = (new Users())->where('roleid', 2)->where('termid', $term->id)->where('statesid', 3)->count();
        $pen = (new Users())->where('roleid', 2)->where('termid', $term->id)->where('statesid', 4)->count();
        $reg = (new Users())->where('roleid', 2)->where('termid', $term->id)->where('statesid', 5)->count();
        $att = (new Users())->where('roleid', 2)->where('termid', $term->id)->where('statesid', 6)->count();
        $sta = (new Users())->where('roleid', 2)->where('termid', $term->id)->where('statesid', 7)->count();
        $gra = (new Users())->where('roleid', 2)->where('termid', $term->id)->where('statesid', 8)->count();
        $rem = (new Users())->where('roleid', 2)->where('termid', $term->id)->where('statesid', 12)->count();
        $result = array(
            "total" => $total,
            "pendingGrade" => $pendingGrade,
            "graded" => $graded,
            "notgraded" => $notgraded,
            "reflected" => $reflected,
            "pro" => $pro,
            "inv" => $inv,
            "rej" => $rej,
            "pen" => $pen,
            "reg" => $reg,
            "att" => $att,
            "sta" => $sta,
            "gra" => $gra,
            "rem" => $rem,
            "acceptedg" => $acceptedg
        );
        return json_encode($result);
    }

    public function loadStudents()
    {
        $term = (new Terms())->getActiveTerm();
        $dataURL = $term->srprojecturl . "/getAll/" . $term->srprojecttoken;
        $data = file_get_contents($dataURL);
        $data = json_decode($data);
        $added = 0;
        $modified = 0;
        $inactive = 0;
        foreach($data as &$student) {
            $curStudent = $this->where('id', $student->id)
                ->where('termid', $term->id)
                ->first();

            $curProject = (new Projects())->where('id', $student->projectID)->first();
            $user = new Users();
            if($curProject == null)
            {
                $newProject = new Projects();
                $newProject->id = $student->projectID;
                $newProject->name = $student->projectTitle;
                $newProject->termid = $term->id;
                $newProject->save();
            }
            else
            {
                if($student->projectTitle != $curProject->name)
                {
                    $curProject->name = $student->projectTitle;
                    $curProject->update();
                }
            }

            if ($curStudent == null)
            {
                $user->id = $student->id;
                $user->email = $student->email."@fiu.edu";
                $user->fname = $student->firstName;
                $user->lname = $student->lastName;
                $user->termid = $term->id;
                $user->roleid = 1;
                $user->statesid = 9;
                $user->locationid = 1;
                $user->projectid = $student->projectID;
                $user->save();

                $added++;
            }
            else
            {
                $curStudent->email = $student->email."@fiu.edu";
                $curStudent->fname = $student->firstName;
                $curStudent->lname = $student->lastName;
                $curStudent->termid = $term->id;
                $user->statesid = 9;
                $user->locationid = 1;
                $curStudent->roleid = 1;
                $curStudent->projectid = $student->projectID;
                $curStudent->save();

                $modified++;
            }
        }
        return '{"Added":"'.$added.'","Modified":"'.$modified.'","Dropped":"'.$inactive.'"}';
    }

    public function getPastStudents()
    {
        $term = (new Terms())->getActiveTerm();

        return $this->where("termid", "<>", $term->id)->where('roleid', 1)->get();
    }

    public function getAllStudents()
    {
        return $this->where('roleid', 1)->get();
    }

    public function getPastJudges()
    {
        $term = (new Terms())->getActiveTerm();

        return $this->where("termid", "<>", $term->id)->where('roleid', 2)->get();
    }

    public function getAllJudges()
    {
        return $this->where('roleid', 2)->get();
    }
}