Jump to content

 

developer

Member Since 02 Jan 2012
Offline Last Active Mar 30 2014 07:21 AM
-----

#146317 implement a badge system in reward points addon

Posted by developer on 04 October 2012 - 09:18 AM

Hello All

I made a "Badge System" in my own way,

I need to make hooks for two events,
'update_profile'
'change_order_status',

I want to assign user to "Bronze" User Group once He/She registered, so i made hook for 'update_profile'. $bronzeUserGroupId is the id of that User Group in below function,
function fn_my_changes_update_profile($action, $user_data, $current_user_data) {
	    if($action == "add"){
		    /*  Assign User to Bronze Group	 */
		    $bronzeUserGroupId = 5;
		    $dataAry = array(
		    'user_id' => $user_data['user_id'],
		    'usergroup_id' => $bronzeUserGroupId,
		    'status' => 'A'
		    );
		    db_query("INSERT INTO ?:usergroup_links ?e", $dataAry);
		    /*  Assign User to Bronze Group	 */
	    }
    }

and after that i made hook to 'change_order_status' event, as reward points only added once order goes to "Complete" status,

function fn_my_changes_change_order_status($status_to, $status_from, $order_info, $force_notification, $order_statuses, $place_order) {
	    if($status_to == "C"){
		    if($order_info['user_id']) {
			    $userId = $order_info['user_id'];
			    /*  Generating Array of currently assigned Usergroups to User   */
			    $curUserGroups = array();
			    $selectCondition = array (
			    'user_id' => $userId
			    );
			    $resultData = db_get_array('SELECT * FROM ?:usergroup_links WHERE ?w', $selectCondition);
			    foreach($resultData as $rsltData)
			    {
				    $curUserGroups[$rsltData['usergroup_id']] = $rsltData['status'];
			    }
			    /*  Generating Array of currently assigned Usergroups to User   */
			    /*  Assign Your Custom User Group Ids Here  */
			    $userGroupIds = array (
			    'Platinum' => 8,
			    'Gold' => 7,
			    'Silver' => 6,
			    'Bronze' => 5
			    );
			    /*  Assign Your Custom User Group Ids Here  */
			    /*  Calculate Total Reward Points Amount of User    */
			    $resultData2 = db_get_row('SELECT SUM(amount) FROM ?:reward_point_changes WHERE user_id = ?i AND amount > 0', $userId);
			    $userTotalRewardPoints = $resultData2['SUM(amount)'];
			    /*  Calculate Total Reward Points Amount of User    */
			    if($userTotalRewardPoints > 10000)
			    {
				    /* Platinum User Group */
				    if(isset($curUserGroups[$userGroupIds['Platinum']])) {
					    if($curUserGroups[$userGroupIds['Platinum']] != 'A'){
						    /*  Update User to Platinum Group	 */
						    $updateData = array (
						    'status' => 'A'
						    );
						    $whereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Platinum']
						    );
						    db_query('UPDATE ?:usergroup_links SET ?u WHERE ?w', $updateData, $whereCondition);   
						    /*  Update User to Platinum Group	 */
						    /*  Remove User From Gold User Group    */
						    $deleteWhereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Gold']
						    );
						    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
						    /*  Remove User From Gold User Group    */
					    }
				    }
				    else {
					    /*  Assign User to Platinum Group	 */
					    $platinumAry = array(
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Platinum'],
					    'status' => 'A'
					    );
					    db_query("INSERT INTO ?:usergroup_links ?e", $platinumAry);
					    /*  Assign User to Platinum Group	 */
					    /*  Remove User From Gold User Group    */
					    $deleteWhereCondition = array (
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Gold']
					    );
					    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
					    /*  Remove User From Gold User Group    */
				    }
			    }
			    elseif($userTotalRewardPoints > 5000)
			    {
				    /* Gold User Group */
				    if(isset($curUserGroups[$userGroupIds['Gold']])) {
					    if($curUserGroups[$userGroupIds['Gold']] != 'A'){
						    /*  Update User to Gold Group	 */
						    $updateData = array (
						    'status' => 'A'
						    );
						    $whereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Gold']
						    );
						    db_query('UPDATE ?:usergroup_links SET ?u WHERE ?w', $updateData, $whereCondition);   
						    /*  Update User to Gold Group	 */
						    /*  Remove User From Silver User Group    */
						    $deleteWhereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Silver']
						    );
						    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
						    /*  Remove User From Silver User Group    */
					    }
				    }
				    else {
					    /*  Assign User to Gold Group	 */
					    $goldAry = array(
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Gold'],
					    'status' => 'A'
					    );
					    db_query("INSERT INTO ?:usergroup_links ?e", $goldAry);
					    /*  Assign User to Gold Group	 */
					    /*  Remove User From Silver User Group    */
					    $deleteWhereCondition = array (
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Silver']
					    );
					    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
					    /*  Remove User From Silver User Group    */
				    }
			    }
			    elseif($userTotalRewardPoints > 2500)
			    {
				    /* Silver User Group */
				    if(isset($curUserGroups[$userGroupIds['Silver']])) {
					    if($curUserGroups[$userGroupIds['Silver']] != 'A'){
						    /*  Update User to Silver Group	 */
						    $updateData = array (
						    'status' => 'A'
						    );
						    $whereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Silver']
						    );
						    db_query('UPDATE ?:usergroup_links SET ?u WHERE ?w', $updateData, $whereCondition);   
						    /*  Update User to Silver Group	 */
						    /*  Remove User From Bronze User Group    */
						    $deleteWhereCondition = array (
						    'user_id' => $userId,
						    'usergroup_id' => $userGroupIds['Bronze']
						    );
						    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
						    /*  Remove User From Bronze User Group    */
					    }
				    }
				    else {
					    /*  Assign User to Silver Group	 */
					    $SilverAry = array(
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Silver'],
					    'status' => 'A'
					    );
					    db_query("INSERT INTO ?:usergroup_links ?e", $SilverAry);
					    /*  Assign User to Silver Group	 */
					    /*  Remove User From Bronze User Group    */
					    $deleteWhereCondition = array (
					    'user_id' => $userId,
					    'usergroup_id' => $userGroupIds['Bronze']
					    );
					    db_query('DELETE FROM ?:usergroup_links WHERE ?w', $deleteWhereCondition);   
					    /*  Remove User From Bronze User Group    */
				    }
			    }
			    else
			    {
				    //Do Nothing
			    }
			    return true;
		    }
	    }
    }

You only need to specify all User Groups ids in $userGroupIds and Reward point's range in above function.