Evaluate all failed open answers with AI
This commit is contained in:
@@ -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