<?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();
        $outcomes = Outcome::orderBy('name', 'asc')->get();
        $outcomeCount = Outcome::all()->count();


        $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);

            // 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)
            {
                if($section->outcomes_achieved!=NULL)
                {
                    $section_outcomes_achieved =json_decode($section->outcomes_achieved, true);
                    $section_outcomes_attempted =json_decode($section->outcomes_attempted, true);
                    for($i=1; $i<=count($outcomes_attempted); $i++)
                    {
                        $outcomes_achieved[$i]+=$section_outcomes_achieved[$i];
                        $outcomes_attempted[$i]+=$section_outcomes_attempted[$i];
                    }
                }
            }

            $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();
        $outcomes = Outcome::orderBy('name', 'asc')->get();
        $outcomeCount = Outcome::all()->count();

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

        foreach ($program_courses as $course)
        {
            if($course->outcomes_achieved!=NULL)
            {
                $course_outcomes_achieved =json_decode($course->outcomes_achieved, true);
                $course_outcomes_attempted =json_decode($course->outcomes_attempted, true);
                for($i=1; $i<=count($outcomes_attempted); $i++)
                {
                    $outcomes_achieved[$i]+=$course_outcomes_achieved[$i];
                    $outcomes_attempted[$i]+=$course_outcomes_attempted[$i];
                }
            }
        }

        // 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'));


    }
}