add new ststs feature
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 51s
Tests / PHPUnit (push) Successful in 1m25s

This commit is contained in:
root
2026-09-13 02:20:14 -04:00
parent 1787144f27
commit c3a30989b2
16 changed files with 1395 additions and 84 deletions
@@ -0,0 +1,92 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class AddAcademicStatisticsNavItem extends Migration
{
private string $url = 'administrator/stats';
public function up(): void
{
if (! $this->db->tableExists('nav_items')) {
return;
}
$existing = $this->db->table('nav_items')->where('url', $this->url)->get()->getRowArray();
if ($existing !== null) {
return;
}
$parentColumn = $this->parentColumn();
$parentBuilder = $this->db->table('nav_items')->where('label', 'Student-Affairs');
if ($parentColumn !== null) {
$parentBuilder->where($parentColumn, null);
}
$parent = $parentBuilder->get()->getRowArray();
$insert = [
'label' => 'School Statistics',
'url' => $this->url,
'sort_order' => 14,
'is_enabled' => 1,
'created_at' => date('Y-m-d H:i:s'),
];
if ($parentColumn !== null) {
$insert[$parentColumn] = $parent['id'] ?? null;
}
$this->db->table('nav_items')->insert($insert);
$navItemId = (int) $this->db->insertID();
$this->grantToRoles($navItemId, ['administrator', 'admin', 'principal', 'vice_principal', 'administrative staff', 'head of department (education)']);
}
public function down(): void
{
if (! $this->db->tableExists('nav_items')) {
return;
}
$row = $this->db->table('nav_items')->where('url', $this->url)->get()->getRowArray();
if ($row === null) {
return;
}
if ($this->db->tableExists('role_nav_items')) {
$this->db->table('role_nav_items')->where('nav_item_id', (int) $row['id'])->delete();
}
$this->db->table('nav_items')->where('id', (int) $row['id'])->delete();
}
private function grantToRoles(int $navItemId, array $roles): void
{
if ($navItemId <= 0 || ! $this->db->tableExists('role_nav_items')) {
return;
}
foreach ($roles as $role) {
$insert = ['nav_item_id' => $navItemId, 'created_at' => date('Y-m-d H:i:s')];
if ($this->db->fieldExists('role_id', 'role_nav_items') && $this->db->tableExists('roles')) {
$roleRow = $this->db->table('roles')->select('id')->where('LOWER(name)', strtolower($role))->get()->getRowArray();
if ($roleRow === null) {
continue;
}
$insert['role_id'] = (int) $roleRow['id'];
} elseif ($this->db->fieldExists('role', 'role_nav_items')) {
$insert['role'] = $role;
} else {
continue;
}
$this->db->table('role_nav_items')->insert($insert);
}
}
private function parentColumn(): ?string
{
if ($this->db->fieldExists('parent_id', 'nav_items')) {
return 'parent_id';
}
return $this->db->fieldExists('menu_parent_id', 'nav_items') ? 'menu_parent_id' : null;
}
}
@@ -0,0 +1,64 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class RenameAndGrantSchoolStatisticsNavItem extends Migration
{
private string $url = 'administrator/stats';
public function up(): void
{
$navItemId = $this->rename('School Statistics');
if ($navItemId <= 0 || ! $this->db->tableExists('role_nav_items')) {
return;
}
foreach (['administrator', 'admin', 'principal', 'vice_principal', 'administrative staff', 'head of department (education)'] as $role) {
$roleRow = $this->db->table('roles')
->select('id')
->where('LOWER(name)', strtolower($role))
->get()
->getRowArray();
if ($roleRow === null) {
continue;
}
$roleId = (int) $roleRow['id'];
$exists = $this->db->table('role_nav_items')
->where('role_id', $roleId)
->where('nav_item_id', $navItemId)
->countAllResults() > 0;
if (! $exists) {
$this->db->table('role_nav_items')->insert([
'role_id' => $roleId,
'nav_item_id' => $navItemId,
'created_at' => date('Y-m-d H:i:s'),
]);
}
}
}
public function down(): void
{
$this->rename('Academic Statistics');
}
private function rename(string $label): int
{
if (! $this->db->tableExists('nav_items')) {
return 0;
}
$row = $this->db->table('nav_items')->select('id')->where('url', $this->url)->get()->getRowArray();
if ($row === null) {
return 0;
}
$navItemId = (int) $row['id'];
$this->db->table('nav_items')->where('id', $navItemId)->update(['label' => $label]);
return $navItemId;
}
}