Thanks for the heads up on this request for support.
I may have figured this out on my own, but I did it based on the assumption that the first file returned by get_user_field (“s2member_file_download_access_log”) will always be the oldest. Is that correct?
Yes, this is true. However, this is not by design, it’s only because of the order in which log entries enter this array. So while it’s fine to assume this, it wouldn’t hurt to scan the array, and sort these based on the “time” for each of them, just to ensure your own sanity, before your custom routine does something further :-)
My code assumes that the “time” value of that first file is the start of the “current period”. Using my example in the previous post, it then adds 7 days to find the end of the current period.
Not completely reliable, given the nature of this array. s2Member does not update this array in real-time, for every user. The array is only updated (with older entries moved into an archive), during the actual download of a file. Therefore, relying on this array (without any additional filtering first), could perhaps return inaccurate results at certain times, for certain users. Particularly those which have not downloaded anything recently.
It also assumes that the current period is a “rolling” 7 day period, where the first file downloaded rolls off the list after 7 days and then the next file’s initial download time is used to calculate the current period.
Files that were downloaded prior to the current period, roll off this list automatically. However, this ONLY occurs during the actual download of a file. Therefore, you will need to scan this array, and make calculations of your own, based on the “time” for each of the files listed, and also based on the configuration of the site, at a specific Level of membership, for the current user. I know… it get’s hairy.
Here is a possible solution for you, which considers all of these things…
<?php
function s2_download_period_from_to()
{
$seconds_in_one_day = 86400;
$user_downloads = s2member_user_downloads();
if($user_downloads['allowed'])
{
$time_current_period_began = strtotime('-'.$user_downloads['allowed_days'].' days');
$time_current_period_ends = $time_current_period_began + ($user_downloads['allowed_days'] * $seconds_in_one_day);
return array('from' => $time_current_period_began, 'to' => $time_current_period_ends);
}
else return FALSE; // This user does NOT have access to files.
}
?>
Reference article:
See s2Member® Codex regarding: s2member_user_downloads()