Notes with Ravinder on Badging System Development Part II


 
Thread Tools Search this Thread
Top Forums Web Development Notes with Ravinder on Badging System Development Part II
# 43  
Old 01-04-2019
Then use something like this in the navbar HTML template:

Code:
<div class="container">
    <div class="alert alert-success alert-dismissible" id="newBadge" style="display:none">
        <a href="#" class="close">&times;</a>
        <strong>New Badge!</strong> Click Here to See Your New Badge(s).
    </div>
</div>

<script>
    $(function () {
        var stateChange = localStorage.getItem('badgestatechange');
        if (stateChange == 'true') {
            $("#newBadge").show();
            localStorage.setItem('badgestatechange', 'false');
        }
        $(".close").click(function () {
            $("#newBadge").alert("close");
        });
    });
</script>

# 44  
Old 01-04-2019
Ravinder,

Please write a PHP function to parse the $color[] array and create the string(2) to append in the badge alert text.

Hint: You need to read a $serialized_badges JSON string (which we will get from the user table and convert it to a PHP array using json_decode() then perhaps a foreach() loop to compare against the current $color[] array.

Or, you can do all this in Javascript by storing the badge[] js array serialized in localstorage, which is how I would do it I think.... No reason to do this in PHP, so I think best to do in Javascript.
# 45  
Old 01-04-2019
Quote:
Originally Posted by Neo
Note, I'm jumping on a plane in a few hours, so I don't have time to check this properly, but I'm thinking:
Code:
<?php
$day = 60 * 60 * 24;
$dayago = time() - $day;
$incrementseq = "UPDATE user SET lastdayactive = (UNIX_TIMESTAMP()/(60*60*24)), daysinsequence =  daysinsequence +1 WHERE userid =" . $uid;
$selectday = "SELECT lastdayactive AS lastactive FROM user WHERE userid =" . $uid;
$resetday = "UPDATE user SET lastdayactive = (UNIX_TIMESTAMP()/(60*60*24)), daysinsequence =  1 WHERE userid =" . $uid;

$day = $vbulletin->db->query_first($selectday);

if ($day['lastactive'] > $dayago && $day['lastactive'] <= $dayago * 2) {
    $status = $vbulletin->db->query_write($incrementseq);
} elseif ($day['lastactive'] > $dayago * 2) {
    $status = $vbulletin->db->query_write($resetday);
}

$getseq = "SELECT daysinsequence AS daysactive FROM user WHERE userid =" . $uid;
$days = $vbulletin->db->query_first($getseq);
if ($days['daysactive'] > 14) {
    $color['fahistory'] == 'black';
} elseif ($days['daysactive'] > 7) {
    $color['fahistory'] == 'indigo';
} elseif ($days['daysactive'] > 3) {
    $color['fahistory'] == 'blue';
} elseif ($days['daysactive'] > 1) {
    $color['fahistory'] == 'limegreen';
} else {
    $color['fahistory'] == 'lightgray';
}
$badgejs .= 'badge["fahistory"] = "' . $color['fahistory'] . '";';
$badgejs .= 'badge["fahistoryval"] = "' . number_format($days['daysactive']) . '";';

Please take a look and let me know what you think Ravinder!

Thanks

PS: I have not debugged that code above yet and realize that we need to add logic to not query the DB after we reset; because we already have the value (1) so no reason to do the extra query, etc.
Hello Neo,

Thank you for writing this one(I apologies that I am not at all getting time to write anything as of now). I have gone through code and it looks great, for my and everyone's understanding I have added explanation too in code(you could let me know if I missed or wrongly mentioned anything here).
Code:
<?php
$day = 60 * 60 * 24;
##Creating day variabale whose value is 1 day's seconds.
$dayago = time() - $day;
##Creating variable dayago whose value is current time - 86400 seconds(day variable) value.
$incrementseq = "UPDATE user SET lastdayactive = (UNIX_TIMESTAMP()/(60*60*24)), daysinsequence =  daysinsequence +1 WHERE userid =" . $uid;
##Creating variable incrementseq which will have update query to update lastactivity day and daysinsequence incremented by 1 here.
$selectday = "SELECT lastdayactive AS lastactive FROM user WHERE userid =" . $uid;
##Creating selectday variable in which we are selecting lastdayactive fiedl value from user table as lasactive.
$resetday = "UPDATE user SET lastdayactive = (UNIX_TIMESTAMP()/(60*60*24)), daysinsequence =  1 WHERE userid =" . $uid;
##Creating restday(which will RESET values of lastdayactive and daysinsequence value to 1 like restarting count of logging of users.)

$day = $vbulletin->db->query_first($selectday);
##Creating day variable where we are running selectday query to get user's lastactiveday from DB.

##Checking condition if lasactive value is greater than yesterday and less than or equal to dayago * 2 value then run incrementseq query.
if ($day['lastactive'] > $dayago && $day['lastactive'] <= $dayago * 2) {
    $status = $vbulletin->db->query_write($incrementseq);
} 
##If lasactiveday value is greater than dayago * 2 value then call reset value for user's day's login.
elseif ($day['lastactive'] > $dayago * 2) {
    $status = $vbulletin->db->query_write($resetday);
}

##Creating variable getseq by select query to get user's updated daysactive value from user table.
$getseq = "SELECT daysinsequence AS daysactive FROM user WHERE userid =" . $uid;
##Run query to get user's active days value here.
$days = $vbulletin->db->query_first($getseq);

##checking conditions to assig badge color as per user's continous logging here.
if ($days['daysactive'] > 14) {
    $color['fahistory'] == 'black';
} elseif ($days['daysactive'] > 7) {
    $color['fahistory'] == 'indigo';
} elseif ($days['daysactive'] > 3) {
    $color['fahistory'] == 'blue';
} elseif ($days['daysactive'] > 1) {
    $color['fahistory'] == 'limegreen';
} else {
    $color['fahistory'] == 'lightgray';
}
$badgejs .= 'badge["fahistory"] = "' . $color['fahistory'] . '";';
$badgejs .= 'badge["fahistoryval"] = "' . number_format($days['daysactive']) . '";';

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 46  
Old 01-05-2019
Quote:
Originally Posted by Neo
Ravinder,
Please write a PHP function to parse the $color[] array and create the string(2) to append in the badge alert text.
Hint: You need to read a $serialized_badges JSON string (which we will get from the user table and convert it to a PHP array using json_decode() then perhaps a foreach() loop to compare against the current $color[] array.
Or, you can do all this in Javascript by storing the badge[] js array serialized in localstorage, which is how I would do it I think.... No reason to do this in PHP, so I think best to do in Javascript.
Sorry Neo I didn't get it Smilie if you could give me little more scenario like sample input and expected output then I will try my BEST to write it. Sorry I am that good like you in coding to be honest.

Thanks,
R. Singh
# 47  
Old 01-05-2019
Hi Ravinder,

It's OK. Thanks for the great ideas on the badging system.

No reason to worry about coding, as I know you are busy working 12 to 13 hours a day on your IT job.

I will code the "days active in sequence" badge when I am back at my desk after the 8th of Jan and will prototype a simple "new badge issued" alert based on a hash of the JSON string-ified badges afterwards.

You have already done a lot and I can easily take it from here and finish the prototype badging system and leave a few badges "in reserve" for the future.

Take care and keep sharing your good ideas for the forums!
This User Gave Thanks to Neo For This Post:
# 48  
Old 01-07-2019
Update:

While on vacation, had some free time and have written the core code to show a Bootstrap alert when a member badge changes. Also, have the basic code in place to determine exactly which badge changes, so that can be added to the message; but have not written the function to change the array keys to text.

Currently testing via PHP logging. Seems to work fine.

When I am back at my desk, will write a function to change array keys to user friendly text and continue testing.
# 49  
Old 01-07-2019
Testing, it seem the comparing cryptographic hashes of the badges array, serialized as a JSON file, do not provide as much consistency as comparing an array stored in the DB with a recently generated array using the PHP function array_diff_assoc():

Code:
array array_diff_assoc ( array $array1 , array $array2 [, array $... ] )

So, tomorrow I will refine the alert code to also use array_diff_assoc() .

Currently am comparing a generated hash with a stored hash as a cookie. This works well, but it could be even better, so I'm going to do both (I think), compare the hashes and also compare the badge arrays. Still testing ...

Also, I'm going to stop posting in the Underground and move this thread to the main forums.
This User Gave Thanks to Neo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. What is on Your Mind?

Badging System: UNIX.COM Bug Hunter Badge (New)

I have moved the bug badge out of reserve and into the main stream. Basically, I will assign a color level like the others, based on who has made a good actionable bug report for UNIX.COM. "Good" means screenshots, links, and even details from web dev tools our the HTML source code. So far,... (0 Replies)
Discussion started by: Neo
0 Replies

2. What is on Your Mind?

Status of Badging System - Beta 1

Dear All, Here is the current status of the badging system: The Beta 1 phase of the new badging system is close to completion. 42 prototype badges have been "allocated" 6 prototype badge slots are held in reserve The "alert you have new badges" prototype is running and is currently... (4 Replies)
Discussion started by: Neo
4 Replies

3. What is on Your Mind?

New Badging System - Badges Prototype Beta 1 (Badges Only)

Today I mapped out the new badging system using FA icons, Beta 1 in no particular order except a 6 x 8 grid: https://www.unix.com/members/1-albums215-picture991.png The prototype HTML code for this layout: <style> .fa-badge-grid { font-size: 1.5em; } .row { ... (38 Replies)
Discussion started by: Neo
38 Replies
Login or Register to Ask a Question