93 lines
3.4 KiB
PHP
93 lines
3.4 KiB
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
require_once dirname(dirname(__DIR__)) . '/bootstrap.php';
|
||
|
|
|
||
|
|
if (!function_exists('task_cert_submit_read_redirect_with_message')) {
|
||
|
|
function task_cert_submit_read_redirect_with_message(string $redirectTo, string $queryKey, string $message): void
|
||
|
|
{
|
||
|
|
if ($redirectTo === '') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$separator = (strpos($redirectTo, '?') === false) ? '?' : '&';
|
||
|
|
TaskCertResponse::redirect($redirectTo . $separator . $queryKey . '=' . rawurlencode($message));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
$postIds = TaskCertRequest::postIntArray('post_ids');
|
||
|
|
$postReadState = TaskCertRequest::postArray('post_read_state');
|
||
|
|
$redirectTo = TaskCertRequest::postString('redirect_to', '');
|
||
|
|
|
||
|
|
if ($assignmentId <= 0 || $methodId <= 0) {
|
||
|
|
TaskCertResponse::error('Ungültige assignment_id oder method_id', 400);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($postReadState !== []) {
|
||
|
|
foreach ($postReadState as $postIdRaw => $stateRaw) {
|
||
|
|
$postId = (int) $postIdRaw;
|
||
|
|
if ($postId <= 0) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
$state = strtolower(trim((string) $stateRaw));
|
||
|
|
if (in_array($state, ['read', '1', 'yes', 'true'], true)) {
|
||
|
|
$postIds[] = $postId;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$postIds = array_values(array_unique(array_map('intval', $postIds)));
|
||
|
|
$postIds = array_values(array_filter($postIds, static function (int $postId): bool {
|
||
|
|
return $postId > 0;
|
||
|
|
}));
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$services = task_cert_services();
|
||
|
|
$submissionId = $services['completionService']->submitRead($assignmentId, $methodId, $postIds);
|
||
|
|
$submission = $services['submissionRepo']->findById($submissionId);
|
||
|
|
$submissionStatus = (string) ($submission['status'] ?? '');
|
||
|
|
$payload = is_array($submission['payload'] ?? null) ? $submission['payload'] : [];
|
||
|
|
|
||
|
|
if ($redirectTo !== '') {
|
||
|
|
if ($submissionStatus === 'submitted') {
|
||
|
|
$missingCount = is_array($payload['missing_post_ids'] ?? null) ? count($payload['missing_post_ids']) : 0;
|
||
|
|
$missingNoun = $missingCount === 1 ? 'Pflichtbeitrag' : 'Pflichtbeiträge';
|
||
|
|
$message = $missingCount > 0
|
||
|
|
? ('Zwischengespeichert. Bitte noch ' . $missingCount . ' ' . $missingNoun . ' als gelesen markieren.')
|
||
|
|
: 'Zwischengespeichert. Bitte alle Pflichtbeiträge als gelesen markieren.';
|
||
|
|
task_cert_submit_read_redirect_with_message($redirectTo, 'task_cert_warning', $message);
|
||
|
|
}
|
||
|
|
|
||
|
|
task_cert_submit_read_redirect_with_message($redirectTo, 'task_cert_notice', 'Wissensbeiträge erfolgreich bestätigt.');
|
||
|
|
}
|
||
|
|
|
||
|
|
TaskCertResponse::json([
|
||
|
|
'success' => true,
|
||
|
|
'submission_id' => $submissionId,
|
||
|
|
'status' => $submissionStatus !== '' ? $submissionStatus : 'auto_passed',
|
||
|
|
]);
|
||
|
|
} catch (Throwable $throwable) {
|
||
|
|
if ($redirectTo !== '') {
|
||
|
|
task_cert_submit_read_redirect_with_message($redirectTo, 'task_cert_error', $throwable->getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
TaskCertResponse::json([
|
||
|
|
'success' => false,
|
||
|
|
'error' => $throwable->getMessage(),
|
||
|
|
], 400);
|
||
|
|
}
|