fix registration enrollment of students
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 48s
Tests / PHPUnit (push) Successful in 1m19s

This commit is contained in:
root
2026-08-26 21:36:03 -04:00
parent a354d78fa9
commit 9676261d65
7 changed files with 208 additions and 53 deletions
+97 -23
View File
@@ -281,7 +281,11 @@ $enrollableCount = 0;
$withdrawableCount = 0;
foreach (($students ?? []) as $student) {
$evaluation = is_array($student['transition_evaluation'] ?? null) ? $student['transition_evaluation'] : [];
if (($evaluation['can_enroll'] ?? false) === true && !$deadlinePassed && $isEditable) {
if (
(($evaluation['can_enroll'] ?? false) === true || ($evaluation['parent_enrollment_allowed'] ?? false) === true)
&& !$deadlinePassed
&& $isEditable
) {
$enrollableCount++;
}
if (($student['enrollment_status'] ?? '') === 'enrolled' && $isEditable) {
@@ -386,7 +390,7 @@ $studentCount = count($students ?? []);
$evaluation = is_array($student['transition_evaluation'] ?? null) ? $student['transition_evaluation'] : [];
if ($hasSettledEnrollmentStatus($student['enrollment_status'] ?? '', $student['admission_status'] ?? '')) {
echo '<span class="text-muted small">' . esc($alreadyEnrolledMessage((string) ($student['enrollment_status'] ?? ''))) . '</span>';
} elseif (($evaluation['can_enroll'] ?? false) === true) {
} elseif (($evaluation['can_enroll'] ?? false) === true || ($evaluation['parent_enrollment_allowed'] ?? false) === true) {
echo '<span class="text-success small">Eligible</span>';
} elseif (! empty($evaluation['primary_parent_message'])) {
echo '<span class="text-danger small">' . esc($evaluation['primary_parent_message']) . '</span>';
@@ -510,13 +514,20 @@ $studentCount = count($students ?? []);
$evaluation = is_array($student['transition_evaluation'] ?? null) ? $student['transition_evaluation'] : [];
$studentName = trim((string) ($student['firstname'] ?? '') . ' ' . (string) ($student['lastname'] ?? ''));
$studentName = $studentName !== '' ? $studentName : 'Student';
$canEnroll = ($evaluation['can_enroll'] ?? false) === true
$studentId = (int) ($student['id'] ?? 0);
$isPreselectedStudent = in_array($studentId, array_map('intval', $preselectedStudentIds ?? []), true);
$canEnroll = (
($evaluation['can_enroll'] ?? false) === true
|| ($evaluation['parent_enrollment_allowed'] ?? false) === true
)
&& !$deadlinePassed
&& $isEditable;
$hasSettledEnrollment = $hasSettledEnrollmentStatus($student['enrollment_status'] ?? '', $student['admission_status'] ?? '');
$blockMessage = $hasSettledEnrollment
$blockMessage = $canEnroll
? ''
: ($hasSettledEnrollment
? $alreadyEnrolledMessage((string) ($student['enrollment_status'] ?? ''))
: (string) ($evaluation['primary_parent_message'] ?? $eligibilityMessage['message'] ?? '');
: (string) ($evaluation['primary_parent_message'] ?? $eligibilityMessage['message'] ?? ''));
$blockTitle = $hasSettledEnrollment
? $alreadyEnrolledTitle((string) ($student['enrollment_status'] ?? ''))
: 'Enrollment Not Available';
@@ -547,6 +558,8 @@ $studentCount = count($students ?? []);
<div class="student-select-card <?= $canEnroll ? '' : 'is-disabled' ?>"
data-student-card
data-student-id="<?= esc($student['id']) ?>"
data-student-name="<?= esc($studentName) ?>"
data-requested-student="<?= $isPreselectedStudent ? '1' : '0' ?>"
data-school-id="<?= esc($student['school_id'] ?? 'N/A') ?>"
data-current-grade="<?= esc($gradeLabel) ?>"
data-required-action="<?= esc($student['required_action_label'] ?? 'Contact administration') ?>"
@@ -935,6 +948,11 @@ $studentCount = count($students ?? []);
const selectedEnrollInputs = () => form ? Array.from(form.querySelectorAll("input[name='enroll[]']:checked")) : [];
const selectedWithdrawInputs = () => form ? Array.from(form.querySelectorAll("input[name='withdraw[]']:checked")) : [];
const studentCards = () => Array.from(document.querySelectorAll('[data-student-card]'));
const requestedStudentCards = () => {
const requested = studentCards().filter(card => card.dataset.requestedStudent === '1');
return requested.length ? requested : studentCards();
};
function setStep(step) {
currentStep = Math.max(0, Math.min(finalStep, step));
@@ -1214,7 +1232,8 @@ $studentCount = count($students ?? []);
function selectStudentsForEnrollment(ids) {
const wanted = Array.isArray(ids) ? ids.map(Number).filter(id => id > 0) : [];
document.querySelectorAll('[data-student-card]').forEach(card => {
const cards = wanted.length ? requestedStudentCards() : studentCards();
cards.forEach(card => {
if (card.dataset.selectable !== '1') {
return;
}
@@ -1340,7 +1359,7 @@ $studentCount = count($students ?? []);
return true;
}
document.querySelectorAll('[data-student-card]').forEach(card => {
studentCards().forEach(card => {
card.querySelectorAll('[data-health-group]').forEach(group => {
group.addEventListener('change', function(event) {
const target = event.target;
@@ -1372,17 +1391,41 @@ $studentCount = count($students ?? []);
function showBlockModal(message, options = {}) {
const alreadyEnrolled = options.alreadyEnrolled === true;
const warning = options.warning === true;
if (blockModalTitle) {
blockModalTitle.textContent = options.title || (alreadyEnrolled ? 'Already Enrolled' : 'Enrollment Not Available');
}
if (blockModalHeader) {
blockModalHeader.classList.toggle('bg-danger', !alreadyEnrolled);
blockModalHeader.classList.toggle('bg-success', alreadyEnrolled);
blockModalHeader.classList.toggle('bg-danger', !alreadyEnrolled && !warning);
blockModalHeader.classList.toggle('bg-success', alreadyEnrolled && !warning);
blockModalHeader.classList.toggle('bg-warning', warning);
blockModalHeader.classList.toggle('text-dark', warning);
blockModalHeader.classList.toggle('text-white', !warning);
}
if (blockModalBody) {
blockModalBody.textContent = message || (alreadyEnrolled
? 'This student is already enrolled for the selected school year.'
: 'Enrollment cannot continue at this time. Please contact school administration.');
const messages = Array.isArray(options.messages) ? options.messages.filter(Boolean) : [];
if (messages.length > 1) {
const list = document.createElement('ul');
list.className = 'mb-0 ps-4';
messages.forEach(item => {
const li = document.createElement('li');
const separatorIndex = item.indexOf(': ');
if (separatorIndex > 0) {
const name = document.createElement('strong');
name.textContent = item.substring(0, separatorIndex);
li.appendChild(name);
li.appendChild(document.createTextNode(item.substring(separatorIndex)));
} else {
li.textContent = item;
}
list.appendChild(li);
});
blockModalBody.replaceChildren(list);
} else {
blockModalBody.textContent = messages[0] || message || (alreadyEnrolled
? 'This student is already enrolled for the selected school year.'
: 'Enrollment cannot continue at this time. Please contact school administration.');
}
}
if (blockModal) {
blockModal.show();
@@ -1400,6 +1443,24 @@ $studentCount = count($students ?? []);
};
}
function blockMessagesForCards(cards) {
return cards
.map(card => {
const studentId = String(card.dataset.studentId || '');
const row = latestEligibility[studentId] || {};
const message = row.primary_parent_message
|| (row.blocking_rule_codes || []).join(', ')
|| card.dataset.blockMessage
|| '';
if (!message) {
return '';
}
const name = row.student_name || card.dataset.studentName || 'Student';
return name + ': ' + message;
})
.filter(Boolean);
}
async function refreshEligibility() {
const response = await fetch(eligibilityRefreshUrl, {
headers: { 'X-Requested-With': 'XMLHttpRequest' },
@@ -1426,7 +1487,7 @@ $studentCount = count($students ?? []);
function applyEligibilityToCards() {
let shouldReload = false;
document.querySelectorAll('[data-student-card]').forEach(card => {
studentCards().forEach(card => {
const studentId = String(card.dataset.studentId || '');
const row = latestEligibility[studentId];
if (!row) {
@@ -1434,7 +1495,7 @@ $studentCount = count($students ?? []);
}
const wasBlocked = card.dataset.selectable !== '1';
const canEnroll = row.can_enroll === true && !deadlinePassed;
const canEnroll = (row.can_enroll === true || row.parent_enrollment_allowed === true) && !deadlinePassed;
card.dataset.selectable = canEnroll ? '1' : '0';
card.classList.toggle('is-disabled', !canEnroll);
if (row.primary_parent_message) {
@@ -1468,19 +1529,29 @@ $studentCount = count($students ?? []);
return;
}
const eligibleCards = Array.from(document.querySelectorAll('[data-student-card]')).filter(card => {
const cardsToConsider = requestedStudentCards();
const eligibleCards = cardsToConsider.filter(card => {
const studentId = String(card.dataset.studentId || '');
return latestEligibility[studentId]?.can_enroll === true;
const row = latestEligibility[studentId] || {};
return row.can_enroll === true || row.parent_enrollment_allowed === true;
});
if (eligibleCards.length === 0) {
const firstBlocked = Object.values(latestEligibility).find(row => row.primary_parent_message || (row.blocking_rule_codes || []).length > 0);
const otherBlockedCards = studentCards().filter(card => !cardsToConsider.includes(card));
const blockedMessages = blockMessagesForCards(cardsToConsider)
.concat(blockMessagesForCards(otherBlockedCards));
const firstBlocked = cardsToConsider
.map(card => latestEligibility[String(card.dataset.studentId || '')])
.find(row => row && (row.primary_parent_message || (row.blocking_rule_codes || []).length > 0))
|| Object.values(latestEligibility).find(row => row.primary_parent_message || (row.blocking_rule_codes || []).length > 0);
const alreadyEnrolled = firstBlocked?.decision === 'ALREADY_ENROLLED';
const blockReason = firstBlocked?.primary_parent_message
|| (firstBlocked?.blocking_rule_codes || []).join(', ')
|| 'No students are currently eligible for enrollment.';
showBlockModal(blockReason, {
alreadyEnrolled,
messages: blockedMessages,
warning: alreadyEnrolled && blockedMessages.length > 1,
title: firstBlocked?.block_title || (alreadyEnrolled ? 'Already Enrolled' : 'Enrollment Not Available'),
});
return;
@@ -1491,7 +1562,7 @@ $studentCount = count($students ?? []);
});
}
document.querySelectorAll('[data-student-card]').forEach(card => {
studentCards().forEach(card => {
card.addEventListener('click', async function(event) {
if (event.target.closest('input, select, textarea, label')) {
return;
@@ -1645,11 +1716,14 @@ $studentCount = count($students ?? []);
});
});
if (enrollmentStartStep >= 2 && startButton && !startButton.disabled && !<?= $flashError ? 'true' : 'false' ?>) {
openEnrollmentAtStep(enrollmentStartStep - 1);
}
refreshEligibility().catch(() => {
refreshEligibility().then(() => {
if (enrollmentStartStep >= 2 && startButton && !startButton.disabled && !<?= $flashError ? 'true' : 'false' ?>) {
openEnrollmentAtStep(enrollmentStartStep - 1);
}
}).catch(() => {
if (enrollmentStartStep >= 2 && startButton && !startButton.disabled && !<?= $flashError ? 'true' : 'false' ?>) {
openEnrollmentAtStep(enrollmentStartStep - 1);
}
// Keep server-rendered eligibility if refresh fails.
});
});