PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
← All changes | integrations/h5p/includes/rules-engine.php +204 -0 7.8.8 → 8.0.5 View file →
@@ -1,0 +1,204 @@
1 +<?php
2 +/**
3 + * Rules Engine
4 + *
5 + * @package GamiPress\H5P\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.1.2
15 + *
16 + * @param int $requirement_id
17 + * @param string $trigger
18 + * @param array $args
19 + *
20 + * @return bool
21 + */
22 +function gamipress_h5p_check_if_meets_requirements( $requirement_id, $trigger, $args ) {
23 +
24 + // Initialize the return value
25 + $return = true;
26 +
27 + // If is specific content type trigger, rules engine needs to check the content type
28 + if( $trigger === 'gamipress_h5p_complete_specific_content_type'
29 + || $trigger === 'gamipress_h5p_max_complete_specific_content_type'
30 + || $trigger === 'gamipress_h5p_complete_specific_content_type_min_score'
31 + || $trigger === 'gamipress_h5p_complete_specific_content_type_max_score'
32 + || $trigger === 'gamipress_h5p_complete_specific_content_type_between_score'
33 + || $trigger === 'gamipress_h5p_complete_specific_content_type_min_percentage'
34 + || $trigger === 'gamipress_h5p_complete_specific_content_type_max_percentage' ) {
35 +
36 + $content_type = $args[3];
37 +
38 + $required_content_type = get_post_meta( $requirement_id, '_gamipress_h5p_content_type', true );
39 +
40 + // True if content type is the required content type
41 + $return = (bool) ( $content_type === $required_content_type );
42 + }
43 +
44 + // If is minimum score trigger, rules engine needs to check the minimum score
45 + if( $return && ( $trigger === 'gamipress_h5p_complete_content_min_score'
46 + || $trigger === 'gamipress_h5p_complete_specific_content_min_score'
47 + || $trigger === 'gamipress_h5p_complete_specific_content_type_min_score'
48 + || $trigger === 'gamipress_h5p_complete_content_min_score_tag' ) ) {
49 +
50 + $score = absint( $args[4] );
51 +
52 + $required_score = absint( get_post_meta( $requirement_id, '_gamipress_h5p_score', true ) );
53 +
54 + // True if there is score is bigger than required score
55 + $return = (bool) ( $score >= $required_score );
56 + }
57 +
58 + // If is maximum score trigger, rules engine needs to check the maximum score
59 + if( $return && ( $trigger === 'gamipress_h5p_complete_content_max_score'
60 + || $trigger === 'gamipress_h5p_complete_specific_content_max_score'
61 + || $trigger === 'gamipress_h5p_complete_specific_content_type_max_score'
62 + || $trigger === 'gamipress_h5p_complete_content_max_score_tag' ) ) {
63 +
64 + $score = absint( $args[4] );
65 +
66 + $required_score = absint( get_post_meta( $requirement_id, '_gamipress_h5p_score', true ) );
67 +
68 + // True if there is score is lower than required score
69 + $return = (bool) ( $score <= $required_score );
70 + }
71 +
72 + // If is between score trigger, rules engine needs to check if score is between range of scores
73 + if( $return && ( $trigger === 'gamipress_h5p_complete_content_between_score'
74 + || $trigger === 'gamipress_h5p_complete_specific_content_between_score'
75 + || $trigger === 'gamipress_h5p_complete_specific_content_type_between_score'
76 + || $trigger === 'gamipress_h5p_complete_content_between_score_tag' ) ) {
77 +
78 + $score = absint( $args[4] );
79 +
80 + $min_score = absint( get_post_meta( $requirement_id, '_gamipress_h5p_min_score', true ) );
81 + $max_score = absint( get_post_meta( $requirement_id, '_gamipress_h5p_max_score', true ) );
82 +
83 + // True if there is score is bigger than min score and lower than max score
84 + $return = (bool) ( $score >= $min_score && $score <= $max_score );
85 + }
86 +
87 + // If is minimum percentage trigger, rules engine needs to check the minimum percentage
88 + if( $return && ( $trigger === 'gamipress_h5p_complete_content_min_percentage'
89 + || $trigger === 'gamipress_h5p_complete_specific_content_min_percentage'
90 + || $trigger === 'gamipress_h5p_complete_specific_content_type_min_percentage'
91 + || $trigger === 'gamipress_h5p_complete_content_min_percentage_tag' ) ) {
92 +
93 + $score = absint( $args[4] );
94 + $max_score = absint( $args[5] );
95 + $percentage = ( $score / $max_score ) * 100;
96 +
97 + $required_percentage = absint( get_post_meta( $requirement_id, '_gamipress_h5p_percentage', true ) );
98 +
99 + // True if there is percentage is bigger than required percentage
100 + $return = (bool) ( $percentage >= $required_percentage );
101 + }
102 +
103 + // If is maximum percentage trigger, rules engine needs to check the minimum percentage
104 + if( $return && ( $trigger === 'gamipress_h5p_complete_content_max_percentage'
105 + || $trigger === 'gamipress_h5p_complete_specific_content_max_percentage'
106 + || $trigger === 'gamipress_h5p_complete_specific_content_type_max_percentage'
107 + || $trigger === 'gamipress_h5p_complete_content_max_percentage_tag' ) ) {
108 +
109 + $score = absint( $args[4] );
110 + $max_score = absint( $args[5] );
111 + $percentage = ( $score / $max_score ) * 100;
112 +
113 + $required_percentage = absint( get_post_meta( $requirement_id, '_gamipress_h5p_percentage', true ) );
114 +
115 + // True if there is percentage is bigger than required percentage
116 + $return = (bool) ( $percentage <= $required_percentage );
117 + }
118 +
119 + // If is between percentage trigger, rules engine needs to check the minimum and maximum percentage
120 + if( $return && ( $trigger === 'gamipress_h5p_complete_content_between_percentage'
121 + || $trigger === 'gamipress_h5p_complete_specific_content_between_percentage'
122 + || $trigger === 'gamipress_h5p_complete_specific_content_type_between_percentage'
123 + || $trigger === 'gamipress_h5p_complete_content_between_percentage_tag' ) ) {
124 +
125 + $score = absint( $args[4] );
126 + $max_score = absint( $args[5] );
127 + $percentage = ( $score / $max_score ) * 100;
128 +
129 + $min_percentage = absint( get_post_meta( $requirement_id, '_gamipress_h5p_min_percentage', true ) );
130 + $max_percentage = absint( get_post_meta( $requirement_id, '_gamipress_h5p_max_percentage', true ) );
131 +
132 + // True if there is score is bigger than min percentage and lower than max percentage
133 + $return = (bool) ( $percentage >= $min_percentage && $percentage <= $max_percentage );
134 + }
135 +
136 + return $return;
137 +
138 +}
139 +
140 +/**
141 + * Filter triggered requirements to reduce the number of requirements to check by the awards engine
142 + *
143 + * @since 1.0.6
144 + *
145 + * @param array $triggered_requirements
146 + * @param integer $user_id
147 + * @param string $trigger
148 + * @param integer $site_id
149 + * @param array $args
150 + *
151 + * @return array
152 + */
153 +function gamipress_h5p_filter_triggered_requirements( $triggered_requirements, $user_id, $trigger, $site_id, $args ) {
154 +
155 + $new_requirements = array();
156 +
157 + foreach( $triggered_requirements as $i => $requirement ) {
158 +
159 + // Skip item
160 + if ( ! gamipress_h5p_check_if_meets_requirements( $requirement->ID, $trigger, $args ) ) {
161 + continue;
162 + }
163 +
164 + // Keep the requirement on the list of requirements to check by the awards engine
165 + $new_requirements[] = $requirement;
166 +
167 + }
168 +
169 + return $new_requirements;
170 +
171 +}
172 +add_filter( 'gamipress_get_triggered_requirements', 'gamipress_h5p_filter_triggered_requirements', 20, 5 );
173 +
174 +/**
175 + * Checks if an user is allowed to work on a given requirement related to a content type
176 + *
177 + * @since 1.0.0
178 + *
179 + * @param bool $return The default return value
180 + * @param int $user_id The given user's ID
181 + * @param int $requirement_id The given requirement's post ID
182 + * @param string $trigger The trigger triggered
183 + * @param int $site_id The site id
184 + * @param array $args Arguments of this trigger
185 + *
186 + * @return bool True if user has access to the requirement, false otherwise
187 + */
188 +function gamipress_h5p_user_has_access_to_achievement( $return = false, $user_id = 0, $requirement_id = 0, $trigger = '', $site_id = 0, $args = array() ) {
189 +
190 + // If we're not working with a requirement, bail here
191 + if ( ! in_array( get_post_type( $requirement_id ), gamipress_get_requirement_types_slugs() ) ) {
192 + return $return;
193 + }
194 +
195 + // Check if user has access to the achievement ($return will be false if user has exceed the limit or achievement is not published yet)
196 + if ( ! $return ) {
197 + return $return;
198 + }
199 +
200 + // Send back our eligibility
201 + return gamipress_h5p_check_if_meets_requirements( $requirement_id, $trigger, $args );
202 +
203 +}
204 +add_filter( 'user_has_access_to_achievement', 'gamipress_h5p_user_has_access_to_achievement', 10, 6 );