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 / 37
CRAP
0.00% covered (danger)
0.00%
0 / 578
DepartmentService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 37
12210.00
0.00% covered (danger)
0.00%
0 / 578
 __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
 getAllDepartmentForApi
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 26
 getDepartmentsByBatchAssociation
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 30
 getDepartmentByCourseTypesString
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 25
 getDepartmentById
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getDepartmentByCourseTypeID
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 12
 getDepartmentByCourseTypeIDForReports
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 21
 getDepartmentByBatchCourseType
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 15
 getDepartmentByCourseTypes
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 19
 getLibraryStaffAssociatedDepartments
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getAccessionPrefixNotSetDepartmentsByBatchCourseType
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 12
 getDepartments
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 21
 getValidTeachingDepartments
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 13
 getDepartmentIdList
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 getTimeTableAdminDepartments
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 19
 getDepartmentSubjectsByBatches
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 11
 getCourseTypesByDeptId
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getDepartmentByBatchId
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 getDepartmentByMultiBatchId
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 getDepartmentByCode
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 12
 getDepartmentsByCourseType
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 15
 getAllDepartment
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 searchDepartment
0.00% covered (danger)
0.00%
0 / 1
132.00
0.00% covered (danger)
0.00%
0 / 38
 getDepartmentsByRequest
0.00% covered (danger)
0.00%
0 / 1
132.00
0.00% covered (danger)
0.00%
0 / 65
 getAllDepartmentForFormways
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 26
 getDepartmentNameById
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 getHandlingDepartmentBySubjectId
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 18
 getDepartmentsFromInternalMarkAssignedStaff
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 21
 createDepartment
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 updateDepartment
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 12
 getAllDepartmentForDataImport
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 getDepartmentByStaff
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 16
 getAllDepartmentForFilter
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 getAllProgramByDepartmentForFilter
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 14
 getdepartmentName
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\base\util\RequestUtil;
use com\linways\base\exception\CoreException;
use com\linways\core\ams\professional\dto\Subject;
use com\linways\core\ams\professional\constant\ExamType;
use com\linways\core\ams\professional\mapper\DepartmentServiceMapper;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\request\SearchDepartmentRequest;
use com\linways\core\ams\professional\request\api\GetAllDepartmentsRequest;
class DepartmentService extends BaseService 
{
    // /Condition 1 - Presence of a static member variable
    private static $_instance = null;
    private $mapper = [];
    // /Condition 2 - Locked down the constructor
    private function __construct()
    {
        $this->mapper = DepartmentServiceMapper::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 GetAllDepartmentsRequest $request
     * @return object|array|\com\linways\base\util\$objectList[]
     * @throws ProfessionalException
     * @author gadheyan
     */
    public function getAllDepartmentForApi(GetAllDepartmentsRequest $request)
    {
        $sql = "";
        $request = $this->realEscapeObject($request);
        $responseList = [];
        $sql = "SELECT 
    deptID AS id,
    deptName AS name,
    departmentDesc AS description
FROM
    department WHERE 1=1 ";
        if ($request) {
            if ($request->id) {
                $sql .= " AND deptID = $request->id";
            } else if (!empty($request->ids)) {
                $sql .= " AND deptID in ('" . implode("','", $request->ids) . "')";
            }
            if ($request->deptShow) {
                $sql .= " AND deptShow = $request->deptShow";
            }
        }
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * get all batch associated departments
     * @param string $sortBy
     * @param string $sortOrder
     * @return array $departmentList
     * @throws ProfessionalException
     */
    public function getDepartmentsByBatchAssociation($sortBy = "deptName", $sortOrder = "ASC", $courseTypeId = null)
    {
        $departmentList = [];
        $condition = "";
        if ($courseTypeId) {
            $condition = " WHERE b.courseTypeID = $courseTypeId ";
        }
        $sql = "SELECT DISTINCT d.deptID as id,d.deptName as name,
            d.departmentDesc as description,
            d.deptShow,d.admissionshow 
            FROM department d INNER JOIN batches b ON b.deptID = d.deptID $condition
            ORDER BY $sortBy $sortOrder";
        //to get External department
        $extDeptSql = "SELECT DISTINCT d.deptID as id,d.deptName as name,
            d.departmentDesc as description,
            d.deptShow,d.admissionshow 
            FROM department d  WHERE d.departmentSpecialization ='" . ExamType::EXTERNAL . "'";
        try {
            $departmentList = $this->executeQueryForList($sql);
            $departmentExt = $this->executeQueryForObject($extDeptSql);
            if($departmentExt && !empty($departmentList)){
                $departmentList[] = $departmentExt;
                if($sortBy == "deptName"){
                    usort($departmentList, function ($a, $b) {
                        return $a->name > $b->name;
                    });
                }
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departmentList;
    }
    /**
     * get all associated departments by Course Types
     * @param string $sortBy
     * @param string $sortOrder
     * @return array $departmentList
     * @throws ProfessionalException
     */
    public function getDepartmentByCourseTypesString($sortBy = "deptName", $sortOrder = "ASC", $courseTypeId = NULL)
    {
        $departmentList = [];
        $condition = "";
        if ($courseTypeId) {
            $condition = " INNER JOIN batches b ON b.deptID = d.deptID WHERE b.courseTypeID IN (" . $courseTypeId . ")";
        }
        $sql = "(SELECT d.deptID as id,d.deptName as name,
        d.departmentDesc as description,
        d.deptShow,d.admissionshow 
        FROM department d WHERE d.deptID NOT IN (SELECT DISTINCT d.deptID
        FROM department d 
        INNER JOIN batches b ON b.deptID = d.deptID) ORDER BY $sortBy $sortOrder)
        UNION
        (SELECT d.deptID as id,d.deptName as name,
            d.departmentDesc as description,
            d.deptShow,d.admissionshow 
            FROM department d $condition
            ORDER BY $sortBy $sortOrder)";
        try {
            $departmentList = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departmentList;
    }
    /**
     * get department details by id
     * @param int $deptId
     * @return object $department
     * @throws ProfessionalException
     */
    public function getDepartmentById($deptId)
    {
        $department = NULL;
        $sql = "SELECT deptID as id,deptName as name,
            departmentDesc as description FROM department WHERE deptID=$deptId";
        try {
            $department = $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $department;
    }
    /**
     * get departments by coursetypeID
     * @param int $courseTypeID
     * @return object|array|\com\linways\base\util\$objectList[]
     * @throws ProfessionalException
     */
    public function getDepartmentByCourseTypeID($courseTypeID)
    {
        $departments = [];
        try {
            $sql = "SELECT DISTINCT dept.* from assigned_dept_courses assigned
                    INNER JOIN department dept ON assigned.deptId = dept.deptID
                    WHERE assigned.courseTypeId = $courseTypeID";
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departments;
    }
    /**
     * get departments by coursetypeID for Reports
     * @param int $courseTypeID
     * @return object|array|\com\linways\base\util\$objectList[]
     * @throws ProfessionalException
     * @author Ajay C
     */
    public function getDepartmentByCourseTypeIDForReports($courseTypeID)
    {
        $departments = [];
        try {
            $sql = "SELECT
                        DISTINCT
                        d.deptID ,
                        d.deptName
                    from
                        batches b
                    inner join course_type ct on
                        ct.courseTypeID = b.courseTypeID
                    inner join department d on
                        b.deptID = d.deptID
                    where
                        b.courseTypeID = '$courseTypeID'";
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departments;
    }
    /**
     * @param int $courseTypeId
     * @return object|array|\com\linways\base\util\$objectList[]
     * @throws ProfessionalException
     * @author sanoop
     * get department with course type
     */
    public function getDepartmentByBatchCourseType($courseTypeId)
    {
        $departments = NULL;
        $sql = "SELECT DISTINCT dept.deptID AS id,dept.deptName AS name, dept.departmentDesc as description,dept.deptShow,
                dept.admissionshow  
                FROM department dept 
                INNER JOIN batches bth ON bth.deptID=dept.deptID AND bth.batchHide=0 
                INNER JOIN batch_course_type bct ON bct.id=bth.patternID 
                WHERE bct.id=$courseTypeId";
        try {
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departments;
    }
    /**
     * @param $courseTypeIds
     * @return object|array|
     * @throws ProfessionalException
     * @author jithin vijayan
     * get department with course type
     */
    public function getDepartmentByCourseTypes($courseTypeIds)
    {
        $departments = NULL;
        $courseTypeIds = $this->realEscapeArray($courseTypeIds);
        $sql = "SELECT DISTINCT dept.deptID AS id,dept.deptName AS name, dept.departmentDesc as description,dept.deptShow,
                dept.admissionshow  
                FROM department dept 
                INNER JOIN batches bth ON bth.deptID=dept.deptID AND bth.batchHide=0 
                INNER JOIN course_type bct ON bct.courseTypeID=bth.courseTypeID 
                WHERE 1=1 ";
        if (!empty($courseTypeIds) && count($courseTypeIds) > 0) {
            $sql .= " AND bct.courseTypeID IN (" . implode(",", $courseTypeIds) . ")";
        }
        try {
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departments;
    }
    /**
     * get library staff associated departments
     * @param int $libraryStaffId
     * @return object|array|\com\linways\base\util\$objectList[]
     * @throws ProfessionalException
     */
    public function getLibraryStaffAssociatedDepartments($libraryStaffId)
    {
        $departmentList = NULL;
        $sql = "SELECT DISTINCT d . deptID as id,d . deptName as name, if (lsd . staffId,1,0) as selected FROM department d INNER JOIN batches b ON b . deptID = d . deptID LEFT JOIN libraryStaffDepartment lsd ON lsd . deptId = d . deptID AND lsd . staffID = $libraryStaffId";
        try {
            $departmentList = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departmentList;
    }
    public function getAccessionPrefixNotSetDepartmentsByBatchCourseType($courseTypeId)
    {
        $departments = NULL;
        $sql = "select distinct dept . deptID as id,dept . deptName as name,
            dept . departmentDesc as description,
            dept . deptShow,dept . admissionshow  from department dept INNER JOIN batches bth ON bth . deptID = dept . deptID AND bth . batchHide = 0 INNER JOIN batch_course_type bct ON bct . id = bth . patternID LEFT JOIN accessionPrefix ap ON ap . deptId = dept . deptID AND ap . courseTypeId = bct . id WHERE bct . id = $courseTypeId AND ap . id is null";
        try {
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departments;
    }
    /**
     * get departments
     * @param bool $showHidden
     * @param string $sortBy
     * @param string $sortOrder
     * @return Department list
     * @return Department[]
     * @throws \com\linways\base\exception\CoreException
     * @throws ProfessionalException
     * @throws ProfessionalException
     * @throws CoreException
     */
    public function getDepartments($showHidden = true, $sortBy = "deptName", $sortOrder = "ASC")
    {
        $departments = [];
        $sql = "SELECT deptID as id,
        deptName as name,
        deptName as text,
        departmentDesc as description,
        deptShow,
        admissionShow
        from department ";
        if (!$showHidden) {
            $sql .= "WHERE deptShow = 1 ";
        }
        $sql .= "order By $sortBy $sortOrder ";
        // Validate sort order and sort by
        RequestUtil::validateSortOrder($sortBy, $sortOrder);
        try {
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED, $e->getMessage());
        }
        return $departments;
    }
    /**
     * @return array|Object
     * @throws ProfessionalException
     * @author jithinvijayan
     */
    public function getValidTeachingDepartments()
    {
        $departments = [];
        $sql = "SELECT DISTINCT d.deptID as id,d.deptName as name,d.departmentDesc as description 
                FROM department d 
                INNER JOIN sd_relation sr on d.deptID = sr.deptID
                WHERE d.deptShow =1 AND d.admissionshow=0";
        try {
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departments;
    }
    public function getDepartmentIdList($showHidden = true, $sortBy = "deptName", $sortOrder = "ASC")
    {
        $departmentList = [];
        $departmentIdList = [];
        $departmentList = $this->getDepartments($showHidden, $sortBy, $sortOrder);
        foreach ($departmentList as $department) {
            $departmentIdList[] = $department->id;
        }
        return $departmentIdList;
    }
    /**
     * Get timetable privillege admin departments
     * @param int $adminTypeId
     * @param string $sortBy
     * @param string $sortOrder
     * @return \com\linways\base\connection\Object
     * @throws ProfessionalException
     */
    public function getTimeTableAdminDepartments($adminTypeId, $sortBy = "name", $sortOrder = "ASC", $showHiddenDept = true)
    {
        $departments = [];
        $adminTypeId = $this->realEscapeString($adminTypeId);
        $sql_depts = null;
        $sql_all_selected = "select id FROM timtable_admin_dept_privilleges WHERE adminTypeId = $adminTypeId and deptId = 0";
        try {
            $allDeptSelected = $this->executeQueryForObject($sql_all_selected);
            $showHiddenDeptCondition = !$showHiddenDept ? " and d . deptShow = 1" : "";
            if (empty($allDeptSelected)) {
                $sql_depts = "SELECT d . deptID as id,d . deptName as name,d . departmentDesc as description,d . deptShow,d . admissionshow FROM department d inner join timtable_admin_dept_privilleges tadp on tadp . deptId = d . deptID WHERE tadp . adminTypeId = $adminTypeId $showHiddenDeptCondition order by $sortBy $sortOrder";
            } else {
                $sql_depts = "SELECT d . deptID as id,d . deptName as name,d . departmentDesc as description,d . deptShow,d . admissionshow FROM department d  where 1 = 1 $showHiddenDeptCondition order by $sortBy $sortOrder ";
            }
            $departments = $this->executeQueryForList($sql_depts);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departments;
    }
    /**
     * Get subjects in all batches of a department
     * @param int $deptId
     * @param int $courseTypeId
     * @return Object
     * @throws ProfessionalException
     */
    public function getDepartmentSubjectsByBatches($deptId, $courseTypeId)
    {
        $deptId = $this->realEscapeString($deptId);
        $courseTypeId = $this->realEscapeString($courseTypeId);
        $sql = "SELECT dept . deptID,dept . deptName,dept . departmentDesc, bat . batchID,bat . batchName,bat . batchStartYear,bat . batchEndYear,bat . semID as currentSemId,sem . semID, sem . semName, sem . year, sa . staffID,sa . staffName,sd . deptName as staffDept,sub . subjectID,sub . subjectName,sub . subjectDesc, sc . subjectcatName AS subjectCategory from department dept inner join batches bat on bat . deptID = dept . deptID inner join semesters sem on sem . semID = bat . semID inner join sbs_relation sr on sr . batchID = bat . batchID and sr . semID = bat . semID inner join staffaccounts sa on sa . staffID = sr . staffID  inner join subjects sub on sub . subjectID = sr . subjectID INNER JOIN subject_category sc ON(sc . subjectcatID = sub . subjectcatID) LEFT JOIN department sd on(sa . deptID = sd . deptID) where dept . deptID = " . $deptId . " and patternID = " . $courseTypeId . " and sa . isResigned = 0 order by bat . semID , sub . subjectcatID Desc";
        try {
            $depatmentSubjectDetails = $this->executeQueryForList($sql, $this->mapper[DepartmentServiceMapper::GET_DEPARTMENT_DETAILS]);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $depatmentSubjectDetails;
    }
    /**
     * Get course types
     * @param int $deptId
     * @return \com\linways\base\connection\Object
     * @throws ProfessionalException
     */
    public function getCourseTypesByDeptId($deptId)
    {
        $deptId = $this->realEscapeString($deptId);
        $sql = "SELECT distinct(patternID), bct . type_name, bct . is_pg, bat . courseTypeID, ct . typeName from  department dept inner join batches bat on dept . deptID = bat . deptID inner join batch_course_type bct on bct . id = bat . patternID INNER JOIN course_type ct ON(ct . courseTypeID = bat . courseTypeID AND ct . courseTypeID = bct . courseTypeID) where dept . deptID = " . $deptId;
        try {
            $couseTypes = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $couseTypes;
    }
    /**
     * get department details using batch id
     * @param int $batchId
     * @return object|NULL|\com\linways\base\util\$objectList[]
     * @throws ProfessionalException
     */
    public function getDepartmentByBatchId($batchId)
    {
        $sql = "SELECT dept .* FROM department dept INNER JOIN batches bat ON bat . deptID = dept . deptID WHERE bat . batchID = $batchId";
        try {
            return $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
     /**
     * get department details using batch id
     * @param int $batchId
     * @return object|NULL|\com\linways\base\util\$objectList[]
     * @throws ProfessionalException
     */
    public function getDepartmentByMultiBatchId($batchId)
    {
        $sql = "SELECT dept .* FROM department dept INNER JOIN batches bat ON bat . deptID = dept . deptID WHERE bat . batchID in ($batchId)";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * get department details by dept code
     * @param string $deptCode
     * @return object $department
     * @throws ProfessionalException
     */
    public function getDepartmentByCode($deptCode)
    {
        $department = NULL;
        $deptCode = $this->realEscapeString($deptCode);
        try {
            $sql = "SELECT deptID as id,deptName as name,
            departmentDesc as description FROM department WHERE deptName = '$deptCode'";
            $department = $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $department;
    }
    /**
     * returns batches by course type id
     * NOTE: Same function available above, but their `batch_course_type` joined.
     * @param $courseTypeId
     * @return Object
     * @throws ProfessionalException
     */
    public function getDepartmentsByCourseType($courseTypeId)
    {
        if (empty($courseTypeId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_COURSE_TYPE_ID, "Invalid course type Id");
        }
        $sql = "SELECT DISTINCT d . deptID as id,d . deptName as name,d . departmentDesc as description 
                FROM department d 
                INNER JOIN batches b ON b . deptID = d . deptID AND b . batchHide = 0
                INNER JOIN course_type ct on b . courseTypeID = ct . courseTypeID
                WHERE ct . courseTypeID = $courseTypeId";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @return Object
     * @throws ProfessionalException
     */
    public function getAllDepartment()
    {
        $sql = "";
        $sql = "select deptID,deptName,departmentDesc from department where deptShow = 1;";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Searching department by batch,course type,name and department id
     * @param SearchDepartmentRequest $request
     * @return Object
     * @throws ProfessionalException
     */
    public function searchDepartment(SearchDepartmentRequest $request)
    {
        $batchJoin = "";
        $batchCondition = "";
        $courseTypeJoin = "";
        $courseTypeCondition = "";
        if (!empty($request->batchId)) {
            $batchJoin = "INNER JOIN batches b ON b . deptID = d . deptID ";
            $batchCondition .= " AND b . batchID = $request->batchId";
        }
        if (!empty($request->courseTypeId) && empty($request->batchId)) {
            $courseTypeJoin = " INNER JOIN batches b ON b . deptID = d . deptID 
                                INNER JOIN course_type ct ON ct . courseTypeID = b . courseTypeID ";
            $courseTypeCondition = " AND ct . courseTypeID = $request->courseTypeId";
        } else if (!empty($request->courseTypeId) && !empty($request->batchId)) {
            $courseTypeJoin = " INNER JOIN course_type ct ON ct . courseTypeID = b . courseTypeID ";
            $courseTypeCondition = " AND ct . courseTypeID = $request->courseTypeId";
        }
        $sql = "SELECT DISTINCT  d . deptID as id,d . deptName as name, d . departmentDesc as description 
                FROM department d
                $batchJoin $courseTypeJoin WHERE 1 = 1 $batchCondition $courseTypeCondition";
        if (!empty($request->name)) {
            $sql .= " AND d . deptName = '$request->name";
        }
        if (!empty($request->id)) {
            $sql .= " AND d . deptID = $request->id ";
        }
        if (!$request->showHiddenDepartment) {
            $sql .= " AND d . deptShow = 1";
        }
        if (!$request->showAdmissionDepartment) {
            $sql .= " AND d . admissionshow = 0";
        }
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * get departments by request for exam controller common component
     * @return Department list
     * @throws AMSException
     * @throws ProfessionalException
     */
    public function getDepartmentsByRequest($request)
    {
        $departments = [];
        $request = $this->realEscapeObject($request);
        $coursePattern = $request->coursePattern;
        $courseId = $request->courseId;
        $examRegId = $request->examRegId;
        $examType = $request->examType;
        $batch = $request->batch;
        $joinTables = "";
        $whereConditions = "";
        if ($examType || $examRegId) {
            if ($examType == ExamType::REGULAR) {
                $examTypeBatchTable = "exam_registration_batches";
                $examTypeBatchTableColumn = "examregID";
            } else if ($examType == ExamType::SUPPLY) {
                $examTypeBatchTable = "supply_improve_batches";
                $examTypeBatchTableColumn = "exam_supplementary_id";
            }
            if ($examType != ExamType::INTERNAL) {
                // should remove after Internal exam registraion batches table creation
                $joinTables .= "   INNER JOIN
                $examTypeBatchTable erb ON erb . batchID = b . batchID";
                if ($examRegId) {
                    $whereConditions .= " AND erb . $examTypeBatchTableColumn IN($examRegId)";
                }
            }
        }
        if ($courseId) {
            $joinTables .= "   INNER JOIN
            course_pattern c ON c . patternID = b . patternID
            ";
            $whereConditions .= " AND b . patternID IN($courseId)
            ";
        }
        if ($batch) {
            $whereConditions .= " AND b . batchID IN($batch)
            ";
        }
        if ($coursePattern) {
            $joinTables .= " INNER JOIN
            pattern_deptcourses pdc ON pdc . patterncourseID = b . patterncourseID";
            $whereConditions .= " AND b . patterncourseID IN($coursePattern)";
        }
        $sql = "SELECT DISTINCT
        dept . deptID AS id,
        dept . deptName AS name,
        dept . departmentDesc AS description,
        dept . deptShow,
        dept . admissionShow
    FROM
        department dept
            INNER JOIN
        batches b ON dept . deptID = b . deptID
        $joinTables
    WHERE 
        dept . deptShow = 1
        $whereConditions
    ORDER BY
        dept . deptName ASC";
        try {
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED, $e->getMessage());
        }
        return $departments;
    }
    /** Formways services **/
    /**
     *
     * @param GetAllDepartmentsRequest $request
     * @return object|array|\com\linways\base\util\$objectList[]
     * @throws ProfessionalException
     * @author gadheyan
     */
    public function getAllDepartmentForFormways(GetAllDepartmentsRequest $request)
    {
        $sql = "";
        $request = $this->realEscapeObject($request);
        $responseList = [];
        $sql = "SELECT 
                deptID AS value,
                deptName AS name,
                departmentDesc AS label
            FROM
                department WHERE 1=1 ";
        if ($request) {
            if ($request->id) {
                $sql .= " AND deptID = '$request->id'";
            } else if (!empty($request->ids)) {
                $sql .= " AND deptID in ('" . implode("','", $request->ids) . "')";
            }
            if ($request->deptShow) {
                $sql .= " AND deptShow = $request->deptShow";
            }
        }
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $departmentId
     * @return mixed|string
     * @throws ProfessionalException
     * @author jithinvijayan
     */
    public function getDepartmentNameById($departmentId)
    {
        $departmentId = $this->realEscapeString($departmentId);
        $sql = "SELECT deptName as name FROM department where  deptID = $departmentId";
        try {
            return $this->executeQueryForObject($sql)->name;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * get handling dept of any subject
     * @param $subjectId
     * @return object
     * @throws ProfessionalException
     */
    public function getHandlingDepartmentBySubjectId($subjectId)
    {
        $subjectId = $this->realEscapeString($subjectId);
        $sql = "SELECT 
                    d.deptID, d.deptName, d.departmentDesc, d.admissionshow
                FROM
                    subjects s
                        INNER JOIN
                    department d ON s.hdl_deptID = d.deptID
                WHERE
                    d.deptShow = 1
                    and s.subjectID = $subjectId";
        try {
            $result = $this->executeQueryForObject($sql);
            return $result;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * get departments
     * @param $sstaffIdortBy
     * @return Department[]
     * @throws ProfessionalException
     */
    public function getDepartmentsFromInternalMarkAssignedStaff($staffId)
    {
        $departments = [];
        $sql = "SELECT 
                    dept.deptID as id, dept.deptName as name, dept.deptName as text, dept.departmentDesc as description 
                FROM 
                    assign_staff_internal_mark_entry asime 
                INNER JOIN 
                    batches bt ON (asime.batchID = bt.batchID) 
                INNER JOIN 
                    department dept ON (dept.deptID = bt.deptID) 
                WHERE 
                    asime.adminID = $staffId
                    GROUP BY dept.deptID 
                    ORDER BY dept.deptName ASC";
        try {
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException(ProfessionalException::QUERY_EXECUTION_FAILED, $e->getMessage());
        }
        return $departments;
    }
    /**
     * Create department
     * @param Department $department
     * @return id
     * @throws ProfessionalException
     */
    public function createDepartment($department)
    {
        $sql = "INSERT INTO department (deptName,departmentDesc,deptShow,admissionShow)
        VALUES ('$department->name','$department->description','$department->deptShow','$department->admissionShow');";
        try {
            return $this->executeQueryForObject($sql, true);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * update department
     * @param Department $department
     * @return unknown
     * @throws AMSException
     */
    public function updateDepartment($department)
    {
        $sql = "UPDATE department  SET
        deptName = '$department->name',
        departmentDesc = '$department->description',
        admissionShow = '$department->admissionShow'  WHERE deptId = $department->id";
        try {
            $this->executeQuery($sql);
            return $department->id;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @return Object
     * @throws ProfessionalException
     */
    public function getAllDepartmentForDataImport()
    {
        $sql = "SELECT deptID as id,deptName as name from department where deptShow = 1;";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param array $departmentIds
     * @return array|Object|Staff[]
     * @throws ProfessionalException
     */
    public function getDepartmentByStaff($staffIds)
    {
        $staffs = [];
        $staffIds = $this->realEscapeArray($staffIds);
        if (!is_array($staffIds)) {
            throw new ProfessionalException(ProfessionalException::INVALID_ARGUMENT_TYPE, "Expected array, but got " . gettype($staffIds));
        }
        $sql = "SELECT dp.deptID ,dp.deptName  FROM staffaccounts sa
                    INNER JOIN department dp ON dp.deptid = sa.deptId
                    WHERE sa.staffID IN(" . implode(",", $staffIds) . ") GROUP BY dp.deptID";
        try {
            $departments = $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $departments;
    }
    public function getAllDepartmentForFilter()
    {
        $sql = "SELECT deptID as id,deptName as `name` from department";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    public function getAllProgramByDepartmentForFilter($deptId)
    {
        $sql = "SELECT
                    p.id as id,
                    p.name as `name` 
                FROM
                    program p
                INNER JOIN program_department_relation pdr ON
                    p.id = pdr.program_id WHERE pdr.department_id = '$deptId'";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param array $departmentIds
     * @return array
     * @author Midhun Shaji 
     */
    public function getdepartmentName($deptId)
    {
        $sql = "SELECT deptName  from department where 1=1 $deptId";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
}