function calendarservice($now, $startYear, $startMonth, $startDay, $endYear, $endMonth, $endDay, $params = null)
{
    date_default_timezone_set('America/New_York');
    $endpoint = "https://www.googleapis.com/calendar/v3/calendars/7407kg1hjqo90so89cenlm25ns%40group.calendar.google.com/events?&key=AIzaSyCHiMDmvdOicdMICskRep27PyCoGNvjz6w&orderBy=startTime&singleEvents=true&timeZone=America%2FNew_York";
    $start = TimeOffsetFromNow($startYear, $startMonth, $startDay, $now);
    $end = TimeOffsetFromNow($endYear, $endMonth, $endDay, $now);
    $url = $endpoint . "&timeMin=" . urlencode(date(DateTime::ISO8601, $now + $start)) . "&timeMax=" . urlencode(date(DateTime::ISO8601, $now + $end)) . $params;
    $respjson = file_get_contents($url);
    $resp = json_decode($respjson, true);
    $events = array();
    foreach ($resp['items'] as $item) {
        $summary = $item['summary'];
        $time = "";
        if (array_key_exists('date', $item['start'])) {
            $ts = new DateTime($item['start']['date']);
            $time = date("l, F jS", $ts->getTimestamp());
        } else {
            if (array_key_exists('dateTime', $item['start'])) {
                $ts_start = new DateTime($item['start']['dateTime']);
                $ts_end = new DateTime($item['end']['dateTime']);
                $time = date('l, F jS f\\r\\o\\m g:i a ', $ts_start->getTimestamp()) . date('\\t\\o g:i a', $ts_end->getTimestamp());
            }
        }
        $location = array_key_exists("location", $item) ? $item['location'] : "TBD";
        array_push($events, array("summary" => $summary, "time" => $time, "location" => $location));
    }
    return $events;
}
function statsservice($stats)
{
    if (count($stats) == 0) {
        return $stats;
    }
    $conn = mysqlconnectionservice();
    $answers = array();
    foreach ($stats as $stat) {
        $querystring = null;
        // create a query string if appropriate and evaluate
        switch ($stat) {
            case 'count_actives':
                $querystring = 'SELECT COUNT(*) AS `number` FROM userroles WHERE roleid="active"';
                break;
            case 'count_actives_majors':
                $querystring = 'SELECT COUNT(DISTINCT(P.major)) AS `number` FROM profile AS P, userroles AS U WHERE P.userid = U.userid AND U.roleid="active"';
                break;
            case 'percent_female_active_brothers':
                // technically this can only be evaluated by mysql query BUT
                // hack for this one cause a single query is too complicated
                $answers[$stat] = getGirlPercentage($conn, $answers['count_actives']);
                break;
            case 'count_alumni':
                $querystring = 'SELECT COUNT(*) AS `number` FROM userroles WHERE roleid="alumni"';
                break;
            case 'count_alumni_companies':
                $querystring = 'SELECT COUNT(DISTINCT(J.company)) AS `number` FROM profile AS P, userroles AS U, jobs as J WHERE P.userid = U.userid AND P.userid=J.userid AND U.roleid="alumni"';
                break;
            case 'count_alumni_cities':
                $querystring = 'SELECT COUNT(DISTINCT(P.city)) AS `number` FROM profile AS P, userroles AS U WHERE P.userid = U.userid AND U.roleid="alumni"';
                break;
        }
        if ($querystring) {
            if ($result = $conn->query($querystring)) {
                $answers[$stat] = $result->fetch_object()->number;
            }
        } else {
            // not suitable for query, so it must be obtained by a custom function
            switch ($stat) {
                case 'chapter_age':
                    $answers[$stat] = floor(abs(TimeOffsetFromNow(1999, 4, 17, time())) / (60 * 60 * 24 * 365));
                    break;
                case 'chapter_number':
                    $answers[$stat] = 51;
                    break;
                case 'events_this_semester':
                    $thismonth = m();
                    $events = array();
                    $now = time();
                    if ($thismonth >= 1 && $thismonth <= 4) {
                        // winter
                        $events = calendarservice($now, y(), 1, 1, y(), 4, 30);
                    } else {
                        if ($thismonth >= 5 && $thismonth <= 7) {
                            // summer
                            $events = calendarservice($now, y(), 5, 1, y(), 7, 31);
                        } else {
                            // fall
                            $events = calendarservice($now, y(), 8, 1, y(), 12, 31);
                        }
                    }
                    $answers[$stat] = count($events);
                    break;
            }
        }
    }
    // END FOREACH (if stat was a known stat, it was set in answers)
    return $answers;
}