Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 308
NotificationHelperService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
3660.00
0.00% covered (danger)
0.00%
0 / 308
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 1
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 processStaffList
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 16
 sendSmsForTodaysUnmarkedFaculty
0.00% covered (danger)
0.00%
0 / 1
90.00
0.00% covered (danger)
0.00%
0 / 56
 sendNotificationWhenMessageSentOnMailBox
0.00% covered (danger)
0.00%
0 / 1
1806.00
0.00% covered (danger)
0.00%
0 / 229
<?php
namespace com\linways\core\ams\professional\service\notification;
use com\linways\core\ams\professional\constant\UserType;
use com\linways\core\ams\professional\constant\notification\NotificationContextConstant;
use com\linways\core\ams\professional\constant\notification\NotificationFeatureConstant;
use com\linways\core\ams\professional\dto\RecipeintType;
use com\linways\core\ams\professional\dto\notification\Notification;
use com\linways\core\ams\professional\dto\notification\NotificationRecipient;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\request\SearchUnMarkedStaffRequest;
use com\linways\core\ams\professional\request\notification\GetGlobalSettingsRequest;
use com\linways\core\ams\professional\service\BaseService;
use com\linways\core\ams\professional\service\StaffService;
use com\linways\core\ams\professional\service\StudentService;
/**
* @author gadheyan
*/
class NotificationHelperService 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;
    }
    private function processStaffList($staffList){
        
        foreach ($staffList as $staff) {
            $staff->hours = [];
            foreach ($staff->subjectDetails as $subjectDetails) {
                $staff->subjectId = $subjectDetails->subjectId;
                $staff->batchId = $subjectDetails->batchId;
                $staff->semId = $subjectDetails->semId;
                foreach ($subjectDetails->hours as $subjectHours) {
                    foreach ($subjectHours as $hour) {
                         $staff->hours[] = $hour;
                    }
                }
            }
            $staff->hours = array_unique($staff->hours);
        }
        return $staffList;
    }
    /**
     * 
     * @return NULL
     * @author gadheyan
     */
    public function sendSmsForTodaysUnmarkedFaculty(){
        $globalSettingsRequest = new GetGlobalSettingsRequest();
        $globalSettingsRequest->context = NotificationContextConstant::ATTENDANCE;
        $globalSettingsRequest->feature = NotificationFeatureConstant::SMS_FACULTIES_WHEN_ATTENDANCE_NOT_MARKED;
        $settings = NotificationSettingsService::getInstance()->getSingleGlobalSettings($globalSettingsRequest);
        $searchRequest = new SearchUnMarkedStaffRequest();
        $searchRequest->fromDate = date('Y-m-d');
        $searchRequest->toDate = date('Y-m-d');
        $searchRequest->startIndex = 0;
        $searchRequest->export = 1;
        $staffList = StaffService::getInstance()->searchUnMarkedFaculties($searchRequest);
        if(empty($staffList)){
            error_log("unmarked faculty sms not sent. staff list is empty : ".getenv("AMS_CONFIG"));
            return null;
        }
        if(empty($settings->customSettingsValue)){
        error_log("unmarked faculty sms not sent. parameters like time and hours is required : ".getenv("AMS_CONFIG"));
            return null;
        }
        $hours = $settings->customSettingsValue->hours;
        if(!$hours){
            error_log("unmarked faculty sms not sent. hours is not defined : ".getenv("AMS_CONFIG"));
            return null;
        }
        $staffList = $this->processStaffList($staffList);
        $notification = new Notification();
        $notification->context = NotificationContextConstant::ATTENDANCE;
        $notification->feature = NotificationFeatureConstant::SMS_FACULTIES_WHEN_ATTENDANCE_NOT_MARKED;
        $notification->recipientType =  UserType::STAFF;
        $notification->smsDeferred = 0;
        $notification->createdBy = 0;
        $notification->creatorType = UserType::STAFF;
        
        foreach ($staffList as $staff) {
            $unmarkedHours = [];
            foreach ($hours as $hour) {
                if(in_array($hour, $staff->hours)){
                    $unmarkedHours[] = $hour;
                }
            }
            if(empty($unmarkedHours)){
                continue;
            }
            $unmarkedHours = implode(",", $unmarkedHours);
            $recipient = null;
            $recipient = new NotificationRecipient();
            $recipient->recipientId = $staff->staffId;
            $recipient->recipientType = UserType::STAFF;
            $recipient->templateParameters = ["batchId" => $staff->batchId, "subjectId" => $staff->subjectId, "semId" => $staff->semId, "hours" => $unmarkedHours];
            $notification->recipient[] = $recipient;
        }
        
    try {
        NotificationService::getInstance()->sendNotification($notification);
    } catch (\Exception $e) {
        //log exception
        $errorMsg = "Error Code : '".$e->getCode()."' . Error Message : ".$e->getMessage();
        error_log($errorMsg);
    }
    
    }
    
    
    
    
/**
 * 
 * @param unknown $messages
 * @throws ProfessionalException
 * @author gadheyan
 */
    public function sendNotificationWhenMessageSentOnMailBox($messages){
        $studentList = [];
        $staffList = [];
        
        switch ($messages->recType)
        {
            case RecipeintType::ALL:
                $sqlStudents = "SELECT studentID as userId FROM studentaccount WHERE student_lock=0 ";
                $sqlStaff = "SELECT staffID as userId FROM staffaccounts WHERE isResigned=0 AND staffID !=$messages->senderId ";
                if($messages->deptId)
                {
                    $sqlStudents .=" AND deptID=$messages->deptId";
                    $sqlStaff .=" AND deptID=$messages->deptId";
                }
                try
                {
                    $studentList = $this->executeQueryForList($sqlStudents);
                    $staffList = $this->executeQueryForList($sqlStaff);
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_BATCH:
                $sqlStudents="SELECT studentID as userId FROM studentaccount WHERE student_lock=0 AND batchID=$messages->batchId AND deptID=$messages->deptId";
                $sqlStaff ="SELECT st.staffID as userId FROM staffaccounts st INNER JOIN sbs_relation sr ON st.staffID = sr.staffID INNER JOIN batches b ON b.batchID = sr.batchID AND b.semID = sr.semID  WHERE st.isResigned=0 AND st.deptID=$messages->deptId AND sr.batchID = $messages->batchId AND st.staffID !=$messages->senderId";
                try
                {
                    $studentList = $this->executeQueryForList($sqlStudents);
                    $staffList = $this->executeQueryForList($sqlStaff);
                    
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_SUBBATCH:
                $sqlStudents="SELECT sa.studentID as userId FROM studentaccount sa INNER JOIN subbatches sub ON sub.batchID =sa.batchID INNER JOIN subbatch_student ss ON sub.subbatchID=ss.subbatchID AND sa.studentID =ss.studentID WHERE sub.subbatchID=$messages->batchId";
                $sqlStaff ="SELECT st.staffID as userId FROM staffaccounts st INNER JOIN sbs_relation sr ON st.staffID = sr.staffID INNER JOIN subbatch_sbs ss ON ss.sbsID=sr.sbsID WHERE st.isResigned=0 AND st.deptID=$messages->deptId AND ss.subbatchID=$messages->batchId AND st.staffID !=$messages->senderId";
                try
                {
                    $studentList = $this->executeQueryForList($sqlStudents);
                    $staffList = $this->executeQueryForList($sqlStaff);
                    
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_FACULTY:
                $sqlStaff ="SELECT staffID as userId FROM staffaccounts WHERE isResigned=0 AND staffID !=$messages->senderId";
                try
                {
                    $staffList = $this->executeQueryForList($sqlStaff);
                    
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_DEPT_FACULTY:
                $sqlStaff ="SELECT staffID as userId FROM staffaccounts WHERE isResigned=0 AND deptID=$messages->deptId AND staffID !=$messages->senderId";
                try
                {
                    $staffList = $this->executeQueryForList($sqlStaff);
                    
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_BATCH_FACULTY:
                $sqlStaff ="SELECT st.staffID as userId FROM staffaccounts st INNER JOIN sbs_relation sr ON st.staffID = sr.staffID INNER JOIN batches b ON b.batchID = sr.batchID AND b.semID = sr.semID  WHERE st.isResigned=0 AND st.deptID=$messages->deptId AND sr.batchID = $messages->batchId AND st.staffID !=$messages->senderId";
                try
                {
                    $staffList = $this->executeQueryForList($sqlStaff);
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_SUBBATCH_FACULTY:
                $sqlStaff ="SELECT st.staffID as userId FROM staffaccounts st INNER JOIN sbs_relation sr ON st.staffID = sr.staffID INNER JOIN subbatch_sbs ss ON ss.sbsID=sr.sbsID WHERE st.isResigned=0 AND st.deptID=$messages->deptId AND ss.subbatchID=$messages->batchId AND st.staffID !=$messages->senderId";
                try
                {
                    $staffList = $this->executeQueryForList($sqlStaff);
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_STUDENTS:
                $sqlStudents = "SELECT studentID as userId FROM studentaccount WHERE student_lock=0";
                try
                {
                    $studentList = $this->executeQueryForList($sqlStudents);
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_BATCH_STUDENT:
                $sqlStudents = "SELECT studentID as userId FROM studentaccount WHERE student_lock=0 AND batchID=$messages->batchId AND deptID=$messages->deptId";
                try
                {
                    $studentList = $this->executeQueryForList($sqlStudents);
                    
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_DEPT_STUDENT:
                $sqlStudents = "SELECT studentID as userId FROM studentaccount WHERE student_lock=0 AND deptID=$messages->deptId";
                try
                {
                    $studentList = $this->executeQueryForList($sqlStudents);
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::ALL_SUBBATCH_STUDENT:
                $sqlStudents = "SELECT sa.studentID as userId FROM studentaccount sa INNER JOIN subbatches sub ON sub.batchID =sa.batchID INNER JOIN subbatch_student ss ON sub.subbatchID=ss.subbatchID AND sa.studentID =ss.studentID WHERE sub.subbatchID=$messages->batchId";
                try
                {
                    $studentList = $this->executeQueryForList($sqlStudents);
                    
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::INDIVIDUAL:
                if($messages->recipeint !=NULL && count($messages->recipeint) > 0)
                {
                    foreach ($messages->recipeint as $user)
                    {
                        if($user->userType == "FACULTY"){
                            $staffObject = new \stdClass();
                            $staffObject->userId = $user->userId;
                            $staffList[] = $staffObject;
                        }elseif ($user->userType == "STUDENT"){
                            $studentObject = new \stdClass();
                            $studentObject->userId = $user->userId;
                            $studentList[] = $studentObject;
                        }
                    }
                }
                break;
            case RecipeintType::HOD:
                $sqlStaff ="SELECT staffID as userId FROM staffaccounts WHERE isResigned=0 AND deptID = $messages->deptId AND isHOD >0";
                try
                {
                    $staffList = $this->executeQueryForList($sqlStaff);
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
                break;
            case RecipeintType::PRINCIPAL:
                $sqlStaff ="SELECT staffID as userId FROM staffaccounts WHERE isResigned=0 AND isPrincipal >0";
                try
                {
                    $staffList = $this->executeQueryForList($sqlStaff);
                }
                catch (\Exception $e)
                {
                    throw new ProfessionalException($e->getCode(), $e->getMessage());
                }
        }
        $urlUserType = "";
        if($messages->senderType == "STUDENT"){
            $senderDetails = StudentService::getInstance()->getStudentDetailsByIdForApi($messages->senderId);
            }else {
            $senderDetails = StaffService::getInstance()->getStaffDetailsByIdForApi($messages->senderId);
        }
        $senderDetails->departmentName = $senderDetails->department->description?$senderDetails->department->description:$senderDetails->department->name;
        
        
        if(!empty($staffList)){
            $notification = null;
            $notification = new Notification();
            $notification->context = NotificationContextConstant::MAILBOX;
            $notification->feature = NotificationFeatureConstant::MESSAGE_SENT_THROUGH_MAILBOX;
            $notification->recipientType =  UserType::STAFF;
//             $notification->smsDeferred = 0;
            $notification->createdBy = 0;
            $notification->creatorType = UserType::STAFF;
            foreach ($staffList as $staff){
                $recipient = null;
                $recipient = new NotificationRecipient();
                $recipient->recipientId = $staff->userId;
                $recipient->recipientType = UserType::STAFF;
                $recipient->templateParameters = ["userType" => "staff", "userName" => $senderDetails->name, "departmentName" => $senderDetails->departmentName];
                $notification->recipient[] = $recipient;
            }
            try {
                NotificationService::getInstance()->sendNotification($notification);
            } catch (\Exception $e) {
                //log exception
                $errorMsg = "Error Code : '".$e->getCode()."' . Error Message : ".$e->getMessage();
                error_log($errorMsg);
            }
            
        }
        if(!empty($studentList)){
            $notification = null;
            $notification = new Notification();
            $notification->context = NotificationContextConstant::MAILBOX;
            $notification->feature = NotificationFeatureConstant::MESSAGE_SENT_THROUGH_MAILBOX;
            $notification->recipientType =  UserType::STUDENT;
//             $notification->smsDeferred = 0;
            $notification->createdBy = 0;
            $notification->creatorType = UserType::STAFF;
            foreach ($studentList as $student){
                $recipient = null;
                $recipient = new NotificationRecipient();
                $recipient->recipientId = $student->userId;
                $recipient->recipientType = UserType::STUDENT;
                $recipient->templateParameters = ["userType" => "student", "userName" => $senderDetails->name, "departmentName" => $senderDetails->departmentName];
                $notification->recipient[] = $recipient;
                
            }
            try {
                NotificationService::getInstance()->sendNotification($notification);
            } catch (\Exception $e) {
                //log exception
                $errorMsg = "Error Code : '".$e->getCode()."' . Error Message : ".$e->getMessage();
                error_log($errorMsg);
            }
            
        }
        
        
        
    }
   
    
    
    
}