Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 287
StudentSpecializationService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 13
4160.00
0.00% covered (danger)
0.00%
0 / 287
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 saveSpecialization
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 32
 validateSaveSpecialization
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 3
 insertSpecialization
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 updateSpecialization
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 13
 deleteSpecialization
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 23
 getAllSpecialization
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 21
 getAllSpecializationStudents
0.00% covered (danger)
0.00%
0 / 1
90.00
0.00% covered (danger)
0.00%
0 / 37
 getStudentspecialization
0.00% covered (danger)
0.00%
0 / 1
210.00
0.00% covered (danger)
0.00%
0 / 44
 saveStudentSpecialization
0.00% covered (danger)
0.00%
0 / 1
132.00
0.00% covered (danger)
0.00%
0 / 20
 insertStudentSpecialization
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 20
 deleteStudentSpecialization
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 18
 getStudentBatchSpecializationByRequest
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 42
<?php
namespace com\linways\ec\core\service;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\base\util\SecurityUtils;
use com\linways\ec\core\constant\StatusConstants;
use com\linways\ec\core\exception\ExamControllerException;
use com\linways\ec\core\dto\StudentSpecialization;
use com\linways\ec\core\logging\Events;
use com\linways\ec\core\logging\entities\Staff;
use com\linways\core\ams\professional\logging\AMSLogger;
use com\linways\ec\core\service\CommonExamService;
class StudentSpecializationService extends BaseService
{
    use MakeSingletonTrait;
    private function __construct()
    {
        $this->logger = AMSLogger::getLogger('exam-controller-log');
    }
    /**
     * Save Student specialization
     * @param StudentSpecialization $specialization
     * @return $id
     */
    public function saveSpecialization(StudentSpecialization $specialization){
        $specialization = $this->realEscapeObject($specialization);
        $staffId = $GLOBALS['userId'];
        try {
            $this->validateSaveSpecialization($specialization);
            if (!empty($specialization->id)) {
                $specialization->id = $this->updateSpecialization($specialization);
            } else {
                $specialization->id = $this->insertSpecialization($specialization);
            }
            $this->logger->info(Events::EC_SAVE_STUDENT_SPECIALIZATION, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $specialization,
                "status" => StatusConstants::SUCCESS
            ]);
        } catch (\Exception $e) {
            $this->logger->error(Events::EC_SAVE_STUDENT_SPECIALIZATION, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $specialization,
                "errorCode" => $e->getCode(),
                "errorMessage" => $e->getMessage(),
                "status" => StatusConstants::FAILED
            ]);
            if ($e->getCode() !== ExamControllerException::INVALID_PARAMETERS && $e->getCode() !== ExamControllerException::EMPTY_PARAMETERS && $e->getCode() !== "DUPLICATE_ENTRY") {
                throw new ExamControllerException($e->getCode(), "Failed to save specialization! Please try again");
            } else if ($e->getCode() === ExamControllerException::DUPLICATE_ENTRY) {
                throw new ExamControllerException(ExamControllerException::DUPLICATE_ENTRY, "Cannot create specialization.This specialization already created");
            } else {
                throw new ExamControllerException($e->getCode(), $e->getMessage());
            }
        }
        return $specialization->id;
    }
    /**
     * Validate Specialization  Before Saving
     * @param StudentSpecialization $specialization
     * @return NULL
     */
    private function validateSaveSpecialization(StudentSpecialization $specialization){
        if (empty($specialization->name))
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, " specialization name is  empty! Please fill name ");
    }
    /**
     * Insert Specialization
     * @param StudentSpecialization $specialization
     * @return  $id
     */
    private function insertSpecialization(StudentSpecialization $specialization){
        $query = "INSERT INTO specialisation_master
                  (specialisationName)
                  VALUES
                  ('$specialization->name')";
        try {
            $specialization->id = $this->executeQuery($query,true)->id;
            return $specialization->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Update Specialization
     * @param StudentSpecialization $specialization
     * @return $specialization->id
     */
    private function updateSpecialization(StudentSpecialization $specialization){
        $query = "UPDATE
                    specialisation_master
                SET
                    specialisationName = '$specialization->name'
                WHERE
                    id = '$specialization->id'";
        try {
            $this->executeQuery($query);
            return $specialization->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
     /**
     * Delete specialization
     * @param String $id
     * @return NULL
     */
    public function deleteSpecialization($id){
        $id = $this->realEscapeString($id);
        $staffId = $GLOBALS['userId'];
        $specialization = new \stdClass();
        $specialization->id = $id;
        $currentSpecializationDetails = reset($this->getAllSpecialization($specialization));
        if(empty($currentSpecializationDetails)){
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't delete ! specialization missing.");
        }
        $query = "DELETE FROM
                    specialisation_master
                WHERE
                id = '$id'";
        try {
            $this->executeQuery($query);
             // LOGGING
            $this->logger->info(Events::EC_DELETE_STUDENT_SPECIALIZATION,[
                "staff" => new Staff(["id" => $staffId]),
                "request" => $currentSpecializationDetails,
                "status" => StatusConstants::SUCCESS
            ]);
        }catch (\Exception $e) {
            throw new ExamControllerException(ExamControllerException::HAVE_RELATION,"Can't delete ! students already assigned !");
        }
    }
    /**
     * get Specialization
     * @param $searchRequest
     * @return $specializations
     */
    public function getAllSpecialization($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
        try {
            $whereQuery = "";
            if (!empty($searchRequest->id)) {
                $specializationIdStr = is_array($searchRequest->id) ? "'" . implode("','", $searchRequest->id) . "'" : "'" . $searchRequest->id . "'";
                $whereQuery .= " AND sm.id IN ( $specializationIdStr )";
            }
            $query = "SELECT
                            DISTINCT 
                            sm.id as id,
                            sm.specialisationName as name
                        FROM
                            specialisation_master sm
                        WHERE
                            1 = 1 ";
            $specializations =  $this->executeQueryForList($query . $whereQuery);
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        return $specializations;
    }
     /**
     * get All Students for specialization
     * @param $searchRequest
     * @return $students
     */
    public function getAllSpecializationStudents($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
        try {
            $students = [];
            if (empty($searchRequest->groupId) || empty($searchRequest->programId) || empty($searchRequest->academicTermId)){
                throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, " Empty parameter. Please choose batch, program and semester");
            }
            $reguestForBatchStudents = new \stdClass();
            $reguestForBatchStudents->groupId = $searchRequest->groupId;
            $reguestForBatchStudents->programId = $searchRequest->programId;
            $students = CommonExamService::getInstance()->getStudentsDetailsByBatchProgram($reguestForBatchStudents);
            if(empty($students)){
                throw new ExamControllerException(ExamControllerException::NO_DETAILS_FOUND,"No students found in this batch");
            }
            $reguestForSpecialization = new \stdClass();
            $specializations = $this->getAllSpecialization($reguestForSpecialization);
            if(empty($specializations)){
                throw new ExamControllerException(ExamControllerException::NO_DETAILS_FOUND,"No specialization found.");
            }
            foreach($students as $student){
                $student->isSelected = false;
                $student->academicTermId = $searchRequest->academicTermId;
                $reguestForStudentSpecialization = new \stdClass();
                $reguestForStudentSpecialization->studentId = $student->id;
                $reguestForStudentSpecialization->groupId = $student->groupId;
                $reguestForStudentSpecialization->programId = $student->programId;
                $reguestForStudentSpecialization->academicTermId = $searchRequest->academicTermId;
                $specialization = $this->getStudentspecialization($reguestForStudentSpecialization);
                $student->specializationId = $specialization->id;
                $student->specializationName = $specialization->name;
                $student->specializationMapId = $specialization->specializationMapId;
                $student->isAlreadyAssigned =  $student->specializationId ? true : false;
            }
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        return $students;
    }
    /**
     * get Student Specialization
     * @param $searchRequest
     * @return $specializations
     */
    public function getStudentspecialization($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
        try {
            $whereQuery = "";
            if (!empty($searchRequest->specialisationId)) {
                $specializationIdStr = is_array($searchRequest->specialisationId) ? "'" . implode("','", $searchRequest->specialisationId) . "'" : "'" . $searchRequest->specialisationId . "'";
                $whereQuery .= " AND essm.specialisation_id IN ( $specializationIdStr )";
            }
            if (!empty($searchRequest->studentId)) {
                $studentIdStr = is_array($searchRequest->studentId) ? "'" . implode("','", $searchRequest->studentId) . "'" : "'" . $searchRequest->studentId . "'";
                $whereQuery .= " AND essm.student_id IN ( $studentIdStr )";
            }
            if (!empty($searchRequest->groupId)) {
                $groupIdStr = is_array($searchRequest->groupId) ? "'" . implode("','", $searchRequest->groupId) . "'" : "'" . $searchRequest->groupId . "'";
                $whereQuery .= " AND essm.groups_id IN ( $groupIdStr )";
            }
            if (!empty($searchRequest->programId)) {
                $programIdStr = is_array($searchRequest->programId) ? "'" . implode("','", $searchRequest->programId) . "'" : "'" . $searchRequest->programId . "'";
                $whereQuery .= " AND essm.program_id IN ( $programIdStr )";
            }
            if (!empty($searchRequest->academicTermId)) {
                $academicTermIdStr = is_array($searchRequest->academicTermId) ? "'" . implode("','", $searchRequest->academicTermId) . "'" : "'" . $searchRequest->academicTermId . "'";
                $whereQuery .= " AND essm.academic_term_id IN ( $academicTermIdStr )";
            }
            if (!empty($searchRequest->id)) {
                $specializationMapIdStr = is_array($searchRequest->id) ? "'" . implode("','", $searchRequest->id) . "'" : "'" . $searchRequest->id . "'";
                $whereQuery .= " AND essm.id IN ( $specializationMapIdStr )";
            }
            $query = "SELECT
                            DISTINCT 
                            essm.id as specializationMapId,
                            essm.specialisation_id as id,
                            sm.specialisationName as name
                        FROM
                            ec_student_specialization_maping essm
                        LEFT JOIN specialisation_master sm ON
                            sm.id = essm.specialisation_id
                        WHERE
                            1 = 1 ";
            $specializations =  $this->executeQueryForObject($query . $whereQuery);
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        return $specializations;
    }
     /**
     * Save Student specialization Maping
     * @param $saveStudentSpecialization
     * @return $id
     */
    public function saveStudentSpecialization($saveStudentSpecialization){
        $saveStudentSpecialization = $this->realEscapeObject($saveStudentSpecialization);
        $staffId = $GLOBALS['userId'];
        $saveStudentSpecialization->createdBy = $staffId;
        $saveStudentSpecialization->updatedBy = $staffId;
        try {
            if (empty($saveStudentSpecialization->groupId) || empty($saveStudentSpecialization->programId) || empty($saveStudentSpecialization->academicTermId) || empty($saveStudentSpecialization->studentId) || empty($saveStudentSpecialization->specializationId)){
                throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, " Empty parameter! error saving");
            }
            $saveStudentSpecialization->id = $this->insertStudentSpecialization($saveStudentSpecialization);
        } catch (\Exception $e) {
            
            if ($e->getCode() !== ExamControllerException::INVALID_PARAMETERS && $e->getCode() !== ExamControllerException::EMPTY_PARAMETERS && $e->getCode() !== "DUPLICATE_ENTRY") {
                throw new ExamControllerException($e->getCode(), "Failed to save student specialization! Please try again");
            } else if ($e->getCode() === ExamControllerException::DUPLICATE_ENTRY) {
                throw new ExamControllerException(ExamControllerException::DUPLICATE_ENTRY, "Cannot create specialization.This specialization already created");
            } else {
                throw new ExamControllerException($e->getCode(), $e->getMessage());
            }
        }
        return $saveStudentSpecialization->id;
    }
    /**
     * Insert Student Specialization maping
     * @param $saveStudentSpecialization
     * @return  $id
     */
    private function insertStudentSpecialization($saveStudentSpecialization){
        $query = "INSERT INTO 
                    ec_student_specialization_maping 
                        (student_id,
                        specialisation_id,
                        groups_id,
                        academic_term_id,
                        program_id,
                        created_by,
                        created_date) 
                    VALUES ('$saveStudentSpecialization->studentId','$saveStudentSpecialization->specializationId',
                    '$saveStudentSpecialization->groupId','$saveStudentSpecialization->academicTermId',
                    '$saveStudentSpecialization->programId','$saveStudentSpecialization->createdBy',now()) 
                    ON DUPLICATE KEY UPDATE specialisation_id = VALUES(specialisation_id),updated_by = VALUES(created_by)";
        try {
            $saveStudentSpecialization->id = $this->executeQuery($query,true)->id;
            return $saveStudentSpecialization->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
     /**
     * Delete student specialization maping
     * @param String $id
     * @return NULL
     */
    public function deleteStudentSpecialization($id){
        $id = $this->realEscapeString($id);
        $staffId = $GLOBALS['userId'];
        $specialization = new \stdClass();
        $specialization->id = $id;
        $currentSpecializationDetails = $this->getStudentspecialization($specialization);
        if(empty($currentSpecializationDetails)){
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't delete ! specialization missing.");
        }
        $query = "DELETE FROM
                    ec_student_specialization_maping
                WHERE
                id = '$id'";
        try {
            $this->executeQuery($query);
        }catch (\Exception $e) {
            throw new ExamControllerException(ExamControllerException::HAVE_RELATION,"Can't delete ! students already assigned !");
        }
    }
     /**
     * get Student Specialization of a batch
     * @param $searchRequest
     * @return $specializations
     */
    public function getStudentBatchSpecializationByRequest($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
        try {
            $whereQuery = "";
            if (!empty($searchRequest->groupId)) {
                $groupIdStr = is_array($searchRequest->groupId) ? "'" . implode("','", $searchRequest->groupId) . "'" : "'" . $searchRequest->groupId . "'";
                $whereQuery .= " AND g.id IN ( $groupIdStr )";
            }
           
            $groupBy = ' GROUP BY spa.id, sm.id';
            $orderBy = " ORDER BY spa.properties ->> '$.registerNumber' ASC";
            $query = "SELECT 
                s.studentID as id,
                g.id AS groupId,
                g.name AS groupName,
                g.properties ->>'$.programId'  AS programId,
                spa.student_id AS studentId,
                spa.properties->>'$.rollNumber' AS studentRollNo,
                IF(IFNULL(spa.properties ->> '$.registerNumber', 'null') = 'null','',spa.properties ->> '$.registerNumber') AS studentRegisterNo,
                s.studentName AS studentName,
                s.studentPhone,
                sm.specialisationName AS specialisation
            FROM 
                studentaccount s 
            INNER JOIN student_program_account spa
                ON spa.student_id = s.studentID
            INNER JOIN student_program_batch_log spbl 
                ON spbl.program_student_id = spa.id AND spbl.properties->>'$.academicStatus' IN ('ACTIVE','COMPLETED')
            INNER JOIN `groups` g 
                ON g.id = spbl.batch_group_id
            INNER JOIN group_members gm ON
                gm.groups_id = g.id AND 
                gm.members->>'$.studentId' = spa.id
            LEFT JOIN ec_student_specialization_maping essm ON 
                essm.student_id = s.studentID
            LEFT JOIN specialisation_master sm ON
                sm.id = essm.specialisation_id
            WHERE 1=1  AND spa.academic_status IN ('ACTIVE', 'COMPLETED')";
            $specializations =  $this->executeQueryForList($query . $whereQuery. $groupBy. $orderBy);
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        return $specializations;
    }
    
}