-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
784 lines (740 loc) · 39.5 KB
/
Copy pathsetup.php
File metadata and controls
784 lines (740 loc) · 39.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
<?php
/**
* setup.php — 考前管理員工具
* 功能:預建所有考生資料夾、測試 NAS 寫入權限、診斷問題
* 使用:瀏覽器開啟此頁,輸入設定檔中的 setup_password
*/
require_once __DIR__ . '/lib.php';
$config = json_decode(file_get_contents(__DIR__ . '/config.json'), true);
$users = json_decode(file_get_contents(__DIR__ . '/user.json'), true);
$password = $config['setup_password'] ?? 'admin';
session_start();
$authed = isset($_SESSION['setup_authed']) && $_SESSION['setup_authed'] === true;
// 登入/登出
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['setup_pw'])) {
if ($_POST['setup_pw'] === $password) {
$_SESSION['setup_authed'] = true;
$authed = true;
} else {
$pwError = '密碼錯誤';
}
}
if (isset($_GET['logout'])) {
unset($_SESSION['setup_authed']);
header('Location: setup.php');
exit;
}
// 更新考生資料:從 ERP 下載最新 user.json
$updateUserResult = null;
if ($authed && isset($_POST['action']) && $_POST['action'] === 'update_users') {
$apiUrl = 'http://192.168.1.100:8080/api/getstaffemail.php';
$ctx = stream_context_create(['http' => ['timeout' => 10]]);
$raw = @file_get_contents($apiUrl, false, $ctx);
if ($raw === false) {
$updateUserResult = ['ok' => false, 'msg' => '無法連線到 ' . $apiUrl];
} else {
$data = json_decode($raw, true);
if (!is_array($data) || empty($data)) {
$updateUserResult = ['ok' => false, 'msg' => '回傳資料格式錯誤或為空'];
} else {
$dest = __DIR__ . '/user.json';
$written = file_put_contents($dest, json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
if ($written === false) {
$updateUserResult = ['ok' => false, 'msg' => '無法寫入 user.json,請確認檔案權限'];
} else {
$users = $data; // 更新目前頁面使用的資料
$updateUserResult = ['ok' => true, 'count' => count($data)];
}
}
}
}
// 匯入 CSV 考生名單
$importResult = null;
if ($authed && isset($_POST['action']) && $_POST['action'] === 'import_csv') {
if (empty($_FILES['csv_file']['tmp_name'])) {
$importResult = ['ok' => false, 'msg' => '未收到檔案,請重試'];
} else {
$tmp = $_FILES['csv_file']['tmp_name'];
$rows = [];
$errors = [];
$lineNo = 0;
if (($fh = fopen($tmp, 'r')) !== false) {
// 跳過 BOM
$bom = fread($fh, 3);
if ($bom !== "\xEF\xBB\xBF") rewind($fh);
while (($cols = fgetcsv($fh)) !== false) {
$lineNo++;
if ($lineNo === 1 && preg_match('/^\s*(員工編號|id|employee)/i', $cols[0] ?? '')) continue; // 跳過標題列
if (count($cols) < 4) { $errors[] = "第 {$lineNo} 行欄位不足(需要:員工編號,部門,姓名,生日八碼,[Email])"; continue; }
[$empId, $units, $name, $birth] = array_map('trim', array_slice($cols, 0, 4));
$email = trim($cols[4] ?? '');
$empId = strtoupper($empId);
if (!preg_match('/^[A-Z0-9]+$/', $empId)) { $errors[] = "第 {$lineNo} 行員工編號格式錯誤:{$empId}"; continue; }
if (!preg_match('/^\d{8}$/', $birth)) { $errors[] = "第 {$lineNo} 行生日格式錯誤(需 8 碼數字):{$birth}"; continue; }
$rows[$empId] = ['UNITS' => $units, 'NAME' => $name, 'BIRTH' => $birth, 'EMAIL' => $email];
}
fclose($fh);
}
if (empty($rows) && empty($errors)) {
$importResult = ['ok' => false, 'msg' => 'CSV 檔案為空或格式不符'];
} elseif (!empty($errors)) {
$importResult = ['ok' => false, 'msg' => implode('<br>', array_map('htmlspecialchars', $errors))];
} else {
$dest = __DIR__ . '/user.json';
$written = file_put_contents($dest, json_encode($rows, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
if ($written === false) {
$importResult = ['ok' => false, 'msg' => '無法寫入 user.json,請確認檔案權限'];
} else {
$users = $rows;
$importResult = ['ok' => true, 'count' => count($rows)];
}
}
}
}
// 執行建立所有資料夾
$createResults = [];
if ($authed && isset($_POST['action']) && $_POST['action'] === 'create_all') {
foreach ($users as $id => $u) {
$folderName = $id . '_' . $u['NAME'];
$result = ensure_user_folder($folderName);
$createResults[$id] = [
'name' => $u['NAME'],
'folder' => $folderName,
'ok' => $result === true,
'msg' => $result === true ? '成功' : $result,
];
}
}
$base = get_answer_base();
// 診斷:各考生資料夾狀態
$status = [];
if ($authed) {
foreach ($users as $id => $u) {
$folderName = $id . '_' . $u['NAME'];
$path = $base . DIRECTORY_SEPARATOR . $folderName;
$exists = is_dir($path);
$writable = false;
$writeMsg = '';
if ($exists) {
$testFile = $path . DIRECTORY_SEPARATOR . '.writable_test';
if (@file_put_contents($testFile, '1') !== false) {
@unlink($testFile);
$writable = true;
} else {
$writeMsg = '資料夾存在但無寫入權限';
}
}
$status[$id] = [
'name' => $u['NAME'],
'folder' => $folderName,
'path' => $path,
'exists' => $exists,
'writable' => $writable,
'msg' => $writeMsg,
];
}
}
// 統計
$totalUsers = count($users);
$existCount = count(array_filter($status, fn($s) => $s['exists']));
$writeCount = count(array_filter($status, fn($s) => $s['writable']));
// 使用時間格式化(秒→X時Y分 或 Y分)
function format_duration(?int $sec): string {
if ($sec === null || $sec <= 0) return '—';
$h = intdiv($sec, 3600);
$m = intdiv($sec % 3600, 60);
if ($h > 0) return sprintf('%d 時 %02d 分', $h, $m);
return sprintf('%d 分', $m);
}
// 考生作答現況
$examStats = [];
if ($authed) {
foreach ($users as $id => $u) {
$folderName = $id . '_' . $u['NAME'];
$path = $base . DIRECTORY_SEPARATOR . $folderName;
$st = [
'name' => $u['NAME'],
'first_login' => null, // 最早登入時間
'last_login' => null, // 最後登入時間(判斷是否已離場用)
'last_logout' => null, // 最後登出時間
'last_time' => null, // log 最後一筆時間
'duration_sec' => null,
'answered' => 0,
'total' => 0,
'keys' => [], // '1-1' => bool
];
// 計算各題完成狀況
foreach ($config['questions'] as $q) {
foreach ($q['subs'] as $s) {
$k = $q['no'] . '-' . $s['no'];
$st['total']++;
$f = is_dir($path) ? find_answer_file($path, $q['no'], $s['no']) : null;
$st['keys'][$k] = (bool)$f;
if ($f) $st['answered']++;
}
}
// 解析 activity.log
// GRADE、LOCK、UNLOCK(批改者操作)和 REVIEW_*(查閱)不計入考試時間
$studentActions = ['LOGIN', 'LOGOUT', 'ANSWER', 'UPLOAD', 'DOWNLOAD', 'PING'];
$logFile = $path . '/activity.log';
$openLogin = null; // 尚未配對的 LOGIN 時間
$totalSec = 0; // 累計考試秒數
if (is_file($logFile)) {
foreach (file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [] as $line) {
$p = explode("\t", $line);
if (count($p) < 2) continue;
[$dt, $action] = [$p[0], $p[1]];
if ($action === 'LOGIN') {
if (!$st['first_login']) $st['first_login'] = $dt;
$st['last_login'] = $dt;
// 若前一段 LOGIN 未登出就又登入,先把前段關掉
if ($openLogin !== null) {
$totalSec += strtotime($dt) - strtotime($openLogin);
}
$openLogin = $dt;
}
if ($action === 'LOGOUT') {
$st['last_logout'] = $dt;
if ($openLogin !== null) {
$totalSec += strtotime($dt) - strtotime($openLogin);
$openLogin = null;
}
}
if (in_array($action, $studentActions, true)) {
$st['last_time'] = $dt;
}
}
}
// 最後一段 LOGIN 若未登出,累加到最後一筆學生操作時間
if ($openLogin !== null && $st['last_time']) {
$totalSec += strtotime($st['last_time']) - strtotime($openLogin);
}
$st['duration_sec'] = $totalSec > 0 ? $totalSec : null;
// 讀取批改資料
$grades = get_grades($path);
$st['score'] = calc_total_score($grades);
$st['readonly'] = !empty($grades['readonly']);
$lockedName = $grades['locked_by_name'] ?? '';
$st['grader_surname'] = $lockedName !== '' ? mb_substr($lockedName, 0, 1, 'UTF-8') : '';
$examStats[$id] = $st;
}
}
// 診斷:根目錄寫入
$baseExists = is_dir($base);
$baseWritable = false;
$baseMsg = '';
if ($authed) {
if (!$baseExists) {
$baseMsg = '目錄不存在';
} else {
$t = $base . DIRECTORY_SEPARATOR . '.base_test';
if (@file_put_contents($t, '1') !== false) {
@unlink($t);
$baseWritable = true;
} else {
$baseMsg = '目錄存在但無寫入權限';
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>考試管理 — <?= htmlspecialchars($config['exam_name']) ?></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
<style>
body { background: #f0f2f5; }
.top-bar { background: #1a3a6b; color: #fff; padding: 12px 24px; }
.path-box { font-family: monospace; background: #f8f9fa; border: 1px solid #dee2e6;
padding: 8px 12px; border-radius: 4px; font-size: .9em; word-break: break-all; }
.icon-ok { color: #198754; }
.icon-warn { color: #ffc107; }
.icon-fail { color: #dc3545; }
.accordion-item { border: none; margin-bottom: 12px; border-radius: 8px !important; overflow: hidden; box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.accordion-button { font-weight: 600; }
.accordion-button:not(.collapsed) { background: #eef2fb; color: #1a3a6b; box-shadow: none; }
.accordion-button:focus { box-shadow: none; }
/* 排序表頭 */
th[data-sort] { cursor: pointer; user-select: none; white-space: nowrap; }
th[data-sort]:hover { background: #d8dde3; }
th[data-sort] .sort-icon { opacity: .3; font-size: .8em; vertical-align: middle; margin-left: 3px; }
th[data-sort].asc .sort-icon,
th[data-sort].desc .sort-icon { opacity: 1; color: #1a3a6b; }
</style>
</head>
<body>
<div class="top-bar d-flex justify-content-between align-items-center">
<div>
<strong>考試管理工具</strong>
<span class="opacity-75 ms-2">— <?= htmlspecialchars($config['exam_name']) ?></span>
</div>
<?php if ($authed): ?>
<a href="?logout=1" class="btn btn-sm btn-outline-light">登出</a>
<?php endif; ?>
</div>
<div class="container py-4" style="max-width:900px;">
<?php if (!$authed): ?>
<!-- 登入 -->
<div class="card shadow-sm mx-auto" style="max-width:360px; margin-top:80px;">
<div class="card-body p-4">
<h5 class="mb-3 text-center">管理員登入</h5>
<?php if (!empty($pwError)): ?>
<div class="alert alert-danger py-2"><?= htmlspecialchars($pwError) ?></div>
<?php endif; ?>
<form method="POST">
<div class="mb-3">
<input type="password" name="setup_pw" class="form-control" placeholder="管理員密碼" autofocus>
</div>
<button class="btn btn-primary w-100">登入</button>
</form>
<p class="text-muted small mt-3 mb-0 text-center">密碼設定於 config.json → setup_password</p>
</div>
</div>
<?php else: ?>
<?php
$inProgress = count(array_filter($examStats, fn($s) =>
$s['first_login'] && !($s['last_logout'] && $s['last_logout'] >= $s['last_login'])));
$leftCount = count(array_filter($examStats, fn($s) =>
$s['last_logout'] && $s['last_logout'] >= $s['last_login']));
$noShowCount = count(array_filter($examStats, fn($s) => !$s['first_login']));
?>
<div class="accordion mb-4" id="setupAccordion">
<!-- ① 更新考生資料 -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button"
data-bs-toggle="collapse" data-bs-target="#acc-update-users" aria-expanded="false">
<i class="bi bi-person-lines-fill me-2"></i>更新考生資料
<span class="ms-3 text-muted small fw-normal">從 ERP 下載最新名單覆蓋 user.json</span>
</button>
</h2>
<div id="acc-update-users" class="accordion-collapse collapse<?= $updateUserResult ? ' show' : '' ?>">
<div class="accordion-body">
<?php if ($updateUserResult): ?>
<?php if ($updateUserResult['ok']): ?>
<div class="alert alert-success py-2 mb-3">
<i class="bi bi-check-circle-fill me-1"></i>
更新成功,共 <?= $updateUserResult['count'] ?> 筆考生資料已寫入 user.json
</div>
<?php else: ?>
<div class="alert alert-danger py-2 mb-3">
<i class="bi bi-x-circle-fill me-1"></i>
<?= htmlspecialchars($updateUserResult['msg']) ?>
</div>
<?php endif; ?>
<?php endif; ?>
<p class="text-muted small mb-3">
資料來源:<code>http://192.168.1.100:8080/api/getstaffemail.php</code><br>
執行後將覆蓋本機 <code>user.json</code>,目前共 <strong><?= count($users) ?></strong> 筆。
</p>
<form method="POST"
onsubmit="return confirm('確定要從 ERP 下載最新考生資料並覆蓋 user.json?');">
<input type="hidden" name="action" value="update_users">
<button class="btn btn-primary">
<i class="bi bi-cloud-download me-1"></i> 立即更新考生資料
</button>
</form>
</div>
</div>
</div>
<!-- ①-b 匯入 CSV 考生名單 -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button"
data-bs-toggle="collapse" data-bs-target="#acc-import-csv" aria-expanded="false">
<i class="bi bi-filetype-csv me-2"></i>匯入 CSV 考生名單
<span class="ms-3 text-muted small fw-normal">上傳 CSV 直接覆蓋 user.json</span>
</button>
</h2>
<div id="acc-import-csv" class="accordion-collapse collapse<?= $importResult ? ' show' : '' ?>">
<div class="accordion-body">
<?php if ($importResult): ?>
<?php if ($importResult['ok']): ?>
<div class="alert alert-success py-2 mb-3">
<i class="bi bi-check-circle-fill me-1"></i>
匯入成功,共 <?= $importResult['count'] ?> 筆考生資料已寫入 user.json
</div>
<?php else: ?>
<div class="alert alert-danger py-2 mb-3">
<i class="bi bi-x-circle-fill me-1"></i>
<?= $importResult['msg'] ?>
</div>
<?php endif; ?>
<?php endif; ?>
<p class="text-muted small mb-2">
CSV 格式(UTF-8 或 UTF-8 BOM):<code>員工編號,部門,姓名,生日八碼,Email</code><br>
第一列若為標題列將自動跳過。生日格式為西元年八碼,例如 <code>19900115</code>。
</p>
<div class="alert alert-light border py-2 mb-3 small">
<strong>範例:</strong><br>
<code>A0001,SA,王小明,19900115,ming@example.com</code><br>
<code>B0023,HR,李美玲,19851220,li@example.com</code>
</div>
<form method="POST" enctype="multipart/form-data"
onsubmit="return confirm('確定要上傳 CSV 並覆蓋 user.json?');">
<input type="hidden" name="action" value="import_csv">
<div class="d-flex align-items-center gap-2 flex-wrap">
<input type="file" name="csv_file" class="form-control" accept=".csv" style="max-width:320px;" required>
<button class="btn btn-primary">
<i class="bi bi-upload me-1"></i>匯入並覆蓋 user.json
</button>
</div>
</form>
</div>
</div>
</div>
<!-- ② 答案存放路徑(預設收合) -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button"
data-bs-toggle="collapse" data-bs-target="#acc-path" aria-expanded="false">
<i class="bi bi-hdd-network me-2"></i>答案存放路徑
<span class="ms-3">
<?php if ($baseWritable): ?>
<i class="bi bi-check-circle-fill icon-ok"></i>
<span class="text-success small fw-normal ms-1">路徑正常</span>
<?php elseif ($baseExists): ?>
<i class="bi bi-exclamation-triangle-fill icon-warn"></i>
<span class="text-warning small fw-normal ms-1">無寫入權限</span>
<?php else: ?>
<i class="bi bi-x-circle-fill icon-fail"></i>
<span class="text-danger small fw-normal ms-1">目錄不存在</span>
<?php endif; ?>
</span>
</button>
</h2>
<div id="acc-path" class="accordion-collapse collapse">
<div class="accordion-body">
<div class="path-box mb-3"><?= htmlspecialchars($base) ?></div>
<div class="d-flex gap-3 flex-wrap mb-2">
<span>
目錄存在:
<?php if ($baseExists): ?>
<i class="bi bi-check-circle-fill icon-ok"></i> 是
<?php else: ?>
<i class="bi bi-x-circle-fill icon-fail"></i> 否
<?php endif; ?>
</span>
<span>
可寫入:
<?php if ($baseWritable): ?>
<i class="bi bi-check-circle-fill icon-ok"></i> 是
<?php elseif ($baseExists): ?>
<i class="bi bi-x-circle-fill icon-fail"></i> 否 — <?= htmlspecialchars($baseMsg) ?>
<?php else: ?>
<i class="bi bi-dash-circle icon-warn"></i> 目錄不存在
<?php endif; ?>
</span>
</div>
<?php if (!$baseWritable): ?>
<div class="alert alert-warning mt-2 mb-0 small">
<strong>修正方法:</strong>
<ol class="mb-0 mt-1">
<li>確認 NAS 共用資料夾已掛載到此路徑</li>
<li>確認 Web 伺服器執行帳號(如 <code>IUSR</code>、<code>www-data</code>)對此路徑有「完全控制」或「修改」權限</li>
<li>若使用 Windows IIS,可在資料夾右鍵→內容→安全性,新增 <code>IIS_IUSRS</code> 並給予寫入權限</li>
<li>修改 <code>config.json</code> 中的 <code>answer_path</code> 為正確的 NAS 掛載路徑</li>
</ol>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- ② 考生資料夾管理(預設收合) -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button"
data-bs-toggle="collapse" data-bs-target="#acc-folders" aria-expanded="false">
<i class="bi bi-folder-fill me-2"></i>考生資料夾管理
<span class="ms-3 text-muted small fw-normal">
共 <?= $totalUsers ?> 人 已建立 <?= $existCount ?> 可寫入 <?= $writeCount ?>
</span>
</button>
</h2>
<div id="acc-folders" class="accordion-collapse collapse">
<div class="accordion-body">
<?php if (!empty($createResults)): ?>
<div class="alert alert-info py-2 mb-3">
執行完畢:
<?= count(array_filter($createResults, fn($r) => $r['ok'])) ?> 成功,
<?= count(array_filter($createResults, fn($r) => !$r['ok'])) ?> 失敗
</div>
<?php endif; ?>
<form method="POST" class="mb-3"
onsubmit="return confirm('確定要為所有考生建立資料夾?');">
<input type="hidden" name="action" value="create_all">
<button class="btn btn-success">
<i class="bi bi-folder-plus"></i> 一鍵建立所有考生資料夾
</button>
<span class="text-muted small ms-2">已存在的資料夾不受影響</span>
</form>
<div class="table-responsive">
<table class="table table-sm table-hover mb-0">
<thead class="table-light">
<tr>
<th>員工編號</th>
<th>姓名</th>
<th>資料夾</th>
<th class="text-center">存在</th>
<th class="text-center">可寫入</th>
<th>備註</th>
</tr>
</thead>
<tbody>
<?php foreach ($status as $id => $s):
$cr = $createResults[$id] ?? null;
?>
<tr>
<td><?= htmlspecialchars($id) ?></td>
<td><?= htmlspecialchars($s['name']) ?></td>
<td class="text-muted small"><?= htmlspecialchars($s['folder']) ?></td>
<td class="text-center">
<?php if ($s['exists']): ?>
<i class="bi bi-check-circle-fill icon-ok"></i>
<?php else: ?>
<i class="bi bi-x-circle-fill icon-fail"></i>
<?php endif; ?>
</td>
<td class="text-center">
<?php if ($s['writable']): ?>
<i class="bi bi-check-circle-fill icon-ok"></i>
<?php elseif ($s['exists']): ?>
<i class="bi bi-x-circle-fill icon-fail"></i>
<?php else: ?>
<i class="bi bi-dash icon-warn"></i>
<?php endif; ?>
</td>
<td class="small text-muted">
<?php if ($cr): ?>
<span class="<?= $cr['ok'] ? 'text-success' : 'text-danger' ?>">
<?= $cr['ok'] ? '剛建立成功' : htmlspecialchars($cr['msg']) ?>
</span>
<?php elseif (!empty($s['msg'])): ?>
<span class="text-danger"><?= htmlspecialchars($s['msg']) ?></span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- ③ 考生作答現況(預設展開) -->
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button"
data-bs-toggle="collapse" data-bs-target="#acc-status" aria-expanded="true">
<i class="bi bi-people-fill me-2"></i>考生作答現況
<span class="ms-3 d-flex align-items-center gap-2">
<span class="badge bg-success">作答中 <?= $inProgress ?></span>
<span class="badge bg-secondary">已離場 <?= $leftCount ?></span>
<span class="badge bg-light text-dark border">未登入 <?= $noShowCount ?></span>
</span>
<span class="ms-auto me-2 text-muted small">
<i class="bi bi-clock"></i> <span id="live-time"><?= date('H:i:s') ?></span>
</span>
</button>
</h2>
<div id="acc-status" class="accordion-collapse collapse show">
<div class="accordion-body p-0">
<div class="px-3 pt-2 pb-2 border-bottom text-end">
<a href="setup.php" class="btn btn-sm btn-outline-secondary py-0 px-2">
<i class="bi bi-arrow-clockwise"></i> 重新整理
</a>
</div>
<div class="table-responsive">
<table class="table table-sm table-hover align-middle mb-0">
<thead class="table-light">
<tr>
<th data-sort="id">員工編號<i class="bi bi-chevron-expand sort-icon"></i></th>
<th data-sort="name">姓名<i class="bi bi-chevron-expand sort-icon"></i></th>
<th class="text-center" data-sort="status">狀態<i class="bi bi-chevron-expand sort-icon"></i></th>
<th data-sort="first_login">首次登入<i class="bi bi-chevron-expand sort-icon"></i></th>
<th data-sort="last_act">最後活動<i class="bi bi-chevron-expand sort-icon"></i></th>
<th data-sort="duration">使用時間<i class="bi bi-chevron-expand sort-icon"></i></th>
<th style="min-width:130px;" data-sort="pct">完成度<i class="bi bi-chevron-expand sort-icon"></i></th>
<th class="text-center" data-sort="score">得分<i class="bi bi-chevron-expand sort-icon"></i></th>
<th class="text-center" data-sort="grader">批改<i class="bi bi-chevron-expand sort-icon"></i></th>
<th>各題作答</th>
</tr>
</thead>
<tbody>
<?php foreach ($examStats as $id => $st):
$hasLogin = (bool)$st['first_login'];
$hasLogout = $st['last_logout'] && $st['last_logout'] >= $st['last_login'];
$pct = $st['total'] > 0 ? round($st['answered'] / $st['total'] * 100) : 0;
$barClass = $pct === 100 ? 'bg-success' : ($pct > 0 ? 'bg-primary' : 'bg-secondary');
$statusVal = !$hasLogin ? 0 : ($hasLogout ? 1 : 2);
$lastActVal = $hasLogout ? $st['last_logout'] : ($st['last_time'] ?? '');
?>
<tr>
<td class="fw-semibold" data-col="id" data-val="<?= htmlspecialchars($id) ?>"><?= htmlspecialchars($id) ?></td>
<td class="text-nowrap" data-col="name" data-val="<?= htmlspecialchars($st['name']) ?>"><?= htmlspecialchars($st['name']) ?></td>
<td class="text-center" data-col="status" data-val="<?= $statusVal ?>">
<?php if (!$hasLogin): ?>
<span class="badge bg-light text-dark border">未登入</span>
<?php elseif ($hasLogout): ?>
<span class="badge bg-secondary">已離場</span>
<?php else: ?>
<span class="badge bg-success">作答中</span>
<?php endif; ?>
<?php if (!empty($st['readonly'])): ?>
<br><span class="badge bg-danger mt-1" style="font-size:.65em;"><i class="bi bi-lock-fill"></i> 唯讀</span>
<?php endif; ?>
</td>
<td class="small text-nowrap" data-col="first_login" data-val="<?= $st['first_login'] ?? '' ?>">
<?= $st['first_login'] ? substr($st['first_login'], 5, 11) : '—' ?>
</td>
<td class="small text-nowrap" data-col="last_act" data-val="<?= $lastActVal ?>">
<?php if ($hasLogout): ?>
<i class="bi bi-box-arrow-right text-secondary" title="已登出"></i>
<?= substr($st['last_logout'], 5, 11) ?>
<?php elseif ($st['last_time']): ?>
<?= substr($st['last_time'], 5, 11) ?>
<?php else: ?>
—
<?php endif; ?>
</td>
<td class="small text-nowrap" data-col="duration" data-val="<?= $st['duration_sec'] ?? -1 ?>"><?= format_duration($st['duration_sec']) ?></td>
<td data-col="pct" data-val="<?= $pct ?>">
<div class="d-flex align-items-center gap-1">
<span class="small text-nowrap"><?= $st['answered'] ?>/<?= $st['total'] ?></span>
<div class="progress flex-grow-1" style="height:8px;min-width:50px;">
<div class="progress-bar <?= $barClass ?>" style="width:<?= $pct ?>%"></div>
</div>
<span class="small text-muted text-nowrap"><?= $pct ?>%</span>
</div>
</td>
<td class="text-center" data-col="score" data-val="<?= $st['score'] ?? -1 ?>">
<?php if ($st['score'] !== null): ?>
<span class="badge bg-warning text-dark fs-6 px-2"><?= $st['score'] ?></span>
<?php else: ?>
<span class="text-muted">—</span>
<?php endif; ?>
</td>
<td class="text-center fw-semibold" data-col="grader" data-val="<?= htmlspecialchars($st['grader_surname']) ?>">
<?= $st['grader_surname'] !== '' ? htmlspecialchars($st['grader_surname']) : '<span class="text-muted">—</span>' ?>
</td>
<td style="min-width:120px;max-width:160px;white-space:normal;">
<?php foreach ($st['keys'] as $k => $done): ?>
<span class="badge <?= $done ? 'bg-success' : 'bg-danger' ?> me-1 mb-1"
style="font-size:.65em;" title="第 <?= $k ?> 題"><?= $k ?></span>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div><!-- /accordion -->
<!-- PHP 環境資訊(直接展開,無手風琴) -->
<div class="card shadow-sm mb-4">
<div class="card-header fw-semibold">
<i class="bi bi-cpu me-2"></i>PHP 環境 / 上傳限制
</div>
<div class="card-body">
<div class="row g-3">
<div class="col-md-4">
<div class="small text-muted">PHP 版本</div>
<strong><?= phpversion() ?></strong>
</div>
<div class="col-md-4">
<div class="small text-muted">upload_max_filesize</div>
<strong><?= ini_get('upload_max_filesize') ?></strong>
<?php if ((int)ini_get('upload_max_filesize') < $config['max_file_size_mb']): ?>
<span class="badge bg-warning text-dark ms-1">小於 config 設定</span>
<?php endif; ?>
</div>
<div class="col-md-4">
<div class="small text-muted">post_max_size</div>
<strong><?= ini_get('post_max_size') ?></strong>
</div>
<div class="col-md-4">
<div class="small text-muted">Web 伺服器帳號</div>
<strong><?= htmlspecialchars(get_current_user() ?: '(無法取得)') ?></strong>
</div>
<div class="col-md-4">
<div class="small text-muted">config.json answer_path</div>
<strong><?= htmlspecialchars($config['answer_path'] ?: '(未設定,使用預設 answers/)') ?></strong>
</div>
<div class="col-md-4">
<div class="small text-muted">config max_file_size_mb</div>
<strong><?= $config['max_file_size_mb'] ?> MB</strong>
</div>
</div>
</div>
</div>
<div class="d-flex justify-content-center gap-2 flex-wrap">
<a href="grade.php" class="btn btn-warning">
<i class="bi bi-pen-fill me-1"></i>批改管理
</a>
<a href="questions.php" class="btn btn-primary">
<i class="bi bi-pencil-square me-1"></i>出題管理
</a>
<a href="index.php" class="btn btn-outline-secondary">回考試登入頁</a>
</div>
<?php endif; ?>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<?php if ($authed): ?>
<script>
// 即時時鐘
function updateClock() {
const now = new Date();
const pad = n => String(n).padStart(2, '0');
const t = `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
const el = document.getElementById('live-time');
if (el) el.textContent = t;
}
setInterval(updateClock, 1000);
// 60 秒自動重新整理(取得最新作答狀態)
let countdown = 60;
setInterval(() => {
countdown--;
if (countdown <= 0) location.reload();
}, 1000);
// 考生作答現況表格排序
(function () {
const table = document.querySelector('#acc-status table');
if (!table) return;
const thead = table.querySelector('thead tr');
const tbody = table.querySelector('tbody');
let sortCol = null, sortDir = 1;
thead.querySelectorAll('th[data-sort]').forEach(th => {
th.addEventListener('click', () => {
const col = th.dataset.sort;
sortDir = (sortCol === col) ? -sortDir : 1;
sortCol = col;
// 更新所有表頭圖示
thead.querySelectorAll('th[data-sort]').forEach(h => {
h.classList.remove('asc', 'desc');
h.querySelector('.sort-icon').className = 'bi bi-chevron-expand sort-icon';
});
th.classList.add(sortDir === 1 ? 'asc' : 'desc');
th.querySelector('.sort-icon').className =
'bi bi-chevron-' + (sortDir === 1 ? 'up' : 'down') + ' sort-icon';
// 排序列
Array.from(tbody.querySelectorAll('tr'))
.sort((a, b) => {
const av = a.querySelector('td[data-col="' + col + '"]')?.dataset.val ?? '';
const bv = b.querySelector('td[data-col="' + col + '"]')?.dataset.val ?? '';
const an = parseFloat(av), bn = parseFloat(bv);
if (!isNaN(an) && !isNaN(bn)) return (an - bn) * sortDir;
return av.localeCompare(bv, 'zh-TW') * sortDir;
})
.forEach(r => tbody.appendChild(r));
});
});
})();
</script>
<?php endif; ?>
</body>
</html>