Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 285 |
| FolioNumberService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
2070.00 | |
0.00% |
0 / 285 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| generateFolioNumber | |
0.00% |
0 / 1 |
72.00 | |
0.00% |
0 / 42 |
|||
| saveFolioNumber | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 30 |
|||
| validateSaveFolioNumber | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 3 |
|||
| insertFolioNumber | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 17 |
|||
| getBatchDetailsByBatchPriority | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 32 |
|||
| getLatestFolioNumber | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 20 |
|||
| getStudentsDetailsByBatch | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 40 |
|||
| checkIfFolioNumberGenerated | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 20 |
|||
| getFolioNumberReport | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 41 |
|||
| getFolioGeneratedStudentsDetailsByBatch | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 38 |
|||
| <?php | |
| namespace com\linways\ec\core\service; | |
| 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\dto\FolioNumber; | |
| use com\linways\ec\core\logging\Events; | |
| use com\linways\ec\core\logging\entities\Staff; | |
| use com\linways\core\ams\professional\logging\AMSLogger; | |
| use com\linways\base\util\TwigRenderer; | |
| use com\linways\core\ams\professional\util\PdfUtil; | |
| class FolioNumberService extends BaseService | |
| { | |
| use MakeSingletonTrait; | |
| private function __construct(){ | |
| $this->logger = AMSLogger::getLogger('exam-controller-log'); | |
| } | |
| /** | |
| * Generate Folio Number | |
| * @param $request | |
| * @return $id | |
| */ | |
| public function generateFolioNumber( $request){ | |
| $request = $this->realEscapeObject($request); | |
| $staffId = $GLOBALS['userId']; | |
| try { | |
| if(empty($request->groupId)){ | |
| throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't generate folio number. No batch found"); | |
| } | |
| $sortedGroupList = $this->getBatchDetailsByBatchPriority($request); | |
| if(count($sortedGroupList) != count($request->groupId)){ | |
| throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't generate folio number. No prefix added"); | |
| } | |
| // check folio number prefix | |
| foreach($sortedGroupList as $group){ | |
| $maxFolioNumber = ""; | |
| $folioNumberCount = 1; | |
| $requestForMaxFolioNumber = new \stdClass; | |
| $requestForMaxFolioNumber->year = $group->endYear; | |
| $maxFolioNumber = $this->getLatestFolioNumber($requestForMaxFolioNumber); | |
| if($maxFolioNumber){ | |
| $folioNumberCount = (int)substr($maxFolioNumber, 2)+1; | |
| } | |
| $requestForStudents = new \stdClass; | |
| $requestForStudents->groupId = $group->id; | |
| $requestForStudents->orderBy = "GENDER_WISE"; | |
| $studentsList = $this->getStudentsDetailsByBatch($requestForStudents); | |
| foreach($studentsList as $student){ | |
| $folioNumber = new FolioNumber; | |
| $folioNumber->groupId = $group->id; | |
| $folioNumber->studentId = $student->id; | |
| $folioNumber->folioNumber = $group->prefix.sprintf("%04d", $folioNumberCount); | |
| $folioNumberProperties = new \stdClass; | |
| $folioNumberProperties->year = $group->endYear; | |
| if($group->priority){ | |
| $folioNumberProperties->batchPriority = $group->priority; | |
| } | |
| $folioNumber->properties = $folioNumberProperties; | |
| $folioNumber->id = $this->saveFolioNumber($folioNumber); | |
| $folioNumberCount ++; | |
| } | |
| } | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Save Folio Number | |
| * @param FolioNumber $folioNumber | |
| * @return $id | |
| */ | |
| public function saveFolioNumber(FolioNumber $folioNumber){ | |
| $folioNumber = $this->realEscapeObject($folioNumber); | |
| $folioNumber->createdBy = $GLOBALS['userId'] ?? $folioNumber->createdBy; | |
| $folioNumber->updatedBy = $GLOBALS['userId'] ?? $folioNumber->updatedBy; | |
| $staffId = $GLOBALS['userId']; | |
| try { | |
| $this->validateSaveFolioNumber($folioNumber); | |
| $folioNumber->id = $this->insertFolioNumber($folioNumber); | |
| AMSLogger::log_info($this->logger,Events::EC_SAVE_FOLIO_NUMBER, [ | |
| "staff" => new Staff(["id" => $staffId]), | |
| "request" => $folioNumber, | |
| "status" => StatusConstants::SUCCESS | |
| ]); | |
| } catch (\Exception $e) { | |
| AMSLogger::log_error($this->logger,Events::EC_SAVE_FOLIO_NUMBER, [ | |
| "staff" => new Staff(["id" => $staffId]), | |
| "request" => $folioNumber, | |
| "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 folio number! Please try again"); | |
| } else if ($e->getCode() === ExamControllerException::DUPLICATE_ENTRY) { | |
| throw new ExamControllerException(ExamControllerException::DUPLICATE_ENTRY, "Cannot add folio number."); | |
| } else { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| return $folioNumber->id; | |
| } | |
| /** | |
| * Validate Folio Number Request Before Saving | |
| * @param FolioNumber $folioNumber | |
| * @return NULL | |
| */ | |
| private function validateSaveFolioNumber(FolioNumber $folioNumber){ | |
| if (empty($folioNumber->groupId) || empty($folioNumber->studentId)) | |
| throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, " empty parameter. can't save folio number! "); | |
| } | |
| /** | |
| * Insert Folio Number | |
| * @param FolioNumber $folioNumber | |
| * @return $id | |
| */ | |
| private function insertFolioNumber(FolioNumber $folioNumber){ | |
| $properties = !empty($folioNumber->properties) ? "'" . json_encode($folioNumber->properties) . "'" : "NULL"; | |
| $query = "INSERT INTO ec_folio_number | |
| (student_id,groups_id,folio_number,properties,created_by,updated_by) | |
| VALUES | |
| ('$folioNumber->studentId','$folioNumber->groupId','$folioNumber->folioNumber',$properties,'$folioNumber->createdBy','$folioNumber->updatedBy') | |
| ON DUPLICATE KEY | |
| UPDATE | |
| updated_by = VALUES(created_by), | |
| folio_number = VALUES(folio_number), | |
| properties = VALUES(properties)"; | |
| try { | |
| $folioNumber->id = $this->executeQuery($query,true)->id; | |
| return $folioNumber->id; | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * get Batch details by batch priority | |
| * @param $searchRequest | |
| * @return $batches | |
| */ | |
| public function getBatchDetailsByBatchPriority($searchRequest){ | |
| $searchRequest = $this->realEscapeObject($searchRequest); | |
| try { | |
| $whereQuery = null; | |
| $orderBy = " ORDER BY egp.priority ASC "; | |
| $whereQuery = ""; | |
| if (!empty($searchRequest->groupId)) { | |
| $groupIdString = is_array($searchRequest->groupId) ? "'" . implode("','", $searchRequest->groupId) . "'" : "'" . $searchRequest->groupId . "'"; | |
| $whereQuery .= " AND g.id IN ( $groupIdString )"; | |
| } | |
| $query = "SELECT | |
| DISTINCT | |
| g.id, | |
| g.identifying_context as identifyingContext, | |
| g.name, | |
| g.type, | |
| g.properties->>'$.endYear' as endYear, | |
| egp.priority, | |
| fnp.prefix | |
| FROM | |
| `groups` g | |
| INNER JOIN folioNumber_prefix fnp ON | |
| fnp.year = g.properties->>'$.endYear' | |
| LEFT JOIN ec_group_priority egp ON | |
| egp.groups_id = g.id | |
| WHERE | |
| 1 = 1 "; | |
| $batches = $this->executeQueryForList($query . $whereQuery .$orderBy ); | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| return $batches; | |
| } | |
| /** | |
| * get latest Folio Number | |
| * @param $searchRequest | |
| * @return $maxFolioNumber | |
| */ | |
| public function getLatestFolioNumber($searchRequest){ | |
| $searchRequest = $this->realEscapeObject($searchRequest); | |
| try { | |
| $whereQuery = null; | |
| $whereQuery = ""; | |
| if (!empty($searchRequest->year)) { | |
| $yearString = is_array($searchRequest->year) ? "'" . implode("','", $searchRequest->year) . "'" : "'" . $searchRequest->year . "'"; | |
| $whereQuery .= " AND efn.properties->>'$.year' IN ( $yearString )"; | |
| } | |
| $query = "SELECT | |
| MAX(efn.folio_number) as maxFolioNumber | |
| FROM | |
| `ec_folio_number` efn | |
| WHERE | |
| 1 = 1 "; | |
| $maxFolioNumber = $this->executeQueryForObject($query . $whereQuery )->maxFolioNumber; | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| return $maxFolioNumber; | |
| } | |
| /** | |
| * get Students Details By Batch | |
| * @param $searchRequest | |
| * @return $students | |
| */ | |
| public function getStudentsDetailsByBatch($searchRequest){ | |
| $searchRequest = $this->realEscapeObject($searchRequest); | |
| try { | |
| $whereQuery = null; | |
| if($searchRequest->orderBy == 'GENDER_WISE'){ | |
| $orderBy = " ORDER BY s.studentGender ASC, s.studentName "; | |
| } | |
| else{ | |
| $orderBy = " ORDER BY spa.properties->>'$.registerNumber' ASC "; | |
| } | |
| $whereQuery = ""; | |
| if (!empty($searchRequest->groupId)) { | |
| $groupIdString = is_array($searchRequest->groupId) ? "'" . implode("','", $searchRequest->groupId) . "'" : "'" . $searchRequest->groupId . "'"; | |
| $whereQuery .= " AND g.id IN ( $groupIdString )"; | |
| } | |
| $query = "SELECT DISTINCT | |
| spa.student_id AS id, | |
| spa.student_id AS studentId, | |
| g.id AS groupId, | |
| g.name AS groupName, | |
| g.properties ->>'$.programId' AS programId, | |
| spa.properties->>'$.rollNumber' AS studentRollNo, | |
| spa.properties->>'$.registerNumber' AS studentRegisterNo, | |
| s.studentName AS studentName | |
| FROM | |
| studentaccount s | |
| INNER JOIN student_program_account spa | |
| ON spa.student_id = s.studentID | |
| INNER JOIN student_program_batch_log spbl | |
| ON spbl.program_student_id = spa.id AND spbl.properties->>'$.academicStatus' IN ('ACTIVE','COMPLETED') | |
| INNER JOIN `program` p | |
| ON p.id = spbl.program_id | |
| INNER JOIN `groups` g | |
| ON g.id = spbl.batch_group_id | |
| WHERE 1=1 "; | |
| $students = $this->executeQueryForList($query . $whereQuery .$orderBy ); | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| return $students; | |
| } | |
| /** | |
| * checkIfFolioNumberGenerated | |
| * @param $searchRequest | |
| * @return $folioNumber | |
| */ | |
| public function checkIfFolioNumberGenerated($searchRequest){ | |
| $searchRequest = $this->realEscapeObject($searchRequest); | |
| try { | |
| $limit = " LIMIT 1"; | |
| $whereQuery = ""; | |
| if (!empty($searchRequest->groupId)) { | |
| $groupIdString = is_array($searchRequest->groupId) ? "'" . implode("','", $searchRequest->groupId) . "'" : "'" . $searchRequest->groupId . "'"; | |
| $whereQuery .= " AND efn.groups_id IN ( $groupIdString )"; | |
| } | |
| $query = "SELECT | |
| efn.id | |
| FROM | |
| `ec_folio_number` efn | |
| WHERE | |
| 1 = 1 "; | |
| $folioNumber = $this->executeQueryForList($query . $whereQuery .$limit); | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| return $folioNumber; | |
| } | |
| /** | |
| * get Regular Exam Subject Wise Result Analysis | |
| * @param $searchRequest | |
| * @return $response | |
| */ | |
| public function getFolioNumberReport($searchRequest) { | |
| $searchRequest = $this->realEscapeObject($searchRequest); | |
| try{ | |
| $students = $this->getFolioGeneratedStudentsDetailsByBatch($searchRequest); | |
| if(empty($students)){ | |
| throw new ExamControllerException(ExamControllerException::NO_REPORTS_DETAILS_FOUND,"No Folio Number Generated"); | |
| } | |
| else{ | |
| $templateName = "template_1"; | |
| $responseHtml = TwigRenderer::renderTemplateFileToHtml(realpath(DOCUMENT_ROOT."../examcontroller-api/src/com/linways/web/templates/folioNumber/$templateName.twig"), [ 'students'=>$students ]); | |
| $prtContent = NULL; | |
| $prtContent .= '<html><head>'; | |
| $prtContent .= "<style> | |
| h6 {font-size: 26px;} .text-center { text-align: center;} .align-middle {vertical-align: middle;}; tr.noBorder td {border: 0; border-collapse:collapse;} | |
| table, th, td {border: 1px solid black;border-collapse: collapse;} | |
| </style>"; | |
| $prtContent .= '</head><title>Folio Number Report</title><body>'; | |
| $prtContent .= $responseHtml; | |
| $prtContent .= '</body></html>'; | |
| $totalWidth = 210; | |
| $totalHeight = 297; | |
| $options = array( | |
| 'page-width' => $totalHeight."mm", | |
| 'page-height' => $totalWidth."mm", | |
| 'dpi' => 96, | |
| 'margin-top' => "9mm", | |
| 'margin-left' => "1mm", | |
| 'margin-right' => "1mm", | |
| 'margin-bottom' => "9mm", | |
| // '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); | |
| } | |
| } | |
| catch (\Exception $e) | |
| { | |
| throw new ExamControllerException($e->getCode(),$e->getMessage()); | |
| } | |
| return $programResult; | |
| } | |
| /** | |
| * getFolioGeneratedStudentsDetailsByBatch | |
| * @param $searchRequest | |
| * @return $students | |
| */ | |
| public function getFolioGeneratedStudentsDetailsByBatch($searchRequest){ | |
| $searchRequest = $this->realEscapeObject($searchRequest); | |
| try { | |
| $whereQuery = null; | |
| $whereQuery = ""; | |
| if (!empty($searchRequest->groupId)) { | |
| $groupIdString = is_array($searchRequest->groupId) ? "'" . implode("','", $searchRequest->groupId) . "'" : "'" . $searchRequest->groupId . "'"; | |
| $whereQuery .= " AND g.id IN ( $groupIdString )"; | |
| } | |
| $query = "SELECT DISTINCT | |
| spa.student_id AS id, | |
| spa.student_id AS studentId, | |
| g.id AS groupId, | |
| g.name AS groupName, | |
| efn.folio_number AS folioNumber, | |
| g.properties ->>'$.programId' AS programId, | |
| spa.properties->>'$.rollNumber' AS studentRollNo, | |
| spa.properties->>'$.registerNumber' AS studentRegisterNo, | |
| s.studentName AS studentName, | |
| s.admissionNo AS admissionNo | |
| FROM | |
| ec_folio_number efn | |
| INNER JOIN studentaccount s ON | |
| s.studentID = efn.student_id | |
| INNER JOIN student_program_account spa | |
| ON spa.student_id = s.studentID | |
| INNER JOIN student_program_batch_log spbl | |
| ON spbl.program_student_id = spa.id AND spbl.properties->>'$.academicStatus' IN ('ACTIVE','COMPLETED') | |
| INNER JOIN `program` p | |
| ON p.id = spbl.program_id | |
| INNER JOIN `groups` g | |
| ON g.id = spbl.batch_group_id AND g.id = efn.groups_id | |
| WHERE 1=1 "; | |
| $students = $this->executeQueryForList($query . $whereQuery ); | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| return $students; | |
| } | |
| } |