| @@ -1,0 +1,71 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Rules Engine | |
| 4 | + * | |
| 5 | + * @package GamiPress\WPLMS\Rules_Engine | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Checks if an user is allowed to work on a given requirement related to a minimum of score | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * | |
| 16 | + * @param bool $return The default return value | |
| 17 | + * @param int $user_id The given user's ID | |
| 18 | + * @param int $requirement_id The given requirement's post ID | |
| 19 | + * @param string $trigger The trigger triggered | |
| 20 | + * @param int $site_id The site id | |
| 21 | + * @param array $args Arguments of this trigger | |
| 22 | + * | |
| 23 | + * @return bool True if user has access to the requirement, false otherwise | |
| 24 | + */ | |
| 25 | +function gamipress_wplms_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) { | |
| 26 | + | |
| 27 | + // If we're not working with a requirement, bail here | |
| 28 | + if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) ) | |
| 29 | + return $return; | |
| 30 | + | |
| 31 | + // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet) | |
| 32 | + if( ! $return ) | |
| 33 | + return $return; | |
| 34 | + | |
| 35 | + // If is minimum score trigger, rules engine needs to check the minimum score | |
| 36 | + if( | |
| 37 | + $trigger === 'gamipress_wplms_complete_course_minimum_mark' | |
| 38 | + || $trigger === 'gamipress_wplms_complete_specific_course_minimum_mark' | |
| 39 | + || $trigger === 'gamipress_wplms_complete_quiz_minimum_mark' | |
| 40 | + || $trigger === 'gamipress_wplms_complete_specific_quiz_minimum_mark' | |
| 41 | + || $trigger === 'gamipress_wplms_complete_assignment_minimum_mark' | |
| 42 | + || $trigger === 'gamipress_wplms_complete_specific_assignment_minimum_mark' | |
| 43 | + ) { | |
| 44 | + | |
| 45 | + $score = $args[2]; | |
| 46 | + $required_score = absint( get_post_meta( $requirement_id, '_gamipress_wplms_score', true ) ); | |
| 47 | + | |
| 48 | + if( is_numeric( $score ) ) { | |
| 49 | + // Numeric score (X) | |
| 50 | + | |
| 51 | + $score = absint( $score ); | |
| 52 | + | |
| 53 | + // True if there is score is bigger than required score | |
| 54 | + $return = (bool) ( $score >= $required_score ); | |
| 55 | + | |
| 56 | + } else if( strpos( $score, '-' ) !== false ) { | |
| 57 | + // Score range (X-Y) | |
| 58 | + | |
| 59 | + $score_range = explode( '-', $score ); | |
| 60 | + | |
| 61 | + // True if there is maximum score in range is bigger than required score | |
| 62 | + $return = (bool) ( $score_range[1] >= $required_score ); | |
| 63 | + } | |
| 64 | + | |
| 65 | + } | |
| 66 | + | |
| 67 | + // Send back our eligibility | |
| 68 | + return $return; | |
| 69 | + | |
| 70 | +} | |
| 71 | +add_filter( 'user_has_access_to_achievement', 'gamipress_wplms_user_has_access_to_achievement', 10, 6 ); | |