<?php

class ProfessorsController extends \BaseController
{

    private $grouped_courses;

    public function __construct()
    {
        $this->courses = Auth::user()->courses;
    }

    public function overview()
    {
        $title = 'My Courses';
        $grouped_courses = Course::where('user_id', Auth::user()->id)->whereIn('semester_id', Session::get('semesters_ids'))->groupBy(array('code', 'number', 'semester_id'))->get();
        $semesters = Session::get('semesters_ids');
        $semesters = DB::table('semesters')->whereIn('id', $semesters)->orderBy('start', 'ASC')->first();
        Log::info($semesters->start);
        $outcomes = Outcome::select(array('id', 'name', 'expected_outcome'))
            ->whereNull('deleted_at')
            ->whereRaw("(deactivation_date IS NULL or deactivation_date >= '{$semesters->start}')")
            ->orderBy('name', 'ASC')->get();
        $outcomeCount = count($outcomes);


        $grouped_outcomes_achieved_results = array();
        $grouped_outcomes_attempted_results = array();
        $grouped_sections = array();

        foreach ($grouped_courses as $index => $grouped_course) {
            // Blank outcomes for one course

            //$outcomes_achieved = array_fill(1, $outcomeCount, 0);
            //$outcomes_attempted = array_fill(1, $outcomeCount, 0);

            $outcomes_achieved = [];
            $outcomes_attempted = [];

            // Find sections belonging to user with identifier of one course
            $sections = Course::where('user_id', Auth::user()->id)
                ->where('code', $grouped_course->code)
                ->whereIn('semester_id', Session::get('semesters_ids'))
                ->where('number', $grouped_course->number)
                ->where('semester_id', $grouped_course->semester_id)
                ->get();

            // For each of the professor's course sections, add the attempted and achieved criteria per outcome
            foreach ($sections as $section) {

                $section_outcomes_achieved = $section->outcomes_ach();
                $section_outcomes_attempted = $section->outcomes_att();

                //if ($section->outcomes_achieved != NULL) {
                if (!empty($section_outcomes_achieved)) {
                    //$section_outcomes_achieved = json_decode($section->outcomes_achieved, true);
                    //$section_outcomes_attempted = json_decode($section->outcomes_attempted, true);
                    foreach ($section_outcomes_attempted as $outcome_id => $score) {

                        if (array_key_exists($outcome_id, $outcomes_achieved)) $outcomes_achieved[$outcome_id] += $section_outcomes_achieved[$outcome_id];
                        else $outcomes_achieved[$outcome_id] = $section_outcomes_achieved[$outcome_id];
                        if (array_key_exists($outcome_id, $outcomes_attempted)) $outcomes_attempted[$outcome_id] +=  $section_outcomes_attempted[$outcome_id];
                        else $outcomes_attempted[$outcome_id] =  $section_outcomes_attempted[$outcome_id];
                        //$outcomes_achieved[$outcome_id] += $section_outcomes_achieved[$outcome_id];
                        //$outcomes_attempted[$outcome_id] += $section_outcomes_attempted[$outcome_id];
                    }
                }
            }

            $grouped_outcomes_achieved_results[] = $outcomes_achieved;
            $grouped_outcomes_attempted_results[] = $outcomes_attempted;
            $grouped_sections[] = $sections;
        }

        return View::make('local.professors.overview', compact('title', 'grouped_courses', 'outcomes', 'grouped_outcomes_attempted_results', 'grouped_outcomes_achieved_results', 'grouped_sections'));
    }

    public function program()
    {
        $program =  Auth::user()->programs[0];
        $title = 'Your Program: ' . $program->name;
        $program_courses = Course::where('program_id', '=', $program->id)->whereIn('semester_id', Session::get('semesters_ids'))->get();
        $semesters = Session::get('semesters_ids');
        $semesters = DB::table('semesters')->whereIn('id', $semesters)->orderBy('start', 'ASC')->first();

        $outcomes = Outcome::select(array('id', 'name', 'expected_outcome'))
            ->whereNull('deleted_at')
            ->whereRaw("(deactivation_date IS NULL or deactivation_date >= '{$semesters->start}')")
            ->orderBy('name', 'ASC')->get();
        $outcomeCount = count($outcomes);
        $outcomes_achieved = [];
        $outcomes_attempted = [];

        Log::info($program_courses);


        foreach ($program_courses as $course) {


            if ($course->outcomes_ach()) {

                $course_outcomes_achieved = $course->outcomes_ach();
                $course_outcomes_attempted = $course->outcomes_att();
                foreach ($course_outcomes_achieved as $outcome => $score) {
                    if (array_key_exists($outcome, $outcomes_achieved))
                        $outcomes_achieved[$outcome] += $score;
                    else $outcomes_achieved[$outcome] = $score;
                }
                foreach ($course_outcomes_attempted as $outcome => $score) {
                    if (array_key_exists($outcome, $outcomes_attempted))
                        $outcomes_attempted[$outcome] += $score;
                    else $outcomes_attempted[$outcome] = $score;
                }
            }
        }


        // Program contact information
        $scoords = User::where('school_id', $program->school_id)
            ->where('role', 2)
            ->get();
        $pcoords = User::select('users.*')
            ->join('program_user', 'users.id', '=', 'program_user.user_id')
            ->where('role', 3)
            ->where('program_id', $program->id)
            ->get();

        return View::make('local.professors.program', compact('title', 'outcomes', 'outcomes_attempted', 'outcomes_achieved', 'scoords', 'pcoords'));
    }


    public function generalStudiesOverview()
    {

        $title = 'General Component Assessment Overview';

        try {
            $programs = Program::generalComponentPrograms();
        } catch (Exception $e) {
            dd('Unable to find general component programs');
        }

        $outcomes = Outcome::orderBy('name', 'asc')->get();
        $schools = School::all();
        $program_packs = array();
        if (!$programs->isEmpty()) {
            foreach ($programs as $program) {
                $program_packs[] = $program->assessmentOverview();
            }
        }

        return View::make('local.managers.shared.general_studies_overview', compact('title', 'outcomes', 'schools', 'program_packs'));
    }
}