Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc6b5ad3e8 | ||
|
|
aa12305e65 |
@@ -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.0).
|
||||
W Joomla wybierz **System → Install → Install from Folder** i wskaż ten katalog. Manifestem instalacyjnym jest `com_testquestions.xml` (wersja 1.43.2).
|
||||
|
||||
Po instalacji komponent jest dostępny w panelu administratora jako **Pytania testowe**.
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
-- No database changes required for version 1.43.1.
|
||||
@@ -0,0 +1 @@
|
||||
-- No database changes required for version 1.43.2.
|
||||
@@ -132,26 +132,34 @@ class ResultModel extends BaseDatabaseModel
|
||||
);
|
||||
}
|
||||
|
||||
$feedback = (new AiService())->evaluate(
|
||||
$aiResult = (new AiService())->evaluate(
|
||||
$provider,
|
||||
$apiKey,
|
||||
strip_tags((string) $question->question),
|
||||
(string) ($answer['selected'] ?? ''),
|
||||
(string) $question->answer_open
|
||||
(string) $question->answer_open,
|
||||
(int) $question->points
|
||||
);
|
||||
$item->answers[$questionId]['ai_feedback'] = $feedback;
|
||||
$item->answers[$questionId]['ai_feedback'] = $aiResult['feedback'];
|
||||
$item->answers[$questionId]['awarded_points'] = $aiResult['awarded_points'];
|
||||
|
||||
$correctionPoints = 0;
|
||||
foreach ($item->answers as $savedAnswer) {
|
||||
$correctionPoints += (int) ($savedAnswer['awarded_points'] ?? 0);
|
||||
}
|
||||
|
||||
$db = Factory::getContainer()->get('DatabaseDriver');
|
||||
$row = (object) [
|
||||
'id' => $id,
|
||||
'answers' => json_encode($item->answers, JSON_UNESCAPED_UNICODE),
|
||||
'points_after' => (int) $item->points_before + $correctionPoints,
|
||||
];
|
||||
|
||||
if (!$db->updateObject('#__testquestions_results', $row, 'id')) {
|
||||
throw new \RuntimeException('Nie udało się zapisać oceny AI.');
|
||||
}
|
||||
|
||||
return $feedback;
|
||||
return $aiResult['feedback'];
|
||||
}
|
||||
|
||||
public function evaluateAllWithAi(int $id): array
|
||||
|
||||
@@ -8,7 +8,7 @@ use Joomla\CMS\Http\HttpFactory;
|
||||
|
||||
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, int $maxPoints): array
|
||||
{
|
||||
$provider = strtolower(trim($provider));
|
||||
$provider = preg_replace('/[^a-z]/', '', $provider);
|
||||
@@ -24,11 +24,13 @@ class AiService
|
||||
throw new \RuntimeException('Nieprawidłowy dostawca AI: ' . $provider);
|
||||
}
|
||||
|
||||
$input = "Oceń odpowiedź ucznia na pytanie otwarte.\n\n"
|
||||
$input = "Oceń odpowiedź ucznia na pytanie otwarte i ustal liczbę uznanych punktów.\n"
|
||||
. "Maksymalna liczba punktów za to pytanie: " . max(0, $maxPoints) . ".\n"
|
||||
. "Przyznaj liczbę całkowitą od 0 do maksymalnej liczby punktów. Przyznaj punkty proporcjonalnie za poprawne elementy odpowiedzi; pełne punkty za komplet, część punktów za częściową odpowiedź. Nie przekraczaj limitu.\n\n"
|
||||
. "Pytanie:\n" . $question . "\n\n"
|
||||
. "Wzorzec odpowiedzi:\n" . $pattern . "\n\n"
|
||||
. "Odpowiedź ucznia:\n" . ($answer !== '' ? $answer : 'Brak odpowiedzi') . "\n\n"
|
||||
. "Napisz po polsku krótką ocenę: czy odpowiedź może zostać uznana za poprawną, jakie elementy są poprawne lub błędne oraz czy warto przyznać punkty. Nie zmieniaj wzorca odpowiedzi.";
|
||||
. "Zwróć wyłącznie poprawny obiekt JSON w formacie {\"awarded_points\": liczba_całkowita, \"feedback\": \"krótka ocena po polsku\"}. W ocenie opisz poprawne i brakujące elementy. Nie zmieniaj wzorca odpowiedzi.";
|
||||
|
||||
$payload = json_encode([
|
||||
'model' => $config['model'],
|
||||
@@ -56,7 +58,25 @@ class AiService
|
||||
throw new \RuntimeException('Brak treści odpowiedzi AI.');
|
||||
}
|
||||
|
||||
return $text;
|
||||
$jsonText = preg_replace('/^```(?:json)?\s*|\s*```$/i', '', trim($text));
|
||||
$result = json_decode($jsonText, true);
|
||||
|
||||
if (!is_array($result) || !isset($result['awarded_points']) || !is_numeric($result['awarded_points'])) {
|
||||
$start = strpos($jsonText, '{');
|
||||
$end = strrpos($jsonText, '}');
|
||||
if ($start !== false && $end !== false && $end > $start) {
|
||||
$result = json_decode(substr($jsonText, $start, $end - $start + 1), true);
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_array($result) || !isset($result['awarded_points']) || !is_numeric($result['awarded_points'])) {
|
||||
throw new \RuntimeException('AI zwróciło nieprawidłowy format oceny i punktów.');
|
||||
}
|
||||
|
||||
return [
|
||||
'awarded_points' => min(max(0, $maxPoints), max(0, (int) $result['awarded_points'])),
|
||||
'feedback' => trim((string) ($result['feedback'] ?? '')),
|
||||
];
|
||||
}
|
||||
|
||||
private function extractText(array $body): string
|
||||
|
||||
@@ -60,16 +60,18 @@
|
||||
<div class="alert alert-info mt-3"><strong><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_AI_RESULT'); ?>:</strong><br><?php echo nl2br(htmlspecialchars((string) $answer['ai_feedback'], ENT_QUOTES, 'UTF-8')); ?></div>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
<div class="row mt-3">
|
||||
<div class="col-md-3">
|
||||
<label for="awarded-points-<?php echo (int) $questionId; ?>" class="form-label"><?php echo \Joomla\CMS\Language\Text::sprintf('COM_TESTQUESTIONS_RESULT_AWARDED_POINTS', (int) ($question->points ?? 0)); ?></label>
|
||||
<input type="number" min="0" max="<?php echo (int) ($question->points ?? 0); ?>" name="awarded_points[<?php echo (int) $questionId; ?>]" id="awarded-points-<?php echo (int) $questionId; ?>" class="form-control" value="<?php echo (int) ($answer['awarded_points'] ?? 0); ?>">
|
||||
<?php if ($question->question_type === 'open') : ?>
|
||||
<div class="row mt-3">
|
||||
<div class="col-md-3">
|
||||
<label for="awarded-points-<?php echo (int) $questionId; ?>" class="form-label"><?php echo \Joomla\CMS\Language\Text::sprintf('COM_TESTQUESTIONS_RESULT_AWARDED_POINTS', (int) ($question->points ?? 0)); ?></label>
|
||||
<input type="number" min="0" max="<?php echo (int) ($question->points ?? 0); ?>" name="awarded_points[<?php echo (int) $questionId; ?>]" id="awarded-points-<?php echo (int) $questionId; ?>" class="form-control" value="<?php echo (int) ($answer['awarded_points'] ?? 0); ?>">
|
||||
</div>
|
||||
<div class="col-md-9">
|
||||
<label for="result-comment-<?php echo (int) $questionId; ?>" class="form-label"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_RESULT_COMMENT'); ?></label>
|
||||
<textarea name="comments[<?php echo (int) $questionId; ?>]" id="result-comment-<?php echo (int) $questionId; ?>" class="form-control" rows="2"><?php echo htmlspecialchars((string) ($answer['comment'] ?? ''), ENT_QUOTES, 'UTF-8'); ?></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-9">
|
||||
<label for="result-comment-<?php echo (int) $questionId; ?>" class="form-label"><?php echo \Joomla\CMS\Language\Text::_('COM_TESTQUESTIONS_RESULT_COMMENT'); ?></label>
|
||||
<textarea name="comments[<?php echo (int) $questionId; ?>]" id="result-comment-<?php echo (int) $questionId; ?>" class="form-control" rows="2"><?php echo htmlspecialchars((string) ($answer['comment'] ?? ''), ENT_QUOTES, 'UTF-8'); ?></textarea>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<creationDate>2026-10-14</creationDate>
|
||||
<author>Komponent</author>
|
||||
<license>GNU General Public License version 2 or later</license>
|
||||
<version>1.43.0</version>
|
||||
<version>1.43.2</version>
|
||||
<description>COM_TESTQUESTIONS_DESCRIPTION</description>
|
||||
<element>com_testquestions</element>
|
||||
<namespace path="src">Komponent\Component\Testquestions</namespace>
|
||||
|
||||
Reference in New Issue
Block a user