54 lines
1.9 KiB
PHP
54 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Database\Migrations;
|
|
|
|
use CodeIgniter\Database\Migration;
|
|
|
|
class AddSchoolYearToAssessmentForms extends Migration
|
|
{
|
|
public function up(): void
|
|
{
|
|
if (! $this->db->tableExists('assessment_forms')) return;
|
|
|
|
if (! $this->db->fieldExists('school_year', 'assessment_forms')) {
|
|
$this->forge->addColumn('assessment_forms', [
|
|
'school_year' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 9,
|
|
'null' => true,
|
|
'after' => 'pool_id',
|
|
],
|
|
]);
|
|
}
|
|
|
|
$schoolYear = $this->activeSchoolYear();
|
|
if ($schoolYear !== null) {
|
|
$this->db->table('assessment_forms')
|
|
->groupStart()->where('school_year', null)->orWhere('school_year', '')->groupEnd()
|
|
->update(['school_year' => $schoolYear]);
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
if ($this->db->tableExists('assessment_forms') && $this->db->fieldExists('school_year', 'assessment_forms')) {
|
|
$this->forge->dropColumn('assessment_forms', 'school_year');
|
|
}
|
|
}
|
|
|
|
private function activeSchoolYear(): ?string
|
|
{
|
|
if ($this->db->tableExists('school_years')) {
|
|
$row = $this->db->table('school_years')->select('name')->where('status', 'active')->orderBy('id', 'DESC')->get()->getRowArray();
|
|
$name = trim((string) ($row['name'] ?? ''));
|
|
if (preg_match('/^\d{4}-\d{4}$/', $name)) return $name;
|
|
}
|
|
if ($this->db->tableExists('configuration')) {
|
|
$row = $this->db->table('configuration')->select('config_value')->where('config_key', 'school_year')->orderBy('id', 'DESC')->get()->getRowArray();
|
|
$name = trim((string) ($row['config_value'] ?? ''));
|
|
if (preg_match('/^\d{4}-\d{4}$/', $name)) return $name;
|
|
}
|
|
return null;
|
|
}
|
|
}
|