Addons.php
4 years ago
Admin.php
4 years ago
Ajax.php
4 years ago
Announcements.php
4 years ago
Assets.php
4 years ago
Backend_Page_Trait.php
4 years ago
Course.php
4 years ago
Course_Filter.php
4 years ago
Course_List.php
4 years ago
Course_Settings_Tabs.php
4 years ago
Course_Widget.php
4 years ago
Custom_Validation.php
4 years ago
Dashboard.php
4 years ago
FormHandler.php
4 years ago
Frontend.php
4 years ago
Gutenberg.php
4 years ago
Input.php
4 years ago
Instructor.php
4 years ago
Instructors_List.php
4 years ago
Lesson.php
4 years ago
Options_V2.php
4 years ago
Post_types.php
4 years ago
Private_Course_Access.php
4 years ago
Q_and_A.php
4 years ago
Question_Answers_List.php
4 years ago
Quiz.php
4 years ago
Quiz_Attempts_List.php
4 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
4 years ago
Theme_Compatibility.php
5 years ago
Tools.php
4 years ago
Tools_V2.php
4 years ago
Tutor.php
4 years ago
TutorEDD.php
4 years ago
Tutor_Base.php
5 years ago
Tutor_List_Table.php
4 years ago
Tutor_Setup.php
4 years ago
Upgrader.php
4 years ago
User.php
4 years ago
Utils.php
4 years ago
Video_Stream.php
4 years ago
Withdraw.php
4 years ago
Withdraw_Requests_List.php
4 years ago
WooCommerce.php
4 years ago
Quiz_Attempts_List.php
288 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 | // Set query based on action tab. |
| 64 | $pass_mark = "(((SUBSTRING_INDEX(SUBSTRING_INDEX(quiz_attempts.attempt_info, '\"passing_grade\";s:2:\"', -1), '\"', 1))/100)*quiz_attempts.total_marks)"; |
| 65 | $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)"; |
| 66 | |
| 67 | $pass_clause = " AND quiz_attempts.earned_marks >= {$pass_mark} "; |
| 68 | |
| 69 | $fail_clause = " AND quiz_attempts.earned_marks < {$pass_mark} "; |
| 70 | |
| 71 | $pending_clause = " AND {$pending_count} > 0 "; |
| 72 | |
| 73 | $count = array(); |
| 74 | $is_ajax_action = isset( $_POST['action'] ) && 'tutor_quiz_attempts_count' === $_POST['action']; |
| 75 | if ( $is_ajax_action ) { |
| 76 | $attempt_cache = new QuizAttempts(); |
| 77 | if ( $attempt_cache->has_cache() ) { |
| 78 | $count = $attempt_cache->get_cache(); |
| 79 | } else { |
| 80 | $count = $wpdb->get_col( |
| 81 | $wpdb->prepare( |
| 82 | "SELECT COUNT( DISTINCT attempt_id) |
| 83 | FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts |
| 84 | INNER JOIN {$wpdb->posts} quiz |
| 85 | ON quiz_attempts.quiz_id = quiz.ID |
| 86 | INNER JOIN {$wpdb->prefix}tutor_quiz_attempt_answers AS ans |
| 87 | ON quiz_attempts.attempt_id = ans.quiz_attempt_id |
| 88 | |
| 89 | WHERE attempt_status != %s |
| 90 | {$pass_clause} |
| 91 | |
| 92 | UNION |
| 93 | |
| 94 | SELECT COUNT( DISTINCT attempt_id) |
| 95 | FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts |
| 96 | INNER JOIN {$wpdb->posts} quiz |
| 97 | ON quiz_attempts.quiz_id = quiz.ID |
| 98 | INNER JOIN {$wpdb->prefix}tutor_quiz_attempt_answers AS ans |
| 99 | ON quiz_attempts.attempt_id = ans.quiz_attempt_id |
| 100 | |
| 101 | WHERE attempt_status != %s |
| 102 | {$fail_clause} |
| 103 | |
| 104 | UNION |
| 105 | |
| 106 | SELECT COUNT( DISTINCT attempt_id) |
| 107 | FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts |
| 108 | INNER JOIN {$wpdb->posts} quiz |
| 109 | ON quiz_attempts.quiz_id = quiz.ID |
| 110 | INNER JOIN {$wpdb->prefix}tutor_quiz_attempt_answers AS ans |
| 111 | ON quiz_attempts.attempt_id = ans.quiz_attempt_id |
| 112 | |
| 113 | WHERE attempt_status != %s |
| 114 | {$pending_clause} |
| 115 | |
| 116 | ", |
| 117 | 'attempt_started', |
| 118 | 'attempt_started', |
| 119 | 'attempt_started' |
| 120 | ) |
| 121 | ); |
| 122 | $attempt_cache->data = array( |
| 123 | $count[0] ?? 0, //pass |
| 124 | $count[1] ?? 0, //fail |
| 125 | $count[2] ?? 0, //pending |
| 126 | ); |
| 127 | $attempt_cache->set_cache(); |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
| 132 | $count_pass = $count[0] ?? 0; |
| 133 | $count_fail = $count[1] ?? 0; |
| 134 | $count_pending = $count[2] ?? 0; |
| 135 | |
| 136 | $all = $count_pass + $count_fail + $count_pending; |
| 137 | $pass = $count_pass; |
| 138 | $fail = $count_fail; |
| 139 | $pending = $count_pending; |
| 140 | $response = compact('all', 'pass', 'fail', 'pending'); |
| 141 | return $is_ajax_action ? wp_send_json_success( $response ) : $response; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Available tabs that will visible on the right side of page navbar |
| 146 | * |
| 147 | * @param string $user_id selected quiz_attempts id | optional. |
| 148 | * @param string $date selected date | optional. |
| 149 | * @param string $search search by user name or email | optional. |
| 150 | * @return array |
| 151 | * @since v2.0.0 |
| 152 | */ |
| 153 | public function tabs_key_value( $user_id, $course_id, $date, $search ): array { |
| 154 | $url = get_pagenum_link(); |
| 155 | $stats = $this->get_quiz_attempts_stat(get_current_user_id()); |
| 156 | |
| 157 | $tabs = array( |
| 158 | array( |
| 159 | 'key' => 'all', |
| 160 | 'title' => __( 'All', 'tutor' ), |
| 161 | 'value' => $stats['all'], |
| 162 | 'url' => $url . '&data=all', |
| 163 | ), |
| 164 | array( |
| 165 | 'key' => 'pass', |
| 166 | 'title' => __( 'Pass', 'tutor' ), |
| 167 | 'value' => $stats['pass'], |
| 168 | 'url' => $url . '&data=pass', |
| 169 | ), |
| 170 | array( |
| 171 | 'key' => 'fail', |
| 172 | 'title' => __( 'Fail', 'tutor' ), |
| 173 | 'value' => $stats['fail'], |
| 174 | 'url' => $url . '&data=fail', |
| 175 | ), |
| 176 | array( |
| 177 | 'key' => 'pending', |
| 178 | 'title' => __( 'Pending', 'tutor' ), |
| 179 | 'value' => $stats['pending'], |
| 180 | 'url' => $url . '&data=pending', |
| 181 | ), |
| 182 | ); |
| 183 | |
| 184 | return $tabs; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Prepare bulk actions that will show on dropdown options |
| 189 | * |
| 190 | * @return array |
| 191 | * @since v2.0.0 |
| 192 | */ |
| 193 | public function prpare_bulk_actions(): array { |
| 194 | $actions = array( |
| 195 | $this->bulk_action_default(), |
| 196 | $this->bulk_action_delete(), |
| 197 | ); |
| 198 | return $actions; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Count enrolled number by status & filters |
| 203 | * Count all enrolment | approved | cancelled |
| 204 | * |
| 205 | * @param string $status | required. |
| 206 | * @param string $user_id selected user id | optional. |
| 207 | * @param string $date selected date | optional. |
| 208 | * @param string $search_term search by user name or email | optional. |
| 209 | * @return int |
| 210 | * @since v2.0.0 |
| 211 | */ |
| 212 | protected static function get_instructor_number( $status = '', $user_id = '', $course_id = '', $attempt_id = '', $date = '', $search_term = '' ): int { |
| 213 | global $wpdb; |
| 214 | $status = sanitize_text_field( $status ); |
| 215 | $course_id = sanitize_text_field( $course_id ); |
| 216 | $user_id = sanitize_text_field( $user_id ); |
| 217 | $attempt_id = sanitize_text_field( $attempt_id ); |
| 218 | $date = sanitize_text_field( $date ); |
| 219 | $search_term = sanitize_text_field( $search_term ); |
| 220 | |
| 221 | $search_term = '%' . $wpdb->esc_like( $search_term ) . '%'; |
| 222 | |
| 223 | // add user id in where clause. |
| 224 | $user_query = ''; |
| 225 | if ( '' !== $user_id ) { |
| 226 | $user_query = "AND user.ID = $user_id"; |
| 227 | } |
| 228 | |
| 229 | // add quiz id in where clause. |
| 230 | $quiz_query = ''; |
| 231 | if ( '' !== $quiz_id ) { |
| 232 | $quiz_query = "AND quiz.ID = $user_id"; |
| 233 | } |
| 234 | |
| 235 | $count = $wpdb->get_var( |
| 236 | $wpdb->prepare( |
| 237 | "SELECT COUNT(attempt_id) |
| 238 | FROM {$wpdb->prefix}tutor_quiz_attempts quiz_attempts |
| 239 | INNER JOIN {$wpdb->tutor_quiz_attempt_answers} quiz |
| 240 | ON quiz_attempts.quiz_id = quiz.ID |
| 241 | INNER JOIN {$wpdb->users} |
| 242 | ON quiz_attempts.user_id = {$wpdb->users}.ID |
| 243 | WHERE attempt_status != %s |
| 244 | AND ( user_email = %s OR display_name LIKE %s OR post_title LIKE %s ) |
| 245 | ", |
| 246 | 'attempt_started', |
| 247 | $status, |
| 248 | $search_term, |
| 249 | $search_term, |
| 250 | $search_term, |
| 251 | $search_term |
| 252 | ) |
| 253 | ); |
| 254 | return $count ? $count : 0; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Handle bulk action for instructor delete |
| 259 | * |
| 260 | * @return string JSON response. |
| 261 | * @since v2.0.0 |
| 262 | */ |
| 263 | public function quiz_attempts_bulk_action() { |
| 264 | // check nonce. |
| 265 | tutor_utils()->checking_nonce(); |
| 266 | |
| 267 | $bulk_action = isset( $_POST['bulk-action'] ) ? sanitize_text_field( $_POST['bulk-action'] ) : ''; |
| 268 | $bulk_ids = isset( $_POST['bulk-ids'] ) ? sanitize_text_field( $_POST['bulk-ids'] ) :''; |
| 269 | $bulk_ids = explode(',', $bulk_ids); |
| 270 | $bulk_ids = array_map(function($id){return (int)trim($id);}, $bulk_ids); |
| 271 | |
| 272 | switch($bulk_action) { |
| 273 | case 'delete' : |
| 274 | tutor_utils()->delete_quiz_attempt( $bulk_ids ); |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | wp_send_json_success(); |
| 279 | } |
| 280 | |
| 281 | function get_bulk_actions() { |
| 282 | $actions = array( |
| 283 | 'delete' => 'Delete', |
| 284 | ); |
| 285 | return $actions; |
| 286 | } |
| 287 | } |
| 288 |