<?php

class ProgramCoordinatorsController extends \BaseController {

    public function overview()
    {
        //TODO Find a better way to prevent non-scoords from reaching the page
        switch(Auth::user()->role)
        {
            case 1:
                return Redirect::to('administrator');
                break;
            case 2:
                return Redirect::to('school-coordinator');
                break;
        }

        $title = 'Program Coordinator Overview';

        $programs = Auth::user()->programs;

        $outcomes = Outcome::orderBy('name', 'asc')->get();
        $outcomeCount = Outcome::all()->count();

        $programs_array = array();
        $programs_contact = array();

        foreach ($programs as $program)
        {
          $program_array = array();

          $program_array['program'] = $program;

          $program_array['outcomes_achieved'] = array_fill(1, $outcomeCount, 0);
          $program_array['outcomes_attempted'] = array_fill(1, $outcomeCount, 0);

          $program_array['program_courses'] = $program->courses;
          $program_array['grouped_objectives'] = $program->objectives();

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

          /**
           * List of grouped courses (grouped sections)
           */

          $program_array['grouped_courses'] = Course::
            select(DB::raw('name, code, number, max(outcomes_attempted) as outcomes_attempted, semester_id, program_id'))
            ->with('semester')
            ->with('program')
            ->where('program_id', $program->id)
            ->whereIn('semester_id', Session::get('semesters_ids'))
            ->groupBy(array('code', 'number', 'semester_id'))
            ->orderBy('code')
            ->orderBy('number')
            ->orderBy('semester_id')
            ->get();


          $programs_array[] = $program_array;


          // Program contact information
          $users = User::
              select('users.*')
              ->leftJoin('program_user', 'users.id', '=', 'program_user.user_id')
              ->where(function($query) use($program)
              {
                $query
                    ->where('school_id', $program->school_id)
                    ->where('role', 2);
              })
              ->orWhere(function($query) use($program)
              {
                $query
                    ->where('role', 3)
                    ->where('program_id', $program->id);
              })
              ->orWhere(function($query) use($program)
              {
                $query
                    ->where('role', 4)
                    ->where('program_id', $program->id);
              })
              ->get();


            $programs_contact[] = $users;

        }


        return View::make('local.managers.pCoords.overview', compact('title', 'programs', 'outcomes', 'programs_array', 'programs_contact'));
    }

}
