Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
acfdf21488 | ||
|
|
3efb937469 | ||
|
|
bb1dc728d1 | ||
|
|
ed2c6b663b |
@@ -30,6 +30,6 @@ Szkielet komponentu Joomla 6 do gromadzenia pytań testowych.
|
|||||||
|
|
||||||
## Instalacja
|
## Instalacja
|
||||||
|
|
||||||
W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.42.5).
|
W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.42.9).
|
||||||
|
|
||||||
Po instalacji komponent jest dostępny w panelu administratora jako **Pytania testowe**.
|
Po instalacji komponent jest dostępny w panelu administratora jako **Pytania testowe**.
|
||||||
|
|||||||
@@ -66,6 +66,9 @@ COM_TESTQUESTIONS_AI_SETTINGS="Ustawienia AI"
|
|||||||
COM_TESTQUESTIONS_AI_PROVIDER="Dostawca AI"
|
COM_TESTQUESTIONS_AI_PROVIDER="Dostawca AI"
|
||||||
COM_TESTQUESTIONS_AI_API_KEY="Klucz API"
|
COM_TESTQUESTIONS_AI_API_KEY="Klucz API"
|
||||||
COM_TESTQUESTIONS_AI_SETTINGS_MENU="Konfiguracja AI"
|
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."
|
||||||
|
COM_TESTQUESTIONS_AI_PROVIDER_INVALID="Wybierz prawidłowego dostawcę AI."
|
||||||
COM_TESTQUESTIONS_AI_EVALUATE="AI"
|
COM_TESTQUESTIONS_AI_EVALUATE="AI"
|
||||||
COM_TESTQUESTIONS_AI_RESULT="Ocena AI"
|
COM_TESTQUESTIONS_AI_RESULT="Ocena AI"
|
||||||
COM_TESTQUESTIONS_AI_NOT_CONFIGURED="Skonfiguruj dostawcę i klucz API w ustawieniach komponentu."
|
COM_TESTQUESTIONS_AI_NOT_CONFIGURED="Skonfiguruj dostawcę i klucz API w ustawieniach komponentu."
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
-- No database changes required for version 1.42.6.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
-- No database changes required for version 1.42.7.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
-- No database changes required for version 1.42.8.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
-- No database changes required for version 1.42.9.
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Komponent\Component\Testquestions\Administrator\Controller;
|
||||||
|
|
||||||
|
defined('_JEXEC') or die;
|
||||||
|
|
||||||
|
use Joomla\CMS\Factory;
|
||||||
|
use Joomla\CMS\Language\Text;
|
||||||
|
use Joomla\CMS\MVC\Controller\BaseController;
|
||||||
|
use Joomla\CMS\Router\Route;
|
||||||
|
use Joomla\CMS\Session\Session;
|
||||||
|
|
||||||
|
class AiController extends BaseController
|
||||||
|
{
|
||||||
|
public function save(): void
|
||||||
|
{
|
||||||
|
if (!Session::checkToken()) {
|
||||||
|
throw new \RuntimeException(Text::_('JINVALID_TOKEN'), 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = Factory::getApplication()->getIdentity();
|
||||||
|
if (!$user->authorise('core.admin', 'com_testquestions') && !$user->authorise('core.options', 'com_testquestions')) {
|
||||||
|
throw new \RuntimeException(Text::_('JERROR_ALERTNOAUTHOR'), 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
$input = Factory::getApplication()->getInput();
|
||||||
|
$provider = strtolower($input->post->getCmd('ai_provider', ''));
|
||||||
|
if (!in_array($provider, ['openai', 'grok'], true)) {
|
||||||
|
throw new \RuntimeException(Text::_('COM_TESTQUESTIONS_AI_PROVIDER_INVALID'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||||
|
$row = $db->setQuery(
|
||||||
|
$db->getQuery(true)
|
||||||
|
->select([$db->quoteName('extension_id'), $db->quoteName('params')])
|
||||||
|
->from($db->quoteName('#__extensions'))
|
||||||
|
->where($db->quoteName('type') . ' = ' . $db->quote('component'))
|
||||||
|
->where($db->quoteName('element') . ' = ' . $db->quote('com_testquestions'))
|
||||||
|
)->loadObject();
|
||||||
|
|
||||||
|
if (!$row) {
|
||||||
|
throw new \RuntimeException(Text::_('COM_TESTQUESTIONS_AI_SAVE_FAILED'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = json_decode((string) $row->params, true);
|
||||||
|
$settings = is_array($settings) ? $settings : [];
|
||||||
|
$apiKey = trim((string) $input->post->get('ai_api_key', '', 'RAW'));
|
||||||
|
|
||||||
|
$settings['ai_provider'] = $provider;
|
||||||
|
if ($apiKey !== '') {
|
||||||
|
$settings['ai_api_key'] = $apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
$extension = (object) [
|
||||||
|
'extension_id' => (int) $row->extension_id,
|
||||||
|
'params' => json_encode($settings, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!$db->updateObject('#__extensions', $extension, 'extension_id')) {
|
||||||
|
throw new \RuntimeException(Text::_('COM_TESTQUESTIONS_AI_SAVE_FAILED'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setRedirect(
|
||||||
|
Route::_('index.php?option=com_testquestions&view=ai', false),
|
||||||
|
Text::_('COM_TESTQUESTIONS_AI_SETTINGS_SAVED')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cancel(): void
|
||||||
|
{
|
||||||
|
$this->setRedirect(Route::_('index.php?option=com_testquestions&view=questions', false));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -47,9 +47,12 @@ class ResultController extends BaseController
|
|||||||
try {
|
try {
|
||||||
$this->getModel('Result')->evaluateWithAi($id, $questionId);
|
$this->getModel('Result')->evaluateWithAi($id, $questionId);
|
||||||
} catch (\Throwable $error) {
|
} catch (\Throwable $error) {
|
||||||
$message = $error->getMessage() === 'AI_NOT_CONFIGURED'
|
$errorMessage = $error->getMessage();
|
||||||
? Text::_('COM_TESTQUESTIONS_AI_NOT_CONFIGURED')
|
$message = str_starts_with($errorMessage, 'AI_KEY_EMPTY|')
|
||||||
: Text::sprintf('COM_TESTQUESTIONS_AI_ERROR', $error->getMessage());
|
? 'Diagnostyka konfiguracji AI (1.42.8): ' . substr($errorMessage, strlen('AI_KEY_EMPTY|'))
|
||||||
|
: ($errorMessage === 'AI_NOT_CONFIGURED'
|
||||||
|
? Text::_('COM_TESTQUESTIONS_AI_NOT_CONFIGURED')
|
||||||
|
: Text::sprintf('COM_TESTQUESTIONS_AI_ERROR', $errorMessage));
|
||||||
$type = 'error';
|
$type = 'error';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,24 +4,22 @@ namespace Komponent\Component\Testquestions\Administrator\Field;
|
|||||||
|
|
||||||
defined('_JEXEC') or die;
|
defined('_JEXEC') or die;
|
||||||
|
|
||||||
use Joomla\CMS\Form\FormField;
|
use Joomla\CMS\Form\Field\PasswordField;
|
||||||
|
|
||||||
class AiapikeyField extends FormField
|
class AiapikeyField extends PasswordField
|
||||||
{
|
{
|
||||||
protected $type = 'Aiapikey';
|
protected $type = 'Aiapikey';
|
||||||
|
|
||||||
protected function getInput(): string
|
protected function getInput(): string
|
||||||
{
|
{
|
||||||
$id = htmlspecialchars($this->id, ENT_QUOTES, 'UTF-8');
|
$id = htmlspecialchars($this->id, ENT_QUOTES, 'UTF-8');
|
||||||
$name = htmlspecialchars($this->name, ENT_QUOTES, 'UTF-8');
|
|
||||||
$value = htmlspecialchars((string) $this->value, ENT_QUOTES, 'UTF-8');
|
|
||||||
$length = mb_strlen((string) $this->value);
|
$length = mb_strlen((string) $this->value);
|
||||||
|
|
||||||
return '<div class="input-group">'
|
return '<div class="input-group">'
|
||||||
. '<input type="password" id="' . $id . '" name="' . $name . '" value="' . $value . '" size="120" autocomplete="off" class="form-control">'
|
. parent::getInput()
|
||||||
. '<button type="button" class="btn btn-outline-secondary" data-ai-key-toggle="' . $id . '">Pokaż</button>'
|
. '<button type="button" class="btn btn-outline-secondary" data-ai-key-toggle="' . $id . '">Pokaż</button>'
|
||||||
. '</div>'
|
. '</div>'
|
||||||
. '<small class="form-text text-muted">Zapisany klucz: ' . (int) $length . ' znaków.</small>'
|
. '<small class="form-text text-muted">Klucz: ' . (int) $length . ' znaków.</small>'
|
||||||
. '<script>
|
. '<script>
|
||||||
document.addEventListener("click", function (event) {
|
document.addEventListener("click", function (event) {
|
||||||
var button = event.target.closest("[data-ai-key-toggle]");
|
var button = event.target.closest("[data-ai-key-toggle]");
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ class ResultModel extends BaseDatabaseModel
|
|||||||
$params = ComponentHelper::getParams('com_testquestions');
|
$params = ComponentHelper::getParams('com_testquestions');
|
||||||
$provider = trim((string) $params->get('ai_provider', 'openai'));
|
$provider = trim((string) $params->get('ai_provider', 'openai'));
|
||||||
$apiKey = trim((string) $params->get('ai_api_key', ''));
|
$apiKey = trim((string) $params->get('ai_api_key', ''));
|
||||||
|
$componentKeyLength = mb_strlen($apiKey);
|
||||||
|
|
||||||
// Odczytaj zapisane parametry bezpośrednio z bazy. Dzięki temu nie użyjemy
|
// Odczytaj zapisane parametry bezpośrednio z bazy. Dzięki temu nie użyjemy
|
||||||
// nieaktualnej wartości z pamięci ComponentHelper, a maska pola hasła
|
// nieaktualnej wartości z pamięci ComponentHelper, a maska pola hasła
|
||||||
@@ -120,6 +121,17 @@ class ResultModel extends BaseDatabaseModel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (trim($apiKey) === '') {
|
||||||
|
$databaseKey = is_array($storedParams) ? trim((string) ($storedParams['ai_api_key'] ?? '')) : '';
|
||||||
|
$databaseKeyLength = preg_match('/^\*+$/', $databaseKey) ? 0 : mb_strlen($databaseKey);
|
||||||
|
throw new \RuntimeException(
|
||||||
|
'AI_KEY_EMPTY|provider=' . ($provider !== '' ? $provider : '(pusty)')
|
||||||
|
. '|component_chars=' . $componentKeyLength
|
||||||
|
. '|database_chars=' . $databaseKeyLength
|
||||||
|
. '|database_params=' . (is_array($storedParams) ? 'json' : 'unreadable')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
$feedback = (new AiService())->evaluate(
|
$feedback = (new AiService())->evaluate(
|
||||||
$provider,
|
$provider,
|
||||||
$apiKey,
|
$apiKey,
|
||||||
|
|||||||
@@ -11,14 +11,19 @@ class AiService
|
|||||||
public function evaluate(string $provider, string $apiKey, string $question, string $answer, string $pattern): string
|
public function evaluate(string $provider, string $apiKey, string $question, string $answer, string $pattern): string
|
||||||
{
|
{
|
||||||
$provider = strtolower(trim($provider));
|
$provider = strtolower(trim($provider));
|
||||||
|
$provider = preg_replace('/[^a-z]/', '', $provider);
|
||||||
$config = $provider === 'grok'
|
$config = $provider === 'grok'
|
||||||
? ['url' => 'https://api.x.ai/v1/responses', 'model' => 'grok-4.7']
|
? ['url' => 'https://api.x.ai/v1/responses', 'model' => 'grok-4.7']
|
||||||
: ['url' => 'https://api.openai.com/v1/responses', 'model' => 'gpt-4.1-mini'];
|
: ['url' => 'https://api.openai.com/v1/responses', 'model' => 'gpt-4.1-mini'];
|
||||||
|
|
||||||
if (!in_array($provider, ['openai', 'grok'], true) || trim($apiKey) === '') {
|
if (trim($apiKey) === '') {
|
||||||
throw new \RuntimeException('AI_NOT_CONFIGURED');
|
throw new \RuntimeException('AI_NOT_CONFIGURED');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!in_array($provider, ['openai', 'grok'], true)) {
|
||||||
|
throw new \RuntimeException('Nieprawidłowy dostawca AI: ' . $provider);
|
||||||
|
}
|
||||||
|
|
||||||
$input = "Oceń odpowiedź ucznia na pytanie otwarte.\n\n"
|
$input = "Oceń odpowiedź ucznia na pytanie otwarte.\n\n"
|
||||||
. "Pytanie:\n" . $question . "\n\n"
|
. "Pytanie:\n" . $question . "\n\n"
|
||||||
. "Wzorzec odpowiedzi:\n" . $pattern . "\n\n"
|
. "Wzorzec odpowiedzi:\n" . $pattern . "\n\n"
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Komponent\Component\Testquestions\Administrator\View\Ai;
|
||||||
|
|
||||||
|
defined('_JEXEC') or die;
|
||||||
|
|
||||||
|
use Joomla\CMS\Factory;
|
||||||
|
use Joomla\CMS\Language\Text;
|
||||||
|
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
|
||||||
|
use Joomla\CMS\Toolbar\ToolbarHelper;
|
||||||
|
|
||||||
|
class HtmlView extends BaseHtmlView
|
||||||
|
{
|
||||||
|
protected array $settings = [];
|
||||||
|
|
||||||
|
public function display($tpl = null)
|
||||||
|
{
|
||||||
|
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||||
|
$raw = (string) $db->setQuery(
|
||||||
|
$db->getQuery(true)
|
||||||
|
->select($db->quoteName('params'))
|
||||||
|
->from($db->quoteName('#__extensions'))
|
||||||
|
->where($db->quoteName('type') . ' = ' . $db->quote('component'))
|
||||||
|
->where($db->quoteName('element') . ' = ' . $db->quote('com_testquestions'))
|
||||||
|
)->loadResult();
|
||||||
|
$this->settings = json_decode($raw, true) ?: [];
|
||||||
|
|
||||||
|
ToolbarHelper::title(Text::_('COM_TESTQUESTIONS_AI_SETTINGS'));
|
||||||
|
ToolbarHelper::link('index.php?option=com_testquestions&view=questions', 'COM_TESTQUESTIONS_QUESTIONS', 'arrow-left');
|
||||||
|
|
||||||
|
return parent::display($tpl);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -65,7 +65,7 @@ class HtmlView extends BaseHtmlView
|
|||||||
);
|
);
|
||||||
|
|
||||||
ToolbarHelper::link(
|
ToolbarHelper::link(
|
||||||
'index.php?option=com_config&view=component&component=com_testquestions',
|
'index.php?option=com_testquestions&view=ai',
|
||||||
'COM_TESTQUESTIONS_AI_SETTINGS_MENU',
|
'COM_TESTQUESTIONS_AI_SETTINGS_MENU',
|
||||||
'cog'
|
'cog'
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
<?php defined('_JEXEC') or die; ?>
|
||||||
|
<?php use Joomla\CMS\HTML\HTMLHelper; ?>
|
||||||
|
<?php use Joomla\CMS\Router\Route; ?>
|
||||||
|
<?php $provider = (string) ($this->settings['ai_provider'] ?? 'openai'); ?>
|
||||||
|
<?php $apiKey = (string) ($this->settings['ai_api_key'] ?? ''); ?>
|
||||||
|
<form action="<?php echo Route::_('index.php?option=com_testquestions&view=ai'); ?>" method="post" name="adminForm" id="adminForm">
|
||||||
|
<div class="card" style="max-width: 900px;">
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="ai-provider" class="form-label"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_AI_PROVIDER'); ?></label>
|
||||||
|
<select id="ai-provider" name="ai_provider" class="form-select" required>
|
||||||
|
<option value="openai" <?php echo $provider === 'openai' ? 'selected' : ''; ?>>OpenAI</option>
|
||||||
|
<option value="grok" <?php echo $provider === 'grok' ? 'selected' : ''; ?>>Grok</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="ai-api-key" class="form-label"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_AI_API_KEY'); ?></label>
|
||||||
|
<div class="input-group">
|
||||||
|
<input type="password" id="ai-api-key" name="ai_api_key" value="<?php echo htmlspecialchars($apiKey, ENT_QUOTES, 'UTF-8'); ?>" class="form-control" size="120" maxlength="4096" autocomplete="new-password">
|
||||||
|
<button type="button" class="btn btn-outline-secondary" id="ai-key-toggle">Pokaż</button>
|
||||||
|
</div>
|
||||||
|
<small class="form-text text-muted">
|
||||||
|
<?php echo $apiKey !== '' ? 'Zapisany klucz: ' . mb_strlen($apiKey) . ' znaków. Pozostaw pole bez zmian, aby zachować klucz.' : 'Nie zapisano jeszcze klucza API.'; ?>
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary" onclick="this.form.task.value='ai.save';">
|
||||||
|
<?php echo \Joomla\CMS\Language\Text::_('JTOOLBAR_SAVE'); ?>
|
||||||
|
</button>
|
||||||
|
<a class="btn btn-secondary" href="<?php echo Route::_('index.php?option=com_testquestions&view=questions'); ?>">
|
||||||
|
<?php echo \Joomla\CMS\Language\Text::_('JCANCEL'); ?>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<input type="hidden" name="task" value="ai.save">
|
||||||
|
<?php echo HTMLHelper::_('form.token'); ?>
|
||||||
|
</form>
|
||||||
|
<script>
|
||||||
|
document.getElementById('ai-key-toggle')?.addEventListener('click', function () {
|
||||||
|
const input = document.getElementById('ai-api-key');
|
||||||
|
const visible = input.type === 'text';
|
||||||
|
input.type = visible ? 'password' : 'text';
|
||||||
|
this.textContent = visible ? 'Pokaż' : 'Ukryj';
|
||||||
|
});
|
||||||
|
</script>
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
<creationDate>2026-10-14</creationDate>
|
<creationDate>2026-10-14</creationDate>
|
||||||
<author>Komponent</author>
|
<author>Komponent</author>
|
||||||
<license>GNU General Public License version 2 or later</license>
|
<license>GNU General Public License version 2 or later</license>
|
||||||
<version>1.42.5</version>
|
<version>1.42.9</version>
|
||||||
<description>COM_TESTQUESTIONS_DESCRIPTION</description>
|
<description>COM_TESTQUESTIONS_DESCRIPTION</description>
|
||||||
<element>com_testquestions</element>
|
<element>com_testquestions</element>
|
||||||
<namespace path="src">Komponent\Component\Testquestions</namespace>
|
<namespace path="src">Komponent\Component\Testquestions</namespace>
|
||||||
|
|||||||
Reference in New Issue
Block a user