| @@ -1,0 +1,230 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Requirements | |
| 4 | + * | |
| 5 | + * @package GamiPress\H5P\Requirements | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Add the content type field to the requirement object | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + * | |
| 17 | + * @param $requirement | |
| 18 | + * @param $requirement_id | |
| 19 | + * | |
| 20 | + * @return array | |
| 21 | + */ | |
| 22 | +function gamipress_h5p_requirement_object( $requirement, $requirement_id ) { | |
| 23 | + | |
| 24 | + if( isset( $requirement['trigger_type'] ) | |
| 25 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type' | |
| 26 | + || $requirement['trigger_type'] === 'gamipress_h5p_max_complete_specific_content_type' | |
| 27 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_min_score' | |
| 28 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_max_score' | |
| 29 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_between_score' | |
| 30 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_min_percentage' | |
| 31 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_max_percentage' | |
| 32 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_between_percentage' ) ) { | |
| 33 | + | |
| 34 | + // Specific content type | |
| 35 | + $requirement['h5p_content_type'] = get_post_meta( $requirement_id, '_gamipress_h5p_content_type', true ); | |
| 36 | + } | |
| 37 | + | |
| 38 | + if( isset( $requirement['trigger_type'] ) | |
| 39 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_content_min_score' | |
| 40 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_min_score' | |
| 41 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_min_score' | |
| 42 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_min_score_tag' | |
| 43 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_max_score' | |
| 44 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_max_score' | |
| 45 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_max_score' | |
| 46 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_max_score_tag' ) ) { | |
| 47 | + | |
| 48 | + // Min/max score | |
| 49 | + $requirement['h5p_score'] = get_post_meta( $requirement_id, '_gamipress_h5p_score', true ); | |
| 50 | + } | |
| 51 | + | |
| 52 | + if( isset( $requirement['trigger_type'] ) | |
| 53 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_content_between_score' | |
| 54 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_between_score' | |
| 55 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_between_score' | |
| 56 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_between_score_tag' ) ) { | |
| 57 | + | |
| 58 | + // Between score | |
| 59 | + $requirement['h5p_min_score'] = get_post_meta( $requirement_id, '_gamipress_h5p_min_score', true ); | |
| 60 | + $requirement['h5p_max_score'] = get_post_meta( $requirement_id, '_gamipress_h5p_max_score', true ); | |
| 61 | + | |
| 62 | + } | |
| 63 | + | |
| 64 | + if( isset( $requirement['trigger_type'] ) | |
| 65 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_content_min_percentage' | |
| 66 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_min_percentage' | |
| 67 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_min_percentage' | |
| 68 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_min_percentage_tag' | |
| 69 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_max_percentage' | |
| 70 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_max_percentage' | |
| 71 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_max_percentage' | |
| 72 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_max_percentage_tag' ) ) { | |
| 73 | + | |
| 74 | + // Percentage score | |
| 75 | + $requirement['h5p_percentage'] = get_post_meta( $requirement_id, '_gamipress_h5p_percentage', true ); | |
| 76 | + } | |
| 77 | + | |
| 78 | + if( isset( $requirement['trigger_type'] ) | |
| 79 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_content_between_percentage' | |
| 80 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_between_percentage' | |
| 81 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_between_percentage' | |
| 82 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_between_percentage_tag' ) ) { | |
| 83 | + | |
| 84 | + // Percentage score | |
| 85 | + $requirement['h5p_min_percentage'] = get_post_meta( $requirement_id, '_gamipress_h5p_min_percentage', true ); | |
| 86 | + $requirement['h5p_max_percentage'] = get_post_meta( $requirement_id, '_gamipress_h5p_max_percentage', true ); | |
| 87 | + } | |
| 88 | + | |
| 89 | + return $requirement; | |
| 90 | +} | |
| 91 | +add_filter( 'gamipress_requirement_object', 'gamipress_h5p_requirement_object', 10, 2 ); | |
| 92 | + | |
| 93 | +/** | |
| 94 | + * Content type field on requirements UI | |
| 95 | + * | |
| 96 | + * @since 1.0.0 | |
| 97 | + * | |
| 98 | + * @param $requirement_id | |
| 99 | + * @param $post_id | |
| 100 | + */ | |
| 101 | +function gamipress_h5p_requirement_ui_fields( $requirement_id, $post_id ) { | |
| 102 | + | |
| 103 | + global $wpdb; | |
| 104 | + | |
| 105 | + // Get active libraries | |
| 106 | + $content_types = $wpdb->get_results( $wpdb->prepare( | |
| 107 | + "SELECT l.name, l.title | |
| 108 | + FROM {$wpdb->prefix}h5p_libraries AS l | |
| 109 | + WHERE l.runnable = 1" | |
| 110 | + ) ); | |
| 111 | + | |
| 112 | + $requirement_content_type = get_post_meta( $requirement_id, '_gamipress_h5p_content_type', true ); ?> | |
| 113 | + | |
| 114 | + <span class="h5p-content-type"> | |
| 115 | + <select> | |
| 116 | + <?php foreach( $content_types as $content_type ) : ?> | |
| 117 | + <option value="<?php echo $content_type->name; ?>" <?php selected( $requirement_content_type, $content_type->name ); ?>><?php echo $content_type->title; ?></option> | |
| 118 | + <?php endforeach; ?> | |
| 119 | + </select> | |
| 120 | + </span> | |
| 121 | + | |
| 122 | + <?php | |
| 123 | + | |
| 124 | + $score = absint( get_post_meta( $requirement_id, '_gamipress_h5p_score', true ) ); | |
| 125 | + $min_score = get_post_meta( $requirement_id, '_gamipress_h5p_min_score', true ); | |
| 126 | + $max_score = get_post_meta( $requirement_id, '_gamipress_h5p_max_score', true ); | |
| 127 | + $percentage = get_post_meta( $requirement_id, '_gamipress_h5p_percentage', true ); | |
| 128 | + $min_percentage = get_post_meta( $requirement_id, '_gamipress_h5p_min_percentage', true ); | |
| 129 | + $max_percentage = get_post_meta( $requirement_id, '_gamipress_h5p_max_percentage', true ); | |
| 130 | + | |
| 131 | + ?> | |
| 132 | + | |
| 133 | + <span class="h5p-score"><input type="text" value="<?php echo $score; ?>" size="3" maxlength="3" placeholder="100" /></span> | |
| 134 | + <span class="h5p-min-score"><input type="text" value="<?php echo ( ! empty( $min_score ) ? absint( $min_score ) : '' ); ?>" size="3" maxlength="3" placeholder="Min" /> -</span> | |
| 135 | + <span class="h5p-max-score"><input type="text" value="<?php echo ( ! empty( $max_score ) ? absint( $max_score ) : '' ); ?>" size="3" maxlength="3" placeholder="Max" /></span> | |
| 136 | + <span class="h5p-percentage"><input type="text" value="<?php echo ( ! empty( $percentage ) ? absint( $percentage ) : '' ); ?>" size="3" maxlength="3" placeholder="" />%</span> | |
| 137 | + <span class="h5p-min-percentage"><input type="text" value="<?php echo ( ! empty( $min_percentage ) ? absint( $min_percentage ) : '' ); ?>" size="3" maxlength="3" placeholder="Min" />% -</span> | |
| 138 | + <span class="h5p-max-percentage"><input type="text" value="<?php echo ( ! empty( $max_percentage ) ? absint( $max_percentage ) : '' ); ?>" size="3" maxlength="3" placeholder="Max" />%</span> | |
| 139 | + | |
| 140 | + | |
| 141 | + <?php | |
| 142 | +} | |
| 143 | +add_action( 'gamipress_requirement_ui_html_after_achievement_post', 'gamipress_h5p_requirement_ui_fields', 10, 2 ); | |
| 144 | + | |
| 145 | +/** | |
| 146 | + * Custom handler to save the content type on requirements UI | |
| 147 | + * | |
| 148 | + * @since 1.0.0 | |
| 149 | + * | |
| 150 | + * @param $requirement_id | |
| 151 | + * @param $requirement | |
| 152 | + */ | |
| 153 | +function gamipress_h5p_ajax_update_requirement( $requirement_id, $requirement ) { | |
| 154 | + | |
| 155 | + $requirement = wp_parse_args( $requirement, array( | |
| 156 | + 'h5p_content_type' => '', | |
| 157 | + 'h5p_score' => '', | |
| 158 | + 'h5p_min_score' => '', | |
| 159 | + 'h5p_max_score' => '', | |
| 160 | + 'h5p_percentage' => '', | |
| 161 | + 'h5p_min_percentage' => '', | |
| 162 | + 'h5p_max_percentage' => '', | |
| 163 | + ) ); | |
| 164 | + | |
| 165 | + if( isset( $requirement['trigger_type'] ) | |
| 166 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type' | |
| 167 | + || $requirement['trigger_type'] === 'gamipress_h5p_max_complete_specific_content_type' | |
| 168 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_min_score' | |
| 169 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_max_score' | |
| 170 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_between_score' | |
| 171 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_min_percentage' | |
| 172 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_max_percentage' | |
| 173 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_between_percentage' ) ) { | |
| 174 | + | |
| 175 | + // Save the content type field | |
| 176 | + update_post_meta( $requirement_id, '_gamipress_h5p_content_type', $requirement['h5p_content_type'] ); | |
| 177 | + } | |
| 178 | + | |
| 179 | + if( isset( $requirement['trigger_type'] ) | |
| 180 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_content_min_score' | |
| 181 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_min_score' | |
| 182 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_min_score' | |
| 183 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_min_score_tag' | |
| 184 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_max_score' | |
| 185 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_max_score' | |
| 186 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_max_score' | |
| 187 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_max_score_tag' ) ) { | |
| 188 | + | |
| 189 | + // Min/max score | |
| 190 | + update_post_meta( $requirement_id, '_gamipress_h5p_score', $requirement['h5p_score'] ); | |
| 191 | + } | |
| 192 | + | |
| 193 | + if( isset( $requirement['trigger_type'] ) | |
| 194 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_content_between_score' | |
| 195 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_between_score' | |
| 196 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_between_score' | |
| 197 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_between_score_tag' ) ) { | |
| 198 | + | |
| 199 | + // Between score | |
| 200 | + update_post_meta( $requirement_id, '_gamipress_h5p_min_score', $requirement['h5p_min_score'] ); | |
| 201 | + update_post_meta( $requirement_id, '_gamipress_h5p_max_score', $requirement['h5p_max_score'] ); | |
| 202 | + | |
| 203 | + } | |
| 204 | + | |
| 205 | + if( isset( $requirement['trigger_type'] ) | |
| 206 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_content_min_percentage' | |
| 207 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_min_percentage' | |
| 208 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_min_percentage' | |
| 209 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_min_percentage_tag' | |
| 210 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_max_percentage' | |
| 211 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_max_percentage' | |
| 212 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_max_percentage' | |
| 213 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_max_percentage_tag' ) ) { | |
| 214 | + | |
| 215 | + // Percentage score | |
| 216 | + update_post_meta( $requirement_id, '_gamipress_h5p_percentage', $requirement['h5p_percentage'] ); | |
| 217 | + } | |
| 218 | + | |
| 219 | + if( isset( $requirement['trigger_type'] ) | |
| 220 | + && ( $requirement['trigger_type'] === 'gamipress_h5p_complete_content_between_percentage' | |
| 221 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_between_percentage' | |
| 222 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_specific_content_type_between_percentage' | |
| 223 | + || $requirement['trigger_type'] === 'gamipress_h5p_complete_content_between_percentage_tag' ) ) { | |
| 224 | + | |
| 225 | + // Percentage score | |
| 226 | + update_post_meta( $requirement_id, '_gamipress_h5p_min_percentage', $requirement['h5p_min_percentage'] ); | |
| 227 | + update_post_meta( $requirement_id, '_gamipress_h5p_max_percentage', $requirement['h5p_max_percentage'] ); | |
| 228 | + } | |
| 229 | +} | |
| 230 | +add_action( 'gamipress_ajax_update_requirement', 'gamipress_h5p_ajax_update_requirement', 10, 2 ); | |