Files
awo-hamburg-intranet/module/tasks/actions/user/task_detail_action.php

126 lines
3.8 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
require_once dirname(dirname(__DIR__)) . '/bootstrap.php';
function task_cert_user_task_detail_action(): array
{
$auth = TaskCertAuthContext::fromGlobals();
if (!$auth->isLoggedIn()) {
return [
'error' => 'Nicht eingeloggt.',
];
}
$assignmentId = TaskCertRequest::queryInt('assignment_id', 0);
if ($assignmentId <= 0) {
return [
'error' => 'Ungültige Assignment-ID.',
];
}
$services = task_cert_services();
$assignment = $services['assignmentRepo']->findById($assignmentId);
if ($assignment === null) {
return [
'error' => 'Assignment nicht gefunden.',
];
}
if ((int) $assignment['main_contact_id'] !== $auth->contactId()) {
return [
'error' => 'Keine Berechtigung für dieses Assignment.',
];
}
$task = $services['definitionRepo']->findById((int) $assignment['task_id']);
if ($task === null) {
return [
'error' => 'Aufgabe nicht gefunden.',
];
}
$methods = $services['ruleRepo']->getMethodsByTask((int) $assignment['task_id']);
$methods = array_values(array_filter($methods, static function (array $method): bool {
return (int) ($method['active'] ?? 1) === 1;
}));
$submissions = $services['submissionRepo']->listByAssignment($assignmentId);
$postIds = [];
foreach ($methods as $method) {
if ((string) ($method['method_type'] ?? '') !== 'knowledge_read') {
continue;
}
foreach ((array) ($method['knowledge_posts'] ?? []) as $post) {
$postId = (int) ($post['knowledge_post_id'] ?? 0);
if ($postId > 0) {
$postIds[] = $postId;
}
}
}
$knowledgePostTitleMap = [];
$lookupService = $services['lookupService'] ?? null;
if ($lookupService instanceof TaskLookupService) {
$languageId = (int) ($GLOBALS['language']['id'] ?? 1);
$knowledgePostTitleMap = $lookupService->getKnowledgePostTitleMapByIds($languageId, $postIds);
}
foreach ($methods as &$method) {
if ((string) ($method['method_type'] ?? '') !== 'knowledge_read') {
continue;
}
foreach ((array) ($method['knowledge_posts'] ?? []) as &$post) {
$postId = (int) ($post['knowledge_post_id'] ?? 0);
$post['title'] = $knowledgePostTitleMap[$postId] ?? ('Beitrag #' . $postId);
}
unset($post);
}
unset($method);
$responsibles = $services['ruleRepo']->getResponsiblesWithContactData((int) $assignment['task_id']);
$neededPrefillColumns = [];
foreach ($methods as $m) {
foreach ((array) ($m['form_fields'] ?? []) as $ff) {
$col = trim((string) ($ff['prefill_column'] ?? ''));
if ($col !== '' && ContactPrefillColumns::isValid($col)) {
$neededPrefillColumns[$col] = true;
}
}
}
$contactData = [];
if ($neededPrefillColumns !== []) {
try {
$contactRow = TaskCertDb::fetchOne(
'SELECT * FROM main_contact WHERE id = ? LIMIT 1',
'i',
[$auth->contactId()]
);
if (is_array($contactRow)) {
$contactData = $contactRow;
}
} catch (Throwable $e) {
$contactData = [];
}
}
return [
'error' => null,
'assignment' => $assignment,
'task' => $task,
'methods' => $methods,
'submissions' => $submissions,
'task_certificates_history' => $services['certificateService']->listUserTaskCertificates(
$auth->contactId(),
(int) $assignment['task_id']
),
'responsibles' => $responsibles,
'contact_data' => $contactData,
'csrf_token' => TaskCertCsrf::token(),
];
}