<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

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;
    }
    //Log::info(Auth::user()->programs);
    $title = 'Program Coordinator Overview';

    $programs = Auth::user()->programs;
    //Log::info($programs);

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

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

    // old code
    /*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;
        }
      }
      */


    foreach ($programs as $program) {
      //Log::info($program);
      $program_array = array();
      $program_object = new \stdClass();
      $program_object->name = $program->name;
      $program_object->school = $program->school->name;
      $program_object->id = $program->id;
      $program_array['program'] = $program_object;
      //Log::info($program_array);

      $program_array['outcomes_achieved'] = [];
      $program_array['outcomes_attempted'] = [];
      //Log::info($program_array);

      $program_array['program_courses'] = $program->courses;


      $program_array['assessed_courses_count'] = 0;
      foreach ($program->courses as $course) {

        $student_report = $course->student_report_for_outcome;

        if ($student_report) {

          foreach ($student_report as $outcome_id => $score) {
            if (array_key_exists($outcome_id, $program_array['outcomes_attempted']))  $program_array['outcomes_attempted'][$outcome_id] += $student_report[$outcome_id]['calculations']['student_attempted'];
            else $program_array['outcomes_attempted'][$outcome_id] = $student_report[$outcome_id]['calculations']['student_attempted'];

            if (array_key_exists($outcome_id, $program_array['outcomes_achieved']))  $program_array['outcomes_achieved'][$outcome_id] += $student_report[$outcome_id]['calculations']['student_achieved'];
            else $program_array['outcomes_achieved'][$outcome_id] = $student_report[$outcome_id]['calculations']['student_achieved'];
          }


          $program_array['assessed_courses_count'] += 1;
        }
      }


      /*foreach ($programs as $program) {
      //Log::info($program);
      $program_array = array();
      $program_object = new \stdClass();
      $program_object->name = $program->name;
      $program_object->school = $program->school->name;
      $program_object->id = $program->id;
      $program_array['program'] = $program_object;
      //Log::info($program_array);

      $program_array['outcomes_achieved'] = [];
      $program_array['outcomes_attempted'] = [];
      //Log::info($program_array);

      $program_array['program_courses'] = $program->courses;


      $program_array['assessed_courses_count'] = 0;


      foreach ($program_array['program_courses'] as $course) {

        //$student_report = $course->student_report_for_outcome;
        //Log::info($program_array);

        $program_array['course_outcomes_achieved'] = $course->outcomes_ach();

        //Log::info("course_outcomes_achieved");
        //Log::info($program_array['course_outcomes_achieved']);
        //Log::info($program_array);
        if ($program_array['course_outcomes_achieved']) {
          //(($program_array['course_outcomes_achieved'])) {
          $program_array['course_outcomes_achieved'] = $course->outcomes_ach();
          $program_array['course_outcomes_attempted'] = $course->outcomes_att();
          //Log::info('course_outcomes_achieved');
          //Log::info($program_array['course_outcomes_achieved']);
          //Log::info($program_array);

          foreach ($program_array['course_outcomes_achieved'] as $outcome_id => $score) {
            if (array_key_exists($outcome_id, $program_array['outcomes_achieved']))  $program_array['outcomes_achieved'][$outcome_id] += $score;
            else $program_array['outcomes_achieved'][$outcome_id] = $score;
            //Log::info('program["outcomes_achieved"]');
            //Log::info($program_array['outcomes_achieved'][$i]);
            //Log::info($program_array);
            Log::info("outcomes_achieved");
            Log::info($program_array['outcomes_achieved']);
          }
          foreach ($program_array['course_outcomes_attempted'] as $i => $score) {

            if (array_key_exists($i, $program_array['outcomes_attempted']))  $program_array['outcomes_attempted'][$i] += $program_array['course_outcomes_attempted'][$i];
            else $program_array['outcomes_attempted'][$i] = $program_array['course_outcomes_attempted'][$i];
            //Log::info('program["outcomes_achieved"]');
            //Log::info($program_array['outcomes_attempted'][$i]);
            //Log::info($program_array);
          }

          $program_array['assessed_courses_count'] += 1;
        }
      }*/


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

      //old code
      /* $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(); */
      //Log::info($program_array);
      $program_array['grouped_courses'] = Course::select(DB::raw('name, code, number, 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();
      //Log::info($program_array);
      $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'));
  }
}