id; $sql = "SELECT * FROM `object_count` " . "LEFT JOIN `song` ON `song`.`id` = `object_count`.`object_id` " . "LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog` " . "WHERE `object_count`.`user` = ? AND `object_count`.`object_type`='song' " . "AND `catalog`.`enabled` = '1' " . "ORDER BY `object_count`.`date` DESC LIMIT 1"; $db_results = Dba::read($sql, array($user_id)); $results = Dba::fetch_assoc($db_results); return $results; } // get_last_song /** * get_object_history * This returns the objects that have happened for $user_id sometime after $time * used primarly by the democratic cooldown code */ public static function get_object_history($user_id='',$time) { $user_id = $user_id ? $user_id : $GLOBALS['user']->id; $sql = "SELECT * FROM `object_count` " . "LEFT JOIN `song` ON `song`.`id` = `object_count`.`object_id` " . "LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog` " . "WHERE `object_count`.`user` = ? AND `object_count`.`object_type`='song' AND `object_count`.`date` >= ? " . "AND `catalog`.`enabled` = '1' " . "ORDER BY `object_count`.`date` DESC"; $db_results = Dba::read($sql, array($user_id, $time)); $results = array(); while ($row = Dba::fetch_assoc($db_results)) { $results[] = $row['object_id']; } return $results; } // get_object_history /** * get_top_sql * This returns the get_top sql */ public static function get_top_sql($type, $threshold = '') { $type = self::validate_type($type); /* If they don't pass one, then use the preference */ if (!$threshold) { $threshold = AmpConfig::get('stats_threshold'); } $date = time() - (86400*$threshold); /* Select Top objects counting by # of rows */ $sql = "SELECT object_id as `id`, COUNT(*) AS `count` FROM object_count" . " WHERE object_type = '" . $type ."' AND date >= '" . $date . "' AND " . Catalog::get_enable_filter($type, '`object_id`') . " GROUP BY object_id ORDER BY `count` DESC "; return $sql; } /** * get_top * This returns the top X for type Y from the * last stats_threshold days */ public static function get_top($type,$count='',$threshold = '',$offset='') { if (!$count) { $count = AmpConfig::get('popular_threshold'); } $count = intval($count); if (!$offset) { $limit = $count; } else { $limit = intval($offset) . "," . $count; } $sql = self::get_top_sql($type, $threshold); $sql .= "LIMIT $limit"; $db_results = Dba::read($sql); $results = array(); while ($row = Dba::fetch_assoc($db_results)) { $results[] = $row['id']; } return $results; } // get_top /** * get_recent_sql * This returns the get_recent sql */ public static function get_recent_sql($type) { $type = self::validate_type($type); $sql = "SELECT DISTINCT(`object_id`) as `id`, MAX(`date`) FROM object_count" . " WHERE `object_type` = '" . $type ."' AND " . Catalog::get_enable_filter($type, '`object_id`') . " GROUP BY `object_id` ORDER BY MAX(`date`) DESC, `id` "; return $sql; } /** * get_recent * This returns the recent X for type Y from the * last stats_threshold days */ public static function get_recent($type, $count='',$threshold = '',$offset='') { /* If they don't pass one, then use the preference */ if (!$threshold) { $threshold = AmpConfig::get('stats_threshold'); } if (!$count) { $count = AmpConfig::get('popular_threshold'); } $count = intval($count); $type = self::validate_type($type); if (!$offset) { $limit = $count; } else { $limit = intval($offset) . "," . $count; } $sql = self::get_recent_sql($type); $sql .= "LIMIT $limit"; $db_results = Dba::read($sql); $results = array(); while ($row = Dba::fetch_assoc($db_results)) { $results[] = $row['id']; } return $results; } // get_recent /** * 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 */ public static function get_user($count,$type,$user,$full='') { $count = intval($count); $type = self::validate_type($type); /* If full then don't limit on date */ if ($full) { $date = '0'; } else { $date = time() - (86400*AmpConfig::get('stats_threshold')); } /* Select Objects based on user */ //FIXME:: Requires table scan, look at improving $sql = "SELECT object_id,COUNT(id) AS `count` FROM object_count" . " WHERE object_type = ? AND date >= ? AND user = ?" . " GROUP BY object_id ORDER BY `count` DESC LIMIT $count"; $db_results = Dba::read($sql, array($type, $date, $user)); $results = array(); while ($r = Dba::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 */ public static function validate_type($type) { switch ($type) { case 'artist': case 'album': case 'genre': case 'song': case 'video': return $type; default: return 'song'; break; } // end switch } // validate_type /** * get_newest_sql * This returns the get_newest sql */ public static function get_newest_sql($type, $catalog=0) { $type = self::validate_type($type); $sql = "SELECT DISTINCT(`$type`) as `id`, MIN(`addition_time`) AS `real_atime` FROM `song` "; $sql .= "LEFT JOIN `catalog` ON `catalog`.`id` = `song`.`catalog` " . "WHERE `catalog`.`enabled` = '1' "; if ($catalog > 0) { $sql .= "AND `catalog` = '" . scrub_in($catalog) ."' "; } $sql .= "GROUP BY `$type` ORDER BY `real_atime` DESC "; return $sql; } /** * get_newest * This returns an array of the newest artists/albums/whatever * in this ampache instance */ public static function get_newest($type, $limit='', $offset='', $catalog=0) { if (!$count) { $count = AmpConfig::get('popular_threshold'); } if (!$offset) { $limit = $count; } else { $limit = $offset . ',' . $count; } $sql = self::get_newest_sql($type, $catalog); $sql .= "LIMIT $limit"; $db_results = Dba::read($sql); $items = array(); while ($row = Dba::fetch_row($db_results)) { $items[] = $row[0]; } // end while results return $items; } // get_newest } // Stats class