<?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 Projects extends Generic
{
	protected $primaryKey = 'id';
    protected $table = 'projects';
    protected $fillable = ['id', 'name', 'created', 'lastupdated', 'termid'];

    public function studentsStats() {
        $termid = (new Terms())->getActiveTerm()->id;

        $json = array();

        $max = 0;
        $max += (new Questions())
            ->where('enabled', 1)
            ->avg('value');

        $user = (new Users())->join('projects', 'projects.id', '=', 'projectid')->where('users.termid', $termid)->where('users.roleid', 1)->get();

        $raw = 0;
        $acc = 0;

        $rawamount = 0;
        $accamount = 0;
        foreach($user as &$row2)
        {
            $currentraw = $raw;
            $currentacc = $acc;
            $raw += (new Grades())
                ->where('studentid', $row2->id)
                ->where('termid', $termid)
                ->where('graded', 1)
                ->where('active', 1)
                ->avg('value');
            if($currentraw < $raw) $rawamount++;

            $acc +=  (new Grades())
                ->where('studentid', $row2->id)
                ->where('termid', $termid)
                ->where('accepted', 1)
                ->where('active', 1)
                ->avg('value');
            if($currentacc < $acc) $accamount++;

            $arr = array('name' => $row2->fname." ".$row2->lname, 'raw' => $raw == 0 ? 'Pending' : $raw / $rawamount, 'accepted' => $acc == 0 ? 'Pending' : $acc / $accamount, 'project' => $row2->name, 'max' => $max);
            array_push($json, $arr);
            $raw = 0;
            $acc = 0;

            $rawamount = 0;
            $accamount = 0;
        }

        return json_encode($json);
    }

    public function projectStats() {

        $termid = (new Terms())->getActiveTerm()->id;
        $result = $this->where('id', '<>', 0)->where('termid', $termid)->get();
        $json = array();
        $max = 0;
        $max += (new Questions())
            ->where('enabled', 1)
            ->avg('value');

        foreach($result as &$row)
        {
            $uid = (new Users())
                ->where('projectid', $row->id)
                ->where('roleid', 1)
                ->where('termid', $termid)->get();
            $raw = 0;
            $acc = 0;

            $rawamount = 0;
            $accamount = 0;

            foreach($uid as &$row2)
            {
                $currentraw = $raw;
                $currentacc = $acc;
                $raw += (new Grades())
                    ->where('studentid', $row2->id)
                    ->where('termid', $termid)
                    ->where('graded', 1)
                    ->where('active', 1)
                    ->avg('value');
                if($currentraw < $raw) $rawamount++;

                $acc +=  (new Grades())
                    ->where('studentid', $row2->id)
                    ->where('termid', $termid)
                    ->where('accepted', 1)
                    ->where('active', 1)
                    ->avg('value');
                if($currentacc < $acc) $accamount++;

            }
            $arr = array('id' => $row->id, 'name' => $row->name, 'raw' => $raw == 0 ? 'Pending' : $raw / $rawamount, 'accepted' => $acc == 0 ? 'Pending' : $acc / $accamount, 'max' => $max);
            array_push($json, $arr);

        }
        return json_encode($json);
    }
}