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

use Illuminate\Database\Query\Expression as raw;

class Grades extends Generic
{
    protected $primaryKey = 'id';
    protected $table = 'grades';
    protected $fillable = ['id','judgeid', 'studentid', 'questionsid', 'accepted', 'value', 'comment', 'created', 'lastupdated', 'termid', 'graded', 'active'];

    public function getById($id)
    {
        return $this::where('id', $id);
    }

    public function student()
    {
        return $this->hasOne('users', 'studentid', 'id');
    }

    public function judge()
    {
        return $this->hasOne('users', 'judgeid', 'id');
    }

    public function getGradesInfo()
    {
        $table = $this::join('users as student', 'student.id', '=', 'grades.studentid')
            ->join('users as judge', 'judge.id', '=', 'grades.judgeid')
            ->select('grades.id', 'student.fname', 'student.lname', 'judge.fname as jFirstName', 'judge.lname as jLastName', 'grades.accepted', new raw('AVG(`grades`.`value`) as value') )
            ->where('active', 1)
            ->orderBy('student.fname')
            ->groupBy('grades.judgeid', 'grades.studentid')
            ->get();

        return $table;
    }

    public function getCurrentAssignments($judgeId)
    {
        $questions = [];
        $enabledQuestions = (new Questions())->where('enabled', 1)->get();
       // var_dump($enabledQuestions);

        foreach($enabledQuestions as &$question)
        {
            array_push($questions, $question->id);

        }
        //var_dump($questions);
        $judgeStudents = $this::join('users as student', 'student.id', '=', 'grades.studentid')
            ->join('projects', 'projects.id', '=', 'student.projectid')
            ->join('locations', 'locations.id', '=', 'student.locationid')
            ->select('grades.studentid', 'grades.judgeid', 'projects.name as project', 'student.id as stuid', 'student.fname', 'student.lname', 'locations.name as location')
            ->where('judgeid', $judgeId)
            ->where('active', 1)
            ->groupBy('studentid')
            ->get();

        $num = 0;
        $gr = 0;
        foreach($judgeStudents as &$student)
        {
            
            $grades = $this->where('studentid', $student->studentid)->where('judgeid', $student->judgeid)->where('active', 1)->get();
            $gradesArr = [];
            foreach($grades as &$grade)
            {
                if(in_array($grade->questionsid, $questions))
                {
                    array_push($gradesArr, $grade->questionsid);
                }
                $num++;
                if($grade->graded == 1)
                {
                    $gr++;
                }
            }
            foreach($questions as &$question)
            {
                if(!in_array($question, $gradesArr))
                {
                    $gradeToAdd = (new Grades());
                    $gradeToAdd->studentid = $student->studentid;
                    $gradeToAdd->judgeid = $student->judgeid;
                    $gradeToAdd->questionsid = $question;
                    $gradeToAdd->termid = (new Terms())->getActiveTerm()->id;
                    $gradeToAdd->value = (new Questions())->getById($question)->value;
                    $gradeToAdd->accepted = 0;
                    $gradeToAdd->graded = 0;
                    $gradeToAdd->active = 1;
                    $gradeToAdd->save();
                }
            }
            

        }
        if($num == $gr)
        {
            $judge = (new Users())->getById($judgeId);
            $judge->statesid = 8;
            $judge->update();
        }

        /*
        foreach ($arr as &$value) {
            $value = $value * 2;
        }*/
        //var_dump($judgeStudents);
        $table = $this::join('users as student', 'student.id', '=', 'grades.studentid')
            ->join('projects', 'projects.id', '=', 'student.projectid')
            ->join('locations', 'locations.id', '=', 'student.locationid')
            ->select('grades.studentid', 'grades.judgeid', 'projects.name as project', 'student.fname', 'student.lname', 'locations.name as location')
            ->where('judgeid', $judgeId)
            ->where('active', 1)
            ->groupBy('judgeid', 'studentid')
            ->get();
        return $table;
    }
}