147 lines
4.4 KiB
PHP
147 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\EnrollmentCollectionV1DashboardModel;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
/**
|
|
* Enrollment Collection V1 KPI API (Metabase SQL as model methods).
|
|
*/
|
|
class EnrollmentCollectionV1DashboardController extends BaseController
|
|
{
|
|
use ResponseTrait;
|
|
|
|
protected function resolvePolicyId(?int $fallback = null): int
|
|
{
|
|
$id = (int) (
|
|
$this->request->getGet('client_policy')
|
|
?? $this->request->getGet('client_policy_id')
|
|
?? $this->request->getPost('client_policy')
|
|
?? $this->request->getPost('client_policy_id')
|
|
?? 0
|
|
);
|
|
|
|
if ($id > 0) {
|
|
return $id;
|
|
}
|
|
|
|
if ($fallback !== null && $fallback > 0) {
|
|
return $fallback;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
protected function resolveKpiMethod(string $kpiKey): ?string
|
|
{
|
|
$kpiKey = trim($kpiKey);
|
|
if ($kpiKey === '') {
|
|
return null;
|
|
}
|
|
|
|
if (in_array($kpiKey, EnrollmentCollectionV1DashboardModel::KPI_MAP, true)) {
|
|
return $kpiKey;
|
|
}
|
|
|
|
if (ctype_digit($kpiKey)) {
|
|
$id = (int) $kpiKey;
|
|
|
|
return EnrollmentCollectionV1DashboardModel::KPI_MAP[$id] ?? null;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function kpi(string $kpiMethod = '')
|
|
{
|
|
$policyId = $this->resolvePolicyId();
|
|
|
|
if ($policyId <= 0) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'message' => 'client_policy or client_policy_id is required.',
|
|
], 422);
|
|
}
|
|
|
|
$kpiMethod = $this->resolveKpiMethod($kpiMethod);
|
|
if ($kpiMethod === null) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'message' => 'Unknown KPI. Pass Metabase id or method slug.',
|
|
'allowed' => EnrollmentCollectionV1DashboardModel::KPI_MAP,
|
|
], 404);
|
|
}
|
|
|
|
$model = new EnrollmentCollectionV1DashboardModel();
|
|
$metabaseId = array_search($kpiMethod, EnrollmentCollectionV1DashboardModel::KPI_MAP, true);
|
|
|
|
return $this->respond([
|
|
'status' => true,
|
|
'policy_id' => $policyId,
|
|
'kpi_id' => $metabaseId !== false ? (int) $metabaseId : null,
|
|
'kpi' => $kpiMethod,
|
|
'label' => EnrollmentCollectionV1DashboardModel::KPI_LABELS[$kpiMethod] ?? $kpiMethod,
|
|
'rows' => $model->getKpi($kpiMethod, $policyId),
|
|
]);
|
|
}
|
|
|
|
public function all()
|
|
{
|
|
$policyId = $this->resolvePolicyId();
|
|
|
|
if ($policyId <= 0) {
|
|
return $this->respond([
|
|
'status' => false,
|
|
'message' => 'client_policy or client_policy_id is required.',
|
|
], 422);
|
|
}
|
|
|
|
$model = new EnrollmentCollectionV1DashboardModel();
|
|
|
|
return $this->respond([
|
|
'status' => true,
|
|
'policy_id' => $policyId,
|
|
'data' => $model->getAllKpis($policyId),
|
|
]);
|
|
}
|
|
|
|
public function preview(int $policyId = 4687)
|
|
{
|
|
$policyId = $this->resolvePolicyId($policyId);
|
|
$path = $this->request->getUri()->getPath();
|
|
$isJwt = stripos($path, 'employeeRest') !== false;
|
|
$prefix = $isJwt ? 'employeeRest/enrollment-collection-v1' : 'util/enrollment-collection-v1';
|
|
|
|
return view('enrollment_collection_v1_dashboard', [
|
|
'policy_id' => $policyId,
|
|
'kpi_map' => EnrollmentCollectionV1DashboardModel::KPI_MAP,
|
|
'kpi_labels' => EnrollmentCollectionV1DashboardModel::KPI_LABELS,
|
|
'api_all_url' => base_url($prefix . '/all'),
|
|
'api_kpi_url' => base_url($prefix . '/kpi'),
|
|
]);
|
|
}
|
|
|
|
public function debug(int $policyId = 4687)
|
|
{
|
|
$policyId = $this->resolvePolicyId($policyId);
|
|
|
|
if ($policyId <= 0) {
|
|
return $this->response
|
|
->setStatusCode(422)
|
|
->setBody('client_policy or client_policy_id is required.');
|
|
}
|
|
|
|
$model = new EnrollmentCollectionV1DashboardModel();
|
|
$body = json_encode([
|
|
'status' => true,
|
|
'policy_id' => $policyId,
|
|
'data' => $model->getAllKpis($policyId),
|
|
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
|
|
|
return $this->response
|
|
->setHeader('Content-Type', 'application/json; charset=UTF-8')
|
|
->setBody($body);
|
|
}
|
|
}
|