function getDoaramaURL($league_nm, $date)
{
    //TODO, could get all doaramas at once eventually to optimize
    $league_key = $league_nm . "-" . $date;
    //TODO: replace with your own DB implementation
    //Getting our doarama key for the given date
    $conn = initDB();
    $sql = "SELECT doarama_key FROM doaramas WHERE league_key = ?";
    $rows = run_stmt_query($conn, $sql, "s", array($league_key));
    $doarama_key = $rows[0]['doarama_key'];
    // Get names for activity, very important to sort by date_created to ensure that the names match the track logs
    $sql = "SELECT u.name\n\t\t\t\tFROM user_activity ua\n\t\t\t\tJOIN user u ON (u.user_id = ua.user_id)\n\t\t\t\tWHERE ua.league_key = ?\n\t\t\t\tORDER BY ua.date_created";
    $rows = run_stmt_query($conn, $sql, "s", array($league_key));
    $url = "http://api.doarama.com/api/0.2/visualisation?k={$doarama_key}";
    $url .= "&avatarBaseUrl=http%3A%2F%2Fwww.yourwebsite.com%2Favatar%2F";
    $url .= "&dzml=http%3A%2F%2Fwww.yourwebsite.com%2Ftask.dzml";
    foreach ($rows as $row) {
        $name = $row["name"];
        $name = ucwords(strtolower($name));
        $nameParts = preg_split("/ /", $name);
        $formattedName = "unknown";
        //Take a name like 'aaron price' and convert it to 'AaronP'
        if (count($nameParts) == 1) {
            $formattedName = $nameParts[0];
        } elseif (count($nameParts) > 1) {
            $formattedName = $nameParts[0] . $nameParts[1][0];
        }
        $url .= "&name={$formattedName}";
    }
    return $url;
}
function uploadTrack($track_filepath, $user_id, $task_date)
{
    //Upload track to Doarama (create new activity)
    $data = array('gps_track' => "@{$track_filepath}");
    $response = doCurl("activity", $data, $user_id);
    $data = json_decode($response, true);
    $activity_id = $data["id"];
    //Next set activity info https://api.doarama.com/api/0.2/activityType
    /* 27	Fly - Hang Glide
       28	Fly - Sailplane / Glider
       29	Fly - Paraglide	*/
    $data = '{"activityTypeId":29}';
    //TODO: refactor to not just be paragliding
    doCurl("activity/{$activity_id}", $data, $user_id);
    //TODO: replace with your own DB initialization.  I set mine to transactional since I don't want to populate
    //it with bad entries if the track upload fails.
    $conn = initDB();
    $conn->autocommit(FALSE);
    //Create a key for our local DB that we can use to link to a visualization.
    //I use a key that given a date and a competition name will map to one specific visualization for that day
    $league_key = "socal-{$task_date}";
    //TODO: factor out for other leagues.
    //Create an entry in the DB that maps a doarama activity_id to a unique_user_id in your local DB
    //Since naming pilots in the API is done in the order of upload, if you want to name the tracks with
    //user names make sure to be able to query your DB for activities that map to the end visualization
    //and can be sorted in the order they were created.  I've setup my DB to populate date_created automatically
    $sql = "INSERT INTO user_activities (USER_ID, ACTIVITY_ID, LEAGUE_KEY) VALUES (?, ?, ?)";
    $types = "iis";
    $params = array($user_id, $activity_id, $league_key);
    insert_row($conn, $sql, $types, $params);
    //TODO replace with your own DB implementation
    //TODO: update with your own DB implementation
    //Try to find the doarama_key in our DB from our league_key, if we don't get any results it means we haven't
    //yet created a visualization so we'll need to make one and save the Doarama Key in our DB
    $sql = "SELECT doarama_key FROM doaramas WHERE league_key = ?";
    $rows = run_stmt_query($conn, $sql, "s", array($league_key));
    if (count($rows) < 1) {
        // Create Visualization
        $data = '{"activityIds":[' . $activity_id . ']}';
        $response = doCurl("visualisation", $data, $user_id);
        //get doarama_key
        $data = json_decode($response, true);
        $doarama_key = $data["key"];
        //TODO: update with your own DB implementation
        //insert into DB
        $sql = "INSERT INTO doaramas (league_key, doarama_key) VALUES (?, ?)";
        $types = "ss";
        $params = array($league_key, $doarama_key);
        insert_row($conn, $sql, $types, $params);
    } else {
        $doarama_key = $rows[0]['doarama_key'];
        // Add activity to visualization
        $data = '{"visualisationKey" : "' . $doarama_key . '", "activityIds": [' . $activity_id . ']}';
        doCurl("visualisation/addActivities", $data, $user_id);
    }
    //TODO: update with your DB implementation
    //all done now, can commite the transaction
    $conn->commit();
    $conn->close();
}
/*
 * Defaults exists for users who haven't created avatars yet
 * I pick defaults by doing the following: user_id%100 = default_id
 *
 * I decided to group user avatars into folders of 100.  I get the foler ID by doing floor(user_id/100) = folderID
 * From there I just reference the user_id direclty as '<user_id>.jpg'.  The whole path for user_id 55 would be:
 * /root/avatars/0/55.jpg
 */
$activity_id = $_GET["activity_id"];
//TODO: replace with your own DB implementation
//Getting the user_id from the DB based on activity_id provided by Doarama request
$conn = initDB();
$sql = "SELECT user_id FROM user_activities WHERE activity_id = ?";
$params = array($activity_id);
$types = "i";
$rows = run_stmt_query($conn, $sql, $types, $params);
$user_id = $rows[0]["user_id"];
$folder_id = floor($user_id / 100);
$avatar_path = "/filesystem_root/avatars/{$folder_id}/{$user_id}.jpg";
if (!file_exists($avatar_path)) {
    $default_id = $user_id % 100;
    $avatar_path = "/filesystem_root/defaults/{$default_id}.jpg";
    if (!file_exists($avatar_path)) {
        error_log('Could not find default avatar at $avatar_path');
        exit;
    }
}
$fp = fopen($avatar_path, 'rb');
// send the right headers
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($avatar_path));