Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ v31.0.00
Activities: display overlapping calendar events next to student names on Activity Attendance page
Admissions: added a progress bar for admissions milestones to Manage Applications
Admissions: added a warning to Accept Application when milestones have not been completed
Admissions: improved the logic to automatically assign a house based on siblings or parents
Attendance: student attendance history now ordered by timetable period
Attendance: fixed the Consecutive Absences check to only count the final status for each day
Attendance: added a foreign table field to attendance logs for recording target of future absences
Expand Down
73 changes: 66 additions & 7 deletions src/Forms/Builder/Process/AssignHouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
namespace Gibbon\Forms\Builder\Process;

use Gibbon\Contracts\Services\Session;
use Gibbon\Domain\School\HouseGateway;
use Gibbon\Domain\User\FamilyGateway;
use Gibbon\Domain\User\RoleGateway;
use Gibbon\Domain\User\UserGateway;
use Gibbon\Forms\Builder\AbstractFormProcess;
use Gibbon\Forms\Builder\FormBuilderInterface;
use Gibbon\Forms\Builder\Storage\FormDataInterface;
use Gibbon\Forms\Builder\View\AssignHouseView;
use Gibbon\Domain\School\HouseGateway;
use Gibbon\Domain\User\UserGateway;

class AssignHouse extends AbstractFormProcess implements ViewableProcess
{
Expand All @@ -36,12 +38,16 @@ class AssignHouse extends AbstractFormProcess implements ViewableProcess
private $session;
private $userGateway;
private $houseGateway;
private $familyGateway;
private $roleGateway;

public function __construct(Session $session, UserGateway $userGateway, HouseGateway $houseGateway)
public function __construct(Session $session, UserGateway $userGateway, HouseGateway $houseGateway, FamilyGateway $familyGateway, RoleGateway $roleGateway)
{
$this->session = $session;
$this->userGateway = $userGateway;
$this->houseGateway = $houseGateway;
$this->familyGateway = $familyGateway;
$this->roleGateway = $roleGateway;
}

public function getViewClass() : string
Expand All @@ -58,15 +64,68 @@ public function process(FormBuilderInterface $builder, FormDataInterface $formDa
{
if (!$formData->has('gibbonPersonIDStudent')) return;

// Get pseudo-randomly assigned house
$assignedHouse = $this->houseGateway->selectAssignedHouseByGender($this->session->get('gibbonSchoolYearIDCurrent'), $formData->get('gibbonYearGroupIDEntry'), $formData->get('gender'))->fetch();
$assignedHouse = null;
$gibbonFamilyID = $formData->get('gibbonFamilyID');
$gibbonPersonIDStudent = $formData->get('gibbonPersonIDStudent');

// Try family-based house assignment if family ID exists
if (!empty($gibbonFamilyID)) {

// Check parents first
$adults = $this->familyGateway->selectAdultsByFamily($gibbonFamilyID, true);
foreach ($adults as $adult) {
// Check if parent is staff and has a house assignment
if ($adult['status'] == 'Full' && !empty($adult['gibbonHouseID'])) {
// Staff members have roles in Staff category
$roleCategory = $this->roleGateway->getRoleCategory($adult['gibbonRoleIDPrimary']);
if ($roleCategory == 'Staff') {
$house = $this->houseGateway->getByID($adult['gibbonHouseID']);
if (!empty($house)) {
$assignedHouse = [
'gibbonHouseID' => $adult['gibbonHouseID'],
'house' => $house['name'],
];
break;
}
}
}
}

// If no staff parent found, check enrolled siblings
if (empty($assignedHouse)) {
$children = $this->familyGateway->selectChildrenByFamily($gibbonFamilyID, true);
foreach ($children as $child) {
// Skip the current student
if ($child['gibbonPersonID'] == $gibbonPersonIDStudent) {
continue;
}

// Check if sibling is enrolled and has a house
if ($child['status'] == 'Full' && !empty($child['gibbonHouseID'])) {
$house = $this->houseGateway->getByID($child['gibbonHouseID']);
if (!empty($house)) {
$assignedHouse = [
'gibbonHouseID' => $child['gibbonHouseID'],
'house' => $house['name'],
];
break;
}
}
}
}
}

// Fallback to gender-based assignment if no family assignment found
if (empty($assignedHouse)) {
$assignedHouse = $this->houseGateway->selectAssignedHouseByGender($this->session->get('gibbonSchoolYearIDCurrent'), $formData->get('gibbonYearGroupIDEntry'), $formData->get('gender'))->fetch();
}

if (empty($assignedHouse)) return;

$formData->set('gibbonHouseID', $assignedHouse['gibbonHouseID']);

// Update the user data for this student
$this->userGateway->update($formData->has('gibbonPersonIDStudent'), [
$this->userGateway->update($gibbonPersonIDStudent, [
'gibbonHouseID' => $formData->get('gibbonHouseID'),
]);

Expand All @@ -77,7 +136,7 @@ public function rollback(FormBuilderInterface $builder, FormDataInterface $formD
{
if (!$formData->has('gibbonPersonIDStudent')) return;

$this->userGateway->update($formData->has('gibbonPersonIDStudent'), [
$this->userGateway->update($formData->get('gibbonPersonIDStudent'), [
'gibbonHouseID' => null,
]);

Expand Down
Loading