PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.2
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / Ajax / AI / AIAssistantAjax.php

AIAssistantAjax.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.2, at inc/Ajax/AI/AIAssistantAjax.php

98 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LearnPress\Ajax\AI;
4
5 use Exception;
6 use LearnPress\Ajax\AbstractAjax;
7 use LearnPress\AI\Assistant\AIAssistantController;
8 use LP_Helper;
9 use LP_Request;
10 use LP_REST_Response;
11 use Throwable;
12
13 /**
14 * Class AIAssistantAjax
15 *
16 * Handles student-facing AJAX requests for the LP AI Assistant.
17 *
18 * Intentionally separate from OpenAiAjax — every method in OpenAiAjax
19 * enforces ROLE_ADMINISTRATOR || ROLE_INSTRUCTOR. Mixing student-facing
20 * methods into the same class creates conflicting permission models.
21 *
22 * Transport: lp-load-ajax (same AbstractAjax mechanic as all other LP AJAX classes).
23 *
24 * @package LearnPress\Ajax\AI
25 * @since 4.3.5
26 */
27 class AIAssistantAjax extends AbstractAjax {
28
29 /**
30 * Handle assistant chat request from a logged-in learner.
31 *
32 * Request data (JSON-encoded in 'data' param):
33 * {
34 * "message": string,
35 * "lesson_id": int,
36 * "course_id": int,
37 * "history": [{role, content}, ...],
38 * "active_quiz_questions": []
39 * }
40 *
41 * Response shape:
42 * { "status": "success", "message": "", "data": { "type": "text|quiz", "message": "...", "quiz": {} } }
43 */
44 public function openai_assistant_chat() {
45 $response = new LP_REST_Response();
46
47 try {
48 if ( ! is_user_logged_in() ) {
49 throw new Exception( __( 'You must be logged in to use the AI Assistant.', 'learnpress' ) );
50 }
51
52 if ( ! AIAssistantController::is_enabled() ) {
53 throw new Exception( __( 'AI Assistant is not available.', 'learnpress' ) );
54 }
55
56 $data_str = LP_Request::get_param( 'data' );
57 $data = LP_Helper::json_decode( $data_str, true );
58
59 if ( ! is_array( $data ) ) {
60 throw new Exception( __( 'Invalid request data.', 'learnpress' ) );
61 }
62
63 $controller = new AIAssistantController();
64 $result = $controller->handle_chat( $data );
65 $result = $this->normalize_response_data( $result );
66
67 $response->status = 'success';
68 $response->data = $result;
69 } catch ( Throwable $e ) {
70 $response->message = $e->getMessage();
71 $response->data = $this->normalize_response_data( array() );
72 }
73
74 wp_send_json( $response );
75 }
76
77 /**
78 * Normalize controller result to required frontend contract.
79 *
80 * @param array $result
81 *
82 * @return array{type:string,message:string,quiz:array|null}
83 */
84 private function normalize_response_data( array $result ): array {
85 $type = $result['type'] ?? 'text';
86
87 if ( ! in_array( $type, array( 'text', 'quiz' ), true ) ) {
88 $type = 'text';
89 }
90
91 return array(
92 'type' => $type,
93 'message' => isset( $result['message'] ) ? (string) $result['message'] : '',
94 'quiz' => is_array( $result['quiz'] ?? null ) ? $result['quiz'] : null,
95 );
96 }
97 }
98