<?php

use Illuminate\Database\Eloquent\SoftDeletingTrait;

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

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

	protected $appends = array("annual_objectives");

	//dirty code, this belongs to annual plan, but outcome has the important data
	public function getExpectedTargetAttribute()
	{
		if (isset($this->semester_id) && isset($this->program_id)) {
			$expected_target = DB::table("target_outcomes_program")
				->where('program_id', $this->program_id)
				->where('semester_id', $this->semester_id)
				->first();

			if ($expected_target != null) {

				return $expected_target->expected_target;
			} else return "Go to Annual Plan and define a target for learning outcome";
		}
		Log::info("PApi rip chabau");
		return null;
	}

	//Dirty code sort of, la accion transformadora es para el programa, ppero esta bajo el dominio
	//tecnicamente esto es hasta del annual plan, pero na, outcomes bajo el annual plan van a tener
	// este atributo

	//program_transformative_actions
	public function getProgramTransformativeActionsAttribute()
	{
		/*$transformative_actions = DB::table('transformative_typ_outcome')
			->where('typ_semester_outcome_id', $this->typ_semester_outcome_id)
			->get();
		$trans_actions = [];
		foreach ($transformative_actions as $trans) {
			$ta = TransformativeAction::where('id', $trans->trans_id)->first();
			$ta->setAttribute('semester_id', $this->semester_id);
			$trans_actions[] = $ta;
		}
		return $trans_actions;*/

		return TransformativeAction::join('transformative_typ_outcome', 'transformative_typ_outcome.trans_id', '=', 'transformative_actions.id')
			->where('typ_semester_outcome_id', $this->typ_semester_outcome_id)
			->select('transformative_actions.*', DB::raw("{$this->semester_id} as semester_id"))
			->get();
	}

	public function getCommentsAttribute()
	{
		return DB::table('typ_outcome_report_comments')
			->where('typ_semester_outcome_id', $this->typ_semester_outcome_id)
			->get();
	}
	// Son los resultados de los estudiantes por dominio, devuelve el percent.
	public function getStudentPerformanceAttribute()
	{

		//create Counter for students

		$whole_dict = [];
		foreach ($this->annual_objectives as $objective) {
			foreach ($objective->grouped_annual_course as $course_code) {

				$course_code->grouped = 1;
				foreach ($course_code->student_report_for_outcome as $student) {

					//If is not defined, define
					if (!isset($whole_dict[$student->student_id])) {
						$whole_dict[$student->student_id] = array(
							'student' => $student->student_id,
							'criteria_attempted' => 0,
							'criteria_achieved' => 0
						);
					}
					$whole_dict[$student->student_id]["criteria_attempted"] += $student->criteria_attempted;
					$whole_dict[$student->student_id]["criteria_achieved"] += $student->criteria_achieved;
				}
			}
		}

		$students_achieved = 0;
		$students_attempted = 0;

		Log::info('Outcome_content');
		Log::info($whole_dict);

		foreach ($whole_dict as $student) {
			if ($student['criteria_attempted']) {

				$students_attempted += 1;
				$percentage = (($student['criteria_achieved'] / $student['criteria_attempted']) * 100);
				if ($percentage >= $this->expected_outcome) {

					$students_achieved += 1;
				}
			}
		}

		if ($students_attempted != 0)
			return round(($students_achieved / $students_attempted) * 100, 2);
		else
			return "N/A";
		/*
                                   if (course_code.criteria.length) {

                                        $.each(course_code.students, function(index, student) {
                                            if (wholeDict[student.student_id] === undefined) {
                                                wholeDict[student.student_id] = {
                                                    'student': student.student_id,
                                                    'criteria_attempted': 0,
                                                    'criteria_achieved': 0
                                                }

                                            }
                                            wholeDict[student.student_id][
                                                    "criteria_attempted"
                                                ] += student
                                                .criteria_attempted;
                                            wholeDict[student.student_id][
                                                    "criteria_achieved"
                                                ] += student
                                                .criteria_achieved;

                                        });

										 $.each(wholeDict, function(key, dict) {


                            if (dict.criteria_attempted) {
                                students_attempted += 1;
                                percentage = ((dict.criteria_achieved / dict.criteria_attempted) * 100).toFixed(
                                    2);
                                if (parseFloat(percentage) >= parseFloat(outcome.expected_outcome)) {
                                    achieved = "Yes"
                                    students_achieved += 1;
                                } else achieved = "No"
                            } else {
                                percentage = "N/A"
                                achieved = "N/A"
                            }
                            tableStudent.row.add([
                                dict.student,
                                dict.criteria_attempted,
                                dict.criteria_achieved,
                                percentage,
                                achieved

                            ]);
                        })
		*/
	}

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


	public function fetchObjectivesReport($semester_id, $program_id, $with_model = false)
	{
		/*$objectives = DB::table('typ_semester_outcome')
			->join('typ_semester_objectives', 'typ_semester_objectives.typ_semester_outcome_id', '=', 'typ_semester_outcome.id')
			->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
			->join("typ_program", 'typ_program.id', '=', 'typ_semester_outcome.typ_program_id')
			->where('typ_semester_outcome.semester_id', $semester_id)
			->where('typ_program.program_id', $program_id)
			->where('typ_semester_outcome.outcome_id', $this->id)
			->select('objectives.*')
			->get();*/

		$objectives = DB::table('objectives')
			->join('typ_semester_objectives', 'typ_semester_objectives.objective_id', '=', 'objectives.id')
			->join('typ_semester_outcome', 'typ_semester_outcome.id', '=', 'typ_semester_objectives.typ_semester_outcome_id')
			->join('typ_program', 'typ_program.id', '=', 'typ_semester_outcome.typ_program_id')
			->where('semester_id', $semester_id)
			->where('program_id', $program_id)
			->where('outcome_id', $this->id)
			->select('objectives.id as objective_id', 'objectives.text', 'typ_semester_objectives.id as typ_semester_objective_id', 'program_id', 'semester_id')
			->distinct();

// 		Log::info('agooooo me');
// 		Log::info($objectives->toSql());exit();
		//Log::info('semester_program_outcome' . $semester_id . ',' . $program_id . ',' . $this->id);

		if ($with_model) {
			//Log::info("Acho aqui estamos en with_model");
			$objective_id  = $objectives->lists('objective_id');
			$typ_semester_objective_ids = $objectives->lists('typ_semester_objective_id');
			//$program_id = $objectives->lists('program_id');
			//$semester_id = $objectives ->lists('semester_id');

			$objectives = Objective::whereIn("objectives.id", $objective_id);
			//Log::info($objective_id);
			//Log::info(array($objectives));

			foreach ($objectives as $i => $o) {
				Log::info("entramos aqui tambien?");
				$o->setAttribute("typ_semester_objective_id", $typ_semester_objective_ids[$i]);
				$o->setAttribute("program_id", $program_id);
				$o->setAttribute("semester_id", $semester_id);
			}
			return $objectives;
		}

// 		Log::info('agooooo me');
		$objectives_result=$objectives->get();
 Log::info('En fetchObjectivesReport'.json_encode($objectives_result));		//exit();
		return $objectives_result;
// 		return $objectives;
	}

	//Intento de get objectives pareados en un plan

	public function getAnnualObjectivesAttribute()
	{
		if (isset($this->typ_semester_outcome_id)) {
			/*Log::info('estamos aqui');
			$objectives = DB::table("typ_semester_objectives")
				->join('objectives', 'objectives.id', '=', 'typ_semester_objectives.objective_id')
				->where('typ_semester_outcome_id', $this->typ_semester_outcome_id)
				->select('typ_semester_objectives.id as typ_semester_objective_id', 'objectives.id as objective_id')
				->get();
			//$objectives_id = $objectives->lists('objective_id');
			//$typ_semester_objective_ids = $objectives->lists('typ_semester_objective_id');
			//$program_id = $objectives->lists('program_id');
			//$semester_id = $objectives ->lists('semester_id');
			//Log::info($this->typ_semester_outcome_id);
			//Log::info($objectives_id);

			$objectives_list = []; // Objective::whereIn("id", $objectives_id)->get();

			foreach ($objectives as $i => $ob) {
				$o = Objective::find($ob->objective_id);
				//Log::info("entramos aqui tambien?");
				$o->setAttribute("typ_semester_objective_id", $ob->typ_semester_objective_id); //$typ_semester_objective_ids[$i]);
				$o->setAttribute("semester_id", $this->semester_id);
				$o->setAttribute('program_id', $this->program_id);
				$objectives_list[] = $o;
			}

			return $objectives_list;*/

			return Objective::join("typ_semester_objectives", 'objectives.id', '=', 'typ_semester_objectives.objective_id')
				->where('typ_semester_outcome_id', $this->typ_semester_outcome_id)
				->select(
					'typ_semester_objectives.id as typ_semester_objective_id',
					'objectives.id as objective_id',
					'objectives.*',
					DB::raw("{$this->semester_id} as semester_id"),
					DB::raw("{$this->program_id} as program_id")
				)
				->get();
			//return $objectives;
		}
		//Log::info("Lol");
		return null;
	}
	/**
	 * Return the objectives that the outcome belongs to
	 *
	 * @return Illuminate\Database\Eloquent\Model
	 */
	public function getObjectivesAttribute()
	{
		//return $this->hasManyThrough('Objective', "Objective_Outcome", 'outcome_id', 'id');
		//		return $this->belongsToMany('Objective', 'objective_outcome');
		$objectives = Objective::join('objective_outcome', 'objectives.id', '=', 'objective_outcome.objective_id')
			->where('outcome_id', $this->id);
		if (isset($this->program_id)) {
			$objectives = $objectives->join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
				->where('program_id', $this->program_id);
		}
		return $objectives->get();
	}

	public function scopeObjectivesFromProgram($query, $programs)
	{
		return Objective::join('objective_program', 'objective_program.objective_id', '=', 'objectives.id')
			->join('objective_outcome', 'objective_outcome.objective_id', '=', 'objectives.id')
			//->join('programs', 'programs.id', '=', 'objective_program.program_id')
			->whereIn("program_id", $programs)
			->where('outcome_id', $this->id)

			->where('objective_program.objective_id', '<>', '0')
			->select('objectives.*')
			->groupBy('objectives.id');
	}
	// 	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 programs_attempted_in_school($semesters, $school_id)
	{
		$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)
			->where('programs.school_id', '=', $school_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 attempted_by_school($semesters, $school_id, $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)
			->where('programs.school_id', '=', $school_id)
			->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 attempted_by_program($semesters, $program_id, $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)
			->where('programs.id', '=', $program_id)
			->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_by_school($semesters, $school_id, $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)
			->where('programs.school_id', '=', $school_id)
			->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_by_program($semesters, $program_id, $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)
			->where('programs.school_.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();
	}
}