4 Replies - 651 Views - Last Post: 29 June 2015 - 05:28 PM Rate Topic: -----

#1 Arielle*15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-June 15

Customize code for first connection follower

Posted 29 June 2015 - 03:55 PM

Hello, I'm trying to customize some code that awards a member 1 credit point for a new follower. I want to award members points only when the new follower is not following anyone else. It must be a first connection. A person can follow any number of people but only the first connection gets rewarded.

'new_follow' => array('creds'      => 1,
	'log'        => '%plural% for following a member',
	'limit'      => 1
	),


	public function run() {

			if ( ! is_user_logged_in() ) return;
// Follow
			if ( $this->prefs['new_follow']['creds'] != $this->core->zero() || $this->prefs['new_follower']['creds'] != $this->core->zero() )
				add_action( 'wp_ajax_send_follow_request', array( $this, 'following' ), 1 );

/**
		 * User Is Following 
		
		 */
		public function user_is_following( $user_id, $follow_id ) {

			global $wpdb;

			$check = $wpdb->get_var( $wpdb->prepare( "
				SELECT follower_id 
				FROM {$wpdb->prefix}usersultra_followers 
				WHERE follower_user_id = %d 
				AND follower_following_user_id = %d;", $follow_id, $user_id ) );

			if ( $check === NULL || $check == '' || $check == 0 )
				return false;

			return true;

		}

		/**
		 * Following 
		 * @since 1.0
		 * @version 1.0
		 */
		public function following() {

			$cui = get_current_user_id();
			$user_to_follow = $_POST['user_id'];

			// We can not follow our own account
			if ( $cui == $user_to_follow ) return;

			// Check if user is excluded
			if ( $this->core->exclude_user( $cui ) ) return;

			// Make sure we are not already following current user
			if ( $this->user_is_following( $cui, $user_to_follow ) ) return;
                        
                       [color="#FF0000"] // Make sure we are not already following any user
			if ( $this->user_is_following( $cui, $user ) ) return; [/color]

			// Award the act of following
			if ( $this->prefs['new_follow']['creds'] != $this->core->zero() ) {

				// Enfoce limit (if used)
				if ( $this->exceeds_limit( 'new_follow', $cui ) ) return;

				// Execute
				$this->core->add_creds(
					'following_member',
					$cui,
					$this->prefs['new_follow']['creds'],
					$this->prefs['new_follow']['log'],
					$user_to_follow,
					array( 'ref_type' => 'user' ),
					$this->mycred_type
				);

			}

			// Award the user being followed for this act
			if ( $this->prefs['new_follower']['creds'] != $this->core->zero() ) {

				// Enfoce limit (if used)
				if ( $this->exceeds_limit( 'new_follower', $user_to_follow ) ) return;

				// Execute
				$this->core->add_creds(
					'new_follower',
					$user_to_follow,
					$this->prefs['new_follower']['creds'],
					$this->prefs['new_follower']['log'],
					$cui,
					array( 'ref_type' => 'user' ),
					$this->mycred_type
				);

			}

		}

		public function check_ip( $ip, $profile_id ) {

			global $wpdb;

			$check = $wpdb->get_row( $wpdb->prepare( "
				SELECT * 
				FROM {$wpdb->prefix}usersultra_ajaxrating_vote 
				WHERE ajaxrating_vote_ip = %s AND ajaxrating_user_id = %s;", $ip, $profile_id ) );

			if ( ! is_object( $check ) )
				return true;

			return false;

		}





Is This A Good Question/Topic? 0
  • +

Replies To: Customize code for first connection follower

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: Customize code for first connection follower

Posted 29 June 2015 - 04:08 PM

$this->core->zero()

One sign that your application design might be a little bit out of control is when you need to reference 2 objects and a method to get to 0.

Your user_is_following method has a query to check for a relationship between two specific users. You can copy that and modify it slightly so that it's only checking for one user (if they are following anyone).
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Customize code for first connection follower

Posted 29 June 2015 - 04:31 PM

Clarification... the new follower following NO ONE else or the new follower is not following anyone that the "followed" has as a follower? If it is the first you can just get the count of followers from the new follower and if that is zero, award points. But I am assuming you mean the latter which case you have to get a list of followers that the "followed" has and those being followed by the new follower and see if there is any intersections.

To do this in MySQL you are going to have to use an inner join...

// Get a list of followers for the "target" being followed
SELECT id
From followers 
where user_being_followed_id = <id here>

// Get a list of users who are being followed by the new follower
select id
from followers
where new_user_doing_following_id = <id here>

// Now merge the two in a join...

select id 
from followers join (select id
  from followers
  where new_user_doing_following_id = <id here>) as followedByNewFollower
on followers.id = followedByNewFollower.id
WHERE user_being_followed_id = <id here>




Here we are trying to say that first get all those users being followed by the new follower and we will call that list of ids "followedByNewFollower". We will then join that list against another list of all those followers of the target user now being followed.

So lets say Jan is now becoming a follower of Tom. We get a list of all those users who Jan currently follows. We link that to a list of users who already follow Tom. If any of the ids match, we know that Jan is following someone who already follows Tom and thus not a first connection.

Make sense?
Was This Post Helpful? 1
  • +
  • -

#4 Arielle*15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-June 15

Re: Customize code for first connection follower

Posted 29 June 2015 - 04:56 PM

Hello Martyr2
Thank you for your response. It actually is the first scenario. Lets say I join as a member and follow you. you get a point if I'm not following someone else. If I then go and follow Tom or Jan or anyone else they do not get a point which makes you my first connection. Only first connections awards points.
I'm trying to put it in a Word press site.

Thank you

This post has been edited by Dormilich: 30 June 2015 - 12:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 Arielle*15   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-June 15

Re: Customize code for first connection follower

Posted 29 June 2015 - 05:28 PM

View PostArtificialSoldier, on 29 June 2015 - 04:08 PM, said:

$this->core->zero()

One sign that your application design might be a little bit out of control is when you need to reference 2 objects and a method to get to 0.

Your user_is_following method has a query to check for a relationship between two specific users. You can copy that and modify it slightly so that it's only checking for one user (if they are following anyone).


Thank you ArtificialSoldier will try your suggestion.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1