From 2faea5f7b345ba0d9319d4466261b52c7bff0325 Mon Sep 17 00:00:00 2001 From: Karl 'vollmerk' Vollmer Date: Mon, 23 Oct 2006 01:47:18 +0000 Subject: re-wroked stats which means major db update, some stats dealies may still not work correctly --- lib/class/stats.class.php | 154 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 lib/class/stats.class.php (limited to 'lib/class/stats.class.php') diff --git a/lib/class/stats.class.php b/lib/class/stats.class.php new file mode 100644 index 00000000..ba97badf --- /dev/null +++ b/lib/class/stats.class.php @@ -0,0 +1,154 @@ +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(object_id) 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(object_id) 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 +?> -- cgit