Files
alrahma_sunday_school/app/Database/Migrations/2026-09-10-000100_SeedNewStudentAssessmentQuestions.php
T
root b5e719382d
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 49s
Tests / PHPUnit (push) Successful in 1m36s
add assessment feature
2026-09-10 06:44:24 -04:00

130 lines
4.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class SeedNewStudentAssessmentQuestions extends Migration
{
private const POOL_NAME = 'New Student Assessment';
private const QUESTIONS = [
[
'type' => 'essay',
'text' => 'What is the students Islamic education background?',
],
[
'type' => 'short_answer',
'text' => 'What is the students Arabic competency level?',
],
[
'type' => 'short_answer',
'text' => 'What is the students English competency level?',
],
[
'type' => 'essay',
'text' => 'Has the student attended any Sunday/Islamic school in the past? If YES, which one?',
],
[
'type' => 'essay',
'text' => 'Has the student memorized any Surahs? If yes, which one(s)?',
],
[
'type' => 'essay',
'text' => 'Why have the parents chosen AlRahma Sunday School for their child(ren)?',
],
[
'type' => 'essay',
'text' => 'Any serious/special medical condition about the student which we have to be aware of?',
],
[
'type' => 'essay',
'text' => 'If surnames of enrolled students are different, please confirm if they are all siblings; if NOT, then please specify their relationship to each other:',
],
[
'type' => 'essay',
'text' => 'Any other questions / concerns that the parents may have?',
],
];
public function up(): void
{
if (! $this->db->tableExists('question_pools') || ! $this->db->tableExists('assessment_questions')) {
return;
}
$pool = $this->db->table('question_pools')
->select('id')
->where('name', self::POOL_NAME)
->get()
->getRowArray();
if ($pool === null) {
$now = date('Y-m-d H:i:s');
$this->db->table('question_pools')->insert([
'name' => self::POOL_NAME,
'subject_tag' => 'New Student Intake',
'created_by' => null,
'created_at' => $now,
'updated_at' => $now,
]);
$poolId = (int) $this->db->insertID();
} else {
$poolId = (int) $pool['id'];
}
if ($poolId <= 0) return;
$now = date('Y-m-d H:i:s');
$maxOrder = $this->db->table('assessment_questions')->selectMax('order_index')->where('pool_id', $poolId)->get()->getRowArray();
$nextOrder = ((int) ($maxOrder['order_index'] ?? 0)) + 1;
foreach (self::QUESTIONS as $question) {
$exists = $this->db->table('assessment_questions')
->where('pool_id', $poolId)
->where('text', $question['text'])
->countAllResults() > 0;
if ($exists) continue;
$this->db->table('assessment_questions')->insert([
'pool_id' => $poolId,
'type' => $question['type'],
'text' => $question['text'],
'options' => null,
'correct_answer' => null,
'points' => 0,
'order_index' => $nextOrder++,
'created_at' => $now,
'updated_at' => $now,
]);
}
}
public function down(): void
{
if (! $this->db->tableExists('question_pools') || ! $this->db->tableExists('assessment_questions')) {
return;
}
$pool = $this->db->table('question_pools')->select('id')->where('name', self::POOL_NAME)->get()->getRowArray();
if ($pool === null) return;
$poolId = (int) $pool['id'];
foreach (array_column(self::QUESTIONS, 'text') as $text) {
$question = $this->db->table('assessment_questions')->select('id')->where(['pool_id' => $poolId, 'text' => $text])->get()->getRowArray();
if ($question === null) continue;
if ($this->db->tableExists('assessment_form_questions')
&& $this->db->table('assessment_form_questions')->where('question_id', (int) $question['id'])->countAllResults() > 0) {
continue;
}
$this->db->table('assessment_questions')->where('id', (int) $question['id'])->delete();
}
$hasQuestions = $this->db->table('assessment_questions')->where('pool_id', $poolId)->countAllResults() > 0;
$hasForms = $this->db->tableExists('assessment_forms')
&& $this->db->table('assessment_forms')->where('pool_id', $poolId)->countAllResults() > 0;
if (! $hasQuestions && ! $hasForms) {
$this->db->table('question_pools')->where('id', $poolId)->delete();
}
}
}