Files
awo-hamburg-intranet/module/tasks/endpoints/user/submit_quiz.php

58 lines
1.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
require_once dirname(dirname(__DIR__)) . '/bootstrap.php';
if (!TaskCertRequest::isPost()) {
TaskCertResponse::error('Methode nicht erlaubt', 405);
}
$auth = TaskCertAuthContext::fromGlobals();
if (!$auth->isLoggedIn()) {
TaskCertResponse::error('Nicht eingeloggt', 401);
}
TaskCertCsrf::assertValidFromRequest(true);
$assignmentId = TaskCertRequest::postInt('assignment_id', 0);
$methodId = TaskCertRequest::postInt('method_id', 0);
$answersRaw = TaskCertRequest::postArray('answers');
if ($assignmentId <= 0 || $methodId <= 0) {
TaskCertResponse::error('Ungültige assignment_id oder method_id', 400);
}
$answers = [];
foreach ($answersRaw as $questionId => $questionAnswers) {
$questionIdInt = (int) $questionId;
if ($questionIdInt <= 0) {
continue;
}
if (!is_array($questionAnswers)) {
$questionAnswers = [$questionAnswers];
}
$answers[$questionIdInt] = array_values(array_unique(array_map('intval', $questionAnswers)));
}
try {
$services = task_cert_services();
$submissionId = $services['completionService']->submitQuiz($assignmentId, $methodId, $answers);
$redirectTo = TaskCertRequest::postString('redirect_to', '');
if ($redirectTo !== '') {
TaskCertResponse::redirect($redirectTo);
}
TaskCertResponse::json([
'success' => true,
'submission_id' => $submissionId,
]);
} catch (Throwable $throwable) {
TaskCertResponse::json([
'success' => false,
'error' => $throwable->getMessage(),
], 400);
}