tinyqc/app/Controllers/Api/DashboardApiController.php
mahdahar 14baa6b758 docs: add comprehensive documentation and refactor API structure
This commit introduces a complete documentation suite, refactors the API layer for
better consistency, and updates the database schema and seeding logic.

Key Changes:
- Documentation:
  - Added `CLAUDE.md` with development guidelines and architecture overview.
  - Created `docs/` directory with detailed guides for architecture, development,
    and source tree analysis.
- Database & Migrations:
  - Implemented `RenameMasterColumns` migration to standardize column naming
    (e.g., `name` -> `dept_name`, `name` -> `control_name`).
  - Added `CmodQcSeeder` to populate the system with realistic sample data
    for depts, controls, tests, and results.
- Backend API:
  - Created `DashboardApiController` with `getRecent()` for dashboard stats.
  - Created `ReportApiController` for managed reporting access.
  - Updated `app/Config/Routes.php` with new API groupings and documentation routes.
- Frontend & Views:
  - Refactored master data views (`dept`, `test`, `control`) to use Alpine.js
    and the updated API structure.
  - Modernized `dashboard.php` and `main_layout.php` with improved UI/UX.
- Infrastructure:
  - Updated `.gitignore` to exclude development-specific artifacts (`_bmad/`, `.claude/`).
2026-01-20 14:44:46 +07:00

86 lines
2.9 KiB
PHP

<?php
namespace App\Controllers\Api;
use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;
use App\Models\Qc\ResultsModel;
use App\Models\Qc\ControlTestsModel;
class DashboardApiController extends BaseController
{
use ResponseTrait;
protected $resultModel;
protected $controlTestModel;
public function __construct()
{
$this->resultModel = new ResultsModel();
$this->controlTestModel = new ControlTestsModel();
}
public function getRecent()
{
try {
$limit = $this->request->getGet('limit') ?? 10;
$builder = $this->resultModel->db->table('results r');
$builder->select('
r.result_id as id,
r.res_date,
r.res_value,
r.created_at,
c.control_name as controlName,
t.test_name as testName,
ct.mean,
ct.sd
');
$builder->join('master_controls c', 'c.control_id = r.control_id');
$builder->join('master_tests t', 't.test_id = r.test_id');
$builder->join('control_tests ct', 'ct.control_id = r.control_id AND ct.test_id = r.test_id', 'left');
$builder->where('r.deleted_at', null);
$builder->orderBy('r.created_at', 'DESC');
$builder->limit((int) $limit);
$results = $builder->get()->getResultArray();
// Calculate QC status for each result
$data = [];
foreach ($results as $row) {
$inRange = false;
$rangeDisplay = 'N/A';
if (!empty($row['mean']) && !empty($row['sd']) && $row['res_value'] !== null) {
$lower = $row['mean'] - (2 * $row['sd']);
$upper = $row['mean'] + (2 * $row['sd']);
$resValue = (float) $row['res_value'];
$inRange = ($resValue >= $lower && $resValue <= $upper);
$rangeDisplay = number_format($lower, 2) . ' - ' . number_format($upper, 2);
}
$data[] = [
'id' => $row['id'],
'resDate' => $row['res_date'],
'resValue' => $row['res_value'],
'createdAt' => $row['created_at'],
'controlName' => $row['controlName'],
'testName' => $row['testName'],
'mean' => $row['mean'],
'sd' => $row['sd'],
'inRange' => $inRange,
'rangeDisplay' => $rangeDisplay
];
}
return $this->respond([
'status' => 'success',
'message' => 'fetch success',
'data' => $data
], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}