Evaluate all failed open answers with AI
This commit is contained in:
@@ -63,6 +63,47 @@ class ResultController extends BaseController
|
||||
);
|
||||
}
|
||||
|
||||
public function aiAll(): void
|
||||
{
|
||||
if (!Session::checkToken()) {
|
||||
throw new \RuntimeException(Text::_('JINVALID_TOKEN'), 403);
|
||||
}
|
||||
|
||||
$input = Factory::getApplication()->getInput();
|
||||
$id = $input->post->getInt('id');
|
||||
$message = '';
|
||||
$type = 'message';
|
||||
|
||||
try {
|
||||
$summary = $this->getModel('Result')->evaluateAllWithAi($id);
|
||||
if ($summary['failed'] > 0) {
|
||||
$message = Text::sprintf(
|
||||
'COM_TESTQUESTIONS_AI_BULK_PARTIAL',
|
||||
$summary['evaluated'],
|
||||
$summary['failed'],
|
||||
$summary['first_error']
|
||||
);
|
||||
$type = 'warning';
|
||||
} else {
|
||||
$message = Text::sprintf('COM_TESTQUESTIONS_AI_BULK_DONE', $summary['evaluated']);
|
||||
}
|
||||
} catch (\Throwable $error) {
|
||||
$errorMessage = $error->getMessage();
|
||||
$message = $errorMessage === 'AI_NO_OPEN_QUESTIONS'
|
||||
? Text::_('COM_TESTQUESTIONS_AI_NO_OPEN_QUESTIONS')
|
||||
: ($errorMessage === 'AI_NOT_CONFIGURED'
|
||||
? Text::_('COM_TESTQUESTIONS_AI_NOT_CONFIGURED')
|
||||
: Text::sprintf('COM_TESTQUESTIONS_AI_ERROR', $errorMessage));
|
||||
$type = 'error';
|
||||
}
|
||||
|
||||
$this->setRedirect(
|
||||
Route::_('index.php?option=com_testquestions&view=result&id=' . $id, false),
|
||||
$message,
|
||||
$type
|
||||
);
|
||||
}
|
||||
|
||||
private function saveAssessment(): void
|
||||
{
|
||||
if (!Session::checkToken()) {
|
||||
|
||||
@@ -153,4 +153,51 @@ class ResultModel extends BaseDatabaseModel
|
||||
|
||||
return $feedback;
|
||||
}
|
||||
|
||||
public function evaluateAllWithAi(int $id): array
|
||||
{
|
||||
$item = $this->getItem($id);
|
||||
|
||||
if (!$item) {
|
||||
throw new \RuntimeException('Nie znaleziono zapisanego testu.');
|
||||
}
|
||||
|
||||
$questionIds = [];
|
||||
foreach ($item->answers as $questionId => $answer) {
|
||||
$question = $item->questions[(int) $questionId] ?? null;
|
||||
if ($question && $question->question_type === 'open' && empty($answer['correct'])) {
|
||||
$questionIds[] = (int) $questionId;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$questionIds) {
|
||||
throw new \RuntimeException('AI_NO_OPEN_QUESTIONS');
|
||||
}
|
||||
|
||||
$evaluated = 0;
|
||||
$failed = 0;
|
||||
$firstError = '';
|
||||
|
||||
foreach ($questionIds as $questionId) {
|
||||
try {
|
||||
$this->evaluateWithAi($id, $questionId);
|
||||
$evaluated++;
|
||||
} catch (\Throwable $error) {
|
||||
$failed++;
|
||||
$firstError = $firstError ?: $error->getMessage();
|
||||
|
||||
if (in_array($error->getMessage(), ['AI_NOT_CONFIGURED'], true)
|
||||
|| str_starts_with($error->getMessage(), 'AI_KEY_EMPTY|')) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'evaluated' => $evaluated,
|
||||
'failed' => $failed,
|
||||
'first_error' => $firstError,
|
||||
'total' => count($questionIds),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user