$template = 'statistics_table.tpl';
$u = isset($_GET['u']) && is_numeric($_GET['u']) ? intval($_GET['u']) : $USER->user_id;
$cacheid = 'viewdirection.' . $u;
$smarty->caching = 2;
if (isset($_GET['refresh']) && $USER->hasPerm('admin')) {
    $smarty->clear_cache($template, $cacheid);
}
if (!$smarty->is_cached($template, $cacheid)) {
    $db = NewADOConnection($GLOBALS['DSN']);
    $table = array();
    $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
    $images = $db->getAll("\n\tSELECT\n\t\tgridimage_id\n\tFROM\n\t\tgridimage \n\tWHERE\n\t\tuser_id = {$u}\n\tAND \n\t\tmoderation_status in ('geograph','accepted')\n\tAND\n\t\tviewpoint_eastings > 0 AND viewpoint_northings > 0\n\tAND\n\t\tviewpoint_eastings != nateastings AND viewpoint_northings != natnorthings\n\tLIMIT\n\t\t100\n\t");
    foreach ($images as $i => $row) {
        $image = new GridImage();
        $image->LoadFromId($row['gridimage_id']);
        $image->compact();
        $line = array();
        $line['Square'] = $image->grid_reference;
        $line['Title'] = "<a href=\"/photo/{$row['gridimage_id']}\">{$image->title}</a>";
        $submore4 = $image->grid_square->nateastings > 0;
        $line['Photographer'] = $image->getPhotographerGridref();
        $line['Subject'] = $image->getSubjectGridref();
        $promore4 = $image->photographer_gridref_precision < 1000;
        //mimic the behaviour doen by getNatEastings to place the location in the center of the 1km square
        if (!$promore4) {
            $image->viewpoint_eastings += 500;
            $image->viewpoint_northings += 500;
        }
        $dist = sqrt(pow($image->grid_square->nateastings - $image->viewpoint_eastings, 2) + pow($image->grid_square->natnorthings - $image->viewpoint_northings, 2));
        $line['Distance'] = sprintf("%0.3f", $dist / 1000);
        #$line['promore4'] = $promore4;