My task involved to limit the number of downloads to 1 but with an exemption for those with a specific role or a so-called "credit". I've used hookdownloadauthorize()
to try this one out.
The first problem I encountered was that if you do a return FALSE
as what the documentation mentioned, it would return the error "A hook denied your access to this file. Please contact the site administrator if this message has been received in error." based on this section of the code:
<?php
//Check any if any hook_download_authorize calls deny the download
foreach (module_implements('download_authorize') as $module) {
$name = $module .'_download_authorize';
$result = $name($user, $file_download);
if (!$result) {
return UC_FILE_ERROR_HOOK_ERROR;
}
}
The second problem I've encountered is that if you wish to create some validations that are called during that hook, all validations need to be passed rather than just an OR since in my problem, there are other options that lets you download the files if you failed one.
I did not set any Download Limits under the File Download Settings. What I did then was this:
<?php
define('MYMODULE_DOWNLOAD_LIMIT', 1) // Need to fake the download limit.
function mymodule_download_authorize($user, $file_download) {
if (in_array('Unlimited Downloads Role', array_values($user->roles))) {
return TRUE;
}
else {
// I've used Userpoints for the Credit part.
if (userpoints_get_current_points($user->uid) >= 1 && $file_download->accessed >= variable_get('mymodule_download_limit', MYMODULE_DOWNLOAD_LIMIT)) {
return TRUE;
}
else {
if ($file_download->accessed < variable_get('mymodule_download_limit', MYMODULE_DOWNLOAD_LIMIT)) {
return TRUE;
}
else {
drupal_set_message(t('Download limit has been reached.', 'error'));
return FALSE;
}
}
}
}
Works fine but seems like a failed logic. Any suggestions much appreciated. :D
Update: For the "A hook has denied..." message problem, rszrama (a developer of Ubercart) has suggested to me to simply replace the message using String Overrides since the messages are being wrapped around t()
.