<?php

// Config
//
// This is the ID of our team, if you uset his script, replace this
// number with your team ID
$teamId=1053;

// How long to cache API responses (in seconds)
$cacheTime 300;

// ----------------------------------------------------------------------------

function callApi($url) {
    
    global 
$cacheTime;
    
    
$urlHash=md5($url);
    
$cTime 0;
    
    
// find the creation time of the cache file if it exists
    
if (file_exists('cache/'.$urlHash)) {
        
$cTime filemtime('cache/'.$urlHash); 
    }
    
    
// is the cache still fresh?
    
if ($cTime time() - $cacheTime ) {
        
// yes it is, use the cached response
        
$xmlFile file_get_contents('cache/'.$urlHash);
    } else {
        
// no it isn't, refresh from API
        
$xmlFile file_get_contents("http://api.kivaws.org/".$url);
        if (!empty(
$xmlFile)) {
            
file_put_contents('cache/'.$urlHash$xmlFile);
        }
    }
    
    
$xml simplexml_load_string($xmlFile);
    if (empty(
$xml)) {
        return 
false;
    } else {
        return 
$xml;
    }
}

// load the team data from Kiva webservice
$teamInfo callApi("v1/teams/{$teamId}.xml");

// load all lenders data
$currentPage 1;
$lenders callApi("v1/teams/{$teamId}/lenders.xml?page={$currentPage}");
    
foreach (
$lenders->lenders->lender as $lender) {
        
$lenderArray[(string)$lender->lender_id]['name']=(string)$lender->name;
        
$lenderArray[(string)$lender->lender_id]['whereabouts']=(string)$lender->whereabouts;
        
$lenderArray[(string)$lender->lender_id]['country_code']=(string)$lender->country_code;
        
$lenderArray[(string)$lender->lender_id]['name']=(string)$lender->name;
    }


$lenderSlices=array_chunk($lenderArray50true);

foreach (
$lenderSlices as $lenderSlice) {
    
$lenderIds=implode(','array_keys($lenderSlice));
    
$lenderInfos=callApi("v1/lenders/{$lenderIds}.xml");
    foreach (
$lenderInfos->lenders->lender as $lender) {
        
$lenderArray[(string)$lender->lender_id]['member_since']=(string)$lender->member_since;
        
$lenderArray[(string)$lender->lender_id]['personal_url']=(string)$lender->personal_url;
        
$lenderArray[(string)$lender->lender_id]['loan_count']=(string)$lender->loan_count;
    }
}



if (
$teamInfo===false || $lenders===false) {
    die(
'Unfortunately, the Kiva web service did not respond.');
}

?>

<html>
<head>
<title>Czech Team on Kiva</title>
<style type="text/css">
body { font-family: georgia, serif; font-size: 2em; text-align: center; }
h1 { text-align: center; }
h1 a {color: black}
td { text-align: center; padding: 0.3em; font-size: 200%; color: #A61000}
td a {color: #A61000;}
.b { font-size: 800%; padding: 0.2em; margin: 0.2em; }
#ft { color: #202020; font-size: 0.5em}
#ft a { color: gray; }
.ftnote {font-size: 30%; color: gray}
table { margin: auto; }
</style>
</head>
<body>
<h1><a href="http://www.kiva.org/team/<?=$teamInfo->teams->team->shortname?>"><?=$teamInfo->teams->team->name?></a> on <a href="http://www.kiva.org/">Kiva</a></h1>
<table>
<tr>
<td class="b" style="background-color: #65E080"><?php echo $teamInfo->teams->team->member_count?></td>
<td class="b" style="background-color: #00C12B"><?php echo $teamInfo->teams->team->loan_count?></td>
<td class="b" style="background-color: #24913C">$<?php echo number_format($teamInfo->teams->team->loaned_amount); ?></td>
</tr>
<tr>
<td style="background-color: #65E080"><a href="http://www.kiva.org/community/viewTeamMembers/?team_id=<?=$teamId?>">members</a></td>
<td style="background-color: #00C12B"><a href="http://www.kiva.org/community/viewTeamLoans/?team_id=<?=$teamId?>">loans</a></td>
<td style="background-color: #24913C">loaned</td>
</tr>
<tr>
<td colspan="3">
<table>
<tr>
<td style="width: 50%">
<h3>Top lenders</h3>

<ol>
<?php 
function loansort($a1$a2) {
    return 
intval($a2['loan_count'])-intval($a1['loan_count']);
}

$sortedLenders $lenderArray;
uasort($sortedLenders,"loansort");

$i=10;
foreach (
array_slice($sortedLenders,0,10) as $lender_id=>$lender) {
    
printf("<li style=\"font-size: %d%%\"><a href=\"http://www.kiva.org/lender/%s\">%s</a> (%s, %s) - %d loans</li>\n"30+5*$i$lender_id$lender['name'], $lender['whereabouts'], $lender['country_code'], $lender['loan_count']);
    
$i--;
}

?>
</ol>
<p class="ftnote">(Why is the sum of the above lender's loans higher than the number of team loans? Because not all loans count toward this team stats, that's why.)</p>
</td>
<td style="width: 50%">
<h3>New members</h3>
<ol>
<?php 
function timesort($a1$a2) {
    if (
$a2['member_since']==$a1['member_since']) {
        return 
0;
    }
    if (
$a2['member_since']>$a1['member_since']) {
        return 
1;
    } else {
        return -
1;
    }
}

$sortedLenders $lenderArray;
uasort($sortedLenders,"timesort");

$i=10;
foreach (
array_slice($sortedLenders,0,10) as $lender_id=>$lender) {
    
printf("<li style=\"font-size: %d%%\"><a href=\"http://www.kiva.org/lender/%s\">%s</a> (%s, %s)</li>\n"30+5*$i$lender_id$lender['name'], $lender['whereabouts'], $lender['country_code']);
    
$i--;
}

?>
</ol>
</td>
</tr>
</table>
</td>
</tr>
</table>
<div id="ft">2009 &copy; <a href="http://www.taborsky.cz">mt</a> &middot; <a href="http://www.kiva.org/about/how">learn more</a> about Kiva.org, <a href="http://www.kiva.org/community/joinTeam_process?team_id=<?=$teamId?>">join us</a> and start helping people who want to help themselves. built using <a href="http://build.kiva.org/">kiva api</a>, <a href="src.php">wanna see the code?</a>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-360287-1");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>