We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit dc35ee73 authored by idflood's avatar idflood

added basic stats page ( derived from tagcloud )

parent b73e0513
<?php
/* Libre.fm -- a free network service for sharing your music listening habits
Copyright (C) 2009 Libre.fm Project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once($install_path . '/database.php');
require_once($install_path . '/data/Server.php');
class PlayStats {
/*
* returns an array counting appareances of a given field and his corresponding bargraph size
* @param string $table table name to be queried
* @param string $field field name to count
* @param integer $limit limit of the query
* @param string $constraint username or artistname depending on field
* @param integer $maxwidth bargraph max width ( to express visually the number of plays )
* inaccurate @param integer $sizes quantity of possible sizes
* inaccurate @param float $max_font_size maximum font size (px, em, %, etc)
* @return array playstats
*/
static function generatePlayStats($table, $field, $limit = 40, $constraint = null, $maxwidth = 100 ) {
global $mdb2;
if (!is_string($field)) return false;
if (!is_string($table)) return false;
if (!is_integer($limit)) return false;
$sizes = array('xx-large', 'x-large', 'large', 'medium', 'small', 'x-small', 'xx-small');
$query = "SELECT $field, count(*) AS count FROM $table";
$query .= (!is_null($constraint) || ($table == "Scrobbles")) ? ' WHERE ' : null;
if ($field == "track") {
$query .= (!is_null($constraint)) ? ' artist = ' . $mdb2->quote($constraint, 'text') : null;
} else {
$query .= (!is_null($constraint)) ? ' username = ' . $mdb2->quote($constraint, 'text') : null;
}
$query .= (!is_null($constraint) && ($table == "Scrobbles")) ? ' AND ' : null;
$query .= ($table == "Scrobbles") ? " rating <> 'S' " : null;
$query .= " GROUP BY $field ORDER BY count DESC LIMIT $limit";
$res = $mdb2->query($query);
if (PEAR::isError($res)) {
echo("ERROR" . $res->getMessage());
}
if (!$res->numRows()) {
return false;
} else {
$data = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
$max = $data[0]['count'];
foreach($data as &$i){
$i['pageurl'] = Server::getArtistURL($i['artist']);
$i['size'] = $i['count'] / $max * $maxwidth;
}
return $data;
}
}
}
?>
<?php
/* Libre.fm -- a free network service for sharing your music listening habits
Copyright (C) 2009 Libre.fm Project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once('database.php');
require_once('templating.php');
require_once('data/User.php');
require_once('data/TagCloud.php');
require_once('data/PlayStats.php');
if(!isset($_GET['user']) && $logged_in == false) {
$smarty->assign('error', 'Error!');
$smarty->assign('details', 'User not set! You shouldn\'t be here!');
$smarty->display('error.tpl');
die();
}
$user = new User($_GET['user']);
if(isset($user->name)) {
$smarty->assign("id", $user->id);
$smarty->assign("acctid", $user->acctid);
$smarty->assign('user', $user->name);
$smarty->assign('homepage', $user->homepage);
$smarty->assign('location', $user->location);
$smarty->assign('location_uri', $user->location_uri);
$aUserScrobbles = $user->getScrobbles(20);
if (!PEAR::isError ($aUserScrobbles)) {
$smarty->assign('scrobbles', $aUserScrobbles);
}
$smarty->assign('userlevel', $user->userlevel);
$smarty->assign('avatar', $user->getAvatar());
$aUserNowPlaying = $user->getNowPlaying(10);
if (!PEAR::isError ($aUserNowPlaying)) {
$smarty->assign('nowplaying', $aUserNowPlaying);
}
$aUserTagCloud = TagCloud::GenerateTagCloud('Scrobbles', 'artist', 40, $user->name);
if (!PEAR::isError ($aUserTagCloud)) {
$smarty->assign('user_tagcloud',$aUserTagCloud);
}
$smarty->assign('stat_barwidth', 300);
$aUserPlayStat = PlayStats::GeneratePlayStats('Scrobbles', 'artist', 40, $user->name, 300);
if (!PEAR::isError ($aUserPlayStat)) {
$smarty->assign('user_playstats',$aUserPlayStat);
}
$smarty->assign('isme', ($_SESSION['user']->name == $user->name));
$smarty->assign('stats', true);
$smarty->display('stats.tpl');
} else {
$smarty->assign('error', 'User not found');
$smarty->assign('details', 'Shall I call in a missing persons report?');
$smarty->display('error.tpl');
}
?>
......@@ -241,3 +241,11 @@ ul#albums dl dd.description {
/* IE */
filter: alpha(opacity=85);
}
/* stats */
.stats_artists{ width: 100%; font-size: 98%; }
.stats_artists td{ border: 1px solid #ddd; }
.stats_artists .artist{ width: 300px; }
.stats_artists .bar{ width: 300px; }
.stats_artists .bar div{ background: #970102; height: 1em; }
.stats_artists .counts{ text-align: right; }
{include file='header.tpl'}
<h2 property="dc:title">{$user|escape:'html':'UTF-8'}'{if $user|substr:-1 != 's'}s{/if} profile</h2>
<div about="{$id|escape:'html':'UTF-8'}" typeof="foaf:Agent" class="user vcard">
<div class="avatar" rel="foaf:depiction">
<!-- Avatar placeholder -->
<img src="{$avatar|escape:'html':'UTF-8'}" alt="avatar" class="photo" />
</div>
{if $isme}
<a class="edit" href="{$base_url}/edit_profile.php">[edit]</a>
{/if}
<dl>
<dt>
<span class="fn" property="foaf:name">{$fullname|escape:'html':'UTF-8'}</span>
<span rel="foaf:holdsAccount" rev="sioc:account_of">
<span about="{$acctid|escape:'html':'UTF-8'}" typeof="sioc:User">
(<span class="nickname" property="foaf:accountName">{$user|escape:'html':'UTF-8'}</span>)
<span rel="foaf:accountServiceHomepage" resource="{$base_url}"></span>
<span rel="foaf:accountProfilePage" rev="foaf:topic" resource=""></span>
</span>
</span>
</dt>
{if $homepage}
<dd>
<a href="{$homepage|escape:'html':'UTF-8'}" rel="me foaf:homepage" class="url">{$homepage|escape:'html':'UTF-8'}</a>
</dd>
{/if}
<dd rel="foaf:based_near">
<span{if $location_uri} about="{$location_uri|escape:'html':'UTF-8'}"{/if} class="label" property="rdfs:comment">{$location|escape:'html':'UTF-8'}</span>
</dd>
<dd class="note" property="bio:olb">{$bio|escape:'html':'UTF-8'}</dd>
</dl>
<hr style="border: 1px solid transparent; clear: both;" rel="foaf:page" rev="foaf:primaryTopic" resource="" />
</div>
<h3>{$user}'{if $user|substr:-1 != 's'}s{/if} most played artists</h3>
<table class="stats_artists" about="{$id}">
{section name=i loop=$user_playstats}
<tr><td class="counts">{$user_playstats[i].count}</td><td class="bar" style="width: {$stat_barwidth}px"><div style="width:{$user_playstats[i].size}px" class="artist"></div></td><td><a
href="{$user_playstats[i].pageurl|escape:'html':'UTF-8'}" rel="{if $user_playstats[i].size|substr:-5 ==
'large'}foaf:interest {/if}tag">{$user_playstats[i].artist|escape:"html":"UTF-8"}</a></td>
{/section}
</table>
<!-- Column break -->
</div></div><div class="yui-u" id="sidebar"><div style="padding: 10px;">
<h3>{$user}'{if $user|substr:-1 != 's'}s{/if} statistics</h3>
<p><strong>More coming soon</strong></p>
{include file='footer.tpl'}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment