| @@ -1,149 +1,149 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace LearnPress\AI\Assistant; | |
| 4 | - | |
| 5 | -use LearnPress\Models\CourseModel; | |
| 6 | -use LearnPress\Models\LessonPostModel; | |
| 7 | -use LearnPress\Models\UserItems\UserQuizModel; | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * Data loaders for AI Assistant tool calls. | |
| 11 | - * | |
| 12 | - * Provides grounded data from the LearnPress Models layer (no raw SQL). | |
| 13 | - * Each method corresponds to an OpenAI function-calling tool definition. | |
| 14 | - * | |
| 15 | - * @package LearnPress\AI\Assistant | |
| 16 | - * @since 4.3.5 | |
| 17 | - */ | |
| 18 | -class DataLoaders { | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Get lesson content for a given lesson ID. | |
| 22 | - * | |
| 23 | - * @param int $lesson_id | |
| 24 | - * @param int $user_id | |
| 25 | - * | |
| 26 | - * @return array{title: string, content: string}|array{error: string} | |
| 27 | - */ | |
| 28 | - public function get_lesson_content( int $lesson_id, int $user_id ): array { | |
| 29 | - $lesson = LessonPostModel::find( $lesson_id, true ); | |
| 30 | - | |
| 31 | - if ( ! $lesson ) { | |
| 32 | - return array( | |
| 33 | - 'error' => __( 'Lesson not found.', 'learnpress' ), | |
| 34 | - ); | |
| 35 | - } | |
| 36 | - | |
| 37 | - return array( | |
| 38 | - 'title' => $lesson->get_the_title(), | |
| 39 | - 'content' => $lesson->get_the_content(), | |
| 40 | - ); | |
| 41 | - } | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * Get the completed result of a specific quiz item for smart review. | |
| 45 | - * | |
| 46 | - * @param int $user_id | |
| 47 | - * @param int $course_id | |
| 48 | - * @param int $quiz_id | |
| 49 | - * | |
| 50 | - * @return array{quiz: array}|array{error: string} | |
| 51 | - */ | |
| 52 | - public function get_quiz_review_result( int $user_id, int $course_id, int $quiz_id ): array { | |
| 53 | - if ( $user_id <= 0 || $course_id <= 0 || $quiz_id <= 0 ) { | |
| 54 | - return array( | |
| 55 | - 'error' => __( 'Quiz review is unavailable for this request.', 'learnpress' ), | |
| 56 | - ); | |
| 57 | - } | |
| 58 | - | |
| 59 | - $course = CourseModel::find( $course_id, true ); | |
| 60 | - if ( ! $course ) { | |
| 61 | - return array( | |
| 62 | - 'error' => __( 'Course not found.', 'learnpress' ), | |
| 63 | - ); | |
| 64 | - } | |
| 65 | - | |
| 66 | - $quiz_items = $this->get_course_quiz_items( $course ); | |
| 67 | - $quiz_item_map = array_column( $quiz_items, null, 'id' ); | |
| 68 | - if ( ! isset( $quiz_item_map[ $quiz_id ] ) ) { | |
| 69 | - return array( | |
| 70 | - 'error' => __( 'Smart Review is only available for quizzes in this course.', 'learnpress' ), | |
| 71 | - ); | |
| 72 | - } | |
| 73 | - | |
| 74 | - $user_quiz = UserQuizModel::find_user_item( | |
| 75 | - $user_id, | |
| 76 | - $quiz_id, | |
| 77 | - LP_QUIZ_CPT, | |
| 78 | - $course_id, | |
| 79 | - LP_COURSE_CPT, | |
| 80 | - true | |
| 81 | - ); | |
| 82 | - if ( ! $user_quiz instanceof UserQuizModel ) { | |
| 83 | - return array( | |
| 84 | - 'error' => __( 'Please complete this quiz to use Smart Review.', 'learnpress' ), | |
| 85 | - ); | |
| 86 | - } | |
| 87 | - | |
| 88 | - if ( $user_quiz->get_status() !== LP_ITEM_COMPLETED ) { | |
| 89 | - return array( | |
| 90 | - 'error' => __( 'Please complete this quiz to use Smart Review.', 'learnpress' ), | |
| 91 | - ); | |
| 92 | - } | |
| 93 | - | |
| 94 | - $result = $user_quiz->get_result(); | |
| 95 | - if ( ! is_array( $result ) || empty( $result ) ) { | |
| 96 | - return array( | |
| 97 | - 'error' => __( 'Quiz result is unavailable for Smart Review.', 'learnpress' ), | |
| 98 | - ); | |
| 99 | - } | |
| 100 | - | |
| 101 | - return array( | |
| 102 | - 'quiz' => array( | |
| 103 | - 'quiz_id' => $quiz_id, | |
| 104 | - 'quiz_title' => (string) ( $quiz_item_map[ $quiz_id ]['title'] ?? get_the_title( $quiz_id ) ), | |
| 105 | - 'result' => $result, | |
| 106 | - ), | |
| 107 | - ); | |
| 108 | - } | |
| 109 | - | |
| 110 | - /** | |
| 111 | - * Collect quiz items from course outline. | |
| 112 | - * | |
| 113 | - * @param CourseModel $course | |
| 114 | - * | |
| 115 | - * @return array<int, array{id: int, title: string}> | |
| 116 | - */ | |
| 117 | - private function get_course_quiz_items( CourseModel $course ): array { | |
| 118 | - $sections_items = $course->get_section_items(); | |
| 119 | - $quiz_items = array(); | |
| 120 | - | |
| 121 | - foreach ( $sections_items as $section ) { | |
| 122 | - if ( empty( $section->items ) ) { | |
| 123 | - continue; | |
| 124 | - } | |
| 125 | - | |
| 126 | - foreach ( $section->items as $item ) { | |
| 127 | - $item_id = (int) ( $item->id ?? $item->item_id ?? 0 ); | |
| 128 | - if ( $item_id <= 0 ) { | |
| 129 | - continue; | |
| 130 | - } | |
| 131 | - | |
| 132 | - $item_type = (string) ( $item->item_type ?? $item->type ?? '' ); | |
| 133 | - if ( '' === $item_type ) { | |
| 134 | - $item_type = (string) get_post_type( $item_id ); | |
| 135 | - } | |
| 136 | - if ( $item_type !== LP_QUIZ_CPT ) { | |
| 137 | - continue; | |
| 138 | - } | |
| 139 | - | |
| 140 | - $quiz_items[ $item_id ] = array( | |
| 141 | - 'id' => $item_id, | |
| 142 | - 'title' => (string) ( $item->title ?? get_the_title( $item_id ) ), | |
| 143 | - ); | |
| 144 | - } | |
| 145 | - } | |
| 146 | - | |
| 147 | - return array_values( $quiz_items ); | |
| 148 | - } | |
| 149 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace LearnPress\AI\Assistant; | |
| 4 | + | |
| 5 | +use LearnPress\Models\CourseModel; | |
| 6 | +use LearnPress\Models\LessonPostModel; | |
| 7 | +use LearnPress\Models\UserItems\UserQuizModel; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Data loaders for AI Assistant tool calls. | |
| 11 | + * | |
| 12 | + * Provides grounded data from the LearnPress Models layer (no raw SQL). | |
| 13 | + * Each method corresponds to an OpenAI function-calling tool definition. | |
| 14 | + * | |
| 15 | + * @package LearnPress\AI\Assistant | |
| 16 | + * @since 4.3.5 | |
| 17 | + */ | |
| 18 | +class DataLoaders { | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Get lesson content for a given lesson ID. | |
| 22 | + * | |
| 23 | + * @param int $lesson_id | |
| 24 | + * @param int $user_id | |
| 25 | + * | |
| 26 | + * @return array{title: string, content: string}|array{error: string} | |
| 27 | + */ | |
| 28 | + public function get_lesson_content( int $lesson_id, int $user_id ): array { | |
| 29 | + $lesson = LessonPostModel::find( $lesson_id, true ); | |
| 30 | + | |
| 31 | + if ( ! $lesson ) { | |
| 32 | + return array( | |
| 33 | + 'error' => __( 'Lesson not found.', 'learnpress' ), | |
| 34 | + ); | |
| 35 | + } | |
| 36 | + | |
| 37 | + return array( | |
| 38 | + 'title' => $lesson->get_the_title(), | |
| 39 | + 'content' => $lesson->get_the_content(), | |
| 40 | + ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Get the completed result of a specific quiz item for smart review. | |
| 45 | + * | |
| 46 | + * @param int $user_id | |
| 47 | + * @param int $course_id | |
| 48 | + * @param int $quiz_id | |
| 49 | + * | |
| 50 | + * @return array{quiz: array}|array{error: string} | |
| 51 | + */ | |
| 52 | + public function get_quiz_review_result( int $user_id, int $course_id, int $quiz_id ): array { | |
| 53 | + if ( $user_id <= 0 || $course_id <= 0 || $quiz_id <= 0 ) { | |
| 54 | + return array( | |
| 55 | + 'error' => __( 'Quiz review is unavailable for this request.', 'learnpress' ), | |
| 56 | + ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + $course = CourseModel::find( $course_id, true ); | |
| 60 | + if ( ! $course ) { | |
| 61 | + return array( | |
| 62 | + 'error' => __( 'Course not found.', 'learnpress' ), | |
| 63 | + ); | |
| 64 | + } | |
| 65 | + | |
| 66 | + $quiz_items = $this->get_course_quiz_items( $course ); | |
| 67 | + $quiz_item_map = array_column( $quiz_items, null, 'id' ); | |
| 68 | + if ( ! isset( $quiz_item_map[ $quiz_id ] ) ) { | |
| 69 | + return array( | |
| 70 | + 'error' => __( 'Smart Review is only available for quizzes in this course.', 'learnpress' ), | |
| 71 | + ); | |
| 72 | + } | |
| 73 | + | |
| 74 | + $user_quiz = UserQuizModel::find_user_item( | |
| 75 | + $user_id, | |
| 76 | + $quiz_id, | |
| 77 | + LP_QUIZ_CPT, | |
| 78 | + $course_id, | |
| 79 | + LP_COURSE_CPT, | |
| 80 | + true | |
| 81 | + ); | |
| 82 | + if ( ! $user_quiz instanceof UserQuizModel ) { | |
| 83 | + return array( | |
| 84 | + 'error' => __( 'Please complete this quiz to use Smart Review.', 'learnpress' ), | |
| 85 | + ); | |
| 86 | + } | |
| 87 | + | |
| 88 | + if ( $user_quiz->get_status() !== LP_ITEM_COMPLETED ) { | |
| 89 | + return array( | |
| 90 | + 'error' => __( 'Please complete this quiz to use Smart Review.', 'learnpress' ), | |
| 91 | + ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + $result = $user_quiz->get_result(); | |
| 95 | + if ( ! is_array( $result ) || empty( $result ) ) { | |
| 96 | + return array( | |
| 97 | + 'error' => __( 'Quiz result is unavailable for Smart Review.', 'learnpress' ), | |
| 98 | + ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + return array( | |
| 102 | + 'quiz' => array( | |
| 103 | + 'quiz_id' => $quiz_id, | |
| 104 | + 'quiz_title' => (string) ( $quiz_item_map[ $quiz_id ]['title'] ?? get_the_title( $quiz_id ) ), | |
| 105 | + 'result' => $result, | |
| 106 | + ), | |
| 107 | + ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * Collect quiz items from course outline. | |
| 112 | + * | |
| 113 | + * @param CourseModel $course | |
| 114 | + * | |
| 115 | + * @return array<int, array{id: int, title: string}> | |
| 116 | + */ | |
| 117 | + private function get_course_quiz_items( CourseModel $course ): array { | |
| 118 | + $sections_items = $course->get_section_items(); | |
| 119 | + $quiz_items = array(); | |
| 120 | + | |
| 121 | + foreach ( $sections_items as $section ) { | |
| 122 | + if ( empty( $section->items ) ) { | |
| 123 | + continue; | |
| 124 | + } | |
| 125 | + | |
| 126 | + foreach ( $section->items as $item ) { | |
| 127 | + $item_id = (int) ( $item->id ?? $item->item_id ?? 0 ); | |
| 128 | + if ( $item_id <= 0 ) { | |
| 129 | + continue; | |
| 130 | + } | |
| 131 | + | |
| 132 | + $item_type = (string) ( $item->item_type ?? $item->type ?? '' ); | |
| 133 | + if ( '' === $item_type ) { | |
| 134 | + $item_type = (string) get_post_type( $item_id ); | |
| 135 | + } | |
| 136 | + if ( $item_type !== LP_QUIZ_CPT ) { | |
| 137 | + continue; | |
| 138 | + } | |
| 139 | + | |
| 140 | + $quiz_items[ $item_id ] = array( | |
| 141 | + 'id' => $item_id, | |
| 142 | + 'title' => (string) ( $item->title ?? get_the_title( $item_id ) ), | |
| 143 | + ); | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + return array_values( $quiz_items ); | |
| 148 | + } | |
| 149 | +} | |