1. 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

Discussion in 'XenForo Development Discussions' started by Matthew Hawley, Jul 16, 2013.

  1. Matthew Hawley

    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...
     
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    You need to query the database and add downloads together. A total count isn't kept anywhere.
     
  3. Matthew Hawley

    Matthew Hawley Well-Known Member

    Okay, so how would I query the database? I have no experience with that what so ever.
     
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    You should read up on interacting with a XenForo database, there are a few resources that deal with this in different contexts.
     
  5. Nobita.Kun

    Nobita.Kun Well-Known Member

    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
     
  6. xf_phantom

    xf_phantom Well-Known Member

    Code:
    SELECT sum( res.download_count ) AS totaldownloads
    FROM `xf_resource` AS res
    WHERE user_id =1
     
  7. Matthew Hawley

    Matthew Hawley Well-Known Member

    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']);
        }
    }
    ?>
     
  8. Nobita.Kun

    Nobita.Kun Well-Known Member

    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);
        }
     
  9. Matthew Hawley

    Matthew Hawley Well-Known Member

    I don't want to use it for a certain user id. I want to add it the sidebar in authors resource list.
     
  10. Nobita.Kun

    Nobita.Kun Well-Known Member

    Yep! Each author will have total downloads. So to get total download You MUST use use_id into condition :)
     
  11. Matthew Hawley

    Matthew Hawley Well-Known Member

    Okay so, what is the file directory and what do I name the file?
     
  12. Jeremy

    Jeremy XenForo Moderator Staff Member

Share This Page