Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 234 |
| GradeService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
2550.00 | |
0.00% |
0 / 234 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| saveGrade | |
0.00% |
0 / 1 |
56.00 | |
0.00% |
0 / 23 |
|||
| saveGrades | |
0.00% |
0 / 1 |
132.00 | |
0.00% |
0 / 35 |
|||
| validateSaveGradeRequest | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 10 |
|||
| insertGrade | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 19 |
|||
| insertGrades | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 28 |
|||
| updateGrade | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 22 |
|||
| deleteGrade | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 21 |
|||
| searchGrade | |
0.00% |
0 / 1 |
90.00 | |
0.00% |
0 / 48 |
|||
| getGradeDetails | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 25 |
|||
| <?php | |
| namespace com\linways\core\ams\professional\service\academic; | |
| use com\linways\base\util\SecurityUtils; | |
| use com\linways\base\util\MakeSingletonTrait; | |
| use com\linways\core\ams\professional\logging\AMSLogger; | |
| use com\linways\core\ams\professional\logging\Events; | |
| use com\linways\core\ams\professional\service\BaseService; | |
| use com\linways\core\ams\professional\mapper\academic\GradeServiceMapper; | |
| use com\linways\core\ams\professional\request\academic\SearchGradeRequest; | |
| use com\linways\core\ams\professional\constant\academic\GradeTypeConstant; | |
| use com\linways\core\ams\professional\constant\academic\StatusConstants; | |
| use com\linways\core\ams\professional\exception\ProfessionalException; | |
| use com\linways\core\ams\professional\dto\api\Grade; | |
| class GradeService extends BaseService | |
| { | |
| use MakeSingletonTrait; | |
| private function __construct() { | |
| $this->mapper = GradeServiceMapper::getInstance()->getMapper(); | |
| $this->logger = AMSLogger::getLogger(); | |
| } | |
| /** | |
| * Save grade | |
| * @param Grade $grades | |
| * @return String $id | |
| */ | |
| public function saveGrade(Grade $grade) | |
| { | |
| $grade = $this->realEscapeObject($grade); | |
| $grade->createdBy = $GLOBALS['userId'] ?? $grade->createdBy; | |
| $grade->updatedBy = $GLOBALS['userId'] ?? $grade->updatedBy; | |
| $this->validateSaveGradeRequest($grade); | |
| try{ | |
| if(!empty($grade->id)) { | |
| $grade->id = $this->updateGrade($grade); | |
| } | |
| else { | |
| $grade->id = $this->insertGrade($grade); | |
| } | |
| }catch(\Exception $e) { | |
| if($e->getCode() !== ProfessionalException::INVALID_PARAMETERS && $e->getCode() !== ProfessionalException::EMPTY_PARAMETERS && $e->getCode() !== ProfessionalException::DUPLICATE_ENTRY) { | |
| throw new ProfessionalException($e->getCode(), "Failed to save grade! Please try again"); | |
| } else if ($e->getCode() === ProfessionalException::DUPLICATE_ENTRY) { | |
| throw new ProfessionalException (ProfessionalException::DUPLICATE_ENTRY, "Cannot create grade.".$grade->name." already exists!"); | |
| } else { | |
| throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| return $grade->id; | |
| } | |
| /** | |
| * Save grades | |
| * @param Grade Array $grades | |
| * @param String $gradeSchemeId | |
| * @return Grade Array $grades | |
| */ | |
| public function saveGrades(Grade $grades, $gradeSchemeId) | |
| { | |
| $grades = $this->realEscapeArray($grades); | |
| $gradeSchemeId = $this->realEscapeString($gradeSchemeId); | |
| foreach ($grades as $grade) { | |
| $grade->createdBy = $GLOBALS['userId'] ?? $grade->createdBy; | |
| $grade->updatedBy = $GLOBALS['userId'] ?? $grade->updatedBy; | |
| $grade->gradeSchemeId = $gradeSchemeId; | |
| $this->validateSaveGradeRequest($grade); | |
| } | |
| try{ | |
| $isSaveAll = count(array_unique(array_column($grades, "id"))) < 2 && reset(array_unique(array_column($grades, "id"))) == null; | |
| if ($isSaveAll) { | |
| $grades = $this->insertGrades($grade); | |
| } | |
| else { | |
| foreach ($grades as $grade) { | |
| if(!empty($grade->id)) { | |
| $grade->id = $this->updateGrade($grade); | |
| } | |
| else { | |
| $grade->id = $this->insertGrade($grade); | |
| } | |
| } | |
| } | |
| }catch(\Exception $e) { | |
| if($e->getCode() !== ProfessionalException::INVALID_PARAMETERS && $e->getCode() !== ProfessionalException::EMPTY_PARAMETERS && $e->getCode() !== ProfessionalException::DUPLICATE_ENTRY) { | |
| throw new ProfessionalException($e->getCode(), "Failed to save grade! Please try again"); | |
| } else if ($e->getCode() === ProfessionalException::DUPLICATE_ENTRY) { | |
| throw new ProfessionalException (ProfessionalException::DUPLICATE_ENTRY, "Cannot create grades. already exists!"); | |
| } else { | |
| throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| return $grades; | |
| } | |
| /** | |
| * Validate Grade Request Before Saving | |
| * @param Grade $grade | |
| * @return NULL | |
| */ | |
| private function validateSaveGradeRequest($grade) | |
| { | |
| if(empty($grade->gradeSchemeId)) | |
| throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS, "Grade scheme is empty! Please choose a scheme for grade"); | |
| if(empty($grade->name)) | |
| throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS, "Grade name is empty! Please enter a name for grade"); | |
| if(empty($grade->rangeFrom)) | |
| throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS, "Grade 'Range from' is empty! Please enter a 'Range from' for grade"); | |
| if(empty($grade->rangeTo)) | |
| throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS, "Grade 'Range to' is empty! Please enter a 'Range to' for grade"); | |
| } | |
| /** | |
| * Insert grade | |
| * @param Grade $grade | |
| * @return String $id | |
| */ | |
| private function insertGrade(Grade $grade) | |
| { | |
| $properties = !empty($grade->properties) ? "'" . json_encode($grade->properties) . "'" : "JSON_OBJECT"; | |
| $id = SecurityUtils::getRandomString(); | |
| $query = "INSERT INTO `grade` | |
| (`id`,`grade_scheme_id`,`range_from`,`range_to`,`name`,`properties`,`created_by`,`updated_by`) | |
| VALUES | |
| ('$id','$grade->gradeSchemeId',$grade->rangeFrom,$grade->rangeTo,'$grade->name',$properties,'$grade->createdBy','$grade->updatedBy')"; | |
| try { | |
| $this->executeQuery($query); | |
| $this->logger->info(Events::GRADE_CREATION,[ | |
| "grade" => new GradeEntity(["id" => $id]), | |
| "status" => StatusConstants::SUCCESS, | |
| "updated_by" => $updatedBy, | |
| ]); | |
| return $id; | |
| } catch (\Exception $e) { | |
| throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Insert grades | |
| * @param Grade Array $grades | |
| * @return Grade Array $grades | |
| */ | |
| private function insertGrades(Grade $grades) | |
| { | |
| $values = null; | |
| $gradeIds = null; | |
| foreach ($grades as $grade) { | |
| $properties = !empty($grade->properties) ? "'" . json_encode($grade->properties) . "'" : "JSON_OBJECT"; | |
| $id = SecurityUtils::getRandomString(); | |
| $gradeIds[] = $grade->id = $id; | |
| $values[] = "('$id','$grade->gradeSchemeId',$grade->rangeFrom,$grade->rangeTo,'$grade->name',$properties,'$grade->createdBy','$grade->updatedBy')"; | |
| } | |
| $valueQuery = implode(", ",$values); | |
| $query = "INSERT INTO `grade` | |
| (`id`,`grade_scheme_id`,`range_from`,`range_to`,`name`,`properties`,`created_by`,`updated_by`) | |
| VALUES | |
| $valueQuery"; | |
| try { | |
| $this->executeQuery($query); | |
| foreach ($gradeIds as $gradeId) { | |
| $this->logger->info(Events::GRADE_CREATION,[ | |
| "grade" => new GradeEntity(["id" => $gradeId]), | |
| "status" => StatusConstants::SUCCESS, | |
| "updated_by" => $updatedBy, | |
| ]); | |
| } | |
| return $grades; | |
| } catch (\Exception $e) { | |
| throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Update Grade | |
| * @param Grade $grade | |
| * @return NULL | |
| */ | |
| private function updateGrade(Grade $grade) | |
| { | |
| $properties = !empty($grade->properties) ? "'" . json_encode($grade->properties) . "'" : "JSON_OBJECT"; | |
| $query = "UPDATE | |
| `grade` | |
| SET | |
| `name` = '$grade->name', | |
| `properties` = $properties, | |
| `updated_by` = '$grade->updatedBy' | |
| WHERE | |
| `id` = '$grade->id'"; | |
| try { | |
| $this->executeQuery($query); | |
| $this->logger->info(Events::GRADE_UPDATING,[ | |
| "grade" => new GradeEntity(["id" => $id]), | |
| "status" => StatusConstants::SUCCESS, | |
| "updated_by" => $updatedBy, | |
| ]); | |
| return $grade->id; | |
| } catch (\Exception $e) { | |
| throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Delete Grade (Soft Delete) | |
| * @param String $id | |
| * @return NULL | |
| */ | |
| public function deleteGrade($id) | |
| { | |
| $id = $this->realEscapeString($id); | |
| $updatedBy = $GLOBALS['userId']; | |
| if(empty($id)) { | |
| throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS, "No grade selected! Please select a grade to delete"); | |
| } | |
| $query = "DELETE FROM | |
| `grade` | |
| WHERE | |
| `id` = '$id'"; | |
| try { | |
| $this->logger->info(Events::GRADE_DELETION,[ | |
| "grade" => new GradeEntity(["id" => $id]), | |
| "status" => StatusConstants::SUCCESS, | |
| "updated_by" => $updatedBy, | |
| ]); | |
| $this->executeQuery($query); | |
| } catch (\Exception $e) { | |
| throw new ProfessionalException($e->getCode(), "Error deleting grade! Please try again"); | |
| } | |
| } | |
| /** | |
| * Search Grade Details | |
| * @param SearchGradeRequest $request | |
| * @return Grade | |
| */ | |
| public function searchGrade(SearchGradeRequest $request) | |
| { | |
| $request = $this->realEscapeObject($request); | |
| $whereQuery = ""; | |
| $limitQuery = ""; | |
| if(!empty($request->id)) { | |
| $whereQuery .= " AND g.id = '$request->id' "; | |
| } | |
| if(!empty($request->name)) { | |
| $whereQuery .= " AND g.name LIKE '%$request->name%' "; | |
| } | |
| if(!empty($request->gradeSchemeId)) { | |
| $whereQuery .= " AND g.grade_scheme_id = '$request->gradeSchemeId' "; | |
| } | |
| if(!empty($request->rangeFrom)) { | |
| $whereQuery .= " AND g.range_from = $request->rangeFrom "; | |
| } | |
| if(!empty($request->rangeFrom)) { | |
| $whereQuery .= " AND g.range_to = $request->rangeTo "; | |
| } | |
| if($request->startIndex !== "" && $request->endIndex !== "") | |
| { | |
| $limitQuery .= " LIMIT $request->startIndex,$request->endIndex"; | |
| } | |
| $query = "SELECT | |
| g.id, | |
| g.grade_scheme_id, | |
| g.name, | |
| g.range_from, | |
| g.range_to, | |
| g.properties, | |
| g.created_by, | |
| g.created_date, | |
| g.updated_by, | |
| g.updated_date, | |
| NULL AS `trashed` | |
| FROM | |
| `grade` g | |
| WHERE | |
| 1 = 1 | |
| $whereQuery | |
| $limitQuery"; | |
| try { | |
| $grades = $this->executeQueryForList($query,$this->mapper[GradeServiceMapper::SEARCH_GRADE]); | |
| } catch (\Exception $e) { | |
| throw new ProfessionalException($e->getCode(),"Cannot fetch grade details! Please try again"); | |
| } | |
| return $grades; | |
| } | |
| /** | |
| * get Grade Scheme by id | |
| * @param String $id | |
| * @param Boolean $request | |
| * @return Grade | |
| */ | |
| public function getGradeDetails($id) | |
| { | |
| $id = $this->realEscapeString($id); | |
| if(empty($id)) | |
| throw new ProfessionalException(ProfessionalException::EMPTY_PARAMETERS,"Grade is invalid! Please enter a valid program"); | |
| $query = "SELECT | |
| g.id, | |
| g.grade_scheme_id, | |
| g.name, | |
| g.range_from, | |
| g.range_to, | |
| g.properties, | |
| g.created_by, | |
| g.created_date, | |
| g.updated_by, | |
| g.updated_date, | |
| NULL AS `trashed` | |
| WHERE | |
| `id` = $id"; | |
| try { | |
| $grade = $this->executeQueryForObject($query, $this->mapper[GradeServiceMapper::SEARCH_GRADE]); | |
| } catch (\Exception $e) { | |
| throw new ProfessionalException($e->getCode(),"Cannot fetch grade details! Please try again"); | |
| } | |
| return $grade; | |
| } | |
| } |