Forum Discussion

PerBonomi's avatar
9 years ago

Badge for consecutive, daily logins

I thought it was about time to upgrade from the "ugly" javascript and use only Freemarker.

 

The old code is here.

 

Here's the new code; please feed back :)

 

<#if !user.anonymous>
	<#assign date_check = http.session.attributes.name.get("badge_daily_check_date", "")/> <#-- Read browser session variable to see if we already checked today -->
	<#assign date_format = restadmin("/users/id/${user.id}/settings/name/layout.format_pattern_date").value/> <#-- Check user's date format settings -->
	<#assign date_now = datesupport.getDateAsString()/> <#-- Get today's date -->
	<#assign date_now = date_now?datetime("${date_format}")?string["yyyy-MM-dd"]/> <#-- convert today's date to our preferred date format -->
	<#if date_check != date_now> <#-- Only run the script if we did not already check today -->
		${http.session.setAttribute("badge_daily_check_date", "${date_now}")} <#-- Set the browser session variable to today's date, so we don't keep running this script on the same day -->
		<#assign userData = restadmin("/users/id/${user.id}/profiles/name/days_consecutive").value />
		<#assign userData_date = date_now/> <#-- Set the date variable to today, so we can compare again the next time the user visits us. This will be used if it's the user's first login -->
		<#assign userData_cur = "0"/> <#-- The current tally of consecutive logins. This will be used if it's the user's first login  -->
		<#assign userData_max = "0"/> <#-- The max tally of consecutive logins ever. This will be used if it's the user's first login -->
		<#if userData == ""> <#-- If we don't have any consecutive login data for the user start fresh -->
			<#assign userData_all = date_now + ',' + userData_cur + ',' + userData_max/> <#-- construct the data for the api call for a new user -->
			<#assign result = restadmin("/users/id/${user.id}/profiles/name/days_consecutive/set?value=${userData_all}") /> <#-- write the new user's consecutive data -->
		<#else> <#-- If we do already have consecutive login data for the user then continue -->
			<#assign userData_date = userData?substring(0,10)/> <#-- Grab the last date the user logged in from the stored variable in the db -->
			<#if userData_date != date_now> <#-- check again if we didn't already check the user today -->
				<#assign userData_max = userData?substring( userData?index_of(",",12)+1,userData?length)/> <#-- get the user's max daily logins tally -->
				<#assign diffCheck = ((date_now?datetime("yyyy-MM-dd")?long - userData_date?datetime("yyyy-MM-dd")?long)/(1000 * 60 * 60 * 24))?int/> <#-- compare the last date the user logged in to the date today -->
				<#if (diffCheck == 1)> <#-- if the user logged in yesterday, and today -->
					<#assign userData_cur = userData?substring( 11 , userData?last_index_of(","))?number + 1/> <#-- set the current tally of the user's consecutive days -->
					<#if (userData_cur?number > userData_max?number)> <#-- if the user's current tally exceeds the user's max -->
						<#assign userData_max = userData_max?number + 1/> <#-- then increment the user's current tally AND the user's max tally -->
					</#if>
				</#if>
				<#assign userData_all = date_now + ',' + userData_cur + ',' + userData_max/> <#-- construct the data for the api call for a new user -->
				<#assign result = restadmin("/users/id/${user.id}/profiles/name/days_consecutive/set?value=${userData_all}") /> <#-- write the new user's consecutive data -->
				<#-- the rest is simply checking if the user has x days consecutive, assigning the needed role if they do -->
				<#assign hasConsecutiveDays_1000 = false/>
				<#assign hasConsecutiveDays_500 = false/>
				<#assign hasConsecutiveDays_200 = false/>
				<#assign hasConsecutiveDays_100 = false/>
				<#assign hasConsecutiveDays_50 = false/>
				<#assign hasConsecutiveDays_25 = false/>
				<#assign hasConsecutiveDays_15 = false/>
				<#assign hasConsecutiveDays_10 = false/>
				<#assign hasConsecutiveDays_9 = false/>
				<#assign hasConsecutiveDays_8 = false/>
				<#assign hasConsecutiveDays_7 = false/>
				<#assign hasConsecutiveDays_6 = false/>
				<#assign hasConsecutiveDays_5 = false/>
				<#assign hasConsecutiveDays_4 = false/>
				<#assign hasConsecutiveDays_3 = false/>
				<#assign hasConsecutiveDays_2 = false/>
				<#assign hasConsecutiveDays_1 = false/>
				<#assign selfRoles = restadmin("/users/self/roles").roles.role />
				<#list selfRoles as selfRole>
					<#if selfRole.name == "consecutiveDays_1000"><#assign hasConsecutiveDays_1000 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_500"><#assign hasConsecutiveDays_500 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_200"><#assign hasConsecutiveDays_200 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_100"><#assign hasConsecutiveDays_100 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_50"><#assign hasConsecutiveDays_50 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_25"><#assign hasConsecutiveDays_25 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_15"><#assign hasConsecutiveDays_15 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_10"><#assign hasConsecutiveDays_10 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_9"><#assign hasConsecutiveDays_9 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_8"><#assign hasConsecutiveDays_8 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_7"><#assign hasConsecutiveDays_7 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_6"><#assign hasConsecutiveDays_6 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_5"><#assign hasConsecutiveDays_5 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_4"><#assign hasConsecutiveDays_4 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_3"><#assign hasConsecutiveDays_3 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_2"><#assign hasConsecutiveDays_2 = true/><#break></#if>
					<#if selfRole.name == "consecutiveDays_1"><#assign hasConsecutiveDays_1 = true/><#break></#if>
				</#list>
				<#assign consecutiveDays = userData_max?number/>
				<#if hasConsecutiveDays_1000 == false>
					<#if (consecutiveDays >= 1000)>
						<#if hasConsecutiveDays_1000 == false><#assign result = restadmin("/roles/name/consecutiveDays_1000/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_500/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 500)>
						<#if hasConsecutiveDays_500 == false><#assign result = restadmin("/roles/name/consecutiveDays_500/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_200/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 200)>
						<#if hasConsecutiveDays_200 == false><#assign result = restadmin("/roles/name/consecutiveDays_200/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_100/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 100)>
						<#if hasConsecutiveDays_100 == false><#assign result = restadmin("/roles/name/consecutiveDays_100/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_50/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 50)>
						<#if hasConsecutiveDays_50 == false><#assign result = restadmin("/roles/name/consecutiveDays_50/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_25/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 25)>
						<#if hasConsecutiveDays_25 == false><#assign result = restadmin("/roles/name/consecutiveDays_25/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_15/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 15)>
						<#if hasConsecutiveDays_15 == false><#assign result = restadmin("/roles/name/consecutiveDays_15/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_10/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 10)>
						<#if hasConsecutiveDays_10 == false><#assign result = restadmin("/roles/name/consecutiveDays_10/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_9/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 9)>
						<#if hasConsecutiveDays_9 == false><#assign result = restadmin("/roles/name/consecutiveDays_9/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_8/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 8)>
						<#if hasConsecutiveDays_8 == false><#assign result = restadmin("/roles/name/consecutiveDays_8/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_7/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 7)>
						<#if hasConsecutiveDays_7 == false><#assign result = restadmin("/roles/name/consecutiveDays_7/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_6/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 6)>
						<#if hasConsecutiveDays_6 == false><#assign result = restadmin("/roles/name/consecutiveDays_6/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_5/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 5)>
						<#if hasConsecutiveDays_5 == false><#assign result = restadmin("/roles/name/consecutiveDays_5/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_4/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 4)>
						<#if hasConsecutiveDays_4 == false><#assign result = restadmin("/roles/name/consecutiveDays_4/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_3/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 3)>
						<#if hasConsecutiveDays_3 == false><#assign result = restadmin("/roles/name/consecutiveDays_3/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_2/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 2)>
						<#if hasConsecutiveDays_2 == false><#assign result = restadmin("/roles/name/consecutiveDays_2/users/add?role.user=id/${user.id}")/><#assign result = restadmin("/roles/name/consecutiveDays_1/users/remove?role.user=id/${user.id}")/></#if>
					<#elseif (consecutiveDays >= 1)>
						<#if hasConsecutiveDays_1 == false><#assign result = restadmin("/roles/name/consecutiveDays_1/users/add?role.user=id/${user.id}")/></#if>
					</#if>
				</#if>	
			</#if>
		</#if>
	</#if>
</#if>

 

  • Thanks for sharing

    Assuming this routine only works in the same browser (session variables) and device? If you accessed via your desktop one day and then your phone the next?
    • PerBonomi's avatar
      PerBonomi
      Boss

      cblown wrote:
      Thanks for sharing

      Assuming this routine only works in the same browser (session variables) and device? If you accessed via your desktop one day and then your phone the next?

      I assume so. So the code would check again, if you switched systems or browsers.