<?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) {
			$assessed = DB::table('activity_criterion')
				->join('assessments', 'assessments.activity_criterion_id', '=', 'activity_criterion.id')
				->where('activity_id', $activity->id)
				->first();
			if (!$assessed) continue;
			$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 function attempted_outcome($outcome_id, $semester)
	// 	{
	// 		$conteo= DB::table('activity_criterion')
	// 	    	->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
	// 	    	->join('courses', 'activities.course_id', '=', 'courses.id')
	// 	    	->join('programs', 'programs.id', '=', 'courses.program_id')
	// 	    	->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'activity_criterion.criterion_id')
	// 	    	->where('criterion_objective_outcome.outcome_id','=',$outcome_id)
	// 	    	->where('programs.id','=',$this->id)
	// 	    	->where('courses.semester_id','=',$semester)
	// 	    	->count(DB::raw('DISTINCT criterion_objective_outcome.outcome_id'))
	// 			;	    	
	// 		return $conteo;		
	// 	}

	public function attempted_criteria_by_outcome($outcome_id, $semesters)
	{
		$semesters_array = [];
		foreach ($semesters as $semester) {
			$semesters_array[] = $semester->id;
		}

		$conteo = DB::table('activity_criterion')
			->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
			->join('courses', 'activities.course_id', '=', 'courses.id')
			->join('programs', 'programs.id', '=', 'courses.program_id')
			->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'activity_criterion.criterion_id')
			->where('criterion_objective_outcome.outcome_id', '=', $outcome_id)
			->where('programs.id', '=', $this->id)
			->whereIn('courses.semester_id', $semesters_array)
			->count(DB::raw('DISTINCT criterion_objective_outcome.criterion_id'));
		return $conteo;
	}

	public function attempted_outcome($outcome_id, $semesters)
	{
		$semesters_array = [];
		foreach ($semesters as $semester) {
			$semesters_array[] = $semester->id;
		}

		$conteo = DB::table('activity_criterion')
			->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
			->join('courses', 'activities.course_id', '=', 'courses.id')
			->join('programs', 'programs.id', '=', 'courses.program_id')
			->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'activity_criterion.criterion_id')
			->where('criterion_objective_outcome.outcome_id', '=', $outcome_id)
			->where('programs.id', '=', $this->id)
			->whereIn('courses.semester_id', $semesters_array)
			->count(DB::raw('DISTINCT criterion_objective_outcome.outcome_id'));
		return $conteo;
	}

	public function achieved_criteria_by_outcome($outcome_id, $semesters)
	{
		$semesters_array = [];
		foreach ($semesters as $semester) {
			$semesters_array[] = $semester->id;
		}

		//   DB::enableQueryLog();
		// 		dd(DB::getQueryLog());
		// 		 $criteria=DB::table('new_criteria')
		// 	    	->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'new_criteria.id')
		// 	    	->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
		// 	    	->join('courses', 'activities.course_id', '=', 'courses.id')
		// 	    	->join('programs', 'programs.id', '=', 'courses.program_id')
		// 	    	->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'new_criteria.id')
		// 	    	->where('criterion_objective_outcome.outcome_id','=',$outcome_id)
		// 	    	->where('programs.id','=',$this->id)
		// 	    	->whereIn('courses.semester_id',$semesters_array)
		// 	    	->select('new_criteria.id','expected_percentage_students_achieving','activity_criterion.activity_id')
		// 	    	->distinct()
		// 	    	->get()
		// 			;

		$criteria = DB::table('criteria')
			->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
			->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
			->join('courses', 'activities.course_id', '=', 'courses.id')
			->join('programs', 'programs.id', '=', 'courses.program_id')
			->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
			->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
			->join('rubrics', 'rubric_activity.rubric_id', '=', 'rubrics.id')
			->where('criterion_objective_outcome.outcome_id', '=', $outcome_id)
			->where('programs.id', '=', $this->id)
			->whereIn('courses.semester_id', $semesters_array)
			->select('criteria.id', 'expected_percentage', 'activity_criterion.activity_id')
			->distinct()
			->get();

		// 		dd(DB::getQueryLog());

		$conteo = 0;
		$attempted_criteria_per_program_array = [];
		// 		$students_attempted=0;
		// 		$students_achieved=0;
		foreach ($criteria as $criterion) {
			if (!isset($students_attempted[$criterion->id])) $students_attempted[$criterion->id] = 0;
			if (!isset($students_achieved[$criterion->id])) $students_achieved[$criterion->id] = 0;
			$attempted_criteria_per_program_array[$criterion->id] = 1;
			$students_attempted[$criterion->id] += Criterion::students_attempted($criterion->id, $criterion->activity_id);
			$students_achieved[$criterion->id] += Criterion::students_achieved($criterion->id, $criterion->activity_id);
		}
		// 		var_dump($outcome_id);
		// 		var_dump($this->id);
		// 		if(isset($students_attempted))var_dump($students_attempted);
		// 		else{print "aqui hay algo";}
		// 		print "<br>";
		// 		if(isset($students_achieved))var_dump($students_achieved);
		// 		print "<br>";
		// 		print "<br>";
		// 			exit();
		if (isset($students_attempted)) {
			foreach ($students_attempted as $criteria_id => $students_attempted_n) {
				if ($students_attempted_n) {
					$percentage_students_who_achieved = 100.0 * $students_achieved[$criteria_id] / $students_attempted[$criteria_id];
				} else {
					$percentage_students_who_achieved = 0;
				}
				if ($percentage_students_who_achieved >= $criterion->expected_percentage) {
					$conteo++;
				}
			}
		}
		$attempted_criteria_per_program = count($attempted_criteria_per_program_array);
		$achievedProgramOutcome = 0;
		$outcome = Outcome::where('id', $outcome_id)->select('expected_outcome')->first();
		// 		var_dump($outcome->expected_outcome);
		// 		exit();
		if ($attempted_criteria_per_program != 0 && 100.0 * $conteo / $attempted_criteria_per_program >= $outcome->expected_outcome) {
			$achievedProgramOutcome = 1;
			// $output[]= 'END OF PROGRAM: '.$program->name.'-'.json_encode($achievedProgramsPerOutcome);
		}
		return $conteo;
	}

	public function achieved_outcome($outcome_id, $semesters)
	{
		$semesters_array = [];
		foreach ($semesters as $semester) {
			$semesters_array[] = $semester->id;
		}

		//   DB::enableQueryLog();
		// 		dd(DB::getQueryLog());
		// 		 $criteria=DB::table('new_criteria')
		// 	    	->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'new_criteria.id')
		// 	    	->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
		// 	    	->join('courses', 'activities.course_id', '=', 'courses.id')
		// 	    	->join('programs', 'programs.id', '=', 'courses.program_id')
		// 	    	->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'new_criteria.id')
		// 	    	->where('criterion_objective_outcome.outcome_id','=',$outcome_id)
		// 	    	->where('programs.id','=',$this->id)
		// 	    	->whereIn('courses.semester_id',$semesters_array)
		// 	    	->select('new_criteria.id','expected_percentage_students_achieving','activity_criterion.activity_id')
		// 	    	->distinct()
		// 	    	->get()
		// 			;

		$criteria = DB::table('criteria')
			->join('activity_criterion', 'activity_criterion.criterion_id', '=', 'criteria.id')
			->join('activities', 'activity_criterion.activity_id', '=', 'activities.id')
			->join('courses', 'activities.course_id', '=', 'courses.id')
			->join('programs', 'programs.id', '=', 'courses.program_id')
			->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
			->join('rubric_activity', 'rubric_activity.activity_id', '=', 'activities.id')
			->join('rubrics', 'rubric_activity.rubric_id', '=', 'rubrics.id')
			->where('criterion_objective_outcome.outcome_id', '=', $outcome_id)
			->where('programs.id', '=', $this->id)
			->whereIn('courses.semester_id', $semesters_array)
			->select('criteria.id', 'expected_percentage', 'activity_criterion.activity_id')
			->distinct()
			->get();

		// 		dd(DB::getQueryLog());

		$conteo = 0;
		$attempted_criteria_per_program_array = [];
		// 		$students_attempted=0;
		// 		$students_achieved=0;
		foreach ($criteria as $criterion) {
			if (!isset($students_attempted[$criterion->id])) $students_attempted[$criterion->id] = 0;
			if (!isset($students_achieved[$criterion->id])) $students_achieved[$criterion->id] = 0;
			$attempted_criteria_per_program_array[$criterion->id] = 1;
			$students_attempted[$criterion->id] += Criterion::students_attempted($criterion->id, $criterion->activity_id);
			$students_achieved[$criterion->id] += Criterion::students_achieved($criterion->id, $criterion->activity_id);
		}
		// 		var_dump($this->id);
		// 		if(isset($students_attempted))var_dump($students_attempted);
		// 		else{print "aqui hay algo";}
		// 		print "<br>";
		// 		if(isset($students_achieved))var_dump($students_achieved);
		// 		print "<br>";
		// 		print "<br>";
		// 			exit();
		if (isset($students_attempted)) {
			foreach ($students_attempted as $criteria_id => $students_attempted_n) {
				if ($students_attempted_n) {
					$percentage_students_who_achieved = 100.0 * $students_achieved[$criteria_id] / $students_attempted[$criteria_id];
				} else {
					$percentage_students_who_achieved = 0;
				}
				if ($percentage_students_who_achieved >= $criterion->expected_percentage) {
					$conteo++;
				}
			}
		}
		$attempted_criteria_per_program = count($attempted_criteria_per_program_array);
		$achievedProgramOutcome = 0;
		$outcome = Outcome::where('id', $outcome_id)->select('expected_outcome')->first();
		// 		var_dump($outcome->expected_outcome);
		// 		exit();
		if ($attempted_criteria_per_program != 0 && 100.0 * $conteo / $attempted_criteria_per_program >= $outcome->expected_outcome) {
			$achievedProgramOutcome = 1;
			// $output[]= 'END OF PROGRAM: '.$program->name.'-'.json_encode($achievedProgramsPerOutcome);
		}
		return $achievedProgramOutcome;
	}

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