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 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 225
SubBatchService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 15
2862.00
0.00% covered (danger)
0.00%
0 / 225
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 __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
 updateSubBatchPseudoSubjectId
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 getSubbatchOfAStudentForASubject
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 15
 getSubbatchesOfAStudentForASubject
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 24
 getSubBatch
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 20
 createSubBatch
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 20
 assignStudentsToSubBatch
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 14
 getSubbatchDetailsByRequest
0.00% covered (danger)
0.00%
0 / 1
110.00
0.00% covered (danger)
0.00%
0 / 29
 getSubBatchBySBS
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getSubBatchesBySBS
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 18
 deleteSubbatch
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 22
 getStudentsFromSubBatch
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 getSubBatchByBatchExam
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 23
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\request\CreateSubBatchRequest;
use com\linways\core\ams\professional\request\GetSubBatchRequest;
class SubBatchService extends BaseService
{
    // /Condition 1 - Presence of a static member variable
    private static $_instance = null;
    // /Condition 2 - Locked down the constructor
    private function __construct()
    {
    }
    // 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 $id
     * @throws ProfessionalException
     */
    public function updateSubBatchPseudoSubjectId($id)
    {
        $id = (int)$this->realEscapeString($id);
        if (empty($id)) {
            throw new ProfessionalException(ProfessionalException::INVALID_REQUEST, "Invalid request sent");
        }
        $sql = "UPDATE subbatches SET psID = 0 WHERE psID =$id";
        try {
            $this->executeQuery($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $request
     * @return int
     * @throws ProfessionalException
     * @author jithinvijayan
     */
    public function getSubbatchOfAStudentForASubject($request)
    {
        $request = $this->realEscapeObject($request);
        $query = "SELECT DISTINCT ss.subbatchID 
                    FROM sbs_relation sbsr 
                    INNER JOIN subbatch_sbs ssbs ON sbsr.sbsID=ssbs.sbsID 
                    INNER JOIN subbatch_student ss ON ssbs.subbatchID=ss.subbatchID 
                    WHERE sbsr.batchID='$request->batchId' AND sbsr.semID='$request->semesterId
                    AND sbsr.subjectID='$request->subjectId' AND ss.studentID='$request->studentId';";
        try {
            $subbatchId = $this->executeQueryForObject($query);
            return $subbatchId ? $subbatchId->subbatchID : 0;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
     /**
     * To get the subbatch Ids of a student for a subject return array of Ids.
     * Slight varition of getSubbatchOfAStudentForASubject.
     * it also check staffId in query condition if present in $request. 
     * 
     * @param $request
     * @return array
     * @throws ProfessionalException
     * @author Sooraj S
     */
    public function getSubbatchesOfAStudentForASubject($request)
    {
        $emptySubbatchId = array(0);
        $request = $this->realEscapeObject($request);
        $query = "SELECT DISTINCT JSON_ARRAYAGG(ss.subbatchID) as subbatches
                    FROM sbs_relation sbsr 
                    INNER JOIN subbatch_sbs ssbs ON sbsr.sbsID=ssbs.sbsID 
                    INNER JOIN subbatch_student ss ON ssbs.subbatchID=ss.subbatchID 
                    WHERE sbsr.batchID='$request->batchId' AND sbsr.semID='$request->semesterId
                    AND sbsr.subjectID='$request->subjectId' AND ss.studentID='$request->studentId'";
        if($request->staffId)
        {
            $query .=" AND sbsr.staffID='$request->staffId'";
        }
        try {
            $subbatchId = json_decode( $this->executeQueryForObject($query)->subbatches );
            if (empty($subbatchId)){
                return $emptySubbatchId;
            } else {
                return $subbatchId;
            }
            
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param GetSubBatchRequest $request
     * @return Object
     * @throws ProfessionalException
     * @author jithinvijayan
     */
    public function getSubBatch(GetSubBatchRequest $request)
    {
        $request = $this->realEscapeObject($request);
        if (empty($request->pseudoSubjectId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_PSEUDO_SUBJECT_ID, "Pseudo subject details not found");
        }
        if (empty($request->batchId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_BATCH_ID, "Invalid batch details given");
        }
        if (empty($request->semesterId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_SEMESTER_ID, "Invalid semester details given");
        }
        $sql = "SELECT subbatchID as id,batchID as batchId 
                FROM subbatches 
                WHERE psID = $request->pseudoSubjectId AND batchID = $request->batchId AND semID = $request->semesterId";
        try {
            return $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * Creating sub batch
     *
     * @param CreateSubBatchRequest $request
     * @return Object
     * @throws ProfessionalException
     * @author jithinvijayan
     */
    public function createSubBatch(CreateSubBatchRequest $request)
    {
        $request = $this->realEscapeObject($request);
        if (empty($request->name)) {
            throw new ProfessionalException(ProfessionalException::INVALID_SUB_BATCH_NAME, "Invalid sub-batch name given");
        }
        if (empty($request->batchId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_BATCH_ID, "No batches assigned to the sub-batch");
        }
        if (empty($request->semesterId)) {
            throw new ProfessionalException(ProfessionalException::INVALID_SEMESTER_ID, "No semesters assigned to the sub-batch");
        }
        $sql = "INSERT INTO subbatches(subbatchName,description,batchID,semID,psID) 
                VALUES ('" . $request->name . "','" . $request->description . "',$request->batchId$request->semesterId
                $request->pseudoSubjectId)";
        try {
            return $this->executeQueryForObject($sql, true);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $subBatchId
     * @param $studentIds
     * @return Object
     * @throws ProfessionalException
     * @author jithinvijayan
     */
    public function assignStudentsToSubBatch($subBatchId, $studentIds)
    {
        $sqlValues = "";
        foreach ($studentIds as $studentId) {
            $sqlValues .= "(" . $subBatchId . ", " . $studentId . ",1,utc_timestamp(),1,utc_timestamp),";
        }
        $sqlValues = rtrim($sqlValues, ",");
        $sql = "INSERT INTO subbatch_student(subbatchID, studentID,created_by,created_date,updated_by,updated_date) 
                VALUES " . $sqlValues;
        try {
            return $this->executeQueryForObject($sql, true);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    
    public function getSubbatchDetailsByRequest($request)
    {
        $request = $this->realEscapeObject($request);
        $condition = "";
        $condition .= $request->staffId?" AND sbs.staffID = $request->staffId ":"";
        $condition .= $request->batchId?" AND sbs.batchID = $request->batchId ":"";
        $condition .= $request->subjectId?" AND sbs.subjectID = $request->subjectId ":"";
        $condition .= $request->semId?" AND sbs.semID = $request->semId ":"";
        $condition .= $request->sbsId?" AND sbs.sbsId = $request->sbsId ":"";
        $condition .= $request->subbatchId?" AND ssbs.subbatchID = $request->subbatchId ":"";
        $condition .= (int)$request->pseudosubjectId?" AND psbs.pseudosubjectID = $request->pseudosubjectId ":"";
        if($condition)
        {
            try {
                $sql = "SELECT sbs.sbsID,group_concat(sub.subbatchID) as subbatchID,psbs.pseudosubjectID,pst.code FROM sbs_relation sbs
                LEFT JOIN subbatch_sbs ssbs ON ssbs.sbsID = sbs.sbsID
                LEFT JOIN subbatches sub ON sub.subbatchID = ssbs.subbatchID AND sub.batchID = sbs.batchID AND sub.semID = sbs.semID
                LEFT JOIN pseudosubjects_sbs psbs ON psbs.sbsID = sbs.sbsID
                LEFT JOIN pseudosubjects ps ON ps.pseudosubjectID = psbs.pseudosubjectID AND ps.isFinalized = 1
                INNER JOIN pseudo_subject_type pst on pst.id = ps.pseudo_subject_type_id
                WHERE 1=1 $condition group by sbs.sbsID;";
                return $this->executeQueryForList($sql);
            } catch (\Exception $e) {
                throw new ProfessionalException($e->getCode(), $e->getMessage());
            }
        }
        else {
            return false;
        }
    }
    /**
     * @param $sbsId
     * @return Object
     * @throws ProfessionalException
     * @author sibin
     */
    public function getSubBatchBySBS($sbsId)
    {
        $sbsId = $this->realEscapeString($sbsId);
        $sql = "SELECT ssbs.subbatchID from subbatch_sbs ssbs 
                    where ssbs.sbsID='$sbsId'";
        try {
            return $this->executeQueryForObject($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $sbsIds
     * @return Object
     * @throws ProfessionalException
     * @author sibin
     */
    public function getSubBatchesBySBS($staffDetails)
    {
        $sbsIdString="";
        $staffDetails = $this->realEscapeArray($staffDetails);
        foreach($staffDetails as $staff){
            if($sbsIdString){
                $sbsIdString= $sbsIdString. "$staff->sbsID";
            }else{
                $sbsIdString ="$staff->sbsID";
            }
        }
        $sql = "SELECT distinct ssbs.subbatchID from subbatch_sbs ssbs 
                    where ssbs.sbsID in ($sbsIdString)";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $sbsId
     * @return Object
     * @throws ProfessionalException
     * @author sibin
     */
    public function deleteSubbatch($id)
    {
        $id = $this->realEscapeString($id);
        if(!$id){
            throw new ProfessionalException(ProfessionalException::INVALID_PSEUDO_SUBJECT_ID,'Invalid Sub-Batch');
        }
        $deleteStudentsExamMarks = "DELETE sm FROM student_marks sm 
        INNER JOIN exam e ON e.examID = sm.examID
        WHERE e.subbatchID = '$id';";
        $deleteStudentsExams = "DELETE FROM exam WHERE subbatchID = '$id'";
        $deleteStudents = "DELETE FROM subbatch_student WHERE subbatchID = '$id'";
        $deleteSubBatchSbs = "DELETE FROM subbatch_sbs WHERE subbatchID = '$id'";
        $deleteSubBatch = "DELETE FROM subbatches WHERE subbatchID = '$id'";
        try {
            $this->executeQuery($deleteStudentsExamMarks);
            $this->executeQuery($deleteStudentsExams);
            $this->executeQuery($deleteStudents);
            $this->executeQuery($deleteSubBatchSbs);
            $this->executeQuery($deleteSubBatch);
    } catch (\Exception $e) {
        throw new ProfessionalException($e->getCode(), $e->getMessage());
    }
    }   
    
    /**
     * @param $subBatchId
     * @return Array
     * @throws ProfessionalException
     * @author Ajay
     */
    public function getStudentsFromSubBatch($subBatchId)
    {
        $staffDetails = $this->realEscapeString($subBatchId);
        $sql = "SELECT s.studentID,s.studentName  FROM subbatch_student ss INNER JOIN studentaccount s ON ss.studentID = s.studentID  WHERE ss.subbatchID =$subBatchId";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    /**
     * @param $request
     * @return Object
     * @throws ProfessionalException
     * @author Sibin
     */
    public function getSubBatchByBatchExam($request)
    {
        $request = $this->realEscapeObject($request);
        $condition = "";
        $joinSubBatch = "LEFT";
        if($request->subBatchId){
            $condition = " AND e.subbatchID IN ($request->subBatchId)";
        }
        if($request->getSubBatchOnly){
            $joinSubBatch = "INNER";
        }
        $sql = "SELECT e.created_by,e.subbatchID,e.examID,s.subbatchName from exam e
                $joinSubBatch JOIN subbatches s ON s.subbatchID = e.subbatchID 
                where e.batchID='$request->batchId' and e.semID='$request->semId' and e.examTypeID='$request->examTypeId' and e.subjectID = '$request->subjectId$condition";
        try {
            if($request->getList){
                    return $this->executeQueryForList($sql);
            }else{
                    return $this->executeQueryForObject($sql);
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
}