<?php
/*
   +----------------------------------------------------------------------+
   | PEAR Web site version 1.0                                            |
   +----------------------------------------------------------------------+
   | Copyright (c) 2001-2005 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Stig Bakken <ssb@fast.no>                                   |
   |          Tomas V.V.Cox <cox@php.net>                                 |
   |          Martin Jansen <mj@php.net>                                  |
   |          Gregory Beaver <cellog@php.net>                             |
   +----------------------------------------------------------------------+
   $Id$
*/

class statistics
{
    /**
     * Get general package statistics
     *
     * @param  integer ID of the package
     * @return array
     */
    static function package($id)
    {
        global $dbh;
        $query = 'SELECT SUM(dl_number) FROM package_stats WHERE pid = ' . (int)$id;
        return $dbh->getOne($query);
    }

    static function release($id, $rid = '')
    {
        global $dbh;

        $query = 'SELECT s.release, s.dl_number, s.last_dl, r.releasedate '
            . 'FROM package_stats AS s '
            . 'LEFT JOIN releases AS r ON (s.rid = r.id) '
            . 'WHERE pid = ' . (int)$id;
        if (!empty($rid)) {
            $query .= ' AND rid = ' . (int)$rid;
        }
        $query .= ' GROUP BY rid ORDER BY rid DESC';

        return $dbh->getAll($query, DB_FETCHMODE_ASSOC);
    }
}