| @@ -9,16 +9,11 @@ | ||
| 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 | 14 | use Tutor\Ecommerce\Tax; |
| 18 | -use InvalidArgumentException; | |
| 19 | 15 | use Tutor\Helpers\QueryHelper; |
| 20 | -use TUTOR\User; | |
| 21 | 16 | use TUTOR_ASSIGNMENTS\Assignments; |
| 22 | 17 | |
| 23 | 18 | /** |
| 24 | 19 | * CourseModel Class |
| @@ -49,29 +44,27 @@ | ||
| 49 | 44 | const MODE_FLEXIBLE = 'flexible'; |
| 50 | 45 | const MODE_STRICT = 'strict'; |
| 51 | 46 | |
| 52 | 47 | /** |
| 53 | - * Course attachment/downloadable resources meta key | |
| 48 | + * Course mapped with the product using this meta key | |
| 54 | 49 | * |
| 55 | 50 | * @var string |
| 56 | 51 | */ |
| 57 | - const ATTACHMENT_META_KEY = '_tutor_attachments'; | |
| 52 | + const WC_PRODUCT_META_KEY = '_tutor_course_product_id'; | |
| 58 | 53 | |
| 59 | 54 | /** |
| 60 | - * Course benefits meta key | |
| 55 | + * Course attachment/downloadable resources meta key | |
| 61 | 56 | * |
| 62 | 57 | * @var string |
| 63 | 58 | */ |
| 64 | - const BENEFITS_META_KEY = '_tutor_course_benefits'; | |
| 59 | + const ATTACHMENT_META_KEY = '_tutor_attachments'; | |
| 65 | 60 | |
| 66 | 61 | /** |
| 67 | - * The constant representing the status when a course is completed. | |
| 62 | + * Course benefits meta key | |
| 68 | 63 | * |
| 69 | - * @since 3.8.1 | |
| 70 | - * | |
| 71 | 64 | * @var string |
| 72 | 65 | */ |
| 73 | - const COURSE_COMPLETED = 'course_completed'; | |
| 66 | + const BENEFITS_META_KEY = '_tutor_course_benefits'; | |
| 74 | 67 | |
| 75 | 68 | /** |
| 76 | 69 | * Get available status list. |
| 77 | 70 | * |
| @@ -91,34 +84,8 @@ | ||
| 91 | 84 | ); |
| 92 | 85 | } |
| 93 | 86 | |
| 94 | 87 | /** |
| 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 | 88 | * Course record count |
| 122 | 89 | * |
| 123 | 90 | * @since 2.0.7 |
| 124 | 91 | * |
| @@ -147,9 +114,9 @@ | ||
| 147 | 114 | * |
| 148 | 115 | * @return bool |
| 149 | 116 | */ |
| 150 | 117 | 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 ) ); | |
| 118 | + return apply_filters( 'tutor_check_course_post_type', get_post_type( $post ) ); | |
| 152 | 119 | } |
| 153 | 120 | |
| 154 | 121 | /** |
| 155 | 122 | * Get courses |
| @@ -296,9 +263,8 @@ | ||
| 296 | 263 | * Get courses by a instructor |
| 297 | 264 | * |
| 298 | 265 | * @since 1.0.0 |
| 299 | 266 | * @since 3.5.0 param $post_types added. |
| 300 | - * @since 4.0.0 params $search and $order added. | |
| 301 | 267 | * |
| 302 | 268 | * @param integer $instructor_id instructor id. |
| 303 | 269 | * @param array|string $post_status post status. |
| 304 | 270 | * @param integer $offset offset. |
| @@ -304,23 +270,12 @@ | ||
| 304 | 270 | * @param integer $offset offset. |
| 305 | 271 | * @param integer $limit limit. |
| 306 | 272 | * @param boolean $count_only count or not. |
| 307 | 273 | * @param array $post_types array of post types. |
| 308 | - * @param string $search search keyword. | |
| 309 | - * @param string $order order. | |
| 310 | 274 | * |
| 311 | 275 | * @return array|null|object |
| 312 | 276 | */ |
| 313 | - public static function get_courses_by_instructor( | |
| 314 | - $instructor_id = 0, | |
| 315 | - $post_status = array( 'publish' ), | |
| 316 | - int $offset = 0, | |
| 317 | - int $limit = PHP_INT_MAX, | |
| 318 | - $count_only = false, | |
| 319 | - $post_types = array(), | |
| 320 | - $search = '', | |
| 321 | - $order = 'DESC' | |
| 322 | - ) { | |
| 277 | + 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() ) { | |
| 323 | 278 | global $wpdb; |
| 324 | 279 | $offset = sanitize_text_field( $offset ); |
| 325 | 280 | $limit = sanitize_text_field( $limit ); |
| 326 | 281 | $instructor_id = tutils()->get_user_id( $instructor_id ); |
| @@ -338,21 +293,8 @@ | ||
| 338 | 293 | $statuses = "'" . implode( "','", $post_status ) . "'"; |
| 339 | 294 | $where_post_status = "AND $wpdb->posts.post_status IN({$statuses}) "; |
| 340 | 295 | } |
| 341 | 296 | |
| 342 | - $search_sql = ''; | |
| 343 | - if ( ! empty( $search ) ) { | |
| 344 | - $like = '%' . $wpdb->esc_like( $search ) . '%'; | |
| 345 | - $search_sql = $wpdb->prepare( | |
| 346 | - " AND ( {$wpdb->posts}.post_title LIKE %s OR {$wpdb->posts}.post_excerpt LIKE %s ) ", | |
| 347 | - $like, | |
| 348 | - $like | |
| 349 | - ); | |
| 350 | - } | |
| 351 | - | |
| 352 | - $order = strtoupper( $order ) === 'ASC' ? 'ASC' : 'DESC'; | |
| 353 | - $order_sql = " ORDER BY $wpdb->posts.post_date {$order} "; | |
| 354 | - | |
| 355 | 297 | $select_col = $count_only ? " COUNT(DISTINCT $wpdb->posts.ID) " : " $wpdb->posts.* "; |
| 356 | 298 | $limit_offset = $count_only ? '' : " LIMIT $offset, $limit "; |
| 357 | 299 | |
| 358 | 300 | //phpcs:disable |
| @@ -357,18 +299,17 @@ | ||
| 357 | 299 | |
| 358 | 300 | //phpcs:disable |
| 359 | 301 | $query = $wpdb->prepare( |
| 360 | 302 | "SELECT $select_col |
| 361 | - FROM $wpdb->posts | |
| 303 | + FROM $wpdb->posts | |
| 362 | 304 | LEFT JOIN {$wpdb->usermeta} |
| 363 | - ON $wpdb->usermeta.user_id = %d | |
| 364 | - AND $wpdb->usermeta.meta_key = %s | |
| 365 | - AND $wpdb->usermeta.meta_value = $wpdb->posts.ID | |
| 366 | - WHERE 1 = 1 {$where_post_status} | |
| 305 | + ON $wpdb->usermeta.user_id = %d | |
| 306 | + AND $wpdb->usermeta.meta_key = %s | |
| 307 | + AND $wpdb->usermeta.meta_value = $wpdb->posts.ID | |
| 308 | + WHERE 1 = 1 {$where_post_status} | |
| 367 | 309 | AND $wpdb->posts.post_type IN ({$post_types}) |
| 368 | 310 | AND ($wpdb->posts.post_author = %d OR $wpdb->usermeta.user_id = %d) |
| 369 | - {$search_sql} | |
| 370 | - {$order_sql} {$limit_offset}", | |
| 311 | + ORDER BY $wpdb->posts.post_date DESC $limit_offset", | |
| 371 | 312 | $instructor_id, |
| 372 | 313 | '_tutor_instructor_course_id', |
| 373 | 314 | $instructor_id, |
| 374 | 315 | $instructor_id |
| @@ -554,49 +495,21 @@ | ||
| 554 | 495 | foreach ( $topic_content_ids as $content_id ) { |
| 555 | 496 | /** |
| 556 | 497 | * Delete Quiz data |
| 557 | 498 | */ |
| 558 | - if ( get_post_type( $content_id ) === $quiz_post_type ) { | |
| 559 | - // Collect file paths from all question types that store files before deleting rows (files deleted after DB for safety). | |
| 560 | - $attempts_for_quiz = QueryHelper::get_all( 'tutor_quiz_attempts', array( 'quiz_id' => $content_id ), 'attempt_id', -1 ); | |
| 561 | - $attempt_file_paths = array(); | |
| 562 | - if ( ! empty( $attempts_for_quiz ) ) { | |
| 563 | - $attempt_ids = array_map( | |
| 564 | - function ( $row ) { | |
| 565 | - return (int) $row->attempt_id; | |
| 566 | - }, | |
| 567 | - $attempts_for_quiz | |
| 568 | - ); | |
| 569 | - $attempt_file_paths = apply_filters( 'tutor_quiz/attempt_file_paths_for_deletion', array(), $attempt_ids ); | |
| 570 | - $attempt_file_paths = is_array( $attempt_file_paths ) ? array_values( array_filter( array_unique( $attempt_file_paths ) ) ) : array(); | |
| 571 | - } | |
| 572 | - | |
| 499 | + if ( get_post_type( $content_id ) === 'tutor_quiz' ) { | |
| 573 | 500 | $wpdb->delete( $wpdb->prefix . 'tutor_quiz_attempts', array( 'quiz_id' => $content_id ) ); |
| 574 | 501 | $wpdb->delete( $wpdb->prefix . 'tutor_quiz_attempt_answers', array( 'quiz_id' => $content_id ) ); |
| 575 | 502 | |
| 576 | - QuizModel::delete_files_by_paths( $attempt_file_paths ); | |
| 577 | - | |
| 578 | 503 | do_action( 'tutor_before_delete_quiz_content', $content_id, null ); |
| 579 | 504 | |
| 580 | - // Collect instructor file paths before deleting question data (e.g. draw_image / pin_image masks). | |
| 581 | - /** | |
| 582 | - * Filter to get file paths for quiz deletion. | |
| 583 | - * Pro and other add-ons register their question types via this filter. | |
| 584 | - * | |
| 585 | - * @param string[] $file_paths Paths collected so far. | |
| 586 | - * @param int $quiz_id Quiz post ID. | |
| 587 | - */ | |
| 588 | - $quiz_file_paths = apply_filters( 'tutor_quiz_quiz_file_paths_for_deletion', array(), $content_id ); | |
| 589 | - | |
| 590 | 505 | $questions_ids = $wpdb->get_col( $wpdb->prepare( "SELECT question_id FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = %d ", $content_id ) ); |
| 591 | 506 | if ( is_array( $questions_ids ) && count( $questions_ids ) ) { |
| 592 | - $in_clause = QueryHelper::prepare_in_clause( $questions_ids ); | |
| 507 | + $in_question_ids = "'" . implode( "','", $questions_ids ) . "'"; | |
| 593 | 508 | //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 594 | - $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id IN({$in_clause}) " ) ); | |
| 509 | + $wpdb->query( "DELETE FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id IN({$in_question_ids}) " ); | |
| 595 | 510 | } |
| 596 | 511 | $wpdb->delete( $wpdb->prefix . 'tutor_quiz_questions', array( 'quiz_id' => $content_id ) ); |
| 597 | - | |
| 598 | - QuizModel::delete_files_by_paths( $quiz_file_paths ); | |
| 599 | 512 | } |
| 600 | 513 | |
| 601 | 514 | /** |
| 602 | 515 | * Delete assignment data ( Assignments, Assignment Submit, Assignment Evalutation ) |
| @@ -1202,9 +1115,9 @@ | ||
| 1202 | 1115 | ); |
| 1203 | 1116 | } |
| 1204 | 1117 | } |
| 1205 | 1118 | |
| 1206 | - return apply_filters( 'tutor_course_dropdown_options', $course_options ); | |
| 1119 | + return $course_options; | |
| 1207 | 1120 | } |
| 1208 | 1121 | |
| 1209 | 1122 | /** |
| 1210 | 1123 | * Get category dropdown options |
| @@ -1237,530 +1150,6 @@ | ||
| 1237 | 1150 | } |
| 1238 | 1151 | } |
| 1239 | 1152 | |
| 1240 | 1153 | return $category_options; |
| 1241 | - } | |
| 1242 | - | |
| 1243 | - /** | |
| 1244 | - * Retrieve all course completion records for a specific course, including meta data. | |
| 1245 | - * | |
| 1246 | - * @since 3.8.1 | |
| 1247 | - * | |
| 1248 | - * @param int $course_id The ID of the course. | |
| 1249 | - * | |
| 1250 | - * @return array Array of course completion objects with meta, empty array if none found, or on database error. | |
| 1251 | - */ | |
| 1252 | - public function get_course_completion_data_by_course_id( int $course_id ): array { | |
| 1253 | - | |
| 1254 | - global $wpdb; | |
| 1255 | - | |
| 1256 | - $where = array( | |
| 1257 | - 'comment_type' => self::COURSE_COMPLETED, | |
| 1258 | - 'comment_post_ID' => $course_id, | |
| 1259 | - 'comment_agent' => 'TutorLMSPlugin', | |
| 1260 | - ); | |
| 1261 | - | |
| 1262 | - $result = QueryHelper::get_all( $wpdb->comments, $where, 'comment_post_ID', -1 ); | |
| 1263 | - | |
| 1264 | - if ( empty( $result ) ) { | |
| 1265 | - return array(); | |
| 1266 | - } | |
| 1267 | - | |
| 1268 | - return array_map( | |
| 1269 | - function ( $item ) { | |
| 1270 | - | |
| 1271 | - return array( | |
| 1272 | - 'completion' => $item, | |
| 1273 | - 'completion_meta' => get_comment_meta( $item->comment_ID ), | |
| 1274 | - ); | |
| 1275 | - }, | |
| 1276 | - $result | |
| 1277 | - ); | |
| 1278 | - } | |
| 1279 | - | |
| 1280 | - /** | |
| 1281 | - * Return completed courses by user_id | |
| 1282 | - * | |
| 1283 | - * @since 1.0.0 | |
| 1284 | - * | |
| 1285 | - * @param int $user_id user id. | |
| 1286 | - * @param int $offset offset. | |
| 1287 | - * @param int $posts_per_page posts per page. | |
| 1288 | - * @param array $args Args to override the defaults. | |
| 1289 | - * | |
| 1290 | - * @return bool|\WP_Query | |
| 1291 | - */ | |
| 1292 | - public static function get_completed_courses_by_user( $user_id = 0, $offset = 0, $posts_per_page = -1, $args = array() ) { | |
| 1293 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 1294 | - $course_ids = tutor_utils()->get_completed_courses_ids_by_user( $user_id, false ); | |
| 1295 | - | |
| 1296 | - if ( count( $course_ids ) ) { | |
| 1297 | - $course_post_type = tutor()->course_post_type; | |
| 1298 | - $course_args = array( | |
| 1299 | - 'post_type' => $course_post_type, | |
| 1300 | - 'post_status' => 'publish', | |
| 1301 | - 'post__in' => $course_ids, | |
| 1302 | - 'posts_per_page' => $posts_per_page, | |
| 1303 | - 'offset' => $offset, | |
| 1304 | - 'orderby' => 'post__in', | |
| 1305 | - ); | |
| 1306 | - | |
| 1307 | - $args = apply_filters( 'tutor_get_completed_courses_by_user', $args, $user_id, $course_post_type ); | |
| 1308 | - $course_args = wp_parse_args( $args, $course_args ); | |
| 1309 | - | |
| 1310 | - return new \WP_Query( $course_args ); | |
| 1311 | - } | |
| 1312 | - | |
| 1313 | - return false; | |
| 1314 | - } | |
| 1315 | - | |
| 1316 | - /** | |
| 1317 | - * Get the active course by user | |
| 1318 | - * | |
| 1319 | - * @since 1.0.0 | |
| 1320 | - * | |
| 1321 | - * @param int $user_id user id. | |
| 1322 | - * @param int $offset offset. | |
| 1323 | - * @param int $posts_per_page posts per page. | |
| 1324 | - * @param array $args Args to override the defaults. | |
| 1325 | - * | |
| 1326 | - * @return bool|\WP_Query | |
| 1327 | - */ | |
| 1328 | - public static function get_active_courses_by_user( $user_id = 0, $offset = 0, $posts_per_page = -1, $args = array() ) { | |
| 1329 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 1330 | - $course_ids = tutor_utils()->get_completed_courses_ids_by_user( $user_id, false ); | |
| 1331 | - $enrolled_course_ids = tutor_utils()->get_enrolled_courses_ids_by_user( $user_id, false ); | |
| 1332 | - $active_courses = array_diff( $enrolled_course_ids, $course_ids ); | |
| 1333 | - | |
| 1334 | - if ( count( $active_courses ) ) { | |
| 1335 | - $course_post_type = tutor()->course_post_type; | |
| 1336 | - $course_args = array( | |
| 1337 | - 'post_type' => apply_filters( 'tutor_active_courses_post_types', array( $course_post_type ) ), | |
| 1338 | - 'post_status' => 'publish', | |
| 1339 | - 'post__in' => $active_courses, | |
| 1340 | - 'posts_per_page' => $posts_per_page, | |
| 1341 | - 'offset' => $offset, | |
| 1342 | - 'orderby' => 'post__in', | |
| 1343 | - ); | |
| 1344 | - | |
| 1345 | - $args = apply_filters( 'tutor_get_active_courses_by_user', $args, $user_id, $course_post_type ); | |
| 1346 | - $course_args = wp_parse_args( $args, $course_args ); | |
| 1347 | - | |
| 1348 | - return new \WP_Query( $course_args ); | |
| 1349 | - } | |
| 1350 | - | |
| 1351 | - return false; | |
| 1352 | - } | |
| 1353 | - | |
| 1354 | - /** | |
| 1355 | - * Get the enrolled courses by user | |
| 1356 | - * | |
| 1357 | - * @since 1.0.0 | |
| 1358 | - * @since 2.5.0 $filters param added to query enrolled courses with additional filters. | |
| 1359 | - * | |
| 1360 | - * @since 3.4.0 $filters replaced with $args to override the defaults. | |
| 1361 | - * | |
| 1362 | - * @param integer $user_id user id. | |
| 1363 | - * @param mixed $post_status post status. | |
| 1364 | - * @param integer $offset offset. | |
| 1365 | - * @param integer $posts_per_page post per page. | |
| 1366 | - * @param array $args Args to override the defaults. | |
| 1367 | - * | |
| 1368 | - * @return bool|\WP_Query | |
| 1369 | - */ | |
| 1370 | - public static function get_enrolled_courses_by_user( $user_id = 0, $post_status = 'publish', $offset = 0, $posts_per_page = -1, $args = array() ) { | |
| 1371 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 1372 | - $course_ids = array_unique( tutor_utils()->get_enrolled_courses_ids_by_user( $user_id, false ) ); | |
| 1373 | - | |
| 1374 | - if ( count( $course_ids ) ) { | |
| 1375 | - $course_post_type = tutor()->course_post_type; | |
| 1376 | - $course_args = array( | |
| 1377 | - 'post_type' => $course_post_type, | |
| 1378 | - 'post_status' => $post_status, | |
| 1379 | - 'post__in' => $course_ids, | |
| 1380 | - 'offset' => $offset, | |
| 1381 | - 'posts_per_page' => $posts_per_page, | |
| 1382 | - 'orderby' => 'post__in', | |
| 1383 | - ); | |
| 1384 | - | |
| 1385 | - $args = apply_filters( 'tutor_get_enrolled_courses_by_user', $args, $user_id, $course_post_type ); | |
| 1386 | - $course_args = wp_parse_args( $args, $course_args ); | |
| 1387 | - | |
| 1388 | - $result = new \WP_Query( $course_args ); | |
| 1389 | - | |
| 1390 | - if ( is_object( $result ) && is_array( $result->posts ) ) { | |
| 1391 | - | |
| 1392 | - // Sort courses according to the id list. | |
| 1393 | - $new_array = array(); | |
| 1394 | - | |
| 1395 | - foreach ( $course_ids as $id ) { | |
| 1396 | - foreach ( $result->posts as $post ) { | |
| 1397 | - $post->ID == $id ? $new_array[] = $post : 0; | |
| 1398 | - } | |
| 1399 | - } | |
| 1400 | - | |
| 1401 | - $result->posts = $new_array; | |
| 1402 | - } | |
| 1403 | - | |
| 1404 | - return $result; | |
| 1405 | - } | |
| 1406 | - | |
| 1407 | - return false; | |
| 1408 | - } | |
| 1409 | - | |
| 1410 | - /** | |
| 1411 | - * Get the number of courses created by an instructor within a given date range. | |
| 1412 | - * | |
| 1413 | - * @since 4.0.0 | |
| 1414 | - * | |
| 1415 | - * If no date range is provided, returns the total course count for the instructor. | |
| 1416 | - * Course creation date is determined using the `_wp_old_date` meta value when available; otherwise, the course | |
| 1417 | - * post date is used. | |
| 1418 | - * | |
| 1419 | - * @param string|null $start_date Start date in Y-m-d format. | |
| 1420 | - * @param string|null $end_date End date in Y-m-d format. | |
| 1421 | - * @param int $user_id Course author (user) ID. | |
| 1422 | - * | |
| 1423 | - * @return int Total number of matching courses. | |
| 1424 | - */ | |
| 1425 | - public static function get_course_count_by_date( $start_date, $end_date, $user_id ) { | |
| 1426 | - | |
| 1427 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 1428 | - | |
| 1429 | - if ( empty( $start_date ) && empty( $end_date ) ) { | |
| 1430 | - return (int) self::get_courses_by_instructor( $user_id, array( 'publish', 'private' ), 0, PHP_INT_MAX, true ); | |
| 1431 | - } | |
| 1432 | - | |
| 1433 | - $courses = self::get_courses_by_instructor( $user_id, array( 'publish', 'private' ), 0, PHP_INT_MAX ); | |
| 1434 | - | |
| 1435 | - if ( empty( $courses ) ) { | |
| 1436 | - return 0; | |
| 1437 | - } | |
| 1438 | - | |
| 1439 | - $start_date_timestamp = strtotime( $start_date . ' 00:00:00' ); | |
| 1440 | - $end_date_timestamp = strtotime( $end_date . ' 23:59:59' ); | |
| 1441 | - | |
| 1442 | - $filtered = array_filter( | |
| 1443 | - $courses, | |
| 1444 | - function ( $course ) use ( $start_date_timestamp, $end_date_timestamp ) { | |
| 1445 | - $creation_date = get_post_meta( $course->ID, '_wp_old_date', true ); | |
| 1446 | - | |
| 1447 | - $course_creation_date = empty( $creation_date ) ? strtotime( $course->post_date_gmt ) : strtotime( $creation_date ); | |
| 1448 | - | |
| 1449 | - return $start_date_timestamp <= $course_creation_date && $end_date_timestamp >= $course_creation_date; | |
| 1450 | - } | |
| 1451 | - ); | |
| 1452 | - | |
| 1453 | - return count( $filtered ); | |
| 1454 | - } | |
| 1455 | - | |
| 1456 | - /** | |
| 1457 | - * Get topic-wise progress data for a course and a student. | |
| 1458 | - * | |
| 1459 | - * @since 4.0.0 | |
| 1460 | - * | |
| 1461 | - * @param int $course_id Course ID. | |
| 1462 | - * @param int $student_id Student ID. | |
| 1463 | - * | |
| 1464 | - * @return array[] Topic progress data. | |
| 1465 | - */ | |
| 1466 | - public static function get_course_progress_details( $course_id, $student_id ) { | |
| 1467 | - | |
| 1468 | - $topics_query = tutor_utils()->get_topics( $course_id ); | |
| 1469 | - $topic_list = array(); | |
| 1470 | - | |
| 1471 | - if ( empty( $topics_query ) || ! $topics_query->have_posts() ) { | |
| 1472 | - return $topic_list; | |
| 1473 | - } | |
| 1474 | - | |
| 1475 | - foreach ( $topics_query->posts as $topic_post ) { | |
| 1476 | - $topic_id = (int) $topic_post->ID; | |
| 1477 | - $items = array(); | |
| 1478 | - $completion_percentage = tutor_utils()->count_completed_contents_by_topic( $topic_id, $student_id ); | |
| 1479 | - $contents_query = tutor_utils()->get_course_contents_by_topic( $topic_id, -1 ); | |
| 1480 | - | |
| 1481 | - if ( ! empty( $contents_query ) && $contents_query->have_posts() ) { | |
| 1482 | - foreach ( $contents_query->posts as $content_post ) { | |
| 1483 | - $items[] = ( new self() )->build_course_progress_item( $content_post, $student_id, $course_id ); | |
| 1484 | - } | |
| 1485 | - } | |
| 1486 | - | |
| 1487 | - $topic_list[] = array( | |
| 1488 | - 'id' => $topic_id, | |
| 1489 | - 'summary' => $topic_post->post_content, | |
| 1490 | - 'title' => get_the_title( $topic_id ), | |
| 1491 | - 'items' => $items, | |
| 1492 | - 'completion_percentage' => $completion_percentage['percentage'] ?? 0, | |
| 1493 | - ); | |
| 1494 | - } | |
| 1495 | - | |
| 1496 | - wp_reset_postdata(); | |
| 1497 | - | |
| 1498 | - return $topic_list; | |
| 1499 | - } | |
| 1500 | - | |
| 1501 | - /** | |
| 1502 | - * Check if the current user has course content access | |
| 1503 | - * | |
| 1504 | - * @since 4.0.0 | |
| 1505 | - * | |
| 1506 | - * @throws InvalidArgumentException If args are invalid. | |
| 1507 | - * | |
| 1508 | - * @param array $args Array of arguments. | |
| 1509 | - * | |
| 1510 | - * `$defaults = array( | |
| 1511 | - * 'current_post_type' => '', | |
| 1512 | - * 'current_post_id' => 0, | |
| 1513 | - * 'course_id' => 0, | |
| 1514 | - * 'is_public' => false, | |
| 1515 | - * 'is_enrolled' => false, | |
| 1516 | - * );`. | |
| 1517 | - * | |
| 1518 | - * @return bool | |
| 1519 | - */ | |
| 1520 | - public static function has_course_content_access( array $args = array() ): bool { | |
| 1521 | - if ( User::is_admin() ) { | |
| 1522 | - return true; | |
| 1523 | - } | |
| 1524 | - | |
| 1525 | - $defaults = array( | |
| 1526 | - 'current_post_type' => '', | |
| 1527 | - 'current_post_id' => 0, | |
| 1528 | - 'course_id' => 0, | |
| 1529 | - 'is_public' => false, | |
| 1530 | - 'is_enrolled' => false, | |
| 1531 | - ); | |
| 1532 | - | |
| 1533 | - $args = wp_parse_args( $args, $defaults ); | |
| 1534 | - | |
| 1535 | - if ( ! $args['course_id'] || ! $args['current_post_id'] || ! $args['current_post_type'] ) { | |
| 1536 | - throw new InvalidArgumentException( __( 'Invalid argument passed', 'tutor' ) ); | |
| 1537 | - } | |
| 1538 | - | |
| 1539 | - $has_access = false; | |
| 1540 | - $user_id = get_current_user_id(); | |
| 1541 | - switch ( $args['current_post_type'] ) { | |
| 1542 | - case tutor()->lesson_post_type: | |
| 1543 | - $is_preview = (int) get_post_meta( $args['current_post_id'], Lesson::PREVIEW_META_KEY, true ); | |
| 1544 | - if ( $args['is_public'] || $is_preview ) { | |
| 1545 | - $has_access = true; | |
| 1546 | - } elseif ( $args['is_enrolled'] || tutor_utils()->has_enrolled_content_access( 'lesson' ) ) { | |
| 1547 | - $has_access = true; | |
| 1548 | - } | |
| 1549 | - break; | |
| 1550 | - case tutor()->quiz_post_type: | |
| 1551 | - case tutor()->assignment_post_type: | |
| 1552 | - if ( $user_id ) { | |
| 1553 | - $content_type = tutor()->quiz_post_type === $args['current_post_type'] ? 'quiz' : 'assignment'; | |
| 1554 | - if ( $args['is_enrolled'] || $args['is_public'] || tutor_utils()->has_enrolled_content_access( $content_type ) ) { | |
| 1555 | - $has_access = true; | |
| 1556 | - } | |
| 1557 | - } | |
| 1558 | - break; | |
| 1559 | - default: | |
| 1560 | - if ( $args['is_enrolled'] ) { | |
| 1561 | - $has_access = true; | |
| 1562 | - } | |
| 1563 | - break; | |
| 1564 | - } | |
| 1565 | - | |
| 1566 | - return apply_filters( 'tutor_course_content_access', $has_access, $args ); | |
| 1567 | - } | |
| 1568 | - | |
| 1569 | - /** | |
| 1570 | - * Build course progress item data for a specific lesson, quiz, assignment, or meeting. | |
| 1571 | - * | |
| 1572 | - * @since 4.0.0 | |
| 1573 | - * | |
| 1574 | - * @param \WP_Post $post The course content post object. | |
| 1575 | - * @param int $user_id The user ID for whom progress is being calculated. | |
| 1576 | - * @param int $course_id The course ID to which the content belongs. | |
| 1577 | - * | |
| 1578 | - * @return array Associative array of progress item data | |
| 1579 | - */ | |
| 1580 | - private function build_course_progress_item( \WP_Post $post, $user_id, $course_id ) { | |
| 1581 | - | |
| 1582 | - $base_items = array( | |
| 1583 | - 'id' => $post->ID, | |
| 1584 | - 'type' => $post->post_type, | |
| 1585 | - 'link' => esc_url_raw( get_permalink( $post->ID ) ), | |
| 1586 | - 'title' => $post->post_title, | |
| 1587 | - ); | |
| 1588 | - | |
| 1589 | - switch ( $post->post_type ) { | |
| 1590 | - case tutor()->quiz_post_type: | |
| 1591 | - $quiz_status = ''; | |
| 1592 | - $icon_name = Icon::QUIZ_2; | |
| 1593 | - $icon_class = 'tutor-text-subdued'; | |
| 1594 | - $last_attempt = ( new QuizModel() )->get_first_or_last_attempt( $post->ID, $user_id ); | |
| 1595 | - | |
| 1596 | - if ( is_object( $last_attempt ) && QuizModel::ATTEMPT_STARTED !== $last_attempt->attempt_status ) { | |
| 1597 | - $quiz_status = QuizModel::get_quiz_result( $post->ID, $user_id ); | |
| 1598 | - | |
| 1599 | - $quiz_icon_map = array( | |
| 1600 | - QuizModel::RESULT_FAIL => array( Icon::CROSS_COLORIZE, 'tutor-icon-critical' ), | |
| 1601 | - QuizModel::RESULT_PENDING => array( Icon::INFO_COLORIZE, 'tutor-icon-warning-secondary' ), | |
| 1602 | - ); | |
| 1603 | - | |
| 1604 | - if ( isset( $quiz_icon_map[ $quiz_status ] ) ) { | |
| 1605 | - list( $icon_name, $icon_class ) = $quiz_icon_map[ $quiz_status ]; | |
| 1606 | - } | |
| 1607 | - } | |
| 1608 | - | |
| 1609 | - return array_merge( | |
| 1610 | - $base_items, | |
| 1611 | - array( | |
| 1612 | - 'is_completed' => QuizModel::RESULT_PASS === $quiz_status, | |
| 1613 | - 'label' => __( 'Quiz', 'tutor' ), | |
| 1614 | - 'icon' => $icon_name, | |
| 1615 | - 'icon_class' => $icon_class, | |
| 1616 | - ) | |
| 1617 | - ); | |
| 1618 | - | |
| 1619 | - case tutor()->assignment_post_type: | |
| 1620 | - $status = ''; | |
| 1621 | - | |
| 1622 | - $is_submitted = tutor_utils()->is_assignment_submitted( $post->ID, $user_id ); | |
| 1623 | - $status = $is_submitted ? Assignments::get_assignment_result( $post->ID, $user_id ) : $status; | |
| 1624 | - | |
| 1625 | - if ( ! $is_submitted && Assignments::is_expired( $post->ID, $user_id, $course_id ) ) { | |
| 1626 | - $status = 'fail'; | |
| 1627 | - } | |
| 1628 | - | |
| 1629 | - $icon = Icon::BOOK_2; | |
| 1630 | - $icon_class = 'tutor-text-subdued'; | |
| 1631 | - | |
| 1632 | - $status_map = array( | |
| 1633 | - 'pending' => array( Icon::INFO_COLORIZE, 'tutor-icon-warning-secondary' ), | |
| 1634 | - 'fail' => array( Icon::CROSS_COLORIZE, 'tutor-icon-critical' ), | |
| 1635 | - ); | |
| 1636 | - | |
| 1637 | - if ( isset( $status_map[ $status ] ) ) { | |
| 1638 | - list( $icon, $icon_class ) = $status_map[ $status ]; | |
| 1639 | - } | |
| 1640 | - | |
| 1641 | - return array_merge( | |
| 1642 | - $base_items, | |
| 1643 | - array( | |
| 1644 | - 'is_completed' => 'pass' === $status, | |
| 1645 | - 'label' => __( 'Assignment', 'tutor' ), | |
| 1646 | - 'icon' => $icon, | |
| 1647 | - 'icon_class' => $icon_class, | |
| 1648 | - ) | |
| 1649 | - ); | |
| 1650 | - | |
| 1651 | - case tutor()->zoom_post_type: | |
| 1652 | - if ( ! tutor_utils()->is_addon_enabled( 'tutor-zoom' ) ) { | |
| 1653 | - return $base_items; | |
| 1654 | - } | |
| 1655 | - | |
| 1656 | - return array_merge( | |
| 1657 | - $base_items, | |
| 1658 | - array( | |
| 1659 | - 'label' => __( 'Zoom', 'tutor' ), | |
| 1660 | - 'icon' => Icon::ZOOM, | |
| 1661 | - 'is_completed' => \TUTOR_ZOOM\Zoom::is_zoom_lesson_done( '', $post->ID, $user_id ), | |
| 1662 | - ) | |
| 1663 | - ); | |
| 1664 | - | |
| 1665 | - case tutor()->meet_post_type: | |
| 1666 | - if ( ! tutor_utils()->is_addon_enabled( 'google-meet' ) ) { | |
| 1667 | - return $base_items; | |
| 1668 | - } | |
| 1669 | - | |
| 1670 | - return array_merge( | |
| 1671 | - $base_items, | |
| 1672 | - array( | |
| 1673 | - 'label' => __( 'Google Meet', 'tutor' ), | |
| 1674 | - 'icon' => Icon::GOOGLE_MEET, | |
| 1675 | - 'is_completed' => \TutorPro\GoogleMeet\Frontend\Frontend::is_lesson_completed( false, $post->ID, $user_id ), | |
| 1676 | - ) | |
| 1677 | - ); | |
| 1678 | - | |
| 1679 | - default: | |
| 1680 | - $video = tutor_utils()->get_video_info( $post->ID ); | |
| 1681 | - | |
| 1682 | - return array_merge( | |
| 1683 | - $base_items, | |
| 1684 | - array( | |
| 1685 | - 'video' => $video, | |
| 1686 | - 'video_play_time' => isset( $video->playtime ) ? $video->playtime : '', | |
| 1687 | - 'is_completed' => (bool) tutor_utils()->is_completed_lesson( $post->ID, $user_id ), | |
| 1688 | - 'label' => empty( $video ) ? __( 'Reading', 'tutor' ) : __( 'Video', 'tutor' ), | |
| 1689 | - 'icon' => empty( $video ) ? Icon::COURSES : Icon::VIDEO_CAMERA, | |
| 1690 | - ) | |
| 1691 | - ); | |
| 1692 | - } | |
| 1693 | - } | |
| 1694 | - | |
| 1695 | - /** | |
| 1696 | - * Calculate the total course duration for a set of courses and returns the total time in hours, minutes, and | |
| 1697 | - * seconds. | |
| 1698 | - * | |
| 1699 | - * @since 4.0.0 | |
| 1700 | - * | |
| 1701 | - * @param array<int> | int $course_ids List of course IDs or a single course ID. | |
| 1702 | - * | |
| 1703 | - * @return array{ | |
| 1704 | - * hours: int, | |
| 1705 | - * minutes: int, | |
| 1706 | - * seconds: int | |
| 1707 | - * } Total accumulated duration from all given courses. | |
| 1708 | - */ | |
| 1709 | - public static function get_total_course_duration( $course_ids ): array { | |
| 1710 | - $total_seconds = 0; | |
| 1711 | - $course_ids = is_array( $course_ids ) ? $course_ids : array( $course_ids ); | |
| 1712 | - foreach ( $course_ids as $id ) { | |
| 1713 | - $total_seconds += self::get_course_duration_in_seconds( $id ); | |
| 1714 | - } | |
| 1715 | - | |
| 1716 | - return DateTimeHelper::split_seconds_into_time_units( $total_seconds ); | |
| 1717 | - } | |
| 1718 | - | |
| 1719 | - /** | |
| 1720 | - * Calculate the estimated time spent on courses based on completion progress. | |
| 1721 | - * | |
| 1722 | - * @since 4.0.0 | |
| 1723 | - * | |
| 1724 | - * @param array<int> $course_ids List of course IDs. | |
| 1725 | - * | |
| 1726 | - * @return array{ | |
| 1727 | - * hours: int, | |
| 1728 | - * minutes: int, | |
| 1729 | - * seconds: int | |
| 1730 | - * } Total accumulated duration from all given courses. | |
| 1731 | - */ | |
| 1732 | - public static function get_total_estimated_time_spent( $course_ids ): array { | |
| 1733 | - $total_seconds = 0; | |
| 1734 | - foreach ( $course_ids as $id ) { | |
| 1735 | - $completion_percentage = tutor_utils()->is_completed_course( $id ) | |
| 1736 | - ? 100 | |
| 1737 | - : (int) tutor_utils()->get_course_completed_percent( (int) $id ); | |
| 1738 | - | |
| 1739 | - if ( 0 === $completion_percentage ) { | |
| 1740 | - continue; | |
| 1741 | - } | |
| 1742 | - $course_duration_in_seconds = self::get_course_duration_in_seconds( $id ); | |
| 1743 | - // Calculate the time spent by a user based on the course completion percentage. | |
| 1744 | - $total_seconds += (int) round( $course_duration_in_seconds * ( $completion_percentage / 100 ) ); | |
| 1745 | - } | |
| 1746 | - | |
| 1747 | - return DateTimeHelper::split_seconds_into_time_units( $total_seconds ); | |
| 1748 | - } | |
| 1749 | - | |
| 1750 | - /** | |
| 1751 | - * Get the total duration of a course in seconds. | |
| 1752 | - * | |
| 1753 | - * @since 4.0.0 | |
| 1754 | - * | |
| 1755 | - * @param int $course_id Course ID. | |
| 1756 | - * | |
| 1757 | - * @return int Course duration in seconds. | |
| 1758 | - */ | |
| 1759 | - public static function get_course_duration_in_seconds( int $course_id ): int { | |
| 1760 | - $duration = tutor_utils()->get_course_duration( $course_id, true ); | |
| 1761 | - | |
| 1762 | - return ( $duration['durationHours'] * HOUR_IN_SECONDS ) | |
| 1763 | - + ( $duration['durationMinutes'] * MINUTE_IN_SECONDS ) | |
| 1764 | - + ( $duration['durationSeconds'] ); | |
| 1765 | 1154 | } |
| 1766 | 1155 | } |