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 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 474
MalpracticeStudentModuleService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 12
9702.00
0.00% covered (danger)
0.00%
0 / 474
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 getAllStudentsForMalpractice
0.00% covered (danger)
0.00%
0 / 1
132.00
0.00% covered (danger)
0.00%
0 / 35
 getstudentsSubjectsForMalpractice
0.00% covered (danger)
0.00%
0 / 1
110.00
0.00% covered (danger)
0.00%
0 / 98
 saveStudentMalPracticeStatus
0.00% covered (danger)
0.00%
0 / 1
342.00
0.00% covered (danger)
0.00%
0 / 82
 saveBlockStudentMal
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 28
 validateSaveBlockStudentMal
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 4
 insertBlockStudentMal
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 deleteBlockStudentMal
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 23
 getBlockStudentMal
0.00% covered (danger)
0.00%
0 / 1
240.00
0.00% covered (danger)
0.00%
0 / 50
 getAllBlockedStudentsForMalpractice
0.00% covered (danger)
0.00%
0 / 1
72.00
0.00% covered (danger)
0.00%
0 / 28
 getstudentsSubjectsForAllowMalpractice
0.00% covered (danger)
0.00%
0 / 1
110.00
0.00% covered (danger)
0.00%
0 / 74
 allowMalPracticeStudent
0.00% covered (danger)
0.00%
0 / 1
72.00
0.00% covered (danger)
0.00%
0 / 37
<?php
namespace com\linways\ec\core\service;
use com\linways\ec\core\dto\FalseNumberSetting;
use com\linways\ec\core\dto\FalseNumberSettingExamLog;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\base\util\SecurityUtils;
use com\linways\ec\core\constant\StatusConstants;
use com\linways\ec\core\exception\ExamControllerException;
use com\linways\ec\core\mapper\MalpracticeStudentModuleServiceMapper;
use com\linways\ec\core\service\CommonExamService;
use com\linways\base\util\TwigRenderer;
use com\linways\core\ams\professional\util\PdfUtil;
use com\linways\core\ams\professional\dto\SettingsConstents;
use com\linways\core\ams\professional\service\StudentService;
use com\linways\ec\core\request\SearchRuleRequest;
use com\linways\ec\core\service\RuleService;
use com\linways\core\ams\professional\dto\examcontroller\exam\StudentMarkEditLog;
use com\linways\oe\core\service\ExamUserMarkService;
use com\linways\oe\core\request\ExamUserMarkRequest;
use com\linways\ec\core\service\ExamValuationService;
use com\linways\core\ams\professional\logging\AMSLogger;
use com\linways\ec\core\logging\Events;
use com\linways\ec\core\logging\entities\Staff;
class MalpracticeStudentModuleService extends BaseService
{
    use MakeSingletonTrait;
    private function __construct() {
        $this->logger = AMSLogger::getLogger('exam-controller-log');
        $this->mapper = MalpracticeStudentModuleServiceMapper::getInstance()->getMapper();
    }
     /**
     * get all students
     * @param $searchRequest
     * @return $malpracticeResponse
     */
    public function getAllStudentsForMalpractice($searchRequest){
        try{
            if(empty($searchRequest->examRegistrationId )){
                throw new ExamControllerException (ExamControllerException::INVALID_PARAMETERS,"Registration Missing!");
            }
            $students = $this->getstudentsSubjectsForMalpractice($searchRequest);
            if(empty($students)){
                throw new ExamControllerException (ExamControllerException::INVAILED_REGISTER_NUMBER,"No Students Found!");
            }
            else{
                $malpracticeResponse = new \stdClass();
                $searchRuleRequest = new SearchRuleRequest;
                $searchRuleRequest->name = "MALPRACTICE_STUDENT_RULE";
                $malpracticeRule = reset(RuleService::getInstance()->searchRule($searchRuleRequest))->rule;
                $malpracticeResponse->notes = [];
                $malpracticeResponse->allSubjectInRegistrationWillBeBlocked = $malpracticeRule->allSubjectInRegistrationWillBeBlocked == 1 ? true : false;
                $malpracticeResponse->isBlockSubjectInNextExams = $malpracticeRule->isBlockSubjectInNextExams == 1 ? true : false;
                if($malpracticeResponse->allSubjectInRegistrationWillBeBlocked){
                    $malpracticeResponse->notes[] = "If malpractice is identified in one subject during an exam registration, it will be presumed that malpractice extends to the other subjects within that same registration. Marks for all subjects in that registration will not be considered under such circumstances";
                }
                if($malpracticeResponse->isBlockSubjectInNextExams){
                    $malpracticeResponse->notes[] = "If malpractice is identified in one subject, the option to apply for consecutive supplementary exams will not be available.";
                }
                foreach($students as $student){
                    foreach($student->subjects as $subject){
                        $subject->isBlockAnotherExam = $subject->blockStudentTableId ? '1' : '0';
                    }
                }
                $malpracticeResponse->students = $students;
            }
        }
        catch (\Exception $e){
            throw new ExamControllerException($e->getCode(),$e->getMessage());
        }
        return $malpracticeResponse;
    }
    /**
     * get All Students By ExamRegistrationId and RegisterNo
     * @param $academicTermIds
     * @param $studentRegisterNo
     * @return $subjects
     */
    public function getstudentsSubjectsForMalpractice($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
       
        try{
            $whereQuery = null;
            $orderBy = " ORDER BY spa.properties->>'$.registerNumber' ASC , s.code ASC ";
            $whereQuery = "";
            if(!empty($searchRequest->registerNo)) {
                $registerNoString = is_array($searchRequest->registerNo) ? "'" . implode("','",$searchRequest->registerNo) . "'" : "'".$searchRequest->registerNo."'";
                $whereQuery .= " AND spa.properties->>'$.registerNumber' IN ( $registerNoString )";
            }
            if(!empty($searchRequest->rollNo)) {
                $rollNoString = is_array($searchRequest->rollNo) ? "'" . implode("','",$searchRequest->rollNo) . "'" : "'".$searchRequest->rollNo."'";
                $whereQuery .= " AND spa.properties->>'$.rollNumber' IN ( $rollNoString )";
            }
            if(!empty($searchRequest->groupId)) {
                $groupIdString = is_array($searchRequest->groupId) ? "'" . implode("','",$searchRequest->groupId) . "'" : "'".$searchRequest->groupId."'";
                $whereQuery .= " AND g.id IN ( $groupIdString )";
            }
            if(!empty($searchRequest->examRegistrationId)) {
                $examRegistrationIdString = is_array($searchRequest->examRegistrationId) ? "'" . implode("','",$searchRequest->examRegistrationId) . "'" : "'".$searchRequest->examRegistrationId."'";
                $whereQuery .= " AND eer.id IN ( $examRegistrationIdString )";
            }
            $query = "SELECT
                            DISTINCT  esar.am_assessment_id as id,
                            esar.am_assessment_id as assessmentId,
                            sa.studentID AS studentAccId,
                            sa.studentName,
                            g.name AS groupName,
                            eer.name AS examRegName,
                            g.id AS groupId,
                            s.code as subjectCode,
                            s.name as subjectName,
                            IF(aps.properties ->> '$.classType' = 'THEORY',1,0) AS isTheory,
                            spa.properties->>'$.registerNumber' as regNo,
                            spa.properties->>'$.rollNumber' as rollNo,
                            aps.id as academicPaperSubjectId,
                            eer.id as examRegistrationId,
                            ostm.mark_obtained  as obtainedExternal,
                            ostm.attendance_status as attendanceStatus,
                            ostm.properties as markProperties,
                            act.id AS academicTermId,
                            act.name AS academicTermName,
                            oe.id AS oeExamId,
                            aps.properties ->> '$.isInternal' as isInternal,
                            aps.properties ->> '$.isExternal' as isExternal,
                            aps.properties ->> '$.externalMaxMark' as externalMaxMark,
                            aps.properties ->> '$.internalMaxMark' as internalMaxMark,
                            aps.properties ->> '$.externalMaxMark' + aps.properties ->> '$.internalMaxMark' as totalMaxMark,
                            embus.id as blockStudentTableId
                        FROM
                            ec_student_assessment_registration esar
                        INNER JOIN studentaccount sa ON 
                            sa.studentID = esar.student_id
                        INNER JOIN student_program_account spa
                            ON spa.student_id = sa.studentID
                        INNER JOIN ec_exam_registration_subject eers ON 
                            eers.am_assessment_id = esar.am_assessment_id
                        INNER JOIN  cm_academic_paper_subjects aps ON 
                            aps.id = eers.cm_academic_paper_subjects_id
                        INNER JOIN v4_ams_subject s ON 
                            s.id = aps.ams_subject_id
                        INNER JOIN ec_exam_registration_batch eerb ON
                            eerb.id = eers.ec_exam_registration_batch_id
                        INNER JOIN ec_exam_registration eer ON
                            eer.id = eerb.ec_exam_registration_id AND eer.type = esar.ec_exam_registration_type
                        INNER JOIN `groups` g ON
                            g.id = eerb.groups_id
                        INNER JOIN groups_relations gr ON 
                            gr.parent_groups_id = g.id
                        INNER JOIN `groups` sg ON 
                            sg.id = gr.child_groups_id AND sg.type = 'SUBJECT'
                        INNER JOIN group_members sgm ON
                            sgm.groups_id = sg.id AND 
                            sgm.academic_status IN ('ACTIVE') AND
                            sgm.members->>'$.studentId' = spa.id
                        INNER JOIN program p ON 
                            p.id = g.properties->>'$.programId'
                        INNER JOIN cm_academic_paper ap ON 
                            aps.cm_academic_paper_id = ap.id
                        INNER JOIN  academic_term act ON 
                            act.id = CAST(eerb.properties ->> '$.academicTermId'AS CHAR)
                        INNER JOIN cm_syllabus_academic_term_settings str ON 
                            str.id = ap.cm_syllabus_academic_term_settings_id 
                        INNER JOIN oe_exams oe ON
                            oe.assessment_id = eers.am_assessment_id AND oe.is_deleted = 0
                        LEFT JOIN oe_student_total_mark ostm ON 
                            ostm.student_id = sa.studentID AND ostm.am_assessment_id = esar.am_assessment_id AND (ostm.valuation_type IS NULL OR ostm.valuation_type = '') AND ostm.valuation_count = 'FINALIZED'
                        LEFT JOIN ec_malpractice_block_unblock_students embus ON 
                            embus.student_id = sa.studentID AND embus.am_assessment_id = esar.am_assessment_id AND embus.ec_exam_registration_id = eer.id AND embus.type = 'BLOCK'
                        WHERE
                            1 = 1 AND esar.am_assessment_id = eers.am_assessment_id AND esar.ec_exam_registration_type = eer.type AND CAST(esar.properties ->> '$.registrationStatus' AS CHAR) = 'REGISTERED' AND  CAST(esar.properties ->> '$.feeStatus' AS CHAR) = 'PAID' ";
                $students =  $this->executeQueryForList($query.$whereQuery.$orderBy, $this->mapper[MalpracticeStudentModuleServiceMapper::GET_STUDENT_SUBJECT_DETAILS]);
        }
        catch (\Exception $e)
        {
            throw new ExamControllerException($e->getCode(),$e->getMessage());
        }
        return $students;
    }
    /**
     * save Student Mal practice status
     * @param $searchRequest 
     * @return $student 
     * @author Krishnajith
     */
    public function saveStudentMalPracticeStatus($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
        try{
            $staffId = $GLOBALS['userId'];
            $insertionValues = [];
            $properties = new \Stdclass();
            $properties->valuationType = "FINALIZED";
            $properties = !empty($properties) ? "'" . json_encode($properties) . "'" : "NULL";
            $studentDetails = $searchRequest->studentDetails;
            if(empty($studentDetails)){
                throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"No Student(s) Found");
            }
            $searchRuleRequest = new SearchRuleRequest;
            $searchRuleRequest->name = "MALPRACTICE_STUDENT_RULE";
            $malpracticeRule = reset(RuleService::getInstance()->searchRule($searchRuleRequest))->rule;
            $malpracticeResponse->notes = [];
            $malpracticeResponse->allSubjectInRegistrationWillBeBlocked = $malpracticeRule->allSubjectInRegistrationWillBeBlocked == 1 ? true : false;
            $malpracticeResponse->isBlockSubjectInNextExams = $malpracticeRule->isBlockSubjectInNextExams == 1 ? true : false;
           
            foreach($studentDetails->subjects as $subject){
                if($malpracticeResponse->allSubjectInRegistrationWillBeBlocked){
                    if(!empty($studentDetails->studentId) && !empty($subject->assessmentId) && ($subject->attendanceStatus != 'MALPRACTICE')){
                        $attendanceStatus = "MALPRACTICE";
                        $markEntryObj = new \stdClass();
                        $markEntryObj->studentId = $studentDetails->studentId;
                        $markEntryObj->assessmentId = $subject->assessmentId;
                        $markEntryObj->oeExamsId = $subject->oeExamId; 
                        $markEntryObj->markObtained = null;
                        $markEntryObj->attendanceStatus = $attendanceStatus;
                        $markEntryObj->valuationCount = 'FINALIZED';
                        $markproperties = "";
                        if($subject->markProperties){
                            $markproperties = $subject->markProperties;
                        }
                        $markEntryObj->properties  = $markproperties;
                        $markEntryObj->valuationType = null;
                        $insertionValues[] = $markEntryObj;
                        $log = new StudentMarkEditLog();
                        $log->oldMark = $subject->obtainedExternal;;
                        $log->newMark = null;
                        $log->remarks = "Block Malpractice ";
                        $log->markType = "EXTERNAL_MARK";
                        $log->actionTakenTime = date("d-m-Y h:i A");
                        $log->updatedStaffId = $staffId;
                        $log->updatedStaffType = "EXAM_CONTROLLER";
                        $logRequest = new \stdClass();
                        $logRequest->studentId =   $studentDetails->studentId;
                        $logRequest->assessmentId = $subject->assessmentId;
                        $logRequest->staffId = $staffId;
                        $logRequest->log = $log;
                        $insertionExtrnalMarkLogTableArray[] = $logRequest;
                    }
                    if($malpracticeResponse->isBlockSubjectInNextExams){
                        if($subject->isBlockAnotherExam == '1'){
                            $blockStudentMal = new \stdClass();
                            $blockStudentMal->studentId =   $studentDetails->studentId;
                            $blockStudentMal->assessmentId = $subject->assessmentId;
                            $blockStudentMal->academicPaperSubjectId = $subject->academicPaperSubjectId;
                            $blockStudentMal->examRegistrationId = $subject->examRegistrationId;
                            $blockStudentMal->type = 'BLOCK';
                            $blockStudentMal->staffId = $staffId;
                            $this->saveBlockStudentMal($blockStudentMal);
                        }
                        else{
                            if($subject->blockStudentTableId){
                                $this->deleteBlockStudentMal($subject->blockStudentTableId);
                            }
                        }
                    }
                }
            }
            if( !empty ( $insertionValues ) ){
                ExamUserMarkService::getInstance()->saveExamUserTotalMark($insertionValues);
            }
            if(! empty($insertionExtrnalMarkLogTableArray)){
                foreach($insertionExtrnalMarkLogTableArray as $logRequest){
                    ExamValuationService::getInstance()->saveExamEditLog($logRequest);
                }
            }
        }
        catch (\Exception $e){
            throw new ExamControllerException($e->getCode(),$e->getMessage());
        }
    }
    /**
     * Save Block Student Mal
     * @param  $blockStudentMal
     * @return $id
     */
    public function saveBlockStudentMal($blockStudentMal){
        $blockStudentMal = $this->realEscapeObject($blockStudentMal);
        $blockStudentMal->createdBy = $GLOBALS['userId'];
        $blockStudentMal->updatedBy = $GLOBALS['userId'];
        $staffId = $GLOBALS['userId'];
        try {
            $this->validateSaveBlockStudentMal($blockStudentMal);
            $blockStudentMal->id = $this->insertBlockStudentMal($blockStudentMal);
            AMSLogger::log_info($this->logger,Events::EC_SAVE_STUDENT_BLOCK_MAL, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $blockStudentMal,
                "status" => StatusConstants::SUCCESS
            ]);
        } catch (\Exception $e) {
            AMSLogger::log_error($this->logger,Events::EC_SAVE_STUDENT_BLOCK_MAL, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $blockStudentMal,
                "errorCode" => $e->getCode(),
                "errorMessage" => $e->getMessage(),
                "status" => StatusConstants::FAILED
            ]);
            if ($e->getCode() !== ExamControllerException::INVALID_PARAMETERS && $e->getCode() !== ExamControllerException::EMPTY_PARAMETERS && $e->getCode() !== "DUPLICATE_ENTRY") {
                throw new ExamControllerException($e->getCode(), "Failed to save student block mal status ! Please try again");
            } else {
                throw new ExamControllerException($e->getCode(), $e->getMessage());
            }
        }
        return $blockStudentMal->id;
    }
    /**
     * Validate Block Student Reason Request Before Saving
     * @param $blockStudentMal
     * @return NULL
     */
    private function validateSaveBlockStudentMal( $blockStudentMal){
        if (empty($blockStudentMal->studentId) || empty($blockStudentMal->examRegistrationId) || empty($blockStudentMal->assessmentId) || empty($blockStudentMal->type) || empty($blockStudentMal->academicPaperSubjectId)){
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, "Empty Parameter!");
        }
    }
     /**
     * Insert Block Student Mal
     * @param  $blockStudentMal
     * @return  $id
     */
    private function insertBlockStudentMal( $blockStudentMal){
        $properties = !empty($blockStudentMal->properties) ? "'" . json_encode($blockStudentMal->properties) . "'" : "'{}'";
        $query = "INSERT INTO ec_malpractice_block_unblock_students
                  (student_id,type,ec_exam_registration_id,cm_academic_paper_subjects_id,am_assessment_id,properties,created_by,updated_by)
                  VALUES
                  ('$blockStudentMal->studentId','$blockStudentMal->type','$blockStudentMal->examRegistrationId','$blockStudentMal->academicPaperSubjectId','$blockStudentMal->assessmentId',$properties,'$blockStudentMal->createdBy','$blockStudentMal->updatedBy') ON DUPLICATE KEY UPDATE `updated_by` = VALUES(updated_by)";
        try {
            $blockStudentMal->id = $this->executeQuery($query,true)->id;
            return $blockStudentMal->id;
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
    }
     /**
     * Delete Block Student Mal 
     * @param String $id
     * @return NULL
     */
    public function deleteBlockStudentMal($id){
        $id = $this->realEscapeString($id);
        $staffId = $GLOBALS['userId'];
        $blockStudentMal = new \stdClass();
        $blockStudentMal->id = $id;
        $blockStudentMalDetails = reset($this->getBlockStudentMal($blockStudentMal));
        if(empty($blockStudentMalDetails)){
            throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't delete ! Block reason missing.");
        }
        $query = "DELETE FROM
                    ec_malpractice_block_unblock_students
                WHERE
                    id = '$id'";
        try {
            $this->executeQuery($query);
             // LOGGING
            AMSLogger::log_info($this->logger,Events::EC_DELETE_STUDENT_BLOCK_MAL, [
                "staff" => new Staff(["id" => $staffId]),
                "request" => $blockStudentMalDetails,
                "status" => StatusConstants::SUCCESS
            ]);
        }catch (\Exception $e) {
            throw new ExamControllerException(ExamControllerException::HAVE_RELATION,"Error deleting Block malpractice ! Please try again");
        }
    }
    /**
     * get Block Student Mal Status
     * @param $searchRequest
     * @return $blockStudentMal
     */
    public function getBlockStudentMal($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
        try {
            $whereQuery = null;
            $whereQuery = "";
            if (!empty($searchRequest->id)) {
                $idString = is_array($searchRequest->id) ? "'" . implode("','", $searchRequest->id) . "'" : "'" . $searchRequest->id . "'";
                $whereQuery .= " AND embus.id IN ( $idString )";
            }
            if (!empty($searchRequest->studentId)) {
                $studentIdString = is_array($searchRequest->studentId) ? "'" . implode("','", $searchRequest->studentId) . "'" : "'" . $searchRequest->studentId . "'";
                $whereQuery .= " AND embus.student_id IN ( $studentIdString )";
            }
            if (!empty($searchRequest->examRegistrationId)) {
                $examRegistrationIdString = is_array($searchRequest->examRegistrationId) ? "'" . implode("','", $searchRequest->examRegistrationId) . "'" : "'" . $searchRequest->examRegistrationId . "'";
                $whereQuery .= " AND embus.ec_exam_registration_id IN ( $examRegistrationIdString )";
            }
            if (!empty($searchRequest->assessmentId)) {
                $assessmentIdString = is_array($searchRequest->assessmentId) ? "'" . implode("','", $searchRequest->assessmentId) . "'" : "'" . $searchRequest->assessmentId . "'";
                $whereQuery .= " AND embus.am_assessment_id IN ( $assessmentIdString )";
            }
            if (!empty($searchRequest->type)) {
                $blockingTypeString = is_array($searchRequest->type) ? "'" . implode("','", $searchRequest->type) . "'" : "'" . $searchRequest->type . "'";
                $whereQuery .= " AND embus.type IN ( $blockingTypeString )";
            }
            if (!empty($searchRequest->academicPaperSubjectId)) {
                $academicPaperSubjectIdString = is_array($searchRequest->academicPaperSubjectId) ? "'" . implode("','", $searchRequest->academicPaperSubjectId) . "'" : "'" . $searchRequest->academicPaperSubjectId . "'";
                $whereQuery .= " AND embus.cm_academic_paper_subjects_id IN ( $academicPaperSubjectIdString )";
            }
            $query = "SELECT
                            DISTINCT 
                            embus.id as id,
                            embus.student_id as studentId,
                            embus.ec_exam_registration_id as examRegistrationId,
                            embus.type as type,
                            embus.am_assessment_id as assessmentId,
                            embus.cm_academic_paper_subjects_id as academicPaperSubjectId,
                            embus.properties
                        FROM
                            ec_malpractice_block_unblock_students embus
                        WHERE
                            1 = 1 ";
            $blockStudentMals =  $this->executeQueryForList($query . $whereQuery);
        } catch (\Exception $e) {
            throw new ExamControllerException($e->getCode(), $e->getMessage());
        }
        foreach($blockStudentMals as $blockStudentMal){
            $blockStudentMal->properties = json_decode($blockStudentMal->properties);
        }
        return $blockStudentMals;
    }
     /**
     * getAll Blocked Students
     * @param $searchRequest
     * @return $malpracticeResponse
     */
    public function getAllBlockedStudentsForMalpractice($searchRequest){
        try{
            if(empty($searchRequest->examRegistrationId )){
                throw new ExamControllerException (ExamControllerException::INVALID_PARAMETERS,"Registration Missing!");
            }
            $searchRequest->isBlockedStudentsOnly = true;
            $students = $this->getstudentsSubjectsForAllowMalpractice($searchRequest);
            if(empty($students)){
                throw new ExamControllerException (ExamControllerException::INVAILED_REGISTER_NUMBER,"No Students Found!");
            }
            else{
                $malpracticeResponse = new \stdClass();
                $searchRuleRequest = new SearchRuleRequest;
                $searchRuleRequest->name = "MALPRACTICE_STUDENT_RULE";
                $malpracticeRule = reset(RuleService::getInstance()->searchRule($searchRuleRequest))->rule;
                $malpracticeResponse->notes = [];
                $malpracticeResponse->allSubjectInRegistrationWillBeBlocked = $malpracticeRule->allSubjectInRegistrationWillBeBlocked == 1 ? true : false;
                $malpracticeResponse->isBlockSubjectInNextExams = $malpracticeRule->isBlockSubjectInNextExams == 1 ? true : false;
                foreach($students as $studentSubject){
                    $studentSubject->unBlockStudentMalStatus = $studentSubject->unBlockStudentTableId ? '1' : '0';
                }
                $malpracticeResponse->students = $students;
            }
        }
        catch (\Exception $e){
            throw new ExamControllerException($e->getCode(),$e->getMessage());
        }
        return $malpracticeResponse;
    }
    /**
     * get All Students By ExamRegistrationId and RegisterNo
     * @param $academicTermIds
     * @param $studentRegisterNo
     * @return $subjects
     */
    public function getstudentsSubjectsForAllowMalpractice($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
       
        try{
            $whereQuery = null;
            $orderBy = " ORDER BY spa.properties->>'$.registerNumber' ASC , s.code ASC ";
            $whereQuery = "";
            if(!empty($searchRequest->registerNo)) {
                $registerNoString = is_array($searchRequest->registerNo) ? "'" . implode("','",$searchRequest->registerNo) . "'" : "'".$searchRequest->registerNo."'";
                $whereQuery .= " AND spa.properties->>'$.registerNumber' IN ( $registerNoString )";
            }
            if(!empty($searchRequest->rollNo)) {
                $rollNoString = is_array($searchRequest->rollNo) ? "'" . implode("','",$searchRequest->rollNo) . "'" : "'".$searchRequest->rollNo."'";
                $whereQuery .= " AND spa.properties->>'$.rollNumber' IN ( $rollNoString )";
            }
            if(!empty($searchRequest->groupId)) {
                $groupIdString = is_array($searchRequest->groupId) ? "'" . implode("','",$searchRequest->groupId) . "'" : "'".$searchRequest->groupId."'";
                $whereQuery .= " AND g.id IN ( $groupIdString )";
            }
            if(!empty($searchRequest->examRegistrationId)) {
                $examRegistrationIdString = is_array($searchRequest->examRegistrationId) ? "'" . implode("','",$searchRequest->examRegistrationId) . "'" : "'".$searchRequest->examRegistrationId."'";
                $whereQuery .= " AND eer.id IN ( $examRegistrationIdString )";
            }
            $query = "SELECT DISTINCT 
                        sa.studentID AS studentAccId,
                        sa.studentName,
                        g.name AS groupName,
                        eer.name AS examRegName,
                        eerParent.name AS blockedExamRegName,
                        eers.am_assessment_id AS assessmentId,
                        g.id AS groupId,
                        s.code as subjectCode,
                        s.name as subjectName,
                        IF(aps.properties ->> '$.classType' = 'THEORY',1,0) AS isTheory,
                        spa.properties->>'$.registerNumber' as regNo,
                        spa.properties->>'$.rollNumber' as rollNo,
                        aps.id as academicPaperSubjectId,
                        eer.id as examRegistrationId,
                        aps.properties ->> '$.isInternal' as isInternal,
                        aps.properties ->> '$.isExternal' as isExternal,
                        aps.properties ->> '$.externalMaxMark' as externalMaxMark,
                        aps.properties ->> '$.internalMaxMark' as internalMaxMark,
                        aps.properties ->> '$.externalMaxMark' + aps.properties ->> '$.internalMaxMark' as totalMaxMark,
                        embus.id as unBlockStudentTableId
                    FROM
                        ec_exam_registration eer
                    INNER JOIN ec_exam_registration_batch eerb ON
                        eerb.ec_exam_registration_id = eer.id
                    INNER JOIN ec_exam_registration_subject eers ON 
                        eers.ec_exam_registration_batch_id = eerb.id
                    INNER JOIN  cm_academic_paper_subjects aps ON 
                        aps.id = eers.cm_academic_paper_subjects_id
                    INNER JOIN v4_ams_subject s ON 
                        s.id = aps.ams_subject_id
                    INNER JOIN `groups` g ON
                        g.id = eerb.groups_id
                    INNER JOIN ec_malpractice_block_unblock_students blockStd ON 
                        blockStd.type = 'BLOCK' AND blockStd.cm_academic_paper_subjects_id = aps.id
                    INNER JOIN studentaccount sa ON 
                        sa.studentID = blockStd.student_id
                    INNER JOIN  ec_exam_registration eerParent
                        ON eerParent.id = blockStd.ec_exam_registration_id
                    INNER JOIN student_program_account spa
                        ON spa.student_id = sa.studentID
                    LEFT JOIN ec_malpractice_block_unblock_students embus ON 
                        embus.student_id = sa.studentID AND embus.am_assessment_id = eers.am_assessment_id AND embus.ec_exam_registration_id = eer.id AND embus.type = 'UN_BLOCK'
                    WHERE
                        1 = 1 ";
                $students =  $this->executeQueryForList($query.$whereQuery.$orderBy);
        }
        catch (\Exception $e)
        {
            throw new ExamControllerException($e->getCode(),$e->getMessage());
        }
        return $students;
    }
    /**
     * save Student Mal practice allow status
     * @param $searchRequest 
     * @return $student 
     * @author Krishnajith
     */
    public function allowMalPracticeStudent($searchRequest){
        $searchRequest = $this->realEscapeObject($searchRequest);
            try{
            $staffId = $GLOBALS['userId'];
            $insertionValues = [];
            $properties = new \Stdclass();
            $studentDetails = $searchRequest->studentDetails;
            if(empty($studentDetails)){
                throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"No Student(s) Found");
            }
            $searchRuleRequest = new SearchRuleRequest;
            $searchRuleRequest->name = "MALPRACTICE_STUDENT_RULE";
            $malpracticeRule = reset(RuleService::getInstance()->searchRule($searchRuleRequest))->rule;
            $malpracticeResponse->notes = [];
            $malpracticeResponse->allSubjectInRegistrationWillBeBlocked = $malpracticeRule->allSubjectInRegistrationWillBeBlocked == 1 ? true : false;
            $malpracticeResponse->isBlockSubjectInNextExams = $malpracticeRule->isBlockSubjectInNextExams == 1 ? true : false;
            if($malpracticeResponse->isBlockSubjectInNextExams){
                if($studentDetails->unBlockStudentMalStatus == '1'){
                    $blockStudentMal = new \stdClass();
                    $blockStudentMal->studentId =   $studentDetails->studentAccId;
                    $blockStudentMal->assessmentId = $studentDetails->assessmentId;
                    $blockStudentMal->academicPaperSubjectId = $studentDetails->academicPaperSubjectId;
                    $blockStudentMal->examRegistrationId = $studentDetails->examRegistrationId;
                    $blockStudentMal->type = 'UN_BLOCK';
                    $blockStudentMal->staffId = $staffId;
                    $this->saveBlockStudentMal($blockStudentMal);
                }
                else{
                    if($studentDetails->unBlockStudentTableId){
                        $this->deleteBlockStudentMal($studentDetails->unBlockStudentTableId);
                    }
                }
            }
        }
        catch (\Exception $e){
            throw new ExamControllerException($e->getCode(),$e->getMessage());
        }
    }
    
    
}