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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / classes / Quiz_Attempts_List.php
tutor / classes Last commit date
Addons.php 3 years ago Admin.php 3 years ago Ajax.php 3 years ago Announcements.php 3 years ago Assets.php 3 years ago Backend_Page_Trait.php 3 years ago Course.php 3 years ago Course_Embed.php 3 years ago Course_Filter.php 3 years ago Course_List.php 3 years ago Course_Settings_Tabs.php 3 years ago Course_Widget.php 4 years ago Custom_Validation.php 4 years ago Dashboard.php 3 years ago FormHandler.php 4 years ago Frontend.php 3 years ago Gutenberg.php 3 years ago Input.php 3 years ago Instructor.php 4 years ago Instructors_List.php 3 years ago Lesson.php 3 years ago Options_V2.php 3 years ago Post_types.php 3 years ago Private_Course_Access.php 4 years ago Q_and_A.php 3 years ago Question_Answers_List.php 4 years ago Quiz.php 3 years ago Quiz_Attempts_List.php 3 years ago RestAPI.php 4 years ago Reviews.php 3 years ago Rewrite_Rules.php 4 years ago Shortcode.php 4 years ago Student.php 4 years ago Students_List.php 4 years ago Taxonomies.php 4 years ago Template.php 3 years ago Theme_Compatibility.php 5 years ago Tools.php 3 years ago Tools_V2.php 4 years ago Tutor.php 3 years ago TutorEDD.php 4 years ago Tutor_Base.php 5 years ago Tutor_List_Table.php 3 years ago Tutor_Setup.php 3 years ago Upgrader.php 4 years ago User.php 4 years ago Utils.php 3 years ago Video_Stream.php 4 years ago Withdraw.php 3 years ago Withdraw_Requests_List.php 3 years ago WooCommerce.php 3 years ago
Quiz_Attempts_List.php
309 lines
1 <?php
2 namespace TUTOR;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use Tutor\Cache\QuizAttempts;
9 use Tutor\Models\QuizModel;
10
11 class Quiz_Attempts_List {
12
13 const QUIZ_ATTEMPT_PAGE = 'tutor_quiz_attempts';
14
15 /**
16 * Trait for utilities
17 *
18 * @var $page_title
19 */
20
21 use Backend_Page_Trait;
22 /**
23 * Page Title
24 *
25 * @var $page_title
26 */
27 public $page_title;
28
29 /**
30 * Bulk Action
31 *
32 * @var $bulk_action
33 */
34 public $bulk_action = true;
35
36 /**
37 * Handle dependencies
38 */
39 public function __construct( $register_hook = true ) {
40
41 $this->page_title = __( 'Quiz Attempts', 'tutor' );
42 if ( ! $register_hook ) {
43 return;
44 }
45
46 /**
47 * Handle bulk action
48 *
49 * @since v2.0.0
50 */
51 add_action( 'wp_ajax_tutor_quiz_attempts_bulk_action', array( $this, 'quiz_attempts_bulk_action' ) );
52 add_action( 'wp_ajax_tutor_quiz_attempts_count', array( $this, 'get_quiz_attempts_stat' ) );
53
54 /**
55 * Delete quiz attempt cache
56 *
57 * @since 2.1.0
58 */
59 add_action( 'tutor_quiz/attempt_ended', array( new QuizAttempts(), 'delete_cache' ) );
60 add_action( 'tutor_quiz/attempt_deleted', array( new QuizAttempts(), 'delete_cache' ) );
61 add_action( 'tutor_quiz/answer/review/after', array( new QuizAttempts(), 'delete_cache' ) );
62 }
63
64 /**
65 * @return array
66 *
67 *
68 * Get the attempts stat from specific instructor context
69 *
70 * @since 2.0.0
71 */
72 public function get_quiz_attempts_stat() {
73 global $wpdb;
74
75 $user_id = get_current_user_id();
76 // Set query based on action tab.
77 $pass_mark = "(((SUBSTRING_INDEX(SUBSTRING_INDEX(quiz_attempts.attempt_info, '\"passing_grade\";s:2:\"', -1), '\"', 1))/100)*quiz_attempts.total_marks)";
78 $pending_count = "(SELECT COUNT(DISTINCT attempt_answer_id) FROM {$wpdb->prefix}tutor_quiz_attempt_answers WHERE quiz_attempt_id=quiz_attempts.attempt_id AND is_correct IS NULL)";
79
80 $pass_clause = " AND quiz_attempts.earned_marks >= {$pass_mark} ";
81
82 $fail_clause = " AND quiz_attempts.earned_marks < {$pass_mark} ";
83
84 $pending_clause = " AND {$pending_count} > 0 ";
85
86 $user_clause = '';
87 if ( ! current_user_can( 'administrator' ) ) {
88 $user_clause = "AND quiz.post_author = {$user_id}";
89 }
90
91 $count = array();
92 $is_ajax_action = isset( $_POST['action'] ) && 'tutor_quiz_attempts_count' === $_POST['action'];
93 if ( $is_ajax_action ) {
94 $attempt_cache = new QuizAttempts();
95
96 if ( $attempt_cache->has_cache() ) {
97 $count = $attempt_cache->get_cache();
98 } else {
99
100 $count = $wpdb->get_col(
101 $wpdb->prepare(
102 "SELECT COUNT( DISTINCT attempt_id)
103 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
104 INNER JOIN {$wpdb->posts} quiz
105 ON quiz_attempts.quiz_id = quiz.ID
106 INNER JOIN {$wpdb->prefix}tutor_quiz_attempt_answers AS ans
107 ON quiz_attempts.attempt_id = ans.quiz_attempt_id
108
109 WHERE attempt_status != %s
110 {$pass_clause}
111 {$user_clause}
112
113 UNION
114
115 SELECT COUNT( DISTINCT attempt_id)
116 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
117 INNER JOIN {$wpdb->posts} quiz
118 ON quiz_attempts.quiz_id = quiz.ID
119 INNER JOIN {$wpdb->prefix}tutor_quiz_attempt_answers AS ans
120 ON quiz_attempts.attempt_id = ans.quiz_attempt_id
121
122 WHERE attempt_status != %s
123 {$fail_clause}
124 {$user_clause}
125
126 UNION
127
128 SELECT COUNT( DISTINCT attempt_id)
129 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
130 INNER JOIN {$wpdb->posts} quiz
131 ON quiz_attempts.quiz_id = quiz.ID
132 INNER JOIN {$wpdb->prefix}tutor_quiz_attempt_answers AS ans
133 ON quiz_attempts.attempt_id = ans.quiz_attempt_id
134
135 WHERE attempt_status != %s
136 {$pending_clause}
137 {$user_clause}
138
139 ",
140 'attempt_started',
141 'attempt_started',
142 'attempt_started'
143 )
144 );
145 $attempt_cache->data = array(
146 $count[0] ?? 0, // Pass.
147 $count[1] ?? 0, // Fail.
148 $count[2] ?? 0, // Pending.
149 );
150 $attempt_cache->set_cache();
151 }
152 }
153
154 $count_pass = $count[0] ?? 0;
155 $count_fail = $count[1] ?? 0;
156 $count_pending = $count[2] ?? 0;
157
158 $all = $count_pass + $count_fail + $count_pending;
159 $pass = $count_pass;
160 $fail = $count_fail;
161 $pending = $count_pending;
162 $response = compact( 'all', 'pass', 'fail', 'pending' );
163 return $is_ajax_action ? wp_send_json_success( $response ) : $response;
164 }
165
166 /**
167 * Available tabs that will visible on the right side of page navbar
168 *
169 * @param string $user_id selected quiz_attempts id | optional.
170 * @param string $date selected date | optional.
171 * @param string $search search by user name or email | optional.
172 * @return array
173 * @since v2.0.0
174 */
175 public function tabs_key_value( $user_id, $course_id, $date, $search ): array {
176 $url = get_pagenum_link();
177 $stats = $this->get_quiz_attempts_stat();
178
179 $tabs = array(
180 array(
181 'key' => 'all',
182 'title' => __( 'All', 'tutor' ),
183 'value' => $stats['all'],
184 'url' => $url . '&data=all',
185 ),
186 array(
187 'key' => 'pass',
188 'title' => __( 'Pass', 'tutor' ),
189 'value' => $stats['pass'],
190 'url' => $url . '&data=pass',
191 ),
192 array(
193 'key' => 'fail',
194 'title' => __( 'Fail', 'tutor' ),
195 'value' => $stats['fail'],
196 'url' => $url . '&data=fail',
197 ),
198 array(
199 'key' => 'pending',
200 'title' => __( 'Pending', 'tutor' ),
201 'value' => $stats['pending'],
202 'url' => $url . '&data=pending',
203 ),
204 );
205
206 return $tabs;
207 }
208
209 /**
210 * Prepare bulk actions that will show on dropdown options
211 *
212 * @return array
213 * @since v2.0.0
214 */
215 public function prpare_bulk_actions(): array {
216 $actions = array(
217 $this->bulk_action_default(),
218 $this->bulk_action_delete(),
219 );
220 return $actions;
221 }
222
223 /**
224 * Count enrolled number by status & filters
225 * Count all enrollment | approved | cancelled
226 *
227 * @param string $status | required.
228 * @param string $user_id selected user id | optional.
229 * @param string $date selected date | optional.
230 * @param string $search_term search by user name or email | optional.
231 * @return int
232 * @since v2.0.0
233 */
234 protected static function get_instructor_number( $status = '', $user_id = '', $course_id = '', $attempt_id = '', $date = '', $search_term = '' ): int {
235 global $wpdb;
236 $status = sanitize_text_field( $status );
237 $course_id = sanitize_text_field( $course_id );
238 $user_id = sanitize_text_field( $user_id );
239 $attempt_id = sanitize_text_field( $attempt_id );
240 $date = sanitize_text_field( $date );
241 $search_term = sanitize_text_field( $search_term );
242
243 $search_term = '%' . $wpdb->esc_like( $search_term ) . '%';
244
245 // add user id in where clause.
246 $user_query = '';
247 if ( '' !== $user_id ) {
248 $user_query = "AND user.ID = $user_id";
249 }
250
251 $count = $wpdb->get_var(
252 $wpdb->prepare(
253 "SELECT COUNT(attempt_id)
254 FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts
255 INNER JOIN {$wpdb->tutor_quiz_attempt_answers} quiz
256 ON quiz_attempts.quiz_id = quiz.ID
257 INNER JOIN {$wpdb->users}
258 ON quiz_attempts.user_id = {$wpdb->users}.ID
259 WHERE attempt_status != %s
260 AND ( user_email = %s OR display_name LIKE %s OR post_title LIKE %s )
261 ",
262 'attempt_started',
263 $status,
264 $search_term,
265 $search_term,
266 $search_term,
267 $search_term
268 )
269 );
270 return $count ? $count : 0;
271 }
272
273 /**
274 * Handle bulk action for instructor delete
275 *
276 * @return string JSON response.
277 * @since v2.0.0
278 */
279 public function quiz_attempts_bulk_action() {
280 // check nonce.
281 tutor_utils()->checking_nonce();
282
283 $bulk_action = isset( $_POST['bulk-action'] ) ? sanitize_text_field( $_POST['bulk-action'] ) : '';
284 $bulk_ids = isset( $_POST['bulk-ids'] ) ? sanitize_text_field( $_POST['bulk-ids'] ) : '';
285 $bulk_ids = explode( ',', $bulk_ids );
286 $bulk_ids = array_map(
287 function( $id ) {
288 return (int) trim( $id );
289 },
290 $bulk_ids
291 );
292
293 switch ( $bulk_action ) {
294 case 'delete':
295 QuizModel::delete_quiz_attempt( $bulk_ids );
296 break;
297 }
298
299 wp_send_json_success();
300 }
301
302 function get_bulk_actions() {
303 $actions = array(
304 'delete' => 'Delete',
305 );
306 return $actions;
307 }
308 }
309