Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 174 |
| MarklistCategorizeSubjectService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
1122.00 | |
0.00% |
0 / 174 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| saveMarklistCategorizeSubject | |
0.00% |
0 / 1 |
56.00 | |
0.00% |
0 / 34 |
|||
| validateSaveMarklistCategorizeSubject | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 3 |
|||
| insertMarklistCategorizeSubject | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 17 |
|||
| updateMarklistCategorizeSubject | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 19 |
|||
| deleteMarklistCategorizeSubject | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 23 |
|||
| getMarklistCategorizeSubject | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 37 |
|||
| getAllSubjectCategories | |
0.00% |
0 / 1 |
56.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\MarklistCategorizeSubject; | |
| use com\linways\ec\core\logging\Events; | |
| use com\linways\ec\core\logging\entities\Staff; | |
| use com\linways\core\ams\professional\logging\AMSLogger; | |
| class MarklistCategorizeSubjectService extends BaseService | |
| { | |
| use MakeSingletonTrait; | |
| private function __construct() | |
| { | |
| $this->logger = AMSLogger::getLogger('exam-controller-log'); | |
| } | |
| /** | |
| * Save Marklist Categorize Subject | |
| * @param MarklistCategorizeSubject $marklistCategorizeSubject | |
| * @return $id | |
| */ | |
| public function saveMarklistCategorizeSubject(MarklistCategorizeSubject $marklistCategorizeSubject){ | |
| $marklistCategorizeSubject = $this->realEscapeObject($marklistCategorizeSubject); | |
| $marklistCategorizeSubject->createdBy = $GLOBALS['userId'] ?? $marklistCategorizeSubject->createdBy; | |
| $marklistCategorizeSubject->updatedBy = $GLOBALS['userId'] ?? $marklistCategorizeSubject->updatedBy; | |
| $staffId = $GLOBALS['userId']; | |
| try { | |
| $this->validateSaveMarklistCategorizeSubject($marklistCategorizeSubject); | |
| if (!empty($marklistCategorizeSubject->id)) { | |
| $marklistCategorizeSubject->id = $this->updateMarklistCategorizeSubject($marklistCategorizeSubject); | |
| } else { | |
| $marklistCategorizeSubject->id = $this->insertMarklistCategorizeSubject($marklistCategorizeSubject); | |
| } | |
| AMSLogger::log_info($this->logger,Events::EC_SAVE_MARKLIST_CATEGORIZE_SUBJECT, [ | |
| "staff" => new Staff(["id" => $staffId]), | |
| "request" => $marklistCategorizeSubject, | |
| "status" => StatusConstants::SUCCESS | |
| ]); | |
| } catch (\Exception $e) { | |
| AMSLogger::log_error($this->logger,Events::EC_SAVE_MARKLIST_CATEGORIZE_SUBJECT, [ | |
| "staff" => new Staff(["id" => $staffId]), | |
| "request" => $marklistCategorizeSubject, | |
| "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 subject category! Please try again"); | |
| } else if ($e->getCode() === ExamControllerException::DUPLICATE_ENTRY) { | |
| throw new ExamControllerException(ExamControllerException::DUPLICATE_ENTRY, "Cannot assign subject category.This subject category already assign in this batch"); | |
| } else { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| return $marklistCategorizeSubject->id; | |
| } | |
| /** | |
| * Validate marklist Categorize Subject Request Before Saving | |
| * @param MarklistCategorizeSubject $marklistCategorizeSubject | |
| * @return NULL | |
| */ | |
| private function validateSaveMarklistCategorizeSubject(MarklistCategorizeSubject $marklistCategorizeSubject){ | |
| if (empty($marklistCategorizeSubject->groupId) || empty($marklistCategorizeSubject->subjectCategoryId)) | |
| throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS, " empty parameter. can't save subject category! "); | |
| } | |
| /** | |
| * Insert marklist Categorize Subject | |
| * @param MarklistCategorizeSubject $marklistCategorizeSubject | |
| * @return $id | |
| */ | |
| private function insertMarklistCategorizeSubject(MarklistCategorizeSubject $marklistCategorizeSubject){ | |
| $properties = !empty($marklistCategorizeSubject->properties) ? "'" . json_encode($marklistCategorizeSubject->properties) . "'" : "NULL"; | |
| $query = "INSERT INTO ec_marklist_categorize_subjects | |
| (name,groups_id,subject_category_id,properties,created_by,updated_by) | |
| VALUES | |
| ('$marklistCategorizeSubject->name','$marklistCategorizeSubject->groupId','$marklistCategorizeSubject->subjectCategoryId',$properties,'$marklistCategorizeSubject->createdBy','$marklistCategorizeSubject->updatedBy') | |
| ON DUPLICATE KEY | |
| UPDATE | |
| updated_by = VALUES(created_by), | |
| name = VALUES(name), | |
| properties = VALUES(properties)"; | |
| try { | |
| $marklistCategorizeSubject->id = $this->executeQuery($query,true)->id; | |
| return $marklistCategorizeSubject->id; | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Update marklist Categorize Subject | |
| * @param MarklistCategorizeSubject $marklistCategorizeSubject | |
| * @return $marklistCategorizeSubject->id | |
| */ | |
| private function updateMarklistCategorizeSubject(MarklistCategorizeSubject $marklistCategorizeSubject) | |
| { | |
| $properties = !empty($marklistCategorizeSubject->properties) ? "'".json_encode($marklistCategorizeSubject->properties)."'" : "NULL"; | |
| $query = "UPDATE | |
| ec_marklist_categorize_subjects | |
| SET | |
| name = '$marklistCategorizeSubject->name', | |
| groups_id = '$marklistCategorizeSubject->groupId', | |
| subject_category_id = '$marklistCategorizeSubject->subjectCategoryId', | |
| properties = $properties, | |
| updated_by = '$marklistCategorizeSubject->updatedBy' | |
| WHERE | |
| id = '$marklistCategorizeSubject->id'"; | |
| try { | |
| $this->executeQuery($query); | |
| return $marklistCategorizeSubject->id; | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * Delete marklist Categorize Subject | |
| * @param String $id | |
| * @return NULL | |
| */ | |
| public function deleteMarklistCategorizeSubject($id){ | |
| $id = $this->realEscapeString($id); | |
| $staffId = $GLOBALS['userId']; | |
| $marklistCategorizeSubject = new \stdClass(); | |
| $marklistCategorizeSubject->id = $id; | |
| $currentMarklistCategorizeSubjectDetails = reset($this->getMarklistCategorizeSubject($marklistCategorizeSubject)); | |
| if(empty($currentMarklistCategorizeSubjectDetails)){ | |
| throw new ExamControllerException(ExamControllerException::EMPTY_PARAMETERS,"Can't delete ! subject category missing."); | |
| } | |
| $query = "DELETE FROM | |
| ec_marklist_categorize_subjects | |
| WHERE | |
| id = '$id'"; | |
| try { | |
| $this->executeQuery($query); | |
| // LOGGING | |
| AMSLogger::log_info($this->logger,Events::EC_DELETE_MARKLIST_CATEGORIZE_SUBJECT,[ | |
| "staff" => new Staff(["id" => $staffId]), | |
| "request" => $currentMarklistCategorizeSubjectDetails, | |
| "status" => StatusConstants::SUCCESS | |
| ]); | |
| }catch (\Exception $e) { | |
| throw new ExamControllerException(ExamControllerException::HAVE_RELATION,"Error deleting subject category! Please try again"); | |
| } | |
| } | |
| /** | |
| * get marklist Categorize Subject | |
| * @param $searchRequest | |
| * @return $feeTypes | |
| */ | |
| public function getMarklistCategorizeSubject($searchRequest){ | |
| $searchRequest = $this->realEscapeObject($searchRequest); | |
| try { | |
| $whereQuery = null; | |
| $whereQuery = ""; | |
| if (!empty($searchRequest->id)) { | |
| $marklistCategorizeSubjectIdString = is_array($searchRequest->id) ? "'" . implode("','", $searchRequest->id) . "'" : "'" . $searchRequest->id . "'"; | |
| $whereQuery .= " AND emcs.id IN ( $marklistCategorizeSubjectIdString )"; | |
| } | |
| if (!empty($searchRequest->groupId)) { | |
| $groupIdString = is_array($searchRequest->groupId) ? "'" . implode("','", $searchRequest->groupId) . "'" : "'" . $searchRequest->groupId . "'"; | |
| $whereQuery .= " AND emcs.groups_id IN ( $groupIdString )"; | |
| } | |
| $query = "SELECT | |
| DISTINCT | |
| emcs.id as id, | |
| emcs.name as name, | |
| emcs.groups_id as groupId, | |
| emcs.subject_category_id as subjectCategoryId, | |
| emcs.properties as properties, | |
| g.name as groupName, | |
| sc.subjectcatName as subjectCategoryName | |
| FROM | |
| ec_marklist_categorize_subjects emcs | |
| INNER JOIN `groups` g ON | |
| g.id = emcs.groups_id | |
| INNER JOIN subject_category sc ON | |
| sc.subjectcatID = emcs.subject_category_id | |
| WHERE | |
| 1 = 1 "; | |
| $marklistCategorizeSubjects = $this->executeQueryForList($query . $whereQuery); | |
| array_walk($marklistCategorizeSubjects, function($marklistCategorizeSubject){ | |
| $marklistCategorizeSubject->properties = json_decode($marklistCategorizeSubject->properties); | |
| }); | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| return $marklistCategorizeSubjects; | |
| } | |
| /** | |
| * get all subject categories | |
| * @param $searchRequest | |
| * @return $feeTypes | |
| */ | |
| public function getAllSubjectCategories($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 | |
| sc.subjectcatName as subjectCategoryName, | |
| sc.subjectcatID as subjectCategoryId, | |
| emcs.id as id, | |
| emcs.name as name, | |
| emcs.groups_id as groupId, | |
| emcs.properties as properties, | |
| g.name as groupName | |
| FROM | |
| subject_category sc | |
| INNER JOIN `groups` g | |
| LEFT JOIN ec_marklist_categorize_subjects emcs ON | |
| emcs.subject_category_id = sc.subjectcatID AND | |
| emcs.groups_id = g.id | |
| WHERE | |
| 1 = 1 "; | |
| $marklistCategorizeSubjects = $this->executeQueryForList($query . $whereQuery); | |
| array_walk($marklistCategorizeSubjects, function($marklistCategorizeSubject){ | |
| $marklistCategorizeSubject->properties = json_decode($marklistCategorizeSubject->properties); | |
| if($marklistCategorizeSubject->properties){ | |
| $marklistCategorizeSubject->isSecondLanguage = $marklistCategorizeSubject->properties->isSecondLanguage == '1' ? true : false; | |
| $marklistCategorizeSubject->isOpenCourse = $marklistCategorizeSubject->properties->isOpenCourse == '1' ? true : false; | |
| $marklistCategorizeSubject->priority = $marklistCategorizeSubject->properties->priority; | |
| } | |
| }); | |
| } catch (\Exception $e) { | |
| throw new ExamControllerException($e->getCode(), $e->getMessage()); | |
| } | |
| return $marklistCategorizeSubjects; | |
| } | |
| } |