Add public ranking listing, link charts better
[disclosr.git] / ranking.php
blob:a/ranking.php -> blob:b/ranking.php
<?php <?php
include_once('include/common.inc.php'); include_once('include/common.inc.php');
include_header('Open Gov Rankings'); include_header('Open Gov Rankings');
$db = $server->get_db('disclosr-agencies'); $db = $server->get_db('disclosr-agencies');
?> ?>
<div class="foundation-header"> <div class="foundation-header">
<h1><a href="about.php">Open Government Rankings</a></h1> <h1><a href="about.php">Open Government Rankings</a></h1>
<h4 class="subheader"></h4> <h4 class="subheader"></h4>
</div> </div>
<table> <table>
<?php <?php
$agenciesdb = $server->get_db('disclosr-agencies'); $agenciesdb = $server->get_db('disclosr-agencies');
//$docsdb = $server->get_db('disclosr-documents'); //$docsdb = $server->get_db('disclosr-documents');
$scoredagencies = Array(); $scoredagencies = Array();
$scores = Array(); $scores = Array();
$columnKeys = Array(); $columnKeys = Array();
   
try { try {
$rows = $agenciesdb->get_view("app", "all", null, true)->rows; $rows = $agenciesdb->get_view("app", "all", null, true)->rows;
   
   
if ($rows) { if ($rows) {
foreach ($rows as $row) { foreach ($rows as $row) {
$columns = Array(); $columns = Array();
foreach ($row->value as $key => $value) { foreach ($row->value as $key => $value) {
if ((strstr($key, "has") || strstr($key, "URL")) && $key != "rtkURLs") { if ((strstr($key, "has") || strstr($key, "URL")) && $key != "rtkURLs") {
//echo "$key<br>"; //echo "$key<br>";
$columns[$key] = $value; $columns[$key] = $value;
} }
} }
//print_r(array_keys($columns)); //print_r(array_keys($columns));
$columnKeys = array_unique(array_merge($columnKeys, array_keys($columns))); $columnKeys = array_unique(array_merge($columnKeys, array_keys($columns)));
//print_r($columnKeys); //print_r($columnKeys);
$score = count($columns); $score = count($columns);
$scores[$score]++; if (isset($scores[$score])){
$scoredagencies[] = Array("id"=> $row->key, "website"=> $row->value->website, "name" => $row->value->name, "columns" => $columns, "score" => $score); $scores[$score]++;
  } else {
  $scores[$score] =1;
  }
  $scoredagencies[] = Array("id"=> $row->key, "website"=> (isset($row->value->website)?$row->value->website:""), "name" => $row->value->name, "columns" => $columns, "score" => $score);
} }
} }
   
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
function cmp($a, $b) function cmp($a, $b)
{ {
if ($a['score'] == $b['score']) { if ($a['score'] == $b['score']) {
return strcmp($a['name'], $b['name']); return strcmp($a['name'], $b['name']);
} }
return ($a['score'] > $b['score']) ? -1 : 1; return ($a['score'] > $b['score']) ? -1 : 1;
} }
   
usort($scoredagencies, "cmp"); usort($scoredagencies, "cmp");
echo "<tr>"; echo "<tr>";
echo "<th>Agency Name</th>"; echo "<th>Agency Name</th>";
echo "<th>Score</th>"; echo "<th>Score</th>";
foreach ($columnKeys as $columnID) { foreach ($columnKeys as $columnID) {
echo "<th>" . (isset($schemas['agency']["properties"][$columnID]['x-title']) ? $schemas['agency']["properties"][$columnID]['x-title'] : "<i>$columnID</i>") . "</th>"; echo "<th>" . (isset($schemas['agency']["properties"][$columnID]['x-title']) ? $schemas['agency']["properties"][$columnID]['x-title'] : "<i>$columnID</i>") . "</th>";
} }
echo "</tr>"; echo "</tr>";
foreach ($scoredagencies as $scoredagency) { foreach ($scoredagencies as $scoredagency) {
echo "<tr>"; echo "<tr>";
echo "<td><b><a href='getAgency.php?id=" . $scoredagency['id'] . "'>". $scoredagency['name'] . "</a></b></td>"; echo "<td><b><a href='getAgency.php?id=" . $scoredagency['id'] . "'>". $scoredagency['name'] . "</a></b></td>";
echo "<td><b>" . $scoredagency['score'] . "</b></td>"; echo "<td><b>" . $scoredagency['score'] . "</b></td>";
foreach ($columnKeys as $key) { foreach ($columnKeys as $key) {
echo "<td style='text-align: center;'>"; echo "<td style='text-align: center;'>";
if (isset($scoredagency['columns'][$key])) { if (isset($scoredagency['columns'][$key])) {
$value = $scoredagency['columns'][$key]; $value = $scoredagency['columns'][$key];
if (is_array($value)) { if (is_array($value)) {
if (count($value) == 1) { if (count($value) == 1) {
$href = $value[0]; $href = $value[0];
} else { } else {
$href = $value[0]; $href = $value[0];
} }
   
} else { } else {
$href = $value; $href = $value;
} }
if ($href[0] == "@") { if (isset($href[0]) && $href[0] == "@") {
$href = str_replace("@","https://twitter.com/",$href); $href = str_replace("@","https://twitter.com/",$href);
} }
//$href= urlencode($href); //$href= urlencode($href);
   
echo "<font color='lightgreen'>"; echo "<font color='lightgreen'>";
   
if (strstr($href, "http")) { if (strstr($href, "http")) {
echo "<a title='Yes' href='$href' style='color:lightgreen;'>&check;</a>"; echo "<a title='Yes' href='$href' style='color:lightgreen;'>&check;</a>";
} else { } else {
echo "&check;"; echo "&check;";
} }
   
echo "</font>"; echo "</font>";
} else { } else {
echo "<font color='orange'><abbr title='No'>✘</abbr></font>"; echo "<font color='orange'><abbr title='No'>✘</abbr></font>";
} }
echo "</td>"; echo "</td>";
} }
echo "</tr>\n"; echo "</tr>\n";
} }
?> ?>
</table><br> </table><br>
<div id="criteria" style="width:500px;height:900px;"></div> <div id="criteria" style="width:500px;height:900px;"></div>
<div id="scores" style="width:900px;height:500px;"></div> <div id="scores" style="width:900px;height:500px;"></div>
<script id="source"> <script id="source">
window.onload = function () { window.onload = function () {
$(document).ready(function () { $(document).ready(function () {
var d1 = []; var d1 = [];
var scorelabels = []; var scorelabels = [];
<?php <?php
try { try {
$rows = $db->get_view("app", "scoreHas?group=true", null, true)->rows; $rows = $db->get_view("app", "scoreHas?group=true", null, true)->rows;
   
   
$dataValues = Array(); $dataValues = Array();
foreach ($rows as $row) { foreach ($rows as $row) {
$dataValues[$row->value] = $row->key; $dataValues[$row->value] = $row->key;
} }
$i = 0; $i = 0;
ksort($dataValues); ksort($dataValues);
foreach ($dataValues as $value => $key) { foreach ($dataValues as $value => $key) {
   
echo " d1.push([$value, $i]);" . PHP_EOL; echo " d1.push([$value, $i]);" . PHP_EOL;
echo " scorelabels.push('$key');" . PHP_EOL; echo " scorelabels.push('$key');" . PHP_EOL;
$i++; $i++;
} }
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
?> ?>
function scoretrackformatter(obj) { function scoretrackformatter(obj) {
if (scorelabels[Math.floor(obj.y)]) { if (scorelabels[Math.floor(obj.y)]) {
return (scorelabels[Math.floor(obj.y)]) + "=" + obj.x; return (scorelabels[Math.floor(obj.y)]) + "=" + obj.x;
   
} else { } else {
return ""; return "";
} }
} }
   
function scoretickformatter(val, axis) { function scoretickformatter(val, axis) {
if (scorelabels[Math.floor(val)]) { if (scorelabels[Math.floor(val)]) {
return (scorelabels[Math.floor(val)]) ; return (scorelabels[Math.floor(val)]) ;
   
} else { } else {
return ""; return "";
} }
} }
   
Flotr.draw(document.getElementById("criteria"), [ Flotr.draw(document.getElementById("criteria"), [
{data: d1} {data: d1}
], { ], {
title: 'Total count of agencies with criteria', title: 'Total count of agencies with criteria',
HtmlText: true, HtmlText: true,
bars: { bars: {
show: true, show: true,
horizontal: true horizontal: true
}, },
mouse: { mouse: {
track: true, track: true,
relative: true, relative: true,
trackFormatter: scoretrackformatter trackFormatter: scoretrackformatter
}, yaxis: { }, yaxis: {
autoscaling: true, autoscaling: true,
minorTickFreq: 0.6, minorTickFreq: 0.6,
noTicks: scorelabels.length, noTicks: scorelabels.length,
tickFormatter: scoretickformatter tickFormatter: scoretickformatter
}, },
xaxis: { xaxis: {
autoscaling: true autoscaling: true
   
} }
}); });
   
var d2 = []; var d2 = [];
<?php <?php
try { try {
   
ksort($scores); ksort($scores);
foreach ($scores as $key => $value) { foreach ($scores as $key => $value) {
   
echo " d2.push([$key,$value]);" . PHP_EOL; echo " d2.push([$key,$value]);" . PHP_EOL;
$i++; $i++;
} }
} catch (SetteeRestClientException $e) { } catch (SetteeRestClientException $e) {
setteErrorHandler($e); setteErrorHandler($e);
} }
?> ?>
   
   
Flotr.draw(document.getElementById("scores"), [ Flotr.draw(document.getElementById("scores"), [
{data: d2} {data: d2}
], { ], {
title: 'Frequency distribution of Scores', title: 'Frequency distribution of Scores',
HtmlText: true, HtmlText: true,
bars: { bars: {
show: true show: true
}, },
mouse: { mouse: {
track: true, track: true,
relative: true relative: true
}, yaxis: { }, yaxis: {
autoscaling: true autoscaling: true
}, },
xaxis: { xaxis: {
autoscaling: true autoscaling: true
   
} }
}); });
   
}); });
}; };
</script> </script>
<?php <?php
include_footer(); include_footer();
?> ?>