| 1 |
<?php |
| 2 |
/** |
| 3 |
* Rules Engine |
| 4 |
* |
| 5 |
* @package GamiPress\QSM\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.0.0 |
| 15 |
* |
| 16 |
* @param int $requirement_id |
| 17 |
* @param string $trigger |
| 18 |
* @param array $args |
| 19 |
* |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
function gamipress_qsm_check_if_meets_requirements( $requirement_id, $trigger, $args ) { |
| 23 |
|
| 24 |
// Initialize the return value |
| 25 |
$return = true; |
| 26 |
|
| 27 |
// If is minimum points trigger, rules engine needs to check the minimum points |
| 28 |
if( $trigger === 'gamipress_qsm_complete_quiz_points' |
| 29 |
|| $trigger === 'gamipress_qsm_complete_specific_quiz_points' ) { |
| 30 |
|
| 31 |
$points = absint( $args[2] ); |
| 32 |
$required_points = absint( get_post_meta( $requirement_id, '_gamipress_qsm_points', true ) ); |
| 33 |
|
| 34 |
// True if there is points is bigger than required points |
| 35 |
$return = (bool) ( $points >= $required_points ); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
// If is maximum points trigger, rules engine needs to check the maximum points |
| 40 |
if( $trigger === 'gamipress_qsm_complete_quiz_max_points' |
| 41 |
|| $trigger === 'gamipress_qsm_complete_specific_quiz_max_points' ) { |
| 42 |
|
| 43 |
$points = absint( $args[2] ); |
| 44 |
|
| 45 |
$required_points = absint( get_post_meta( $requirement_id, '_gamipress_qsm_points', true ) ); |
| 46 |
|
| 47 |
// True if there is points is bigger than required points |
| 48 |
$return = (bool) ( $points <= $required_points ); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
// If is between points trigger, rules engine needs to check the minimum and maximum points allowed |
| 53 |
if( $trigger === 'gamipress_qsm_complete_quiz_between_points' |
| 54 |
|| $trigger === 'gamipress_qsm_complete_specifc_quiz_between_points' ) { |
| 55 |
|
| 56 |
$points = absint( $args[2] ); |
| 57 |
|
| 58 |
$min_points = absint( get_post_meta( $requirement_id, '_gamipress_qsm_min_points', true ) ); |
| 59 |
$max_points = absint( get_post_meta( $requirement_id, '_gamipress_qsm_max_points', true ) ); |
| 60 |
|
| 61 |
// True if there is points is bigger than required points |
| 62 |
$return = (bool) ( $points >= $min_points && $points <= $max_points ); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
return $return; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Filter triggered requirements to reduce the number of requirements to check by the awards engine |
| 71 |
* |
| 72 |
* @since 1.0.0 |
| 73 |
* |
| 74 |
* @param array $triggered_requirements |
| 75 |
* @param integer $user_id |
| 76 |
* @param string $trigger |
| 77 |
* @param integer $site_id |
| 78 |
* @param array $args |
| 79 |
* |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
function gamipress_qsm_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) { |
| 83 |
|
| 84 |
$new_requirements = array(); |
| 85 |
|
| 86 |
foreach( $triggered_requirements as $i => $requirement ) { |
| 87 |
|
| 88 |
// Skip item |
| 89 |
if( ! gamipress_qsm_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
// Keep the requirement on the list of requirements to check by the awards engine |
| 94 |
$new_requirements[] = $requirement; |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
return $new_requirements; |
| 99 |
|
| 100 |
} |
| 101 |
add_filter( 'gamipress_get_triggered_requirements', 'gamipress_qsm_filter_triggered_requirements', 20, 5 ); |
| 102 |
|
| 103 |
/** |
| 104 |
* Checks if a user is allowed to work on a given requirement related to a minimum of points |
| 105 |
* |
| 106 |
* @since 1.0.0 |
| 107 |
* |
| 108 |
* @param bool $return The default return value |
| 109 |
* @param int $user_id The given user's ID |
| 110 |
* @param int $requirement_id The given requirement's post ID |
| 111 |
* @param string $trigger The trigger triggered |
| 112 |
* @param int $site_id The site id |
| 113 |
* @param array $args Arguments of this trigger |
| 114 |
* |
| 115 |
* @return bool True if user has access to the requirement, false otherwise |
| 116 |
*/ |
| 117 |
function gamipress_qsm_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) { |
| 118 |
|
| 119 |
// If we're not working with a requirement, bail here |
| 120 |
if( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) ) |
| 121 |
return $return; |
| 122 |
|
| 123 |
// Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet) |
| 124 |
if( ! $return ) |
| 125 |
return $return; |
| 126 |
|
| 127 |
// Send back our eligibility |
| 128 |
return gamipress_qsm_check_if_meets_requirements( $requirement_id, $trigger, $args ); |
| 129 |
|
| 130 |
} |
| 131 |
add_filter( 'user_has_access_to_achievement', 'gamipress_qsm_user_has_access_to_achievement', 10, 6 ); |