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 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 131
IntimationService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
812.00
0.00% covered (danger)
0.00%
0 / 131
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 sendSmsIntimationBulk
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 9
 sendIntimationRecursive
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 25
 getStudentBasicDetails
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
 sendIntimation
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 sendBulkIntimationRecursive
0.00% covered (danger)
0.00%
0 / 1
72.00
0.00% covered (danger)
0.00%
0 / 41
 sendIntimationForStaffInDigitalValuation
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 8
 sendBulkIntimationForStaffRecursive
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 26
<?php
namespace com\linways\ec\core\service;
use com\linways\base\util\StringUtil;
use com\linways\base\util\MakeSingletonTrait;
use com\linways\ec\core\dto\IntimationData;
use com\linways\core\ams\professional\logging\AMSLogger;
use com\linways\ec\core\logging\Events;
use com\linways\ec\core\logging\entities\Staff;
use com\linways\ec\core\constant\StatusConstants;
use com\linways\ec\core\util\ExamControllerIntimationUtil;
use com\linways\ec\core\dto\IntimationDataProperties;
use com\linways\core\ams\professional\service\CommonService;
use com\linways\core\ams\professional\constant\Modules;
use com\linways\core\ams\professional\util\CommonUtil;
use PDO;
class IntimationService extends BaseService
{
    use MakeSingletonTrait;
    /**
     * @var \Monolog\Logger
     */
    private $logger = null;
    public function __construct()
    {
        $this->logger = AMSLogger::getLogger('exam-controller-log');
    }
    /**
     * Send SMS intimation for bulk students
     *
     * @param IntimationData $intimationData
     * @return void
     */
    private function sendSmsIntimationBulk(IntimationData $intimationData)
    {
        /**
         * Send SMS to array of recipients
         */
        $recipientMobileNo = array_filter(array_column($intimationData->recipientList, 'mobileNo'));
        try {
            if (count($recipientMobileNo) > 0 && in_array("SMS", $intimationData->intimationProperties->intimationTypes)) {
                $this->sendIntimationRecursive($recipientMobileNo, $intimationData);
            }
        } catch (\Exception $e) {
        }
    }
    /**
     * Recursive function to send SMS intimation
     *
     * @param Array $recipientMobileNo
     * @param IntimationData $intimationData
     * @return void
     */
    private function sendIntimationRecursive(array $recipientMobileNo, IntimationData $intimationData)
    {
        try {
            $phoneNumberList = array_slice($recipientMobileNo, 0, 10);
            AMSLogger::log_info($this->logger,Events::EC_SEND_SMS_INTIMATION_BULK, [
                "phoneNumberList" => $phoneNumberList,
                "IntimationData" => $intimationData,
                "markedStaff" => new Staff(["id" => $GLOBALS['userId']]),
                "status" => StatusConstants::SUCCESS
            ]);
            ExamControllerIntimationUtil::sendSmsAsync($phoneNumberList, $intimationData->intimationProperties->smsContent, $intimationData->intimationProperties->dltId);
            $recipientMobileNo = array_slice($recipientMobileNo, 10);
            if (!empty($recipientMobileNo)) {
                $this->sendIntimationRecursive($recipientMobileNo, $intimationData);
            }
        } catch (\Exception $e) {
            AMSLogger::log_error($this->logger,Events::EC_SEND_SMS_INTIMATION_BULK, [
                "phoneNumberList" => $phoneNumberList,
                "IntimationData" => $intimationData,
                "markedStaff" => new Staff(["id" => $GLOBALS['userId']]),
                "errorCode" => $e->getCode(),
                "errorMessage" => $e->getMessage(),
                "status" => StatusConstants::FAILURE
            ]);
        }
    }
    /**
     * Fetch student basic details for SEND intimation
     *
     * @param [type] $studentIdList
     * @return Array
     */
    private function getStudentBasicDetails($studentIdList)
    {
        $sql = "SELECT studentId, studentName as name, studentEmail as emailId, studentPhone as mobileNo from studentaccount sa inner join student_program_account spa on sa.studentID = spa.student_id  where spa.id in (" . implode(" , ", $studentIdList) . ")";
        try {
            return $this->executeQueryForList($sql);
        } catch (\Exception $e) {
            // throw new AcademicException($e->getCode(), $e->getMessage());
        }
    }
    public function sendIntimation($intimationData)
    {
        try {
            // TODO : we may need to fetch student details recursively
            if( !$intimationData->intimationData->hasStudentDetails){
                $studentDataList = $this->getStudentBasicDetails($intimationData->studentIdList);
            }
            else{
                $studentDataList = $intimationData->studentList;
            }
            $this->sendBulkIntimationRecursive($intimationData, $studentDataList);
        } catch (\Exception $e) {
            // throw new AcademicException($e->getCode(), $e->getMessage());
        }
    }
    private function sendBulkIntimationRecursive($intimationData, $allStudentList)
    {
        try {
            $studentDataList = array_slice($allStudentList, 0, 10);
            $req = new IntimationData();
            $req->recipientList = $studentDataList;
            $req->intimationProperties = new IntimationDataProperties();
            $req->intimationProperties->intimationTypes = $intimationData->intimationData->intimationTypes;
            $req->intimationProperties->smsContent = $intimationData->intimationData->smsContent;
            $req->intimationProperties->dltId = $intimationData->intimationData->dltId;
            $smsContent = $intimationData->intimationData->smsContent;
           
            if (!str_contains($smsContent, "{{")) {
                // string contains does not contain any tag replaceable data
                $this->sendSmsIntimationBulk($req);
            }
            if (str_contains($smsContent, "{{")) {
                foreach ($studentDataList as $studentDetails) {
                    $req->recipientList = [$studentDetails];
                    if (str_contains($smsContent, "{{")) {
                        $smsMessage = StringUtil::replace_tags($smsContent, array(
                            'studentName' => $studentDetails->name,
                            'academicOrderNo' => strtolower(CommonUtil::ordinalNumberInWords( $studentDetails->termOrderNo )),
                            'examDate' => date("d-m-Y", strtotime($studentDetails->examDate)),
                            'registrationStartDate' => date("d-m-Y", strtotime($studentDetails->registrationStartDate)),
                            'collegeName' =>  $intimationData->intimationData->collegeName,
                            'autonomous' =>  $intimationData->intimationData->autonomous,
                            'place' =>  $intimationData->intimationData->place
                        ));
                        $req->intimationProperties->smsContent = $smsMessage;
                        $this->sendSmsIntimationBulk($req);
                    }
                }
            }
        } catch (\Exception $e) {
        }
        try {
            $allStudentList = array_slice($allStudentList, 10);
            if (!empty($allStudentList)) {
                $this->sendBulkIntimationRecursive($intimationData, $allStudentList);
            }
        } catch (\Exception $e) {
        }
    }
    public function sendIntimationForStaffInDigitalValuation($intimationData){
        try {
            $staffDataList = $intimationData->staffList;
            if(!empty($staffDataList)){
                $this->sendBulkIntimationForStaffRecursive($intimationData, $staffDataList);
            }
        } catch (\Exception $e) {
            // throw new AcademicException($e->getCode(), $e->getMessage());
        }
    }
    private function sendBulkIntimationForStaffRecursive($intimationData, $staffDataList){
        try {
            $req = new IntimationData();
            $req->recipientList = $staffDataList;
            $req->intimationProperties = new IntimationDataProperties();
            $req->intimationProperties->intimationTypes = $intimationData->intimationData->intimationTypes;
            $req->intimationProperties->smsContent = $intimationData->intimationData->smsContent;
            $req->intimationProperties->dltId = $intimationData->intimationData->dltId;
            $smsContent = $intimationData->intimationData->smsContent;
            foreach ($staffDataList as $staffDetails) {
                $smsTags = [
                    'scriptCount' => $staffDetails->scriptCount,
                    'subjectDesc' => $staffDetails->subjectDesc,
                    'subjectCode' => $staffDetails->subjectCode,
                    'valuationStartDate' => $staffDetails->valuationStartDate
                ];
                $finalTags = [];
                foreach ($intimationData->intimationData->tags as $tag) {
                    $tag = str_replace(['{{', '}}'], '', $tag->tag);
                    $finalTags[$tag] = $smsTags[$tag];
                }
                $req->intimationProperties->smsContent = StringUtil::replace_tags($smsContent, $finalTags);
                $this->sendSmsIntimationBulk($req);
            }
        } catch (\Exception $e) {
        }
    }
}