39428-vm/okr_detail.php
Flatlogic Bot 765d998fa1 V1
2026-04-01 10:36:51 +00:00

375 lines
22 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/okr_app.php';
okr_require_schema();
function e(string $value): string
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
$profile = okr_current_profile();
$id = (int) ($_GET['id'] ?? 0);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
okr_verify_csrf();
$action = (string) ($_POST['action_type'] ?? '');
$targetId = (int) ($_POST['id'] ?? $id);
switch ($action) {
case 'update_owner_scores':
okr_update_owner_scores($targetId, $_POST['owner_score'] ?? [], $profile);
okr_flash('success', 'Owner scores updated.');
break;
case 'submit_okr':
okr_submit_entry($targetId, $profile);
okr_flash('success', 'Objective submitted into the approval workflow.');
break;
case 'review_okr':
$note = trim((string) ($_POST['review_note'] ?? ''));
okr_review_entry($targetId, (string) ($_POST['decision'] ?? 'approve'), $_POST['manager_score'] ?? [], $note, $profile);
okr_flash('success', 'Review decision saved.');
break;
case 'add_comment':
okr_add_comment($targetId, (string) ($_POST['comment_message'] ?? ''), $profile);
okr_flash('success', 'Comment posted.');
break;
case 'delete_okr':
okr_delete_entry($targetId, $profile);
okr_flash('success', 'Draft objective deleted.');
okr_redirect('index.php#my-okrs');
break;
default:
throw new RuntimeException('Unknown action.');
}
okr_redirect('okr_detail.php?id=' . $targetId);
} catch (Throwable $exception) {
okr_flash('danger', $exception->getMessage());
okr_redirect('okr_detail.php?id=' . $targetId);
}
}
$entry = okr_fetch_entry($id);
if (!$entry) {
okr_flash('danger', 'Objective not found.');
okr_redirect('index.php');
}
$flash = okr_flash();
$projectName = project_name();
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? project_description();
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
$cssVersion = is_file(__DIR__ . '/assets/css/custom.css') ? (string) filemtime(__DIR__ . '/assets/css/custom.css') : (string) time();
$jsVersion = is_file(__DIR__ . '/assets/js/main.js') ? (string) filemtime(__DIR__ . '/assets/js/main.js') : (string) time();
$canEditOwner = okr_can_edit_owner($entry, $profile) || okr_is_admin($profile);
$canReview = okr_can_review($entry, $profile);
$notifications = okr_collect_notifications(okr_fetch_entries(), 8);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title><?= e($projectName) ?> · <?= e($entry['objective_title']) ?></title>
<?php if ($projectDescription): ?>
<meta name="description" content="<?= e((string) $projectDescription) ?>" />
<meta property="og:description" content="<?= e((string) $projectDescription) ?>" />
<meta property="twitter:description" content="<?= e((string) $projectDescription) ?>" />
<?php endif; ?>
<?php if ($projectImageUrl): ?>
<meta property="og:image" content="<?= e((string) $projectImageUrl) ?>" />
<meta property="twitter:image" content="<?= e((string) $projectImageUrl) ?>" />
<?php endif; ?>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="assets/css/custom.css?v=<?= e($cssVersion) ?>">
</head>
<body>
<div class="detail-shell">
<header class="detail-topbar">
<div>
<a href="index.php" class="text-decoration-none small text-uppercase text-muted">← Back to workspace</a>
<h1 class="page-title mt-2 mb-1"><?= e($entry['objective_title']) ?></h1>
<div class="d-flex flex-wrap gap-2 align-items-center">
<span class="status-pill status-<?= strtolower($entry['status']) ?>"><?= e($entry['status']) ?></span>
<span class="small text-muted"><?= e($entry['department_name']) ?> · <?= e($entry['period_label']) ?></span>
<span class="small text-muted">Owner: <?= e($entry['owner_name']) ?></span>
</div>
</div>
<div class="profile-chip compact-profile">
<span class="profile-avatar"><?= e(substr($profile['name'], 0, 1)) ?></span>
<div>
<strong><?= e($profile['name']) ?></strong>
<small><?= e($profile['role']) ?><?= $profile['level'] !== $profile['role'] ? ' · ' . e($profile['level']) : '' ?></small>
</div>
</div>
</header>
<main class="app-content detail-content">
<section class="page-section pt-0">
<div class="row g-4">
<div class="col-xl-8">
<div class="surface-card mb-4">
<div class="row g-3 align-items-start">
<div class="col-lg-8">
<div class="section-kicker">Objective summary</div>
<h2 class="section-title"><?= e($entry['objective_title']) ?></h2>
<div class="meta-list meta-grid mt-3">
<div>
<dt>Owner</dt>
<dd><?= e($entry['owner_name']) ?><span><?= e($entry['owner_email']) ?></span></dd>
</div>
<div>
<dt>Approver</dt>
<dd><?= e($entry['approver_name']) ?><span><?= e($entry['approver_level']) ?> level</span></dd>
</div>
<div>
<dt>Submitted</dt>
<dd><?= e(okr_time_label((string) $entry['submitted_at'])) ?></dd>
</div>
<div>
<dt>Approved</dt>
<dd><?= e(okr_time_label((string) $entry['approved_at'])) ?></dd>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="score-panel">
<span>Objective score</span>
<strong><?= e(number_format((float) $entry['objective_score'], 1)) ?>%</strong>
<small><?= e((string) $entry['completed_key_results']) ?> / <?= e((string) $entry['key_result_count']) ?> key results above 70%</small>
</div>
</div>
</div>
</div>
<div class="surface-card mb-4">
<div class="d-flex justify-content-between align-items-center gap-3 mb-3">
<div>
<div class="section-kicker">Scoring matrix</div>
<h2 class="section-title">Key results and scores</h2>
</div>
<?php if ($canEditOwner && $entry['status'] === 'Draft'): ?>
<form method="post">
<input type="hidden" name="csrf_token" value="<?= e(okr_csrf_token()) ?>">
<input type="hidden" name="action_type" value="submit_okr">
<input type="hidden" name="id" value="<?= (int) $entry['id'] ?>">
<button class="btn btn-success" type="submit">Submit for approval</button>
</form>
<?php endif; ?>
</div>
<div class="table-responsive">
<table class="table align-middle app-table">
<thead>
<tr>
<th>Key result</th>
<th>Due date</th>
<th>Owner score</th>
<th>Manager score</th>
<th>Effective</th>
</tr>
</thead>
<tbody>
<?php foreach ($entry['key_results'] as $index => $keyResult): ?>
<tr>
<td>
<strong><?= e((string) $keyResult['title']) ?></strong>
</td>
<td><?= e((string) ($keyResult['due_date'] ?: '—')) ?></td>
<td><?= e(number_format((float) ($keyResult['owner_score'] ?? 0), 1)) ?>%</td>
<td><?= $keyResult['manager_score'] === null ? '—' : e(number_format((float) $keyResult['manager_score'], 1)) . '%' ?></td>
<td><?= e(number_format(okr_effective_score($keyResult, (string) $entry['status']), 1)) ?>%</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php if ($canEditOwner): ?>
<div class="surface-card mb-4">
<div class="section-kicker">Owner action</div>
<h2 class="section-title">Update self-scores</h2>
<form method="post" class="vstack gap-3">
<input type="hidden" name="csrf_token" value="<?= e(okr_csrf_token()) ?>">
<input type="hidden" name="action_type" value="update_owner_scores">
<input type="hidden" name="id" value="<?= (int) $entry['id'] ?>">
<?php foreach ($entry['key_results'] as $index => $keyResult): ?>
<div class="row g-3 align-items-center">
<div class="col-md-8">
<label class="form-label mb-1"><?= e((string) $keyResult['title']) ?></label>
<div class="small text-muted">Owner score before manager approval.</div>
</div>
<div class="col-md-4">
<input class="form-control" type="number" min="0" max="100" step="0.1" name="owner_score[<?= $index ?>]" value="<?= e((string) $keyResult['owner_score']) ?>">
</div>
</div>
<?php endforeach; ?>
<div class="d-flex justify-content-between align-items-center gap-3">
<div class="small text-muted">You can refine progress at any time; the objective score recalculates automatically.</div>
<button class="btn btn-outline-dark" type="submit">Save self-scores</button>
</div>
</form>
</div>
<?php endif; ?>
<?php if ($canReview && $entry['status'] !== 'Approved'): ?>
<div class="surface-card mb-4">
<div class="section-kicker">Approver action</div>
<h2 class="section-title">Approve or return with feedback</h2>
<form method="post" class="vstack gap-3">
<input type="hidden" name="csrf_token" value="<?= e(okr_csrf_token()) ?>">
<input type="hidden" name="action_type" value="review_okr">
<input type="hidden" name="id" value="<?= (int) $entry['id'] ?>">
<?php foreach ($entry['key_results'] as $index => $keyResult): ?>
<div class="row g-3 align-items-center">
<div class="col-md-8">
<label class="form-label mb-1"><?= e((string) $keyResult['title']) ?></label>
<div class="small text-muted">Owner score: <?= e(number_format((float) $keyResult['owner_score'], 1)) ?>%</div>
</div>
<div class="col-md-4">
<input class="form-control" type="number" min="0" max="100" step="0.1" name="manager_score[<?= $index ?>]" value="<?= e((string) ($keyResult['manager_score'] ?? $keyResult['owner_score'])) ?>">
</div>
</div>
<?php endforeach; ?>
<div>
<label class="form-label" for="review_note">Feedback note</label>
<textarea class="form-control" rows="3" id="review_note" name="review_note" maxlength="500" placeholder="Add guidance or approval context"></textarea>
</div>
<div class="d-flex flex-wrap justify-content-end gap-2">
<button class="btn btn-outline-secondary" type="submit" name="decision" value="reject">Return to draft</button>
<button class="btn btn-success" type="submit" name="decision" value="approve">Approve and score</button>
</div>
</form>
</div>
<?php endif; ?>
<div class="surface-card">
<div class="section-kicker">Discussion</div>
<h2 class="section-title">Comments</h2>
<form method="post" class="vstack gap-3 mb-4">
<input type="hidden" name="csrf_token" value="<?= e(okr_csrf_token()) ?>">
<input type="hidden" name="action_type" value="add_comment">
<input type="hidden" name="id" value="<?= (int) $entry['id'] ?>">
<div>
<label class="form-label" for="comment_message">Add comment</label>
<textarea id="comment_message" class="form-control" name="comment_message" rows="3" maxlength="500" placeholder="Share context, blockers, or coaching notes"></textarea>
</div>
<div class="d-flex justify-content-end">
<button class="btn btn-dark" type="submit">Post comment</button>
</div>
</form>
<div class="comment-stream">
<?php if ($entry['comments'] === []): ?>
<div class="empty-state compact-empty">
<strong>No comments yet.</strong>
<span>Comments are shared and visible alongside the approval trail.</span>
</div>
<?php else: ?>
<?php foreach (array_reverse($entry['comments']) as $comment): ?>
<article class="comment-card">
<div class="activity-topline">
<strong><?= e((string) $comment['actor_name']) ?></strong>
<span><?= e(okr_time_label((string) $comment['time'])) ?></span>
</div>
<div class="activity-text"><?= e((string) $comment['message']) ?></div>
<div class="activity-meta"><?= e((string) $comment['actor_role']) ?></div>
</article>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
<div class="col-xl-4">
<div class="surface-card mb-4">
<div class="section-kicker">Permissions</div>
<h2 class="section-title">Current access</h2>
<div class="meta-list">
<span><strong><?= e($profile['name']) ?></strong></span>
<span><?= e($profile['role']) ?><?= $profile['level'] !== $profile['role'] ? ' · ' . e($profile['level']) : '' ?></span>
<span><?= $canReview ? 'Can review this objective' : 'Cannot review this objective' ?></span>
<span><?= $canEditOwner ? 'Can update owner scores' : 'Cannot update owner scores' ?></span>
</div>
</div>
<div class="surface-card mb-4">
<div class="section-kicker">Recent activity</div>
<h2 class="section-title">Objective timeline</h2>
<div class="activity-list">
<?php foreach (array_reverse($entry['activity']) as $activity): ?>
<div class="activity-item static-item">
<div class="activity-topline">
<strong><?= e((string) $activity['actor_name']) ?></strong>
<span><?= e(okr_time_label((string) $activity['time'])) ?></span>
</div>
<div class="activity-text"><?= e((string) $activity['message']) ?></div>
<div class="activity-meta"><?= e((string) ($activity['actor_role'] ?? 'System')) ?></div>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="surface-card mb-4">
<div class="section-kicker">Global notifications</div>
<h2 class="section-title">What everyone can see</h2>
<div class="activity-list" id="activityFeed" data-feed-url="feed.php">
<?php if ($notifications === []): ?>
<div class="empty-state compact-empty">
<strong>No notifications yet.</strong>
<span>Workspace activity will show up here automatically.</span>
</div>
<?php else: ?>
<?php foreach ($notifications as $notification): ?>
<a href="okr_detail.php?id=<?= (int) $notification['objective_id'] ?>" class="activity-item text-decoration-none">
<div class="activity-topline">
<strong><?= e((string) $notification['actor_name']) ?></strong>
<span><?= e(okr_time_label((string) $notification['time'])) ?></span>
</div>
<div class="activity-text"><?= e((string) $notification['message']) ?></div>
<div class="activity-meta"><?= e((string) $notification['objective_title']) ?></div>
</a>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<?php if ($canEditOwner && $entry['status'] === 'Draft'): ?>
<div class="surface-card">
<div class="section-kicker">Cleanup</div>
<h2 class="section-title">Delete draft</h2>
<p class="text-muted small mb-3">Delete is limited to draft objectives in this first iteration to keep the workflow safe.</p>
<form method="post" onsubmit="return confirm('Delete this draft objective?');">
<input type="hidden" name="csrf_token" value="<?= e(okr_csrf_token()) ?>">
<input type="hidden" name="action_type" value="delete_okr">
<input type="hidden" name="id" value="<?= (int) $entry['id'] ?>">
<button class="btn btn-outline-secondary w-100" type="submit">Delete draft</button>
</form>
</div>
<?php endif; ?>
</div>
</div>
</section>
</main>
</div>
<?php if ($flash): ?>
<div class="app-flash" data-flash-type="<?= e((string) $flash['type']) ?>" data-flash-message="<?= e((string) $flash['message']) ?>"></div>
<?php endif; ?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="assets/js/main.js?v=<?= e($jsVersion) ?>" defer></script>
</body>
</html>