diff --git a/README.md b/README.md index 585ee96..dce16fe 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,6 @@ Szkielet komponentu Joomla 6 do gromadzenia pytań testowych. ## Instalacja -W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.43.2). +W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.43.3). Po instalacji komponent jest dostępny w panelu administratora jako **Pytania testowe**. diff --git a/administrator/components/com_testquestions/language/en-GB/com_testquestions.ini b/administrator/components/com_testquestions/language/en-GB/com_testquestions.ini index 55409c2..73d7144 100644 --- a/administrator/components/com_testquestions/language/en-GB/com_testquestions.ini +++ b/administrator/components/com_testquestions/language/en-GB/com_testquestions.ini @@ -65,6 +65,8 @@ COM_TESTQUESTIONS_SET_VARIANT_DURATION_INFO="Czas testu jest pobierany z zestawu COM_TESTQUESTIONS_AI_SETTINGS="Ustawienia AI" COM_TESTQUESTIONS_AI_PROVIDER="Dostawca AI" COM_TESTQUESTIONS_AI_API_KEY="Klucz API" +COM_TESTQUESTIONS_AI_OPENAI_KEY="Klucz API OpenAI" +COM_TESTQUESTIONS_AI_GROK_KEY="Klucz API Grok" COM_TESTQUESTIONS_AI_SETTINGS_MENU="Konfiguracja AI" COM_TESTQUESTIONS_AI_SETTINGS_SAVED="Ustawienia AI zapisano." COM_TESTQUESTIONS_AI_SAVE_FAILED="Nie udało się zapisać ustawień AI." diff --git a/administrator/components/com_testquestions/sql/updates/mysql/1.43.3.sql b/administrator/components/com_testquestions/sql/updates/mysql/1.43.3.sql new file mode 100644 index 0000000..9c3a546 --- /dev/null +++ b/administrator/components/com_testquestions/sql/updates/mysql/1.43.3.sql @@ -0,0 +1 @@ +-- No database changes required for version 1.43.3. diff --git a/administrator/components/com_testquestions/src/Controller/AiController.php b/administrator/components/com_testquestions/src/Controller/AiController.php index 4c5cfe4..265442f 100644 --- a/administrator/components/com_testquestions/src/Controller/AiController.php +++ b/administrator/components/com_testquestions/src/Controller/AiController.php @@ -44,12 +44,22 @@ class AiController extends BaseController $settings = json_decode((string) $row->params, true); $settings = is_array($settings) ? $settings : []; - $apiKey = trim((string) $input->post->get('ai_api_key', '', 'RAW')); + $legacyKey = trim((string) ($settings['ai_api_key'] ?? '')); $settings['ai_provider'] = $provider; - if ($apiKey !== '') { - $settings['ai_api_key'] = $apiKey; + foreach (['openai', 'grok'] as $keyProvider) { + $fieldName = 'ai_' . $keyProvider . '_api_key'; + $apiKey = trim((string) $input->post->get($fieldName, '', 'RAW')); + + if ($apiKey !== '') { + $settings[$fieldName] = $apiKey; + } + + if ($keyProvider === $provider && empty($settings[$fieldName]) && $legacyKey !== '') { + $settings[$fieldName] = $legacyKey; + } } + unset($settings['ai_api_key']); $extension = (object) [ 'extension_id' => (int) $row->extension_id, diff --git a/administrator/components/com_testquestions/src/Model/ResultModel.php b/administrator/components/com_testquestions/src/Model/ResultModel.php index 6b26278..ba4abbe 100644 --- a/administrator/components/com_testquestions/src/Model/ResultModel.php +++ b/administrator/components/com_testquestions/src/Model/ResultModel.php @@ -92,7 +92,9 @@ class ResultModel extends BaseDatabaseModel $params = ComponentHelper::getParams('com_testquestions'); $provider = trim((string) $params->get('ai_provider', 'openai')); - $apiKey = trim((string) $params->get('ai_api_key', '')); + $provider = strtolower(preg_replace('/[^a-z]/', '', $provider)); + $keyName = 'ai_' . $provider . '_api_key'; + $apiKey = trim((string) $params->get($keyName, $params->get('ai_api_key', ''))); $componentKeyLength = mb_strlen($apiKey); // Odczytaj zapisane parametry bezpośrednio z bazy. Dzięki temu nie użyjemy @@ -115,14 +117,21 @@ class ResultModel extends BaseDatabaseModel if ($storedProvider !== '') { $provider = $storedProvider; } + } - if ($storedApiKey !== '' && !preg_match('/^\*+$/', $storedApiKey)) { - $apiKey = $storedApiKey; - } + $provider = strtolower(preg_replace('/[^a-z]/', '', $provider)); + $keyName = 'ai_' . $provider . '_api_key'; + $apiKey = trim((string) $params->get($keyName, $params->get('ai_api_key', ''))); + $componentKeyLength = mb_strlen($apiKey); + $storedApiKey = is_array($storedParams) + ? trim((string) ($storedParams[$keyName] ?? $storedParams['ai_api_key'] ?? '')) + : ''; + if ($storedApiKey !== '' && !preg_match('/^\*+$/', $storedApiKey)) { + $apiKey = $storedApiKey; } if (trim($apiKey) === '') { - $databaseKey = is_array($storedParams) ? trim((string) ($storedParams['ai_api_key'] ?? '')) : ''; + $databaseKey = $storedApiKey; $databaseKeyLength = preg_match('/^\*+$/', $databaseKey) ? 0 : mb_strlen($databaseKey); throw new \RuntimeException( 'AI_KEY_EMPTY|provider=' . ($provider !== '' ? $provider : '(pusty)') diff --git a/administrator/components/com_testquestions/tmpl/ai/default.php b/administrator/components/com_testquestions/tmpl/ai/default.php index ec1b589..ee16a4f 100644 --- a/administrator/components/com_testquestions/tmpl/ai/default.php +++ b/administrator/components/com_testquestions/tmpl/ai/default.php @@ -2,7 +2,9 @@ settings['ai_provider'] ?? 'openai'); ?> -settings['ai_api_key'] ?? ''); ?> +settings['ai_api_key'] ?? ''); ?> +settings['ai_openai_api_key'] ?? ($provider === 'openai' ? $legacyKey : '')); ?> +settings['ai_grok_api_key'] ?? ($provider === 'grok' ? $legacyKey : '')); ?>