<?php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

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

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

    /**
     * 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', '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);

        //$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->students = Course::getStudentReportForOutcome($course_code);
            Log::info(array($course_code));
            $course_code->criteria = Course::getCriteriaPlanReport($course_code);
        }
        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');
    }
}