Can you help me create a method for notifying a particular user of when a file is downloaded. I have a site that has a content type of "tender" which has a CCK field that has the tender in pdf format. I need an email sent to a user or role when the file is downloaded.

Thanks

Comments

svendecabooter’s picture

You can achieve this by using the hook_filefield_tracker. Some example code to get you started (which should go in a separate module):

/**
 * Implementation of hook_filefield_tracker().
 */
function mymodule_filefield_tracker($entry) {
  $params['file'] = field_file_load($entry->fid);
  $params['filename'] = $file['filename'];
  $params['node'] = node_load(array('nid' => $entry->nid));
  $params['downloader'] = user_load($entry->uid);

  drupal_mail('mymodule', 'download_notify', 'notifyme@example.com', language_default(), $params);
}

/**
 * Implementation of hook_mail().
 */
function mymodule_mail($key, &$message, $params) {
  if ($key == 'download_notify') {
    $message['subject'] = t('New download: !file', array('!file' => $params['filename']));
    $message['body'][] = t('User !user downloaded file !file', array('!user' => $params['downloader']->name, '!file' => $params['filename']));
  }
}
svendecabooter’s picture

Status: Active » Fixed

Marking as fixed. Let me know if you have any problems

ginga8’s picture

Do you think that you will be implementing something like this in the next release. I am very novice when it comes to php and drupal and I am not sure I can implement this?

svendecabooter’s picture

Assigned: Unassigned » svendecabooter
Status: Fixed » Active
yetihunter1000’s picture

This looks like the solution I'm after, where should I place the above code?

svendecabooter’s picture

Assigned: svendecabooter » Unassigned
Category: feature » support
Status: Active » Fixed

You should put it in your own custom module.
Basically you need to create a new folder in sites/all/modules, add a mymodule.info and mymodule.module file, and paste the code above in mymodule.module.
You can replace all instances of "mymodule" with a better name for your module.
For more instructions on the .info file and creating your module: http://drupal.org/node/206753

svendecabooter’s picture

Assigned: Unassigned » svendecabooter
Category: support » feature
Status: Fixed » Active

Actually i might look into integrating with the Actions / Triggers module, so you can use the Drupal UI to create this email notification.
Will check later if this is possible...

yetihunter1000’s picture

Thank you for your replies, that would be great if that was an option to use as my client has asked to be emailed on the download and at the moment I cannot get this to work, even with the custom module, so am looking for ways around this.