<?php

function checkBranchClassSubject($conn_rjllearning_specific, $branch_id, $class_id, $subject_id, $institution_id)
{
 
    if ($branch_id === "all") {
        // If branch_id is "all", directly return true
        // return true;
    } else {
       
        $sql_branch = "SELECT * from branch where id = ? AND institution_id = ?";
        $stmt_branch =  $conn_rjllearning_specific->prepare($sql_branch);
        $stmt_branch->execute(array($branch_id,$institution_id));
        $result_branch = $stmt_branch->fetchAll();
        if (count($result_branch) != 1) {
            errorreturn('Branch does not exist in the specified institution.');
        }
    }

    if ($class_id == 'all') {
    } else {
       
        if ($branch_id == 'all') {
        } else {
            if($class_id !=  null){
              
            $sql_branch_class = "SELECT * from class_info where id = ? AND institution_id = ? AND (branch_id LIKE ? OR branch_id = 'all')";
            $stmt_branch_class =  $conn_rjllearning_specific->prepare($sql_branch_class);
            $stmt_branch_class->execute(array($class_id, $institution_id, '%' . $branch_id . '%'));
            $result_branch_class = $stmt_branch_class->fetchAll();
            if (count($result_branch_class) != 1) {
                errorreturn('Class does not exist in the specified branches.');
            }
            }

            if ($subject_id !=  null) {
            $sql_class_subject = "SELECT * from subject_info where id = ? AND institution_id = ? AND class_id= ?";
            $stmt_class_subject =  $conn_rjllearning_specific->prepare($sql_class_subject);
            $stmt_class_subject->execute(array($subject_id, $institution_id, $class_id));
            $result_class_subject_class = $stmt_class_subject->fetchAll();
            if (count($result_class_subject_class) != 1) {
                errorreturn('Subject does not exist in the specified class.');
            }
        }
    }
    }
    return true;
}