← All changes
|
integrations/thrive-quiz-builder/includes/rules-engine.php
+117
-0
7.9.2
→
8.0.3
View file →
| @@ -1,0 +1,117 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Rules Engine | |
| 4 | + * | |
| 5 | + * @package GamiPress\LearnDash\Rules_Engine | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Checks if given requirement meets the requirements of triggered event | |
| 13 | + * | |
| 14 | + * @since 1.2.5 | |
| 15 | + * | |
| 16 | + * @param int $requirement_id | |
| 17 | + * @param string $trigger | |
| 18 | + * @param array $args | |
| 19 | + * | |
| 20 | + * @return bool | |
| 21 | + */ | |
| 22 | +function gamipress_thrive_quiz_builder_check_if_meets_requirements( $requirement_id, $trigger, $args ) { | |
| 23 | + | |
| 24 | + // Initialize the return value | |
| 25 | + $return = true; | |
| 26 | + | |
| 27 | + // If is minimum score trigger, rules engine needs to check the minimum score | |
| 28 | + if( $trigger === 'gamipress_thrive_quiz_builder_complete_percentage_quiz' | |
| 29 | + || $trigger === 'gamipress_thrive_quiz_builder_complete_specific_percentage_quiz' ) { | |
| 30 | + | |
| 31 | + $percentage = absint( $args[2] ); | |
| 32 | + | |
| 33 | + $condition = get_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_percentage_condition', true ); | |
| 34 | + $required_percentage = absint( get_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_percentage', true ) ); | |
| 35 | + | |
| 36 | + // True if there is score is bigger than required score | |
| 37 | + $return = (bool) ( gamipress_number_condition_matches( $percentage, $required_percentage, $condition ) ); | |
| 38 | + } | |
| 39 | + | |
| 40 | + // If is between score trigger, rules engine needs to check the minimum and maximum score allowed | |
| 41 | + if( $trigger === 'gamipress_thrive_quiz_builder_complete_quiz_type') { | |
| 42 | + | |
| 43 | + $quiz_type = $args[2]; | |
| 44 | + | |
| 45 | + $required_quiz_type = get_post_meta( $requirement_id, '_gamipress_thrive_quiz_builder_quiz_type', true ); | |
| 46 | + | |
| 47 | + // True if there is score is bigger than min score and lower than max score | |
| 48 | + $return = (bool) ( $quiz_type !== $required_quiz_type ); | |
| 49 | + } | |
| 50 | + | |
| 51 | + return $return; | |
| 52 | + | |
| 53 | +} | |
| 54 | + | |
| 55 | +/** | |
| 56 | + * Filter triggered requirements to reduce the number of requirements to check by the awards engine | |
| 57 | + * | |
| 58 | + * @since 1.2.5 | |
| 59 | + * | |
| 60 | + * @param array $triggered_requirements | |
| 61 | + * @param integer $user_id | |
| 62 | + * @param string $trigger | |
| 63 | + * @param integer $site_id | |
| 64 | + * @param array $args | |
| 65 | + * | |
| 66 | + * @return array | |
| 67 | + */ | |
| 68 | +function gamipress_thrive_quiz_builder_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) { | |
| 69 | + | |
| 70 | + $new_requirements = array(); | |
| 71 | + | |
| 72 | + foreach( $triggered_requirements as $i => $requirement ) { | |
| 73 | + | |
| 74 | + // Skip item | |
| 75 | + if( ! gamipress_thrive_quiz_builder_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) { | |
| 76 | + continue; | |
| 77 | + } | |
| 78 | + | |
| 79 | + // Keep the requirement on the list of requirements to check by the awards engine | |
| 80 | + $new_requirements[] = $requirement; | |
| 81 | + | |
| 82 | + } | |
| 83 | + | |
| 84 | + return $new_requirements; | |
| 85 | + | |
| 86 | +} | |
| 87 | +add_filter( 'gamipress_get_triggered_requirements', 'gamipress_thrive_quiz_builder_filter_triggered_requirements', 20, 5 ); | |
| 88 | + | |
| 89 | +/** | |
| 90 | + * Checks if an user is allowed to work on a given requirement related to a minimum of score | |
| 91 | + * | |
| 92 | + * @since 1.0.0 | |
| 93 | + * | |
| 94 | + * @param bool $return The default return value | |
| 95 | + * @param int $user_id The given user's ID | |
| 96 | + * @param int $requirement_id The given requirement's post ID | |
| 97 | + * @param string $trigger The trigger triggered | |
| 98 | + * @param int $site_id The site id | |
| 99 | + * @param array $args Arguments of this trigger | |
| 100 | + * | |
| 101 | + * @return bool True if user has access to the requirement, false otherwise | |
| 102 | + */ | |
| 103 | +function gamipress_thrive_quiz_builder_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) { | |
| 104 | + | |
| 105 | + // If we're not working with a requirement, bail here | |
| 106 | + if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) ) | |
| 107 | + return $return; | |
| 108 | + | |
| 109 | + // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet) | |
| 110 | + if( ! $return ) | |
| 111 | + return $return; | |
| 112 | + | |
| 113 | + // Send back our eligibility | |
| 114 | + return gamipress_thrive_quiz_builder_check_if_meets_requirements( $requirement_id, $trigger, $args ); | |
| 115 | + | |
| 116 | +} | |
| 117 | +add_filter( 'user_has_access_to_achievement', 'gamipress_thrive_quiz_builder_user_has_access_to_achievement', 10, 6 ); | |