<?php 

require_once(dirname(__FILE__)."/pages/register-candidate.php");
require_once(dirname(__FILE__)."/pages/entrance-exam-subjects.php");
require_once(dirname(__FILE__)."/pages/candidate-list.php");
require_once(dirname(__FILE__)."/pages/upload-entrance-result.php");
require_once(dirname(__FILE__)."/pages/candidate-profile.php");
require_once(dirname(__FILE__)."/pages/check-result.php");


function select_options_from_classes_of_admission() {
	$classes_of_admission = get_option('classes_of_admission');
	$res = [];
	if(_count($classes_of_admission) && is_array($classes_of_admission)) {
		foreach ($classes_of_admission as $key => &$value) {
			$res[get_class_name($value)] = $value;
		}
	}
	return $res;
}

function get_entrance_exam_subjects($adm_class, $select_option = true) {
	$post = new post;
	$all_subjects = $post->get_post_by_meta_data(['type' => 'ent_subject']);
	$res = [];
	if(_count($all_subjects)) {
		foreach ($all_subjects as $key => $value) {
			$classes = unserialize($value['classes_involved']);
			if(in_array($adm_class, $classes)){
				if($select_option) {
					$res[$value['subject_name']] = $value['id'];
				} else $res[$value['id']] = $value;
			}
		}
	}
	return $res;
}

function get_all_candidates($adm_class, $adm_term) {
	$post = new post;
	$user = new user;
	$candidates = $user->get_user_by_meta_data(['role' => 'candidate', 'class_of_admission' => $adm_class]);
	$cut_off = ($option = get_option('ent_exam_cut_off')) ? $option : 150;
	$res = array();
	$existing = db_post_unique(['type' => 'entrance_exam_result', 'adm_class' => $adm_class, 'adm_term' => $adm_term]);
	if($existing) {
		$subjects = get_entrance_exam_subjects($adm_class, true);
		$score = unserialize($existing['score']);
		if(_count($candidates)) {
			foreach ($candidates as $key => &$candidate) {
				$total_score = 0;
				if(_count($subjects)) {
					foreach ($subjects as $subject_id) {
						if(!isset($score[$candidate['id']][$subject_id])) {
							continue;
						} else {
							$total_score += (is_numeric($score[$candidate['id']][$subject_id])) ? $score[$candidate['id']][$subject_id] : 0;
						}
					}
					if($total_score == 0) {
						$candidate['exam_status'] = '<span class="badge badge-default">No Exam Record</span>';
					} else {
						if($total_score >= $cut_off) {
							$candidate['exam_status'] = '<span class="badge badge-success">Passed</span> </br><hr>Score: '.$total_score;
						} else $candidate['exam_status'] = '<span class="badge badge-danger">Failed</span> </br><hr>Score: '.$total_score;
					}
				}
			}
		}
	} else {
		if(_count($candidates)) {
			foreach ($candidates as $key => &$candidate) {
				$candidate['exam_status'] = '<span class="badge badge-default">No Exam Record</span>';
			}
		}
	}
	return $candidates;
}

function get_waiting_list($adm_class, $adm_term) {
	$user = new user;
	return $user->get_user_by_meta_data(['role' => 'fresher', 'class_of_admission' => $adm_class, 'term_of_admission' => $adm_term]);
}

function get_passed_candidates($adm_class, $adm_term) {
	$post = new post;
	$user = new user;
	$candidates = $user->get_user_by_meta_data(['role' => 'candidate', 'class_of_admission' => $adm_class]);
	$cut_off = ($option = get_option('ent_exam_cut_off')) ? $option : 150;
	$res = array();
	$existing = db_post_unique(['type' => 'entrance_exam_result', 'adm_class' => $adm_class, 'adm_term' => $adm_term]);
	if($existing) {
		$subjects = get_entrance_exam_subjects($adm_class, true);
		$score = unserialize($existing['score']);
		if(_count($candidates)) {
			foreach ($candidates as $key => &$candidate) {
				if(!isset($score[$candidate['id']])) {
					unset($candidates[$key]);
					continue;
				}
				$total_score = 0;
				if(_count($subjects)) {
					foreach ($subjects as $subject_id) {
						if(!isset($score[$candidate['id']][$subject_id])) {
							continue;
						} else {
							$total_score += (is_numeric($score[$candidate['id']][$subject_id])) ? $score[$candidate['id']][$subject_id] : 0;
						}
					}
					if($total_score == 0) {
						$candidate['exam_status'] = '<span class="badge badge-default">No Exam Record</span>';
					} else {
						if($total_score >= $cut_off) {
							$candidate['exam_status'] = '<span class="badge badge-success">Passed</span> </br><hr>Score: '.$total_score;
						} else unset($candidates[$key]);
					}
				}
			}
		}
	} else {
		if(_count($candidates)) {
			foreach ($candidates as $key => &$candidate) {
				$candidate['exam_status'] = '<span class="badge badge-default">No Exam Record</span>';
			}
		}
	}
	return $candidates;
}

function get_failed_candidates($adm_class, $adm_term) {
	$post = new post;
	$user = new user;
	$candidates = $user->get_user_by_meta_data(['role' => 'candidate', 'class_of_admission' => $adm_class]);
	$cut_off = ($option = get_option('ent_exam_cut_off')) ? $option : 150;
	$res = array();
	$existing = db_post_unique(['type' => 'entrance_exam_result', 'adm_class' => $adm_class, 'adm_term' => $adm_term]);
	if($existing) {
		$subjects = get_entrance_exam_subjects($adm_class, true);
		$score = unserialize($existing['score']);
		if(_count($candidates)) {
			foreach ($candidates as $key => &$candidate) {
				if(!isset($score[$candidate['id']])) {
					unset($candidates[$key]);
					continue;
				}
				$total_score = 0;
				if(_count($subjects)) {
					foreach ($subjects as $subject_id) {
						if(!isset($score[$candidate['id']][$subject_id])) {
							continue;
						} else {
							$total_score += (is_numeric($score[$candidate['id']][$subject_id])) ? $score[$candidate['id']][$subject_id] : 0;
						}
					}
					if($total_score == 0) {
						$candidate['exam_status'] = '<span class="badge badge-default">No Exam Record</span>';
					} else {
						if($total_score >= $cut_off) {
							unset($candidates[$key]);
						} else $candidate['exam_status'] = '<span class="badge badge-danger">Failed</span> </br><hr>Score: '.$total_score;
					}
				}
			}
		}
	} else {
		if(_count($candidates)) {
			foreach ($candidates as $key => &$candidate) {
				$candidate['exam_status'] = '<span class="badge badge-default">No Exam Record</span>';
			}
		}
	}
	return $candidates;
}

function get_no_record_candidates($adm_class, $adm_term) {
	$post = new post;
	$user = new user;
	$candidates = $user->get_user_by_meta_data(['role' => 'candidate', 'class_of_admission' => $adm_class]);
	$cut_off = ($option = get_option('ent_exam_cut_off')) ? $option : 150;
	$res = array();
	$existing = db_post_unique(['type' => 'entrance_exam_result', 'adm_class' => $adm_class, 'adm_term' => $adm_term]);
	if($existing) {
		$subjects = get_entrance_exam_subjects($adm_class, true);
		$score = unserialize($existing['score']);
		if(_count($candidates)) {
			foreach ($candidates as $key => &$candidate) {
				if(!isset($score[$candidate['id']])) {
					$candidate['exam_status'] = '<span class="badge badge-default">No Exam Record</span>';
				} else {
					unset($candidates[$key]);
				}
			}
		}
	} else {
		if(_count($candidates)) {
			foreach ($candidates as $key => &$candidate) {
				$candidate['exam_status'] = '<span class="badge badge-default">No Exam Record</span>';
			}
		}
	}
	return $candidates;
}

function get_next_category($gender, $class_of_admission, $role) {
	$sorting = new sorting;
	$distibution = get_class_category_gender_distribution($gender, $class_of_admission, $role);
	$distibution = $sorting->sort_by_meta_value($distibution, "count", "desc");
	$least = end($distibution);
	return $least['category'];
}

function rf_get_payment_status($student_meta, $fee_meta, $session) {
	$post = new post;
	if(is_array($student_meta)) {
		$student_id = $student_meta['id'];
	} else $student_id = $student_meta;
	$existing = $post->get_post_by_meta_data(['type' => 'credit', 'cat' => $fee_meta['cat'], 'term' => $fee_meta['adm_term'], 'session' => $session, 'student_id' => $student_id, 'status' => 'confirmed']);
	if(_count($existing)) {
		$paid = 0;
		foreach ($existing as $credit_meta) {
			$paid += (is_numeric($credit_meta['amount'])) ? $credit_meta['amount'] : 0;
		}
		if($paid == 0) return $fee_meta['amount'];
		if($paid > $fee_meta['amount']) return false;
		return $fee_meta['amount'] - $paid;
	} else return $fee_meta['amount'];
}

function rf_get_affected_students($fee_meta) {
	$user = new user;
	if($fee_meta['gender'] == 'Both') {
		$students = $user->get_user_by_meta_data(["role" => "student", "status" => "active", "class_of_admission" => $fee_meta['adm_class'], "term_of_admission" => $fee_meta['adm_term']]);
		$students = array_merge($students, $user->get_user_by_meta_data(["role" => "fresher", "status" => "active", "class_of_admission" => $fee_meta['adm_class'], "term_of_admission" => $fee_meta['adm_term']]));
	} else {
		$students = $user->get_user_by_meta_data(["role" => "student", "status" => "active", "gender" => $fee_meta['gender'], "class_of_admission" => $fee_meta['adm_class'], "term_of_admission" => $fee_meta['adm_term']]);
		$students = array_merge($students, $user->get_user_by_meta_data(["role" => "fresher", "gender" => $fee_meta['gender'], "status" => "active", "class_of_admission" => $fee_meta['adm_class'], "term_of_admission" => $fee_meta['adm_term']]));
	}
	

	return $students;
}

function rf_get_student_that_paid($fee_meta) {
	$post = new post;
	$aff_students = rf_get_affected_students($fee_meta);
	$students_that_paid = $students_oweing = array();
	$term_meta = $post->get_post_meta($fee_meta['adm_term']);
	$session = $term_meta['acsession'];
	if(_count($aff_students)) {
		foreach ($aff_students as $student_meta) {
			$status = rf_get_payment_status($student_meta['id'], $fee_meta, $session);
			if($status > 0) {
				array_push($students_oweing, $student_id);
			} else array_push($students_that_paid, $student_id);
		}
	}
	return array("students_oweing" => $students_oweing, "students_that_paid" => $students_that_paid);
}