<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

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;
  }

  public function viewAllPlans($program_id)
  {
    $title = "Annual Plans";
    $annual_plans = DB::select("select semester_start, semester_end, program_id, annual_plans.id, academic_year from annual_plans, annual_cycle   where annual_plans.annual_cycle_id = annual_cycle.id and program_id ={$program_id} order by id desc");


    return View::make('local.managers.sCoords.view-annual-plans', compact('title', 'program_id', 'annual_plans'));
  }

  /**
   * 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 postReport()
  {
    $program_id = Input::get('program_id') + 0;
    $realized = Input::get('realized');
    $logrado = Input::get('logrado');
    $continued = Input::get('continued');
    $semester = Input::get('semester');
    $typ_objective = Input::get('typ_objective');
    $transformative = Input::get('transformative');
    $comments = Input::get("comments");
    $annual_id = Input::get("annual_id") + 0;
    $id = Auth::user()['id'];

    for ($i = 0; $i < count($semester); $i++) {
      $real = $realized[$i] + 0;
      $logr = $logrado[$i] + 0;
      $cont = $continued[$i] + 0;
      $obj = $typ_objective[$i] + 0;
      $trans = $transformative[$i] + 0;
      $sem = $semester[$i] + 0;
      $comment = $comments[$i];
      //if it was continued
      if ($real == 1) $real = 2;
      else $real = 1;
      if ($cont == 1) $cont = $sem + 1;
      else $cont = "NULL";
      $annual_trans_id = DB::select("select id from annual_plan_transformative where annual_plan_id ={$annual_id} and trans_id = {$trans} and typ_semester_objective_id = {$obj}")[0]->id;


      $queryUpdate = DB::select("select * from annual_report_transformative where annual_trans_id = {$annual_trans_id}");
      if (!count($queryUpdate)) {
        DB::insert("insert into annual_report_transformative (accomplished, cycle_of_life, semester_used, semester_continue, annual_trans_id, supervised_coordinator_id, comments) values ({$logr},{$real},{$sem},{$cont},{$annual_trans_id}, {$id}, '{$comment}')");
      } else {
        DB::update("update annual_report_transformative set accomplished = {$logr}, cycle_of_life ={$real}, semester_used ={$sem}, semester_continue={$cont}, supervised_coordinator_id={$id}, comments = '{$comment}' where annual_trans_id ={$annual_trans_id}");
      }
    }
    return;
  }
  /*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'));
  }*/
  public function adminIndex($school_id)
  {
    $title = "Annual Plans";
    $programs = Program::where("school_id", $school_id)->get();

    return View::make('local.managers.admins.appraisal-program', compact('title', 'programs'));
  }

  public function showPlan($program_id, $typ_id = null)
  {
    $title = "Annual Plans";
    //$typ_parts = DB::select("select * from typ_parts");

//     $current_typ = DB::select("select * from three_year_plan where year_start <=" . date('Y') . " and year_end >=" . date('Y'))[0];
    $current_typ_arr = DB::select("select * from three_year_plan where year_start <=" . date('Y') . " and year_end >=" . date('Y'));
// 	var_dump($current_typ);exit();
	if(!empty($current_typ_arr))$current_typ=$current_typ_arr[0];else {$current_typ= new stdClass();$current_typ->id=0;}


    $program = Program::where('id', '=', $program_id)->first();
    $annual_plans = DB::select("select academic_year, semester_start, semester_end, program_id, annual_plans.id as id from annual_plans, annual_cycle where program_id = {$program_id} and annual_cycle.id = annual_plans.annual_cycle_id order by annual_plans.id desc ");
    Log::info($annual_plans);
    $outcomes = array();
    $allSemesterOrder = array();
    /*foreach ($annual_plans as $an_plan) {
      Log::info($an_plan->id);
      $outcomes[$an_plan->id]["first"] = DB::select("select * from outcomes where id in (select outcome_id from typ_semester_outcome where semester_id = {$an_plan->semester_start} and typ_program_id in (select id from typ_program where program_id ={$an_plan->program_id} and three_year_plan_id ={$current_typ->id} ))");
      $allSemesterOrder[$an_plan->id]["first"] = DB::select("select * from semesters where id = {$an_plan->semester_start}")[0];
      $outcomes[$an_plan->id]["second"] = DB::select("select * from outcomes where id in (select outcome_id from typ_semester_outcome where semester_id ={$an_plan->semester_end} and typ_program_id in (select id from typ_program where program_id ={$an_plan->program_id} and three_year_plan_id = {$current_typ->id} ))");
      $allSemesterOrder[$an_plan->id]["second"] = DB::select("select * from semesters where id = {$an_plan->semester_end}")[0];
    }*/
    $alltyp = DB::select('select * from three_year_plan order by id desc');
    $current_typ = $current_typ->id;
    return View::make('local.managers.sCoords.annual-plans', compact('title', 'annual_plans', 'current_typ', 'program', 'outcomes', 'allSemesterOrder', 'alltyp'));
  }

  public function fetchInfo()
  {
    Log::info(Input::get('id'));
    $an_plan = DB::table("annual_plans")
      ->join('annual_cycle', 'annual_cycle.id', '=', 'annual_plans.annual_cycle_id')
      ->where('annual_plans.id', '=', Input::get('id'))
      ->select('annual_plans.id as id', 'program_id', 'academic_year', 'semester_start', 'semester_end')
      ->first();

    $years = explode('-', $an_plan->academic_year);
    $current_typ_program = DB::select("select * from typ_program where program_id = {$an_plan->program_id} and three_year_plan_id in (select id from three_year_plan where year_start <= {$years[0]} and year_end>= {$years[1]})")[0];


    $json_to_send = [];
    $json_to_send["annual_plans"] = $an_plan;
    $json_to_send["outcomes"]["first"] = DB::select("select * from outcomes where id in (select outcome_id from typ_semester_outcome where semester_id = {$an_plan->semester_start} and typ_program_id = {$current_typ_program->id})");
    $json_to_send["allSemesterOrder"]["first"] = DB::select("select * from semesters where id = {$an_plan->semester_start}")[0];
    $json_to_send["outcomes"]["second"] = DB::select("select * from outcomes where id in (select outcome_id from typ_semester_outcome where semester_id ={$an_plan->semester_end} and typ_program_id  = {$current_typ_program->id})");
    $json_to_send["allSemesterOrder"]["second"] = DB::select("select * from semesters where id = {$an_plan->semester_end}")[0];
    return json_encode($json_to_send);
  }
  /**
   * 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 fetchTYP($program_id)
  {
    $typ_info = array();
    $semester = DB::select("select * from semesters where id=?", array(Input::get('semester')))[0];


    //$typ_info['courses'] = DB::select("select * from courses where id in (select course_id from typ_semester_courses where typ_semester_outcome_id in (select id from typ_semester_outcome where semester_id ={$semester->id} and typ_program_id in (select id from typ_program where program_id ={$program_id} )and outcome_id = ?))", array(Input::get('id')));
    //foreach ($typ_info['courses'] as $course) {
    //  $typ_info['courses_objective'][$course->id] = DB::select("select * from objectives where id in (select objective_id from typ_semester_objectives where typ_semester_course_id in (select id from typ_semester_courses where typ_semester_outcome_id in (select id from typ_semester_outcome where semester_id = {$semester->id} and typ_program_id in (select id from typ_program where program_id ={$program_id}))and course_id ={$course->id}))");
    //  foreach ($typ_info['courses_objective'][$course->id] as $objective) {
    //    $typ_info['criteria'][$objective->id] = DB::select("select * from criteria where id in (select criterion_id from criterion_objective_outcome where outcome_id = ? and objective_id = {$objective->id})", array(Input::get('id')));
    //  }
    //}
    //$typ_info['courses'] = DB::select("select * from courses where id in (select course_id from typ_semester_courses where typ_semester_objective_id in (select id from typ_semester_objectives where typ_semester_outcome_id in (select id from type_semester_outcome where semester_id ={$semester->id} and typ_program_id in (select id from typ_program where program_id ={$program_id} )and outcome_id = ?)))", array(Input::get('id')));
    //$typ_info['unique_objective'] = DB::select("select * from objectives where id in (select distinct objective_id from typ_semester_objectives where  typ_semester_outcome_id in (select id from typ_semester_outcome where semester_id = {$semester->id} and outcome_id =? and typ_program_id in (select id from typ_program where program_id ={$program_id})))", array(Input::get('id')));
    //foreach ($typ_info['unique_objective'] as $objective) {
    //  $typ_info['courses_objective'][$objective->id] = DB::select("select * from courses where id in (SELECT course_id from typ_semester_courses where typ_semester_outcome_id in (select id from typ_semester_outcome where semester_id={$semester->id} and outcome_id =? and typ_program_id in (select id from typ_program where program_id = {$program_id})) and id in (select typ_semester_course_id from typ_semester_objectives where objective_id  = {$objective->id}))", array(Input::get('id')));
    //  $typ_info['criteria'][$objective->id] = DB::select("select * from criteria where id in (select criterion_id from criterion_objective_outcome where outcome_id = ? and objective_id = {$objective->id})", array(Input::get('id')));
    //}
    $annual_plan = DB::select("select annual_plans.id from annual_plans, annual_cycle where annual_plans.annual_cycle_id = annual_cycle.id and (semester_start = {$semester->id} or semester_end ={$semester->id}) and program_id ={$program_id}")[0];
    $typ_info['objectives'] = DB::select("select * from objectives where id in (select objective_id from typ_semester_objectives where typ_semester_outcome_id in(select id from typ_semester_outcome where outcome_id = ? and semester_id = {$semester->id} and typ_program_id in (select id from typ_program where program_id ={$program_id})))", array(Input::get('id')));
    $typ_info['transformative_actions'] = DB::select("select * from transformative_actions where by_professor =0 and is_custom=0");
    foreach ($typ_info['objectives'] as $objective) {

      $typ_info['courses'][$objective->id] = DB::select("select c.id, c.number, c.name, c.code, typ.id typ_course_id from courses c, typ_semester_courses typ, (select course_id, id  from typ_semester_courses where typ_semester_objective_id in (select id from typ_semester_objectives where objective_id ={$objective->id} and typ_semester_outcome_id in (select id from typ_semester_outcome where outcome_id =? and semester_id ={$semester->id} and typ_program_id in(select id from typ_program where program_id ={$program_id})))) rel where typ.course_id =c.id and rel.course_id = c.id and typ.id = rel.id ", array(Input::get('id')));

      $typ_info['criteria'][$objective->id] = DB::select("select * from criteria where deleted_at IS NULL and id in (select criterion_id from criterion_objective_outcome where outcome_id = ? and objective_id = {$objective->id})", array(Input::get('id')));
      Log::info($typ_info['criteria'][$objective->id]);
      $typ_info['typ_objective_id'][$objective->id] = DB::select("select id from typ_semester_objectives where objective_id = {$objective->id} and typ_semester_outcome_id in (select id from typ_semester_outcome where outcome_id =? and semester_id ={$semester->id} and typ_program_id in (select id from typ_program where program_id ={$program_id}))", array(Input::get('id')));
      Log::info($typ_info['typ_objective_id'][$objective->id]);
      $typ_info['typ_objective_id'][$objective->id] = $typ_info['typ_objective_id'][$objective->id][0];

      Log::info($typ_info['typ_objective_id'][$objective->id]->id);
      Log::info($annual_plan->id);
      //  $typ_info['annual_plans_transformative'][$objective->id] = DB::select(
      //    "select trans_id from annual_plan_transformative
      //     where annual_plan_id={$annual_plan->id} 
      //     and typ_semester_objective_id ={$typ_info['typ_objective_id'][$objective->id]->id}"
      //  );
      foreach ($typ_info['courses'][$objective->id] as $course) {
        $typ_info['selected_criteria'][$objective->id][$course->typ_course_id] = DB::select("select criteria_id, typ_semester_course_id typ_course_id from annual_plan_objective where annual_plan_id ={$annual_plan->id} and typ_semester_course_id ={$course->typ_course_id} ");
        $typ_info['annual_plans_transformative'][$objective->id][$course->typ_course_id] = DB::table('annual_plan_transformative')
          ->select('trans_id')
          ->where('annual_plan_id', $annual_plan->id)
          ->where('typ_semester_course_id', $course->typ_course_id)
          ->get();
        $typ_info['custom_transformative'][$objective->id][$course->id] = DB::table('transformative_actions')
          ->join('ta_course', 'ta_course.ta_id', '=', 'transformative_actions.id')
          ->join('transformative_objective', 'transformative_objective.ta_id', '=', 'transformative_actions.id')
          ->where('is_custom', 1)
          ->where('by_professor', 0)
          ->where('ta_course.course_number', $course->number)
          ->where('ta_course.course_code', $course->code)
          ->where('transformative_objective.objective_id', $objective->id)
          ->get();
      }
    }
    $typ_info['annual_plan'] = $annual_plan;
    Log::info($typ_info);

    return json_encode($typ_info);
  }
  public function deleteTA()
  {
    $annual_id = Input::get('annual_id') + 0;
    $typ_id = Input::get('typ_id') + 0;
    $trans_id = Input::get('TA_id') + 0;

    DB::delete("delete from `annual_plan_transformative` where annual_plan_id ={$annual_id} and typ_semester_course_id ={$typ_id} and trans_id={$trans_id}");
    return;
  }

  public function postTA()
  {
    $annual_id = Input::get('annual_id') + 0;
    $TA_id = Input::get('TA_id') + 0;
    $typ_id = Input::get('typ_course_id') + 0;
    $old_ta = Input::get('old_ta') + 0;
    $id = Auth::user()['id'];
    $date = date('Y-m-d');
    $date = strtotime($date);


    $query = DB::select("select * from annual_plan_transformative where annual_plan_id ={$annual_id} and trans_id ={$old_ta} and typ_semester_course_id ={$typ_id}");

    if (!count($query)) {
      DB::insert("insert into `annual_plan_transformative` (`annual_plan_id`,`trans_id`, `typ_semester_course_id`, `proposing_coordinator_id`, `proposed_date`) values({$annual_id},{$TA_id},{$typ_id}, {$id}, now() ) ");
      return "Insert Successful";
    } else {
      if ($TA_id == 0) {
        DB::delete("delete from annual_plan_transformative where trans_id={$TA_id} and annual_plan_id ={$annual_id} and typ_semester_course_id ={$typ_id}");
        return "deleted";
      } else {
        DB::update("update annual_plan_transformative set trans_id = {$TA_id}, proposing_coordinator_id = {$id}, proposed_date = now() where trans_id={$old_ta} and annual_plan_id ={$annual_id} and typ_semester_course_id ={$typ_id}");
        return "updated";
      }
    }
  }
  public function transformativeReport()
  {
    $json_to_send = array();
    $annual_id = Input::get('an_id');
    $transformative_action_info = DB::select("select * from transformative_actions where id in (select distinct trans_id from annual_plan_transformative where annual_plan_id = {$annual_id}) ");
    foreach ($transformative_action_info as $trans) {
      $json_to_send['Trans_act'][$trans->id] = $trans;
      $json_to_send['outcomes'][$trans->id] = DB::select("select * from outcomes where id in (select outcome_id from typ_semester_outcome where id in(select typ_semester_outcome_id from typ_semester_objectives where id in (select typ_semester_objective_id from annual_plan_transformative where trans_id ={$trans->id} and annual_plan_id = {$annual_id})))");

      foreach ($json_to_send['outcomes'][$trans->id] as $outcome) {
        $json_to_send['typ_objective'][$outcome->id][$trans->id] = DB::select("select * from typ_semester_objectives where typ_semester_outcome_id in (select id from typ_semester_outcome where outcome_id ={$outcome->id}) and id in (select typ_semester_objective_id from annual_plan_transformative where annual_plan_id ={$annual_id} and trans_id = {$trans->id})");
        foreach ($json_to_send['typ_objective'][$outcome->id][$trans->id] as $typ) {
          $json_to_send['objective'][$outcome->id][$typ->id][$trans->id] = DB::select("select * from objectives where id = {$typ->objective_id}")[0];
          $json_to_send['coordinator'][$outcome->id][$typ->id][$trans->id] = DB::select("select * from users where id in (select proposing_coordinator_id from annual_plan_transformative where typ_semester_objective_id = {$typ->id} and annual_plan_id = {$annual_id} and trans_id = {$trans->id})")[0];
          $json_to_send['proposed_date'][$outcome->id][$typ->id][$trans->id] = DB::select("select proposed_date from annual_plan_transformative where typ_semester_objective_id = {$typ->id} and annual_plan_id = {$annual_id} and trans_id = {$trans->id} ")[0];
          $json_to_send['all_info_report'][$outcome->id][$typ->id][$trans->id] = DB::select("select art.accomplished, art.cycle_of_life, semesters.name, art.semester_continue,  art.annual_trans_id, art.supervised_coordinator_id  from annual_report_transformative art, (select id from annual_plan_transformative where typ_semester_objective_id = {$typ->id} and annual_plan_id = {$annual_id} and trans_id = {$trans->id} ) apt, semesters  where art.annual_trans_id = apt.id and semesters.id = art.semester_used")[0];
        }
      }


      if ($trans->user_id) {
        $json_to_send['suggested'][$trans->id] = DB::select("select * from users where id = {$trans->user_id}");
      }
    }
    return json_encode($json_to_send);
  }

  public function deleteCriteria()
  {
    $criteria = Input::get('criteria') + 0;
    $typ_course_id = Input::get('typ_course_id') + 0;
    $annual_plan = Input::get('annual_plan') + 0;
    $old_criteria = Input::get('old_criteria') + 0;
    $message = '';

    DB::delete("delete from annual_plan_objective where annual_plan_id ={$annual_plan} and typ_semester_course_id ={$typ_course_id} and criteria_id={$criteria}");
    return;
  }

  public function fetchAllTables()
  {
    $annual_plan = array();
    $an_id = Input::get('id');
    $program_id = Input::get('program_id');

    $an_semesters = DB::select("select semester_start, semester_end, program_id from annual_plans, annual_cycle where annual_plans.annual_cycle_id = annual_cycle.id and annual_plans.id ={$an_id} and program_id ={$program_id}")[0];

    $annual_plan['first']['outcomes'] = DB::select("select * from outcomes where id in (select outcome_id from typ_semester_outcome where semester_id = {$an_semesters->semester_start} and typ_program_id in(select id from typ_program where program_id ={$an_semesters->program_id}))");

    $annual_plan['second']['outcomes'] = DB::select("select * from outcomes where id in (select outcome_id from typ_semester_outcome where semester_id = {$an_semesters->semester_end} and typ_program_id in(select id from typ_program where program_id ={$an_semesters->program_id}))");

    $annual_plan['first']['semester'] = array('id' => $an_semesters->semester_start);
    $annual_plan['second']['semester'] = array('id' => $an_semesters->semester_end);
    foreach ($annual_plan['first']['outcomes'] as $outcomes) {

      $annual_plan['first']['objectives'][$outcomes->id] = DB::select("select * from objectives where id in (select objective_id from typ_semester_objectives where typ_semester_outcome_id in(select id from typ_semester_outcome where outcome_id = {$outcomes->id} and semester_id = {$an_semesters->semester_start} and typ_program_id in (select id from typ_program where program_id ={$an_semesters->program_id})))");
      Log::info($outcomes->id);
      Log::info($annual_plan['first']['objectives'][$outcomes->id]);
      foreach ($annual_plan['first']['objectives'][$outcomes->id] as $objective) {
        $annual_plan['first']['typ_objective_id'][$objective->id] = DB::select("select id from typ_semester_objectives where objective_id = {$objective->id} and  typ_semester_outcome_id in (select id from typ_semester_outcome where outcome_id ={$outcomes->id} and semester_id ={$an_semesters->semester_start} and typ_program_id in(select id from typ_program where program_id ={$an_semesters->program_id})) ")[0];
        $annual_plan['first']['courses'][$objective->id] = DB::select("select typ.id typ_course_id, typ.course_id, c.code, c.number, c.name from (select course_id, id from typ_semester_courses where typ_semester_objective_id = {$annual_plan['first']['typ_objective_id'][$objective->id]->id}) typ, courses c where c.id = typ.course_id");
        foreach ($annual_plan['first']['courses'][$objective->id] as $course) {
          $annual_plan['first']['criteria'][$objective->id][$course->typ_course_id] = DB::select("select * from criteria where  id in(select criteria_id from annual_plan_objective where annual_plan_id = {$an_id} and typ_semester_course_id = {$course->typ_course_id} )");
          $annual_plan['first']['trans_actions'][$objective->id][$course->typ_course_id] = DB::select("select * from transformative_actions where id in (select trans_id from annual_plan_transformative where typ_semester_course_id = {$course->typ_course_id})");
        }

        Log::info($objective->id);
        /*$annual_plan['first']['trans_actions'][$objective->id] = DB::select("select * from transformative_actions where id in (select trans_id from annual_plan_transformative where typ_semester_objective_id ={$annual_plan['first']['typ_objective_id'][$objective->id]->id})");
        foreach ($annual_plan['first']['trans_actions'][$objective->id] as $trans) {
          Log::info($annual_plan['first']['typ_objective_id'][$objective->id]->id);
          Log::info($trans->id);
          Log::info($an_id);
          $annual_plan['fill_info'][$annual_plan['first']['typ_objective_id'][$objective->id]->id][$trans->id] = DB::select("select * from annual_report_transformative where annual_trans_id in (select id from annual_plan_transformative where trans_id = {$trans->id} and typ_semester_objective_id ={$annual_plan['first']['typ_objective_id'][$objective->id]->id} and annual_plan_id = {$an_id} )");
          if (count($annual_plan['fill_info'][$annual_plan['first']['typ_objective_id'][$objective->id]->id][$trans->id])) {
            Log::info("entró");
            $annual_plan['fill_info'][$annual_plan['first']['typ_objective_id'][$objective->id]->id][$trans->id] =  $annual_plan['fill_info'][$annual_plan['first']['typ_objective_id'][$objective->id]->id][$trans->id][0];
          }
        }*/
      }
    }
    foreach ($annual_plan['second']['outcomes'] as $outcomes) {
      $annual_plan['second']['objectives'][$outcomes->id] = DB::select("select * from objectives where id in (select objective_id from typ_semester_objectives where typ_semester_outcome_id in(select id from typ_semester_outcome where outcome_id = {$outcomes->id} and semester_id = {$an_semesters->semester_end} and typ_program_id in (select id from typ_program where program_id ={$an_semesters->program_id})))");
      foreach ($annual_plan['second']['objectives'][$outcomes->id] as $objective) {
        $annual_plan['second']['typ_objective_id'][$objective->id] = DB::select("select id from typ_semester_objectives where objective_id = {$objective->id} and  typ_semester_outcome_id in (select id from typ_semester_outcome where outcome_id ={$outcomes->id} and semester_id ={$an_semesters->semester_end} and typ_program_id in(select id from typ_program where program_id ={$an_semesters->program_id})) ")[0];

        $annual_plan['second']['courses'][$objective->id] = DB::select("select typ.id typ_course_id, typ.course_id, c.code, c.number, c.name from (select course_id, id from typ_semester_courses where typ_semester_objective_id = {$annual_plan['second']['typ_objective_id'][$objective->id]->id}) typ, courses c where c.id = typ.course_id");
        foreach ($annual_plan['second']['courses'][$objective->id] as $course) {
          $annual_plan['second']['criteria'][$objective->id][$course->course_id] = DB::select("select * from criteria where id in(select criteria_id from annual_plan_objective where annual_plan_id = {$an_id} and typ_semester_course_id ={$course->typ_course_id})");
          $annual_plan['second']['trans_actions'][$objective->id][$course->typ_course_id] = DB::select("select * from transformative_actions where id in (select trans_id from annual_plan_transformative where typ_semester_course_id = {$course->typ_course_id})");
        }
        /*
        $annual_plan['second']['trans_actions'][$objective->id] = DB::select("select * from transformative_actions where id in (select trans_id from annual_plan_transformative where typ_semester_objective_id ={$annual_plan['second']['typ_objective_id'][$objective->id]->id})");
        Log::info($annual_plan['second']['typ_objective_id'][$objective->id]->id);
        foreach ($annual_plan['second']['trans_actions'][$objective->id] as $trans) {

          $annual_plan['fill_info'][$annual_plan['second']['typ_objective_id'][$objective->id]->id][$trans->id] = DB::select("select * from annual_report_transformative where annual_trans_id in (select id from annual_plan_transformative where trans_id = {$trans->id} and typ_semester_objective_id ={$annual_plan['second']['typ_objective_id'][$objective->id]->id} and annual_plan_id = {$an_id} )");
          Log::info("lol");
          if (count($annual_plan['fill_info'][$annual_plan['second']['typ_objective_id'][$objective->id]->id][$trans->id])) {
            Log::info("entró");
            $annual_plan['fill_info'][$annual_plan['second']['typ_objective_id'][$objective->id]->id][$trans->id] =  $annual_plan['fill_info'][$annual_plan['second']['typ_objective_id'][$objective->id]->id][$trans->id][0];
          }
        }*/
      }
    }

    return json_encode($annual_plan);
  }

  public function postAnnualPlan()
  {
    $criteria = Input::get('criteria') + 0;
    $typ_course_id = Input::get('typ_course_id') + 0;
    $annual_plan = Input::get('annual_plan') + 0;
    $old_criteria = Input::get('old_criteria') + 0;
    $message = '';
    $query = DB::select("select * from annual_plan_objective where annual_plan_id ={$annual_plan} and typ_semester_course_id = {$typ_course_id} and criteria_id ={$old_criteria}");
    if (!count($query)) {
      $query = DB::select("select * from annual_plan_objective where annual_plan_id ={$annual_plan} and typ_semester_course_id = {$typ_course_id} and criteria_id ={$criteria}");

      if (!count($query)) {

        DB::insert("insert into `annual_plan_objective` (`annual_plan_id`, `typ_semester_course_id`, `criteria_id`) values ({$annual_plan}, {$typ_course_id}, {$criteria})");
        $message = "inserting was a success";
      } else {
        $message = "Duplicate entry, please choose another criteria.";
      }
    } else {
      $query = DB::select("select * from annual_plan_objective where annual_plan_id ={$annual_plan} and typ_semester_course_id = {$typ_course_id} and criteria_id ={$criteria}");

      if (!count($query)) {
        DB::update("update `annual_plan_objective` set criteria_id = {$criteria} where annual_plan_id = {$annual_plan} and typ_semester_course_id ={$typ_course_id} and criteria_id = {$old_criteria} ");
        $message = "Updating was a success";
      } else {
        $message = "Duplicate entry, please choose another criteria.";
      }
    }
    return $message;
  }
  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'));
  }
}
