RM: How to get Total download count for author

Matthew Hawley

Well-known member
How would I get the total downloads count for an author. I tried looking into the star rating to see how it pulled the rating from all resources, but no luck. I'm thinking that I may have to use a xen:calc...
 
You need to query the database and add downloads together. A total count isn't kept anywhere.
 
You should read up on interacting with a XenForo database, there are a few resources that deal with this in different contexts.
 
XenForo_Model_Thread::countThreads (This is same way to do)
Maybe this is helpful to you.
With conditions you must have:
user_id = $userId into sql clause
 
Code:
SELECT sum( res.download_count ) AS totaldownloads
FROM `xf_resource` AS res
WHERE user_id =1

Is this correct?

Code:
<?php
class TotalResourceDownloads_Installer_Installer
{
    protected static $table = array(
        'createQuery' => 'CREATE TABLE IF NOT EXISTS `xf_total_resource_downloads` (         
                    SELECT sum( res.download_count ) AS totaldownloads
                    FROM `xf_resource` AS res
                    WHERE user_id =1
                    )
                ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;',
        'dropQuery' => 'DROP TABLE IF EXISTS `xf_total_resource_downloads`'
    );
    /**
    * This is the function to create a table in the database so our addon will work.
    *
    * @since Version 1.0.0
    * @version 1.0.0
    * @ Matthew Hawley
    */
    public static function install()
    {
        $db = XenForo_Application::get('db');
        $db->query(self::$table['createQuery']);
    }
    /**
    * This is the function to DELETE the table of our addon in the database.
    *
    * @since Version 1.0.0
    * @version 1.0.0
    * @ Matthew Hawley
    */
    public static function uninstall()
    {
        $db = XenForo_Application::get('db');
        $db->query(self::$table['dropQuery']);
    }
}
?>
 
Why you make new table?
You can create new model. Same code, maybe helpful to you
PHP:
<?php
class YourAddon_Model_Resource extends XenForo_Model
{
    public function countAllDownloadedByUserId($userId)
    {
        return $this->_getDb()->fetchOne('
            SELECT SUM(download_count) AS download
            FROM xf_resource
            WHERE user_id = ?
        ', $userId);
    }
}
 
Top Bottom