← All changes
|
integrations/learndash/includes/triggers/complete-quiz.php
+154
-0
5.7.7
→
6.0.3
View file →
| @@ -1,0 +1,154 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Complete Quiz | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\LearnDash\Triggers\Complete_Quiz | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_LearnDash_Complete_Quiz extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + public $integration = 'learndash'; | |
| 15 | + public $trigger = 'learndash_complete_quiz'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Register the trigger | |
| 19 | + * | |
| 20 | + * @since 1.0.0 | |
| 21 | + */ | |
| 22 | + public function register() { | |
| 23 | + | |
| 24 | + automatorwp_register_trigger( $this->trigger, array( | |
| 25 | + 'integration' => $this->integration, | |
| 26 | + 'label' => __( 'User attempts, passes or fails a quiz', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'User <strong>attempts, passes or fails</strong> a quiz', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Operation (attempts, passes or fails). %2$s: Post title. %3$s: Number of times. */ | |
| 29 | + 'edit_label' => sprintf( __( 'User %1$s %2$s %3$s time(s)', 'automatorwp' ), '{operation}', '{post}', '{times}' ), | |
| 30 | + /* translators: %1$s: Operation (attempts, passes or fails). %2$s: Post title. */ | |
| 31 | + 'log_label' => sprintf( __( 'User %1$s %2$s', 'automatorwp' ), '{operation}', '{post}' ), | |
| 32 | + 'action' => ( defined( 'LEARNDASH_VERSION' ) && version_compare( LEARNDASH_VERSION, '3.0.0', '>=' ) ? 'learndash_quiz_submitted' : 'learndash_quiz_completed' ), | |
| 33 | + 'function' => array( $this, 'listener' ), | |
| 34 | + 'priority' => 10, | |
| 35 | + 'accepted_args' => 2, | |
| 36 | + 'options' => array( | |
| 37 | + 'operation' => array( | |
| 38 | + 'from' => 'operation', | |
| 39 | + 'fields' => array( | |
| 40 | + 'operation' => array( | |
| 41 | + 'name' => __( 'Operation:', 'automatorwp' ), | |
| 42 | + 'type' => 'select', | |
| 43 | + 'options' => array( | |
| 44 | + 'attempt' => __( 'attempts', 'automatorwp' ), | |
| 45 | + 'pass' => __( 'passes', 'automatorwp' ), | |
| 46 | + 'fail' => __( 'fails', 'automatorwp' ), | |
| 47 | + ), | |
| 48 | + 'default' => 'attempt' | |
| 49 | + ), | |
| 50 | + ) | |
| 51 | + ), | |
| 52 | + 'post' => automatorwp_utilities_post_option( array( | |
| 53 | + 'name' => __( 'Quiz:', 'automatorwp' ), | |
| 54 | + 'option_none_label' => __( 'any quiz', 'automatorwp' ), | |
| 55 | + 'post_type' => 'sfwd-quiz' | |
| 56 | + ) ), | |
| 57 | + 'times' => automatorwp_utilities_times_option(), | |
| 58 | + ), | |
| 59 | + 'tags' => array_merge( | |
| 60 | + automatorwp_utilities_post_tags(), | |
| 61 | + automatorwp_utilities_times_tag() | |
| 62 | + ) | |
| 63 | + ) ); | |
| 64 | + | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * Trigger listener | |
| 69 | + * | |
| 70 | + * @since 1.0.0 | |
| 71 | + * | |
| 72 | + * @param array $quiz_data array( | |
| 73 | + * 'course' => WP_Post, | |
| 74 | + * 'quiz' => WP_Post, | |
| 75 | + * 'pass' => integer, | |
| 76 | + * 'percentage' => integer, | |
| 77 | + * ) | |
| 78 | + * @param WP_User $current_user | |
| 79 | + */ | |
| 80 | + public function listener( $quiz_data, $current_user ) { | |
| 81 | + | |
| 82 | + $user_id = $current_user->ID; | |
| 83 | + $quiz_id = automatorwp_learndash_get_post_id( $quiz_data['quiz'] ); | |
| 84 | + $course_id = automatorwp_learndash_get_post_id( $quiz_data['course'] ); | |
| 85 | + | |
| 86 | + automatorwp_trigger_event( array( | |
| 87 | + 'trigger' => $this->trigger, | |
| 88 | + 'user_id' => $user_id, | |
| 89 | + 'post_id' => $quiz_id, | |
| 90 | + 'course_id' => $course_id, | |
| 91 | + 'pass' => $quiz_data['pass'], | |
| 92 | + ) ); | |
| 93 | + | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * User deserves check | |
| 98 | + * | |
| 99 | + * @since 1.0.0 | |
| 100 | + * | |
| 101 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 102 | + * @param stdClass $trigger The trigger object | |
| 103 | + * @param int $user_id The user ID | |
| 104 | + * @param array $event Event information | |
| 105 | + * @param array $trigger_options The trigger's stored options | |
| 106 | + * @param stdClass $automation The trigger's automation object | |
| 107 | + * | |
| 108 | + * @return bool True if user deserves trigger, false otherwise | |
| 109 | + */ | |
| 110 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 111 | + | |
| 112 | + // Shorthand | |
| 113 | + $operation = $trigger_options['operation']; | |
| 114 | + | |
| 115 | + // Ensure operation default value | |
| 116 | + if( empty( $operation ) ) { | |
| 117 | + $operation = 'attempt'; | |
| 118 | + } | |
| 119 | + | |
| 120 | + // Don't deserve if post is not received | |
| 121 | + if( ! isset( $event['post_id'] ) && ! isset( $event['pass'] ) ) { | |
| 122 | + return false; | |
| 123 | + } | |
| 124 | + | |
| 125 | + // Don't deserve if post doesn't match with the trigger option | |
| 126 | + if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) { | |
| 127 | + return false; | |
| 128 | + } | |
| 129 | + | |
| 130 | + switch ( $operation ) { | |
| 131 | + case 'attempt': | |
| 132 | + // Attempt doesn't requires any extra check | |
| 133 | + break; | |
| 134 | + case 'pass': | |
| 135 | + // Don't deserve if user hasn't passed this quiz | |
| 136 | + if( ! $event['pass'] ) { | |
| 137 | + return false; | |
| 138 | + } | |
| 139 | + break; | |
| 140 | + case 'fail': | |
| 141 | + // Don't deserve if user hasn't failed this quiz | |
| 142 | + if( $event['pass'] ) { | |
| 143 | + return false; | |
| 144 | + } | |
| 145 | + break; | |
| 146 | + } | |
| 147 | + | |
| 148 | + return $deserves_trigger; | |
| 149 | + | |
| 150 | + } | |
| 151 | + | |
| 152 | +} | |
| 153 | + | |
| 154 | +new AutomatorWP_LearnDash_Complete_Quiz(); | |