<?php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Objective extends Eloquent
{
    use SoftDeletingTrait;
    protected $fillable = array('text', 'outcome_id',  'active');
    protected $table = 'objectives';
    protected $appends = array("grouped_annual_course", 'programs');

    /**
     * Return the program that the objective belongs to
     *
     * @return Illuminate\Database\Eloquent\Model
     */
    public function getProgramsAttribute()
    {
        return Program::join('objective_program', 'programs.id', '=', 'objective_program.program_id')
            ->where('objective_program.objective_id', $this->id)
            ->get();
    }

    //paired_outcome

    public function getPairedOutcomeAttribute()
    {
        return Outcome::join('typ_semester_outcome', 'typ_semester_outcome.outcome_id', '=', 'outcomes.id')
            ->join('typ_semester_objectives', 'typ_semester_objectives.typ_semester_outcome_id', '=', 'typ_semester_outcome.id')
            ->where('typ_semester_objectives.id', $this->typ_semester_objective_id)
            ->select('typ_semester_outcome.id as typ_semester_outcome_id', 'outcomes.*')
            ->first();
    }


    //public function pairedCriteria(){

    //    return Criterion::
    //}


    public function getGroupedAnnualCourseAttribute()
    {
        if (isset($this->typ_semester_objective_id)) {
            /*$course_codes = DB::table('typ_semester_courses')
                ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
                ->where('typ_semester_objective_id', $this->typ_semester_objective_id)
                ->where('courses.program_id', $this->program_id)
                ->select('courses.code', 'courses.number', 'courses.id as course_id', 'typ_semester_courses.id as typ_semester_course_id', DB::raw("'{$this->semester_id}' as semester_id"), "courses.program_id")
                ->get();*/

            return Course::join('typ_semester_courses',  'courses.id', '=', 'typ_semester_courses.course_id')
                ->where('typ_semester_objective_id', $this->typ_semester_objective_id)
                ->where('courses.program_id', $this->program_id)
                ->select('courses.*', 'typ_semester_courses.id as typ_semester_course_id', DB::raw("'{$this->semester_id}' as semester_id"), "courses.program_id")
                ->get();
            // $course_code_id = $course_codes->lists('course_id');
            //$course_code_typ = $course_codes->lists('typ_semester_course_id');


            /*

            $courses = []; //Course::whereIn("id", $course_code_id)->get();

            foreach ($course_codes as $i => $code) {
                $c = Course::where('id', $code->course_id)->first();
                $c->setAttribute('typ_semester_course_id', $code->typ_semester_course_id); //$course_code_typ[$i]);
                /*$c->setAttribute(
                    'transforming_actions',
                    DB::table('annual_plan_transformative')
                        ->join('transformative_actions', 'transformative_actions.id', '=', 'annual_plan_transformative.trans_id')
                        ->where('typ_semester_course_id', $c->typ_semester_course_id)
                        ->select('transformative_actions.*')
                        ->get()
                );
                //$c->setAttribute("future_typ_course_id", $this->()); //Course::getFutureTypSemesterCourses($c->typ_semester_course_id, $c->program_id));

                $c->setAttribute("students", Course::getStudentReportForOutcome($c));
                //$c->setAttribute("criteria", Course::getCriteriaPlanReport($c));
                $courses[] = $c;
            }




            return $courses;
            */
        }
        return null;
    }
    /**
     * Return the outcomes that the objective belongs to
     *
     * @return Illuminate\Database\Eloquent\Model
     */
    //Must have typ_semester_objective_id, program_id,semester_id to work

    //funciona con Outcome::getObjectivesReport
    //Es como la imagen suelta por el app llamada, tabla_estudiantes.jpg
    // 
    public static function getPlanReport($objective)
    {
        $course_codes = DB::table('typ_semester_courses')
            ->join('courses', 'courses.id', '=', 'typ_semester_courses.course_id')
            ->where('typ_semester_objective_id', $objective->typ_semester_objective_id)
            ->where('courses.program_id', $objective->program_id)
            ->select('courses.code', 'courses.number', 'courses.id as course_id', 'typ_semester_courses.id as typ_semester_course_id', DB::raw("{$objective->semester_id} as semester_id"), "courses.program_id")
            ->get();
        //Log::info($course_codes);
        Log::info("No es aqui");

        //$course_codes['studentPerOutcome'] = array();

        foreach ($course_codes as $course_code) {
            $course_code->transforming_actions = DB::table('annual_plan_transformative')
                ->join('transformative_actions', 'transformative_actions.id', '=', 'annual_plan_transformative.trans_id')
                ->where('typ_semester_course_id', $course_code->typ_semester_course_id)
                ->select('transformative_actions.*')
                ->get();
            $course_code->future_typ_course_id = Course::getFutureTypSemesterCourses($course_code->typ_semester_course_id, $course_code->program_id);
            $course_code->grouped = 1;

            $course_code->students = Course::getStudentReportForOutcome($course_code);
            //Log::info(array($course_code));
            Log::info("dimelo");
            $course_code->criteria = Course::getCriteriaPlanReport($course_code);
        }
        Log::info("???");
        return $course_codes;
    }
    public function outcomes()
    {
        return $this->belongsToMany('Outcome', 'objective_outcome', 'objective_id', 'outcome_id');
    }

    public function outcome()
    {
        return $this->belongsTo('Outcome');
    }




    /**
     * Return the program that the objective belongs to
     *
     * @return Illuminate\Database\Eloquent\Model
     */
    public function outcome_id()
    {
        return $this->hasMany('Objective_Outcome', 'objective_id');
    }
}