| @@ -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 |
| @@ -91,34 +86,8 @@ | ||
| 91 | 86 | ); |
| 92 | 87 | } |
| 93 | 88 | |
| 94 | 89 | /** |
| 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 | 90 | * Course record count |
| 122 | 91 | * |
| 123 | 92 | * @since 2.0.7 |
| 124 | 93 | * |
| @@ -272,12 +241,25 @@ | ||
| 272 | 241 | * @since 1.0.0 |
| 273 | 242 | * |
| 274 | 243 | * @param int $quiz_id quiz id. |
| 275 | 244 | * |
| 276 | - * @return mixed WP_Post|null | |
| 245 | + * @return array|bool|null|object|void | |
| 277 | 246 | */ |
| 278 | 247 | public static function get_course_by_quiz( $quiz_id ) { |
| 279 | - return get_post_parent( get_post_parent( $quiz_id ) ); | |
| 248 | + $quiz_id = tutils()->get_post_id( $quiz_id ); | |
| 249 | + $post = get_post( $quiz_id ); | |
| 250 | + | |
| 251 | + if ( $post ) { | |
| 252 | + $course = get_post( $post->post_parent ); | |
| 253 | + if ( $course ) { | |
| 254 | + if ( tutor()->course_post_type !== $course->post_type ) { | |
| 255 | + $course = get_post( $course->post_parent ); | |
| 256 | + } | |
| 257 | + return $course; | |
| 258 | + } | |
| 259 | + } | |
| 260 | + | |
| 261 | + return false; | |
| 280 | 262 | } |
| 281 | 263 | |
| 282 | 264 | /** |
| 283 | 265 | * Get courses by a instructor |
| @@ -283,9 +265,8 @@ | ||
| 283 | 265 | * Get courses by a instructor |
| 284 | 266 | * |
| 285 | 267 | * @since 1.0.0 |
| 286 | 268 | * @since 3.5.0 param $post_types added. |
| 287 | - * @since 4.0.0 params $search and $order added. | |
| 288 | 269 | * |
| 289 | 270 | * @param integer $instructor_id instructor id. |
| 290 | 271 | * @param array|string $post_status post status. |
| 291 | 272 | * @param integer $offset offset. |
| @@ -291,23 +272,12 @@ | ||
| 291 | 272 | * @param integer $offset offset. |
| 292 | 273 | * @param integer $limit limit. |
| 293 | 274 | * @param boolean $count_only count or not. |
| 294 | 275 | * @param array $post_types array of post types. |
| 295 | - * @param string $search search keyword. | |
| 296 | - * @param string $order order. | |
| 297 | 276 | * |
| 298 | 277 | * @return array|null|object |
| 299 | 278 | */ |
| 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 | - ) { | |
| 279 | + 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 | 280 | global $wpdb; |
| 311 | 281 | $offset = sanitize_text_field( $offset ); |
| 312 | 282 | $limit = sanitize_text_field( $limit ); |
| 313 | 283 | $instructor_id = tutils()->get_user_id( $instructor_id ); |
| @@ -325,21 +295,8 @@ | ||
| 325 | 295 | $statuses = "'" . implode( "','", $post_status ) . "'"; |
| 326 | 296 | $where_post_status = "AND $wpdb->posts.post_status IN({$statuses}) "; |
| 327 | 297 | } |
| 328 | 298 | |
| 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 | 299 | $select_col = $count_only ? " COUNT(DISTINCT $wpdb->posts.ID) " : " $wpdb->posts.* "; |
| 343 | 300 | $limit_offset = $count_only ? '' : " LIMIT $offset, $limit "; |
| 344 | 301 | |
| 345 | 302 | //phpcs:disable |
| @@ -344,18 +301,17 @@ | ||
| 344 | 301 | |
| 345 | 302 | //phpcs:disable |
| 346 | 303 | $query = $wpdb->prepare( |
| 347 | 304 | "SELECT $select_col |
| 348 | - FROM $wpdb->posts | |
| 305 | + FROM $wpdb->posts | |
| 349 | 306 | 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} | |
| 307 | + ON $wpdb->usermeta.user_id = %d | |
| 308 | + AND $wpdb->usermeta.meta_key = %s | |
| 309 | + AND $wpdb->usermeta.meta_value = $wpdb->posts.ID | |
| 310 | + WHERE 1 = 1 {$where_post_status} | |
| 354 | 311 | AND $wpdb->posts.post_type IN ({$post_types}) |
| 355 | 312 | AND ($wpdb->posts.post_author = %d OR $wpdb->usermeta.user_id = %d) |
| 356 | - {$search_sql} | |
| 357 | - {$order_sql} {$limit_offset}", | |
| 313 | + ORDER BY $wpdb->posts.post_date DESC $limit_offset", | |
| 358 | 314 | $instructor_id, |
| 359 | 315 | '_tutor_instructor_course_id', |
| 360 | 316 | $instructor_id, |
| 361 | 317 | $instructor_id |
| @@ -541,49 +497,21 @@ | ||
| 541 | 497 | foreach ( $topic_content_ids as $content_id ) { |
| 542 | 498 | /** |
| 543 | 499 | * Delete Quiz data |
| 544 | 500 | */ |
| 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 | - | |
| 501 | + if ( get_post_type( $content_id ) === 'tutor_quiz' ) { | |
| 560 | 502 | $wpdb->delete( $wpdb->prefix . 'tutor_quiz_attempts', array( 'quiz_id' => $content_id ) ); |
| 561 | 503 | $wpdb->delete( $wpdb->prefix . 'tutor_quiz_attempt_answers', array( 'quiz_id' => $content_id ) ); |
| 562 | 504 | |
| 563 | - QuizModel::delete_files_by_paths( $attempt_file_paths ); | |
| 564 | - | |
| 565 | 505 | do_action( 'tutor_before_delete_quiz_content', $content_id, null ); |
| 566 | 506 | |
| 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 | 507 | $questions_ids = $wpdb->get_col( $wpdb->prepare( "SELECT question_id FROM {$wpdb->prefix}tutor_quiz_questions WHERE quiz_id = %d ", $content_id ) ); |
| 578 | 508 | if ( is_array( $questions_ids ) && count( $questions_ids ) ) { |
| 579 | - $in_clause = QueryHelper::prepare_in_clause( $questions_ids ); | |
| 509 | + $in_question_ids = "'" . implode( "','", $questions_ids ) . "'"; | |
| 580 | 510 | //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}) " ) ); | |
| 511 | + $wpdb->query( "DELETE FROM {$wpdb->prefix}tutor_quiz_question_answers WHERE belongs_question_id IN({$in_question_ids}) " ); | |
| 582 | 512 | } |
| 583 | 513 | $wpdb->delete( $wpdb->prefix . 'tutor_quiz_questions', array( 'quiz_id' => $content_id ) ); |
| 584 | - | |
| 585 | - QuizModel::delete_files_by_paths( $quiz_file_paths ); | |
| 586 | 514 | } |
| 587 | 515 | |
| 588 | 516 | /** |
| 589 | 517 | * Delete assignment data ( Assignments, Assignment Submit, Assignment Evalutation ) |
| @@ -1189,9 +1117,9 @@ | ||
| 1189 | 1117 | ); |
| 1190 | 1118 | } |
| 1191 | 1119 | } |
| 1192 | 1120 | |
| 1193 | - return apply_filters( 'tutor_course_dropdown_options', $course_options ); | |
| 1121 | + return $course_options; | |
| 1194 | 1122 | } |
| 1195 | 1123 | |
| 1196 | 1124 | /** |
| 1197 | 1125 | * Get category dropdown options |
| @@ -1277,9 +1205,9 @@ | ||
| 1277 | 1205 | * @return bool|\WP_Query |
| 1278 | 1206 | */ |
| 1279 | 1207 | public static function get_completed_courses_by_user( $user_id = 0, $offset = 0, $posts_per_page = -1, $args = array() ) { |
| 1280 | 1208 | $user_id = tutor_utils()->get_user_id( $user_id ); |
| 1281 | - $course_ids = tutor_utils()->get_completed_courses_ids_by_user( $user_id, false ); | |
| 1209 | + $course_ids = tutor_utils()->get_completed_courses_ids_by_user( $user_id ); | |
| 1282 | 1210 | |
| 1283 | 1211 | if ( count( $course_ids ) ) { |
| 1284 | 1212 | $course_post_type = tutor()->course_post_type; |
| 1285 | 1213 | $course_args = array( |
| @@ -1287,9 +1215,8 @@ | ||
| 1287 | 1215 | 'post_status' => 'publish', |
| 1288 | 1216 | 'post__in' => $course_ids, |
| 1289 | 1217 | 'posts_per_page' => $posts_per_page, |
| 1290 | 1218 | 'offset' => $offset, |
| 1291 | - 'orderby' => 'post__in', | |
| 1292 | 1219 | ); |
| 1293 | 1220 | |
| 1294 | 1221 | $args = apply_filters( 'tutor_get_completed_courses_by_user', $args, $user_id, $course_post_type ); |
| 1295 | 1222 | $course_args = wp_parse_args( $args, $course_args ); |
| @@ -1313,10 +1240,10 @@ | ||
| 1313 | 1240 | * @return bool|\WP_Query |
| 1314 | 1241 | */ |
| 1315 | 1242 | public static function get_active_courses_by_user( $user_id = 0, $offset = 0, $posts_per_page = -1, $args = array() ) { |
| 1316 | 1243 | $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 ); | |
| 1244 | + $course_ids = tutor_utils()->get_completed_courses_ids_by_user( $user_id ); | |
| 1245 | + $enrolled_course_ids = tutor_utils()->get_enrolled_courses_ids_by_user( $user_id ); | |
| 1319 | 1246 | $active_courses = array_diff( $enrolled_course_ids, $course_ids ); |
| 1320 | 1247 | |
| 1321 | 1248 | if ( count( $active_courses ) ) { |
| 1322 | 1249 | $course_post_type = tutor()->course_post_type; |
| @@ -1325,9 +1252,8 @@ | ||
| 1325 | 1252 | 'post_status' => 'publish', |
| 1326 | 1253 | 'post__in' => $active_courses, |
| 1327 | 1254 | 'posts_per_page' => $posts_per_page, |
| 1328 | 1255 | 'offset' => $offset, |
| 1329 | - 'orderby' => 'post__in', | |
| 1330 | 1256 | ); |
| 1331 | 1257 | |
| 1332 | 1258 | $args = apply_filters( 'tutor_get_active_courses_by_user', $args, $user_id, $course_post_type ); |
| 1333 | 1259 | $course_args = wp_parse_args( $args, $course_args ); |
| @@ -1355,9 +1281,9 @@ | ||
| 1355 | 1281 | * @return bool|\WP_Query |
| 1356 | 1282 | */ |
| 1357 | 1283 | public static function get_enrolled_courses_by_user( $user_id = 0, $post_status = 'publish', $offset = 0, $posts_per_page = -1, $args = array() ) { |
| 1358 | 1284 | $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 ) ); | |
| 1285 | + $course_ids = array_unique( tutor_utils()->get_enrolled_courses_ids_by_user( $user_id ) ); | |
| 1360 | 1286 | |
| 1361 | 1287 | if ( count( $course_ids ) ) { |
| 1362 | 1288 | $course_post_type = tutor()->course_post_type; |
| 1363 | 1289 | $course_args = array( |
| @@ -1365,9 +1291,8 @@ | ||
| 1365 | 1291 | 'post_status' => $post_status, |
| 1366 | 1292 | 'post__in' => $course_ids, |
| 1367 | 1293 | 'offset' => $offset, |
| 1368 | 1294 | 'posts_per_page' => $posts_per_page, |
| 1369 | - 'orderby' => 'post__in', | |
| 1370 | 1295 | ); |
| 1371 | 1296 | |
| 1372 | 1297 | $args = apply_filters( 'tutor_get_enrolled_courses_by_user', $args, $user_id, $course_post_type ); |
| 1373 | 1298 | $course_args = wp_parse_args( $args, $course_args ); |
| @@ -1380,17 +1305,13 @@ | ||
| 1380 | 1305 | $new_array = array(); |
| 1381 | 1306 | |
| 1382 | 1307 | foreach ( $course_ids as $id ) { |
| 1383 | 1308 | foreach ( $result->posts as $post ) { |
| 1384 | - if ( $post->ID == $id ) { | |
| 1385 | - $new_array[] = $post; | |
| 1386 | - } | |
| 1309 | + $post->ID == $id ? $new_array[] = $post : 0; | |
| 1387 | 1310 | } |
| 1388 | 1311 | } |
| 1389 | 1312 | |
| 1390 | - if ( count( $new_array ) ) { | |
| 1391 | - $result->posts = $new_array; | |
| 1392 | - } | |
| 1313 | + $result->posts = $new_array; | |
| 1393 | 1314 | } |
| 1394 | 1315 | |
| 1395 | 1316 | return $result; |
| 1396 | 1317 | } |
| @@ -1395,433 +1316,6 @@ | ||
| 1395 | 1316 | return $result; |
| 1396 | 1317 | } |
| 1397 | 1318 | |
| 1398 | 1319 | return false; |
| 1399 | - } | |
| 1400 | - | |
| 1401 | - /** | |
| 1402 | - * Get the number of courses created by an instructor within a given date range. | |
| 1403 | - * | |
| 1404 | - * @since 4.0.0 | |
| 1405 | - * | |
| 1406 | - * If no date range is provided, returns the total course count for the instructor. | |
| 1407 | - * Course creation date is determined using the `_wp_old_date` meta value when available; otherwise, the course | |
| 1408 | - * post date is used. | |
| 1409 | - * | |
| 1410 | - * @param string|null $start_date Start date in Y-m-d format. | |
| 1411 | - * @param string|null $end_date End date in Y-m-d format. | |
| 1412 | - * @param int $user_id Course author (user) ID. | |
| 1413 | - * | |
| 1414 | - * @return int Total number of matching courses. | |
| 1415 | - */ | |
| 1416 | - public static function get_course_count_by_date( $start_date, $end_date, $user_id ) { | |
| 1417 | - | |
| 1418 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 1419 | - | |
| 1420 | - if ( empty( $start_date ) && empty( $end_date ) ) { | |
| 1421 | - return (int) self::get_courses_by_instructor( $user_id, array( 'publish', 'private' ), 0, PHP_INT_MAX, true ); | |
| 1422 | - } | |
| 1423 | - | |
| 1424 | - $courses = self::get_courses_by_instructor( $user_id, array( 'publish', 'private' ), 0, PHP_INT_MAX ); | |
| 1425 | - | |
| 1426 | - if ( empty( $courses ) ) { | |
| 1427 | - return 0; | |
| 1428 | - } | |
| 1429 | - | |
| 1430 | - $start_date_timestamp = strtotime( $start_date . ' 00:00:00' ); | |
| 1431 | - $end_date_timestamp = strtotime( $end_date . ' 23:59:59' ); | |
| 1432 | - | |
| 1433 | - $filtered = array_filter( | |
| 1434 | - $courses, | |
| 1435 | - function ( $course ) use ( $start_date_timestamp, $end_date_timestamp ) { | |
| 1436 | - $creation_date = get_post_meta( $course->ID, '_wp_old_date', true ); | |
| 1437 | - | |
| 1438 | - $course_creation_date = empty( $creation_date ) ? strtotime( $course->post_date_gmt ) : strtotime( $creation_date ); | |
| 1439 | - | |
| 1440 | - return $start_date_timestamp <= $course_creation_date && $end_date_timestamp >= $course_creation_date; | |
| 1441 | - } | |
| 1442 | - ); | |
| 1443 | - | |
| 1444 | - return count( $filtered ); | |
| 1445 | - } | |
| 1446 | - | |
| 1447 | - /** | |
| 1448 | - * Get topic-wise progress data for a course and a student. | |
| 1449 | - * | |
| 1450 | - * @since 4.0.0 | |
| 1451 | - * | |
| 1452 | - * @param int $course_id Course ID. | |
| 1453 | - * @param int $student_id Student ID. | |
| 1454 | - * | |
| 1455 | - * @return array[] Topic progress data. | |
| 1456 | - */ | |
| 1457 | - public static function get_course_progress_details( $course_id, $student_id ) { | |
| 1458 | - | |
| 1459 | - $topics_query = tutor_utils()->get_topics( $course_id ); | |
| 1460 | - $topic_list = array(); | |
| 1461 | - | |
| 1462 | - if ( empty( $topics_query ) || ! $topics_query->have_posts() ) { | |
| 1463 | - return $topic_list; | |
| 1464 | - } | |
| 1465 | - | |
| 1466 | - foreach ( $topics_query->posts as $topic_post ) { | |
| 1467 | - $topic_id = (int) $topic_post->ID; | |
| 1468 | - $items = array(); | |
| 1469 | - $completion_percentage = tutor_utils()->count_completed_contents_by_topic( $topic_id, $student_id ); | |
| 1470 | - $contents_query = tutor_utils()->get_course_contents_by_topic( $topic_id, -1 ); | |
| 1471 | - | |
| 1472 | - if ( ! empty( $contents_query ) && $contents_query->have_posts() ) { | |
| 1473 | - foreach ( $contents_query->posts as $content_post ) { | |
| 1474 | - $items[] = ( new self() )->build_course_progress_item( $content_post, $student_id, $course_id ); | |
| 1475 | - } | |
| 1476 | - } | |
| 1477 | - | |
| 1478 | - $topic_list[] = array( | |
| 1479 | - 'id' => $topic_id, | |
| 1480 | - 'summary' => $topic_post->post_content, | |
| 1481 | - 'title' => get_the_title( $topic_id ), | |
| 1482 | - 'items' => $items, | |
| 1483 | - 'completion_percentage' => $completion_percentage['percentage'] ?? 0, | |
| 1484 | - ); | |
| 1485 | - } | |
| 1486 | - | |
| 1487 | - wp_reset_postdata(); | |
| 1488 | - | |
| 1489 | - return $topic_list; | |
| 1490 | - } | |
| 1491 | - | |
| 1492 | - /** | |
| 1493 | - * Check if the current user has course content access | |
| 1494 | - * | |
| 1495 | - * @since 4.0.0 | |
| 1496 | - * | |
| 1497 | - * @throws InvalidArgumentException If args are invalid. | |
| 1498 | - * | |
| 1499 | - * @param array $args Array of arguments. | |
| 1500 | - * | |
| 1501 | - * `$defaults = array( | |
| 1502 | - * 'current_post_type' => '', | |
| 1503 | - * 'current_post_id' => 0, | |
| 1504 | - * 'course_id' => 0, | |
| 1505 | - * 'is_public' => false, | |
| 1506 | - * 'is_enrolled' => false, | |
| 1507 | - * );`. | |
| 1508 | - * | |
| 1509 | - * @return bool | |
| 1510 | - */ | |
| 1511 | - public static function has_course_content_access( array $args = array() ): bool { | |
| 1512 | - if ( User::is_admin() ) { | |
| 1513 | - return true; | |
| 1514 | - } | |
| 1515 | - | |
| 1516 | - $defaults = array( | |
| 1517 | - 'current_post_type' => '', | |
| 1518 | - 'current_post_id' => 0, | |
| 1519 | - 'course_id' => 0, | |
| 1520 | - 'is_public' => false, | |
| 1521 | - 'is_enrolled' => false, | |
| 1522 | - ); | |
| 1523 | - | |
| 1524 | - $args = wp_parse_args( $args, $defaults ); | |
| 1525 | - | |
| 1526 | - if ( ! $args['course_id'] || ! $args['current_post_id'] || ! $args['current_post_type'] ) { | |
| 1527 | - throw new InvalidArgumentException( __( 'Invalid argument passed', 'tutor' ) ); | |
| 1528 | - } | |
| 1529 | - | |
| 1530 | - $has_access = false; | |
| 1531 | - $user_id = get_current_user_id(); | |
| 1532 | - switch ( $args['current_post_type'] ) { | |
| 1533 | - case tutor()->lesson_post_type: | |
| 1534 | - $is_preview = (int) get_post_meta( $args['current_post_id'], Lesson::PREVIEW_META_KEY, true ); | |
| 1535 | - if ( $args['is_public'] || $is_preview ) { | |
| 1536 | - $has_access = true; | |
| 1537 | - } elseif ( $args['is_enrolled'] || tutor_utils()->has_enrolled_content_access( 'lesson' ) ) { | |
| 1538 | - $has_access = true; | |
| 1539 | - } | |
| 1540 | - break; | |
| 1541 | - case tutor()->quiz_post_type: | |
| 1542 | - case tutor()->assignment_post_type: | |
| 1543 | - if ( $user_id ) { | |
| 1544 | - $content_type = tutor()->quiz_post_type === $args['current_post_type'] ? 'quiz' : 'assignment'; | |
| 1545 | - if ( $args['is_enrolled'] || $args['is_public'] || tutor_utils()->has_enrolled_content_access( $content_type ) ) { | |
| 1546 | - $has_access = true; | |
| 1547 | - } | |
| 1548 | - } | |
| 1549 | - break; | |
| 1550 | - default: | |
| 1551 | - if ( $args['is_enrolled'] ) { | |
| 1552 | - $has_access = true; | |
| 1553 | - } | |
| 1554 | - break; | |
| 1555 | - } | |
| 1556 | - | |
| 1557 | - return apply_filters( 'tutor_course_content_access', $has_access, $args ); | |
| 1558 | - } | |
| 1559 | - | |
| 1560 | - /** | |
| 1561 | - * Build course progress item data for a specific lesson, quiz, assignment, or meeting. | |
| 1562 | - * | |
| 1563 | - * @since 4.0.0 | |
| 1564 | - * | |
| 1565 | - * @param \WP_Post $post The course content post object. | |
| 1566 | - * @param int $user_id The user ID for whom progress is being calculated. | |
| 1567 | - * @param int $course_id The course ID to which the content belongs. | |
| 1568 | - * | |
| 1569 | - * @return array Associative array of progress item data | |
| 1570 | - */ | |
| 1571 | - private function build_course_progress_item( \WP_Post $post, $user_id, $course_id ) { | |
| 1572 | - | |
| 1573 | - $base_items = array( | |
| 1574 | - 'id' => $post->ID, | |
| 1575 | - 'type' => $post->post_type, | |
| 1576 | - 'link' => esc_url_raw( get_permalink( $post->ID ) ), | |
| 1577 | - 'title' => $post->post_title, | |
| 1578 | - ); | |
| 1579 | - | |
| 1580 | - switch ( $post->post_type ) { | |
| 1581 | - case tutor()->quiz_post_type: | |
| 1582 | - $quiz_status = ''; | |
| 1583 | - $icon_name = Icon::QUIZ_2; | |
| 1584 | - $icon_class = 'tutor-text-subdued'; | |
| 1585 | - $last_attempt = ( new QuizModel() )->get_first_or_last_attempt( $post->ID, $user_id ); | |
| 1586 | - | |
| 1587 | - if ( is_object( $last_attempt ) && QuizModel::ATTEMPT_STARTED !== $last_attempt->attempt_status ) { | |
| 1588 | - $quiz_status = QuizModel::get_quiz_result( $post->ID, $user_id ); | |
| 1589 | - | |
| 1590 | - $quiz_icon_map = array( | |
| 1591 | - QuizModel::RESULT_FAIL => array( Icon::CROSS_COLORIZE, 'tutor-icon-critical' ), | |
| 1592 | - QuizModel::RESULT_PENDING => array( Icon::INFO_COLORIZE, 'tutor-icon-warning-secondary' ), | |
| 1593 | - ); | |
| 1594 | - | |
| 1595 | - if ( isset( $quiz_icon_map[ $quiz_status ] ) ) { | |
| 1596 | - list( $icon_name, $icon_class ) = $quiz_icon_map[ $quiz_status ]; | |
| 1597 | - } | |
| 1598 | - } | |
| 1599 | - | |
| 1600 | - return array_merge( | |
| 1601 | - $base_items, | |
| 1602 | - array( | |
| 1603 | - 'is_completed' => QuizModel::RESULT_PASS === $quiz_status, | |
| 1604 | - 'label' => __( 'Quiz', 'tutor' ), | |
| 1605 | - 'icon' => $icon_name, | |
| 1606 | - 'icon_class' => $icon_class, | |
| 1607 | - ) | |
| 1608 | - ); | |
| 1609 | - | |
| 1610 | - case tutor()->assignment_post_type: | |
| 1611 | - $status = ''; | |
| 1612 | - | |
| 1613 | - $is_submitted = tutor_utils()->is_assignment_submitted( $post->ID, $user_id ); | |
| 1614 | - $status = $is_submitted ? Assignments::get_assignment_result( $post->ID, $user_id ) : $status; | |
| 1615 | - | |
| 1616 | - if ( ! $is_submitted && Assignments::is_expired( $post->ID, $user_id, $course_id ) ) { | |
| 1617 | - $status = 'fail'; | |
| 1618 | - } | |
| 1619 | - | |
| 1620 | - $icon = Icon::BOOK_2; | |
| 1621 | - $icon_class = 'tutor-text-subdued'; | |
| 1622 | - | |
| 1623 | - $status_map = array( | |
| 1624 | - 'pending' => array( Icon::INFO_COLORIZE, 'tutor-icon-warning-secondary' ), | |
| 1625 | - 'fail' => array( Icon::CROSS_COLORIZE, 'tutor-icon-critical' ), | |
| 1626 | - ); | |
| 1627 | - | |
| 1628 | - if ( isset( $status_map[ $status ] ) ) { | |
| 1629 | - list( $icon, $icon_class ) = $status_map[ $status ]; | |
| 1630 | - } | |
| 1631 | - | |
| 1632 | - return array_merge( | |
| 1633 | - $base_items, | |
| 1634 | - array( | |
| 1635 | - 'is_completed' => 'pass' === $status, | |
| 1636 | - 'label' => __( 'Assignment', 'tutor' ), | |
| 1637 | - 'icon' => $icon, | |
| 1638 | - 'icon_class' => $icon_class, | |
| 1639 | - ) | |
| 1640 | - ); | |
| 1641 | - | |
| 1642 | - case tutor()->zoom_post_type: | |
| 1643 | - if ( ! tutor_utils()->is_addon_enabled( 'tutor-zoom' ) ) { | |
| 1644 | - return $base_items; | |
| 1645 | - } | |
| 1646 | - | |
| 1647 | - return array_merge( | |
| 1648 | - $base_items, | |
| 1649 | - array( | |
| 1650 | - 'label' => __( 'Zoom', 'tutor' ), | |
| 1651 | - 'icon' => Icon::ZOOM, | |
| 1652 | - 'is_completed' => \TUTOR_ZOOM\Zoom::is_zoom_lesson_done( '', $post->ID, $user_id ), | |
| 1653 | - ) | |
| 1654 | - ); | |
| 1655 | - | |
| 1656 | - case tutor()->meet_post_type: | |
| 1657 | - if ( ! tutor_utils()->is_addon_enabled( 'google-meet' ) ) { | |
| 1658 | - return $base_items; | |
| 1659 | - } | |
| 1660 | - | |
| 1661 | - return array_merge( | |
| 1662 | - $base_items, | |
| 1663 | - array( | |
| 1664 | - 'label' => __( 'Google Meet', 'tutor' ), | |
| 1665 | - 'icon' => Icon::GOOGLE_MEET, | |
| 1666 | - 'is_completed' => \TutorPro\GoogleMeet\Frontend\Frontend::is_lesson_completed( false, $post->ID, $user_id ), | |
| 1667 | - ) | |
| 1668 | - ); | |
| 1669 | - | |
| 1670 | - default: | |
| 1671 | - $video = tutor_utils()->get_video_info( $post->ID ); | |
| 1672 | - | |
| 1673 | - return array_merge( | |
| 1674 | - $base_items, | |
| 1675 | - array( | |
| 1676 | - 'video' => $video, | |
| 1677 | - 'video_play_time' => isset( $video->playtime ) ? $video->playtime : '', | |
| 1678 | - 'is_completed' => (bool) tutor_utils()->is_completed_lesson( $post->ID, $user_id ), | |
| 1679 | - 'label' => empty( $video ) ? __( 'Reading', 'tutor' ) : __( 'Video', 'tutor' ), | |
| 1680 | - 'icon' => empty( $video ) ? Icon::COURSES : Icon::VIDEO_CAMERA, | |
| 1681 | - ) | |
| 1682 | - ); | |
| 1683 | - } | |
| 1684 | - } | |
| 1685 | - | |
| 1686 | - /** | |
| 1687 | - * Calculate the total course duration for a set of courses and returns the total time in hours, minutes, and | |
| 1688 | - * seconds. | |
| 1689 | - * | |
| 1690 | - * @since 4.0.0 | |
| 1691 | - * | |
| 1692 | - * @param array<int> | int $course_ids List of course IDs or a single course ID. | |
| 1693 | - * | |
| 1694 | - * @return array{ | |
| 1695 | - * hours: int, | |
| 1696 | - * minutes: int, | |
| 1697 | - * seconds: int | |
| 1698 | - * } Total accumulated duration from all given courses. | |
| 1699 | - */ | |
| 1700 | - public static function get_total_course_duration( $course_ids ): array { | |
| 1701 | - $total_seconds = 0; | |
| 1702 | - $course_ids = is_array( $course_ids ) ? $course_ids : array( $course_ids ); | |
| 1703 | - foreach ( $course_ids as $id ) { | |
| 1704 | - $total_seconds += self::get_course_duration_in_seconds( $id ); | |
| 1705 | - } | |
| 1706 | - | |
| 1707 | - return DateTimeHelper::split_seconds_into_time_units( $total_seconds ); | |
| 1708 | - } | |
| 1709 | - | |
| 1710 | - /** | |
| 1711 | - * Calculate the estimated time spent on courses based on completion progress. | |
| 1712 | - * | |
| 1713 | - * @since 4.0.0 | |
| 1714 | - * | |
| 1715 | - * @param array<int> $course_ids List of course IDs. | |
| 1716 | - * | |
| 1717 | - * @return array{ | |
| 1718 | - * hours: int, | |
| 1719 | - * minutes: int, | |
| 1720 | - * seconds: int | |
| 1721 | - * } Total accumulated duration from all given courses. | |
| 1722 | - */ | |
| 1723 | - public static function get_total_estimated_time_spent( $course_ids ): array { | |
| 1724 | - $total_seconds = 0; | |
| 1725 | - foreach ( $course_ids as $id ) { | |
| 1726 | - $completion_percentage = tutor_utils()->is_completed_course( $id ) | |
| 1727 | - ? 100 | |
| 1728 | - : (int) tutor_utils()->get_course_completed_percent( (int) $id ); | |
| 1729 | - | |
| 1730 | - if ( 0 === $completion_percentage ) { | |
| 1731 | - continue; | |
| 1732 | - } | |
| 1733 | - $course_duration_in_seconds = self::get_course_duration_in_seconds( $id ); | |
| 1734 | - // Calculate the time spent by a user based on the course completion percentage. | |
| 1735 | - $total_seconds += (int) round( $course_duration_in_seconds * ( $completion_percentage / 100 ) ); | |
| 1736 | - } | |
| 1737 | - | |
| 1738 | - return DateTimeHelper::split_seconds_into_time_units( $total_seconds ); | |
| 1739 | - } | |
| 1740 | - | |
| 1741 | - /** | |
| 1742 | - * Get the total duration of a course in seconds. | |
| 1743 | - * | |
| 1744 | - * @since 4.0.0 | |
| 1745 | - * | |
| 1746 | - * @param int $course_id Course ID. | |
| 1747 | - * | |
| 1748 | - * @return int Course duration in seconds. | |
| 1749 | - */ | |
| 1750 | - public static function get_course_duration_in_seconds( int $course_id ): int { | |
| 1751 | - $duration = tutor_utils()->get_course_duration( $course_id, true ); | |
| 1752 | - | |
| 1753 | - return ( $duration['durationHours'] * HOUR_IN_SECONDS ) | |
| 1754 | - + ( $duration['durationMinutes'] * MINUTE_IN_SECONDS ) | |
| 1755 | - + ( $duration['durationSeconds'] ); | |
| 1756 | - } | |
| 1757 | - | |
| 1758 | - /** | |
| 1759 | - * Update the post_author of a course's content (topics, lessons, | |
| 1760 | - * quizzes and assignments) to a given user. | |
| 1761 | - * | |
| 1762 | - * @since 4.0.3 | |
| 1763 | - * | |
| 1764 | - * @param int $course_id course id. | |
| 1765 | - * @param int $author_id new author (user) id. | |
| 1766 | - * | |
| 1767 | - * @return bool | |
| 1768 | - */ | |
| 1769 | - public static function update_course_content_author( int $course_id, int $author_id ): bool { | |
| 1770 | - global $wpdb; | |
| 1771 | - | |
| 1772 | - $content_ids = get_posts( | |
| 1773 | - array( | |
| 1774 | - 'post_parent' => $course_id, | |
| 1775 | - 'post_type' => tutor()->topics_post_type, | |
| 1776 | - 'fields' => 'ids', | |
| 1777 | - 'posts_per_page' => -1, | |
| 1778 | - 'post_status' => 'any', | |
| 1779 | - ) | |
| 1780 | - ); | |
| 1781 | - | |
| 1782 | - $content_ids = is_array( $content_ids ) ? $content_ids : array(); | |
| 1783 | - $default_post_types = array( tutor()->lesson_post_type, tutor()->quiz_post_type ); | |
| 1784 | - $content_post_types = array_unique( apply_filters( 'tutor_course_contents_post_types', $default_post_types ) ); | |
| 1785 | - | |
| 1786 | - $primary_table = 'posts as course'; | |
| 1787 | - $topic_table = 'posts as topic'; | |
| 1788 | - $content_table = 'posts as content'; | |
| 1789 | - | |
| 1790 | - $joined_data = QueryHelper::get_joined_data( | |
| 1791 | - $primary_table, | |
| 1792 | - array( | |
| 1793 | - array( | |
| 1794 | - 'type' => 'INNER', | |
| 1795 | - 'table' => $topic_table, | |
| 1796 | - 'on' => 'course.ID = topic.post_parent', | |
| 1797 | - ), | |
| 1798 | - array( | |
| 1799 | - 'type' => 'INNER', | |
| 1800 | - 'table' => $content_table, | |
| 1801 | - 'on' => 'topic.ID = content.post_parent', | |
| 1802 | - ), | |
| 1803 | - ), | |
| 1804 | - array( 'content.ID' ), | |
| 1805 | - array( | |
| 1806 | - 'course.ID' => $course_id, | |
| 1807 | - 'content.post_type' => array( 'IN', $content_post_types ), | |
| 1808 | - ), | |
| 1809 | - array(), | |
| 1810 | - '', | |
| 1811 | - -1 | |
| 1812 | - ); | |
| 1813 | - | |
| 1814 | - $content_ids = array_merge( $content_ids, wp_list_pluck( $joined_data['results'], 'ID' ) ); | |
| 1815 | - $content_ids = array_filter( array_unique( array_map( 'absint', $content_ids ) ) ); | |
| 1816 | - | |
| 1817 | - if ( empty( $content_ids ) ) { | |
| 1818 | - return false; | |
| 1819 | - } | |
| 1820 | - | |
| 1821 | - return (bool) QueryHelper::update_where_in( | |
| 1822 | - $wpdb->posts, | |
| 1823 | - array( 'post_author' => $author_id ), | |
| 1824 | - implode( ',', $content_ids ) | |
| 1825 | - ); | |
| 1826 | 1320 | } |
| 1827 | 1321 | } |