| @@ -9,13 +9,14 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace TUTOR; |
| 12 | 12 | |
| 13 | -defined( 'ABSPATH' ) || exit; | |
| 14 | - | |
| 15 | 13 | use Tutor\Helpers\QueryHelper; |
| 16 | 14 | use Tutor\Models\CourseModel; |
| 17 | 15 | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit; | |
| 18 | +} | |
| 18 | 19 | /** |
| 19 | 20 | * Course List class |
| 20 | 21 | * |
| 21 | 22 | * @since 2.0.0 |
| @@ -39,9 +40,8 @@ | ||
| 39 | 40 | /** |
| 40 | 41 | * Constructor |
| 41 | 42 | * |
| 42 | 43 | * @return void |
| 43 | - * | |
| 44 | 44 | * @since 2.0.0 |
| 45 | 45 | */ |
| 46 | 46 | public function __construct() { |
| 47 | 47 | /** |
| @@ -46,21 +46,21 @@ | ||
| 46 | 46 | public function __construct() { |
| 47 | 47 | /** |
| 48 | 48 | * Handle bulk action |
| 49 | 49 | * |
| 50 | - * @since 2.0.0 | |
| 50 | + * @since v2.0.0 | |
| 51 | 51 | */ |
| 52 | 52 | add_action( 'wp_ajax_tutor_course_list_bulk_action', array( $this, 'course_list_bulk_action' ) ); |
| 53 | 53 | /** |
| 54 | 54 | * Handle ajax request for updating course status |
| 55 | 55 | * |
| 56 | - * @since 2.0.0 | |
| 56 | + * @since v2.0.0 | |
| 57 | 57 | */ |
| 58 | 58 | add_action( 'wp_ajax_tutor_change_course_status', array( $this, 'tutor_change_course_status' ) ); |
| 59 | 59 | /** |
| 60 | 60 | * Handle ajax request for delete course |
| 61 | 61 | * |
| 62 | - * @since 2.0.0 | |
| 62 | + * @since v2.0.0 | |
| 63 | 63 | */ |
| 64 | 64 | add_action( 'wp_ajax_tutor_course_delete', array( $this, 'tutor_course_delete' ) ); |
| 65 | 65 | } |
| 66 | 66 | |
| @@ -81,11 +81,10 @@ | ||
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * Prepare bulk actions that will show on dropdown options |
| 84 | 84 | * |
| 85 | + * @return array | |
| 85 | 86 | * @since 2.0.0 |
| 86 | - * | |
| 87 | - * @return array | |
| 88 | 87 | */ |
| 89 | 88 | public function prepare_bulk_actions(): array { |
| 90 | 89 | $actions = array( |
| 91 | 90 | $this->bulk_action_default(), |
| @@ -95,12 +94,12 @@ | ||
| 95 | 94 | ); |
| 96 | 95 | |
| 97 | 96 | $active_tab = Input::get( 'data', '' ); |
| 98 | 97 | |
| 99 | - if ( CourseModel::STATUS_TRASH === $active_tab ) { | |
| 98 | + if ( 'trash' === $active_tab ) { | |
| 100 | 99 | array_push( $actions, $this->bulk_action_delete() ); |
| 101 | 100 | } |
| 102 | - if ( CourseModel::STATUS_TRASH !== $active_tab ) { | |
| 101 | + if ( 'trash' !== $active_tab ) { | |
| 103 | 102 | array_push( $actions, $this->bulk_action_trash() ); |
| 104 | 103 | } |
| 105 | 104 | |
| 106 | 105 | if ( ! User::is_admin() ) { |
| @@ -105,9 +104,14 @@ | ||
| 105 | 104 | |
| 106 | 105 | if ( ! User::is_admin() ) { |
| 107 | 106 | $can_trash_post = tutor_utils()->get_option( 'instructor_can_delete_course' ) && current_user_can( 'edit_tutor_courses' ); |
| 108 | 107 | if ( ! $can_trash_post ) { |
| 109 | - $actions = array_filter( $actions, fn ( $val ) => CourseModel::STATUS_TRASH !== $val['value'] ); | |
| 108 | + $actions = array_filter( | |
| 109 | + $actions, | |
| 110 | + function ( $val ) { | |
| 111 | + return 'trash' !== $val['value']; | |
| 112 | + } | |
| 113 | + ); | |
| 110 | 114 | } |
| 111 | 115 | } |
| 112 | 116 | return apply_filters( 'tutor_course_bulk_actions', $actions ); |
| 113 | 117 | } |
| @@ -114,10 +118,8 @@ | ||
| 114 | 118 | |
| 115 | 119 | /** |
| 116 | 120 | * Available tabs that will visible on the right side of page navbar |
| 117 | 121 | * |
| 118 | - * @since 2.0.0 | |
| 119 | - * | |
| 120 | 122 | * @param string $category_slug category slug. |
| 121 | 123 | * @param integer $course_id course ID. |
| 122 | 124 | * @param string $date selected date | optional. |
| 123 | 125 | * @param string $search search by user name or email | optional. |
| @@ -122,8 +124,10 @@ | ||
| 122 | 124 | * @param string $date selected date | optional. |
| 123 | 125 | * @param string $search search by user name or email | optional. |
| 124 | 126 | * |
| 125 | 127 | * @return array |
| 128 | + * | |
| 129 | + * @since v2.0.0 | |
| 126 | 130 | */ |
| 127 | 131 | public function tabs_key_value( $category_slug, $course_id, $date, $search ): array { |
| 128 | 132 | $url = apply_filters( 'tutor_data_tab_base_url', get_pagenum_link() ); |
| 129 | 133 | |
| @@ -195,10 +199,8 @@ | ||
| 195 | 199 | /** |
| 196 | 200 | * Count courses by status & filters |
| 197 | 201 | * Count all | min | published | pending | draft |
| 198 | 202 | * |
| 199 | - * @since 2.0.0 | |
| 200 | - * | |
| 201 | 203 | * @param string $status | required. |
| 202 | 204 | * @param string $category_slug course category | optional. |
| 203 | 205 | * @param string $course_id selected course id | optional. |
| 204 | 206 | * @param string $date selected date | optional. |
| @@ -204,8 +206,10 @@ | ||
| 204 | 206 | * @param string $date selected date | optional. |
| 205 | 207 | * @param string $search_term search by user name or email | optional. |
| 206 | 208 | * |
| 207 | 209 | * @return int |
| 210 | + * | |
| 211 | + * @since 2.0.0 | |
| 208 | 212 | */ |
| 209 | 213 | protected static function count_course( string $status, $category_slug = '', $course_id = '', $date = '', $search_term = '' ): int { |
| 210 | 214 | $user_id = get_current_user_id(); |
| 211 | 215 | $status = sanitize_text_field( $status ); |
| @@ -230,11 +234,11 @@ | ||
| 230 | 234 | } |
| 231 | 235 | |
| 232 | 236 | $date_filter = sanitize_text_field( $date ); |
| 233 | 237 | |
| 234 | - $year = gmdate( 'Y', strtotime( $date_filter ) ); | |
| 235 | - $month = gmdate( 'm', strtotime( $date_filter ) ); | |
| 236 | - $day = gmdate( 'd', strtotime( $date_filter ) ); | |
| 238 | + $year = date( 'Y', strtotime( $date_filter ) ); | |
| 239 | + $month = date( 'm', strtotime( $date_filter ) ); | |
| 240 | + $day = date( 'd', strtotime( $date_filter ) ); | |
| 237 | 241 | |
| 238 | 242 | // Add date query. |
| 239 | 243 | if ( '' !== $date_filter ) { |
| 240 | 244 | $args['date_query'] = array( |
| @@ -273,11 +277,10 @@ | ||
| 273 | 277 | |
| 274 | 278 | /** |
| 275 | 279 | * Handle bulk action for enrollment cancel | delete |
| 276 | 280 | * |
| 281 | + * @return void | |
| 277 | 282 | * @since 2.0.0 |
| 278 | - * | |
| 279 | - * @return void | |
| 280 | 283 | */ |
| 281 | 284 | public function course_list_bulk_action() { |
| 282 | 285 | |
| 283 | 286 | tutor_utils()->checking_nonce(); |
| @@ -285,15 +288,15 @@ | ||
| 285 | 288 | $action = Input::post( 'bulk-action', '' ); |
| 286 | 289 | $bulk_ids = Input::post( 'bulk-ids', '' ); |
| 287 | 290 | |
| 288 | 291 | // Check if user is privileged. |
| 289 | - if ( ! User::is_admin() ) { | |
| 292 | + if ( ! current_user_can( 'administrator' ) ) { | |
| 290 | 293 | $course_ids = explode( ',', $bulk_ids ); |
| 291 | 294 | |
| 292 | 295 | if ( current_user_can( 'edit_tutor_courses' ) ) { |
| 293 | 296 | $can_publish_course = tutor_utils()->get_option( 'instructor_can_publish_course' ); |
| 294 | 297 | |
| 295 | - if ( CourseModel::STATUS_PUBLISH === $action && ! $can_publish_course ) { | |
| 298 | + if ( 'publish' === $action && ! $can_publish_course ) { | |
| 296 | 299 | wp_send_json_error( tutor_utils()->error_message() ); |
| 297 | 300 | } |
| 298 | 301 | } else { |
| 299 | 302 | wp_send_json_error( tutor_utils()->error_message() ); |
| @@ -346,21 +349,20 @@ | ||
| 346 | 349 | |
| 347 | 350 | /** |
| 348 | 351 | * Handle ajax request for updating course status |
| 349 | 352 | * |
| 353 | + * @return void | |
| 350 | 354 | * @since 2.0.0 |
| 351 | - * | |
| 352 | - * @return void | |
| 353 | 355 | */ |
| 354 | 356 | public static function tutor_change_course_status() { |
| 355 | 357 | tutor_utils()->checking_nonce(); |
| 356 | 358 | |
| 357 | 359 | $status = Input::post( 'status' ); |
| 358 | - $id = Input::post( 'id', 0, Input::TYPE_INT ); | |
| 360 | + $id = Input::post( 'id' ); | |
| 359 | 361 | $course = get_post( $id ); |
| 360 | 362 | |
| 361 | 363 | // Check if user is privileged. |
| 362 | - if ( ! User::is_admin() ) { | |
| 364 | + if ( ! current_user_can( 'administrator' ) ) { | |
| 363 | 365 | |
| 364 | 366 | if ( ! tutor_utils()->can_user_edit_course( get_current_user_id(), $course->ID ) ) { |
| 365 | 367 | wp_send_json_error( tutor_utils()->error_message() ); |
| 366 | 368 | } |
| @@ -367,13 +369,13 @@ | ||
| 367 | 369 | |
| 368 | 370 | $can_delete_course = tutor_utils()->get_option( 'instructor_can_delete_course' ); |
| 369 | 371 | $can_publish_course = tutor_utils()->get_option( 'instructor_can_publish_course' ); |
| 370 | 372 | |
| 371 | - if ( CourseModel::STATUS_PUBLISH === $status && ! $can_publish_course ) { | |
| 373 | + if ( 'publish' === $status && ! $can_publish_course ) { | |
| 372 | 374 | wp_send_json_error( tutor_utils()->error_message() ); |
| 373 | 375 | } |
| 374 | 376 | |
| 375 | - if ( CourseModel::STATUS_TRASH === $status && $can_delete_course ) { | |
| 377 | + if ( 'trash' === $status && $can_delete_course ) { | |
| 376 | 378 | $args = array( |
| 377 | 379 | 'ID' => $id, |
| 378 | 380 | 'post_status' => $status, |
| 379 | 381 | ); |
| @@ -436,13 +438,11 @@ | ||
| 436 | 438 | |
| 437 | 439 | /** |
| 438 | 440 | * Execute bulk delete action |
| 439 | 441 | * |
| 440 | - * @since 2.0.0 | |
| 441 | - * | |
| 442 | 442 | * @param string $bulk_ids ids that need to update. |
| 443 | - * | |
| 444 | 443 | * @return bool |
| 444 | + * @since 2.0.0 | |
| 445 | 445 | */ |
| 446 | 446 | public static function bulk_delete_course( $bulk_ids ): bool { |
| 447 | 447 | $bulk_ids = explode( ',', sanitize_text_field( $bulk_ids ) ); |
| 448 | 448 | |
| @@ -455,14 +455,14 @@ | ||
| 455 | 455 | |
| 456 | 456 | /** |
| 457 | 457 | * Update course status |
| 458 | 458 | * |
| 459 | - * @since 2.0.0 | |
| 460 | - * | |
| 461 | 459 | * @param string $status for updating course status. |
| 462 | 460 | * @param string $bulk_ids comma separated ids. |
| 463 | 461 | * |
| 464 | 462 | * @return bool |
| 463 | + * | |
| 464 | + * @since 2.0.0 | |
| 465 | 465 | */ |
| 466 | 466 | public static function update_course_status( string $status, $bulk_ids ): bool { |
| 467 | 467 | global $wpdb; |
| 468 | 468 | $post_table = $wpdb->posts; |
| @@ -471,9 +471,9 @@ | ||
| 471 | 471 | |
| 472 | 472 | $ids = array_map( 'intval', explode( ',', $bulk_ids ) ); |
| 473 | 473 | $in_clause = QueryHelper::prepare_in_clause( $ids ); |
| 474 | 474 | |
| 475 | - $wpdb->query( | |
| 475 | + $update = $wpdb->query( | |
| 476 | 476 | $wpdb->prepare( |
| 477 | 477 | "UPDATE {$post_table} SET post_status = %s WHERE ID IN ($in_clause)", //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 478 | 478 | $status |
| 479 | 479 | ) |
| @@ -482,15 +482,59 @@ | ||
| 482 | 482 | return true; |
| 483 | 483 | } |
| 484 | 484 | |
| 485 | 485 | /** |
| 486 | - * Check course is public or not | |
| 486 | + * Get course enrollment list with student info | |
| 487 | 487 | * |
| 488 | - * @since 1.0.0 | |
| 488 | + * @param int $course_id int | required. | |
| 489 | + * @return array | |
| 490 | + * @since 2.0.0 | |
| 491 | + */ | |
| 492 | + public static function course_enrollments_with_student_details( int $course_id ) { | |
| 493 | + global $wpdb; | |
| 494 | + $course_id = sanitize_text_field( $course_id ); | |
| 495 | + $course_completed = 0; | |
| 496 | + $course_inprogress = 0; | |
| 497 | + | |
| 498 | + $enrollments = $wpdb->get_results( | |
| 499 | + $wpdb->prepare( | |
| 500 | + "SELECT enroll.ID AS enroll_id, enroll.post_author AS enroll_author, user.*, course.ID AS course_id | |
| 501 | + FROM {$wpdb->posts} AS enroll | |
| 502 | + LEFT JOIN {$wpdb->users} AS user ON user.ID = enroll.post_author | |
| 503 | + LEFT JOIN {$wpdb->posts} AS course ON course.ID = enroll.post_parent | |
| 504 | + WHERE enroll.post_type = %s | |
| 505 | + AND enroll.post_status = %s | |
| 506 | + AND enroll.post_parent = %d | |
| 507 | + ", | |
| 508 | + 'tutor_enrolled', | |
| 509 | + 'completed', | |
| 510 | + $course_id | |
| 511 | + ) | |
| 512 | + ); | |
| 513 | + | |
| 514 | + foreach ( $enrollments as $enrollment ) { | |
| 515 | + $course_progress = tutor_utils()->get_course_completed_percent( $course_id, $enrollment->enroll_author ); | |
| 516 | + if ( 100 == $course_progress ) { | |
| 517 | + $course_completed++; | |
| 518 | + } else { | |
| 519 | + $course_inprogress++; | |
| 520 | + } | |
| 521 | + } | |
| 522 | + | |
| 523 | + return array( | |
| 524 | + 'enrollments' => $enrollments, | |
| 525 | + 'total_completed' => $course_completed, | |
| 526 | + 'total_inprogress' => $course_inprogress, | |
| 527 | + 'total_enrollments' => count( $enrollments ), | |
| 528 | + ); | |
| 529 | + } | |
| 530 | + | |
| 531 | + /** | |
| 532 | + * Check wheather course is public or not | |
| 489 | 533 | * |
| 490 | 534 | * @param integer $course_id course id to check with. |
| 491 | - * | |
| 492 | 535 | * @return boolean true if public otherwise false. |
| 536 | + * @since 1.0.0 | |
| 493 | 537 | */ |
| 494 | 538 | public static function is_public( int $course_id ): bool { |
| 495 | 539 | $is_public = get_post_meta( $course_id, '_tutor_is_public_course', true ); |
| 496 | 540 | return 'yes' === $is_public ? true : false; |