| @@ -9,16 +9,10 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace Tutor\Models; |
| 12 | 12 | |
| 13 | -use Tutor\Helpers\DateTimeHelper; | |
| 14 | -use TUTOR\Icon; | |
| 15 | 13 | use TUTOR\Course; |
| 16 | -use TUTOR\Lesson; | |
| 17 | -use Tutor\Ecommerce\Tax; | |
| 18 | -use InvalidArgumentException; | |
| 19 | 14 | use Tutor\Helpers\QueryHelper; |
| 20 | -use TUTOR\User; | |
| 21 | 15 | use TUTOR_ASSIGNMENTS\Assignments; |
| 22 | 16 | |
| 23 | 17 | /** |
| 24 | 18 | * CourseModel Class |
| @@ -49,29 +43,27 @@ | ||
| 49 | 43 | const MODE_FLEXIBLE = 'flexible'; |
| 50 | 44 | const MODE_STRICT = 'strict'; |
| 51 | 45 | |
| 52 | 46 | /** |
| 53 | - * Course attachment/downloadable resources meta key | |
| 47 | + * Course mapped with the product using this meta key | |
| 54 | 48 | * |
| 55 | 49 | * @var string |
| 56 | 50 | */ |
| 57 | - const ATTACHMENT_META_KEY = '_tutor_attachments'; | |
| 51 | + const WC_PRODUCT_META_KEY = '_tutor_course_product_id'; | |
| 58 | 52 | |
| 59 | 53 | /** |
| 60 | - * Course benefits meta key | |
| 54 | + * Course attachment/downloadable resources meta key | |
| 61 | 55 | * |
| 62 | 56 | * @var string |
| 63 | 57 | */ |
| 64 | - const BENEFITS_META_KEY = '_tutor_course_benefits'; | |
| 58 | + const ATTACHMENT_META_KEY = '_tutor_attachments'; | |
| 65 | 59 | |
| 66 | 60 | /** |
| 67 | - * The constant representing the status when a course is completed. | |
| 61 | + * Course benefits meta key | |
| 68 | 62 | * |
| 69 | - * @since 3.8.1 | |
| 70 | - * | |
| 71 | 63 | * @var string |
| 72 | 64 | */ |
| 73 | - const COURSE_COMPLETED = 'course_completed'; | |
| 65 | + const BENEFITS_META_KEY = '_tutor_course_benefits'; | |
| 74 | 66 | |
| 75 | 67 | /** |
| 76 | 68 | * Get available status list. |
| 77 | 69 | * |
| @@ -91,34 +83,8 @@ | ||
| 91 | 83 | ); |
| 92 | 84 | } |
| 93 | 85 | |
| 94 | 86 | /** |
| 95 | - * Check if a course is available for enrollment or purchase. | |
| 96 | - * | |
| 97 | - * @since 4.0.0 | |
| 98 | - * | |
| 99 | - * @param integer $course_id the course id. | |
| 100 | - * | |
| 101 | - * @return bool | |
| 102 | - */ | |
| 103 | - public static function is_course_accessible( $course_id = 0 ) { | |
| 104 | - $course = get_post( $course_id ); | |
| 105 | - if ( ! $course || ! is_object( $course ) ) { | |
| 106 | - return false; | |
| 107 | - } | |
| 108 | - | |
| 109 | - if ( ! in_array( $course->post_type, array( tutor()->course_post_type, tutor()->bundle_post_type ), true ) ) { | |
| 110 | - return false; | |
| 111 | - } | |
| 112 | - | |
| 113 | - if ( ! in_array( $course->post_status, array( self::STATUS_PUBLISH, self::STATUS_PRIVATE ), true ) ) { | |
| 114 | - return false; | |
| 115 | - } | |
| 116 | - | |
| 117 | - return true; | |
| 118 | - } | |
| 119 | - | |
| 120 | - /** | |
| 121 | 87 | * Course record count |
| 122 | 88 | * |
| 123 | 89 | * @since 2.0.7 |
| 124 | 90 | * |
| @@ -147,9 +113,9 @@ | ||
| 147 | 113 | * |
| 148 | 114 | * @return bool |
| 149 | 115 | */ |
| 150 | 116 | public static function get_post_types( $post ) { |
| 151 | - return apply_filters( 'tutor_check_course_post_type', self::POST_TYPE === get_post_type( $post ), get_post_type( $post ) ); | |
| 117 | + return apply_filters( 'tutor_check_course_post_type', get_post_type( $post ) ); | |
| 152 | 118 | } |
| 153 | 119 | |
| 154 | 120 | /** |
| 155 | 121 | * Get courses |
| @@ -272,12 +238,25 @@ | ||
| 272 | 238 | * @since 1.0.0 |
| 273 | 239 | * |
| 274 | 240 | * @param int $quiz_id quiz id. |
| 275 | 241 | * |
| 276 | - * @return mixed WP_Post|null | |
| 242 | + * @return array|bool|null|object|void | |
| 277 | 243 | */ |
| 278 | 244 | public static function get_course_by_quiz( $quiz_id ) { |
| 279 | - return get_post_parent( get_post_parent( $quiz_id ) ); | |
| 245 | + $quiz_id = tutils()->get_post_id( $quiz_id ); | |
| 246 | + $post = get_post( $quiz_id ); | |
| 247 | + | |
| 248 | + if ( $post ) { | |
| 249 | + $course = get_post( $post->post_parent ); | |
| 250 | + if ( $course ) { | |
| 251 | + if ( tutor()->course_post_type !== $course->post_type ) { | |
| 252 | + $course = get_post( $course->post_parent ); | |
| 253 | + } | |
| 254 | + return $course; | |
| 255 | + } | |
| 256 | + } | |
| 257 | + | |
| 258 | + return false; | |
| 280 | 259 | } |
| 281 | 260 | |
| 282 | 261 | /** |
| 283 | 262 | * Get courses by a instructor |
| @@ -283,9 +262,8 @@ | ||
| 283 | 262 | * Get courses by a instructor |
| 284 | 263 | * |
| 285 | 264 | * @since 1.0.0 |
| 286 | 265 | * @since 3.5.0 param $post_types added. |
| 287 | - * @since 4.0.0 params $search and $order added. | |
| 288 | 266 | * |
| 289 | 267 | * @param integer $instructor_id instructor id. |
| 290 | 268 | * @param array|string $post_status post status. |
| 291 | 269 | * @param integer $offset offset. |
| @@ -291,23 +269,12 @@ | ||
| 291 | 269 | * @param integer $offset offset. |
| 292 | 270 | * @param integer $limit limit. |
| 293 | 271 | * @param boolean $count_only count or not. |
| 294 | 272 | * @param array $post_types array of post types. |
| 295 | - * @param string $search search keyword. | |
| 296 | - * @param string $order order. | |
| 297 | 273 | * |
| 298 | 274 | * @return array|null|object |
| 299 | 275 | */ |
| 300 | - public static function get_courses_by_instructor( | |
| 301 | - $instructor_id = 0, | |
| 302 | - $post_status = array( 'publish' ), | |
| 303 | - int $offset = 0, | |
| 304 | - int $limit = PHP_INT_MAX, | |
| 305 | - $count_only = false, | |
| 306 | - $post_types = array(), | |
| 307 | - $search = '', | |
| 308 | - $order = 'DESC' | |
| 309 | - ) { | |
| 276 | + public static function get_courses_by_instructor( $instructor_id = 0, $post_status = array( 'publish' ), int $offset = 0, int $limit = PHP_INT_MAX, $count_only = false, $post_types = array() ) { | |
| 310 | 277 | global $wpdb; |
| 311 | 278 | $offset = sanitize_text_field( $offset ); |
| 312 | 279 | $limit = sanitize_text_field( $limit ); |
| 313 | 280 | $instructor_id = tutils()->get_user_id( $instructor_id ); |
| @@ -325,21 +292,8 @@ | ||
| 325 | 292 | $statuses = "'" . implode( "','", $post_status ) . "'"; |
| 326 | 293 | $where_post_status = "AND $wpdb->posts.post_status IN({$statuses}) "; |
| 327 | 294 | } |
| 328 | 295 | |
| 329 | - $search_sql = ''; | |
| 330 | - if ( ! empty( $search ) ) { | |
| 331 | - $like = '%' . $wpdb->esc_like( $search ) . '%'; | |
| 332 | - $search_sql = $wpdb->prepare( | |
| 333 | - " AND ( {$wpdb->posts}.post_title LIKE %s OR {$wpdb->posts}.post_excerpt LIKE %s ) ", | |
| 334 | - $like, | |
| 335 | - $like | |
| 336 | - ); | |
| 337 | - } | |
| 338 | - | |
| 339 | - $order = strtoupper( $order ) === 'ASC' ? 'ASC' : 'DESC'; | |
| 340 | - $order_sql = " ORDER BY $wpdb->posts.post_date {$order} "; | |
| 341 | - | |
| 342 | 296 | $select_col = $count_only ? " COUNT(DISTINCT $wpdb->posts.ID) " : " $wpdb->posts.* "; |
| 343 | 297 | $limit_offset = $count_only ? '' : " LIMIT $offset, $limit "; |
| 344 | 298 | |
| 345 | 299 | //phpcs:disable |
| @@ -344,18 +298,17 @@ | ||
| 344 | 298 | |
| 345 | 299 | //phpcs:disable |
| 346 | 300 | $query = $wpdb->prepare( |
| 347 | 301 | "SELECT $select_col |
| 348 | - FROM $wpdb->posts | |
| 302 | + FROM $wpdb->posts | |
| 349 | 303 | LEFT JOIN {$wpdb->usermeta} |
| 350 | - ON $wpdb->usermeta.user_id = %d | |
| 351 | - AND $wpdb->usermeta.meta_key = %s | |
| 352 | - AND $wpdb->usermeta.meta_value = $wpdb->posts.ID | |
| 353 | - WHERE 1 = 1 {$where_post_status} | |
| 304 | + ON $wpdb->usermeta.user_id = %d | |
| 305 | + AND $wpdb->usermeta.meta_key = %s | |
| 306 | + AND $wpdb->usermeta.meta_value = $wpdb->posts.ID | |
| 307 | + WHERE 1 = 1 {$where_post_status} | |
| 354 | 308 | AND $wpdb->posts.post_type IN ({$post_types}) |
| 355 | 309 | AND ($wpdb->posts.post_author = %d OR $wpdb->usermeta.user_id = %d) |
| 356 | - {$search_sql} | |
| 357 | - {$order_sql} {$limit_offset}", | |
| 310 | + ORDER BY $wpdb->posts.post_date DESC $limit_offset", | |
| 358 | 311 | $instructor_id, |
| 359 | 312 | '_tutor_instructor_course_id', |
| 360 | 313 | $instructor_id, |
| 361 | 314 | $instructor_id |
| @@ -361,10 +314,8 @@ | ||
| 361 | 314 | $instructor_id |
| 362 | 315 | ); |
| 363 | 316 | //phpcs:enable |
| 364 | 317 | |
| 365 | - $query = apply_filters( 'modify_get_courses_by_instructor_query', $query, $instructor_id, $where_post_status, $post_types, $limit_offset, $select_col ); | |
| 366 | - | |
| 367 | 318 | //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 368 | 319 | return $count_only ? $wpdb->get_var( $query ) : $wpdb->get_results( $query, OBJECT ); |
| 369 | 320 | } |
| 370 | 321 | |
| @@ -541,49 +492,21 @@ | ||
| 541 | 492 | foreach ( $topic_content_ids as $content_id ) { |
| 542 | 493 | /** |
| 543 | 494 | * Delete Quiz data |
| 544 | 495 | */ |
| 545 | - if ( get_post_type( $content_id ) === $quiz_post_type ) { | |
| 546 | - // Collect file paths from all question types that store files before deleting rows (files deleted after DB for safety). | |
| 547 | - $attempts_for_quiz = QueryHelper::get_all( 'tutor_quiz_attempts', array( 'quiz_id' => $content_id ), 'attempt_id', -1 ); | |
| 548 | - $attempt_file_paths = array(); | |
| 549 | - if ( ! empty( $attempts_for_quiz ) ) { | |
| 550 | - $attempt_ids = array_map( | |
| 551 | - function ( $row ) { | |
| 552 | - return (int) $row->attempt_id; | |
| 553 | - }, | |
| 554 | - $attempts_for_quiz | |
| 555 | - ); | |
| 556 | - $attempt_file_paths = apply_filters( 'tutor_quiz/attempt_file_paths_for_deletion', array(), $attempt_ids ); | |
| 557 | - $attempt_file_paths = is_array( $attempt_file_paths ) ? array_values( array_filter( array_unique( $attempt_file_paths ) ) ) : array(); | |
| 558 | - } | |
| 559 | - | |
| 496 | + if ( get_post_type( $content_id ) === 'tutor_quiz' ) { | |
| 560 | 497 | $wpdb->delete( $wpdb->prefix . 'tutor_quiz_attempts', array( 'quiz_id' => $content_id ) ); |
| 561 | 498 | $wpdb->delete( $wpdb->prefix . 'tutor_quiz_attempt_answers', array( 'quiz_id' => $content_id ) ); |
| 562 | 499 | |
| 563 | - QuizModel::delete_files_by_paths( $attempt_file_paths ); | |
| 564 | - | |
| 565 | 500 | do_action( 'tutor_before_delete_quiz_content', $content_id, null ); |
| 566 | 501 | |
| 567 | - // Collect instructor file paths before deleting question data (e.g. draw_image / pin_image masks). | |
| 568 | - /** | |
| 569 | - * Filter to get file paths for quiz deletion. | |
| 570 | - * Pro and other add-ons register their question types via this filter. | |
| 571 | - * | |
| 572 | - * @param string[] $file_paths Paths collected so far. | |
| 573 | - * @param int $quiz_id Quiz post ID. | |
| 574 | - */ | |
| 575 | - $quiz_file_paths = apply_filters( 'tutor_quiz_quiz_file_paths_for_deletion', array(), $content_id ); | |
| 576 | - | |
| 577 | 502 | $questions_ids = $wpdb->get_col( $wpdb->prepare( "SELECT question_id FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = %d ", $content_id ) ); |
| 578 | 503 | if ( is_array( $questions_ids ) && count( $questions_ids ) ) { |
| 579 | - $in_clause = QueryHelper::prepare_in_clause( $questions_ids ); | |
| 504 | + $in_question_ids = "'" . implode( "','", $questions_ids ) . "'"; | |
| 580 | 505 | //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 581 | - $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id IN({$in_clause}) " ) ); | |
| 506 | + $wpdb->query( "DELETE FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id IN({$in_question_ids}) " ); | |
| 582 | 507 | } |
| 583 | 508 | $wpdb->delete( $wpdb->prefix . 'tutor_quiz_questions', array( 'quiz_id' => $content_id ) ); |
| 584 | - | |
| 585 | - QuizModel::delete_files_by_paths( $quiz_file_paths ); | |
| 586 | 509 | } |
| 587 | 510 | |
| 588 | 511 | /** |
| 589 | 512 | * Delete assignment data ( Assignments, Assignment Submit, Assignment Evalutation ) |
| @@ -748,8 +671,9 @@ | ||
| 748 | 671 | } |
| 749 | 672 | |
| 750 | 673 | $args = wp_parse_args( $args, $default_args ); |
| 751 | 674 | return new \WP_Query( $args ); |
| 675 | + | |
| 752 | 676 | } |
| 753 | 677 | |
| 754 | 678 | /** |
| 755 | 679 | * Check the course is completeable or not |
| @@ -986,35 +910,17 @@ | ||
| 986 | 910 | return $instructor_ids; |
| 987 | 911 | } |
| 988 | 912 | |
| 989 | 913 | /** |
| 990 | - * Check tax collection is enabled for single purchase course/bundle | |
| 991 | - * | |
| 992 | - * @since 3.7.0 | |
| 993 | - * | |
| 994 | - * @param int $post_id course or bundle id. | |
| 995 | - * | |
| 996 | - * @return boolean | |
| 997 | - */ | |
| 998 | - public static function is_tax_enabled_for_single_purchase( $post_id ) { | |
| 999 | - if ( ! Tax::is_individual_control_enabled() ) { | |
| 1000 | - return true; | |
| 1001 | - } | |
| 1002 | - | |
| 1003 | - $data = get_post_meta( $post_id, Course::TAX_ON_SINGLE_META, true ); | |
| 1004 | - return ( '1' === $data || '' === $data ); | |
| 1005 | - } | |
| 1006 | - | |
| 1007 | - /** | |
| 1008 | 914 | * Count total attachments available in all courses or specific |
| 1009 | 915 | * |
| 1010 | 916 | * @since 3.6.0 |
| 1011 | 917 | * |
| 1012 | - * @param int|array $course_id Course id or array of ids, to get course's attachment. | |
| 918 | + * @param int $course_id Course id to get only a particular course's attachment. | |
| 1013 | 919 | * |
| 1014 | 920 | * @return int |
| 1015 | 921 | */ |
| 1016 | - public static function count_attachment( $course_id = 0 ) { | |
| 922 | + public static function count_attachment( int $course_id = 0 ) { | |
| 1017 | 923 | global $wpdb; |
| 1018 | 924 | |
| 1019 | 925 | $total_count = 0; |
| 1020 | 926 | |
| @@ -1030,11 +936,10 @@ | ||
| 1030 | 936 | // Prepare query. |
| 1031 | 937 | $select = array( 'pm.meta_value' ); |
| 1032 | 938 | $where = array(); |
| 1033 | 939 | if ( $course_id ) { |
| 1034 | - $where['p.ID'] = is_array( $course_id ) ? array( 'IN', $course_id ) : $course_id; | |
| 940 | + $where['p.ID'] = $course_id; | |
| 1035 | 941 | } |
| 1036 | - | |
| 1037 | 942 | $search = array(); |
| 1038 | 943 | $limit = 0; // Get all. |
| 1039 | 944 | $offset = 0; |
| 1040 | 945 | $order_by = ''; |
| @@ -1072,682 +977,36 @@ | ||
| 1072 | 977 | return $total_count; |
| 1073 | 978 | } |
| 1074 | 979 | |
| 1075 | 980 | /** |
| 1076 | - * Count total questions available in all or specific courses | |
| 1077 | - * | |
| 1078 | - * @since 3.7.1 | |
| 1079 | - * | |
| 1080 | - * @param int|array $course_id Course id or array of ids, to get course's attachment. | |
| 1081 | - * | |
| 1082 | - * @return int | |
| 1083 | - */ | |
| 1084 | - public static function count_questions( $course_id = 0 ) { | |
| 1085 | - global $wpdb; | |
| 1086 | - | |
| 1087 | - $total_count = 0; | |
| 1088 | - $quiz_post_type = tutor()->quiz_post_type; | |
| 1089 | - $topic_post_type = tutor()->topics_post_type; | |
| 1090 | - $course_post_type = tutor()->course_post_type; | |
| 1091 | - | |
| 1092 | - $primary_table = "{$wpdb->prefix}tutor_quiz_questions question"; | |
| 1093 | - | |
| 1094 | - $joining_tables = array( | |
| 1095 | - array( | |
| 1096 | - 'type' => 'INNER', | |
| 1097 | - 'table' => "{$wpdb->posts} q", | |
| 1098 | - 'on' => "q.ID = question.quiz_id AND q.post_type='{$quiz_post_type}'", | |
| 1099 | - ), | |
| 1100 | - array( | |
| 1101 | - 'type' => 'INNER', | |
| 1102 | - 'table' => "{$wpdb->posts} t", | |
| 1103 | - 'on' => "q.post_parent = t.ID AND t.post_type='{$topic_post_type}'", | |
| 1104 | - ), | |
| 1105 | - array( | |
| 1106 | - 'type' => 'INNER', | |
| 1107 | - 'table' => "{$wpdb->posts} c", | |
| 1108 | - 'on' => "c.ID = t.post_parent AND c.post_type='{$course_post_type}'", | |
| 1109 | - ), | |
| 1110 | - ); | |
| 1111 | - | |
| 1112 | - // Prepare query. | |
| 1113 | - $where = array(); | |
| 1114 | - if ( $course_id ) { | |
| 1115 | - $where['c.ID'] = is_array( $course_id ) ? array( 'IN', $course_id ) : $course_id; | |
| 1116 | - } | |
| 1117 | - | |
| 1118 | - $search = array(); | |
| 1119 | - | |
| 1120 | - $total_count = QueryHelper::get_joined_count( | |
| 1121 | - $primary_table, | |
| 1122 | - $joining_tables, | |
| 1123 | - $where, | |
| 1124 | - $search, | |
| 1125 | - '*' | |
| 1126 | - ); | |
| 1127 | - | |
| 1128 | - return $total_count; | |
| 1129 | - } | |
| 1130 | - | |
| 1131 | - /** | |
| 1132 | 981 | * Count course content |
| 1133 | 982 | * |
| 1134 | 983 | * @since 3.6.0 |
| 1135 | 984 | * |
| 1136 | 985 | * @param string $content_type Content type. |
| 1137 | - * @param array $course_ids Course ids. | |
| 1138 | 986 | * |
| 1139 | 987 | * @return int |
| 1140 | 988 | */ |
| 1141 | - public static function count_course_content( string $content_type, array $course_ids = array() ): int { | |
| 989 | + public static function count_course_content( string $content_type ): int { | |
| 1142 | 990 | $total_count = 0; |
| 1143 | 991 | switch ( $content_type ) { |
| 1144 | 992 | case tutor()->lesson_post_type: |
| 1145 | - $total_count = tutor_utils()->get_total_lesson( $course_ids ); | |
| 993 | + $total_count = tutor_utils()->get_total_lesson(); | |
| 1146 | 994 | break; |
| 1147 | 995 | case tutor()->quiz_post_type: |
| 1148 | - $total_count = tutor_utils()->get_total_quiz( $course_ids ); | |
| 996 | + $total_count = tutor_utils()->get_total_quiz(); | |
| 1149 | 997 | break; |
| 1150 | 998 | case tutor()->assignment_post_type: |
| 1151 | 999 | if ( tutor_utils()->is_addon_enabled( 'tutor-assignments' ) ) { |
| 1152 | - $total_count = ( new Assignments( false ) )->get_total_assignment( $course_ids ); | |
| 1000 | + $total_count = ( new Assignments( false ) )->get_total_assignment(); | |
| 1153 | 1001 | } |
| 1154 | 1002 | break; |
| 1155 | 1003 | case 'attachments': |
| 1156 | - $total_count = self::count_attachment( $course_ids ); | |
| 1004 | + $total_count = self::count_attachment(); | |
| 1157 | 1005 | break; |
| 1158 | - case 'questions': | |
| 1159 | - $total_count = self::count_questions( $course_ids ); | |
| 1160 | - break; | |
| 1161 | 1006 | default: |
| 1162 | 1007 | break; |
| 1163 | 1008 | } |
| 1164 | 1009 | |
| 1165 | 1010 | return (int) $total_count; |
| 1166 | - } | |
| 1167 | - | |
| 1168 | - /** | |
| 1169 | - * Get course dropdown options | |
| 1170 | - * | |
| 1171 | - * @since 3.7.0 | |
| 1172 | - * | |
| 1173 | - * @return array | |
| 1174 | - */ | |
| 1175 | - public static function get_course_dropdown_options() { | |
| 1176 | - $course_options = array( | |
| 1177 | - array( | |
| 1178 | - 'key' => '', | |
| 1179 | - 'title' => __( 'All Courses', 'tutor' ), | |
| 1180 | - ), | |
| 1181 | - ); | |
| 1182 | - | |
| 1183 | - $courses = current_user_can( 'administrator' ) ? self::get_courses() : self::get_courses_by_instructor(); | |
| 1184 | - if ( ! empty( $courses ) ) { | |
| 1185 | - foreach ( $courses as $course ) { | |
| 1186 | - $course_options[] = array( | |
| 1187 | - 'key' => $course->ID, | |
| 1188 | - 'title' => $course->post_title, | |
| 1189 | - ); | |
| 1190 | - } | |
| 1191 | - } | |
| 1192 | - | |
| 1193 | - return apply_filters( 'tutor_course_dropdown_options', $course_options ); | |
| 1194 | - } | |
| 1195 | - | |
| 1196 | - /** | |
| 1197 | - * Get category dropdown options | |
| 1198 | - * | |
| 1199 | - * @since 3.7.0 | |
| 1200 | - * | |
| 1201 | - * @return array | |
| 1202 | - */ | |
| 1203 | - public static function get_category_dropdown_options() { | |
| 1204 | - $category_options = array( | |
| 1205 | - array( | |
| 1206 | - 'key' => '', | |
| 1207 | - 'title' => __( 'All Categories', 'tutor' ), | |
| 1208 | - ), | |
| 1209 | - ); | |
| 1210 | - | |
| 1211 | - $categories = get_terms( | |
| 1212 | - array( | |
| 1213 | - 'taxonomy' => self::COURSE_CATEGORY, | |
| 1214 | - 'orderby' => 'term_id', | |
| 1215 | - 'order' => 'DESC', | |
| 1216 | - ) | |
| 1217 | - ); | |
| 1218 | - if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) { | |
| 1219 | - foreach ( $categories as $category ) { | |
| 1220 | - $category_options[] = array( | |
| 1221 | - 'key' => $category->slug, | |
| 1222 | - 'title' => $category->name, | |
| 1223 | - ); | |
| 1224 | - } | |
| 1225 | - } | |
| 1226 | - | |
| 1227 | - return $category_options; | |
| 1228 | - } | |
| 1229 | - | |
| 1230 | - /** | |
| 1231 | - * Retrieve all course completion records for a specific course, including meta data. | |
| 1232 | - * | |
| 1233 | - * @since 3.8.1 | |
| 1234 | - * | |
| 1235 | - * @param int $course_id The ID of the course. | |
| 1236 | - * | |
| 1237 | - * @return array Array of course completion objects with meta, empty array if none found, or on database error. | |
| 1238 | - */ | |
| 1239 | - public function get_course_completion_data_by_course_id( int $course_id ): array { | |
| 1240 | - | |
| 1241 | - global $wpdb; | |
| 1242 | - | |
| 1243 | - $where = array( | |
| 1244 | - 'comment_type' => self::COURSE_COMPLETED, | |
| 1245 | - 'comment_post_ID' => $course_id, | |
| 1246 | - 'comment_agent' => 'TutorLMSPlugin', | |
| 1247 | - ); | |
| 1248 | - | |
| 1249 | - $result = QueryHelper::get_all( $wpdb->comments, $where, 'comment_post_ID', -1 ); | |
| 1250 | - | |
| 1251 | - if ( empty( $result ) ) { | |
| 1252 | - return array(); | |
| 1253 | - } | |
| 1254 | - | |
| 1255 | - return array_map( | |
| 1256 | - function ( $item ) { | |
| 1257 | - | |
| 1258 | - return array( | |
| 1259 | - 'completion' => $item, | |
| 1260 | - 'completion_meta' => get_comment_meta( $item->comment_ID ), | |
| 1261 | - ); | |
| 1262 | - }, | |
| 1263 | - $result | |
| 1264 | - ); | |
| 1265 | - } | |
| 1266 | - | |
| 1267 | - /** | |
| 1268 | - * Return completed courses by user_id | |
| 1269 | - * | |
| 1270 | - * @since 1.0.0 | |
| 1271 | - * | |
| 1272 | - * @param int $user_id user id. | |
| 1273 | - * @param int $offset offset. | |
| 1274 | - * @param int $posts_per_page posts per page. | |
| 1275 | - * @param array $args Args to override the defaults. | |
| 1276 | - * | |
| 1277 | - * @return bool|\WP_Query | |
| 1278 | - */ | |
| 1279 | - public static function get_completed_courses_by_user( $user_id = 0, $offset = 0, $posts_per_page = -1, $args = array() ) { | |
| 1280 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 1281 | - $course_ids = tutor_utils()->get_completed_courses_ids_by_user( $user_id, false ); | |
| 1282 | - | |
| 1283 | - if ( count( $course_ids ) ) { | |
| 1284 | - $course_post_type = tutor()->course_post_type; | |
| 1285 | - $course_args = array( | |
| 1286 | - 'post_type' => $course_post_type, | |
| 1287 | - 'post_status' => 'publish', | |
| 1288 | - 'post__in' => $course_ids, | |
| 1289 | - 'posts_per_page' => $posts_per_page, | |
| 1290 | - 'offset' => $offset, | |
| 1291 | - 'orderby' => 'post__in', | |
| 1292 | - ); | |
| 1293 | - | |
| 1294 | - $args = apply_filters( 'tutor_get_completed_courses_by_user', $args, $user_id, $course_post_type ); | |
| 1295 | - $course_args = wp_parse_args( $args, $course_args ); | |
| 1296 | - | |
| 1297 | - return new \WP_Query( $course_args ); | |
| 1298 | - } | |
| 1299 | - | |
| 1300 | - return false; | |
| 1301 | - } | |
| 1302 | - | |
| 1303 | - /** | |
| 1304 | - * Get the active course by user | |
| 1305 | - * | |
| 1306 | - * @since 1.0.0 | |
| 1307 | - * | |
| 1308 | - * @param int $user_id user id. | |
| 1309 | - * @param int $offset offset. | |
| 1310 | - * @param int $posts_per_page posts per page. | |
| 1311 | - * @param array $args Args to override the defaults. | |
| 1312 | - * | |
| 1313 | - * @return bool|\WP_Query | |
| 1314 | - */ | |
| 1315 | - public static function get_active_courses_by_user( $user_id = 0, $offset = 0, $posts_per_page = -1, $args = array() ) { | |
| 1316 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 1317 | - $course_ids = tutor_utils()->get_completed_courses_ids_by_user( $user_id, false ); | |
| 1318 | - $enrolled_course_ids = tutor_utils()->get_enrolled_courses_ids_by_user( $user_id, false ); | |
| 1319 | - $active_courses = array_diff( $enrolled_course_ids, $course_ids ); | |
| 1320 | - | |
| 1321 | - if ( count( $active_courses ) ) { | |
| 1322 | - $course_post_type = tutor()->course_post_type; | |
| 1323 | - $course_args = array( | |
| 1324 | - 'post_type' => apply_filters( 'tutor_active_courses_post_types', array( $course_post_type ) ), | |
| 1325 | - 'post_status' => 'publish', | |
| 1326 | - 'post__in' => $active_courses, | |
| 1327 | - 'posts_per_page' => $posts_per_page, | |
| 1328 | - 'offset' => $offset, | |
| 1329 | - 'orderby' => 'post__in', | |
| 1330 | - ); | |
| 1331 | - | |
| 1332 | - $args = apply_filters( 'tutor_get_active_courses_by_user', $args, $user_id, $course_post_type ); | |
| 1333 | - $course_args = wp_parse_args( $args, $course_args ); | |
| 1334 | - | |
| 1335 | - return new \WP_Query( $course_args ); | |
| 1336 | - } | |
| 1337 | - | |
| 1338 | - return false; | |
| 1339 | - } | |
| 1340 | - | |
| 1341 | - /** | |
| 1342 | - * Get the enrolled courses by user | |
| 1343 | - * | |
| 1344 | - * @since 1.0.0 | |
| 1345 | - * @since 2.5.0 $filters param added to query enrolled courses with additional filters. | |
| 1346 | - * | |
| 1347 | - * @since 3.4.0 $filters replaced with $args to override the defaults. | |
| 1348 | - * | |
| 1349 | - * @param integer $user_id user id. | |
| 1350 | - * @param mixed $post_status post status. | |
| 1351 | - * @param integer $offset offset. | |
| 1352 | - * @param integer $posts_per_page post per page. | |
| 1353 | - * @param array $args Args to override the defaults. | |
| 1354 | - * | |
| 1355 | - * @return bool|\WP_Query | |
| 1356 | - */ | |
| 1357 | - public static function get_enrolled_courses_by_user( $user_id = 0, $post_status = 'publish', $offset = 0, $posts_per_page = -1, $args = array() ) { | |
| 1358 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 1359 | - $course_ids = array_unique( tutor_utils()->get_enrolled_courses_ids_by_user( $user_id, false ) ); | |
| 1360 | - | |
| 1361 | - if ( count( $course_ids ) ) { | |
| 1362 | - $course_post_type = tutor()->course_post_type; | |
| 1363 | - $course_args = array( | |
| 1364 | - 'post_type' => $course_post_type, | |
| 1365 | - 'post_status' => $post_status, | |
| 1366 | - 'post__in' => $course_ids, | |
| 1367 | - 'offset' => $offset, | |
| 1368 | - 'posts_per_page' => $posts_per_page, | |
| 1369 | - 'orderby' => 'post__in', | |
| 1370 | - ); | |
| 1371 | - | |
| 1372 | - $args = apply_filters( 'tutor_get_enrolled_courses_by_user', $args, $user_id, $course_post_type ); | |
| 1373 | - $course_args = wp_parse_args( $args, $course_args ); | |
| 1374 | - | |
| 1375 | - $result = new \WP_Query( $course_args ); | |
| 1376 | - | |
| 1377 | - if ( is_object( $result ) && is_array( $result->posts ) ) { | |
| 1378 | - | |
| 1379 | - // Sort courses according to the id list. | |
| 1380 | - $new_array = array(); | |
| 1381 | - | |
| 1382 | - foreach ( $course_ids as $id ) { | |
| 1383 | - foreach ( $result->posts as $post ) { | |
| 1384 | - $post->ID == $id ? $new_array[] = $post : 0; | |
| 1385 | - } | |
| 1386 | - } | |
| 1387 | - | |
| 1388 | - $result->posts = $new_array; | |
| 1389 | - } | |
| 1390 | - | |
| 1391 | - return $result; | |
| 1392 | - } | |
| 1393 | - | |
| 1394 | - return false; | |
| 1395 | - } | |
| 1396 | - | |
| 1397 | - /** | |
| 1398 | - * Get the number of courses created by an instructor within a given date range. | |
| 1399 | - * | |
| 1400 | - * @since 4.0.0 | |
| 1401 | - * | |
| 1402 | - * If no date range is provided, returns the total course count for the instructor. | |
| 1403 | - * Course creation date is determined using the `_wp_old_date` meta value when available; otherwise, the course | |
| 1404 | - * post date is used. | |
| 1405 | - * | |
| 1406 | - * @param string|null $start_date Start date in Y-m-d format. | |
| 1407 | - * @param string|null $end_date End date in Y-m-d format. | |
| 1408 | - * @param int $user_id Course author (user) ID. | |
| 1409 | - * | |
| 1410 | - * @return int Total number of matching courses. | |
| 1411 | - */ | |
| 1412 | - public static function get_course_count_by_date( $start_date, $end_date, $user_id ) { | |
| 1413 | - | |
| 1414 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 1415 | - | |
| 1416 | - if ( empty( $start_date ) && empty( $end_date ) ) { | |
| 1417 | - return (int) self::get_courses_by_instructor( $user_id, array( 'publish', 'private' ), 0, PHP_INT_MAX, true ); | |
| 1418 | - } | |
| 1419 | - | |
| 1420 | - $courses = self::get_courses_by_instructor( $user_id, array( 'publish', 'private' ), 0, PHP_INT_MAX ); | |
| 1421 | - | |
| 1422 | - if ( empty( $courses ) ) { | |
| 1423 | - return 0; | |
| 1424 | - } | |
| 1425 | - | |
| 1426 | - $start_date_timestamp = strtotime( $start_date . ' 00:00:00' ); | |
| 1427 | - $end_date_timestamp = strtotime( $end_date . ' 23:59:59' ); | |
| 1428 | - | |
| 1429 | - $filtered = array_filter( | |
| 1430 | - $courses, | |
| 1431 | - function ( $course ) use ( $start_date_timestamp, $end_date_timestamp ) { | |
| 1432 | - $creation_date = get_post_meta( $course->ID, '_wp_old_date', true ); | |
| 1433 | - | |
| 1434 | - $course_creation_date = empty( $creation_date ) ? strtotime( $course->post_date_gmt ) : strtotime( $creation_date ); | |
| 1435 | - | |
| 1436 | - return $start_date_timestamp <= $course_creation_date && $end_date_timestamp >= $course_creation_date; | |
| 1437 | - } | |
| 1438 | - ); | |
| 1439 | - | |
| 1440 | - return count( $filtered ); | |
| 1441 | - } | |
| 1442 | - | |
| 1443 | - /** | |
| 1444 | - * Get topic-wise progress data for a course and a student. | |
| 1445 | - * | |
| 1446 | - * @since 4.0.0 | |
| 1447 | - * | |
| 1448 | - * @param int $course_id Course ID. | |
| 1449 | - * @param int $student_id Student ID. | |
| 1450 | - * | |
| 1451 | - * @return array[] Topic progress data. | |
| 1452 | - */ | |
| 1453 | - public static function get_course_progress_details( $course_id, $student_id ) { | |
| 1454 | - | |
| 1455 | - $topics_query = tutor_utils()->get_topics( $course_id ); | |
| 1456 | - $topic_list = array(); | |
| 1457 | - | |
| 1458 | - if ( empty( $topics_query ) || ! $topics_query->have_posts() ) { | |
| 1459 | - return $topic_list; | |
| 1460 | - } | |
| 1461 | - | |
| 1462 | - foreach ( $topics_query->posts as $topic_post ) { | |
| 1463 | - $topic_id = (int) $topic_post->ID; | |
| 1464 | - $items = array(); | |
| 1465 | - $completion_percentage = tutor_utils()->count_completed_contents_by_topic( $topic_id, $student_id ); | |
| 1466 | - $contents_query = tutor_utils()->get_course_contents_by_topic( $topic_id, -1 ); | |
| 1467 | - | |
| 1468 | - if ( ! empty( $contents_query ) && $contents_query->have_posts() ) { | |
| 1469 | - foreach ( $contents_query->posts as $content_post ) { | |
| 1470 | - $items[] = ( new self() )->build_course_progress_item( $content_post, $student_id, $course_id ); | |
| 1471 | - } | |
| 1472 | - } | |
| 1473 | - | |
| 1474 | - $topic_list[] = array( | |
| 1475 | - 'id' => $topic_id, | |
| 1476 | - 'summary' => $topic_post->post_content, | |
| 1477 | - 'title' => get_the_title( $topic_id ), | |
| 1478 | - 'items' => $items, | |
| 1479 | - 'completion_percentage' => $completion_percentage['percentage'] ?? 0, | |
| 1480 | - ); | |
| 1481 | - } | |
| 1482 | - | |
| 1483 | - wp_reset_postdata(); | |
| 1484 | - | |
| 1485 | - return $topic_list; | |
| 1486 | - } | |
| 1487 | - | |
| 1488 | - /** | |
| 1489 | - * Check if the current user has course content access | |
| 1490 | - * | |
| 1491 | - * @since 4.0.0 | |
| 1492 | - * | |
| 1493 | - * @throws InvalidArgumentException If args are invalid. | |
| 1494 | - * | |
| 1495 | - * @param array $args Array of arguments. | |
| 1496 | - * | |
| 1497 | - * `$defaults = array( | |
| 1498 | - * 'current_post_type' => '', | |
| 1499 | - * 'current_post_id' => 0, | |
| 1500 | - * 'course_id' => 0, | |
| 1501 | - * 'is_public' => false, | |
| 1502 | - * 'is_enrolled' => false, | |
| 1503 | - * );`. | |
| 1504 | - * | |
| 1505 | - * @return bool | |
| 1506 | - */ | |
| 1507 | - public static function has_course_content_access( array $args = array() ): bool { | |
| 1508 | - if ( User::is_admin() ) { | |
| 1509 | - return true; | |
| 1510 | - } | |
| 1511 | - | |
| 1512 | - $defaults = array( | |
| 1513 | - 'current_post_type' => '', | |
| 1514 | - 'current_post_id' => 0, | |
| 1515 | - 'course_id' => 0, | |
| 1516 | - 'is_public' => false, | |
| 1517 | - 'is_enrolled' => false, | |
| 1518 | - ); | |
| 1519 | - | |
| 1520 | - $args = wp_parse_args( $args, $defaults ); | |
| 1521 | - | |
| 1522 | - if ( ! $args['course_id'] || ! $args['current_post_id'] || ! $args['current_post_type'] ) { | |
| 1523 | - throw new InvalidArgumentException( __( 'Invalid argument passed', 'tutor' ) ); | |
| 1524 | - } | |
| 1525 | - | |
| 1526 | - $has_access = false; | |
| 1527 | - $user_id = get_current_user_id(); | |
| 1528 | - switch ( $args['current_post_type'] ) { | |
| 1529 | - case tutor()->lesson_post_type: | |
| 1530 | - $is_preview = (int) get_post_meta( $args['current_post_id'], Lesson::PREVIEW_META_KEY, true ); | |
| 1531 | - if ( $args['is_public'] || $is_preview ) { | |
| 1532 | - $has_access = true; | |
| 1533 | - } elseif ( $args['is_enrolled'] || tutor_utils()->has_enrolled_content_access( 'lesson' ) ) { | |
| 1534 | - $has_access = true; | |
| 1535 | - } | |
| 1536 | - break; | |
| 1537 | - case tutor()->quiz_post_type: | |
| 1538 | - case tutor()->assignment_post_type: | |
| 1539 | - if ( $user_id ) { | |
| 1540 | - $content_type = tutor()->quiz_post_type === $args['current_post_type'] ? 'quiz' : 'assignment'; | |
| 1541 | - if ( $args['is_enrolled'] || $args['is_public'] || tutor_utils()->has_enrolled_content_access( $content_type ) ) { | |
| 1542 | - $has_access = true; | |
| 1543 | - } | |
| 1544 | - } | |
| 1545 | - break; | |
| 1546 | - default: | |
| 1547 | - if ( $args['is_enrolled'] ) { | |
| 1548 | - $has_access = true; | |
| 1549 | - } | |
| 1550 | - break; | |
| 1551 | - } | |
| 1552 | - | |
| 1553 | - return apply_filters( 'tutor_course_content_access', $has_access, $args ); | |
| 1554 | - } | |
| 1555 | - | |
| 1556 | - /** | |
| 1557 | - * Build course progress item data for a specific lesson, quiz, assignment, or meeting. | |
| 1558 | - * | |
| 1559 | - * @since 4.0.0 | |
| 1560 | - * | |
| 1561 | - * @param \WP_Post $post The course content post object. | |
| 1562 | - * @param int $user_id The user ID for whom progress is being calculated. | |
| 1563 | - * @param int $course_id The course ID to which the content belongs. | |
| 1564 | - * | |
| 1565 | - * @return array Associative array of progress item data | |
| 1566 | - */ | |
| 1567 | - private function build_course_progress_item( \WP_Post $post, $user_id, $course_id ) { | |
| 1568 | - | |
| 1569 | - $base_items = array( | |
| 1570 | - 'id' => $post->ID, | |
| 1571 | - 'type' => $post->post_type, | |
| 1572 | - 'link' => esc_url_raw( get_permalink( $post->ID ) ), | |
| 1573 | - 'title' => $post->post_title, | |
| 1574 | - ); | |
| 1575 | - | |
| 1576 | - switch ( $post->post_type ) { | |
| 1577 | - case tutor()->quiz_post_type: | |
| 1578 | - $quiz_status = ''; | |
| 1579 | - $icon_name = Icon::QUIZ_2; | |
| 1580 | - $icon_class = 'tutor-text-subdued'; | |
| 1581 | - $last_attempt = ( new QuizModel() )->get_first_or_last_attempt( $post->ID, $user_id ); | |
| 1582 | - | |
| 1583 | - if ( is_object( $last_attempt ) && QuizModel::ATTEMPT_STARTED !== $last_attempt->attempt_status ) { | |
| 1584 | - $quiz_status = QuizModel::get_quiz_result( $post->ID, $user_id ); | |
| 1585 | - | |
| 1586 | - $quiz_icon_map = array( | |
| 1587 | - QuizModel::RESULT_FAIL => array( Icon::CROSS_COLORIZE, 'tutor-icon-critical' ), | |
| 1588 | - QuizModel::RESULT_PENDING => array( Icon::INFO_COLORIZE, 'tutor-icon-warning-secondary' ), | |
| 1589 | - ); | |
| 1590 | - | |
| 1591 | - if ( isset( $quiz_icon_map[ $quiz_status ] ) ) { | |
| 1592 | - list( $icon_name, $icon_class ) = $quiz_icon_map[ $quiz_status ]; | |
| 1593 | - } | |
| 1594 | - } | |
| 1595 | - | |
| 1596 | - return array_merge( | |
| 1597 | - $base_items, | |
| 1598 | - array( | |
| 1599 | - 'is_completed' => QuizModel::RESULT_PASS === $quiz_status, | |
| 1600 | - 'label' => __( 'Quiz', 'tutor' ), | |
| 1601 | - 'icon' => $icon_name, | |
| 1602 | - 'icon_class' => $icon_class, | |
| 1603 | - ) | |
| 1604 | - ); | |
| 1605 | - | |
| 1606 | - case tutor()->assignment_post_type: | |
| 1607 | - $status = ''; | |
| 1608 | - | |
| 1609 | - $is_submitted = tutor_utils()->is_assignment_submitted( $post->ID, $user_id ); | |
| 1610 | - $status = $is_submitted ? Assignments::get_assignment_result( $post->ID, $user_id ) : $status; | |
| 1611 | - | |
| 1612 | - if ( ! $is_submitted && Assignments::is_expired( $post->ID, $user_id, $course_id ) ) { | |
| 1613 | - $status = 'fail'; | |
| 1614 | - } | |
| 1615 | - | |
| 1616 | - $icon = Icon::BOOK_2; | |
| 1617 | - $icon_class = 'tutor-text-subdued'; | |
| 1618 | - | |
| 1619 | - $status_map = array( | |
| 1620 | - 'pending' => array( Icon::INFO_COLORIZE, 'tutor-icon-warning-secondary' ), | |
| 1621 | - 'fail' => array( Icon::CROSS_COLORIZE, 'tutor-icon-critical' ), | |
| 1622 | - ); | |
| 1623 | - | |
| 1624 | - if ( isset( $status_map[ $status ] ) ) { | |
| 1625 | - list( $icon, $icon_class ) = $status_map[ $status ]; | |
| 1626 | - } | |
| 1627 | - | |
| 1628 | - return array_merge( | |
| 1629 | - $base_items, | |
| 1630 | - array( | |
| 1631 | - 'is_completed' => 'pass' === $status, | |
| 1632 | - 'label' => __( 'Assignment', 'tutor' ), | |
| 1633 | - 'icon' => $icon, | |
| 1634 | - 'icon_class' => $icon_class, | |
| 1635 | - ) | |
| 1636 | - ); | |
| 1637 | - | |
| 1638 | - case tutor()->zoom_post_type: | |
| 1639 | - if ( ! tutor_utils()->is_addon_enabled( 'tutor-zoom' ) ) { | |
| 1640 | - return $base_items; | |
| 1641 | - } | |
| 1642 | - | |
| 1643 | - return array_merge( | |
| 1644 | - $base_items, | |
| 1645 | - array( | |
| 1646 | - 'label' => __( 'Zoom', 'tutor' ), | |
| 1647 | - 'icon' => Icon::ZOOM, | |
| 1648 | - 'is_completed' => \TUTOR_ZOOM\Zoom::is_zoom_lesson_done( '', $post->ID, $user_id ), | |
| 1649 | - ) | |
| 1650 | - ); | |
| 1651 | - | |
| 1652 | - case tutor()->meet_post_type: | |
| 1653 | - if ( ! tutor_utils()->is_addon_enabled( 'google-meet' ) ) { | |
| 1654 | - return $base_items; | |
| 1655 | - } | |
| 1656 | - | |
| 1657 | - return array_merge( | |
| 1658 | - $base_items, | |
| 1659 | - array( | |
| 1660 | - 'label' => __( 'Google Meet', 'tutor' ), | |
| 1661 | - 'icon' => Icon::GOOGLE_MEET, | |
| 1662 | - 'is_completed' => \TutorPro\GoogleMeet\Frontend\Frontend::is_lesson_completed( false, $post->ID, $user_id ), | |
| 1663 | - ) | |
| 1664 | - ); | |
| 1665 | - | |
| 1666 | - default: | |
| 1667 | - $video = tutor_utils()->get_video_info( $post->ID ); | |
| 1668 | - | |
| 1669 | - return array_merge( | |
| 1670 | - $base_items, | |
| 1671 | - array( | |
| 1672 | - 'video' => $video, | |
| 1673 | - 'video_play_time' => isset( $video->playtime ) ? $video->playtime : '', | |
| 1674 | - 'is_completed' => (bool) tutor_utils()->is_completed_lesson( $post->ID, $user_id ), | |
| 1675 | - 'label' => empty( $video ) ? __( 'Reading', 'tutor' ) : __( 'Video', 'tutor' ), | |
| 1676 | - 'icon' => empty( $video ) ? Icon::COURSES : Icon::VIDEO_CAMERA, | |
| 1677 | - ) | |
| 1678 | - ); | |
| 1679 | - } | |
| 1680 | - } | |
| 1681 | - | |
| 1682 | - /** | |
| 1683 | - * Calculate the total course duration for a set of courses and returns the total time in hours, minutes, and | |
| 1684 | - * seconds. | |
| 1685 | - * | |
| 1686 | - * @since 4.0.0 | |
| 1687 | - * | |
| 1688 | - * @param array<int> | int $course_ids List of course IDs or a single course ID. | |
| 1689 | - * | |
| 1690 | - * @return array{ | |
| 1691 | - * hours: int, | |
| 1692 | - * minutes: int, | |
| 1693 | - * seconds: int | |
| 1694 | - * } Total accumulated duration from all given courses. | |
| 1695 | - */ | |
| 1696 | - public static function get_total_course_duration( $course_ids ): array { | |
| 1697 | - $total_seconds = 0; | |
| 1698 | - $course_ids = is_array( $course_ids ) ? $course_ids : array( $course_ids ); | |
| 1699 | - foreach ( $course_ids as $id ) { | |
| 1700 | - $total_seconds += self::get_course_duration_in_seconds( $id ); | |
| 1701 | - } | |
| 1702 | - | |
| 1703 | - return DateTimeHelper::split_seconds_into_time_units( $total_seconds ); | |
| 1704 | - } | |
| 1705 | - | |
| 1706 | - /** | |
| 1707 | - * Calculate the estimated time spent on courses based on completion progress. | |
| 1708 | - * | |
| 1709 | - * @since 4.0.0 | |
| 1710 | - * | |
| 1711 | - * @param array<int> $course_ids List of course IDs. | |
| 1712 | - * | |
| 1713 | - * @return array{ | |
| 1714 | - * hours: int, | |
| 1715 | - * minutes: int, | |
| 1716 | - * seconds: int | |
| 1717 | - * } Total accumulated duration from all given courses. | |
| 1718 | - */ | |
| 1719 | - public static function get_total_estimated_time_spent( $course_ids ): array { | |
| 1720 | - $total_seconds = 0; | |
| 1721 | - foreach ( $course_ids as $id ) { | |
| 1722 | - $completion_percentage = tutor_utils()->is_completed_course( $id ) | |
| 1723 | - ? 100 | |
| 1724 | - : (int) tutor_utils()->get_course_completed_percent( (int) $id ); | |
| 1725 | - | |
| 1726 | - if ( 0 === $completion_percentage ) { | |
| 1727 | - continue; | |
| 1728 | - } | |
| 1729 | - $course_duration_in_seconds = self::get_course_duration_in_seconds( $id ); | |
| 1730 | - // Calculate the time spent by a user based on the course completion percentage. | |
| 1731 | - $total_seconds += (int) round( $course_duration_in_seconds * ( $completion_percentage / 100 ) ); | |
| 1732 | - } | |
| 1733 | - | |
| 1734 | - return DateTimeHelper::split_seconds_into_time_units( $total_seconds ); | |
| 1735 | - } | |
| 1736 | - | |
| 1737 | - /** | |
| 1738 | - * Get the total duration of a course in seconds. | |
| 1739 | - * | |
| 1740 | - * @since 4.0.0 | |
| 1741 | - * | |
| 1742 | - * @param int $course_id Course ID. | |
| 1743 | - * | |
| 1744 | - * @return int Course duration in seconds. | |
| 1745 | - */ | |
| 1746 | - public static function get_course_duration_in_seconds( int $course_id ): int { | |
| 1747 | - $duration = tutor_utils()->get_course_duration( $course_id, true ); | |
| 1748 | - | |
| 1749 | - return ( $duration['durationHours'] * HOUR_IN_SECONDS ) | |
| 1750 | - + ( $duration['durationMinutes'] * MINUTE_IN_SECONDS ) | |
| 1751 | - + ( $duration['durationSeconds'] ); | |
| 1752 | 1011 | } |
| 1753 | 1012 | } |