| @@ -507,29 +507,46 @@ | ||
| 507 | 507 | |
| 508 | 508 | /** |
| 509 | 509 | * Get section id of item |
| 510 | 510 | * |
| 511 | - * @param int $item_id | |
| 511 | + * A curriculum item's identity is the (item_id, item_type) pair, not the numeric | |
| 512 | + * ID alone: once items live in per-type tables a lesson and a quiz can share the | |
| 513 | + * same ID. Pass $item_type to match the composite identity. | |
| 512 | 514 | * |
| 515 | + * @param int $item_id | |
| 516 | + * @param string $item_type Optional. Curriculum item type to match as well. Empty | |
| 517 | + * keeps the legacy ID-only match for callers not yet migrated. | |
| 518 | + * | |
| 513 | 519 | * @return int |
| 514 | 520 | * @since 4.2.8 |
| 515 | - * @version 1.0.0 | |
| 521 | + * @version 1.1.0 | |
| 516 | 522 | */ |
| 517 | - public function get_section_of_item( int $item_id ): int { | |
| 518 | - $section_id = 0; | |
| 523 | + public function get_section_of_item( int $item_id, string $item_type = '' ): int { | |
| 524 | + $section_items = $this->get_section_items(); | |
| 519 | 525 | |
| 520 | - $section_items = $this->get_section_items(); | |
| 521 | 526 | foreach ( $section_items as $section ) { |
| 527 | + if ( empty( $section->items ) ) { | |
| 528 | + continue; | |
| 529 | + } | |
| 530 | + | |
| 522 | 531 | foreach ( $section->items as $item ) { |
| 523 | 532 | $item_id_check = (int) ( $item->item_id ?? $item->id ?? 0 ); |
| 524 | - if ( $item_id_check === $item_id ) { | |
| 525 | - $section_id = $section->section_id ?? $section->id ?? 0; | |
| 526 | - break; | |
| 533 | + if ( $item_id_check !== $item_id ) { | |
| 534 | + continue; | |
| 527 | 535 | } |
| 536 | + | |
| 537 | + if ( '' !== $item_type ) { | |
| 538 | + $item_type_check = (string) ( $item->item_type ?? $item->type ?? '' ); | |
| 539 | + if ( $item_type_check !== $item_type ) { | |
| 540 | + continue; | |
| 541 | + } | |
| 542 | + } | |
| 543 | + | |
| 544 | + return (int) ( $section->section_id ?? $section->id ?? 0 ); | |
| 528 | 545 | } |
| 529 | 546 | } |
| 530 | 547 | |
| 531 | - return (int) $section_id; | |
| 548 | + return 0; | |
| 532 | 549 | } |
| 533 | 550 | |
| 534 | 551 | /** |
| 535 | 552 | * Get course Evaluation type. |
| @@ -548,9 +565,9 @@ | ||
| 548 | 565 | * @param string $type |
| 549 | 566 | * |
| 550 | 567 | * @return array |
| 551 | 568 | * @since 4.3.2.6 |
| 552 | - * @version 1.0.0 | |
| 569 | + * @version 1.0.1 | |
| 553 | 570 | */ |
| 554 | 571 | public static function get_evaluation_types( string $type = '' ): array { |
| 555 | 572 | if ( has_filter( 'learnpress/course-evaluation/methods' ) ) { |
| 556 | 573 | $methods = apply_filters( 'learnpress/course-evaluation/methods', [], 0 ); |
| @@ -584,9 +601,12 @@ | ||
| 584 | 601 | |
| 585 | 602 | if ( empty( $type ) ) { |
| 586 | 603 | return $types; |
| 587 | 604 | } elseif ( empty( $types[ $type ] ) && ! empty( $methods[ $type ] ) ) { |
| 588 | - return $methods[ $type ]; | |
| 605 | + $types[ $type ] = [ | |
| 606 | + 'label' => strip_tags( $methods[ $type ] ), | |
| 607 | + 'tip' => strip_tags( $methods[ $type ] ), | |
| 608 | + ]; | |
| 589 | 609 | } |
| 590 | 610 | |
| 591 | 611 | return $types[ $type ] ?? []; |
| 592 | 612 | } |
| @@ -852,9 +872,9 @@ | ||
| 852 | 872 | $coursePost = new CoursePostModel( $this ); |
| 853 | 873 | $value = $coursePost->get_meta_value_by_key( $key, $default_value, $single ); |
| 854 | 874 | } |
| 855 | 875 | |
| 856 | - $value = maybe_unserialize( $value ); | |
| 876 | + $value = maybe_unserialize( $value ); | |
| 857 | 877 | $this->meta_data->{$key} = $value; |
| 858 | 878 | |
| 859 | 879 | return $value; |
| 860 | 880 | } |
| @@ -1256,15 +1276,26 @@ | ||
| 1256 | 1276 | |
| 1257 | 1277 | /** |
| 1258 | 1278 | * Get item model assigned to this course |
| 1259 | 1279 | * |
| 1280 | + * @param int $item_id | |
| 1281 | + * @param string $item_type | |
| 1282 | + * @param bool $check_assign | default true check assign item to course | |
| 1283 | + * | |
| 1260 | 1284 | * @return mixed|false|null|WP_Post|PostModel |
| 1261 | 1285 | * @since v4.2.7.6 |
| 1262 | - * @version 1.0.1 | |
| 1286 | + * @version 1.0.3 | |
| 1263 | 1287 | */ |
| 1264 | - public function get_item_model( int $item_id, string $item_type ) { | |
| 1288 | + public function get_item_model( int $item_id, string $item_type, bool $check_assign = true ) { | |
| 1265 | 1289 | try { |
| 1266 | 1290 | $item = false; |
| 1291 | + | |
| 1292 | + // Find item has in section. Match the composite (item_id, item_type) identity, | |
| 1293 | + // so a quiz ID can never resolve through a lesson request and vice versa. | |
| 1294 | + $section_id = $this->get_section_of_item( $item_id, $item_type ); | |
| 1295 | + if ( $check_assign && ! $section_id ) { | |
| 1296 | + return $item; | |
| 1297 | + } | |
| 1267 | 1298 | |
| 1268 | 1299 | switch ( $item_type ) { |
| 1269 | 1300 | case LP_LESSON_CPT: |
| 1270 | 1301 | $item = LessonPostModel::find( $item_id, true ); |