<?php
//include ('connection.php');
//$tiff = json_encode(getGrades());
//echo $tiff;
function getGrades($user=null){
    $query = "SELECT * FROM `grade` ";
    $db = getConnection();
    $result = mysqli_query($db, $query);

    $allgrades = array();
    
    foreach($result as $class){
        $grade = $class['grade'];
        $gradeID = $class['gradeId'];

        //echo $gradeID;
        $learningAreas = find_learningAreas($gradeID);

        $allsubjects = array();

        foreach($learningAreas as $learning){
            $subjects = $learning['subject'];
            $subjectID = $learning['subjectID'];

            $strands = strands($subjectID);
            $allstrands = array();

            foreach($strands as $strand){
                $topic = $strand['strand'];
                $strandID = $strand['strandID'];

                //$url = $strand['downloadUrl'];
                $allsubstrand= array();

                $subtopics = sub_strands($strandID);
                foreach($subtopics as $subtopic){
                    $substrand=$subtopic['substrand'];
                    $substrandID=$subtopic['subStrandID'];
                    $url = $subtopic['downloadUrl'];

                    $response = array(
                    "substrand" => "$substrand",
                    "substrandID" =>"$substrandID",
                    "zipUrl" =>"$url",
                    );

                    array_push($allsubstrand, $response);

                }
                $response1 = array(
                    "topic" => "$topic",
                    "strandID" =>"$strandID",
                    "subStrands" =>$allsubstrand,
                    );
                    array_push($allstrands, $response1); 
            }

            $response2 = array(
                "subject" => "$subjects",
                "subjectID" =>"$subjectID",
                "strands" =>$allstrands,
                );
                array_push($allsubjects, $response2); 

        }
        $response3 = array(
            "grade" => "$grade",
            "gradeID" =>"$gradeID",
            "subjects" =>$allsubjects,
            );
            array_push($allgrades, $response3);


    }
        return $allgrades;

    }
    function find_learningAreas($gradeID){
        $query = "SELECT * FROM `subjects` Where `gradeID`='$gradeID'";
        $db = getConnection();
        $result = mysqli_query($db, $query);
        return $result;
    }
    function strands($subjectID){
        $query = "SELECT * FROM `strands` Where `subjectID`='$subjectID'";
        $db = getConnection();
        $result = mysqli_query($db, $query);
        return $result;
    }
    function sub_strands($strandID){
        $query = "SELECT * FROM `subStrands` Where `strandID`='$strandID'";
        $db = getConnection();
        $result = mysqli_query($db, $query);
        return $result;
    }

?>