Support » Plugin: Gravity Forms Personality Quiz Add-On » Tie Breakers via ACF?

  • Resolved PDidee

    (@pdidee)


    Hi Collin & Dabernathy,

    I work as a front end developer and am currently using your quiz add-on for a site I and my team are building. We needed to apply weights to more important questions for the quiz in order to make a “tiebreaker” rather than have the tie breaker be chosen randomly, so we added some code to your php class and set the important questions to loop through via ACF fields.

    Here is the code:

    /* BEGINNING OF CUSTOM */
    
          	// A+A addition: get tiebreaker field
          	$tiebreaker1 = get_field('tiebreaker1', 'option');
          	$tiebreaker2 = get_field('tiebreaker2', 'option');
          	$tiebreaker3 = get_field('tiebreaker3', 'option');
          	$tiebreaker4 = get_field('tiebreaker4', 'option');
    
          	// if tiebreaker1 exists, set final with Array ID, else set at ID 0
            if ($tiebreaker1) {
          		$tiebreaker_next1 =  $tiebreaker1;
              $tiebreaker_final1 = $lead[$fields[$tiebreaker_next1]->id];
          	} else {
          		$tiebreaker_final1 = '';
          	}
          	// if tiebreaker2 exists, set final with Array ID, else set at ID 0
          	if ($tiebreaker2) {
          		$tiebreaker_next2 =  $tiebreaker2;
              $tiebreaker_final2 = $lead[$fields[$tiebreaker_next2]->id];
          	} else {
          		$tiebreaker_final2 = '';
          	}
          	// if tiebreaker3 exists, set final with Array ID, else set at ID 0
          	if ($tiebreaker3) {
          		$tiebreaker_next3 =  $tiebreaker3;
              $tiebreaker_final3 = $lead[$fields[$tiebreaker_next3]->id];
          	} else {
          		$tiebreaker_final3 = '';
          	}
          	// if tiebreaker4 exists, set final with Array ID, else set at ID 0
          	if ($tiebreaker4) {
          		$tiebreaker_next4 =  $tiebreaker4;
              $tiebreaker_final4 = $lead[$fields[$tiebreaker_next4]->id];
          	} else {
          		$tiebreaker_final4 = '';
          	}
          	// invoke only if more than 1 winner
          	if ( count($winners) > 1 ) {
          			// if the tiebreaker1 answer is a winner, print it
                if (in_array($tiebreaker_final1, $winners)) {
                  return $tiebreaker_final1;
          			// if the tiebreaker2 answer is a winner, print it
          			} elseif (in_array($tiebreaker_final2, $winners)) {
                  return $tiebreaker_final2;
          			// if the tiebreaker3 answer is a winner, print it
          			} elseif (in_array($tiebreaker_final3, $winners)) {
          				return $tiebreaker_final3;
          			// if the tiebreaker4 answer is a winner, print it
          			} elseif (in_array($tiebreaker_final4, $winners)) {
          				return $tiebreaker_final4;
          			// otherwise do the regular thing
          			} else {
          			shuffle($winners);
                  	return $winners[0];
          			}
          	// else resume normal operation
          	} else {
    
                  // return the winner, or if it was a tie, a random winner
                   shuffle($winners);
                   return $winners[0];
    
          	}
        }
    
    /* END OF CUSTOM */

    Problem we are having is that ONLY the first tiebreaker is working and ONLY for the first quiz. We have two quizzes with the same weighted questions, however only the first tiebreaker for the first quiz seems to be working. Would you be able to give us some guidance on what we can do to fix the issue? Any help would be much appreciated.

    Look forward to hearing from you.
    Phillip

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author dabernathy89

    (@dabernathy89)

    Hey Phillip – what values are you using in the custom fields? It looks like they’re supposed to be indices from the $form[‘fields’] array, is that right?

    Also – consider adding boolean ‘true’ as the 3rd parameter to the in_array() function, which will use strict comparison. Hard to say if it will help since i’m not 100% sure what the code is doing here.

    Hi Dabernathy,

    Thanks you so much for your response I did try adding the additional boolean as a 3rd parameter sadly to no avail. The additional code was added to your class-gravity-forms-personality-quiz-addon.php file.

    To clarify, basically we’ve created 4 fields in ACF to add more “weight” to specific questions within the quiz, to essentially ‘force’ a tiebreaker rather then use the random option. Within the backend ACF fields we store the numbers of the questions in order of importance (ie. field one has the number 2 in it to denote that question 2 is the most important question in the event of a tiebreaker).

    Each answer has a specific letter associated with it N,I,S or D. In the event of a tie (ie. same number of “N’s” & “I’s” chosen) the code should cycle through and see if first tiebreaker question (question 2 in this case) has a value of either N or I. If so, the that letter would be the final result. If neither N or I was chosen for question 2 the tiebreaker code should then move on to the next highest weighted question and preform the same test, and so on.

    Given that the current number of questions is currently only 10, it’s highly improbably we will need more than two tiebreaker scenarios, but at the moment only the one seems to be working for ONLY the first quiz.

    Please let me know if you need further clarification or to see the code and test it online yourself, I’d be happy to send you the links and files via email.

    Thanks again for your help,
    Phillip

    Plugin Author dabernathy89

    (@dabernathy89)

    The only issue I can see is that your code is assuming that the number held in the custom field corresponds to the array index of the field inside the $form['fields'] array. I’m looking at this part:

    $fields[$tiebreaker_next1]

    It might be better to store the ID of the GF field in the ACF custom field. You would need to loop through the fields each time to check, but you could extract that to a function like get_gf_field_by_id( $id, $fields ) or something.

    I’m confused about how the array index of the field relates to the field ID. We tried targeting the field IDs and it didn’t seem to be working that way.

    Hey Dabernathy,

    We got this working seems that having questions prior to the quiz questions were interfering with the quiz results. By moving them to the bottom, the tiebreakers now work as expected.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Tie Breakers via ACF?’ is closed to new replies.