WordPress.org

Forums

WP-Filebase Download Manager
[resolved] User download tracking (12 posts)

  1. stu11
    Member
    Posted 3 years ago #

    I know there is a download tracker but is there anyway to track which specific users are doing the individual downloads?
    If not does anyone have an coding alteration suggestions for it?

    Thanks!

    http://wordpress.org/extend/plugins/wp-filebase/

  2. stu11
    Member
    Posted 3 years ago #

    Don't worry I've worked it out

  3. trabifant
    Member
    Posted 3 years ago #

    Plz post the solution!

  4. stu11
    Member
    Posted 3 years ago #

    Ok sure, can you help me display files from particular roles? I posted a question before about it.

    To do the download tracker:
    in wp-filebase.php
    go down to where it says "//database settings"
    add this line:
    $wpdb->wpfilebase_file_download_manager = $wpdb->prefix . 'wpfb_file_download_manager';
    just after:
    $wpdb->wpfilebase_files_id3 = $wpdb->prefix . 'wpfb_files_id3';

    Now go to setup.php
    go down to this function "static function SetupDBTables()"
    add this
    $tbl_file_download_manager = $wpdb->prefix . 'wpfb_file_download_manager';
    after
    $tbl_files_id3 = $wpdb->prefix . 'wpfb_files_id3';

    add this just under that before the $queries
    $queries[] = "CREATE TABLE IF NOT EXISTS $tbl_file_download_manager (
    tracker_id bigint(20) unsigned NOT NULL auto_increment,
    dl_file_id bigint(20) unsigned NOT NULL default '0',
    dl_file_name varchar(127) NOT NULL default '',
    dl_user_id bigint(20) unsigned NOT NULL default '0',
    dl_user_name varchar(127) NOT NULL default '',
    dl_company_name varchar(127) NOT NULL default '',
    dl_time timestamp default CURRENT_TIMESTAMP,
    PRIMARY KEY (tracker_id)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";

    scroll down to the bottom function DropDBTables()
    and replace the definition of $tables with this:
    $tables = array($wpdb->wpfilebase_files, $wpdb->wpfilebase_files_id3, $wpdb->wpfilebase_cats, $wpdb->wpfilebase_file_download_manager);

    Now go to File.php and scroll down to function Download()
    I put my code in after the //count download section inside the Download() funtion just cause it looks nice. Add this to it:

    if(!$is_admin) {
    			$wpdb->query( $wpdb->prepare(
    						"
    						INSERT INTO $wpdb->wpfilebase_file_download_manager
    						( dl_file_id, dl_file_name, dl_user_id, dl_user_name, dl_company_name)
    						VALUES ('%d', '%s', '%d', '%s', '%s')
    						",
           					 array(
    						$this->file_id,
    						$this->file_name,
    						$current_user->ID,
    						$current_user->user_login,
    						$user_role
    						)
    				) );
    		}

    Now all you have to do is save all that stuff, deactivate and then reactive the plugin (which will run the setup file and do your tables).

    To call data from it is pretty simple just use standard SQL requests (you can put this in your templates or what ever or even just make a short code of it):

    $userdownloads = $wpdb->get_results(
    			"
    			SELECT dl_file_name, dl_user_name, dl_company_name, dl_time
    			FROM $wpdb->wpfilebase_file_download_manager
    			WHERE dl_company_name != 'administrator'
    			"
    			);
    
    		foreach ( $userdownloads as $userdownload )
    		{
    			echo 'Company: ' . $userdownload ->dl_company_name . "<br />";
    			echo 'User: ' . $userdownload ->dl_user_name . "<br />";
    			echo 'Download time: ' . $userdownload ->dl_time . "<br />";
    			echo 'File name: ' . $userdownload ->dl_file_name . "<br /><br />";
    		}

    and voila! you have it.
    Enjoy

  5. stu11
    Member
    Posted 3 years ago #

    it doesn't show up in the thing above but before and after all your database column names like tracker_id and dl_file etc you have to have this symbol `

  6. Delila
    Member
    Posted 3 years ago #

    Hello,

    Thanks a lot for posting a solution on this subject.
    Could you detailled the call data part a bit more ? Because I'm not sure where I should put the SQL request, I'm not very used to SQL. Thank you very much in advance, this would really help.

  7. stu11
    Member
    Posted 2 years ago #

    Delila
    you can literally copy and paste that into one of your template file that you want it to display in.

    I've got mine displaying company name, user name, downloaded file name and time of download by putting this in the php file:

    <p><?php 
    
    $userdownloads = $wpdb->get_results(
    	"
    	SELECT dl_file_name, dl_user_name, dl_company_name, dl_time
    			FROM $wpdb->wpfilebase_file_download_manager
    			WHERE dl_company_name != 'administrator'
    			"
    			);
    
    		foreach ( $userdownloads as $userdownload )
    		{
    			echo 'Company: ' . $userdownload ->dl_company_name . "<br />";
    			echo 'User: ' . $userdownload ->dl_user_name . "<br />";
    			echo 'Download time: ' . $userdownload ->dl_time . "<br />";
    			echo 'File name: ' . $userdownload ->dl_file_name . "<br /><br />";
    
    ?></p>
  8. Delila
    Member
    Posted 2 years ago #

    Thank you so much for your answer, it is nearly working :-) The only thing is that it is just showing one download, the last one. Is there a way to show all the downloads that where made ?

    Thanks a lot in advance.

  9. abot
    Member
    Posted 2 years ago #

    Hey stu11,

    I'm sorry but I've got the same issue Delila had... not sure what to do with the final step, where should the code go? I can't find any php files for my templates...

    Sorry but I'm lost, video engineer by profession - wordpress developer by necessity.

    Thank you

  10. stu11
    Member
    Posted 2 years ago #

    So you put that code into a template file (appearances->editor->***templatefile*** ) that is for the page you want to display it on:

    <p><?php
    //making the request
    $userdownloads = $wpdb->get_results(
    	"
    /* selecting all the feilds that we want */
    	SELECT dl_file_name, dl_user_name, dl_company_name, dl_time
    /* from the download manager table */
    			FROM $wpdb->wpfilebase_file_download_manager
    /*where the company name is not administrator, this means that it doesn't track the company name 'administrator' (note that I put this in the above code anyway) downloads */
    			WHERE dl_company_name != 'administrator'
    			"
    			);
    /* this puts out all the rows that aren't admin */
    		foreach ( $userdownloads as $userdownload )
    		{
    			echo 'Company: ' . $userdownload ->dl_company_name . "<br />";
    			echo 'User: ' . $userdownload ->dl_user_name . "<br />";
    			echo 'Download time: ' . $userdownload ->dl_time . "<br />";
    			echo 'File name: ' . $userdownload ->dl_file_name . "<br /><br />";
    
    ?></p>

    Does that clear it up? if you're testing the download tracker using the admin to download files it won't show what you want since we are only tracking non admin downloads (In previous code I posted). Look in your database to see if the downloads are being tracked as well by accessing phpmyadmin and then navigating to the table.

  11. stu11
    Member
    Posted 2 years ago #

    Sorry guys! just realised what was happening! here's the fix for the template file:

    $userdownloads = $wpdb->get_results(
    	"
    /* selecting all the feilds that we want */
    	SELECT dl_file_name, dl_user_name, dl_company_name, dl_time
    /* from the download manager table */
    			FROM $wpdb->wpfilebase_file_download_manager
    /*where the company name is not administrator, this means that it doesn't track the company name 'administrator' (note that I put this in the above code anyway) downloads */
    			WHERE dl_company_name != 'administrator'
    			"
    			);
    
            while($row = mysql_fetch_array($userdownloads)){
    	echo 'Company: ' . $row['dl_company_name'] . "<br />";
    	echo 'User: ' . $row['dl_user_name']. "<br />";
    	echo 'Download time: ' . $row['dl_time']. "<br />";
    	echo 'File name: ' . $row['dl_file_name']. "<br /><br />";
            }

    Let me know if you have problems with this

  12. rheide
    Member
    Posted 2 years ago #

    I've been trying to reap the benefits of all your work here! I'm no PHP expert but I had a bunch of issues with the code that writes the record to the database.

    I have a couple questions if you don't mind ...

    To get the system to write the record to the database, I had to use this:

    // record download
    
    		if(!$is_admin) {
    			$wpdb->query( $wpdb->query(
    						"
    						INSERT INTO $wpdb->wpfilebase_file_download_manager
    						( dl_file_id, dl_file_name, dl_user_id, dl_user_name, dl_company_name)
    						VALUES ($this->file_id,'$this->file_name', $current_user->ID, '$current_user->user_login', '$user_role')
    						",
           					 array(
    						$this->file_id,
    						$this->file_name,
    						$current_user->ID,
    						$current_user->user_login,
    						$user_role
    						)
    				) );
    		}

    Any idea why that worked at the original code did not?

    Although I can see the records in the database, I have yet to be able to get the template part working. I get this error:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/name/public_html/site.net/wp-content/themes/responsive/download-data.php on line 16

    and line 16 is
    while($row = mysql_fetch_array($userdownloads)){

    and just to clarify, I'm using the latest code in this post

    Any suggestions?

    Thanks in advance!!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP-Filebase Download Manager
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic