<?php

use Illuminate\Database\Eloquent\Collection;
use Barryvdh\DomPDF\PDF as PDF;
use Illuminate\Support\Facades\View;

class ThreeYearPlanController extends \BaseController
{

  public function create()
  {
    try {
      DB::beginTransaction();
      $yearStart = Input::get('years') + 0;
      $yearEnd = $yearStart + 3;
      $program_id = Input::get('program_id');
      DB::insert("insert into three_year_plan (`year_start`, `year_end`) values ({$yearStart}, {$yearEnd})");

      $typ = DB::table('three_year_plan')->where('year_start', '=', $yearStart)
        ->where('year_end', '=', $yearEnd)
        ->first();
      for ($i = $yearStart; $i < $yearEnd; $i++) {
        $string = 'First Semester ' . $i . '-' . ($i + 1);
        $string2 = 'Second Semester ' . $i . '-' . ($i + 1);

        $Firstsemester = DB::table('semesters')->where('name', '=', $string)->first();
        $secondSemester = DB::table('semesters')->where('name', '=', $string2)->first();
        Log::info($Firstsemester->id);
        Log::info($secondSemester->id);
        Log::info($typ->id);
        DB::insert("insert into typ_semesters (typ_id, semester_id) values ({$typ->id}, {$Firstsemester->id}), ({$typ->id}, {$secondSemester->id})");
      }
      DB::commit();
      Session::flash('status', 'success');
      Session::flash('message', '<p> Cycle created.</p>');
      return Redirect::to("three-years-plan/{$program_id}");
    } catch (Exception $e) {
      Session::flash('status', 'danger');
      Session::flash('message', "<p>Could not create cycle</p>");
      Log::info($e);
      DB::rollback();
      return Redirect::to("three-years-plan/{$program_id}");
    }
  }
  public function adminIndex()
  {
    Log::info("Entre" . json_encode($this));

    $title = "Three Years Plans";
    $programs = Program::orderBy('name', "ASC")->get();
    $typ = DB::table("three_year_plan")
      ->orderBy('year_start', "DESC")
      ->get();

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

  public function fetchSubmitted()
  {
    $typ = Input::get('typ');

    return DB::table('typ_program')


      ->where('three_year_plan_id', $typ)
      ->get();
  }

  public function manageSemesterCycle()
  {
    //   	Log::info("Entre".json_encode($this));

    $title = "Semesters Cycles";
    //     $programs = Program::get();

    return $this->adminIndex();
    //     return View::make('local.managers.admins.show-typ-program', compact('title', 'programs'));
  }

  //submit

  public function submit($program_id)
  {
    $typ_id = Input::get('typ_id');
    $rows = DB::table('typ_program')
      ->where('three_year_plan_id', $typ_id)
      ->where('program_id', $program_id)
      ->update(array(
        "is_submitted" => 1
      ));

    if (isset($rows)) {
      return "estamos ready";
    } else {
      return 500;
    }
  }

  //busca los links de public y los loadea
  public function findHTML($path_id)
  {

    $path = DB::table('paths_for_three_year_plans')->where('id', $path_id)->first();

    if (isset($path)) {

      $path = trim($path->path_to_pdf, app_path() . "/views/three_year_plan_htmls/");
      $path = trim($path, ".blade.php");
      return View::make("three_year_plan_htmls." . $path);
    }
    Session::flash('status', 'warning');
    Session::flash('message', 'Error loading typ. Please try again later.');
    return Redirect::back();
  }
  // load the Three year Plan page
  public function threeYearsReport($program_id)
  {
    $user_id = Auth::user()->id;

    $last_year = DB::table('three_year_plan')->select('year_end')
      ->orderBy('year_end', 'desc')->first();
    if (!$last_year) {
      // $last_year = date('Y');
      $last_year = 2019;
    } else {
      $last_year = $last_year->year_end;
    }

    $program = Program::find($program_id);
    Log::info($program);

    // el ID de los semestres que el usuario tiene seleccionado.
    $semesters_ids = Session::get('semesters_ids');
    // buscar informacion de los semestres seleccionados
    $semesters = Semester::whereIn('id', $semesters_ids)->get();

    $title = "Three Year Plan for " . $program->name;
    $typs = DB::table('three_year_plan')
      ->orderBy('year_start', 'asc')
      ->get();
    $criteria = Criterion::withTrashed()->orderBy('name', 'ASC')->get();
    /*   $program_id = DB::table('program_user')
      ->where('user_id', $user_id)
      ->select('program_id')
      ->get();
    $program_id = $program_id[0]->program_id;*/
    if ($program->is_graduate == 0) {
      $outcomes = DB::table('outcomes')->where('deactivation_date', '=', null)->orderBy('name', 'ASC')->get();
    } else {
      $outcomes = DB::table('outcomes')->where('deactivation_date', '=', null)->where('level', '=', 3)->orderBy('name', 'ASC')->get();
    }

    // se annadio la nueva variable
    return View::make('global.view-three-year-plan', compact('title', 'last_year', 'outcomes', 'typs', 'criteria', 'semesters', 'program_id'));
  }


  //when the user select a three year period from the left column, load the corresponding info
  public function fetchThreeYears()
  {
    $id = Input::get('id');

    $semesters = DB::table('typ_semesters')
      ->join('semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
      ->where('typ_semesters.typ_id', $id)
      ->orderBy('typ_semesters.semester_id', 'asc')
      ->get();
    $typs = DB::table('three_year_plan')
      ->where('id', $id)
      ->orderBy('year_start', 'asc')
      ->get();

    $typs  = $typs[0];
    $typs->semesters = $semesters;

    return array(
      'typ' => $typs,
    );
  }

  // add or remove a semester associated with an outcome when a user selects a checkbox
  public function update_typ_outcomes_semesters()
  {
    $outcomeSemesterArray = json_decode(Input::get('outcomeSemesterArray'), true);
    $typ_id = Input::get('typ_id');

    $user_id = auth::user()->id;
    $program_id = Input::get('program_id');


    $result = DB::table('typ_program')
      ->where('three_year_plan_id', $typ_id)
      ->where('program_id', $program_id)
      ->get();

    // create the relation if it doesnt exist
    if (count($result) == 0) {
      $typ_program_id = DB::table('typ_program')->insertGetId(
        array(
          'three_year_plan_id' => $typ_id,
          'program_id' => $program_id
        )
      );
    } else {
      $typ_program_id = $result[0]->id;
    }



    //go through array
    foreach ($outcomeSemesterArray as $outcome_semester) {

      // foreach of the 6 semesters
      foreach ($outcome_semester["semesters"] as $semester) {
        $insert = $semester[1];

        $outcome_id = explode("-sem", $semester[0])[0];

        $semester_code = explode("-sem", $semester[0])[1];
        $result = DB::table('semesters')
          ->where('code', $semester_code)
          ->select('id')
          ->get();
        if (count($result) == 0) {
          continue;
        }
        $semester_id = $result[0]->id;

        // verify if it exists
        $result = DB::table('typ_semester_outcome')
          ->where('typ_program_id', $typ_program_id)
          ->where('semester_id', $semester_id)
          ->where('outcome_id', $outcome_id)
          ->get();

        // if true, it should get inserted
        if ($insert == true) {
          // if == 0, then insert
          if (count($result) == 0) {
            $_ = DB::table('typ_semester_outcome')->insertGetId(
              array(
                'typ_program_id' => $typ_program_id,
                'semester_id' => $semester_id,
                'outcome_id' => $outcome_id
              )
            );
          }
          //if exists, then do nothing

        }
        // if false, it should get deleted
        else { //value == false

          // if == 0, it doesnt exist. we do nothing
          if (count($result) == 0) {
            //pass
          }
          //if exists, then do nothing
          else {
            DB::table('typ_semester_outcome')
              ->where('typ_program_id', $typ_program_id)
              ->where('semester_id', $semester_id)
              ->where('outcome_id', $outcome_id)
              ->delete();
          }
        }
      }
    }
    return;
  }

  // load semesters asociated with the outcomes if previously selected
  public function lookup_typ_semester_outcome()
  {
    $box_value = array();

    $info = Input::get('info');

    foreach ($info as $values) {
      $outcome_id = $values[0];
      $semester_code = $values[1];
      $program_id = $values[2];
      $box_id = $values[3];
      $typ_id = $values[4];

      //buscar id del typ
      $result = DB::table('typ_program')
        ->where('three_year_plan_id', $typ_id)
        ->where('program_id', $program_id)
        ->get();
      $typ_program_id = $result[0]->id;

      // buscar si existe el outcome_semester
      $result = DB::table('typ_semester_outcome')
        ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
        ->where('typ_program_id', $typ_program_id)
        ->where('semesters.code', $semester_code)
        ->where('outcome_id', $outcome_id)
        ->get();

      // if it doesnt exist
      if (count($result) == 0) {
        array_push($box_value, array($box_id, 0));
      } else {
        array_push($box_value, array($box_id, 1));
      }
    }
    return array(
      'box_value' => ($box_value)
    );
  }

  // save or delete Objectives associated with the outcomes and semsters
  public function section2_on_change()
  {
    $typ_id = Input::get('typ_id');
    $outcome_id = Input::get('outcome_id');
    $semester_id = Input::get('semester_id');
    $previous_objective_id = Input::get('previous_objective_id');
    $new_objective_id = Input::get('new_objective_id');
    $program_id = Input::get('program_id');

    // get program_id
    $user_id = auth::user()->id;
    Log::info(Input::all());

    // get typ_program_id
    $result = DB::table('typ_program')
      ->where('three_year_plan_id', $typ_id)
      ->where('program_id', $program_id)
      ->get();
    $typ_program_id = $result[0]->id;

    // get typ_semester_outcome_id
    $result = DB::table('typ_semester_outcome')
      ->where('typ_program_id', $typ_program_id)
      ->where('semester_id', $semester_id)
      ->where('outcome_id', $outcome_id)
      ->get();
    $typ_semester_outcome_id = $result[0]->id;

    //delete old objective relation if it exists
    if ($previous_objective_id != 'nothing_selected') {
      $result = DB::table('typ_semester_objectives')
        ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
        ->where('objective_id', $previous_objective_id)
        ->get();
      if (count($result) != 0) {
        DB::table('typ_semester_objectives')
          ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
          ->where('objective_id', $previous_objective_id)
          ->delete();
      }
    }

    //create new objective relation
    if ($new_objective_id != 'nothing_selected') {
      $result = DB::table('typ_semester_objectives')
        ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
        ->where('objective_id', $new_objective_id)
        ->get();
      if (count($result) == 0) {
        $_ = DB::table('typ_semester_objectives')->insertGetId(
          array(
            'typ_semester_outcome_id' => $typ_semester_outcome_id,
            'objective_id' => $new_objective_id
          )
        );
      }
    }
    return 'update was succes';
  }

  // load the corresponding info after associating semesters with outcomes
  public function section2_arrive()
  {
    // get program_id
    $user_id = auth::user()->id;
    $program_id = Input::get('program_id');

    // get typ_program_id
    $typ_id = Input::get('typ_id');
    $result = DB::table('typ_program')
      ->where('three_year_plan_id', $typ_id)
      ->where('program_id', $program_id)
      ->get();
    $typ_program_id = $result[0]->id;


    //search selected outcomes,semesters and get their objectives
    $selected_outcomes = DB::table('typ_semester_outcome')
      ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
      ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
      ->distinct('typ_semester_outcome.outcome_id')
      ->select('typ_semester_outcome.outcome_id')
      ->orderBy('outcomes.name', 'asc')
      ->get();

    $selected_objectives_array = array();
    $outcomes_info_array = array();
    // $objectives_info_array = array();
    $semesters_array = array();

    $info = array();
    foreach ($selected_outcomes as $outcome) {


      $outcome_id = $outcome->outcome_id;
      $result_outcomes = DB::table('outcomes')
        ->where('outcomes.id', $outcome_id)
        ->orderBy('outcomes.name', 'asc')
        ->get();

      foreach ($result_outcomes as $outcome_) {

        $result_semesters = DB::table('typ_semester_outcome')
          ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
          ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
          ->where('typ_semester_outcome.outcome_id', $outcome_->id)
          ->select('typ_semester_outcome.id', 'typ_semester_outcome.semester_id', 'semesters.code', 'semesters.name')
          ->orderBy('semesters.code')
          ->get();
        // buscar info sobre selected_semsters
        $outcome_->selected_semesters = array();
        foreach ($result_semesters as $semesters_) {
          $typ_semester_outcome_id = $semesters_->id;

          // lookup previusly selected objectives
          $result_objectives = DB::table('typ_semester_objectives')
            ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
            ->where('typ_semester_objectives.typ_semester_outcome_id', $typ_semester_outcome_id)
            ->orderBy('typ_semester_objectives.id', 'asc')
            ->get();

          $semesters_->selected_objectives = array();
          foreach ($result_objectives as $objectives_) {

            array_push($semesters_->selected_objectives, $objectives_);
          }

          // lookup available objectives
          $result_objectives = DB::table('objective_outcome')
            ->join('outcomes', 'outcomes.id', '=', 'objective_outcome.outcome_id')
            ->join('objectives', 'objectives.id', '=', 'objective_outcome.objective_id')
            ->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
            ->where('objective_program.program_id', $program_id)
            ->where('objective_outcome.outcome_id', $outcome_id)
            ->where('objective_outcome.objective_id', "<>", 0)
            ->select('objectives.id', 'objectives.text', 'outcomes.name as outcome_name')
            ->orderBy('objectives.text', 'asc')
            ->get();

          $semesters_->available_objectives = array();
          foreach ($result_objectives as $objectives_) {

            array_push($semesters_->available_objectives, $objectives_);
          }

          array_push($outcome_->selected_semesters, $semesters_);
        }
        array_push($info, $outcome_);
      }
    }
    return $info;
  }

  // load the corresponding info after associating objectives to semesters outcomes
  public function section3_arrive()
  {
    // get program_id
    $user_id = auth::user()->id;
    $program_id = Input::get('program_id');

    // get typ_program_id
    $typ_id = Input::get('typ_id');
    $result = DB::table('typ_program')
      ->where('three_year_plan_id', $typ_id)
      ->where('program_id', $program_id)
      ->get();
    $typ_program_id = $result[0]->id;


    //search selected outcomes,semesters and get their objectives
    $selected_outcomes = DB::table('typ_semester_outcome')
      ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
      ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
      ->distinct('typ_semester_outcome.outcome_id')
      ->select('typ_semester_outcome.outcome_id')
      ->orderBy('outcomes.name', 'asc')
      ->get();

    $selected_objectives_array = array();
    $outcomes_info_array = array();
    // $objectives_info_array = array();
    $semesters_array = array();

    $info = array();
    foreach ($selected_outcomes as $outcome) {


      $outcome_id = $outcome->outcome_id;
      $result_outcomes = DB::table('outcomes')
        ->where('outcomes.id', $outcome_id)
        ->get();

      foreach ($result_outcomes as $outcome_) {

        $result_semesters = DB::table('typ_semester_outcome')
          ->join('semesters', 'semesters.id', '=', 'typ_semester_outcome.semester_id')
          ->where('typ_semester_outcome.typ_program_id', $typ_program_id)
          ->where('typ_semester_outcome.outcome_id', $outcome_->id)
          ->select('typ_semester_outcome.id', 'typ_semester_outcome.semester_id', 'semesters.code', 'semesters.name')
          ->orderBy('semesters.code')
          ->get();
        // buscar info sobre selected_semsters
        $outcome_->selected_semesters = array();
        foreach ($result_semesters as $semesters_) {
          $typ_semester_outcome_id = $semesters_->id;
          $result_objectives = DB::table('typ_semester_objectives')
            ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
            ->where('typ_semester_objectives.typ_semester_outcome_id', $typ_semester_outcome_id)
            ->select('typ_semester_objectives.id as typ_semester_objectives_id', 'objectives.*', 'typ_semester_objectives.*')
            ->orderBy('typ_semester_objectives.id', 'asc')
            ->get();

          $semesters_->selected_objectives = array();
          foreach ($result_objectives as $objectives_) {
            //             $result_courses = DB::table('courses')
            //               ->where('courses.program_id', $program_id)
            //               ->select('courses.id as course_id', 'courses.name', 'courses.code', 'courses.number', 'courses.program_id')
            //               ->orderBy('courses.number', 'asc')
            //               ->groupBy('courses.name', 'courses.code')
            //               ->get();

            $result_courses = DB::table('courses')
              ->where('courses.program_id', $program_id)
              ->orWhereIn('courses.program_id', function ($q) use ($program_id) {
                $q->select('other_program_id')
                  ->from('other_programs_courses_typ')
                  ->where('program_id', $program_id);
              })
              ->select('courses.id as course_id', 'courses.name', 'courses.code', 'courses.number', 'courses.program_id')
              ->orderBy('courses.code', 'asc')
              ->orderBy('courses.number', 'asc')
              ->groupBy('courses.name', 'courses.code')
              ->get();

            #los cursos resultantes creo que deberian ser: todos los que se estan dando en mi programa

            $objectives_->available_courses = array();
            foreach ($result_courses as $courses_) {
              array_push($objectives_->available_courses, $courses_);
            }

            // search for previusly selected courses
            $typ_semester_objective_id = $objectives_->typ_semester_objectives_id;
            $result_courses = DB::table('typ_semester_courses')
              ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
              ->where('typ_semester_courses.typ_semester_objective_id', $typ_semester_objective_id)
              ->select('courses.id as course_id', 'courses.*', 'typ_semester_courses.*')
              ->orderBy('courses.name', 'asc')
              ->get();

            $objectives_->selected_courses = array();
            foreach ($result_courses as $courses_) {
              array_push($objectives_->selected_courses, $courses_);
            }

            array_push($semesters_->selected_objectives, $objectives_);
          }

          array_push($outcome_->selected_semesters, $semesters_);
        }
        array_push($info, $outcome_);
      }
    }
    return $info;
  }

  // save or delete courses associated with objectives
  public function section3_on_change()
  {
    $typ_id = Input::get('typ_id');
    $outcome_id = Input::get('outcome_id');
    $semester_id = Input::get('semester_id');
    $objective_id = Input::get('objective_id');
    $previous_course_id = Input::get('previous_course_id');
    $new_course_id = Input::get('new_course_id');

    // get program_id
    $program_id = Input::get('program_id');

    // get typ_program_id
    $result = DB::table('typ_program')
      ->where('three_year_plan_id', $typ_id)
      ->where('program_id', $program_id)
      ->get();
    $typ_program_id = $result[0]->id;

    // get typ_semester_outcome_id
    $result = DB::table('typ_semester_outcome')
      ->where('typ_program_id', $typ_program_id)
      ->where('semester_id', $semester_id)
      ->where('outcome_id', $outcome_id)
      ->get();
    $typ_semester_outcome_id = $result[0]->id;

    // get typ_semester_objective_id
    $result = DB::table('typ_semester_objectives')
      ->where('typ_semester_outcome_id', $typ_semester_outcome_id)
      ->where('objective_id', $objective_id)
      ->get();
    $typ_semester_objective_id = $result[0]->id;

    //delete old objective relation if it exists
    if ($previous_course_id != 'nothing_selected') {
      $result = DB::table('typ_semester_courses')
        ->where('typ_semester_objective_id', $typ_semester_objective_id)
        ->where('course_id', $previous_course_id)
        ->get();
      if (count($result) != 0) {
        DB::table('typ_semester_courses')
          ->where('typ_semester_objective_id', $typ_semester_objective_id)
          ->where('course_id', $previous_course_id)
          ->delete();
      }
    }

    //create new objective relation
    if ($new_course_id != 'nothing_selected') {
      $result = DB::table('typ_semester_courses')
        ->where('typ_semester_objective_id', $typ_semester_objective_id)
        ->where('course_id', $new_course_id)
        ->get();
      if (count($result) == 0) {
        $_ = DB::table('typ_semester_courses')->insertGetId(
          array(
            'typ_semester_objective_id' => $typ_semester_objective_id,
            'course_id' => $new_course_id
          )
        );
      }
    }
    return 'update succes?';
  }
  public function createAnnualPlan($program_id)
  {
    $input_id =  Input::get('typ_id');
    $current_typ = DB::select("select * from three_year_plan where id  = {$input_id}")[0];
    $count = 0;
    $yearStartPlusOne = $current_typ->year_start + 1;
    $yearStart = $current_typ->year_start;
    //Log::info($yearStart);
    //Log::info($yearStartPlusOne);
    while ($count < 3) {



      $firstSemester = DB::select("select * from semesters where name like 'First Semester {$yearStart}-{$yearStartPlusOne}'");

      $secondSemester = DB::select("select * from semesters where name like 'Second Semester {$yearStart}-{$yearStartPlusOne}'");
      if (count($firstSemester) && count($secondSemester)) {
        Log::info($firstSemester);
        Log::info($secondSemester);
        $query = DB::select("select * from annual_cycle where semester_start ={$firstSemester[0]->id} and semester_end ={$secondSemester[0]->id} ");
        if (!count($query)) {
          DB::insert("insert into annual_cycle (academic_year, semester_start, semester_end) values ('{$yearStart}-{$yearStartPlusOne}', {$firstSemester[0]->id}, {$secondSemester[0]->id})");
        }
        $an_cycle = DB::table('annual_cycle')->where('semester_start', '=', $firstSemester[0]->id)
          ->where('semester_end', '=', $secondSemester[0]->id)
          ->first();
        $query = DB::table('annual_plans')->where("annual_cycle_id", '=', $an_cycle->id)
          ->where('program_id', '=', $program_id)->first();
        if (!$query) {
          DB::insert("insert into annual_plans (`annual_cycle_id`, `program_id`) values ({$an_cycle->id}, {$program_id})");
        }
      }
      $count++;
      $yearStart++;
      $yearStartPlusOne++;
    }
    $this->createThreeYearPlanPdf($program_id, $input_id);
  }

  public function  createThreeYearPlanPdf($program_id, $typ)
  {

    $program = DB::table('programs')->where('id', $program_id)->first();
    $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();

    $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
      ->where('program_id', $program->id)
      ->first()->id;


    $typ_semesters = DB::table('semesters')
      ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
      ->where('typ_id', $three_year_plan->id)
      ->orderBy('semester_id')
      ->get();

    //Log::info("Stop 2");


    foreach ($typ_semesters as $semester) {
      $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
        ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
        ->where('typ_program_id', $typ_program_id)
        ->where('semester_id', $semester->semester_id)
        ->select('typ_semester_outcome.id as typ_id')
        ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
        ->get();

      foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
        $outcome->objectives = DB::table('typ_semester_objectives')
          ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
          ->where('typ_semester_outcome_id', $outcome->typ_id)
          ->select('typ_semester_objectives.id as typ_obj_id')
          ->addSelect('objectives.text', 'objectives.id as objective_id')
          ->get();
        foreach ($outcome->objectives as $objective) {
          $objective->courses = DB::table('typ_semester_courses')
            ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
            ->where('typ_semester_objective_id', $objective->typ_obj_id)
            ->get();
        }
      }
    }

    //html para salvar

    $submit = 1;
    $view = 0;


    //$pdf = new PDF(app('config'), app("Filesystem"), app('view'), '/storage/three_year_plan_pdfs');


    //$pdf = $pdf->loadView('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome'))
    //  ->setOrientation("landscape")

    // ->setPaper('legal', 'landscape');


    $path = app_path() . '/views/three_year_plan_htmls/' . date('d-m-Y') . '-for-' . $program->id . '-by-' . Auth::user()->id . '.blade.php';



    $name = date('d-m-Y') . '-for-' . $program->id . '-by-' . Auth::user()->id . '.blade.php';



    //is a path already there, if it is, leave the same path

    $it_exists = DB::table("paths_for_three_year_plans")
      ->where('path_to_pdf', $path)
      ->first();
    //Log::info("pero aqui?");

    if (isset($it_exists)) {
      return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view', 'app_path', 'submit'));
    }

    //else save the new path and update time


    DB::table("paths_for_three_year_plans")
      ->where('typ_program_id', $typ_program_id)
      ->where('user_id', Auth::user()->id)

      ->update(array(
        'last' => 0
      ));


    DB::table('paths_for_three_year_plans')->insert(array(
      "path_to_pdf" => $path,
      'typ_program_id' => $typ_program_id,
      'last' => 1,
      'user_id' => Auth::user()->id,
      'date_posted' => date('Y-m-d')
    ));
    return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view', 'app_path', 'submit'));
  }

  public function printPlan($program_id, $typ, $submit = NULL)
  {
    $view = 0;

    $program = DB::table('programs')->where('id', $program_id)->first();
    $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();

    $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
      ->where('program_id', $program->id)
      ->first()->id;

    $typ_semesters = DB::table('semesters')
      ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
      ->where('typ_id', $three_year_plan->id)
      ->orderBy('semester_id')
      ->get();


    foreach ($typ_semesters as $semester) {
      $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
        ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
        ->where('typ_program_id', $typ_program_id)
        ->where('semester_id', $semester->semester_id)
        ->select('typ_semester_outcome.id as typ_id')
        ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
        ->get();

      foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
        $outcome->objectives = DB::table('typ_semester_objectives')
          ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
          ->where('typ_semester_outcome_id', $outcome->typ_id)
          ->select('typ_semester_objectives.id as typ_obj_id')
          ->addSelect('objectives.text', 'objectives.id as objective_id')
          ->get();
        foreach ($outcome->objectives as $objective) {
          $objective->courses = DB::table('typ_semester_courses')
            ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
            ->where('typ_semester_objective_id', $objective->typ_obj_id)
            ->get();
        }
      }
    }
    //Log::info($typ_semester_outcome);


    //TEMP

    //$app_path = app_path() . '/jsPDF-master/src/jspdf.js';
    //$submit = 1;
    return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view', 'app_path', 'submit'));
  }

  public function viewPlan($program_id, $typ)
  {
    $view = 1;
    $program = DB::table('programs')->where('id', $program_id)->first();
    $three_year_plan = DB::table('three_year_plan')->where('id', $typ)->first();

    $typ_program_id = DB::table('typ_program')->where('three_year_plan_id', $three_year_plan->id)
      ->where('program_id', $program->id)
      ->first()->id;

    $typ_semesters = DB::table('semesters')
      ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
      ->where('typ_id', $three_year_plan->id)
      ->orderBy('semester_id')
      ->get();


    foreach ($typ_semesters as $semester) {
      $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
        ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
        ->where('typ_program_id', $typ_program_id)
        ->where('semester_id', $semester->semester_id)
        ->select('typ_semester_outcome.id as typ_id')
        ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
        ->get();

      foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
        $outcome->objectives = DB::table('typ_semester_objectives')
          ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
          ->where('typ_semester_outcome_id', $outcome->typ_id)
          ->select('typ_semester_objectives.id as typ_obj_id')
          ->addSelect('objectives.text', 'objectives.id as objective_id')
          ->get();
        foreach ($outcome->objectives as $objective) {
          $objective->courses = DB::table('typ_semester_courses')
            ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
            ->where('typ_semester_objective_id', $objective->typ_obj_id)
            ->get();
        }
      }
    }
    Log::info($typ_semester_outcome);


    return View::make('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome', 'view'));
  }

  public function showPDFs($program_id)
  {

    $role = Auth::user()->role;
    switch ($role) {
      case 1:
      case 2:
      case 3:
        $last = [1, 0];
        break;

      case 4:
        $last = [1];

        break;
      default:
        App::abort('404');
    }


    $title = "Three Year Plans for " . Program::findOrFail($program_id)->name;
    $paths_with_users = DB::table('paths_for_three_year_plans')
      ->join('typ_program', 'typ_program.id', '=', 'paths_for_three_year_plans.typ_program_id')
      ->join('three_year_plan', 'typ_program.three_year_plan_id', '=', 'three_year_plan.id')
      ->join('users', 'users.id', '=', 'paths_for_three_year_plans.user_id')

      ->whereIn('last', $last)
      ->where('typ_program.program_id', $program_id)
      ->orderBy('date_posted', 'desc')
      ->select('paths_for_three_year_plans.path_to_pdf', 'users.*', 'date_posted', 'three_year_plan.*', 'paths_for_three_year_plans.last', 'paths_for_three_year_plans.id as path_id')
      ->get();

    return View::make('local.managers.shared.new_view_three_year_plans', compact('paths_with_users', 'title', 'last', 'program_id'));
  }

  /*public function downloadPDF($download, $path_id)
  {
    $pdf = new PDF(app('config'), app("Filesystem"), app('view'), '/storage/three_year_plan_pdfs');

    $queryToPath = DB::table('paths_for_three_year_plans')
      ->join('typ_program', 'typ_program.id', '=', 'paths_for_three_year_plans.typ_program_id')
      ->where('paths_for_three_year_plans.id', $path_id)
      ->first();

    //Log::info("ERES TU? creo que no");

    // $annualPlan = AnnualPlan::findOrFail($queryToPath->annual_plan_id);


    $program = Program::findOrFail($queryToPath->program_id);


    $typ_program_id = $queryToPath->typ_program_id;

    $three_year_plan = DB::table('three_year_plan')
      ->where('id', $queryToPath->three_year_plan_id)
      ->first();

    $typ_semesters = DB::table('semesters')
      ->join('typ_semesters', 'semesters.id', '=', 'typ_semesters.semester_id')
      ->where('typ_id', $queryToPath->three_year_plan_id)
      ->orderBy('semester_id')
      ->get();


    foreach ($typ_semesters as $semester) {
      $typ_semester_outcome[$semester->semester_id] = DB::table('typ_semester_outcome')
        ->join('outcomes', 'outcomes.id', '=', 'typ_semester_outcome.outcome_id')
        ->where('typ_program_id', $typ_program_id)
        ->where('semester_id', $semester->semester_id)
        ->select('typ_semester_outcome.id as typ_id')
        ->addSelect('outcomes.id as outcome_id', 'outcomes.name')
        ->get();

      foreach ($typ_semester_outcome[$semester->semester_id] as $outcome) {
        $outcome->objectives = DB::table('typ_semester_objectives')
          ->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
          ->where('typ_semester_outcome_id', $outcome->typ_id)
          ->select('typ_semester_objectives.id as typ_obj_id')
          ->addSelect('objectives.text', 'objectives.id as objective_id')
          ->get();
        foreach ($outcome->objectives as $objective) {
          $objective->courses = DB::table('typ_semester_courses')
            ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
            ->where('typ_semester_objective_id', $objective->typ_obj_id)
            ->get();
        }
      }
    }
    //Log::info("ERES TU?");

    $pdf = $pdf->loadView('global.print_three_year_plan', compact('typ_semesters', 'program', 'three_year_plan', 'typ_semesters', 'typ_semester_outcome'))
      ->setOrientation("landscape")

      ->setPaper('legal', 'landscape');
    if ($download == "download")
      return $pdf->download(basename($queryToPath->path_to_pdf));

    else
      return $pdf->stream(basename($queryToPath->path_to_pdf));
  }
  */
}