hasPermission('r-task-cert-admin')) { TaskCertResponse::json(['error' => 'Keine Berechtigung'], 403); } $assignmentId = (int) ($_GET['assignment_id'] ?? 0); if ($assignmentId <= 0) { TaskCertResponse::json(['error' => 'Ungültige assignment_id'], 400); } $services = task_cert_services(); $assignment = $services['assignmentRepo']->findById($assignmentId); if ($assignment === null) { TaskCertResponse::json(['error' => 'Zuweisung nicht gefunden'], 404); } $taskId = (int) ($assignment['task_id'] ?? 0); $task = $services['definitionRepo']->findById($taskId); if ($task === null) { TaskCertResponse::json(['error' => 'Aufgabe nicht gefunden'], 404); } $contactId = (int) ($assignment['main_contact_id'] ?? 0); $contact = null; if ($contactId > 0) { $contact = TaskCertDb::fetchOne( 'SELECT id, name, email FROM main_contact WHERE id = ?', 'i', [$contactId] ); } $methods = array_values(array_filter( $services['ruleRepo']->getMethodsByTask($taskId), static fn(array $m): bool => (int) ($m['active'] ?? 1) === 1 )); $existingSubmissions = $services['submissionRepo']->listByAssignment($assignmentId); $fulfilledMethodIds = []; foreach ($existingSubmissions as $sub) { $subStatus = (string) ($sub['status'] ?? ''); if (in_array($subStatus, ['auto_passed', 'approved'], true)) { $fulfilledMethodIds[(int) ($sub['method_id'] ?? 0)] = true; } } $methodsOut = []; foreach ($methods as $method) { $methodId = (int) ($method['id'] ?? 0); $methodType = (string) ($method['method_type'] ?? ''); $formFields = []; if ($methodType === 'form_submit') { foreach ((array) ($method['form_fields'] ?? []) as $field) { $options = []; $rawOptions = is_array($field['options'] ?? null) ? $field['options'] : []; foreach ($rawOptions as $opt) { $options[] = [ 'value' => (string) ($opt['value'] ?? ''), 'label' => (string) ($opt['label'] ?? $opt['value'] ?? ''), ]; } $formFields[] = [ 'field_key' => (string) ($field['field_key'] ?? ''), 'label' => (string) ($field['label'] ?? ''), 'field_type' => (string) ($field['field_type'] ?? 'text'), 'is_required' => (int) ($field['is_required'] ?? 0), 'options' => $options, 'placeholder' => (string) ($field['placeholder'] ?? ''), ]; } } $methodsOut[] = [ 'id' => $methodId, 'method_type' => $methodType, 'is_required' => (int) ($method['is_required'] ?? 1), 'already_fulfilled' => isset($fulfilledMethodIds[$methodId]), 'knowledge_post_count' => count((array) ($method['knowledge_posts'] ?? [])), 'quiz_question_count' => count((array) ($method['quiz_questions'] ?? [])), 'form_fields' => $formFields, ]; } TaskCertResponse::json([ 'task_id' => $taskId, 'task_title' => (string) ($task['title'] ?? ''), 'assignment_id' => $assignmentId, 'contact_name' => (string) ($contact['name'] ?? '-'), 'contact_email' => (string) ($contact['email'] ?? ''), 'cycle_key' => (string) ($assignment['cycle_key'] ?? ''), 'status' => (string) ($assignment['status'] ?? ''), 'due_at' => (string) ($assignment['due_at'] ?? ''), 'methods' => $methodsOut, 'csrf_token' => TaskCertCsrf::token(), ]);