Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 332 |
ActivityService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 14 |
2652.00 | |
0.00% |
0 / 332 |
__construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
__clone | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
getInstance | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
createActivity | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 24 |
|||
updateActivity | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 16 |
|||
deleteActivity | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 9 |
|||
getActivityById | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 12 |
|||
searchActivities | |
0.00% |
0 / 1 |
210.00 | |
0.00% |
0 / 54 |
|||
addAttendee | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 19 |
|||
addAttendees | |
0.00% |
0 / 1 |
20.00 | |
0.00% |
0 / 21 |
|||
addAttendeesByBatch | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 51 |
|||
addAttendeesByDept | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 51 |
|||
searchAttendees | |
0.00% |
0 / 1 |
56.00 | |
0.00% |
0 / 60 |
|||
deleteAttendee | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 8 |
<?php | |
namespace com\linways\core\ams\professional\service; | |
use com\linways\core\ams\professional\dto\Activity; | |
use com\linways\core\ams\professional\dto\Attendee; | |
use com\linways\core\ams\professional\request\SearchActivityRequest; | |
use com\linways\core\ams\professional\request\SearchAttendeesRequest; | |
use com\linways\core\ams\professional\request\AddAttendeesByBatchRequest; | |
use com\linways\core\ams\professional\request\AddAttendeesByDeptRequest; | |
use com\linways\core\ams\professional\response\SearchActivityResponse; | |
use com\linways\core\ams\professional\response\SearchAttendeesResponse; | |
use com\linways\core\ams\professional\exception\ProfessionalException; | |
class ActivityService extends BaseService | |
{ | |
// /Condition 1 - Presence of a static member variable | |
private static $_instance = null; | |
// /Condition 2 - Locked down the constructor | |
private function __construct() | |
{} | |
// Prevent any oustide instantiation of this class | |
// /Condition 3 - Prevent any object or instance of that class to be cloned | |
private function __clone() | |
{} | |
// Prevent any copy of this object | |
// /Condition 4 - Have a single globally accessible static method | |
public static function getInstance() | |
{ | |
if (!is_object(self::$_instance)) // or if( is_null(self::$_instance) ) or if( self::$_instance == null ) | |
self::$_instance = new self(); | |
return self::$_instance; | |
} | |
/** | |
* Method for create acitivity | |
* | |
* @param Activity $activity | |
*/ | |
public function createActivity($activity) | |
{ | |
$id = NULL; | |
$sql = "INSERT INTO activity | |
(code,name,description,date,venu, | |
createdBy,updatedBy,createdDate,updatedDate,categoryId) | |
VALUES | |
('$activity->code', | |
'$activity->name', | |
'$activity->description', | |
'$activity->date', | |
'$activity->venu', | |
'$activity->createdBy', | |
'$activity->updatedBy', | |
utc_timestamp(), | |
utc_timestamp(), | |
'$activity->categoryId' | |
) "; | |
try { | |
$id = $this->executeQueryForObject($sql, true); | |
} catch (\Exception $e) { | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
return $id; | |
} | |
/** | |
* Method for update acitivity | |
* | |
* @param Activity $activity | |
*/ | |
public function updateActivity($activity) | |
{ | |
$sql = "UPDATE activity SET | |
name='$activity->name', | |
description='$activity->description', | |
date='$activity->date', | |
venu='$activity->venu', | |
updatedBy='$activity->updatedBy', | |
updatedDate=utc_timestamp(), | |
categoryId='$activity->categoryId' | |
WHERE id=$activity->id;"; | |
try { | |
$this->executeQuery($sql); | |
} catch (\Exception $e) { | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* Method for deleting acitivity | |
* | |
* @param int $activityId | |
* - activity id | |
*/ | |
public function deleteActivity($activityId) | |
{ | |
$sql = "DELETE FROM activity | |
WHERE id=$activityId"; | |
try { | |
$this->executeQuery($sql); | |
} catch (\Exception $e) { | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* Get acitivity details by passing activity id | |
* | |
* @param int $activityId | |
* - activity id | |
* @return Activity | |
*/ | |
public function getActivityById($activityId) | |
{ | |
$acitivity = null; | |
$sql = "SELECT a.*,ac.name as categoryName | |
from activity as a INNER JOIN activity_category as ac | |
ON a.categoryId=ac.id where a.id=$activityId"; | |
try { | |
$acitivity = $this->executeQueryForObject($sql); | |
} catch (\Exception $e) { | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
return $acitivity; | |
} | |
/** | |
* Search acitivity details | |
* | |
* @param SearchActivityRequest $searchActivityRequest | |
* @return array | |
*/ | |
public function searchActivities($searchActivityRequest) | |
{ | |
$response = new SearchActivityResponse(); | |
$criteria =" WHERE a.id IS NOT NULL "; | |
$selectCountQuery = "SELECT count(distinct a.id) as totalRecords | |
FROM activity a INNER JOIN activity_category ac ON a.categoryid=ac.id "; | |
$selectQuery = "SELECT a.*,ac.name as categoryName,ac.id as categoryID | |
FROM activity a INNER JOIN activity_category ac ON a.categoryid=ac.id "; | |
if ($searchActivityRequest != NULL) { | |
// user and type filter | |
if (! empty($searchActivityRequest->userId) && | |
! empty($searchActivityRequest->userType)) { | |
$selectQuery .= " LEFT JOIN activity_attendee aa on aa.activityId = a.id "; | |
$selectCountQuery .= " LEFT JOIN activity_attendee aa on aa.activityId = a.id "; | |
$criteria .= " AND aa.userId='$searchActivityRequest->userId' AND aa.userType = '$searchActivityRequest->userType' "; | |
} | |
// Code | |
if (! empty($searchActivityRequest->code)) { | |
$criteria .= " AND a.code like '%$searchActivityRequest->code%'"; | |
} | |
// Description | |
if (! empty($searchActivityRequest->description)) { | |
$criteria .= " AND a.description like '%$searchActivityRequest->description%'"; | |
} | |
// Name | |
if (! empty($searchActivityRequest->name)) { | |
$criteria .= " AND a.name like '%$searchActivityRequest->name%'"; | |
} | |
// Venu | |
if (! empty($searchActivityRequest->venu)) { | |
$criteria .= " AND a.venu like '%$searchActivityRequest->venu%'"; | |
} | |
// Activity Date | |
if (! empty($searchActivityRequest->date)) { | |
$criteria .= " AND a.date='$searchActivityRequest->date'"; | |
} | |
if (! empty($searchActivityRequest->categoryId)) { | |
$criteria .= " AND a.categoryId='$searchActivityRequest->categoryId'"; | |
} | |
// Activity Date Range Criteria | |
if (! empty($searchActivityRequest->fromDate) && ! empty($searchActivityRequest->toDate)) { | |
$criteria .= " AND (a.date BETWEEN '$searchActivityRequest->fromDate' AND '$searchActivityRequest->toDate')"; | |
} | |
$selectCountQuery .= $criteria; | |
// Add Sorting | |
if (! empty($searchActivityRequest->sortBy)) { | |
$criteria .= " ORDER BY $searchActivityRequest->sortBy $searchActivityRequest->sortOrder"; | |
} | |
$selectQuery .= $criteria; | |
try | |
{ | |
$response->totalRecords = $this->executeQueryForObject($selectCountQuery)->totalRecords; | |
$response->activities = $this->executeQueryForList($selectQuery); | |
} catch (\Exception $e) | |
{ | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
} else | |
{ | |
throw new ProfessionalException (ProfessionalException::INVALID_REQUEST, "Request is invalid or null!"); | |
} | |
return $response; | |
} | |
/** | |
* create activity attendee | |
* | |
* @param Attendee $attendee | |
* @throws ProfessionalException | |
* @return int | |
*/ | |
public function addAttendee($attendee) | |
{ | |
$id = NULL; | |
$sql = "INSERT INTO activity_attendee | |
(userId,userType,activityId,createdBy,updatedBy,createdDate,updatedDate) | |
VALUES | |
('$attendee->userId', | |
'$attendee->userType', | |
'$attendee->activityId', | |
'$attendee->createdBy', | |
utc_timestamp(), | |
'$attendee->updatedBy', | |
utc_timestamp()); "; | |
try { | |
$id = $this->executeQueryForObject($sql, true); | |
} catch (\Exception $e) { | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
return $id; | |
} | |
/** | |
* create activity attendees | |
* | |
* @param array $attendeesList | |
* @throws ProfessionalException | |
*/ | |
public function addAttendees($attendeesList) | |
{ | |
if (! empty($attendeesList)) { | |
foreach ($attendeesList as $attendee) { | |
$sql = "INSERT INTO activity_attendee | |
(userId,userType,activityId,createdBy,updatedBy,createdDate,updatedDate) | |
VALUES | |
('$attendee->userId', | |
'$attendee->userType', | |
'$attendee->activityId', | |
'$attendee->createdBy', | |
utc_timestamp(), | |
'$attendee->updatedBy', | |
utc_timestamp()); "; | |
try { | |
$this->executeQuery($sql); | |
} catch (\Exception $e) { | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
} | |
} | |
} | |
/** | |
* create activity attendee by Batch | |
* | |
* @param AddAttendeesByBatchRequest $addAttendeesByBatchRequest | |
* @throws ProfessionalException | |
*/ | |
public function addAttendeesByBatch($addAttendeesByBatchRequest) | |
{ | |
$sql = "INSERT INTO activity_attendee (userId,userType,activityId, | |
createdDate,createdBy,updatedDate,updatedBy)"; | |
switch ($addAttendeesByBatchRequest->userType) { | |
case Attendee::STAFF: | |
$sqlValues = " | |
SELECT DISTINCT sbsr.staffID, | |
'$addAttendeesByBatchRequest->userType', | |
$addAttendeesByBatchRequest->activityId, | |
utc_timestamp(), | |
$addAttendeesByBatchRequest->createdBy, | |
utc_timestamp(), | |
$addAttendeesByBatchRequest->updatedBy | |
FROM batches b | |
INNER JOIN sbs_relation sbsr ON sbsr.batchID = b.batchId and sbsr.semID = b.semID | |
INNER JOIN staffaccounts st on st.staffID = sbsr.staffID | |
LEFT JOIN activity_attendee aa on aa.activityId = $addAttendeesByBatchRequest->activityId | |
AND aa.userType = 'STAFF' AND aa.userId =sbsr.staffID | |
WHERE b.batchHide = 0 AND st.isResigned = 0 AND b.batchName <> 'failed' | |
AND aa.id IS NULL"; | |
break; | |
case Attendee::STUDENT: | |
$sqlValues = "SELECT distinct s.studentId, | |
'$addAttendeesByBatchRequest->userType', | |
$addAttendeesByBatchRequest->activityId, | |
utc_timestamp(), | |
$addAttendeesByBatchRequest->createdBy, | |
utc_timestamp(), | |
$addAttendeesByBatchRequest->updatedBy | |
FROM batches b INNER JOIN studentaccount s on s.batchID = b.batchId | |
LEFT JOIN activity_attendee aa on aa.activityId = $addAttendeesByBatchRequest->activityId | |
AND aa.userType = 'STUDENT' AND aa.userId =s.studentId | |
WHERE b.batchHide = 0 AND b.batchName <> 'failed' | |
AND aa.id IS NULL "; | |
break; | |
default: | |
throw new ProfessionalException (ProfessionalException::INVALID_ACTIVITY_USER_TYPE, "User Type is empty or not supportted!"); | |
break; | |
} | |
try { | |
$sql .= " " . $sqlValues; | |
// Add batch All condition | |
if (! empty($addAttendeesByBatchRequest->batchIds)) { | |
$batchIds = implode(",", $addAttendeesByBatchRequest->batchIds); | |
$sql .= " AND b.batchID IN ( $batchIds )"; | |
} | |
$this->executeQuery($sql); | |
} catch (\Exception $e) { | |
echo $sql; | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* create activity attendee by dept | |
* | |
* @param AddAttendeesByDeptRequest $addAttendeesByDeptRequest | |
* @throws ProfessionalException | |
*/ | |
public function addAttendeesByDept($addAttendeesByDeptRequest) | |
{ | |
$sql = "INSERT INTO activity_attendee (userId,userType,activityId, | |
createdDate,createdBy,updatedDate,updatedBy) "; | |
switch ($addAttendeesByDeptRequest->userType) { | |
case Attendee::STAFF: | |
$sqlValues = "SELECT DISTINCT sbsr.staffID, | |
'$addAttendeesByDeptRequest->userType', | |
$addAttendeesByDeptRequest->activityId, | |
utc_timestamp(), | |
$addAttendeesByDeptRequest->createdBy, | |
utc_timestamp(), | |
$addAttendeesByDeptRequest->updatedBy | |
FROM batches b | |
INNER JOIN sbs_relation sbsr ON sbsr.batchID = b.batchId and sbsr.semID = b.semID | |
INNER JOIN staffaccounts st on st.staffID = sbsr.staffID | |
LEFT JOIN activity_attendee aa on aa.activityId = $addAttendeesByDeptRequest->activityId | |
AND aa.userType = 'STAFF' AND aa.userId =sbsr.staffID | |
WHERE b.batchHide = 0 AND st.isResigned = 0 AND b.batchName <> 'failed' | |
AND aa.id IS NULL "; | |
break; | |
case Attendee::STUDENT: | |
$sqlValues = " SELECT DISTINCT s.studentID, | |
'$addAttendeesByDeptRequest->userType', | |
$addAttendeesByDeptRequest->activityId, | |
utc_timestamp(), | |
$addAttendeesByDeptRequest->createdBy, | |
utc_timestamp(), | |
$addAttendeesByDeptRequest->updatedBy | |
FROM department d INNER JOIN batches b ON b.deptID=d.deptID | |
INNER JOIN studentaccount s on s.batchID = b.batchId | |
LEFT JOIN activity_attendee aa on aa.activityId = $addAttendeesByDeptRequest->activityId | |
AND aa.userType = 'STUDENT' AND aa.userId =s.studentID | |
WHERE b.batchHide = 0 and b.batchName <> 'failed' | |
AND aa.id IS NULL"; | |
break; | |
default: | |
throw new ProfessionalException (ProfessionalException::INVALID_ACTIVITY_USER_TYPE, "User Type is empty or not supportted!"); | |
break; | |
} | |
try { | |
$sql .= " " . $sqlValues; | |
// Add dept All condition | |
if (! empty($addAttendeesByDeptRequest->deptIds)) { | |
$deptIds = implode(",", $addAttendeesByDeptRequest->deptIds); | |
$sql .= " AND b.deptID IN ( $deptIds )"; | |
} | |
$this->executeQuery($sql); | |
} catch (\Exception $e) { | |
echo $sql; | |
throw new ProfessionalException($e->getCode(), $e->getMessage()); | |
} | |
} | |
/** | |
* Search acitivity attendees | |
* | |
* @param SearchAttendeesRequest $searchAttendeesRequest | |
* @return array | |
*/ | |
public function searchAttendees($searchAttendeesRequest) | |
{ | |
$response = new SearchAttendeesResponse(); | |
$criteria = null; | |
$selectCountQuery = "SELECT count(distinct userId) as totalRecords FROM | |
(SELECT aa.activityId | |
,aa.userId as userId | |
,sfa.staffName as userName | |
,aa.userType as userType | |
FROM activity_attendee aa | |
INNER JOIN staffaccounts sfa on sfa.staffID = aa.userId and aa.userType = 'STAFF' | |
UNION | |
SELECT aa.activityId | |
,aa.userId as userId | |
,sta.studentName as userName | |
,aa.userType as userType | |
FROM activity_attendee aa | |
INNER JOIN studentaccount sta on sta.studentID = aa.userId and aa.userType = 'STUDENT' | |
) as attendees WHERE userId IS NOT NULL"; | |
$selectQuery = "SELECT attendees.*,a.name as activityName,a.code as activityCode FROM | |
(SELECT aa.activityId | |
,aa.userId as userId | |
,sfa.staffName as userName | |
,aa.userType as userType | |
FROM activity_attendee aa | |
INNER JOIN staffaccounts sfa on sfa.staffID = aa.userId and aa.userType = 'STAFF' | |
UNION | |
SELECT aa.activityId | |
,aa.userId as userId | |
,sta.studentName as userName | |
,aa.userType as userType | |
FROM activity_attendee aa | |
INNER JOIN studentaccount sta on sta.studentID = aa.userId and aa.userType = 'STUDENT' | |
) as attendees | |
INNER JOIN activity a on a.id = attendees.activityId | |
WHERE userId IS NOT NULL "; | |
if ($searchAttendeesRequest != NULL) { | |
// activityId | |
if (! empty($searchAttendeesRequest->activityId)) { | |
$criteria .= " AND activityId = '$searchAttendeesRequest->activityId'"; | |
} | |
// Attendee Name | |
if (! empty($searchAttendeesRequest->userName)) { | |
$criteria .= " AND userName like '%$searchAttendeesRequest->userName%'"; | |
} | |
// attendee Type | |
if (! empty($searchAttendeesRequest->userType)) { | |
$criteria .= " AND userType = '$searchAttendeesRequest->userType'"; | |
} | |
$selectCountQuery .= $criteria; | |
// Add Sorting | |
if (! empty($searchAttendeesRequest->sortBy)) { | |
$criteria .= " ORDER BY $searchAttendeesRequest->sortBy $searchAttendeesRequest->sortOrder"; | |
} | |
// if($searchAttendeesRequest->endIndex !=0){ | |
// $criteria.=" LIMIT $searchAttendeesRequest->startIndex,$searchAttendeesRequest->endIndex"; | |
// } | |
// Add Pagination | |
$criteria .= " LIMIT $searchAttendeesRequest->startIndex,$searchAttendeesRequest->endIndex"; | |
$selectQuery .= $criteria; | |
} | |
try { | |
$response->totalRecords = $this->executeQueryForObject($selectCountQuery)->totalRecords; | |
$response->attendees = $this->executeQueryForList($selectQuery); | |
} catch (\Exception $e) { | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
return $response; | |
} | |
// delete attendees | |
function deleteAttendee($uid, $aid) | |
{ | |
$sql = "DELETE FROM activity_attendee where userId='$uid' AND activityId='$aid'"; | |
try { | |
$this->executeQuery($sql); | |
} catch (\Exception $e) { | |
throw new ProfessionalException ($e->getCode(), $e->getMessage()); | |
} | |
} | |
} | |
?> |