<?php

class AnnualPlansController extends \BaseController {

  /**
   * Checks whether a user has permission to view a page in this controller
   *
   * @var User $user        Authenticated user
   */
  private function userHasAccess($program_id)
  {
    $user = Auth::user();

    switch ($user->role) {
      case '3':
        $programs = $user->programs->lists('id');

        // If program does not belong to user, show 403
        if(!in_array($program_id, $programs))
          return false;

        break;

      case '2':
        $programs = Program::where('school_id', $user->school_id)->lists('id');

        // If program is not in user's school, show 403
        if(!in_array($program_id, $programs))
          return false;

        break;
      case '4':
        return false;
    }

    return true;
  }

  /**
   * Lists annual plans by year and program
   * @var string $title     Title for page
   * @var Program $programs Collection of programs user has access to
   * @var User $user        Authenticated user
   * @var Quinquennium $quinquenniums      All current or past quinquenniums
   * @var Quinquennium $current_quinquennium      Current quinquennium
   */
  public function index()
  {
    $title = 'Annual Plans';
    $user = Auth::user();
    $quinquenniums = Quinquennium::where('start_date','<=', date('Y-m-d'))->get();
    $current_quinquennium = Quinquennium::
      where('start_date', '<=', date('Y-m-d'))
      ->where('end_date', '>=', date('Y-m-d'))
      ->first();

    switch ($user->role) {
      case '1':
        $programs = Program::all();
        break;
      case '2':
        $programs = Program::where('school_id', $user->school_id)->get();
        break;
      case 3:
        $programs = $user->programs;
        break;
      default:
        App::abort('404');
        break;
    }

    return View::make('local.managers.shared.index_annual_plans', compact('title', 'quinquenniums', 'programs', 'current_quinquennium'));
  }

  /**
   * Page to create a new plan for the current quinquennium
   * @var string $title     Title for page
   * @var Program $programs Collection of programs user has access to
   * @var Outcome $outcomes      List of outcomes ordered by name
   * @var User $user      Currently logged user
   * @var Course $courses      Courses for a particular program
   */
  public function create(Program $program)
  {
    $title = 'New Annual Plan for '.$program->name;
    $user = Auth::user();
    $outcomes = Outcome::orderBy('name')->get();

    $current_quinquennium = Quinquennium::
      where('start_date', '<=', date('Y-m-d'))
      ->where('end_date', '>=', date('Y-m-d'))
      ->first();

    $courses = Course::
      select('id', 'code', 'number', 'name')
      ->where('program_id', $program->id)
      ->groupBy('name')
      ->orderBy('code','ASC')
      ->orderBy('number','ASC')
      ->orderBy('name','ASC')
      ->get();

    // Check if user can create a plan
    if(!$this->userHasAccess($program->id))
    {
      return View::make('global.403');
    }

    return View::make('local.managers.shared.create_annual_plan', compact('title', 'program', 'current_quinquennium', 'outcomes', 'courses'));
  }

}
