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