QuizzesReportsGenerator

Extends report generator for quizzes reports

Description

Source

File: bp-integrations/learndash/buddypress/generators/QuizzesReportsGenerator.php

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class QuizzesReportsGenerator extends ReportsGenerator
{
    /**
     * Constructor
     *
     * @since BuddyBoss 1.0.0
     */
    public function __construct()
    {
        $this->completed_table_title = __('Marked Quizzes', 'buddyboss');
        $this->incompleted_table_title = __('Unmarked Quizzes', 'buddyboss');
 
        add_action('bp_ld_sync/ajax/pre_fetch_reports', [$this, 'loadAdditionalFields']);
 
        parent::__construct();
    }
 
    /**
     * Returns the columns and their settings
     *
     * @since BuddyBoss 1.0.0
     */
    protected function columns()
    {
        return [
            'user_id'         => $this->column('user_id'),
            'user'            => $this->column('user'),
            'course_id'       => $this->column('course_id'),
            'course'          => $this->column('course'),
            'quiz'            => [
                'label'     => __( 'Quiz', 'buddyboss' ),
                'sortable'  => true,
                'order_key' => 'post_title',
            ],
            'completion_date' => $this->column('completion_date'),
            'updated_date'    => $this->column('updated_date'),
            'score'           => [
                'label'     => __( 'Score', 'buddyboss' ),
                'sortable'  => true,
                'order_key' => 'activity_score',
            ],
            'time_spent'      => $this->column('time_spent'),
            'quiz_points'     => [
                'label'     => __( 'Points Earned', 'buddyboss' ),
                'sortable'  => true,
                'order_key' => 'activity_points',
            ],
            'attempts'         => [
                'label'     => __( 'Attempts', 'buddyboss' ),
                'sortable'  => false,
                'order_key' => 'post_title',
            ],
        ];
    }
 
    /**
     * Format the activity results for each column
     *
     * @since BuddyBoss 1.0.0
     */
    protected function formatData($activity)
    {
        return [
            'user_id'         => $activity->user_id,
            'user'            => $activity->user_display_name,
            'course_id'       => $activity->activity_course_id,
            'course'          => $activity->activity_course_title,
            'quiz'            => $activity->post_title,
            'completion_date' => $this->completionDate($activity),
            'updated_date'    => $this->updatedDate($activity),
            'score'           => $activity->activity_score,
            'time_spent'      => $this->timeSpent($activity),
            'quiz_points'     => $activity->activity_points,
            'attempts'        => $activity->activity_attemps,
        ];
    }
 
    /**
     * Remove and add additional sql field on ajax
     *
     * @since BuddyBoss 1.0.0
     */
    public function loadAdditionalFields($generator)
    {
        if (! is_a($generator, get_class($this))) {
            return;
        }
 
        $this->excludeCourseTimeSpent();
 
        add_filter('bp_ld_sync/reports/activity_fields', [$this, 'addQuizActivityFields'], 10, 2);
    }
 
    /**
     * Add quiz activity fields on sql statement
     *
     * @since BuddyBoss 1.0.0
     */
    public function addQuizActivityFields($strFields, $queryArgs)
    {
        global $wpdb;
        $metaTable = $wpdb->prefix ."learndash_user_activity_meta";
        $table = $wpdb->prefix ."learndash_user_activity";
 
        $strFields .= ", (
                SELECT mt_points.activity_meta_value
                FROM {$metaTable} as mt_points
                WHERE mt_points.activity_id = ld_user_activity.activity_id
                AND mt_points.activity_meta_key = 'points'
            ) as activity_points
        ";
 
        $strFields .= ", (
                SELECT mt_score.activity_meta_value
                FROM {$metaTable} as mt_score
                WHERE mt_score.activity_id = ld_user_activity.activity_id
                AND mt_score.activity_meta_key = 'percentage'
            ) as activity_score
        ";
 
        $strFields .= ", (
                SELECT mt_time_spent.activity_meta_value
                FROM {$metaTable} as mt_time_spent
                WHERE mt_time_spent.activity_id = ld_user_activity.activity_id
                AND mt_time_spent.activity_meta_key = 'timespent'
            ) as activity_time_spent
        ";
 
        $strFields .= ", (
                SELECT count(*)
                FROM {$table} as mt_attempts
                WHERE mt_attempts.post_id = posts.ID
                AND mt_attempts.user_id = users.ID
            ) as activity_attemps
        ";
 
        return $strFields;
    }
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.