<?php

class Program extends Eloquent
{
	public function school()
	{
		return $this->belongsTo('School');
	}

	public function courses()
	{
		return $this->hasMany('Course')->with('semester')->whereIn('semester_id', Session::get('semesters_ids'))->orderBy('code')->orderBy('number')->orderBy('section');
	}

	public function coordinators()
	{
		return $this->hasMany('User');
	}

	public function professors()
	{
		return $this->hasMany('Professor');
	}

	public function students()
	{
		return $this->hasMany('Student');
	}

	public function templates()
	{
		return $this->hasMany('Template');
	}

	public function activities()
	{
		return $this->hasManyThrough('Activity', 'Course')->whereNotNull('activities.outcomes_attempted')->whereIn('semester_id', Session::get('semesters_ids'));
	}

	public function publishedActivities()
	{
		return $this->hasManyThrough('Activity', 'Course')->whereNotNull('activities.outcomes_attempted')->where('activities.draft', 0)->whereIn('semester_id', Session::get('semesters_ids'));
	}

	public function assessesOutcome($outcome_id)
	{

		foreach ($this->publishedActivities as $activity)
		{
			$outcomes_attempted = (array)$activity->o_att_array;
			if(array_key_exists($outcome_id, $outcomes_attempted) && $outcomes_attempted[$outcome_id] != 0)
			{
				return true;
			}
		}
		return false;
	}

	// return the users that are pcoords for a program
	public function users()
	{
		return $this->belongsToMany('User');
	}

	public function objectives()
	{
		return $this->hasMany('Objective');
	}

	// Return learning objectives ordered by outcome
	public function objectivesByOutcome()
	{
        // Objective::
		$objectives = DB::table('outcomes')
		->select('outcomes.id as outcome_id', 'outcomes.name as outcome_name', 'objectives.id as objective_id', 'objectives.text', 'objectives.active')
		->leftJoin('objectives', function($join)
		{
			$join
				->on('outcomes.id', '=', 'objectives.outcome_id')
				->where('objectives.program_id', '=', $this->id);
		})
		->orderBy('outcome_name', 'ASC')
		->orderBy('objectives.text', 'ASC')
		->get();

		return $objectives;
	}

	public function hasObjectivesInOutcome($outcome_id)
	{
		return Objective::where('program_id', $this->id)
			->where('outcome_id', $outcome_id)->count();
	}

	/**
	 * Return all the criteria in/for the program, if any
	 *
	 * @return Illuminate\Database\Eloquent\Collection
	 */
	public function criteria()
	{
		return $this->hasMany('Criterion');
	}


	public static function generalComponentPrograms(){
		return self::whereIn('id', array(123,124,125,126,127,128,129))->get();
	}

	public function assessmentOverview(){

        $assessment_overview = array();

		$assessment_overview['program_courses'] = $this->courses;
        $outcomeCount = Outcome::all()->count();


        $assessment_overview['outcomes_achieved'] = array_fill(1, $outcomeCount, 0);
        $assessment_overview['outcomes_attempted'] = array_fill(1, $outcomeCount, 0);

        $assessment_overview['assessed_courses_count'] = 0;
        foreach ($assessment_overview['program_courses'] as $course)
        {
            if($course->outcomes_achieved!=NULL)
            {
                $course_outcomes_achieved =json_decode($course->outcomes_achieved, true);
                $course_outcomes_attempted =json_decode($course->outcomes_attempted, true);
                for($i=1; $i<=count($assessment_overview['outcomes_attempted']); $i++)
                {
                  $assessment_overview['outcomes_achieved'][$i]+=$course_outcomes_achieved[$i];
                  $assessment_overview['outcomes_attempted'][$i]+=$course_outcomes_attempted[$i];
                }
                $assessment_overview['assessed_courses_count']+=1;
            }
        }

        /**
         * List of grouped courses (grouped sections)
         */

        $assessment_overview['grouped_courses'] = Course::
            select(DB::raw('name, code, number, max(outcomes_attempted) as outcomes_attempted, semester_id, program_id'))
            ->with('semester')
            ->with('program')
            ->where('program_id', $this->id)
            ->whereIn('semester_id', Session::get('semesters_ids'))
            ->groupBy(array('code', 'number', 'semester_id'))
            ->orderBy('code')
            ->orderBy('number')
            ->orderBy('semester_id')
            ->get();

            Log::info(Course::
            select(DB::raw('name, code, number, max(outcomes_attempted) as outcomes_attempted, semester_id, program_id'))
            ->with('semester')
            ->with('program')
            ->where('program_id', $this->id)
            ->whereIn('semester_id', Session::get('semesters_ids'))
            ->groupBy(array('code', 'number', 'semester_id'))
            ->orderBy('code')
            ->orderBy('number')
            ->orderBy('semester_id')
            ->toSql());


        return $assessment_overview;
	}

}
