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 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 443
Template16SupplementaryIndividualResultGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 5
8010.00
0.00% covered (danger)
0.00%
0 / 443
 __construct
n/a
0 / 0
1
n/a
0 / 0
 getTemplateName
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
 processData
0.00% covered (danger)
0.00%
0 / 1
240.00
0.00% covered (danger)
0.00%
0 / 72
 renderIndividualMarkCardResult
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 32
 processSupplementaryRegularStudentData
0.00% covered (danger)
0.00%
0 / 1
4556.00
0.00% covered (danger)
0.00%
0 / 317
 getSgpaByMonthYear
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 15
<?php
//Template Individual Generator For SAINTGITS Colleges
namespace com\linways\ec\core\service\SupplementaryIndividualMarkCardGenerator;
use com\linways\ec\core\service\StudentMarkListService;
use com\linways\ec\core\service\ExamRegistrationService;
use com\linways\ec\core\exception\ExamControllerException;
use com\linways\base\util\TwigRenderer;
use com\linways\core\ams\professional\util\PdfUtil;
use com\linways\ec\core\service\CommonExamService;
use com\linways\ec\core\service\SupplementaryIndividualMarkCardGenerator\SupplementaryIndividualMarkCardResultDataGenerator;
use com\linways\core\ams\professional\util\CommonUtil;
use TCPDFBarcode;
use com\linways\core\ams\professional\service\AmsCustomFieldsService;
use com\linways\core\ams\professional\constant\AmsCustomFieldsEntities;
use com\linways\ec\core\service\GradeSchemeService;
use com\linways\ec\core\service\MarksCardService;
use com\linways\core\ams\professional\service\academic\AcademicTermService;
use com\linways\core\ams\professional\request\academic\SearchAcademicTermRequest;
use com\linways\core\ams\professional\service\StudentService;
use com\linways\ec\core\service\ExamValuationService;
class Template16SupplementaryIndividualResultGenerator extends SupplementaryIndividualMarkCardResultDataGenerator
{
    public function __construct(){}
    
    protected function getTemplateName($request){
        $templateName = "template_16";
        $examRegistrationDetailsArray = ExamRegistrationService::getInstance()->searchDetailedExamRegistrationDetails($request);
        if(empty($examRegistrationDetailsArray)){
            throw new ExamControllerException(ExamControllerException::NO_DETAILS_FOUND,"No Details Found");
        }
        return $templateName;
    }
     /**
     * Process Student data college base
     *
     * @param $request
     */
    protected function processData($request){
        $response = new \stdClass;
        $studentsMarkDetails = [];
        
            $examRegistrationData = StudentMarkListService::getInstance()->getExamRegistrationDetailsDataForConsolidatedAndIndividualReports($request);
            if(empty($examRegistrationData)){
                throw new ExamControllerException(ExamControllerException::NO_DETAILS_FOUND,"No Exam Found");
            }
            $searchRequest = new \stdClass();
            $searchRequest->groupId = $request->groupId;
            $searchRequest->studentId = $request->studentId;
            $searchRequest->selectedSemesters = $examRegistrationData->academicTermId;
            $examRegistrationData->examTypeName =  $examRegistrationData->properties->isSpecialExam == '1' ? 'Additional Exam' : 'ATKT Exam';
            $examRegistrationData->isSpecialExam =  $examRegistrationData->properties->isSpecialExam;
            $studentsMarkDetails = StudentMarkListService::getInstance()->getAllRegistredStudentMarkDetailsDummyData($searchRequest);
            if(empty($studentsMarkDetails)){
                throw new ExamControllerException(ExamControllerException::NO_DETAILS_FOUND,"No Details Found");
            }
            if( $request->isStudentSideExamResult == '1' && !$request->groupId){
                $request->groupId = reset($studentsMarkDetails)->studentDetails->batchId;
                $examRegistrationData = StudentMarkListService::getInstance()->getExamRegistrationDetailsDataForConsolidatedAndIndividualReports($request);
                $examRegistrationData->examTypeName =  $examRegistrationData->properties->isSpecialExam == '1' ? 'Additional Exam' : 'ATKT Exam';
                $examRegistrationData->isSpecialExam =  $examRegistrationData->properties->isSpecialExam;
            }
            $studentsMarkDetails = $this->processSupplementaryRegularStudentData($studentsMarkDetails,$request,$examRegistrationData);
        
            $examRegistrationData->resultDeclaredDate = $examRegistrationData->publishingStartDate ? date("d M Y", strtotime( $examRegistrationData->publishingStartDate)) : "";
            if( $request->displayContentOptions->enableDateFlag == '1'){
                if($request->displayContentOptions->markListDate ){
                    $examRegistrationData->resultDeclaredDate = date("d M Y", strtotime($request->displayContentOptions->markListDate));
                }
            }
        $gradeSchemeRequest = new \stdClass;
        $gradeSchemeRequest->groupId = $examRegistrationData->groupId;
        $gradeSchemeRequest->orderBy = "ASC";
        $subjectGrades = GradeSchemeService::getInstance()->getAllSubjectGradeSchemes($gradeSchemeRequest);
        array_walk($subjectGrades, function($grade){
            $grade->properties = json_decode($grade->properties);
            $grade->percentTo = $grade->range_to;
            $grade->percentFrom = $grade->range_from;
            $grade->class = $grade->properties->class;
            $grade->gradePoint = $grade->properties->gradePoint;
        });
        $minPercentTo = min(array_column($subjectGrades, "percentTo"));
        $maxPercentTo = max(array_column($subjectGrades, "percentTo"));
        $gradeDiplayArray = [];
        foreach ($subjectGrades as $key => $subjectGrade) {
            if ($subjectGrade->percentTo == $maxPercentTo) {
                $gradeDiplayArray[$subjectGrade->percentTo]->name = sprintf('%0.2f',  floor($subjectGrade->percentFrom * 100) /100) . " and above";
            }
            else if($subjectGrade->percentTo == $minPercentTo) {
                $gradeDiplayArray[$subjectGrade->percentTo]->name = "Below " . round($subjectGrade->percentTo);
            }
            else {
                $gradeDiplayArray[$subjectGrade->percentTo]->name = sprintf('%0.2f', floor($subjectGrade->percentFrom * 100) /100) . " to " . sprintf('%0.2f', floor($subjectGrade->percentTo * 100) /100);
            }
            $gradeDiplayArray[$subjectGrade->percentTo]->grade = "$subjectGrade->name";
            $gradeDiplayArray[$subjectGrade->percentTo]->gradePoint = "$subjectGrade->gradePoint";
        }
        krsort($gradeDiplayArray);
        $gradeDiplayArray = array_values($gradeDiplayArray); 
        $examRegistrationData->grades =  $gradeDiplayArray;
        $response->studentData = $studentsMarkDetails->studentsDetails;
        $response->examRegistrationData = $examRegistrationData;
        $response->collegeData = CommonExamService::getInstance()->getCollegeDetails();
        if( $request->displayContentOptions->enableDateFlag == '1'){
            if(!$request->displayContentOptions->markListDate ){
                $request->displayContentOptions->markListDate = date("d-m-Y");
            }
            else{
                $request->displayContentOptions->markListDate = date("d-m-Y", strtotime($request->displayContentOptions->markListDate));
            }
        }
        $response->displayContentOptions = $request->displayContentOptions;
        return $response;
    }
     /**
     * Render Program Result
     *
     * @param $templateName
     * @param Object $data
     * @return Object
     */
    protected function renderIndividualMarkCardResult($templateName, $data){
        if(empty($data)){
            throw new ExamControllerException(ExamControllerException::NO_DETAILS_FOUND,"No Details Found");
        }
        else{
            $responseHtml = TwigRenderer::renderTemplateFileToHtml(realpath(DOCUMENT_ROOT."../examcontroller-api/src/com/linways/web/templates/SupplementaryIndividualMarkCards/Template16/$templateName.twig"), [ 'data'=>$data ]);
            $prtContent = NULL;
            $prtContent .= '<html><head>';
            $prtContent .= "<style>
                
                </style>";
            $prtContent .= '</head><title>Supplementary MarkList</title><body>';
            $prtContent .= $responseHtml;
            $prtContent .= '</body></html>';
            $totalWidth = 29.5;
            $totalHeight = 21;
            $options = array(
                'page-width'     => $totalWidth."cm",
                'page-height'    => $totalHeight."cm",
                'dpi'            => 96,
                'margin-top' => "4cm",
                'margin-left' => "1cm",
                'margin-right' => "1cm",
                'margin-bottom' => "0.5cm",
                'disable-smart-shrinking',
                'orientation'=> "portrait",
                // 'binary' => "/usr/local/bin/wkhtmltopdf", // For Mac
                'user-style-sheet' => realpath(DOCUMENT_ROOT . "/libcommon/bootstrap/css/bootstrap.min.css")
            );
            $programResult = new \stdClass;
            $programResult->dispalyHtmlData = $responseHtml;
            $programResult->printData = PdfUtil::renderPdf($prtContent, $options);
        return  $programResult;
        }
       
    }
    /**
     * processSupplementaryRegularStudentData
     * @param studentDetails
     * @return studentDetails
     * @author Krishnajith
     */
    public function processSupplementaryRegularStudentData($studentMarkDetails,$examRegistrationRequest,$examRegistrationDetails){
        $response = new \stdClass;
        $totalStudentDetails = new \stdClass;
        $totalStudentDetails->totalStudents = count($studentMarkDetails);
        $displaySubjectArray = [];
        $displaySubjectCategories = [];
        $studentsDetails = [];
        $getAcademicTermRequest = new SearchAcademicTermRequest();
        $getAcademicTermRequest->id  = $examRegistrationDetails->finalTermId;
        $getAcademicTermRequest->type = "SEMESTER";
        $finalSemDetails = reset(AcademicTermService::getInstance()->searchAcademicTerm($getAcademicTermRequest));
        $examSeatNoSerachRequest = new \stdClass;
        $examSeatNoSerachRequest->groupId = $examRegistrationRequest->groupId;
        $examSeatNoSerachRequest->examRegistrationId = $examRegistrationRequest->examRegistrationId;
        $examSeatNoSerachRequest->orderByRollNo = 1;
        $studentExamSeatNumbers = StudentMarkListService::getInstance()->getAllRegistredStudentMarkDetails($examSeatNoSerachRequest);
        foreach($studentMarkDetails as $student){
            $studentsDetails[$student->id]->slNo = "";
            $studentsDetails[$student->id]->id = $student->id;
            $studentsDetails[$student->id]->studentImage  = StudentService::getInstance()->getStudentProfilePic($student->id)->docpath;
            $customFields = AmsCustomFieldsService::getInstance()->getAmsCustomFieldValue(AmsCustomFieldsEntities::STUDENT, $student->id, []);
            if( $customFields ){
                $studentsDetails[$student->id]->ktuCode = CommonExamService::getInstance()->getCustomFieldObjectList($customFields)->studentData->KTU_CODE;
            }
            $studentSeatNo = reset(array_filter($studentExamSeatNumbers,function($value)use($student){
                return $value->id == $student->id;
            }));
            if ( empty($studentSeatNo->seatNo) && $examRegistrationDetails->isSpecialExam ){
                $regularExamRegistration = reset(array_filter($student->examRegistrations,function($value){
                    return $value->examType == 'REGULAR';
                }));
                $examSeatNoSerachRequest = new \stdClass;
                $examSeatNoSerachRequest->studentId = $student->id;
                $examSeatNoSerachRequest->examRegistrationId = $regularExamRegistration->id;
                $examSeatNoSerachRequest->orderByRollNo = 1;
                $studentExamSeatNumbers = StudentMarkListService::getInstance()->getAllRegistredStudentMarkDetails($examSeatNoSerachRequest);
                $studentSeatNo = reset($studentExamSeatNumbers);
            }
            if( $studentSeatNo ){
                $studentsDetails[$student->id]->universityExamSeatNo = $studentSeatNo->seatNo;
            }
            $studentsDetails[$student->id]->name = $student->studentDetails->name;
            $studentsDetails[$student->id]->myImage = $student->studentDetails->myImage;
            $studentsDetails[$student->id]->registerNo = $student->studentDetails->registerNo;
            $studentsDetails[$student->id]->rollNo = $student->studentDetails->rollNo;
            $studentsDetails[$student->id]->gender = $student->studentDetails->gender;
            $studentsDetails[$student->id]->motherName = $student->studentDetails->motherName;
            foreach($student->academicTerms as $academicTerm){
                $currentAcademicTerm = reset(array_filter(reset($student->academicTerms)->markHistory,function($value)use($examRegistrationRequest){
                    return $value->examRegistrationId == $examRegistrationRequest->examRegistrationId;
                }));
                $regularExamDetails = reset(array_filter(reset($student->academicTerms)->markHistory,function($value){
                    return $value->historyType == 'REGULAR';
                }));
                $examTermOrderNo = reset($student->academicTerms)->academicOrderNo;
                $studentsDetails[$student->id]->regularExamRegistrationId = $regularExamDetails->examRegistrationId;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->id = $academicTerm->id;
                $semesterNames = CommonExamService::getInstance()->getDifferentSemesterName($academicTerm->name);
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semInRomanLetter = $semesterNames->romanLetter;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semInFullName = $semesterNames->fullName;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semInSemNumber = $semesterNames->semNumber;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->failedStatus =  $currentAcademicTerm->failedStatus;
                $studentsDetails[$student->id]->failedStatus = $currentAcademicTerm->failedStatus;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterCreditGradePoint = $currentAcademicTerm->creditGradePoint;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterGradeUpgradedMark = round($currentAcademicTerm->semesterGradeUpgradedMark);
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterGrade = $currentAcademicTerm->grade;
                $studentsDetails[$student->id]->semesterGrade = $currentAcademicTerm->grade;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterCredit = $academicTerm->credit;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterGradePoint = $academicTerm->gradePoint;
                $studentsDetails[$student->id]->semesterSgpa = sprintf('%0.2f', $currentAcademicTerm->sgpa);
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterSgpa =sprintf('%0.2f', $currentAcademicTerm->sgpa);
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->latestExamMonth = $currentAcademicTerm->examMonth;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->latestExamYear = $currentAcademicTerm->examYear;
                foreach( $academicTerm->subjects as $subject){ 
                    if ($student->studentDetails->degree == "BSC"){
                        $subjectClassType = $subject->classType == "THEORY" ? "(TH)" : ( $subject->classType == "PRACTICAL" ? "(PR)" : "");
                        $subject->name = $subject->name.$subjectClassType;
                    }
                    $subject->markHistory = array_filter($subject->subjectMarkHistory,function($value)use($examRegistrationDetails){
                        return $value->examYear."-".date("m", mktime(0, 0, 0, (int)$value->examMonth, 10))  <= $examRegistrationDetails->examYear."-".date("m", mktime(0, 0, 0, (int)$examRegistrationDetails->examMonth, 10)); 
                    });
                    usort($subject->markHistory, function($a, $b) {
                        return ($a->examYear."-".date("m", mktime(0, 0, 0, (int)$a->examMonth, 10))) < ($b->examYear."-".date("m", mktime(0, 0, 0, (int)$b->examMonth, 10)));
                    });
                    usort($subject->markHistory, function($a, $b) {
                        return ($a->externalMark < $b->externalMark); 
                    });
                    $supplySubjectHistory = reset(array_filter($subject->markHistory,function($value)use($examRegistrationRequest){
                        return $value->examRegistrationId == $examRegistrationRequest->examRegistrationId;
                    }));
                    $regularStudentHistory = "";
                    $isCurrentSupplimentaryExternalChange = false;
                    if ( $supplySubjectHistory ){
                        $subject->latestMarkHistory = $supplySubjectHistory;
                        $regularStudentHistory = reset(array_filter($subject->markHistory,function($value){
                            return $value->examMarkType == "REGULAR";
                        }));
                    }
                    else{
                        $subject->latestMarkHistory = reset($subject->markHistory);
                        $subject->latestMarkHistory->examMarkType = "CARRIED";
                        $isCurrentSupplimentaryExternalChange = true;
                    }
                    $subject->code = $subject->courseCode ? $subject->courseCode : $subject->code;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id] = $subject;
                    if( $regularStudentHistory && (($subject->latestMarkHistory->internalMark != $regularStudentHistory->internalMark) ||  ($subject->latestMarkHistory->internalAttendanceStatus != $regularStudentHistory->internalAttendanceStatus)) ){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->isImprovedInternal = 1;
                        if ($subject->latestMarkHistory->improvedInternalCarriedStatus ){
                            $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->isImprovedInternal = 0;
                        }
                        if( empty($subject->latestMarkHistory->externalMarkObtainedInExam ) && !$regularStudentHistory->isExternalFailed  ){
                            $subject->latestMarkHistory->externalMarkObtainedInExam = $subject->consolidatedMarkDetails->externalMark; 
                            $subject->latestMarkHistory->markObtained = $subject->consolidatedMarkDetails->markObtained;
                            $subject->latestMarkHistory->grade = $subject->consolidatedMarkDetails->grade;
                            $subject->latestMarkHistory->gradePoint = $subject->consolidatedMarkDetails->gradePoint;
                            $subject->latestMarkHistory->resultStatus = $subject->consolidatedMarkDetails->isFailed ? 'FAILED' : 'PASSED';
                            $subject->latestMarkHistory->creditGradePoint = $subject->consolidatedMarkDetails->creditGradePoint;
                            $subject->latestMarkHistory->externalGrade = $regularStudentHistory->externalGrade;
                            $isCurrentSupplimentaryExternalChange = true;
                        }
                    }
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->isCurrentSupplimentaryExternalChange = $isCurrentSupplimentaryExternalChange;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->id = $subject->id;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->examMarkType = $subject->latestMarkHistory->examMarkType;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->totalMaxMark = $subject->totalMarks;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->percentage = round($subject->latestMarkHistory->percentage,2);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->failedStatus = $subject->latestMarkHistory->resultStatus;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->grade = $subject->latestMarkHistory->grade;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->class = $subject->latestMarkHistory->class;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->earnedCredit = $subject->latestMarkHistory->resultStatus == 'FAILED' ? 0 : (int)$subject->credit;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->latestExamYear = $subject->latestMarkHistory->examYear;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->latestexamMonth = $subject->latestMarkHistory->examMonth;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->attendanceStatus = $subject->latestMarkHistory->attendanceStatus; 
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->markObtained = $subject->latestMarkHistory->markObtained;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalGrade = $subject->latestMarkHistory->externalGrade;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalGrade = $subject->latestMarkHistory->internalGrade;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->gradePoint = $subject->latestMarkHistory->gradePoint;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->creditGradePoint = $subject->latestMarkHistory->creditGradePoint;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->latestExamMonthName = ExamRegistrationService::getInstance()->getMonthName($subject->latestMarkHistory->examMonth);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalMark = sprintf('%02d', $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalMark);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalMark = sprintf('%02d', $subject->latestMarkHistory->internalMark);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->markObtained = sprintf('%02d', $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->markObtained);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->creditGradePoint = sprintf('%02d', $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->creditGradePoint);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->studentTotalmarkObtained = sprintf('%02d', $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->markObtained);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalMarkObtainedInExam = $subject->latestMarkHistory->externalMarkObtainedInExam;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalGraceMark = $subject->latestMarkHistory->internalGraceMark;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalHashGraceMark = $subject->latestMarkHistory->internalHashGraceMark;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalMarkObtainedInExam = sprintf('%02d', $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalMarkObtainedInExam);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->graceMark = round($subject->latestMarkHistory->graceMark);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->gradeUpgradedMark = round($subject->latestMarkHistory->gradeUpgradedMark);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->gradeUpgradedMark +=  round($subject->latestMarkHistory->gradeUpgradedMark);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalGraceMark = round($subject->latestMarkHistory->externalGraceMark);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->hasCondonationGraceMark = $subject->latestMarkHistory->hasCondonationGraceMark;
                    if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->hasCondonationGraceMark){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->totalGraceMarkToDisplay =  $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalGraceMark + $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalGraceMark;
                    }
            
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->hashGraceMark = round($subject->latestMarkHistory->hashGraceMark);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->revaluationMark = round($subject->latestMarkHistory->revaluationMark);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->moderationMark = round($subject->latestMarkHistory->moderationMark);
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->updatedExternalMark = 
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalMarkObtainedInExam + 
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->graceMark + 
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->gradeUpgradedMark + 
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalGraceMark +
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->revaluationMark + 
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->moderationMark;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->isShowUpdatedExternalMark = $supplySubjectHistory ? '0' : '1';
    
                    if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->totalGraceMarkToDisplay){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->totalGraceMarkToDisplay += $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->hashGraceMark ;
                    }
                    if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalGraceMark){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalMark = $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalMark - $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalGraceMark;
                    }
                    if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalHashGraceMark){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalMark = $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalMark - $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalHashGraceMark;
                    }
                    if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->graceMark){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->markObtained -= $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->graceMark;
                    }
                    if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->gradeUpgradedMark){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->markObtained -= $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->gradeUpgradedMark;
                    } 
                    if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalGraceMark && $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->hasCondonationGraceMark){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->markObtained -= $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalGraceMark;
                    }
                    if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalGraceMark && $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->hasCondonationGraceMark){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->markObtained -= $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalGraceMark;
                    }
                    if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->hashGraceMark){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->markObtained -= $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->hashGraceMark;
                    }
                    if($subject->consolidatedMarkDetails->excludeSubjectFromTotal == '1'){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->grade = $subject->latestMarkHistory->resultStatus == "PASSED" ? 'P' : 'F' ;
                    }
                    if($subject->latestMarkHistory->attendanceStatus == 'ABSENT'){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalMarkObtainedInExam = 'AB';
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->grade = 'F';
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalGrade = 'F(Ab)';
                        
                    }
                    if($subject->latestMarkHistory->internalAttendanceStatus == 'ABSENT'){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalMark = 'AB';
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->internalGrade = 'F(Ab)';
                        
                    }
                    if($subject->latestMarkHistory->attendanceStatus == 'MALPRACTICE'){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalMarkObtainedInExam = '00                                                                                                                                                                                                          ';
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->grade = 'F';
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->externalGrade = 'F';
                    }    
                    else if($subject->latestMarkHistory->resultStatus == 'FAILED'){
                        $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects[$subject->id]->grade = 'F';
                    }
                } 
                $studentsDetails[$student->id]->semesterEarnedCredit = array_sum(array_column($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects,'earnedCredit'));
                $sgpaSearchReq = new \stdClass();
                // $sgpaSearchReq->groupId = $student->studentDetails->batchId;
                $sgpaSearchReq->studentWithSgpa = 1;
                $sgpaSearchReq->sgpaStudentId = $student->id;
                $sgpaStudentList = ExamValuationService::getInstance()->getStudentDetailsForSgpaDirectEntry($sgpaSearchReq);
                
                $semesterMarksRequest = new \stdClass;
                // $semesterMarksRequest->groupId = $student->studentDetails->batchId;
                $semesterMarksRequest->studentId = $student->id;
                $semesterMarks = MarksCardService::getInstance()->getStudentSemesterMarkDetails($semesterMarksRequest);
                foreach($semesterMarks as $semKey => $semester){
                    if($examTermOrderNo < $semester->orderNo){
                        unset($semesterMarks[$semKey]);
                        continue;
                    }
                    $currentHistory = $this->getSgpaByMonthYear($semester->markHistory, $examRegistrationDetails, false);
                    $semesterNames = new \stdClass;
                    $semesterNames = CommonExamService::getInstance()->getDifferentSemesterName($semester->name);
                    $semester->sgpa =sprintf('%0.2f', ($currentHistory->sgpa + $currentHistory->sgpaAddonMark));
                    $semester->semInRomanLetter = $semesterNames->romanLetter;
                    $semester->totalCredit = $semester->markDetails->credit;
                    $semester->markDetails->isFailed = $currentHistory->failedStatus == "FAILED" ? 1 : 0;
                    $semester->markDetails->arrearCount = 0;
                    foreach($semester->subjects as $subject){
                        $currentSubject = $this->getSgpaByMonthYear($subject->subjectMarkHistory, $examRegistrationDetails, 1);
                        if (count($subject->subjectMarkHistory) > 1){
                            $regularStudentHistory = reset(array_filter($subject->subjectMarkHistory,function($value){
                                return $value->examMarkType == "REGULAR";
                            }));
                            if( $regularStudentHistory && ($currentSubject->internalMark != $regularStudentHistory->internalMark) ){
                                if( empty($currentSubject->externalMarkObtainedInExam ) && !$regularStudentHistory->isExternalFailed ){
                                    $currentSubject->resultStatus = $subject->markDetails->isFailed ? 'FAILED' : 'PASSED';
                                }
                            }
                        }
                        $subject->earnedCredit = $currentSubject->resultStatus == "FAILED" ? 0 : $subject->markDetails->credit;
                        $semester->markDetails->arrearCount = $currentSubject->resultStatus == "FAILED" ? $semester->markDetails->arrearCount+1 : $semester->markDetails->arrearCount;
                    }
                    $semester->totalEarnedCredit = array_sum(array_column($semester->subjects,'earnedCredit'));
                }
                $semArray = [];
                $barcodeVar = $student->studentDetails->rollNo.", SGPA-";
                foreach($semesterMarks as $semester){
                    $semArray[$semester->orderNo] = $semester;
                }
    
                for($i=1; $i <= $finalSemDetails->properties->orderNo; $i++){
                    if( empty( $semArray[$i])){
                        $semesterNames = CommonExamService::getInstance()->getDifferentSemesterName($i);
                        $semArray[$i]->semInRomanLetter = $semesterNames->romanLetter;
                        $semArray[$i]->noDetails = 1;
                    }
                    $studentEnteredSgpa = reset(array_filter($sgpaStudentList,function($value)use($i){
                        return $value->academicTermId == $i;
                    }));
                    if(round($studentEnteredSgpa->sgpa)){
                        $semArray[$i]->sgpa =sprintf('%0.2f', ($studentEnteredSgpa->sgpa));
                        $semArray[$i]->markDetails->isFailed = 0;
                        $semArray[$i]->noDetails = 0;
                    }
                    if(round($studentEnteredSgpa->totalCredit)){
                        $semArray[$i]->totalCredit = $studentEnteredSgpa->totalCredit;
                        $semArray[$i]->noDetails = 0;
                    }
                    if(round($studentEnteredSgpa->earnedCredit)){
                        $semArray[$i]->totalEarnedCredit = $studentEnteredSgpa->earnedCredit;
                        $semArray[$i]->noDetails = 0;
                    }
                    $barcodeVar .= $semArray[$i]->name.":";
                    if( $semArray[$i]->markDetails->isFailed){
                        $barcodeVar .= "-, ";
                    }
                    else if ($semArray[$i]->markDetails){
                        $barcodeVar .= $semArray[$i]->sgpa.", ";
                    }
    
                }
                $barcodeVar .= $studentsDetails[$student->id]->semesterGrade.", ";
                $barcodeVar .= $studentsDetails[$student->id]->failedStatus == 'FAILED'? 'Fail' : 'Pass';
                $barcodeObj = new TCPDFBarcode($barcodeVar, 'C128');
                $studentsDetails[$student->id]->barcodeObj = $barcodeObj->getBarcodeHTML($w = 1, $h = 30, $color = 'black');
                ksort($semArray);
                $semesterMarks = array_slice($semArray, 0, 4);
                
                $studentsDetails[$student->id]->semesters = $semesterMarks;
                $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterMarkObtained = array_sum(array_column($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects,'studentTotalmarkObtained'));
                // $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterCreditGradePoint = array_sum(array_column($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects,'creditGradePoint'));
                if($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->gradeUpgradedMark && $examRegistrationDetails->isSpecialExam){
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterMarkObtained -= $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->gradeUpgradedMark;
                    $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->semesterMarkObtained .= " @".$studentsDetails[$student->id]->academicTerms[$academicTerm->id]->gradeUpgradedMark;
                }
            }
            if($studentsDetails[$student->id]->failedStatus  == 'PASSED'){
                $slNoRequest =  new \stdClass;
                $slNoRequest->studentId = $student->id;
                $slNoRequest->examRegistrationId = $studentsDetails[$student->id]->regularExamRegistrationId;
                $studentsDetails[$student->id]->slNo = CommonExamService::getInstance()->getStudentExamMarkListSerialNumber($slNoRequest)->markListSerialNo;
                if ($studentsDetails[$student->id]->slNo) {
                    $studentsDetails[$student->id]->slNo = str_pad($studentsDetails[$student->id]->slNo, 6, "0", STR_PAD_LEFT);
                }
            }
            $studentsDetails[$student->id]->academicTerms[$academicTerm->id]->certicifateNo = 'No:GC/'.reset($studentsDetails[$student->id]->academicTerms)->latestExamYear.'/S'.reset($studentsDetails[$student->id]->academicTerms)->semInSemNumber.'/'.$studentsDetails[$student->id]->slNo;
        }
        uasort($studentsDetails[$student->id]->academicTerms[$academicTerm->id]->subjects, function($a, $b) {
            return ($a->priority > $b->priority);
        });
        $displaySubjectCategories = array_values($displaySubjectCategories); 
        foreach($displaySubjectCategories as $displaySubjectCategory){
            $displaySubjectCategory->subjects = array_values($displaySubjectCategory->subjects); 
        }
        uasort($displaySubjectArray, function($a, $b) {
            return ($a->priority > $b->priority);
        });
        uasort($studentsDetails, function($a, $b) {
            return ($a->rollNo > $b->rollNo);
        });
        $studentsDetails = array_values($studentsDetails); 
        foreach($studentsDetails as $student){
            $student->academicTerms = array_values($student->academicTerms); 
            foreach($student->academicTerms as $academicTerm){
                $academicTerm->subjects = array_values($academicTerm->subjects); 
            } 
        }
        $response->studentsDetails = $studentsDetails;
        return $response;
    }
protected function getSgpaByMonthYear($markHistory, $request, $subjectRequest){
        $currentHistory = '';
        if( $subjectRequest ){
            $currentHistory = array_filter($markHistory,function($value)use($request){
                return $value->examYear."-".date("m", mktime(0, 0, 0, (int)$value->examMonth, 10))  <= $request->examYear."-".date("m", mktime(0, 0, 0, (int)$request->examMonth, 10)); 
            });
            usort($currentHistory, function($a, $b) {
                return ($a->examYear."-".date("m", mktime(0, 0, 0, (int)$a->examMonth, 10))."-".$a->examMarkType) < ($b->examYear."-".date("m", mktime(0, 0, 0, (int)$b->examMonth, 10))."-".$b->examMarkType);
            });
        }
        else{
            
            $currentHistory = array_filter($markHistory,function($value)use($request){
                return $value->examYear."-".date("m", mktime(0, 0, 0, (int)$value->examMonth, 10))  <= $request->examYear."-".date("m", mktime(0, 0, 0, (int)$request->examMonth, 10)); 
            });
            usort($currentHistory, function($a, $b) {
                return ($a->examYear."-".date("m", mktime(0, 0, 0, (int)$a->examMonth, 10))."-".$a->historyType) < ($b->examYear."-".date("m", mktime(0, 0, 0, (int)$b->examMonth, 10))."-".$b->historyType);
            });
        }
       
        return reset($currentHistory);
    }
}