<?php
    function redirect_to($location){
        header("location: {$location}");
        exit();
    }
    
	//////////// FUNCTION TO REMOVE SLASHES AND TO REMOVE SPACES //////////
function inject_checker($connection, $string) {
    if (!is_string($string)) {
        return '';
    }

    return mysqli_real_escape_string($connection, trim($string));
}

	
	/////////// FUNCTION TO STRIP SLASHES ////////////////
	function input_checker ($data){
		$data = trim($data);
		$data = stripcslashes($data);
		$data = htmlspecialchars($data);
		return $data;
	}
	

	//////////////// FUNCTION TO VERIFY ONLY TEXT FIELDS ///////////////
	function text_only_validator ($name){
		return(@ereg('^[a-zA-Z]+[a-z0-9A-Z]+$', $name));
	}

	//////////////// FUNCTION TO VERIFY ONLY USERNAME FIELDS ///////////////
	function username_validator ($username){
		return(@ereg('^[a-zA-Z]+[a-z0-9A-Z]+$', $username));
	}

	////////////////FUNCTION TO VERIFY ONLY EMAIL FIELDS ///////////////
	function single_email_validator ($email){
		return(@ereg('^[a-z0-9A-Z]+@[a-z0-9A-Z]+\\.[a-z]+$', $email));
	}
	
	/////////////// TO ENCRYPT DATA SENT THROUGH GETS //////////////
	function confused ($get){
		return( sha1(sha1(sha1(htmlentities(urlencode($sent))))) );
	}
	
	
	function subject_position($position){
		$position_array = array(11, 12, 13, 111, 112, 113, 211, 212, 213, 311, 312, 313, 411, 412, 413, 511, 512, 513, 611, 612, 613, 711, 712, 713, 811, 812, 813, 911, 912, 913);
		
		//Get the reversed order of the positions
		$reversed_position = strrev($position);
		
		//Get the first integer when the position has been reversed
		$first_number_reversed = substr($reversed_position, 0, 1);
		
		if(in_array($position, $position_array)){
			$position_in_class = "<b>{$position}th</b>";
			return $msg = $position_in_class;
		}
		// If the when the position is reversed and the first integer is 1, it should append "st" to the position
		elseif($first_number_reversed == 1){
			$position_in_class = "<b>{$position}st</b>";
			return $msg = $position_in_class;
		}
		
		// If the when the position is reversed and the first integer is 2, it should append "nd" to the position
		elseif($first_number_reversed == 2){
			$position_in_class = "<b>{$position}nd</b>";
			return $msg = $position_in_class;
		}
		
		// If the when the position is reversed and the first integer is 3, it should append "rd" to the position
		elseif($first_number_reversed == 3){
			$position_in_class = "<b>{$position}rd</b>";
			return $msg = $position_in_class;
		}
		//Else the rest of the positions will be appended with "th"
		else{
			$position_in_class = "<b>{$position}th</b></b>	";
			return $msg = $position_in_class;
		}
	}
	
	function rnd($length, $lower=true, $upper=true, $numbers=true){
		$pool = "";
		$result = "";
		
		if($lower == true){
			$pool .= "abcdefghijklmnopqrstuvwxyz";
		}
		
		if($upper == true){
			$pool .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		}
		
		if($numbers == true){
			$pool .= "01234567890";
		}
		
		$cc = 0;
		while($cc < $length){
			$result .= $pool[mt_rand(0, strlen($pool)-1)];
			$cc++;
		}
		return $result;
	}
// permissions.php
function check_permission($connection, $role_id, $page_name) {
    $stmt = $connection->prepare("
        SELECT 1 FROM role_permissions
        JOIN permissions ON permissions.id = role_permissions.permission_id
        WHERE role_permissions.role_id = ? AND permissions.permission_name = ?
    ");
    $stmt->bind_param("is", $role_id, $page_name);
    $stmt->execute();
    $result = $stmt->get_result();
    return $result->num_rows > 0;
}


	
	function get_role_id_by_name($connection, $role_name) {
    $stmt = $connection->prepare("SELECT id FROM role WHERE role_name = ?");
    $stmt->bind_param("s", $role_name);
    $stmt->execute();
    $stmt->bind_result($id);
    $stmt->fetch();
    $stmt->close();
    return $id;
}

function getSubjectsByStudentClass($connection, $student_class) {
    // Step 1: Get level of the class
    $stmt1 = $connection->prepare("SELECT level FROM classes WHERE classes = ?");
    $stmt1->bind_param("s", $student_class);
    $stmt1->execute();
    $result1 = $stmt1->get_result();

    if ($result1->num_rows === 0) {
        return ["No level found for class"];
    }

    $row = $result1->fetch_assoc();
    $level = $row['level'];
    $stmt1->close();

    // Step 2: Get subjects by level
    $stmt2 = $connection->prepare("SELECT subject FROM school_subject_levels WHERE level = ? ORDER BY subject ASC");
    $stmt2->bind_param("s", $level);
    $stmt2->execute();
    $result2 = $stmt2->get_result();

    $subjects = [];
    if ($result2->num_rows > 0) {
        while ($sub = $result2->fetch_assoc()) {
            $subjects[] = $sub['subject'];
        }
    }
    $stmt2->close();

    return $subjects;
}
	function getOrdinal($number) 
	{
        $suffix = ['th', 'st', 'nd', 'rd'];
        $value = $number % 100;
        return $number . '<sup>' .($suffix[($value % 10 > 3 || $value % 100 > 13) ? 0 : ($value % 10)] ?? $suffix[0] . '</sup>');
    }
    
    // Reusable function to get failing subjects
	function getFailingSubjects($connection, $reg_number, $class, $term, $session) {
    $query = "SELECT subjects FROM results1 WHERE reg_number = ? AND class = ? AND term = ? AND session = ? AND subject_total < 40";
    $stmt = $connection->prepare($query);
    $stmt->bind_param("ssss", $reg_number, $class, $term, $session);
    $stmt->execute();
    $result = $stmt->get_result();

    $subjects = [];
    while ($row = $result->fetch_assoc()) {
        $subjects[] = $row['subjects'];
    }

    return $subjects;
}
function abbreviateName($name) {
    $trimmed = trim($name);
    $wordCount = str_word_count($trimmed);
    $length = strlen($trimmed);

    if ($wordCount >= 3 && $length > 25) {
        $parts = explode(' ', $trimmed);
        return $parts[0] . ' ' . strtoupper(substr($parts[1], 0, 1)) . '. ' . $parts[2];
    }
    return $trimmed;
}

?>

