• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

RM: How to get Total download count for author

Matthew Hawley

Well-known member
#1
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...
 

Jeremy

Well-known member
#4
You should read up on interacting with a XenForo database, there are a few resources that deal with this in different contexts.
 

truonglv

Formerly Nobita.Kun
#5
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
 

Matthew Hawley

Well-known member
#7
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']);
    }
}
?>
 

truonglv

Formerly Nobita.Kun
#8
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);
    }
}