1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Controller; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Service\AI\AiProviderFactory; |
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Controller to handle AI-related functionalities. |
16
|
|
|
*/ |
17
|
|
|
#[Route('/ai')] |
18
|
|
|
class AiController |
19
|
|
|
{ |
20
|
|
|
public function __construct( |
21
|
|
|
private readonly AiProviderFactory $aiProviderFactory |
22
|
|
|
) {} |
23
|
|
|
|
24
|
|
|
#[Route('/generate_aiken', name: 'chamilo_core_ai_generate_aiken', methods: ['POST'])] |
25
|
|
|
public function generateAiken(Request $request): JsonResponse |
26
|
|
|
{ |
27
|
|
|
try { |
28
|
|
|
$data = json_decode($request->getContent(), true); |
29
|
|
|
$nQ = (int) ($data['nro_questions'] ?? 0); |
30
|
|
|
$language = (string) ($data['language'] ?? 'en'); |
31
|
|
|
$topic = trim((string) ($data['quiz_name'] ?? '')); |
32
|
|
|
$questionType = $data['question_type'] ?? 'multiple_choice'; |
33
|
|
|
$aiProvider = $data['ai_provider'] ?? null; |
34
|
|
|
|
35
|
|
|
if ($nQ <= 0 || empty($topic)) { |
36
|
|
|
return new JsonResponse([ |
37
|
|
|
'success' => false, |
38
|
|
|
'text' => 'Invalid request parameters. Ensure all fields are filled correctly.', |
39
|
|
|
], 400); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$aiService = $this->aiProviderFactory->getProvider($aiProvider); |
43
|
|
|
$questions = $aiService->generateQuestions($topic, $nQ, $questionType, $language); |
44
|
|
|
|
45
|
|
|
if (str_starts_with($questions, 'Error:')) { |
46
|
|
|
return new JsonResponse([ |
47
|
|
|
'success' => false, |
48
|
|
|
'text' => $questions, |
49
|
|
|
], 500); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return new JsonResponse([ |
53
|
|
|
'success' => true, |
54
|
|
|
'text' => trim($questions), |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
} catch (\Exception $e) { |
58
|
|
|
error_log("AI Request failed: " . $e->getMessage()); |
59
|
|
|
return new JsonResponse([ |
60
|
|
|
'success' => false, |
61
|
|
|
'text' => "An error occurred while generating questions. Please contact the administrator.", |
62
|
|
|
], 500); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
#[Route('/generate_learnpath', name: 'chamilo_core_ai_generate_learnpath', methods: ['POST'])] |
67
|
|
|
public function generateLearnPath(Request $request): JsonResponse |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
|
|
$data = json_decode($request->getContent(), true); |
71
|
|
|
$topic = trim((string) ($data['lp_name'] ?? '')); |
72
|
|
|
$chaptersCount = (int) ($data['nro_items'] ?? 5); |
73
|
|
|
$language = (string) ($data['language'] ?? 'en'); |
74
|
|
|
$wordsCount = (int) ($data['words_count'] ?? 500); |
75
|
|
|
$addTests = filter_var($data['add_tests'] ?? false, FILTER_VALIDATE_BOOLEAN); |
76
|
|
|
$numQuestions = (int) ($data['nro_questions'] ?? 0); |
77
|
|
|
$aiProvider = $data['ai_provider'] ?? null; |
78
|
|
|
|
79
|
|
|
if (empty($topic)) { |
80
|
|
|
return new JsonResponse(['success' => false, 'text' => 'Invalid parameters.'], 400); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$aiService = $this->aiProviderFactory->getProvider($aiProvider); |
84
|
|
|
$lpData = $aiService->generateLearnPath($topic, $chaptersCount, $language, $wordsCount, $addTests, $numQuestions); |
85
|
|
|
|
86
|
|
|
if (!$lpData['success']) { |
87
|
|
|
return new JsonResponse(['success' => false, 'text' => 'Failed to generate learning path.'], 500); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return new JsonResponse([ |
91
|
|
|
'success' => true, |
92
|
|
|
'data' => $lpData |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
} catch (\Exception $e) { |
96
|
|
|
error_log("ERROR: " . $e->getMessage()); |
97
|
|
|
return new JsonResponse(['success' => false, 'text' => "An error occurred."], 500); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|