From f5b88251910da11a3d6893bf4a8d1d5ba4117a7d Mon Sep 17 00:00:00 2001 From: Ali Date: Fri, 17 May 2024 14:04:13 +0800 Subject: [PATCH] Individual Needs: Transplanting SQL queries into gateway classes --- modules/Individual Needs/in_edit.php | 21 +++++------- modules/Individual Needs/in_editProcess.php | 33 ++++++++----------- .../in_edit_assistant_deleteProcess.php | 7 ++-- modules/Individual Needs/in_summary.php | 15 +++++---- .../IndividualNeeds/INArchiveGateway.php | 8 +++++ src/Domain/IndividualNeeds/INGateway.php | 8 +++++ src/Domain/System/AlertLevelGateway.php | 8 +++++ 7 files changed, 58 insertions(+), 42 deletions(-) diff --git a/modules/Individual Needs/in_edit.php b/modules/Individual Needs/in_edit.php index 8289f2aff8..9d10b0402f 100644 --- a/modules/Individual Needs/in_edit.php +++ b/modules/Individual Needs/in_edit.php @@ -24,10 +24,13 @@ use Gibbon\Domain\DataSet; use Gibbon\Services\Format; use Gibbon\Tables\DataTable; +use Gibbon\Domain\User\UserGateway; use Gibbon\Forms\CustomFieldHandler; use Gibbon\Forms\DatabaseFormFactory; use Gibbon\Domain\System\SettingGateway; +use Gibbon\Domain\IndividualNeeds\INGateway; use Gibbon\Domain\System\CustomFieldGateway; +use Gibbon\Domain\IndividualNeeds\INArchiveGateway; use Gibbon\Domain\IndividualNeeds\INAssistantGateway; //Module includes @@ -58,16 +61,12 @@ ->add(__('Edit Individual Needs Record')); } + $result = $container->get(UserGateway::class)->getUserDetails($gibbonPersonID, $session->get('gibbonSchoolYearID')); - $data = array('gibbonSchoolYearID' => $session->get('gibbonSchoolYearID'), 'gibbonPersonID' => $gibbonPersonID); - $sql = "SELECT gibbonPerson.gibbonPersonID, gibbonStudentEnrolmentID, surname, preferredName, gibbonYearGroup.name AS yearGroup, gibbonFormGroup.nameShort AS formGroup, dateStart, dateEnd, image_240 FROM gibbonPerson, gibbonStudentEnrolment, gibbonYearGroup, gibbonFormGroup WHERE (gibbonPerson.gibbonPersonID=gibbonStudentEnrolment.gibbonPersonID) AND (gibbonStudentEnrolment.gibbonYearGroupID=gibbonYearGroup.gibbonYearGroupID) AND (gibbonStudentEnrolment.gibbonFormGroupID=gibbonFormGroup.gibbonFormGroupID) AND gibbonFormGroup.gibbonSchoolYearID=:gibbonSchoolYearID AND gibbonPerson.gibbonPersonID=:gibbonPersonID AND gibbonPerson.status='Full' ORDER BY surname, preferredName"; - $result = $connection2->prepare($sql); - $result->execute($data); - - if ($result->rowCount() != 1) { + if (empty($result)) { $page->addError(__('The selected record does not exist, or you do not have access to it.')); } else { - $student = $result->fetch(); + $student = $result; $search = $_GET['search'] ?? null; $allStudents = $_GET['allStudents'] ?? null; @@ -98,15 +97,11 @@ $educationalAssistants = $container->get(INAssistantGateway::class)->selectINAssistantsByStudent($gibbonPersonID)->fetchAll(); // Grab IEP data - $data = array('gibbonPersonID' => $gibbonPersonID); - $sql = "SELECT * FROM gibbonIN WHERE gibbonPersonID=:gibbonPersonID"; - $result = $pdo->executeQuery($data, $sql); + $result = $container->get(INGateway::class)->selectBy(['gibbonPersonID' => $gibbonPersonID]); $IEP = ($result->rowCount() > 0)? $result->fetch() : array(); // Grab archived data - $data = array('gibbonPersonID' => $gibbonPersonID); - $sql = "SELECT gibbonINArchiveID as groupBy, gibbonINArchive.* FROM gibbonINArchive WHERE gibbonPersonID=:gibbonPersonID ORDER BY archiveTimestamp DESC"; - $result = $pdo->executeQuery($data, $sql); + $result = $container->get(INArchiveGateway::class)->selectINArchivesByPersonID($gibbonPersonID); $archivedIEPs = ($result->rowCount() > 0)? $result->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE) : array(); $gibbonINArchiveID = !empty($_POST['gibbonINArchiveID'])? $_POST['gibbonINArchiveID'] : ''; diff --git a/modules/Individual Needs/in_editProcess.php b/modules/Individual Needs/in_editProcess.php index 4c59d46aea..26a1d7b53e 100644 --- a/modules/Individual Needs/in_editProcess.php +++ b/modules/Individual Needs/in_editProcess.php @@ -19,10 +19,13 @@ along with this program. If not, see . */ +use Gibbon\Data\Validator; use Gibbon\Services\Format; use Gibbon\Comms\NotificationEvent; +use Gibbon\Domain\User\UserGateway; use Gibbon\Forms\CustomFieldHandler; -use Gibbon\Data\Validator; +use Gibbon\Domain\IndividualNeeds\INGateway; +use Gibbon\Domain\IndividualNeeds\INAssistantGateway; require_once '../../gibbon.php'; @@ -50,22 +53,20 @@ } else { //Check access to specified student try { - $data = array('gibbonSchoolYearID' => $session->get('gibbonSchoolYearID'), 'gibbonPersonID' => $gibbonPersonID); - $sql = "SELECT gibbonPerson.gibbonPersonID, gibbonStudentEnrolmentID, surname, preferredName, gibbonYearGroup.nameShort AS yearGroup, gibbonFormGroup.nameShort AS formGroup, dateStart, dateEnd, gibbonYearGroup.gibbonYearGroupID FROM gibbonPerson, gibbonStudentEnrolment, gibbonYearGroup, gibbonFormGroup WHERE (gibbonPerson.gibbonPersonID=gibbonStudentEnrolment.gibbonPersonID) AND (gibbonStudentEnrolment.gibbonYearGroupID=gibbonYearGroup.gibbonYearGroupID) AND (gibbonStudentEnrolment.gibbonFormGroupID=gibbonFormGroup.gibbonFormGroupID) AND gibbonFormGroup.gibbonSchoolYearID=:gibbonSchoolYearID AND gibbonPerson.gibbonPersonID=:gibbonPersonID AND gibbonPerson.status='Full' ORDER BY surname, preferredName"; - $result = $connection2->prepare($sql); - $result->execute($data); + $result = $container->get(UserGateway::class)->getUserDetails($gibbonPersonID, $session->get('gibbonSchoolYearID')); + } catch (PDOException $e) { $URL .= '&return=error2'; header("Location: {$URL}"); exit(); } - if ($result->rowCount() != 1) { + if (empty($result)) { $URL .= '&return=error1'; header("Location: {$URL}"); } else { $partialFail = false; - $row = $result->fetch(); + $row = $result; if ($highestAction == 'Individual Needs Records_viewEdit') { //UPDATE STATUS @@ -101,10 +102,8 @@ $fields = $container->get(CustomFieldHandler::class)->getFieldDataFromPOST('Individual Needs', [], $customRequireFail); try { - $data = array('gibbonPersonID' => $gibbonPersonID); - $sql = 'SELECT * FROM gibbonIN WHERE gibbonPersonID=:gibbonPersonID'; - $result = $connection2->prepare($sql); - $result->execute($data); + $result = $container->get(INGateway::class)->selectBy(['gibbonPersonID' => $gibbonPersonID]); + } catch (PDOException $e) { $partialFail = true; } @@ -135,10 +134,8 @@ foreach ($staff as $t) { //Check to see if person is already registered as an assistant try { - $dataGuest = array('gibbonPersonIDAssistant' => $t, 'gibbonPersonIDStudent' => $gibbonPersonID); - $sqlGuest = 'SELECT * FROM gibbonINAssistant WHERE gibbonPersonIDAssistant=:gibbonPersonIDAssistant AND gibbonPersonIDStudent=:gibbonPersonIDStudent'; - $resultGuest = $connection2->prepare($sqlGuest); - $resultGuest->execute($dataGuest); + $resultGuest = $container->get(INAssistantGateway::class)->selectBy(['gibbonPersonIDAssistant' => $t, 'gibbonPersonIDStudent' => $gibbonPersonID]); + } catch (PDOException $e) { $partialFail = true; } @@ -158,10 +155,8 @@ //UPDATE IEP $strategies = $_POST['strategies'] ?? ''; try { - $data = array('gibbonPersonID' => $gibbonPersonID); - $sql = 'SELECT * FROM gibbonIN WHERE gibbonPersonID=:gibbonPersonID'; - $result = $connection2->prepare($sql); - $result->execute($data); + $result = $container->get(INGateway::class)->selectBy(['gibbonPersonID' => $gibbonPersonID]); + } catch (PDOException $e) { $partialFail = true; } diff --git a/modules/Individual Needs/in_edit_assistant_deleteProcess.php b/modules/Individual Needs/in_edit_assistant_deleteProcess.php index 8531917f40..6533cad5c9 100644 --- a/modules/Individual Needs/in_edit_assistant_deleteProcess.php +++ b/modules/Individual Needs/in_edit_assistant_deleteProcess.php @@ -19,6 +19,8 @@ along with this program. If not, see . */ +use Gibbon\Domain\IndividualNeeds\INAssistantGateway; + include '../../gibbon.php'; $gibbonPersonIDAssistant = $_GET['gibbonPersonIDAssistant'] ?? ''; @@ -43,10 +45,7 @@ header("Location: {$URL}"); } else { try { - $data = array('gibbonPersonIDStudent' => $gibbonPersonIDStudent, 'gibbonPersonIDAssistant' => $gibbonPersonIDAssistant); - $sql = 'SELECT * FROM gibbonINAssistant WHERE gibbonPersonIDStudent=:gibbonPersonIDStudent AND gibbonPersonIDAssistant=:gibbonPersonIDAssistant'; - $result = $connection2->prepare($sql); - $result->execute($data); + $result = $container->get(INAssistantGateway::class)->selectBy(['gibbonPersonIDStudent' => $gibbonPersonIDStudent, 'gibbonPersonIDAssistant' => $gibbonPersonIDAssistant]); } catch (PDOException $e) { $URL .= '&return=error2'; header("Location: {$URL}"); diff --git a/modules/Individual Needs/in_summary.php b/modules/Individual Needs/in_summary.php index 0ac3ac9d09..a3b43848f1 100644 --- a/modules/Individual Needs/in_summary.php +++ b/modules/Individual Needs/in_summary.php @@ -20,9 +20,10 @@ */ use Gibbon\Forms\Form; -use Gibbon\Forms\DatabaseFormFactory; -use Gibbon\Tables\DataTable; use Gibbon\Services\Format; +use Gibbon\Tables\DataTable; +use Gibbon\Forms\DatabaseFormFactory; +use Gibbon\Domain\System\AlertLevelGateway; use Gibbon\Domain\IndividualNeeds\INGateway; //Module includes @@ -63,15 +64,17 @@ $form->addHiddenValue('address', $session->get('address')); //SELECT FROM ARRAY - $sql = "SELECT gibbonINDescriptorID as value, name FROM gibbonINDescriptor ORDER BY sequenceNumber"; + $result = $container->get(INGateway::class)->selecINDescriptor(); + $row = $form->addRow(); $row->addLabel('gibbonINDescriptorID', __('Descriptor')); - $row->addSelect('gibbonINDescriptorID')->fromQuery($pdo, $sql)->selected($gibbonINDescriptorID)->placeholder(); + $row->addSelect('gibbonINDescriptorID')->fromResults($result)->selected($gibbonINDescriptorID)->placeholder(); + + $result = $container->get(AlertLevelGateway::class)->selectAlertLevels(); - $sql = "SELECT gibbonAlertLevelID as value, name FROM gibbonAlertLevel ORDER BY sequenceNumber"; $row = $form->addRow(); $row->addLabel('gibbonAlertLevelID', __('Alert Level')); - $row->addSelect('gibbonAlertLevelID')->fromQuery($pdo, $sql)->selected($gibbonAlertLevelID)->placeholder(); + $row->addSelect('gibbonAlertLevelID')->fromResults($result)->selected($gibbonAlertLevelID)->placeholder(); $row = $form->addRow(); $row->addLabel('gibbonFormGroupID', __('Form Group')); diff --git a/src/Domain/IndividualNeeds/INArchiveGateway.php b/src/Domain/IndividualNeeds/INArchiveGateway.php index edf0c5c7b7..62ef2ca4c9 100644 --- a/src/Domain/IndividualNeeds/INArchiveGateway.php +++ b/src/Domain/IndividualNeeds/INArchiveGateway.php @@ -45,5 +45,13 @@ class INArchiveGateway extends QueryableGateway implements ScrubbableGateway private static $scrubbableKey = 'gibbonPersonID'; private static $scrubbableColumns = ['strategies' => '','targets' => '','notes' => '','descriptors' => '']; + + public function selectINArchivesByPersonID($gibbonPersonID) + { + $data = ['gibbonPersonID' => $gibbonPersonID]; + $sql = "SELECT gibbonINArchiveID as groupBy, gibbonINArchive.* FROM gibbonINArchive WHERE gibbonPersonID=:gibbonPersonID ORDER BY archiveTimestamp DESC"; + + return $this->db()->select($sql, $data); + } } diff --git a/src/Domain/IndividualNeeds/INGateway.php b/src/Domain/IndividualNeeds/INGateway.php index 5dda4aec8c..eaa61abcf3 100644 --- a/src/Domain/IndividualNeeds/INGateway.php +++ b/src/Domain/IndividualNeeds/INGateway.php @@ -235,4 +235,12 @@ public function getINStudentByPersonID($gibbonPersonID) return $this->db()->select($sql, $data); } + + public function selecINDescriptor() + { + $data = []; + $sql = "SELECT gibbonINDescriptorID as value, name FROM gibbonINDescriptor ORDER BY sequenceNumber"; + + return $this->db()->select($sql, $data); + } } diff --git a/src/Domain/System/AlertLevelGateway.php b/src/Domain/System/AlertLevelGateway.php index 35561b2892..5430228d96 100644 --- a/src/Domain/System/AlertLevelGateway.php +++ b/src/Domain/System/AlertLevelGateway.php @@ -75,4 +75,12 @@ public function getByID(int $gibbonAlertLevelID, bool $translated = true) ] : $row; } + + public function selectAlertLevels() + { + $data= []; + $sql = "SELECT gibbonAlertLevelID as value, name FROM gibbonAlertLevel ORDER BY sequenceNumber"; + + return $this->db()->select($sql, $data); + } }