How do I add number of times downloaded?


Thanks for helping me out! I wanted to add a counter to the file download page to display the number of times something is downloaded (the way that this site's plugins pages have a counter).

Does anyone know how to do this? I'd appreciate the pointers on how to script this (I'm not much of a programmer) and what ELGG file belongs to the File download view (I'm struggling to find it!).

Much appreciated!!

  • tpogonat, the best way to do this would be to use annotation, you can look at the fivestar plugin how it was done there. The other solution is to use metadata and use the metadata handler to allow everyone to change metadata:

    register_plugin_hook('permissions_check:metadata', 'user', 'hereFunctionThatChecksIfMetadataIsCounter',1);


            function hereFunctionThatChecksIfMetadataIsCounter($event, $object_type, $object, $vars){
                if (!isset($vars['metadata']['name']))
                    return $object;
                if ($vars['metadata']['name'] == 'download_counter')
                    return true;
                return $object;
                //return the original value of the hook (true/false in this case)
                return $object;


    Then it's enought if you use $object->download_counter++; after each download.

  • Ok, I think I understand somewhat in regards to your description of the Metadata.

    So by using the metadata handler as you suggested, I registered the plugin hook and added the function call within the start.php of the mod/file/ directory. I added the download counter increase to the download.php in the actions directory (within the "if ($file) {}" statement) - the assumption I made was that after the exit statement, the counter would increase for every download.

    What I'm having a hard time figuring out is how do I display this counter within the user's view of the file information (file/views/default/object/file.php). What would I need to do to call this value that gets incremented?

    Also, i'm having a hard time relating how each file has it's own counter value? (thanks to my limited knowledge of scripting)....