<?php

class AdministratorsController extends \BaseController
{
	private function outcomes_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();
		$outcomeCount = 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);
			})
			->count();

		foreach ($outcomes as $outcome) {

			// 		var_dump($outcome->id);
			// 		print "<br>";
			$outcomes_attempted[$outcome->id] = $outcome->attempted($selected_semesters, $level);
			$outcomes_achieved[$outcome->id] = $outcome->achieved($selected_semesters, $level);
		}

		$uhs_school_id = School::where('name', 'LIKE', '%UHS%')->first()->id;
		$uhs_program_id = Program::where('name', 'LIKE', '%UHS%')->first()->id;

		foreach ($outcomes as $outcome) {
			$achievedProgramsPerOutcome[$outcome->id] = 0;         // For each program with courses that do assessment
			$attemptedProgramsPerOutcome[$outcome->id] = 0;         // For each program with courses that do assessment
			foreach (Program::with(array('courses' => function ($query) {
				//                 $query->whereNotNull('outcomes_attempted');
				$query->where('code', '!=', 'TEST');
				$query->whereIn('semester_id', Session::get('semesters_ids'));
			}))->where('is_graduate', $level)->where('school_id', '!=', $uhs_school_id)->orderBy('name', 'asc')->get() as $program) {
				$achieved_outcomes_per_program[$program->id] = 0;
				$attempted_outcomes_per_program[$program->id] = 0;
				$participating_programs[$program->id] = (object)array('id' => $program->id, 'name' => $program->name, 'is_graduate' => $program->is_graduate, 'school_id' => $program->school_id);

				// 				SELECT ac.id activity_criterion_id, ac.activity_id, ac.expected_student_score, ac.expected_percentage_students_achieving, co.program_id FROM activity_criterion ac, criteria c, activities a, objectives o, courses co where co.id=a.course_id and co.semester_id=13 and a.id=ac.activity_id and c.id = ac.criterion_id and o.id=c.objective_id and o.outcome_id = 2 order by program_id
				$program_attempted_outcome = $program->attempted_outcome($outcome->id, $selected_semesters);
				$attempted_outcomes_per_program[$program->id] += $program_attempted_outcome;
				// 				var_dump($program_attempted_outcome);
				// 				exit();
				if ($program_attempted_outcome) {
					$program_achieved_outcome = $program->achieved_outcome($outcome->id, $selected_semesters);
					$achieved_outcomes_per_program[$program->id] += $program_achieved_outcome;

					$attemptedProgramsPerOutcome[$outcome->id] += $program_attempted_outcome;
					//                     $attemptedProgramsPerOutcome[$outcome->id]+=1;
					$achievedProgramsPerOutcome[$outcome->id] += $program_achieved_outcome;
					// 					$achieved_outcomes_per_program[$outcome->id]=$program->achieved_outcome($outcome->id, $selected_semesters);
					// 					if($attempted_outcomes_per_program[$outcome->id]!=0 && 100.0*$achieved_outcomes_per_program[$outcome->id]/$attempted_outcomes_per_program[$outcome->id] >= $outcome->expected_outcome)
					// 					{
					// 						$achievedProgramsPerOutcome[$outcome->id]+=1;
					// 						// $output[]= 'END OF PROGRAM: '.$program->name.'-'.json_encode($achievedProgramsPerOutcome);
					// 					}
				}
			}
		}

		return array('outcomes' => $outcomes, 'outcomes_attempted' => $outcomes_attempted, 'outcomes_achieved' => $outcomes_achieved, 'attemptedProgramsPerOutcome' => $attemptedProgramsPerOutcome, 'achievedProgramsPerOutcome' => $achievedProgramsPerOutcome, 'participating_programs' => $participating_programs);
	}
	public function overview()
	{

		$selected_semesters = Semester::find(Session::get('semesters_ids'));

		$subgraduado = $this->outcomes_semesters($selected_semesters, 0);
		$outcomes_subgrad = $subgraduado['outcomes'];
		$undergrad_outcomes_achieved = $subgraduado['outcomes_achieved'];
		$undergrad_outcomes_attempted = $subgraduado['outcomes_attempted'];
		$attemptedUndergradProgramsPerOutcome = $subgraduado['attemptedProgramsPerOutcome'];
		$achievedUndergradProgramsPerOutcome = $subgraduado['achievedProgramsPerOutcome'];
		$participating_undergrad_programs = $subgraduado['participating_programs'];


		$graduado = $this->outcomes_semesters($selected_semesters, 1);
		$outcomes_grad = $graduado['outcomes'];
		$grad_outcomes_achieved = $graduado['outcomes_achieved'];
		$grad_outcomes_attempted = $graduado['outcomes_attempted'];
		$attemptedGradProgramsPerOutcome = $graduado['attemptedProgramsPerOutcome'];
		$achievedGradProgramsPerOutcome = $graduado['achievedProgramsPerOutcome'];
		$participating_grad_programs = $graduado['participating_programs'];

		//Total amount of learning outcomes
		//         $outcomeCount = Outcome::withTrashed()->count();
		//         $selected_semester = Semester::find(Session::get('semesters_ids')[0]);
		//         $outcomes_subgrad = Outcome::where(function($query) use ($selected_semester)
		//         								   {
		// 												$query->where('deactivation_date', '>=', $selected_semester->start)
		// 													  ->orWhere('deactivation_date', null);
		//         								   })
		//         							->where('activation_date', '<=', $selected_semester->end)
		//         							->where(function($query2)
		//         								    {
		// 												$query2->where("level",1)
		// 													   ->orWhere("level",3);
		//         									})
		//         							->orderBy('name', 'ASC')
		//         							->get();
		//         $outcomeCount_subgrad = Outcome::where(function($query) use ($selected_semester)
		//         									   {
		//         									   		$query->where('deactivation_date', '>=', $selected_semester->start)
		//         									   			  ->orWhere('deactivation_date', null);
		//         									   	})
		//            							->where('activation_date', '<=', $selected_semester->end)
		//      								->where(function($query2)
		//         									    {
		//         									    	$query2->where("level",1)
		//         									    		   ->orWhere("level",3);
		//         									    })
		//         								->count();
		//
		//         $outcomes_grad = Outcome::where(function($query) use ($selected_semester){$query->where('deactivation_date', '>=', $selected_semester->start)->orWhere('deactivation_date', null);})->where(function($query2){$query2->where("level",2)->orWhere("level",3);})->orderBy('name', 'ASC')->get();
		//         $outcomeCount_grad = Outcome::where(function($query) use ($selected_semester){$query->where('deactivation_date', '>=', $selected_semester->start)->orWhere('deactivation_date', null);})->where(function($query2){$query2->where("level",2)->orWhere("level",3);})->count();

		// 		print($outcomeCount_subgrad);
		// 		print($outcomeCount_grad);
		// Id for excluding UHS from records
		$uhs_school_id = School::where('name', 'LIKE', '%UHS%')->first()->id;
		//         $uhs_program_id = Program::where('name', 'LIKE', '%UHS%')->first()->id;

		$schools = School::where('id', '!=', $uhs_school_id)->orderBy('name', 'asc')->get();
		$title = 'Campus Overview';


		/**
		 * Calculate campus wide outcome performance by counting the number of students
         $undergrad_outcomes_attempted will have the number of criteria that was assessed in undergradute courses during the selected semester
         $undergrad_outcomes_achieved will have the number of criteria that was assessed in undergradute courses  during the selected semester
            where the percentage of students that obtained a better or equal score than the expected_student_score established in the
            activity_criterion table is greater or equal than the expected_percentage_students_achieving also established in the
            activity_criterion table
		 */

		// 		foreach($outcomes_subgrad as $outcome)
		// 		{
		// 			$undergrad_outcomes_attempted[$outcome->id]=$outcome->attempted($selected_semester->id,0);
		// 			$undergrad_outcomes_achieved[$outcome->id]=$outcome->achieved($selected_semester->id,0);
		// 		}
		// 		var_dump($undergrad_outcomes_attempted);
		// 		print"<br>";
		// 		var_dump($undergrad_outcomes_achieved);
		// 		print"<br>";
		// 		foreach($outcomes_grad as $outcome)
		// 		{
		// 			$grad_outcomes_attempted[$outcome->id]=$outcome->attempted($selected_semester->id,1);
		// 			$grad_outcomes_achieved[$outcome->id]=$outcome->achieved($selected_semester->id,1);
		// 		}

		/**
		 * Calculate how many programs achieved and attempted each outcome
		 */

		// Number of programs that achieved a particular learning outcome
		//         $achievedUndergradProgramsPerOutcome = array_fill(1, $outcomeCount_subgrad, 0);

		// Number of programs that attempted a particular learning outcome
		//         $attemptedUndergradProgramsPerOutcome = array_fill(1, $outcomeCount_subgrad, 0);

		// Names of programs doing assessment
		// Fetch programs with participation
		//         $participating_undergrad_programs = DB::table('VIEW_participating_programs')
		//             ->whereIn('semester_id', Session::get('semesters_ids'))
		//             ->where('is_graduate', 0)
		//             ->groupBy('id')
		//             ->orderBy('name', 'asc')
		//             ->get();
		//         $output = array();

		/**
		 * Calculate how many undergrad programs achieved and attempted each outcome
		 */
		//         foreach ($outcomes_subgrad as $outcome)
		//         {
		//  			$achieved_outcomes_per_undergrad_program[$outcome->id]=0;
		// 			$attempted_outcomes_per_undergrad_program[$outcome->id]=0;
		// 			$achievedUndergradProgramsPerOutcome[$outcome->id]=0;         // For each program with courses that do assessment
		// 			$attemptedUndergradProgramsPerOutcome[$outcome->id]=0;         // For each program with courses that do assessment
		//             foreach (Program::with(array('courses' => function($query){
		// //                 $query->whereNotNull('outcomes_attempted');
		//                 $query->where('code', '!=', 'TEST');
		//                 $query->whereIn('semester_id', Session::get('semesters_ids'));
		//             }))->where('is_graduate', 0)->where('school_id', '!=', $uhs_school_id)->orderBy('name', 'asc')->get() as $program)
		//             {
		//             	$participating_undergrad_programs[$program->id]=(object)array('id'=>$program->id, 'name'=>$program->name, 'is_graduate'=> $program->is_graduate, 'school_id'=>$program->school_id,'semestre_id',$selected_semester);
		//
		// // 				SELECT ac.id activity_criterion_id, ac.activity_id, ac.expected_student_score, ac.expected_percentage_students_achieving, co.program_id FROM activity_criterion ac, criteria c, activities a, objectives o, courses co where co.id=a.course_id and co.semester_id=13 and a.id=ac.activity_id and c.id = ac.criterion_id and o.id=c.objective_id and o.outcome_id = 2 order by program_id
		// 				$program_attempted_outcome=$program->attempted_outcome($outcome->id, $selected_semester->id);
		// 				$attempted_outcomes_per_undergrad_program[$outcome->id]+=$program_attempted_outcome;
		// // 				var_dump($program_attempted_outcome);
		// // 				exit();
		// 				if($program_attempted_outcome)
		// 				{
		//                     $attemptedUndergradProgramsPerOutcome[$outcome->id]+=1;
		// 					$achieved_outcomes_per_undergrad_program[$outcome->id]=$program->achieved_outcome($outcome->id, $selected_semester->id);
		// 					if($attempted_outcomes_per_undergrad_program[$outcome->id]!=0 && 100.0*$achieved_outcomes_per_undergrad_program[$outcome->id]/$attempted_outcomes_per_undergrad_program[$outcome->id] >= $outcome->expected_outcome)
		// 					{
		// 						$achievedUndergradProgramsPerOutcome[$outcome->id]+=1;
		// 						// $output[]= 'END OF PROGRAM: '.$program->name.'-'.json_encode($achievedUndergradProgramsPerOutcome);
		// 					}
		// 				}
		// 			}
		// 		}
		// 		var_dump($attempted_outcomes_per_undergrad_program);
		// 		print"<br>";
		// 		var_dump($achieved_outcomes_per_undergrad_program);
		// 		print"<br>";
		// 		var_dump($attemptedUndergradProgramsPerOutcome);
		// 		print"<br>";
		// 		var_dump($achievedUndergradProgramsPerOutcome);
		// 		print"<br>";
		// 		exit();
		/**
		 * Calculate how many grad programs achieved and attempted each outcome
         $grad_outcomes_attempted will have the number of criteria that was assessed in gradute courses during the selected semester
         $grad_outcomes_achieved will have the number of criteria that was assessed in gradute courses  during the selected semester
            where the percentage of students that obtained a better or equal score than the expected_student_score established in the
            activity_criterion table is greater or equal than the expected_percentage_students_achieving also established in the
            activity_criterion table
		 */


		// Names of programs doing assessment
		//         $participating_grad_programs = DB::table('VIEW_participating_programs')
		//             ->whereIn('semester_id', Session::get('semesters_ids'))
		//             ->where('is_graduate', 1)
		//             ->groupBy('id')
		//             ->orderBy('name', 'asc')
		//             ->get();


		//         foreach ($outcomes_grad as $outcome)
		//         {
		//  			$achieved_outcomes_per_grad_program[$outcome->id]=0;
		// 			$attempted_outcomes_per_grad_program[$outcome->id]=0;
		// 			$achievedGradProgramsPerOutcome[$outcome->id]=0;         // For each program with courses that do assessment
		// 			$attemptedGradProgramsPerOutcome[$outcome->id]=0;         // For each program with courses that do assessment
		//             foreach (Program::with(array('courses' => function($query){
		//                 $query->whereNotNull('outcomes_attempted');
		//                 $query->where('code', '!=', 'TEST');
		//                 $query->whereIn('semester_id', Session::get('semesters_ids'));
		//             }))->where('is_graduate', 1)->where('school_id', '!=', $uhs_school_id)->orderBy('name', 'asc')->get() as $program)
		//             {
		//             	$participating_grad_programs[$program->id]=(object)array('id'=>$program->id, 'name'=>$program->name, 'is_graduate'=> $program->is_graduate, 'school_id'=>$program->school_id,'semestre_id',$selected_semester);
		// // 				SELECT ac.id activity_criterion_id, ac.activity_id, ac.expected_student_score, ac.expected_percentage_students_achieving, co.program_id FROM activity_criterion ac, criteria c, activities a, objectives o, courses co where co.id=a.course_id and co.semester_id=13 and a.id=ac.activity_id and c.id = ac.criterion_id and o.id=c.objective_id and o.outcome_id = 2 order by program_id
		// 				$program_attempted_outcome=$program->attempted_outcome($outcome->id, $selected_semester->id);
		// 				$attempted_outcomes_per_grad_program[$outcome->id]+=$program_attempted_outcome;
		// // 				var_dump($attempted_outcomes_per_undergrad_program);
		// // 				exit();
		// 				if($program_attempted_outcome)
		// 				{
		//                     $attemptedGradProgramsPerOutcome[$outcome->id]+=1;
		// 					$achieved_outcomes_per_grad_program[$outcome->id]=$program->achieved_outcome($outcome->id, $selected_semester->id);
		// 					if($attempted_outcomes_per_grad_program[$outcome->id]!=0 && (float)$achieved_outcomes_per_grad_program[$outcome->id]/$attempted_outcomes_per_grad_program[$outcome->id]*100 >= $outcome->expected_outcome)
		// 					{
		// 						$achievedGradProgramsPerOutcome[$outcome->id]+=1;
		// 						// $output[]= 'END OF PROGRAM: '.$program->name.'-'.json_encode($achievedUndergradProgramsPerOutcome);
		// 					}
		// 				}
		// 			}
		// 		}
		// 		var_dump($grad_outcomes_attempted);
		// 		print("<br>");
		// 		var_dump($grad_outcomes_achieved);
		// 		exit();
		return View::make('local.managers.admins.overview', compact('title', 'schools', 'outcomes_subgrad', 'outcomes_grad', 'undergrad_outcomes_achieved', 'undergrad_outcomes_attempted', 'grad_outcomes_achieved', 'grad_outcomes_attempted', 'attemptedUndergradProgramsPerOutcome', 'participating_undergrad_programs', 'achievedUndergradProgramsPerOutcome', 'attemptedGradProgramsPerOutcome', 'participating_grad_programs', 'achievedGradProgramsPerOutcome'));
	}


	public function overview2()
	{
		// '''
		//stucture of outcomes array:
		//
		// $outcome->id
		// $outcome->name
		// $outcome->semesters
		//
		// $outcome->semesters[i]->id
		// $outcome->semesters[i]->code
		// $outcome->semesters[i]->name
		// $outcome->semesters[i]->tested
		// $outcome->semesters[i]->achievers
		// '''
		$is_graduate=0;
		$levels=array(1,3);
		$selected_semesters = Semester::find(Session::get('semesters_ids'));
		$title = 'Overview';

		$semesters = DB::table('overview')
			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
			->select('semesters.id', 'semesters.name', 'semesters.code')
			->orderBy('semesters.code', 'asc')
			->groupBy('semesters.id')
			->get();


		$outcomes = DB::table('overview')
			->rightJoin('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
			->whereIn('outcomes.level',$levels)
// 			->selectRaw('outcomes.name, coalesce(outcomes.new_outcome_id,outcomes.id) as id, sum(overview.total_evaluated) as total_evaluated, sum(overview.total_achieved) as total_achieved')
			->selectRaw('coalesce(outcomes.new_outcome_id,outcomes.id) as id, sum(overview.total_evaluated) as total_evaluated, sum(overview.total_achieved) as total_achieved')
			->orderBy('outcomes.name', 'asc')
			->groupBy(DB::raw('coalesce(outcomes.new_outcome_id,outcomes.id)'))
			->get();
//outcomes activos 
// 		$outcomes = DB::table('outcomes')
// 			->whereNull('deactivation_date')
// 			
// 			->get();
			
// DB::enableQueryLog();
		foreach ($outcomes as $outcome) {
// 		select semester_id, program_id, sum(`total_evaluated`) eval, sum(`total_achieved`) ach from overview where outcome_id in (select id from outcomes where coalesce(`new_outcome_id`,id) = 12) group by semester_id, program_id
			$outcome->semesters = DB::table('overview')
				->join('semesters', 'semesters.id', '=', 'overview.semester_id')
				->whereIn('overview.outcome_id',function ($query) use ($outcome)
				{
					$query->select('outcomes.id')
						  ->from('outcomes')
						  ->whereRaw('coalesce(outcomes.new_outcome_id,outcomes.id)=?',[$outcome->id]);
				})
				->selectRaw('semesters.id, semesters.name, semesters.code, sum(overview.total_evaluated) as total_evaluated, sum(overview.total_achieved) as total_achieved')
				->orderBy('semesters.code', 'asc')
				->groupBy('overview.semester_id')
				->get();
			$outcome->name=DB::table('outcomes')->select('name')->where('id',$outcome->id)->pluck('name');
		}
		usort($outcomes, function ($item1, $item2) {
    if ($item1->name == $item2->name) return 0;
    return $item1->name < $item2->name ? -1 : 1;
});
// var_dump($outcomes);
// 		exit();
// dd(DB::getQueryLog());

		$filters = array();
		$filters['school'] = DB::table('overview')
			->join('programs', 'programs.id', '=', 'overview.program_id')
			->join('schools', 'schools.id', '=', 'programs.school_id')
			->where('programs.is_graduate',$is_graduate)
			->select('schools.id', 'schools.name')
			->orderBy('schools.name', 'asc')
			->groupBy('schools.id')
			->get();

// DB::enableQueryLog();
		$filters['program'] = DB::table('overview')
			->join('programs', 'programs.id', '=', 'overview.program_id')
			->where('programs.is_graduate',$is_graduate)
			->select('programs.id', 'programs.name')
			->orderBy('programs.name', 'asc')
			->groupBy('overview.program_id')
			->get();
// dd(DB::getQueryLog());

		$filters['semester'] = DB::table('overview')
			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
			->select('semesters.id', 'semesters.name', 'semesters.code')
			->orderBy('semesters.code', 'asc')
			->groupBy('overview.semester_id')
			->get();

		$filters['year'] = DB::table('overview') //
			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
			->select('semesters.id', 'semesters.start')
			->orderBy('semesters.code', 'asc')
			->groupBy('overview.semester_id')
			->get();
// DB::enableQueryLog();
//select `outcomes`.`id`, `outcomes`.`name` from outcomes where deactivation_date is null and `outcomes`.`id` in (select outcome_id from overview)

		$filters['outcome'] = DB::table('outcomes')
			->select('outcomes.id', 'outcomes.name')
			->whereNull('deactivation_date')
			->whereIn('outcomes.id',function($query)
			{
				$query->select('outcome_id')
				 ->from('overview');
			}
			
			)
			->orderBy('outcomes.name', 'asc')
			->get();
// dd(DB::getQueryLog());
// exit();
		$target = 0.70;

		return View::make('local.managers.admins.overview2', compact('title', 'selected_semesters', 'outcomes', 'semesters', 'filters', 'target'));
	}

	// this is called when filters are changed
	public function overview2_filter_results()
	{

		// selected filter data
		$schools = Input::get('schools');
		$programs = Input::get('programs');
		$semesters = Input::get('semesters');
		$years = Input::get('years');
		$outcomes = Input::get('outcomes');
		$is_graduate=Input::get('is_graduate');
// 		$is_graduate=0;
		if($is_graduate)$levels=array(2,3);
		else $levels=array(1,3);

		// make an array id's of schools selected. if All, use All available id's
		if ($schools == array("0")) {
			$result = DB::table('overview')
				->join('programs', 'programs.id', '=', 'overview.program_id')
				->join('schools', 'schools.id', '=', 'programs.school_id')
				->where('programs.is_graduate',$is_graduate)
				->select('schools.id')
				->orderBy('schools.name', 'asc')
				->groupBy('schools.id')
				->get();
			$schools_id = array();
			foreach ($result as $school) {
				array_push($schools_id, $school->id);
			}
		} else {
			$schools_id = array();
			foreach ($schools as $school_id) {
				array_push($schools_id, intval($school_id));
			}
		}
		// make an array id's of programs selected. if All, use All available id's
		if ($programs == array("0")) {
			$result = DB::table('overview')
				->join('programs', 'programs.id', '=', 'overview.program_id')
				->where('programs.is_graduate',$is_graduate)
				->select('programs.id')
				->orderBy('programs.name', 'asc')
				->groupBy('overview.program_id')
				->get();
			$programs_id = array();
			foreach ($result as $program) {
				array_push($programs_id, $program->id);
			}
		} else {
			$programs_id = array();
			foreach ($programs as $program_id) {
				array_push($programs_id, intval($program_id));
			}
		}
		// make an array id's of semesters selected. if All, use All available id's
		if ($semesters == array("0")) {
			$result = DB::table('overview')
				->join('semesters', 'semesters.id', '=', 'overview.semester_id')
				->select('semesters.id')
				->orderBy('semesters.code', 'asc')
				->groupBy('overview.semester_id')
				->get();
			$semesters_id = array();
			foreach ($result as $semester) {
				array_push($semesters_id, $semester->id);
			}
		} else {
			$semesters_id = array();
			foreach ($semesters as $semester_id) {
				array_push($semesters_id, intval($semester_id));
			}
		}
		// make an array id's of years selected. if All, use All available id's
// 		if ($years == array("0")) {
// 			$result = DB::table('overview') //
// 				->join('semesters', 'semesters.id', '=', 'overview.semester_id')
// 				->select('semesters.id')
// 				->orderBy('semesters.code', 'asc')
// 				->groupBy('overview.semester_id')
// 				->get();
// 			$years_id = array();
// 			foreach ($result as $year) {
// 				array_push($years_id, $year->id);
// 			}
// 		} else {
// 			$years_id = array();
// 			foreach ($years as $year_id) {
// 				array_push($years_id, intval($year_id));
// 			}
// 		}
		// make an array id's of outcomes selected. if All, use All available id's
		if ($outcomes == array("0")) {

		$result = DB::table('overview')
			->rightJoin('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
			->whereIn('outcomes.level',$levels)
			->selectRaw('coalesce(outcomes.new_outcome_id,outcomes.id) as id')
			->orderBy('outcomes.name', 'asc')
			->groupBy(DB::raw('coalesce(outcomes.new_outcome_id,outcomes.id)'))
			->get();
// 		$result = DB::table('outcomes')
// 			->select('outcomes.id')
// 			->whereIn('outcomes.level',$levels)
// 			->whereNull('deactivation_date')
// // 			->whereIn('outcomes.id',function($query)
// // 				{
// // 					$query->select('outcome_id')
// // 					 ->from('overview');
// // 				}
// // 			)
// 			->orderBy('outcomes.name', 'asc')
// 			->get();
// 
// 			$result = DB::table('overview')
// 				->join('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
// 				->select('outcomes.id')
// 				->orderBy('outcomes.name', 'asc')
// 				->groupBy('overview.outcome_id')
// 				->get();
			$outcomes_id = array();
			foreach ($result as $outcome) {
				array_push($outcomes_id, $outcome->id);
			}
		} else {
			$outcomes_id = array();
			foreach ($outcomes as $outcome_id) {
				array_push($outcomes_id, intval($outcome_id));
			}
		}


		// get data for graph
		$semesters = DB::table('overview')
			->join('programs', 'programs.id', '=', 'overview.program_id')
			->join('schools', 'schools.id', '=', 'programs.school_id')
			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
			->join('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
// 			->where('programs.is_graduate',$is_graduate)
			->whereIn('schools.id', $schools_id)
			->whereIn('programs.id', $programs_id)
			->whereIn('semesters.id', $semesters_id)
			// ->whereIn('years.id',$years_id)
			->whereIn('outcomes.id', $outcomes_id)
			->select('semesters.name', 'semesters.code')
			->orderBy('semesters.code', 'asc')
			->groupBy('semesters.id')
			->get();

// 		$outcomes = DB::table('overview')
// 			->rightJoin('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
// 			->join('programs', 'programs.id', '=', 'overview.program_id')
// 			->join('schools', 'schools.id', '=', 'programs.school_id')
// 			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
// 			->whereIn('schools.id', $schools_id)
// 			->whereIn('programs.id', $programs_id)
// 			->whereIn('semesters.id', $semesters_id)
// 			->whereIn('outcomes.id', $outcomes_id)
// 			->selectRaw('outcomes.name, coalesce(outcomes.new_outcome_id,outcomes.id) as id, sum(overview.total_evaluated) as total_evaluated, sum(overview.total_achieved) as total_achieved')
// 			->orderBy('outcomes.name', 'asc')
// 			->groupBy(DB::raw('coalesce(outcomes.new_outcome_id,outcomes.id)'))
// 			->get();
			
// 			$sql=DB::table('overview')
// 				->join('programs', 'programs.id', '=', 'overview.program_id')
// 				->join('schools', 'schools.id', '=', 'programs.school_id')
// 				->join('semesters', 'semesters.id', '=', 'overview.semester_id')
// 				->select('overview.id','program_id','outcome_id','semester_id','total_evaluated','total_achieved')
// 				->whereIn('schools.id', $schools_id)
// 				->whereIn('programs.id', $programs_id)
// 				->whereIn('semesters.id', $semesters_id);

			$schools_text=implode(",", $schools_id);
			$programs_text=implode(",", $programs_id);
			$semesters_text=implode(",", $semesters_id);

			$sql="select overview.* from `overview` inner join `programs` on `programs`.`id` = `overview`.`program_id` inner join `schools` on `schools`.`id` = `programs`.`school_id` inner join `semesters` on `semesters`.`id` = `overview`.`semester_id` where `schools`.`id` in ($schools_text) and `programs`.`id` in ($programs_text) and `semesters`.`id` in ($semesters_text)";
			
			$outcomes = DB::table('outcomes')
			->leftJoin(DB::raw("({$sql}) as overview2"),'outcomes.id', '=', 'overview2.outcome_id')
			->whereIn('outcomes.id', $outcomes_id)
			->selectRaw('outcomes.name, coalesce(outcomes.new_outcome_id,outcomes.id) as id, sum(overview2.total_evaluated) as total_evaluated, sum(overview2.total_achieved) as total_achieved')
			->orderBy('outcomes.name', 'asc')
			->groupBy(DB::raw('coalesce(outcomes.new_outcome_id,outcomes.id)'))
// 			->mergeBindings($sql->getQuery())
			->get();
			
		foreach ($outcomes as $outcome) {
// 			$outcome->semesters = DB::table('overview')
// 				->join('programs', 'programs.id', '=', 'overview.program_id')
// 				->join('schools', 'schools.id', '=', 'programs.school_id')
// 				->join('semesters', 'semesters.id', '=', 'overview.semester_id')
// 				->join('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
// 				->whereIn('schools.id', $schools_id)
// 				->whereIn('programs.id', $programs_id)
// 				->whereIn('semesters.id', $semesters_id)
// 				// ->whereIn('years.id',$years_id)
// 				->whereIn('outcomes.id', $outcomes_id)
// 				->where('overview.outcome_id', $outcome->id)
// 				->selectRaw('semesters.id, semesters.name, semesters.code, sum(overview.total_evaluated) as total_evaluated, sum(overview.total_achieved) as total_achieved')
// 				->orderBy('semesters.code', 'asc')
// 				->groupBy('overview.outcome_id')
// 				->groupBy('overview.semester_id')
// 				->get();

			$outcome->semesters = DB::table('overview')
				->join('programs', 'programs.id', '=', 'overview.program_id')
				->join('schools', 'schools.id', '=', 'programs.school_id')
				->join('semesters', 'semesters.id', '=', 'overview.semester_id')
				->whereIn('schools.id', $schools_id)
				->whereIn('programs.id', $programs_id)
				->whereIn('semesters.id', $semesters_id)
				->whereIn('overview.outcome_id',function ($query) use ($outcome)
				{
					$query->select('outcomes.id')
						  ->from('outcomes')
						  ->whereRaw('coalesce(outcomes.new_outcome_id,outcomes.id)=?',[$outcome->id]);
				})
				->selectRaw('semesters.id, semesters.name, semesters.code, sum(overview.total_evaluated) as total_evaluated, sum(overview.total_achieved) as total_achieved')
				->orderBy('semesters.code', 'asc')
				->groupBy('overview.semester_id')
				->get();
		}

		// get data for filters
		$filters = array();
		$filters['school'] = DB::table('overview')
			->join('programs', 'programs.id', '=', 'overview.program_id')
			->join('schools', 'schools.id', '=', 'programs.school_id')
			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
			->join('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
			->whereIn('schools.id', $schools_id)
			->whereIn('programs.id', $programs_id)
			->whereIn('semesters.id', $semesters_id)
			// ->whereIn('years.id',$years_id)
			->whereIn('outcomes.id', $outcomes_id)
			->where('programs.is_graduate',$is_graduate)
			->select('schools.id', 'schools.name')
			->orderBy('schools.name', 'asc')
			->groupBy('schools.id')
			->get();
			
		$filters['program'] = DB::table('overview')
			->join('programs', 'programs.id', '=', 'overview.program_id')
			->join('schools', 'schools.id', '=', 'programs.school_id')
			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
			->join('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
			->whereIn('schools.id', $schools_id)
			// ->whereIn('programs.id',$programs_id)
			->whereIn('semesters.id', $semesters_id)
			// ->whereIn('years.id',$years_id)
			->whereIn('outcomes.id', $outcomes_id)
			->where('programs.is_graduate',$is_graduate)
			->select('programs.id', 'programs.name')
			->orderBy('programs.name', 'asc')
			->groupBy('overview.program_id')
			->get();
		$filters['semester'] = DB::table('overview')
			->join('programs', 'programs.id', '=', 'overview.program_id')
			->join('schools', 'schools.id', '=', 'programs.school_id')
			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
			->join('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
			->whereIn('schools.id', $schools_id)
			->whereIn('programs.id', $programs_id)
			// ->whereIn('semesters.id',$semesters_id)
			// ->whereIn('years.id',$years_id)
			->whereIn('outcomes.id', $outcomes_id)
			->where('programs.is_graduate',$is_graduate)
			->select('semesters.id', 'semesters.name', 'semesters.code')
			->orderBy('semesters.code', 'asc')
			->groupBy('overview.semester_id')
			->get();
// 		$filters['year'] = DB::table('overview') ////////////////////////////////////////////
// 			->join('programs', 'programs.id', '=', 'overview.program_id')
// 			->join('schools', 'schools.id', '=', 'programs.school_id')
// 			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
// 			->join('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
// 			->whereIn('schools.id', $schools_id)
// 			->whereIn('programs.id', $programs_id)
// 			->whereIn('semesters.id', $semesters_id)
// 			// ->whereIn('years.id',$years_id)
// 			->whereIn('outcomes.id', $outcomes_id)
// 
// 			->get();
			$sql="select overview.* from `overview` inner join `programs` on `programs`.`id` = `overview`.`program_id` inner join `schools` on `schools`.`id` = `programs`.`school_id` inner join `semesters` on `semesters`.`id` = `overview`.`semester_id` where `schools`.`id` in ($schools_text) and `programs`.`id` in ($programs_text) and `semesters`.`id` in ($semesters_text) and programs.is_graduate=$is_graduate";
			
			$filters['outcome']  = DB::table('outcomes')
			->leftJoin(DB::raw("({$sql}) as overview2"),'outcomes.id', '=', 'overview2.outcome_id')
			->select('outcomes.id', 'outcomes.name')
			->orderBy('outcomes.name', 'asc')
			->groupBy(DB::raw('coalesce(outcomes.new_outcome_id,outcomes.id)'))
// 			->mergeBindings($sql->getQuery())
			->get();

// 		$filters['outcome'] = DB::table('overview')
// 			->join('programs', 'programs.id', '=', 'overview.program_id')
// 			->join('schools', 'schools.id', '=', 'programs.school_id')
// 			->join('semesters', 'semesters.id', '=', 'overview.semester_id')
// 			->join('outcomes', 'outcomes.id', '=', 'overview.outcome_id')
// 			->whereIn('schools.id', $schools_id)
// 			->whereIn('programs.id', $programs_id)
// 			->whereIn('semesters.id', $semesters_id)
// 			// ->whereIn('years.id',$years_id)
// 			// ->whereIn('outcomes.id',$outcomes_id)
// 			->where('programs.is_graduate',$is_graduate)
// 			->select('outcomes.id', 'outcomes.name')
// 			->orderBy('outcomes.name', 'asc')
// 			->groupBy('overview.outcome_id')
// 			->get();

		return array(
			"semesters" => $semesters,
			"outcomes_info" => $outcomes,
			"filters" => $filters,
		);
	}
}
