summaryrefslogtreecommitdiffstats
path: root/lib/class/stats.class.php
blob: 745232b944c2e0dd9125170cd71deb8ee4b616ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/*

 Copyright (c) 2001 - 2006 Ampache.org
 All rights reserved.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 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 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/**
 * Stats
 * this class handles the object_count
 * Stuff, before this was done in the user class
 * but that's not good, all done through here. 
 */
class Stats {

	/* Base vars */
	var $id; 
	var $object_type; 
	var $object_id;
	var $date;
	var $user;


	/**
 	 * Constructor
	 * This doesn't do anything currently
	 */
	function Stats() { 

		return true;

	} // Constructor

	/**
 	 * insert
	 * This inserts a new record for the specified object
	 * with the specified information, amazing! 
	 */
	function insert($type,$oid,$user) { 

		$type 	= $this->validate_type($type);
		$oid	= sql_escape($oid);
		$user	= sql_escape($user);	

		$sql = "INSERT INTO object_count (`object_type`,`object_id`,`date`,`user`) " . 
			" VALUES ('$type','$oid','$date','$user')";
		$db_results = mysql_query($sql,dbh());

	} // insert

	/**
 	 * get_top
	 * This returns the top X for type Y from the
	 * last conf('stats_threshold') days
	 */
	function get_top($count,$type) { 

		$count	= intval($count);
		$type	= $this->validate_type($type);
		$date 	= time() - (86400*conf('stats_threshold'));

		/* Select Top objects counting by # of rows */
		$sql = "SELECT object_id,COUNT(id) AS `count` FROM object_count" . 
			" WHERE object_type='$type' AND date >= '$date'" .
			" GROUP BY object_id ORDER BY `count` DESC LIMIT $count";
		$db_results = mysql_query($sql, dbh());

		$results = array();

		while ($r = mysql_fetch_assoc($db_results)) { 
			$results[] = $r;
		}

		return $results;

	} // get_top

	/**
 	 * get_user
	 * This gets all stats for atype based on user with thresholds and all
	 * If full is passed, doesn't limit based on date 
	 */
	function get_user($count,$type,$user,$full='') { 

		$count 	= intval($count);
		$type	= $this->validate_type($type);
		$user	= sql_escape($user);
	
		/* If full then don't limit on date */	
		if ($full) { 
			$date = '0';
		}
		else { 
			$date = time() - (86400*conf('stats_threshold'));
		}

		/* Select Objects based on user */
		$sql = "SELECT object_id,COUNT(id) AS `count` FROM object_count" . 
			" WHERE object_type='$type' AND date >= '$date' AND user = '$user'" . 
			" GROUP BY object_id ORDER BY `count` DESC LIMIT $count";
		$db_results = mysql_query($sql, dbh());

		$results = array();

		while ($r = mysql_fetch_assoc($db_results)) { 
			$results[] = $r;
		} 

		return $results;

	} // get_user

	/**
	 * validate_type
	 * This function takes a type and returns only those
	 * which are allowed, ensures good data gets put into the db
	 */
	function validate_type($type) { 

		switch ($type) { 
			case 'artist':
				return 'artist';
			break;
			case 'album':
				return 'album';
			break;
			case 'genre':
				return 'genre';
			break;
			case 'song':
			default:
				return 'song';
			break;
		} // end switch

	} // validate_type

} //Stats class
?>