latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Display days remaining in download period?

Home Forums Community Forum Display days remaining in download period?

This topic contains 4 replies, has 3 voices. Last updated by  Paul Woods 4 years, 7 months ago.

Topic Author Topic
Posted: Monday Jun 4th, 2012 at 11:04 am #15375

Is there some (easy) way to display the number of days remaining in a user’s current download period?

For example, I am allowing level 0 members to download 5 files every 7 days. I see that I can use constants to inform members that they have downloaded X of 5 files in the current 7 day period. But I think it would also make sense to tell them how many days they’ll need to wait until the counter is reset.

I assume the period start time is recorded in the database somewhere and could be extracted via custom php, but is there an easier way, or is there any sample code already available?

Thanks.

List Of Topic Replies

Viewing 4 replies - 1 through 4 (of 4 total)
Author Replies
Author Replies
Posted: Monday Jun 4th, 2012 at 2:03 pm #15383

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?

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.

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.

However, these assumptions may or may not be correct based on Jason’s answer to a previous question I had about download calculations: Need Clarification on Downloads per X Days

In that previous example, it was assumed that the user downloaded all 5 allowed files on the first day, which made the counter reset to 0 after the 7th day. But if instead the user downloaded 1 file each day for 5 days, it would seem to make more sense for the current period to “roll” as described above.

  • This reply was modified 4 years, 7 months ago by  Paul Woods.
Posted: Tuesday Jun 5th, 2012 at 6:08 am #15450

Hi Paul.

From what Jason replied there, what I understand is that the first download determines when the time starts being counted. If you allow the downloads every 7 days, and the first download happened on a Tuesday, then it’ll be reset every Tuesday (every 7 days). It won’t take the next download time and add 7 days to that.

I will email Jason asking him to confirm this.

Posted: Thursday Jun 7th, 2012 at 6:46 pm #15851
Staff Member

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&#91;'allowed'&#93;)
			{
				$time_current_period_began = strtotime('-'.$user_downloads&#91;'allowed_days'&#93;.' days');
				$time_current_period_ends = $time_current_period_began + ($user_downloads&#91;'allowed_days'&#93; * $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()

Posted: Friday Jun 8th, 2012 at 4:54 pm #15961

Jason, thank you very much for the detailed reply and code sample. It looks like there are a few scenarios I had not considered based on the way s2Member updates the array (as I suspected there probably would be). Knowing that it is only updated when a file is downloaded is a critical factor, so thanks for pointing that out… I will need to re-think a few things.

I think it’s important to provide the user ahead of time with the information to make smart decisions about which files they want to download now vs which ones they can wait for. But you’re right, it can get a little hairy depending on the situation.

Viewing 4 replies - 1 through 4 (of 4 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.