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

Statistic.php 3.6 KB
Newer Older
1
<?php
2
/* GNU FM -- a free network service for sharing your music listening habits
3

4
   Copyright (C) 2009 Free Software Foundation, Inc
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

   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/>.

*/

21
require_once($install_path . '/database.php');
22 23
require_once($install_path . '/data/Server.php');

24
class Statistic {
elleo's avatar
elleo committed
25 26 27 28 29 30 31 32 33 34 35 36
	/*
	 * 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 ) {
clint's avatar
clint committed
37
		global $adodb;
clint's avatar
clint committed
38
		if (!is_string($field))          return false;
elleo's avatar
elleo committed
39 40
		if (!is_string($table))          return false;
		if (!is_integer($limit))         return false;
41
		$query = 'SELECT ' . $field . ', count(*) AS count FROM ' . $table;
elleo's avatar
elleo committed
42
		$query .= (!is_null($constraint)) ? ' WHERE ' : null;
43
		if ($field == 'track') {
clint's avatar
clint committed
44
			$query .= (!is_null($constraint)) ? ' artist = ' . $adodb->qstr($constraint) : null;
elleo's avatar
elleo committed
45
		} else {
46
			$query .= (!is_null($constraint)) ? ' userid = ' . ($constraint) : null;
elleo's avatar
elleo committed
47
		}
48
		$query .= ' GROUP BY ' . $field . ' ORDER BY count DESC LIMIT ' . $limit;
clint's avatar
clint committed
49 50 51
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		try {
			$res = $adodb->GetAll($query);
elleo's avatar
elleo committed
52
		}
clint's avatar
clint committed
53 54 55 56
		catch (exception $e) {
			echo('ERROR' . $e->getMessage());
		}
		if (!$res) {
elleo's avatar
elleo committed
57 58
			return false;
		} else {
clint's avatar
clint committed
59
			$max = $res[0]['count'];
60

clint's avatar
clint committed
61
			foreach($res as &$i){
elleo's avatar
elleo committed
62 63 64
				$i['pageurl'] = Server::getArtistURL($i['artist']);
				$i['size'] = $i['count'] / $max * $maxwidth;
			}
clint's avatar
clint committed
65

clint's avatar
clint committed
66
			return $res;
elleo's avatar
elleo committed
67 68
		}
	}
clint's avatar
clint committed
69

idflood's avatar
idflood committed
70
	static function generatePlayByDays($table, $limit = 100, $constraint = null, $maxwidth = 100 ) {
clint's avatar
clint committed
71
		global $adodb;
elleo's avatar
elleo committed
72
		global $connect_string;
clint's avatar
clint committed
73

elleo's avatar
elleo committed
74 75
		if (!is_string($table))          return false;
		if (!is_integer($limit))         return false;
clint's avatar
clint committed
76

elleo's avatar
elleo committed
77
		/*
clint's avatar
clint committed
78
		 * todo: completly remove this dirty db type check.
elleo's avatar
elleo committed
79
		 */
80 81
		$query = 'SELECT COUNT(*) as count, DATE(TO_TIMESTAMP(time)) as date FROM ' . $table;
		if( strpos($connect_string , 'mysql' ) === 0 ) $query = 'SELECT COUNT(*) as count,DATE(FROM_UNIXTIME(time)) as date FROM ' .  $table;
clint's avatar
clint committed
82

elleo's avatar
elleo committed
83
		$query .= (!is_null($constraint)) ? ' WHERE ' : null;
clint's avatar
yeesh  
clint committed
84
		$query .= (!is_null($constraint)) ? ' userid = ' . ($constraint) : null;
85
		$query .= ' GROUP BY date ORDER BY date DESC LIMIT ' . $limit;
clint's avatar
clint committed
86 87 88
		$adodb->SetFetchMode(ADODB_FETCH_ASSOC);
		try {
			$res = $adodb->GetAll($query);
idflood's avatar
idflood committed
89
		}
clint's avatar
clint committed
90 91 92 93
		catch (exception $e) {
			echo('ERROR' . $e->getMessage());
		}
		if (!$res) {
clint's avatar
clint committed
94
			return false;
elleo's avatar
elleo committed
95 96
		} else {
			$max = 0;
idflood's avatar
idflood committed
97

clint's avatar
clint committed
98
			foreach($res as &$i){
idflood's avatar
idflood committed
99
				if( $i['count'] > $max ) $max =  $i['count'];
elleo's avatar
elleo committed
100
			}
clint's avatar
clint committed
101

clint's avatar
clint committed
102
			foreach($res as &$i){
idflood's avatar
idflood committed
103
				$i['size'] = $i['count'] / $max * $maxwidth;
elleo's avatar
elleo committed
104 105
			}

clint's avatar
clint committed
106
			return $res;
elleo's avatar
elleo committed
107 108
		}
	}
109 110
}
?>