/var/www/html_uk/wp-content/plugins/automatewoo/includes/Triggers/Sensei_Quiz_Completed.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

namespace AutomateWoo;

use 
AutomateWoo\DataTypes\DataTypes;

defined'ABSPATH' ) || exit;

/**
 * Class Trigger_Sensei_Quiz_Completed.
 *
 * @since 5.6.10
 * @package AutomateWoo
 */
class Trigger_Sensei_Quiz_Completed extends Trigger {

    
/**
     * Sets supplied data for the trigger.
     *
     * @var array
     */
    
public $supplied_data_items = [
        
DataTypes::SENSEI_COURSE,
        
DataTypes::SENSEI_LESSON,
        
DataTypes::SENSEI_QUIZ,
        
DataTypes::SENSEI_TEACHER,
        
DataTypes::CUSTOMER,
    ];

    
/**
     * Load admin details.
     */
    
public function load_admin_details() {
        
$this->title       __'Quiz Completed''automatewoo' );
        
$this->description __'This trigger fires after the quiz is completed.''automatewoo' );
        
$this->group       Sensei_Workflow_Helper::get_group_name();
    }

    
/**
     * Register trigger hooks.
     */
    
public function register_hooks() {
        
add_action'sensei_user_quiz_grade', array( $this'handle_quiz_grade' ), 10);
    }

    
/**
     * Registers any fields used on for a trigger
     */
    
public function load_fields() {
        
$quizzes Sensei_Workflow_Helper::get_quizzes_field();
        
$this->add_field$quizzes );
    }

    
/**
     * Handle Quiz Grade.
     *
     * @param int $user_id  User ID.
     * @param int $quiz_id  Quiz ID.
     * @param int $grade    Grade.
     * @param int $passmark Passmark.
     */
    
public function handle_quiz_grade$user_id$quiz_id$grade$passmark ) {
        
$quiz get_post$quiz_id );
        
$user get_user_by'id'$user_id );
        if ( ! 
$quiz || ! $user || ! function_exists'Sensei' ) ) {
            return;
        }

        
// Skip trigger for "Quiz Failed", if the quiz not failed.
        
if ( 'sensei_quiz_failed' === $this->get_name() && $grade >= $passmark ) {
            return;
        }

        
// Skip trigger for "Quiz Passed", if the quiz not passed.
        
if ( 'sensei_quiz_passed' === $this->get_name() && $grade $passmark ) {
            return;
        }

        
$lesson_id Sensei()->quiz->get_lesson_id$quiz_id );
        
$course_id Sensei()->lesson->get_course_id$lesson_id );
        
$lesson    get_post$lesson_id );
        
$course    get_post$course_id );

        if ( ! 
$course || ! $lesson ) {
            return;
        }

        foreach ( 
$this->get_workflows() as $workflow ) {
            
$sensei_quizzes Clean::ids$workflow->get_trigger_option'sensei_quizzes' ) );

            if ( ! empty( 
$sensei_quizzes ) && ! in_array$quiz->ID$sensei_quizzestrue ) ) {
                continue;
            }

            
// Prevent duplicate triggers.
            
$query = new Queue_Query();
            
$query->where_workflow$workflow->get_id() );
            
$query->where_user$user_id );
            
$query->where_quiz$quiz_id );
            
$results $query->get_results();
            if ( ! empty( 
$results ) ) {
                continue;
            }

            
$workflow->maybe_run(
                [
                    
DataTypes::SENSEI_COURSE  => $course,
                    
DataTypes::SENSEI_TEACHER => get_user_by'id'$course->post_author ),
                    
DataTypes::SENSEI_LESSON  => $lesson,
                    
DataTypes::SENSEI_QUIZ    => $quiz,
                    
DataTypes::CUSTOMER       => Customer_Factory::get_by_user_id$user_id ),
                ]
            );
        }
    }
}