<?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, new_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, new_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, new_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'));
    }
}