Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 376 |
PacketService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 15 |
4692.00 | |
0.00% |
0 / 376 |
__construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
getHallListForPacket | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 29 |
|||
saveAnswersheetPacket | |
0.00% |
0 / 1 |
56.00 | |
0.00% |
0 / 26 |
|||
validateSaveAnswersheetPacketRequest | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 8 |
|||
generateAnswersheetPackets | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 24 |
|||
insertAnswersheetPacket | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 16 |
|||
updateAnswersheetPacket | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 18 |
|||
deleteAnswersheetPacket | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 19 |
|||
restoreAnswersheetPacket | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 19 |
|||
searchAnswersheetPacket | |
0.00% |
0 / 1 |
156.00 | |
0.00% |
0 / 57 |
|||
getHallListByAssessmentId | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 38 |
|||
getAssignedPackets | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 25 |
|||
getCourseTypeIdByExamRegistrationId | |
0.00% |
0 / 1 |
12.00 | |
0.00% |
0 / 36 |
|||
getThirdValuationAssignedPackets | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 34 |
|||
getStudentAssignedPackets | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 25 |
<?php | |
namespace com\linways\ec\core\service; | |
use com\linways\base\util\SecurityUtils; | |
use com\linways\base\util\MakeSingletonTrait; | |
use com\linways\ec\core\exception\ExamControllerException; | |
use com\linways\ec\core\exception\ECCoreException; | |
use com\linways\ec\core\constant\StatusConstants; | |
use com\linways\ec\core\request\SearchAnswersheetPacketRequest; | |
use com\linways\ec\core\mapper\PacketServiceMapper; | |
use com\linways\ec\core\dto\AnswersheetPacket; | |
class PacketService extends BaseService | |
{ | |
use MakeSingletonTrait; | |
private function __construct() { | |
$this->mapper = PacketServiceMapper::getInstance()->getMapper(); | |
} | |
/** | |
* Get hall list | |
* @param $paperSubjectId | |
* @return Array $blockReasons | |
*/ | |
public function getHallListForPacket ($paperSubjectId) | |
{ | |
$paperSubjectId = $this->realEscapeString($paperSubjectId); | |
$whereQuery = ""; | |
$limitQuery = ""; | |
$query = "SELECT | |
eh.hallID AS id, | |
eh.hallName AS name, | |
ehas.studentID AS studentId, | |
esar.properties | |
FROM | |
ec_exam_registration_subject eers | |
INNER JOIN exam e ON | |
e.am_assessment_id = eers.am_assessment_id | |
INNER JOIN exam_hall_arranged_students ehas ON | |
ehas.examID = e.examID | |
INNER JOIN exam_halls eh ON | |
eh.hallID = ehas.hallID | |
INNER JOIN ec_student_assessment_registration esar ON | |
esar.student_id = ehas.studentID | |
AND esar.am_assessment_id = eers.am_assessment_id | |
WHERE | |
eers.cm_academic_paper_subjects_id = '$paperSubjectId'"; | |
try { | |
$examHalls = $this->executeQueryForList($query, $this->mapper[PacketServiceMapper::EXAM_CHARTED_HALLS]); | |
} catch (\Exception $e) { | |
throw new ExamControllerException(ExamControllerException::NO_HALLS_FOR_EXAM,"Halls not defines! Please try again."); | |
} | |
return $examHalls; | |
} | |
/** | |
* Save answersheet packet | |
* @param AnswersheetPacket $answersheetPacket | |
* @return AnswersheetPacket $answersheetPacket | |
*/ | |
public function saveAnswersheetPacket (AnswersheetPacket $answersheetPacket) | |
{ | |
$answersheetPacket = $this->realEscapeObject($answersheetPacket); | |
$answersheetPacket->createdBy = $GLOBALS['userId'] ?? $answersheetPacket->createdBy; | |
$answersheetPacket->updatedBy = $GLOBALS['userId'] ?? $answersheetPacket->updatedBy; | |
// $answersheetPacket->identifyingContext = '{ | |
// "examRegistrationId" : "abcdefg" | |
// }'; | |
// $answersheetPacket->type = "EXAM_REGISTRATION"; | |
try{ | |
$answersheetPacket = $this->generateAnswersheetPackets($answersheetPacket); | |
$this->validateSaveAnswersheetPacketRequest($answersheetPacket); | |
if(!empty($answersheetPacket->id)) | |
{ | |
$answersheetPacket->id = $this->updateAnswersheetPacket($answersheetPacket); | |
} | |
else | |
{ | |
$answersheetPacket->id = $this->insertAnswersheetPacket($answersheetPacket); | |
} | |
}catch(\Exception $e) { | |
if($e->getCode() !== ECCoreException::INVALID_PARAMETERS && $e->getCode() !== ECCoreException::EMPTY_PARAMETERS && $e->getCode() !== ECCoreException::DUPLICATE_ENTRY) { | |
throw new ECCoreException(ECCoreException::ERROR_SAVING_GRADE_SCHEME, "Failed to save answersheet packet! Please try again"); | |
} else if ($e->getCode() === ECCoreException::DUPLICATE_ENTRY) { | |
throw new ECCoreException (ECCoreException::DUPLICATE_ENTRY, "Cannot create answersheet packet! already exists."); | |
} else { | |
throw new ECCoreException ($e->getCode(), $e->getMessage()); | |
} | |
} | |
return $answersheetPacket; | |
} | |
/** | |
* Validate Answersheet Packet Request Before Saving | |
* @param AnswersheetPacket $answersheetPacket | |
* @return NULL | |
*/ | |
private function validateSaveAnswersheetPacketRequest($answersheetPacket) | |
{ | |
if(empty($answersheetPacket->identifyingContext) || empty($answersheetPacket->type)) | |
throw new ECCoreException(ECCoreException::EMPTY_PARAMETERS, "Answersheet packet is assigned to invalid context. Please retry!"); | |
if(empty($answersheetPacket->config)) | |
throw new ECCoreException(ECCoreException::EMPTY_PARAMETERS, "Answersheet packet config is empty! Please configure answersheet packet"); | |
if(empty($answersheetPacket->packets)) | |
throw new ECCoreException(ECCoreException::EMPTY_PARAMETERS, "Answersheet packet list is empty! Please generate packets for answersheet"); | |
} | |
/** | |
* Generate Answersheet Packets | |
* @param AnswersheetPacket $answersheetPacket | |
* @return NULL | |
*/ | |
private function generateAnswersheetPackets($answersheetPacket) | |
{ | |
if(!empty($answersheetPacket->config->startNo) && !empty($answersheetPacket->config->endNo)){ | |
foreach (range($answersheetPacket->config->startNo, $answersheetPacket->config->endNo) as $packetNo) { | |
$packet = new \stdClass(); | |
$packet->name = $answersheetPacket->config->prefix . $packetNo; | |
$packet->count = $answersheetPacket->config->countPerPacket; | |
$packet->isFilled = false; | |
$packetList[$packet->name] = $packet; | |
} | |
if (!empty($answersheetPacket->packets)) { | |
$newPaketList = array_column($packetList,"name"); | |
$oldPacketList = array_column(array_filter($answersheetPacket->packets,function($packet){ | |
return $packet->isFilled; | |
}),"name"); | |
if (!empty(array_diff($oldPacketList, $newPaketList))) { | |
throw new ECCoreException(ECCoreException::ERROR_DELETING_FILLED_PACKETS, "Answersheet packet is filled.Can not delete filled packets!"); | |
} | |
} | |
$packetList = array_values($packetList); | |
$packetDetailsObj = new \stdClass(); | |
$packetDetailsObj->packetDetails = $packetList ; | |
$answersheetPacket->packets = $packetDetailsObj; | |
} | |
return $answersheetPacket; | |
} | |
/** | |
* Insert answersheet packet | |
* @param AnswersheetPacket $answersheetPacket | |
* @return String $id | |
*/ | |
private function insertAnswersheetPacket(AnswersheetPacket $answersheetPacket) | |
{ | |
$config = !empty($answersheetPacket->config) ? "'" . json_encode($answersheetPacket->config) . "'" : "JSON_OBJECT"; | |
$packets = !empty($answersheetPacket->packets) ? "'" . json_encode($answersheetPacket->packets) . "'" : "JSON_OBJECT"; | |
$identifyingContext = !empty($answersheetPacket->identifyingContext) ? "'" . json_encode($answersheetPacket->identifyingContext) . "'" : "JSON_OBJECT"; | |
$id = SecurityUtils::getRandomString(); | |
$query = "INSERT INTO ec_answersheet_packet | |
(id,identifying_context,identifying_type,config,packet_list,created_by,updated_by) | |
VALUES | |
('$id',$identifyingContext,'$answersheetPacket->type',$config,$packets,'$answersheetPacket->createdBy','$answersheetPacket->updatedBy')"; | |
try { | |
$this->executeQuery($query); | |
return $id; | |
} catch (\Exception $e) { | |
throw new ECCoreException($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* Update Answersheet Packet | |
* @param AnswersheetPacket $answersheetPacket | |
* @return NULL | |
*/ | |
private function updateAnswersheetPacket(AnswersheetPacket $answersheetPacket) | |
{ | |
$config = !empty($answersheetPacket->config) ? "'" . json_encode($answersheetPacket->config) . "'" : "JSON_OBJECT"; | |
$packets = !empty($answersheetPacket->packets) ? "'" . json_encode($answersheetPacket->packets) . "'" : "JSON_OBJECT"; | |
$query = "UPDATE | |
ec_answersheet_packet | |
SET | |
config = $config, | |
packet_list = $packets, | |
updated_by = '$answersheetPacket->updatedBy' | |
WHERE | |
id = '$answersheetPacket->id'"; | |
try { | |
$this->executeQuery($query); | |
return $answersheetPacket->id; | |
} catch (\Exception $e) { | |
throw new ECCoreException($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* Delete Answersheet Packet (Soft Delete) | |
* @param String $id | |
* @return NULL | |
*/ | |
public function deleteAnswersheetPacket($id) | |
{ | |
$id = $this->realEscapeString($id); | |
$updatedBy = $GLOBALS['userId']; | |
if(empty($id)) { | |
throw new ECCoreException(ECCoreException::EMPTY_PARAMETERS, "No answersheet packet selected! Please select a answersheetPacket to delete"); | |
} | |
//TODO: Do validation before deleting | |
$query = "UPDATE | |
ec_answersheet_packet | |
SET | |
trashed = UTC_TIMESTAMP(), | |
updated_by = '$updatedBy' | |
WHERE | |
id = '$id'"; | |
try { | |
$this->executeQuery($query); | |
} catch (\Exception $e) { | |
throw new ECCoreException(ECCoreException::ERROR_DELETING_GRADE_SCHEME, "Error deleting answersheet packet! Please try again"); | |
} | |
} | |
/** | |
* Restore Answersheet Packet | |
* @param String $id | |
* @return NULL | |
*/ | |
public function restoreAnswersheetPacket($id) | |
{ | |
$id = $this->realEscapeString($id); | |
$updatedBy = $GLOBALS['userId']; | |
if(empty($id)) { | |
throw new ECCoreException(ECCoreException::EMPTY_PARAMETERS,"No answersheet packet selected! Please select a answersheetPacket to restore"); | |
} | |
$query = "UPDATE | |
ec_answersheet_packet | |
SET | |
trashed = NULL, | |
updated_by = '$updatedBy' | |
WHERE | |
id = '$id'"; | |
try { | |
$this->executeQuery($query); | |
} catch (\Exception $e) { | |
throw new ECCoreException(ECCoreException::ERROR_RESTORING_GRADE_SCHEME,"Error restoring answersheet packet! Please try again"); | |
} | |
} | |
/** | |
* Search Answersheet Packet Details | |
* @param SearchAnswersheetPacketRequest $request | |
* @return AnswersheetPacket | |
*/ | |
public function searchAnswersheetPacket(SearchAnswersheetPacketRequest $request) | |
{ | |
$request = $this->realEscapeObject($request); | |
$whereQuery = ""; | |
$limitQuery = ""; | |
$answersheetPacketServiceMapper = PacketServiceMapper::SEARCH_ASWERSHEET_PACKETS; | |
if($request->trashed === StatusConstants::ACTIVE) { | |
$whereQuery .= " AND eap.trashed IS NULL "; | |
} | |
if($request->trashed === StatusConstants::TRASHED) { | |
$whereQuery .= " AND eap.trashed IS NOT NULL "; | |
} | |
if(!empty($request->id)) { | |
$whereQuery .= " AND eap.id='$request->id' "; | |
} | |
if(!empty($request->examRegistrationId)) { | |
$whereQuery .= " AND eap.identifying_context->>'$.examRegistrationId' = '$request->examRegistrationId' "; | |
} | |
if(!empty($request->revaluationType)) { | |
$whereQuery .= " AND eap.identifying_context->>'$.revaluationType' = '$request->revaluationType' "; | |
} | |
if(!empty($request->paperSubjectId)) { | |
$whereQuery .= " AND eap.identifying_context->>'$.paperSubjectId' = '$request->paperSubjectId' "; | |
} | |
if(!empty($request->type)) { | |
$whereQuery .= " AND eap.identifying_type = '$request->type' "; | |
} | |
if($request->startIndex !== "" && $request->endIndex !== "") | |
{ | |
$limitQuery .= " LIMIT $request->startIndex, $request->endIndex"; | |
} | |
$query = "SELECT | |
eap.id, | |
eap.identifying_context, | |
eap.identifying_type, | |
eap.config, | |
eap.packet_list, | |
eap.trashed, | |
eap.created_by, | |
eap.created_date, | |
eap.updated_by, | |
eap.updated_date | |
FROM | |
ec_answersheet_packet eap | |
WHERE | |
1 = 1 | |
$whereQuery | |
$limitQuery"; | |
try { | |
$answersheetPackets = $this->executeQueryForList($query,$this->mapper[$answersheetPacketServiceMapper]); | |
} catch (\Exception $e) { | |
throw new ECCoreException(ECCoreException::ERROR_FETCHING_ANSWERSHEET_PACKETS,"Cannot fetch answersheet packet details! Please try again"); | |
} | |
foreach ($answersheetPackets as $packet) { | |
$packet->packets = array_values((array)$packet->packets); | |
} | |
return $answersheetPackets; | |
} | |
/** | |
* Get hall list By assessment Id | |
* @param $assessmentId | |
* @return Array $blockReasons | |
*/ | |
public function getHallListByAssessmentId ($assessmentId){ | |
$assessmentId = stripslashes($assessmentId); | |
$examHalls = []; | |
$whereQuery = ""; | |
$limitQuery = ""; | |
$query = "SELECT | |
eeh.id AS id, | |
eeh.name AS name, | |
ehagas.student_id AS studentId, | |
ehagah.id AS studentHallId, | |
esar.properties | |
FROM | |
ec_hall_arrangement_group_assigned_student ehagas | |
INNER JOIN ec_hall_arrangement_group_assigned_hall ehagah ON | |
ehagah.id = ehagas.ec_hall_arrangement_group_assigned_hall_id AND ehagah.is_locked = '1' | |
INNER JOIN ec_exam_hall eeh ON | |
eeh.id = ehagah.ec_exam_hall_id | |
INNER JOIN ec_student_assessment_registration esar ON | |
esar.student_id = ehagas.student_id | |
AND esar.am_assessment_id = ehagas.am_assessment_id | |
AND esar.properties ->> '$.registrationStatus' = 'REGISTERED' | |
WHERE | |
ehagas.am_assessment_id IN ('$assessmentId') | |
ORDER BY eeh.name ASC"; | |
try { | |
$examHallStudents = $this->executeQueryForList($query); | |
foreach($examHallStudents as $examHallStudent){ | |
$examHalls[$examHallStudent->id]->id = $examHallStudent->id; | |
$examHalls[$examHallStudent->id]->name = $examHallStudent->name; | |
$examHalls[$examHallStudent->id]->students[$examHallStudent->studentId]->id = $examHallStudent->studentId; | |
$examHalls[$examHallStudent->id]->students[$examHallStudent->studentId]->hallId = $examHallStudent->studentHallId; | |
$examHalls[$examHallStudent->id]->students[$examHallStudent->studentId]->properties = $examHallStudent->properties ? json_decode($examHallStudent->properties) : new \stdClass(); | |
} | |
$examHalls = array_values($examHalls); | |
} catch (\Exception $e) { | |
throw new ExamControllerException(ExamControllerException::NO_HALLS_FOR_EXAM,"Halls not defines! Please try again."); | |
} | |
return $examHalls; | |
} | |
/** | |
* Get assigned packets | |
* @param $request | |
* @return Array $packets | |
*/ | |
public function getAssignedPackets ($request){ | |
$request = $this->realEscapeObject($request); | |
$request->assessmentId = stripslashes($request->assessmentId); | |
if(!empty($request->assessmentId)) { | |
$whereQuery .= " AND aa.id NOT IN ('$request->assessmentId')"; | |
} | |
if(!empty($request->examRegistrationId)) { | |
$whereQuery .= " AND aa.identifying_context->>'$.examRegistrationId' = '$request->examRegistrationId'"; | |
} | |
$query = "SELECT DISTINCT | |
esar.valuation_details->>'$.packetNo' as packetNo | |
FROM | |
ec_student_assessment_registration esar | |
INNER JOIN studentaccount sa ON | |
sa.studentID = esar.student_id | |
INNER JOIN am_assessment aa ON | |
aa.id = esar.am_assessment_id | |
WHERE | |
esar.trashed IS NULL AND esar.valuation_details->>'$.packetNo' IS NOT NULL"; | |
try { | |
$packets = $this->executeQueryForList($query.$whereQuery); | |
} catch (\Exception $e) { | |
throw new ExamControllerException(ExamControllerException::ERROR_FETCHING_STUDENT_REGISTRATION,"Cannot fetch packets! Please try again"); | |
} | |
return $packets; | |
} | |
/** | |
* getCourseTypeIdByExamRegistrationId | |
* @param ExamRegistrationId $examRegistrationId | |
* @return $courseTypeId | |
*/ | |
public function getCourseTypeIdByExamRegistrationId($examRegistrationId,$type=null) | |
{ | |
$examRegistrationId = $this->realEscapeString($examRegistrationId); | |
$whereQuery = ""; | |
$joinQuery = ""; | |
if($type == 'REVALUATION'){ | |
$joinQuery .=" INNER JOIN ec_exam_registration eerp ON | |
eerp.id = eerb.ec_exam_registration_id AND | |
eerp.trashed IS NULL | |
INNER JOIN ec_exam_registration eer ON | |
eer.properties->>'$.parentExamRegistrationId' = eerp.id AND | |
eer.trashed IS NULL "; | |
$whereQuery .= " AND eer.id = '$examRegistrationId'"; | |
} | |
else{ | |
$whereQuery .= " AND eerb.ec_exam_registration_id = '$examRegistrationId'"; | |
} | |
try{ | |
$query = "SELECT | |
p.course_type_id as courseTypeId | |
FROM | |
ec_exam_registration_batch eerb | |
$joinQuery | |
INNER JOIN `groups` g ON | |
g.id = eerb.groups_id | |
INNER JOIN program p ON | |
p.id = g.properties->>'$.programId' | |
WHERE | |
1=1 "; | |
$courseType = $this->executeQueryForObject($query.$whereQuery); | |
} | |
catch (\Exception $e) | |
{ | |
throw new ExamControllerException($e->getCode(),$e->getMessage()); | |
} | |
return $courseType->courseTypeId; | |
} | |
/** | |
* Get assigned packets | |
* @param $request | |
* @return Array $packets | |
*/ | |
public function getThirdValuationAssignedPackets ($request){ | |
$request = $this->realEscapeObject($request); | |
$request->assessmentId = stripslashes($request->assessmentId); | |
if(!empty($request->assessmentId)) { | |
$whereQuery .= " AND aa.id IN ('$request->assessmentId')"; | |
} | |
if(!empty($request->examRegistrationId)) { | |
$whereQuery .= " AND aa.identifying_context->>'$.examRegistrationId' = '$request->examRegistrationId'"; | |
} | |
if($request->type == 'REVALUATION'){ | |
$joinQuery .= "INNER JOIN ec_revaluation_mark erm2 ON | |
erm2.student_id = sa.studentID AND erm2.am_assessment_id = esar.am_assessment_id AND erm2.revaluation_type = CAST(esar.identifying_context->>'$.revaluationType'AS CHAR) AND erm2.valuation_count = '2' AND erm2.properties ->> '$.hasEligibleThirdValuation' = '1'"; | |
} | |
else{ | |
$joinQuery = "INNER JOIN externalexam_thirdvalstudents ets ON | |
ets.studentID = sa.studentID AND ets.am_assessment_id = esar.am_assessment_id"; | |
} | |
$query = "SELECT DISTINCT | |
esar.valuation_details->>'$.packetNo' as packetNo | |
FROM | |
ec_student_assessment_registration esar | |
INNER JOIN studentaccount sa ON | |
sa.studentID = esar.student_id | |
INNER JOIN am_assessment aa ON | |
aa.id = esar.am_assessment_id | |
$joinQuery | |
WHERE | |
esar.trashed IS NULL AND esar.valuation_details->>'$.packetNo' IS NOT NULL"; | |
try { | |
$packets = $this->executeQueryForList($query.$whereQuery); | |
} catch (\Exception $e) { | |
throw new ExamControllerException(ExamControllerException::ERROR_FETCHING_STUDENT_REGISTRATION,"Cannot fetch packets! Please try again"); | |
} | |
return $packets; | |
} | |
/** | |
* Get assigned packets | |
* @param $request | |
* @return Array $packets | |
*/ | |
public function getStudentAssignedPackets ($request){ | |
$request = $this->realEscapeObject($request); | |
$request->assessmentId = stripslashes($request->assessmentId); | |
if(!empty($request->assessmentId)) { | |
$whereQuery .= " AND aa.id IN ('$request->assessmentId')"; | |
} | |
if(!empty($request->examRegistrationId)) { | |
$whereQuery .= " AND aa.identifying_context->>'$.examRegistrationId' = '$request->examRegistrationId'"; | |
} | |
$query = "SELECT DISTINCT | |
esar.valuation_details->>'$.packetNo' as packetNo | |
FROM | |
ec_student_assessment_registration esar | |
INNER JOIN studentaccount sa ON | |
sa.studentID = esar.student_id | |
INNER JOIN am_assessment aa ON | |
aa.id = esar.am_assessment_id | |
WHERE | |
esar.trashed IS NULL AND esar.valuation_details->>'$.packetNo' IS NOT NULL"; | |
try { | |
$packets = $this->executeQueryForList($query.$whereQuery); | |
} catch (\Exception $e) { | |
throw new ExamControllerException(ExamControllerException::ERROR_FETCHING_STUDENT_REGISTRATION,"Cannot fetch packets! Please try again"); | |
} | |
return $packets; | |
} | |
} |