<?php

class AdministratorsController extends \BaseController
{
    public function overview()
    {
        //Total amount of learning outcomes
        $outcomeCount = Outcome::withTrashed()->count();

        // Id for excluding UHS from records
        $uhs_school_id = School::where('name', 'LIKE', '%UHS%')->first()->id;
        $uhs_program_id = Program::where('name', 'LIKE', '%UHS%')->first()->id;

        $schools = School::where('id', '!=', $uhs_school_id)->orderBy('name', 'asc')->get();
        $title = 'Campus Overview';
        $outcomes = Outcome::withTrashed()->select('id', 'name', 'expected_outcome')->orderBy('name', 'asc')->get();

        $undergrad_outcomes_achieved = array_fill(1, $outcomeCount, 0);
        $undergrad_outcomes_attempted = array_fill(1, $outcomeCount, 0);
        $grad_outcomes_achieved = array_fill(1, $outcomeCount, 0);
        $grad_outcomes_attempted = array_fill(1, $outcomeCount, 0);

        /**
         * Calculate campus wide outcome performance
         */

        // Calculate campus wide outcome performance for undergrad and grad
        foreach (Course::with('program')
            ->where('program_id', '!=', $uhs_program_id)
            ->where('code', '!=', 'TEST')
            ->whereNotNull('outcomes_achieved')
            ->whereIn('semester_id', Session::get('semesters_ids'))->get() as $course)
        {
            if(!$course->program->is_graduate)
            {
                $course_outcomes_achieved =json_decode($course->outcomes_achieved, true);
                $course_outcomes_attempted =json_decode($course->outcomes_attempted, true);
                for($i=1; $i<=count($undergrad_outcomes_attempted); $i++)
                {

                    $undergrad_outcomes_achieved[$i]+=$course_outcomes_achieved[$i];
                    $undergrad_outcomes_attempted[$i]+=$course_outcomes_attempted[$i];
                }
            }
            else
            {
                $course_outcomes_achieved =json_decode($course->outcomes_achieved, true);
                $course_outcomes_attempted =json_decode($course->outcomes_attempted, true);
                for($i=1; $i<=count($grad_outcomes_attempted); $i++)
                {

                    $grad_outcomes_achieved[$i]+=$course_outcomes_achieved[$i];
                    $grad_outcomes_attempted[$i]+=$course_outcomes_attempted[$i];
                }
            }

        }

        /**
         * Calculate how many programs achieved and attempted each outcome
         */

        // Number of programs that achieved a particular learning outcome
        $achievedUndergradProgramsPerOutcome = array_fill(1, $outcomeCount, 0);

        // Number of programs that attempted a particular learning outcome
        $attemptedUndergradProgramsPerOutcome = array_fill(1, $outcomeCount, 0);

        // Names of programs doing assessment
        // Fetch programs with participation
        $participating_undergrad_programs = DB::table('VIEW_participating_programs')
            ->whereIn('semester_id', Session::get('semesters_ids'))
            ->where('is_graduate', 0)
            ->groupBy('id')
            ->orderBy('name', 'asc')
            ->get();

        $output = array();


        // For each outcome
        foreach ($outcomes as $outcome)
        {
            // For each program with courses that do assessment
            foreach (Program::with(array('courses' => function($query){
                $query->whereNotNull('outcomes_attempted');
                $query->where('code', '!=', 'TEST');
                $query->whereIn('semester_id', Session::get('semesters_ids'));
            }))->where('is_graduate', 0)->orderBy('name', 'asc')->get() as $program)
            {
                // To acummulate all criteria for one program
                $achieved_outcomes_per_undergrad_program = array_fill(1, $outcomeCount, 0);
                $attempted_outcomes_per_undergrad_program = array_fill(1, $outcomeCount, 0);

                //Flag for counting programs
                $flag =false;

                // For each course in the program
                foreach ($program->courses as $course)
                {
                    // If the outcome in question is being evaluated
                    $course_outcomes_attempted2 = json_decode($course->outcomes_attempted, true);
                    $course_outcomes_achieved2 = json_decode($course->outcomes_achieved, true);
                    if(array_key_exists($outcome->id, $course_outcomes_attempted2 )
                        && $course_outcomes_attempted2[$outcome->id]>0)
                    {
                        $achieved_outcomes_per_undergrad_program[$outcome->id]+=$course_outcomes_achieved2[$outcome->id];
                        $attempted_outcomes_per_undergrad_program[$outcome->id]+=$course_outcomes_attempted2[$outcome->id];


                        // Add one to the programs assessing, if it wasn't added before
                        if(!$flag)
                        {
                            $attemptedUndergradProgramsPerOutcome[$outcome->id]+=1;
                            $flag= true;
                        }
                    }

                    // $output[] = 'ACHIEVED: '.$program->name.'-'.json_encode($achieved_outcomes_per_undergrad_program);
                    // $output[] = 'ATTEMPTED: '.$program->name.'-'.json_encode($attempted_outcomes_per_undergrad_program);

                }
                //If the accumulated achieved criteria for a specific outcome in a program divided by the accumulated attempted criteria for a specific outcome in a program is greated than the expected outcome
                if($attempted_outcomes_per_undergrad_program[$outcome->id]!=0 && (float)$achieved_outcomes_per_undergrad_program[$outcome->id]/$attempted_outcomes_per_undergrad_program[$outcome->id]*100 >= $outcome->expected_outcome)
                {
                    $achievedUndergradProgramsPerOutcome[$outcome->id]+=1;
                    // $output[]= 'END OF PROGRAM: '.$program->name.'-'.json_encode($achievedUndergradProgramsPerOutcome);
                }
            }
        }


        /**
         * Calculate how many programs achieved and attempted each outcome
         */

        // Number of programs that achieved a particular learning outcome
        $achievedGradProgramsPerOutcome = array_fill(1, $outcomeCount, 0);

        // Number of programs that attempted a particular learning outcome
        $attemptedGradProgramsPerOutcome = array_fill(1, $outcomeCount, 0);

        // Names of programs doing assessment
        $participating_grad_programs = DB::table('VIEW_participating_programs')
            ->whereIn('semester_id', Session::get('semesters_ids'))
            ->where('is_graduate', 1)
            ->groupBy('id')
            ->orderBy('name', 'asc')
            ->get();

        $output = array();


        // For each outcome
        foreach ($outcomes as $outcome)
        {
            // For each program with courses that do assessment
            foreach (Program::with(array('courses' => function($query){
                $query->whereNotNull('outcomes_attempted');
                $query->where('code', '!=', 'TEST');
                $query->whereIn('semester_id', Session::get('semesters_ids'));
            }))->where('is_graduate', 1)->orderBy('name', 'asc')->get() as $program)
            {
                // To acummulate all criteria for one program
                $achieved_outcomes_per_grad_program = array_fill(1, $outcomeCount, 0);
                $attempted_outcomes_per_grad_program = array_fill(1, $outcomeCount, 0);

                //Flag for counting programs
                $flag =false;

                // For each course in the program
                foreach ($program->courses as $course)
                {
                    // If the outcome in question is being evaluated
                    $course_outcomes_attempted2 = json_decode($course->outcomes_attempted, true);
                    $course_outcomes_achieved2 = json_decode($course->outcomes_achieved, true);
                    if(array_key_exists($outcome->id, $course_outcomes_attempted2 )
                        && $course_outcomes_attempted2[$outcome->id]>0)
                    {
                        $achieved_outcomes_per_grad_program[$outcome->id]+=$course_outcomes_achieved2[$outcome->id];
                        $attempted_outcomes_per_grad_program[$outcome->id]+=$course_outcomes_attempted2[$outcome->id];


                        // Add one to the programs assessing, if it wasn't added before
                        if(!$flag)
                        {
                            $attemptedGradProgramsPerOutcome[$outcome->id]+=1;
                            $flag= true;
                        }
                    }

                    // $output[] = 'ACHIEVED: '.$program->name.'-'.json_encode($achieved_outcomes_per_grad_program);
                    // $output[] = 'ATTEMPTED: '.$program->name.'-'.json_encode($attempted_outcomes_per_grad_program);

                }
                //If the accumulated achieved criteria for a specific outcome in a program divided by the accumulated attempted criteria for a specific outcome in a program is greated than the expected outcome
                if($attempted_outcomes_per_grad_program[$outcome->id]!=0 && (float)$achieved_outcomes_per_grad_program[$outcome->id]/$attempted_outcomes_per_grad_program[$outcome->id]*100 >= $outcome->expected_outcome)
                {
                    $achievedGradProgramsPerOutcome[$outcome->id]+=1;
                    // $output[]= 'END OF PROGRAM: '.$program->name.'-'.json_encode($achievedGradProgramsPerOutcome);
                }
            }
        }

        return View::make('local.managers.admins.overview', compact('title', 'schools', 'outcomes', 'undergrad_outcomes_achieved', 'undergrad_outcomes_attempted', 'grad_outcomes_achieved', 'grad_outcomes_attempted', 'attemptedUndergradProgramsPerOutcome', 'participating_undergrad_programs', 'achievedUndergradProgramsPerOutcome', 'attemptedGradProgramsPerOutcome', 'participating_grad_programs', 'achievedGradProgramsPerOutcome'));
    }
}