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 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 142
GroupService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
1482.00
0.00% covered (danger)
0.00%
0 / 142
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 searchGroupByRequest
0.00% covered (danger)
0.00%
0 / 1
600.00
0.00% covered (danger)
0.00%
0 / 67
 searchFinalTermGroups
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 42
 getAllBatchAcademicYears
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 31
<?php
namespace com\linways\ec\core\service;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\ec\core\mapper\GroupServiceMapper;
use com\linways\ec\core\constant\StatusConstants;
use com\linways\ec\core\exception\ECCoreException;
use com\linways\core\ams\professional\request\academic\SearchGroupRequest;
class GroupService extends BaseService
{
    use MakeSingletonTrait;
    private function __construct() {
        $this->mapper = GroupServiceMapper::getInstance()->getMapper();
    }
     /**
     * Search Academic term Details
     * @param SearchGroupRequest $request
     * @return Group
     */
    public function searchGroupByRequest(SearchGroupRequest $request)
    {
        $request = $this->realEscapeObject($request);
        $whereQuery = "";
        $limitQuery = "";
        $where = [];
        
        !empty($request->startYear)?$where [] = " g.properties->>'$.startYear' = '$request->startYear":null;
        !empty($request->endYear)?$where [] = " JSON_CONTAINS(g.properties, '{\"endYear\":\"$request->endYear\"}') ":null;
        !empty($request->programId)?$where [] = " JSON_CONTAINS(g.properties, '{\"programId\":\"$request->programId\"}') ":null;
        !empty($request->departmentId)?$where [] = " g.properties->>'$.departmentId' = \"$request->departmentId\"":null;
        !empty($request->currentTermId)?$where [] = " JSON_CONTAINS(g.properties, '{\"currentTermId\":\"$request->currentTermId\"}') ":null;
        !empty($request->curriculumId)?$where [] = " JSON_CONTAINS(g.properties, '{\"curriculumId\":\"$request->curriculumId\"}') ":null;
        if(!empty($request->departmentIds)){
            $departmentIdString = is_array($request->departmentIds) ? "'" . implode("','", $request->departmentIds) . "'" : "'" . $request->departmentIds . "'";
            $where [] = " g.properties->>'$.departmentId' IN ($departmentIdString";
        }
        if(count($request->syllabusIds)){
            $or = [];
            foreach ($request->syllabusIds as $key => $syllabusId) {
                $or [] = " JSON_CONTAINS(g.properties->'$.extraSyllabusId', '\"$syllabusId\"' ,'$') ";
            }
            $where [] = " (".implode(" OR ",$or).") ";
        }
        $innerJoin = " INNER JOIN program p on p.id  = g.properties->>'$.programId'";
        if(!empty($request->degreeIds)){
            $where [] = " p.degree_id IN ('".implode("','", $request->degreeIds)."') ";
        }
        if(!empty($request->courseTypeId)){
            $where [] = " p.course_type_id = '$request->courseTypeId";
        }
        if(!empty($request->id)){
            $where [] = " g.id = '$request->id";
        }
        
        !empty($request->id)?$where [] = " g.id = '$request->id":null;
        $request->trashed === StatusConstants::ACTIVE ? $where [] = " g.trashed IS NULL ":null;
        
        $request->trashed === StatusConstants::TRASHED ? $where [] = " g.trashed IS NOT NULL ":null;
    
        !empty($request->name) ? $where [] = " g.name LIKE '%$request->name%' ":null;
        !empty($request->type) ? $where [] = " g.type LIKE '%$request->type%' ":null;
    
        
        if($request->startIndex !== "" && $request->endIndex !== "")
        {
            $limitQuery .= " LIMIT $request->startIndex,$request->endIndex";
        }
        $query = "SELECT
            g.id,
            g.identifying_context,
            g.name,
            g.type,
            g.properties,
            g.trashed,
            g.created_by,
            g.created_date,
            g.updated_by,
            g.updated_date
        FROM
            `groups` g 
            $innerJoin
            ".(count($where)? " WHERE ".implode(' AND ',$where):"");
        try {
            $groups = $this->executeQueryForList($query." ".$limitQuery, $this->mapper[GroupServiceMapper::SEARCH_GROUPS]);
        } catch (\Exception $e) {
            throw new ECCoreException(ECCoreException::ERROR_FETCHING,"Cannot fetch group details! Please try again.");
        }
        if(empty($groups)) 
        {
            $groups = [];
        }
        return $groups;
    } 
     /**
     * Search Final Term Batches
     * @param  $request
     * @return Group
     */
    public function searchFinalTermGroups( $request){
        $request = $this->realEscapeObject($request);
        $whereQuery = "";
        $limitQuery = "";
        $where = [];
        $where [] = " g.type = 'BATCH' ";
        $where [] = " g.properties->'$.currentTermId' = g.properties->'$.finalTermId' ";
        $where [] = " g.trashed IS NULL ";
        $innerJoin = " INNER JOIN program p on p.id  = g.properties->>'$.programId'";
        $innerJoin .= " INNER JOIN department d on d.deptID  = g.properties->>'$.departmentId'";
        $innerJoin .= " INNER JOIN academic_term at on at.id = g.properties->>'$.currentTermId'";
        $leftJoin = " LEFT JOIN ec_group_priority egp on egp.groups_id = g.id ";
        if(!empty($request->courseTypeId)){
            $courseTypeIdIdString = is_array($request->courseTypeId) ? "'" . implode("','", $request->courseTypeId) . "'" : "'" . $request->courseTypeId . "'";
            $where [] = " p.course_type_id IN ($courseTypeIdIdString";
        }
        !empty($request->id)?$where [] = " g.id = '$request->id":null;
        $query = "SELECT DISTINCT
            g.id,
            g.identifying_context as identifyingContext,
            g.name,
            g.type,
            g.properties,
            g.trashed,
            d.deptName AS departmentName,
            at.name as semesterName,
            egp.priority as batchPriority
        FROM
            `groups` g 
            $innerJoin $leftJoin
            ".(count($where)? " WHERE ".implode(' AND ',$where):"");
        try {
            $groups = $this->executeQueryForList($query." ".$limitQuery);
        } catch (\Exception $e) {
            throw new ECCoreException(ECCoreException::ERROR_FETCHING,"Cannot fetch group details! Please try again.");
        }
        array_walk($groups, function($group){
            $group->properties = json_decode($group->properties);
        });
        if(empty($groups)) {
            $groups = [];
        }
        return $groups;
    } 
        /**
     * get all batch academic years
     * @param $year START_YEAR, END_YEAR, ACADEMIC_YEAR
     * @return Group
     */
    public function getAllBatchAcademicYears($year="START_YEAR")
    {
        $year = $this->realEscapeString($year);
        $columns = "";
        if ($year === "START_YEAR") {
            $columns = "g.properties->>'$.startYear' AS year";
        }
        if ($year === "END_YEAR") {
            $columns = "g.properties->>'$.endYear' AS year";
        }
        if ($year === "ACADEMIC_YEAR") {
            $columns = "CONCAT(g.properties->>'$.startYear', '-', g.properties->>'\$endYear') AS year";
        }
        $query = "SELECT DISTINCT
            $columns
        FROM
            `groups` g
        WHERE
            g.trashed IS NULL AND g.properties->>'$.startYear' IS NOT NULL";
        try {
            $groups = $this->executeQueryForList($query);
        } catch (\Exception $e) {
            throw new ProfessionalException(ProfessionalException::ERROR_FETCHING,"Cannot fetch group details! Please try again.");
        }
        usort($groups,function($groupA, $groupB){
            return $groupA->year > $groupB->year;
        });
        $groups = array_values(array_filter($groups, function($g) { return $g->year; }));
        if(empty($groups))
        {
            throw new ProfessionalException(ProfessionalException::EMPTY_SEARCH_ITEMS,"No Batch Groups Found");
        }
        return $groups;
    }
   
}