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 / 33
CRAP
0.00% covered (danger)
0.00%
0 / 486
CBSService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 33
9120.00
0.00% covered (danger)
0.00%
0 / 486
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 getAllApplications
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 22
 getAllCBSApplication
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 19
 createCBSApplication
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 17
 assignCBSApplicationSemesters
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 16
 updateCBSApplication
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 17
 addCBSAppliedBatches
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 addSubjectsToCBS
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 15
 fetchNumberOfSeatsAllottedForPseudoSubject
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 18
 fetchNumberOfStudentsAppliedForPseudoSubject
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 12
 getCountOfAppliedCBSSubjects
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 getStudentAppliedCBSGroups
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getOpenCourseDetailsById
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getAllCoursesOfCBSApplication
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 30
 deleteCBSApplicationIfStudentNotRegistered
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 18
 deleteCBSApplicationById
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 deleteCBSApplicationBatches
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 deleteApplicationSubjectSeatsByApplicationId
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 fetchCountStudentsRegisteredForCBSApplication
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 13
 getApplicationDetails
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 33
 getAllCBSSubjectsIdsByApplicationId
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 14
 deleteSubjectFromCBS
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 12
 getCBSBatches
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 15
 deleteCBSBatch
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 13
 batchWiseOptedStudentsCount
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 18
 isDeletable
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 13
 getAllottedCoursesOfStudent
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 22
 getCbsApplicationDetails
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 31
 getCBSSemesters
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 updateCBSApplicationSemesters
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 removeCBSApplicationSemester
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\core\ams\professional\constant\PseudoSubjectType;
use com\linways\core\ams\professional\dto\CBSApplication;
use com\linways\core\ams\professional\dto\PseudoSubject;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\mapper\CBSServiceMapper;
use com\linways\core\ams\professional\request\AddCBSBatchRequest;
use com\linways\core\ams\professional\request\CreateCBSRequest;
use com\linways\core\ams\professional\request\UpdateCBSRequest;
use com\linways\core\ams\professional\request\UpdateCBSSemestersRequest;
class CBSService extends BaseService
{
    private static $_instance = null;
    private $mapper = [];
    // /Condition 2 - Locked down the constructor
    private function __construct()
    {
        $this->mapper = CBSServiceMapper::getInstance()->getMapper();
    }
    // Prevent any oustide instantiation of this class
    // /Condition 3 - Prevent any object or instance of that class to be cloned
    private function __clone()
    {
    }
    // Prevent any copy of this object
    // /Condition 4 - Have a single globally accessible static method
    public static function getInstance()
    {
        if (!is_object(self::$_instance)) // or if( is_null(self::$_instance) ) or if( self::$_instance == null )
            self::$_instance = new self();
        return self::$_instance;
    }
    /**
     * @param $studentId
     * @param $applicationType
     * @return Object|CBSApplication
     * @throws ProfessionalException
     */
    public function getAllApplications($studentId, $applicationType = null)
    {
        $response = [];
        $sql = "SELECT oai.id,oai.applnEndDate,oai.applnStartDate,oai.opencourseName,pst.id as cbsTypeId,pst.name as cbsType,
                osa.id as applicationId,pst.code as cbsCode
                FROM opencourse_appln_initiate oai
                INNER JOIN cbs_application_batches cab on oai.id = cab.application_id
                INNER JOIN cbs_application_semesters cas on oai.id = cas.cbs_application_id
                INNER JOIN batches b on cab.batchId = b.batchID AND b.semID =cas.semester_id
                INNER JOIN studentaccount sa ON sa.batchID = cab.batchId
                LEFT JOIN pseudo_subject_type pst ON pst.id = oai.cbs_type_id
                LEFT JOIN opencourse_student_appln osa ON osa.opencourse_appln_initiate_id = oai.id AND osa.studentaccounts_id =sa.studentID
                WHERE sa.studentID=$studentId";
        if (!empty($applicationType)) {
            $sql .= " AND pst.code= '$applicationType'";
        }
        try {
            $response = $this->executeQueryForList($sql, $this->mapper[CBSServiceMapper::GET_ALL_APPLICATIONS_LIST_MAPPER]);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $response;
    }
    /**
     * @return array|Object
     * @throws ProfessionalException
     */
    public function getAllCBSApplication()
    {
        $response = [];
        $sql = "SELECT oai.id,DATE_FORMAT(oai.applnEndDate,'%d-%m-%Y %h:%i %p') as applnEndDate,
                DATE_FORMAT(oai.applnStartDate,'%d-%m-%Y %h:%i %p') as applnStartDate ,
                oai.opencourseName,pst.id as cbsTypeId,pst.name as cbsType, pst.code as cbsCode
                FROM opencourse_appln_initiate oai
                INNER JOIN cbs_application_batches cab on oai.id = cab.application_id
                LEFT JOIN batches b on cab.batchId = b.batchID AND b.semID =oai.semesters_id
                LEFT JOIN pseudo_subject_type pst ON pst.id = oai.cbs_type_id";
        if (!empty($applicationType)) {
            $sql .= " AND pst.code= '$applicationType'";
        }
        try {
            $response = $this->executeQueryForList($sql, $this->mapper[CBSServiceMapper::GET_ALL_APPLICATIONS_LIST_MAPPER]);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $response;
    }
    /**
     * @param CreateCBSRequest $request
     * @return Object
     * @throws ProfessionalException
     */
    public function createCBSApplication(CreateCBSRequest $request)
    {
        $request = $this->realEscapeObject($request);
        $sql = "INSERT INTO opencourse_appln_initiate (opencourseName, course_type_id, 
                applnStartDate, applnEndDate, considerPlusTwoMarks, 
                maximum_selections, is_grouped_subjects_enabled, cbs_type_id,exclude_subject_from_student_department) 
                VALUES ('$request->name',$request->courseTypeId,'$request->startDateTime',
                        '$request->endDateTime',$request->considerPlusTwoMarks,$request->maximumSelections,
                        $request->groupedSubjectsOnly,$request->cbsTypeId,$request->excludeSubjectsFromStudentDepartment)";
        try {
            $applicationId = $this->executeQueryForObject($sql, true);
            $request->applicationId = $applicationId;
            $this->assignCBSApplicationSemesters($request);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $applicationId;
    }
    /**
     * @param CreateCBSRequest $request
     * @throws ProfessionalException
     */
    public function assignCBSApplicationSemesters(CreateCBSRequest $request)
    {
        $request = $this->realEscapeObject($request);
        $sql = "INSERT INTO cbs_application_semesters (cbs_application_id, semester_id, created_by, created_date, 
                                       updated_by, updated_date) 
                                       VALUES ";
        foreach ($request->semesters as $semester) {
            $sql .= "($request->applicationId,$semester,$request->createdBy,UTC_TIMESTAMP(),$request->updatedBy,
            UTC_TIMESTAMP()),";
        }
        $sql = rtrim($sql, ',');
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param UpdateCBSRequest $request
     * @throws ProfessionalException
     */
    public function updateCBSApplication(UpdateCBSRequest $request)
    {
        $request = $this->realEscapeObject($request);
        if (empty($request->id)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "UPDATE opencourse_appln_initiate SET opencourseName='$request->name', course_type_id =$request->courseTypeId,
                applnStartDate='$request->startDateTime',applnEndDate='$request->endDateTime',
                considerPlusTwoMarks=$request->considerPlusTwoMarks,maximum_selections = $request->maximumSelections,
                is_grouped_subjects_enabled=$request->groupedSubjectsOnly,cbs_type_id=$request->cbsTypeId,
                exclude_subject_from_student_department = $request->excludeSubjectsFromStudentDepartment
                WHERE id = $request->id";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param AddCBSBatchRequest $request
     * @throws ProfessionalException
     */
    public function addCBSAppliedBatches(AddCBSBatchRequest $request)
    {
        $sql = "INSERT INTO cbs_application_batches (application_id, batchId, created_by, created_date) 
                VALUES ($request->applicationId,$request->batchId,$request->createdBy,UTC_TIMESTAMP())";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @param $subjectId
     * @param $seats
     * @throws ProfessionalException
     */
    public function addSubjectsToCBS($applicationId, $subjectId, $seats)
    {
        $applicationId = $this->realEscapeString($applicationId);
        $subjectId = $this->realEscapeString($subjectId);
        $seats = $this->realEscapeString($seats);
        if (empty($applicationId) || empty($subjectId) || empty($seats)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "INSERT INTO opencourse_subject_seats ( opencourse_appln_initiate_id, pseudosubjects_id, seats) 
            VALUES ($applicationId,$subjectId,$seats)";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * returns number of seats allotted for pseudo subject in an application
     * @param $applicationId
     * @param $pseudoSubjectId
     * @return mixed
     * @throws ProfessionalException
     */
    public function fetchNumberOfSeatsAllottedForPseudoSubject($applicationId, $pseudoSubjectId)
    {
        if (empty($applicationId) || empty($pseudoSubjectId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "SELECT COUNT(DISTINCT ps.studentID) as totalAllotedSeats  
                    FROM opencourse_student_appln osa
                    INNER JOIN cbs_application_batches cbs ON cbs.application_id = osa.opencourse_appln_initiate_id
                    INNER JOIN  batches b on cbs.batchId = b.batchID
                    INNER JOIN department d ON d.deptID = b.deptID
                    INNER JOIN studentaccount s on s.deptID =d.deptID
                    INNER JOIN pseudosubjects_students ps ON ps.pseudosubjectID =osa.pseudosubjects_id AND ps.studentID = s.studentID
                    WHERE opencourse_appln_initiate_id = $applicationId AND pseudosubjects_id =$pseudoSubjectId";
        try {
            return $this->executeQueryForObject($sql)->totalAllotedSeats;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @param $pseudoSubjectId
     * @return mixed
     * @throws ProfessionalException
     */
    public function fetchNumberOfStudentsAppliedForPseudoSubject($applicationId, $pseudoSubjectId)
    {
        if (empty($applicationId) || empty($pseudoSubjectId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "SELECT COUNT(id) as totalAllotedSeats   FROM opencourse_student_appln 
                WHERE opencourse_appln_initiate_id=$applicationId AND pseudosubjects_id=$pseudoSubjectId;";
        try {
            return $this->executeQueryForObject($sql)->totalAllotedSeats;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @param $student
     * @return mixed
     * @throws ProfessionalException
     */
    public function getCountOfAppliedCBSSubjects($applicationId, $student)
    {
        $sql = "SELECT count(id) as totalRecords FROM opencourse_student_appln 
                WHERE opencourse_appln_initiate_id =$applicationId AND studentaccounts_id=$student";
        try {
            return $this->executeQueryForObject($sql)->totalRecords;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @param $studentId
     * @return Object|array
     * @throws ProfessionalException
     */
    public function getStudentAppliedCBSGroups($applicationId, $studentId)
    {
        $sql = "SELECT DISTINCT ps.pseudo_subject_group_id as groupId FROM opencourse_student_appln osa 
               INNER JOIN pseudosubjects ps ON ps.pseudosubjectID = osa.pseudosubjects_id
               WHERE osa.opencourse_appln_initiate_id = $applicationId AND osa.studentaccounts_id  =$studentId";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $id
     * @return Object
     * @throws ProfessionalException
     */
    public function getOpenCourseDetailsById($id)
    {
        $sql = "SELECT id,opencourseName as name,course_type_id as courseTypeId,semesters_id as semesterId,applnStartDate, 
                applnEndDate,maximum_selections as maximumSelections,is_grouped_subjects_enabled as isSubjectGroupingEnabled,
                cbs_type_id as cbsTyeId
                FROM opencourse_appln_initiate WHERE id = $id";
        try {
            return $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $studentId
     * @param $openCourseApplicationInitiateId
     * @param $excludeSubjectFromStudentDepartment
     * @return Object|PseudoSubject[]
     * @throws ProfessionalException
     */
    public function getAllCoursesOfCBSApplication($studentId, $openCourseApplicationInitiateId, $excludeSubjectFromStudentDepartment)
    {
        $sql = "";
        $studentId = $this->realEscapeString($studentId);
        $openCourseList = [];
        //this query takes all pseudo subjects even if id is present in pseudosubject_students
        $condition = "";
        if ($excludeSubjectFromStudentDepartment) {
            $condition .= " AND ps.hdl_deptID!=d.deptID ";
        }
        $sql = "SELECT  ps.pseudosubjectID,ps.subjectName,handlingDept.deptID,handlingDept.deptName, 
                oai.is_grouped_subjects_enabled as isSubjectGroupingEnabled,psg.id as groupId,
                psg.name as groupName,oai.id as applicationId
                FROM opencourse_appln_initiate oai
                INNER JOIN opencourse_subject_seats oss ON oss.opencourse_appln_initiate_id =oai.id
                INNER JOIN cbs_application_batches cab on oai.id = cab.application_id
                INNER JOIN batches b on cab.batchId = b.batchID
                INNER JOIN department d ON d.deptID = b.deptID
                INNER JOIN studentaccount sa ON sa.batchID = b.batchID
                LEFT JOIN pseudosubjects ps ON ps.pseudosubjectID = oss.pseudosubjects_id
                LEFT JOIN  department handlingDept ON ps.hdl_deptID = handlingDept.deptID
                LEFT JOIN  pseudosubjects_sbs psbs ON ps.pseudosubjectID = psbs.pseudosubjectID
                LEFT JOIN pseudo_subject_groups psg ON psg.id = ps.pseudo_subject_group_id
                LEFT JOIN pseudo_subject_type pst ON pst.id =ps.pseudo_subject_type_id
                WHERE oai.id = $openCourseApplicationInitiateId $condition AND studentID = $studentId ";
        try {
            $openCourseList = $this->executeQueryForList($sql, $this->mapper[CBSServiceMapper::GET_ALL_COURSES_OF_SBS_APPLICATION_MAPPER]);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $openCourseList;
    }
    /**
     * @param $applicationId
     * @throws ProfessionalException
     */
    public function deleteCBSApplicationIfStudentNotRegistered($applicationId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        if (empty($applicationId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        try {
            $totalRegistrations = $this->fetchCountStudentsRegisteredForCBSApplication($applicationId);
            if (empty($totalRegistrations)) {
                $this->deleteCBSApplicationBatches($applicationId);
                $this->deleteApplicationSubjectSeatsByApplicationId($applicationId);
                $this->deleteCBSApplicationById($applicationId);
            } else {
                throw new ProfessionalException(ProfessionalException::CANNOT_DELETE_NON_EMPTY_APPLICATIONS, "You cannot delete this application because $totalRegistrations students registered for this application.");
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @throws ProfessionalException
     */
    public function deleteCBSApplicationById($applicationId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        if (empty($applicationId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "DELETE FROM opencourse_appln_initiate WHERE id =$applicationId";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @throws ProfessionalException
     */
    public function deleteCBSApplicationBatches($applicationId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        if (empty($applicationId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "DELETE FROM cbs_application_batches WHERE application_id=$applicationId";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @throws ProfessionalException
     */
    public function deleteApplicationSubjectSeatsByApplicationId($applicationId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        if (empty($applicationId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "DELETE FROM opencourse_subject_seats WHERE opencourse_appln_initiate_id=$applicationId";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @return mixed
     * @throws ProfessionalException
     */
    public function fetchCountStudentsRegisteredForCBSApplication($applicationId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        if (empty($applicationId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "SELECT COUNT(id) as totalRecords FROM opencourse_student_appln 
                WHERE opencourse_appln_initiate_id=$applicationId";
        try {
            return $this->executeQueryForObject($sql)->totalRecords;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @return Object|CBSApplication
     * @throws ProfessionalException
     */
    public function getApplicationDetails($applicationId)
    {
        $applicationDetails = null;
        $applicationId = $this->realEscapeString($applicationId);
        if (empty($applicationId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "SELECT oai.exclude_subject_from_student_department,oai.id,oai.cbs_type_id,cas.semester_id,
                oai.is_grouped_subjects_enabled,oai.opencourseName,oai.maximum_selections,
                DATE_FORMAT(oai.applnStartDate,'%Y-%m-%d') as startDate,
                DATE_FORMAT(oai.applnEndDate,'%Y-%m-%d') as endDate, DATE_FORMAT(oai.applnStartDate,'%h:%i %p') as startTime,
                DATE_FORMAT(oai.applnEndDate,'%h:%i %p') as endTime,oai.considerPlusTwoMarks,oai.course_type_id,
                d.deptID,d.deptName,oss.seats,ps.pseudosubjectID,psg.id as groupId,psg.name as groupName,
                b.batchID,b.batchName,ps.subjectName,handlingDepartment.deptID as handlingDeptId,
                handlingDepartment.deptName as handlingDeptName
                FROM opencourse_appln_initiate oai
                LEFT JOIN cbs_application_batches cab on oai.id = cab.application_id
                LEFT JOIN cbs_application_semesters cas ON cas.cbs_application_id = oai.id
                LEFT JOIN batches b on cab.batchId = b.batchID
                LEFT JOIN department d ON d.deptID = b.deptID
                LEFT JOIN semesters s ON s.semID =cas.semester_id
                LEFT JOIN opencourse_subject_seats oss ON oss.opencourse_appln_initiate_id = oai.id
                LEFT JOIN pseudosubjects ps ON ps.pseudosubjectID = oss.pseudosubjects_id
                LEFT JOIN department as handlingDepartment ON handlingDepartment.deptID=ps.hdl_deptID
                LEFT JOIN pseudo_subject_groups psg ON psg.id = ps.pseudo_subject_group_id
                LEFT JOIN pseudo_subject_type pst ON pst.id = oai.cbs_type_id
                WHERE oai.id =$applicationId";
        try {
            $applicationDetails = $this->executeQueryForObject($sql, false, $this->mapper[CBSServiceMapper::GET_APPLICATION_DETAILS]);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $applicationDetails;
    }
    /**
     * Return all CBS pseudo subjects ids
     * @param $applicationId
     * @return Object|array
     * @throws ProfessionalException
     */
    public function getAllCBSSubjectsIdsByApplicationId($applicationId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        if (empty($applicationId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "SELECT DISTINCT  pseudosubjects_id as pseudoSubjectId,ps.subjectName FROM opencourse_subject_seats oss 
                INNER JOIN pseudosubjects ps ON ps.pseudosubjectID = oss.pseudosubjects_id
                WHERE opencourse_appln_initiate_id = $applicationId";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Deleting pseudo subjects from the cbs application
     * @param $applicationId
     * @param $pseudoSubjectId
     * @throws ProfessionalException
     */
    public function deleteSubjectFromCBS($applicationId, $pseudoSubjectId)
    {
        if (empty($applicationId) || empty($pseudoSubjectId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "DELETE FROM opencourse_subject_seats WHERE opencourse_appln_initiate_id = $applicationId 
                 AND pseudosubjects_id = $pseudoSubjectId";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @return Object
     * @throws ProfessionalException
     */
    public function getCBSBatches($applicationId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        if (empty($applicationId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "SELECT b.batchID as batchId,b.batchName 
                FROM cbs_application_batches cab
                INNER JOIN batches b on cab.batchId = b.batchID
                WHERE  application_id  = $applicationId";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @param $batchId
     * @throws ProfessionalException
     */
    public function deleteCBSBatch($applicationId, $batchId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        $batchId = $this->realEscapeString($batchId);
        if (empty($applicationId) || empty($batchId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "DELETE FROM cbs_application_batches WHERE batchId =$batchId AND application_id =$applicationId";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @param $batchId
     * @return mixed|integer
     * @throws ProfessionalException
     */
    public function batchWiseOptedStudentsCount($applicationId, $batchId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        $batchId = $this->realEscapeString($batchId);
        if (empty($applicationId) || empty($batchId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "SELECT COUNT(DISTINCT sa.studentID) as totalAllottedStudents 
                FROM opencourse_appln_initiate oai 
                INNER JOIN opencourse_student_appln osa ON osa.opencourse_appln_initiate_id = oai.id
                INNER JOIN cbs_application_batches cab on oai.id = cab.application_id
                INNER JOIN  studentaccount sa ON sa.studentID = osa.studentaccounts_id AND sa.batchID = cab.batchId
                WHERE oai.id = $applicationId AND cab.batchId  =$batchId";
        try {
            return $this->executeQueryForObject($sql)->totalAllottedStudents;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $pseudoSubjectId
     * @return bool
     * @throws ProfessionalException
     */
    public function isDeletable($pseudoSubjectId)
    {
        $sql = "SELECT COUNT(id) as totalRecords FROM opencourse_subject_seats WHERE pseudosubjects_id =$pseudoSubjectId";
        try {
            $totalRecords = $this->executeQueryForObject($sql)->totalRecords;
            if (empty($totalRecords)) {
                return true;
            } else {
                return false;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $studentId
     * @param $cbsApplicationId
     * @return array|Object
     * @throws ProfessionalException
     */
    public function getAllottedCoursesOfStudent($studentId, $cbsApplicationId)
    {
        $studentId = $this->realEscapeString($studentId);
        $cbsApplicationId = $this->realEscapeString($cbsApplicationId);
        if (empty($studentId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_USER_ID, "Invalid user id given");
        }
        if (empty($cbsApplicationId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_CBS_APPLICATION_ID, "Invalid application id given");
        }
        $studentAllotedOpenCourseDetails = [];
        try {
            $sql = "SELECT p.subjectName AS pseudoSubjectName, p.pseudosubjectID AS pseudoSubjectId FROM pseudosubjects p 
                    INNER JOIN pseudosubjects_students ps ON p.pseudosubjectID = ps.pseudosubjectID 
                    INNER JOIN opencourse_student_appln osa ON p.pseudosubjectID = osa.pseudosubjects_id 
                   AND ps.studentID = osa.studentaccounts_id AND ps.pseudosubjectID = osa.pseudosubjects_id 
                    WHERE osa.studentaccounts_id = $studentId and osa.opencourse_appln_initiate_id = $cbsApplicationId";
            $studentAllotedOpenCourseDetails = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $studentAllotedOpenCourseDetails;
    }
    /**
     * Undocumented function
     *
     * @param $studentId
     * @param $cbsApplicationId
     * @param $semesterId
     * @return array|Object
     * @throws ProfessionalException
     */
    public function getCbsApplicationDetails($studentId, $cbsApplicationId, $semesterId)
    {
        $sql = "";
        $studentId = $this->realEscapeString($studentId);
        $cbsApplicationId = $this->realEscapeString($cbsApplicationId);
        $semesterId = $this->realEscapeString($semesterId);
        $openCourseList = null;
        try {
            $sql = "SELECT 
                    oai.id as applicationInitiateId,
                    oai.exclude_subject_from_student_department as excludeSubjectFromStudentDepartment,
                    oai.opencourseName as openCourseName,
                    b.semID as semId,
                    oai.applnStartDate,
                    oai.applnEndDate,
                    oai.considerPlusTwoMarks,
                    oai.maximum_selections as maximumSelection,
                    oai.is_grouped_subjects_enabled as isSubjectGroupingAdded,
                    pst.code as cbsTypeCode
                FROM batches b 
                INNER JOIN studentaccount sa ON b.batchID = sa.batchID AND sa.studentID = $studentId
                INNER JOIN opencourse_appln_initiate oai ON b.courseTypeID = oai.course_type_id AND oai.id = $cbsApplicationId
                INNER JOIN cbs_application_batches cab ON cab.batchId = sa.batchID AND cab.application_id =oai.id
                INNER JOIN cbs_application_semesters cas on oai.id = cas.cbs_application_id AND cas.semester_id = b.semID
                LEFT JOIN pseudo_subject_type pst ON pst.id = oai.cbs_type_id
                WHERE cas.semester_id = $semesterId";
            $openCourseList = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $openCourseList;
    }
    /**
     * @param $applicationId
     * @return Object
     * @throws ProfessionalException
     */
    public function getCBSSemesters($applicationId)
    {
        $sql = "SELECT DISTINCT semester_id as semesterId FROM opencourse_appln_initiate oai
                INNER JOIN cbs_application_semesters cas on oai.id = cas.cbs_application_id
                WHERE oai.id =$applicationId";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param UpdateCBSSemestersRequest $request
     * @throws ProfessionalException
     */
    public function updateCBSApplicationSemesters(UpdateCBSSemestersRequest $request)
    {
        $request = $this->realEscapeObject($request);
        try {
            foreach ($request->semesters as $semester) {
                $sql = "INSERT IGNORE INTO cbs_application_semesters (cbs_application_id, semester_id,updated_by, updated_date) 
                        VALUES  ($request->applicationId,$semester,$request->updatedBy, UTC_TIMESTAMP)";
                $this->executeQuery($sql);
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $applicationId
     * @param $semesterId
     * @throws ProfessionalException
     */
    public function removeCBSApplicationSemester($applicationId, $semesterId)
    {
        $applicationId = $this->realEscapeString($applicationId);
        $semesterId = $this->realEscapeString($semesterId);
        $sql = "DELETE FROM cbs_application_semesters WHERE cbs_application_id = $applicationId
                AND semester_id  =$semesterId";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
}