[TBA]: Total Points Put up by team and year

I wrote a simple function using the API to get the averaged or unaveraged points scored by teams and year.
(Excessive unnecessary comments…)


function getScore($teamnumber, $year, $average = FALSE)
{
    $competitions = array(); //Contains TBA assigned "eventid"'s for events attended by team X
    $score = 0; //Initialization of $score. Not sure it's entirely neccisary
    $events = make_array(get_events(NULL, $year, NULL)); //Assigns array of events that occured in designated $year to $events
    foreach ($events as $event){ //breakdown of each $event within $events
        if($event"week"] < 90){ //If event is NOT champs OR off-season
            $attendance = make_array(get_attendance($event"eventid"], $teamnumber)); //Lookup of $teamnumber within the $event
            if(count($attendance) > 0) //If the array has data
            {
                $competitions] = $event"eventid"]; //Add eventid to array of competitions attended
            }
        }
        else{ //Otherwise
           //Do nothing
        }
    }
    foreach($competitions as $compID){ //Breakdown of each $compID (eventid) withing $competitions
        $matches = make_array(get_matches($teamnumber, $compID)); //lookup matched for each team at the event
    }
    $record = make_array(get_official_record($teamnumber, $year)); //official record of team by $year
    foreach ($matches as $match){ //breakdown of $match in $matches
        $matchid = $match"matchid"]; //assigns matchid to a variable
        if($match"red1"] == $teamnumber || $match"red2"] == $teamnumber || $match"red3"] == $teamnumber){ //determines if the team is on the red alliance
            $alliance = "red"; //Assigns alliance variable an alliance name
        }
        else{ //if they weren't red
            $alliance = "blue"; //must be blue
        }
        if($alliance == "red"){ //If alliance is RED
            $score += $match"redscore"]; //add redscore for specific match to the total score
        }
        else{ //If alliance is blue
            $score += $match"bluescore"]; //add bluescore for specific match to the total score
        }
    }
    if($average){ //if parameter $average is set to TRUE
    $average_score = round($score/count($matches), 0); //round the amount of the total score over total matches in question
    }
    else{ //If parameter $average is set to FALSE [DEFAULT]
    $average_score = $score; //$average_score is the total score
    }
    return $average_score; //output $average_score
}

When i call it as getScore(330, 2007); or getScore(330, 2007, TRUE); (or even 2006 instead of 2007) and It outputs a score. The score doesn’t match up with my manual calculation (2007 unaveraged should be 3,000+ and only returns around 870). If I change the year to 2008 however, it returns 0 (or if I remove the $score initialization line, outputs nothing). Someone suggested that maybe its trying to grab data from 2008 champs and therefore throwing off the data. However, I figured since no matches were listed for champs it most likely wasn’t the issue. Nonetheless I added the check for which “week” the events were in, if (week < 90){

Any ideas how to resolve this issue?