<?php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Outcome extends Eloquent
{
	use SoftDeletingTrait;
	protected $dates = ['deleted_at'];

	protected $fillable = array('name', 'definition');

	public function criteria()
	{
		return $this->hasManyThrough('Criterion', 'Objective')->orderBy('name');
	}

	/**
	 * Return the objectives that the outcome belongs to
	 *
	 * @return Illuminate\Database\Eloquent\Model
	 */
	public function objectives()
	{
		return $this->hasMany('Objective');
		//		return $this->belongsToMany('Objective', 'objective_outcome');
	}
	// 	public function attempted($semester, $is_grad)
	// 	{
	// 		return 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','=',$this->id)
	// 	    	->where('programs.is_graduate','=',$is_grad)
	// 	    	->where('courses.semester_id','=',$semester)
	// 	    	->count(DB::raw('DISTINCT new_criteria.id,activity_criterion.activity_id'))
	// 			;
	//
	// 	}
	// 	public function attempted($semester, $is_grad)
	// 	{
	// 		 $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','=',$this->id)
	// 	    	->where('programs.is_graduate','=',$is_grad)
	// 	    	->where('courses.semester_id','=',$semester)
	// 	    	->select('new_criteria.id','expected_percentage_students_achieving','activity_criterion.activity_id')
	// 	    	->distinct()
	// 	    	->get()
	// 			;
	// 		$conteo=0;
	// 		foreach($criteria as $criterion)
	// 		{
	// 			$students_attempted=Criterion::students_attempted($criterion->id, $criterion->activity_id);
	// 			if($students_attempted)
	// 			{
	// 				$conteo++;
	// 			}
	// 		}
	// 		return $conteo;
	// 	}
	public static function active_by_semesters($selected_semesters, $level)
	{
		$min_start = "9000-01-01 00:00:00";
		$max_end = "1000-01-01 00:00:00";
		foreach ($selected_semesters as $semester) {
			if ($min_start > $semester->start) {
				$min_start = $semester->start;
			}
			if ($max_end < $semester->end) {
				$max_end = $semester->end;
			}
		}
		$outcomes = Outcome::where(function ($query) use ($min_start) {
			$query->where('deactivation_date', '>=', $min_start)
				->orWhere('deactivation_date', null);
		})
			->where('activation_date', '<=', $max_end)
			->where(function ($query2) use ($level) {
				$query2->where("level", $level + 1)
					->orWhere("level", 3);
			})
			->orderBy('name', 'ASC')
			->get();

		// 		return array('outcomes'=>$outcomes,'outcomes_attempted'=>$outcomes_attempted, 'outcomes_achieved'=>$outcomes_achieved, 'attemptedProgramsPerOutcome'=>$attemptedProgramsPerOutcome, 'achievedProgramsPerOutcome'=>$achievedProgramsPerOutcome,'participating_programs'=>$participating_programs);
		return $outcomes;
	}

	public function programs_attempted($semesters)
	{
		$semesters_array = [];
		foreach ($semesters as $semester) {
			$semesters_array[] = $semester->id;
		}
		// 		 $programs=DB::table('programs')
		// 	    	->join('courses', 'programs.id', '=', 'courses.program_id')
		// 	    	->join('activities', 'activities.course_id', '=', 'courses.id')
		// 	    	->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
		// 	    	->join('new_criteria', 'activity_criterion.criterion_id', '=', 'new_criteria.id')
		// 	    	->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'new_criteria.id')
		// 	    	->where('criterion_objective_outcome.outcome_id','=',$this->id)
		// 	    	->whereIn('courses.semester_id',$semesters_array)
		// 	    	->distinct()
		// 	    	->select('programs.id')
		// 	    	->get()
		// 			;
		$programs = DB::table('programs')
			->join('courses', 'programs.id', '=', 'courses.program_id')
			->join('activities', 'activities.course_id', '=', 'courses.id')
			->join('activity_criterion', 'activity_criterion.activity_id', '=', 'activities.id')
			->join('criteria', 'activity_criterion.criterion_id', '=', 'criteria.id')
			->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
			->where('criterion_objective_outcome.outcome_id', '=', $this->id)
			->whereIn('courses.semester_id', $semesters_array)
			->distinct()
			->select('programs.id')
			->get();

		return $programs;
	}

	public function attempted($semesters, $is_grad)
	{
		$semesters_array = [];
		foreach ($semesters as $semester) {
			$semesters_array[] = $semester->id;
		}
		// 		 $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','=',$this->id)
		// 	    	->where('programs.is_graduate','=',$is_grad)
		// 	    	->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('rubric_activity', 'activities.id', '=', 'rubric_activity.activity_id')
			->join('rubrics', 'rubrics.id', '=', 'rubric_activity.rubric_id')
			->join('criterion_objective_outcome', 'criterion_objective_outcome.criterion_id', '=', 'criteria.id')
			->where('criterion_objective_outcome.outcome_id', '=', $this->id)
			->where('programs.is_graduate', '=', $is_grad)
			->whereIn('courses.semester_id', $semesters_array)
			->select('criteria.id', 'expected_percentage', 'activity_criterion.activity_id')
			->distinct()
			->get();
		$conteo = 0;
		foreach ($criteria as $criterion) {
			$students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
			if ($students_attempted) {
				$conteo++;
			}
		}
		// 		var_dump($conteo);
		// 		exit();
		return $conteo;
	}

	public function courses_attempted($courses)
	{
		$courses_array = [];
		foreach ($courses as $course) {
			// 		var_dump($course);
			// 		exit();
			$courses_array[] = $course->id;
		}
		// 		 $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','=',$this->id)
		// 	    	->whereIn('courses.id',$courses_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', '=', $this->id)
			->whereIn('courses.id', $courses_array)
			->select('criteria.id', 'expected_percentage', 'activity_criterion.activity_id')
			->distinct()
			->get();
		$conteo = 0;
		foreach ($criteria as $criterion) {
			$students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
			if ($students_attempted) {
				$conteo++;
			}
		}
		// 		var_dump($conteo);
		// 		exit();
		return $conteo;
	}

	public function courses_achieved($courses)
	{
		$courses_array = [];
		foreach ($courses as $course) {
			$courses_array[] = $course->id;
		}

		// 		 $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','=',$this->id)
		// 	    	->whereIn('courses.id',$courses_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', '=', $this->id)
			->whereIn('courses.id', $courses_array)
			->select('criteria.id', 'expected_percentage as expected_percentage_students_achieving', 'activity_criterion.activity_id')
			->distinct()
			->get();

		$conteo = 0;
		foreach ($criteria as $criterion) {
			$students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
			$students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);

			if ($students_attempted) {
				$percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
			} else {
				$percentage_students_who_achieved = 0;
			}
			if ($percentage_students_who_achieved >= $criterion->expected_percentage_students_achieving) {
				$conteo++;
			}
		}
		return $conteo;
	}

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

		//   DB::enableQueryLog();
		// 		 $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','=',$this->id)
		// 	    	->where('programs.is_graduate','=',$is_grad)
		// 	    	->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', '=', $this->id)
			->where('programs.is_graduate', '=', $is_grad)
			->whereIn('courses.semester_id', $semesters_array)
			->select('criteria.id', 'expected_percentage', 'activity_criterion.activity_id')
			->distinct()
			->get();
		// 		dd(DB::getQueryLog());

		$conteo = 0;
		foreach ($criteria as $criterion) {
			$students_attempted = Criterion::students_attempted($criterion->id, $criterion->activity_id);
			$students_achieved = Criterion::students_achieved($criterion->id, $criterion->activity_id);

			if ($students_attempted) {
				$percentage_students_who_achieved = 100.0 * $students_achieved / $students_attempted;
			} else {
				$percentage_students_who_achieved = 0;
			}
			if ($percentage_students_who_achieved >= $criterion->expected_percentage) {
				$conteo++;
			}
		}
		return $conteo;
	}


	// 	public function achieved($semester, $is_grad)
	// 	{
	//
	// //   DB::enableQueryLog();
	// 		 $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')
	// 	    	->where('criterion_objective_outcome.outcome_id','=',$this->id)
	// 	    	->where('programs.is_graduate','=',$is_grad)
	// 	    	->where('courses.semester_id','=',$semester)
	// 	    	->select('criteria.id','expected_percentage_students_achieving','activity_criterion.activity_id')
	// 	    	->distinct()
	// 	    	->get()
	// 			;
	// // 		dd(DB::getQueryLog());
	//
	// 		$conteo=0;
	// 		foreach($criteria as $criterion)
	// 		{
	// 			$outcome_performance=DB::table('outcome_performance')
	// 				->where('outcome_performance.outcome_id','=',$this->id)
	// 				->where('outcome_performance.criterion_id','=',$criterion->id)
	// 				->where('outcome_performance.semester_id','=',$semester)
	// 				->where('outcome_performance.level','=',$is_grad)
	// 				->select('students_attempted','students_achieved')
	// 				->first();
	// 			if(!empty($outcome_performance) and $outcome_performance->students_attempted)
	// 			{
	// 				$percentage_students_who_achieved=100.0*$outcome_performance->students_achieved/$outcome_performance->students_attempted;
	// 			}
	// 			else
	// 			{
	// 				$students_attempted=Criterion::students_attempted($criterion->id, $criterion->activity_id);
	// 				$students_achieved=Criterion::students_achieved($criterion->id, $criterion->activity_id);
	//
	// 				if($students_attempted)
	// 				{
	// 					$percentage_students_who_achieved=100.0*$students_achieved/$students_attempted;
	// 					DB::table('outcome_performance')->insert(array('criterion_id'=>$criterion->id,'outcome_id'=>$this->id,'semester_id'=>$semester,
	// 					'students_attempted' => $students_attempted,'students_achieved' => $students_achieved,'level'=>$is_grad));
	// 				}
	// 				else
	// 				{
	// 					$percentage_students_who_achieved=0;
	// 				}
	// 			}
	// 			if($percentage_students_who_achieved>=$criterion->expected_percentage_students_achieving)
	// 			{
	// 				$conteo++;
	// 			}
	// 		}
	// 		return $conteo;
	// 	}
	//

	public static function active()
	{
		//TODO: Check when semester doesnt exist or session is empty
		$selected_semester = Semester::find(Session::get('semesters_ids')[0]);
		return Outcome::withTrashed()->where('deactivation_date', '>=', $selected_semester->start)->orWhere('deactivation_date', null)->orderBy('name', 'ASC')->get();
	}
}
