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 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 336
CalendarService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 18
6162.00
0.00% covered (danger)
0.00%
0 / 336
 __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
 isHoliday
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 10
 isCollegeHoliday
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 10
 getHolidaysBetweenADateRange
0.00% covered (danger)
0.00%
0 / 1
90.00
0.00% covered (danger)
0.00%
0 / 69
 getEventDetails
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 9
 getHolidays
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 29
 getHolidaysBetweenDate
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 28
 copyTimetableFromCalendar
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 11
 updateCopiedTimetableFromCalendar
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 deleteCopiedTimetableFromCalendarByEventId
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getCopiedTimetableFromCalendarByEventId
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 hasCopiedHolidayTimetable
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
 getAllCollegeWideHrHolidaysBetweenADateRange
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 13
 getAllDepartmentHolidayBetweenDateRange
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 30
 getAllHolidaysWithGivenDateRange
0.00% covered (danger)
0.00%
0 / 1
342.00
0.00% covered (danger)
0.00%
0 / 59
 getAllEvents
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 19
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\dto\CopyTimetable;
use com\linways\core\ams\professional\dto\CalendarRequest;
use com\linways\core\ams\professional\util\CommonUtil;
use com\linways\core\ams\professional\constant\apiendpoints\ApiEndpoints;
class CalendarService 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;
    }
    /**
     * Return 1 if the given date is a holiday otherwise returns 0 for a batch.
     *
     * @param string $date
     * @return number
     * @throws ProfessionalException
     */
    public function isHoliday($deptID, $batchID, $date)
    {
        $unixdate = strtotime($date);
        $sql_calendar = "select * from lms_calender where ((deptID=0 and batchID=0) || (deptID=\"$deptID\" and batchID=0) || (deptID=\"$deptID\" and batchID=\"$batchID\")) and flag in (1,3) and \"" . $unixdate . "\" between timeStart and timeEnd;";
        // echo $sql_calendar."\n\n";
        try {
            
            $result_calendar = $this->executeQueryForObject($sql_calendar);
        } catch (\Exception $e) {
            
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        
        return empty($result_calendar) ? 0 : 1;
    }
    
    /**
     * This method check if the day is general holiday or not
     *
     * @param string $date
     * @return number
     */
    public function isCollegeHoliday($date)
    {
        $unixdate = strtotime($date);
        $sql_calendar = "select * from lms_calender where ((deptID=0 and batchID=0) and \"".$unixdate."\" between timeStart and timeEnd) and flag in (1,3)";
        // echo $sql_calendar."\n\n";
        try {
            
            $result_calendar = $this->executeQueryForObject($sql_calendar);
        } catch (\Exception $e) {
            
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        
        return empty($result_calendar) ? 0 : 1;
    }
    /**
     * Undocumented function
     *
     * @param [type] $timetableDetails
     * @return void
     */
    public function getHolidaysBetweenADateRange($timetableDetails,$isv4 = false){
        if(!$isv4)
        {
            $timetableDetails = $this->realEscapeObject($timetableDetails);
            $eventList = [];
            $timetableDetails->startDateInTimestamp = strtotime($timetableDetails->copyFromStartDate);
            $timetableDetails->endDateInTimestamp = strtotime($timetableDetails->copyFromEndDate);
    
            if($timetableDetails->startDateInTimestamp == $timetableDetails->endDateInTimestamp){
                $sql = "select eventID, eventTitle, eventDesc, timeStart, timeEnd from lms_calender where ((deptID=0 AND batchID=0) || (deptID=\"$timetableDetails->deptId\" AND batchID=0) || (deptID=\"$timetableDetails->deptId\" AND batchID=\"$timetableDetails->batchId\")) AND flag in (1,3) AND $timetableDetails->startDateInTimestamp = timeStart";
            }else{
                $sql = "select eventID, eventTitle, eventDesc, timeStart, timeEnd from lms_calender where ((deptID=0 AND batchID=0) || (deptID=\"$timetableDetails->deptId\" AND batchID=0) || (deptID=\"$timetableDetails->deptId\" AND batchID=\"$timetableDetails->batchId\")) AND flag in (1,3) AND $timetableDetails->startDateInTimestamp <= timeStart AND $timetableDetails->endDateInTimestamp >= timeEnd";
            }
    
            try {
                $eventList = $this->executeQueryForList($sql);
            } catch (\Exception $e) {
                throw new ProfessionalException($e->getCode(), $e->getMessage());
            }
            foreach($eventList as $event){
                $event->holidayStartDate = date("d-m-Y", $event->timeStart);
                $event->holidayEndDate = date("d-m-Y", $event->timeEnd);
    
            }
            return $eventList;
        }
        else{
            $result = [];
            $calenderRequest = new CalendarRequest();
            $calenderRequest->startDate = $timetableDetails->copyFromStartDate;
            $calenderRequest->endDate = $timetableDetails->copyFromEndDate;
            $calenderRequest->deptIds=[];
            if(!empty($request->programId)){
            $calenderRequest->programIds=[$request->programId];
            }else{
            $calenderRequest->programIds=[];
            }
            $calenderRequest->batchIds[]=$timetableDetails->batchID;
            
            // $calenderRequest->startDate = $request->fromDate;
            // $calenderRequest->endDate = $request->toDate;
            // $events = AcademicCalenderService::getInstance()->getAllEvents($calenderRequest);
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_POST, true);
            $domain = "";
            if ($_SERVER['HTTP_HOST'] != 'localhost') {
                $domain = rtrim($_SERVER['HTTP_HOST']);
            }
            if (getenv("DEBUG") !== "true") {
                $handlerUrl = "https://" . $domain . "".ApiEndpoints::GET_EVENTS."";
            } else {
                $handlerUrl = "http://localhost".ApiEndpoints::GET_EVENTS."";
            }
            // $header = $_GLOBALS['_SERVER']['REDIRECT_HTTP_AUTHORIZATION'];
            $header = "Bearer ".$GLOBALS['_COOKIE']['AUTH_SESSION'];
            $headers = array(
                'Authorization:' . $header
            );
            curl_setopt($curl, CURLOPT_URL, $handlerUrl);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            
            $postData = [];
            $postData['request'] = $calenderRequest;
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($calenderRequest));
            $curl_response = curl_exec($curl);
            $data = json_decode($curl_response)->data;
            foreach($data as $event){
                $ev = new \stdClass();
                $ev->eventID = $event->id;
                $ev->eventTitle = $event->title;
                $ev->eventDesc = $event->description;
                $ev->holidayStartDate = date("d-m-Y", strtotime($event->date." ".$event->start_time));
                $ev->holidayEndDate = date("d-m-Y", strtotime($event->date." ".$event->end_time));
                $result[] = $ev;    
            }
          
           return $result;
            
        }
    }
    
    public function getEventDetails($date)
    {
        $unixdate = strtotime($date);
        $sql_calendar = "select * from lms_calender where ((deptID=0 and batchID=0) and \"".$unixdate."\" between timeStart and timeEnd) and flag in (1,3)";
        // echo $sql_calendar."\n\n";
        try {
            
            return $this->executeQueryForObject($sql_calendar);
        } catch (\Exception $e) {
            
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        
    }
    public function getHolidays($deptID, $batchID, $date=null)
    {
        $dates=[];
        $sql ="SELECT from_unixtime(t1.timeStart,'%Y-%m-%d') as start,from_unixtime(t1.timeEnd,'%Y-%m-%d') as end FROM lms_calender t1 WHERE ((t1.deptID=$deptID AND t1.batchID = $batchID) OR (t1.deptID=$deptID AND t1.batchID = 0) OR (t1.deptID=0 AND t1.batchID = 0)) AND t1.flag IN (3,1)";
        if($date)
        {
            $sql .=" AND t1.timeStart <= unix_timestamp('".date('Y-m-d',strtotime($date))."')";
        }
        try {
            
            $holidays = $this->executeQueryForList($sql);
            foreach ($holidays as $holiday)
            {
                
                if($holiday->start==$holiday->end)
                {
                    $dates[]=$holiday->start;
                }
                else
                {
                    $period = new \DatePeriod(new \DateTime($holiday->start), new \DateInterval('P1D'), new \DateTime($holiday->end));
                    foreach ($period as $holidayDate) {
                        $dates[] = $holidayDate->format("Y-m-d");
                    }
                    $dates[]=$holiday->end;
                }
            }
        } catch (\Exception $e) {
            
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        
        return $dates;
        
    }
    
    public function getHolidaysBetweenDate($deptID, $batchID, $fromDate, $toDate , $hrHoliday = null)
    {
        $dates=[];
        $sql ="SELECT from_unixtime(t1.timeStart,'%Y-%m-%d') as start,from_unixtime(t1.timeEnd,'%Y-%m-%d') as end FROM lms_calender t1 WHERE ( t1.timeStart BETWEEN unix_timestamp('".date('Y-m-d',strtotime($fromDate))."') AND unix_timestamp('".date('Y-m-d',strtotime($toDate))."') OR t1.timeEnd BETWEEN unix_timestamp('".date('Y-m-d',strtotime($fromDate))."') AND unix_timestamp('".date('Y-m-d',strtotime($toDate))."')) AND ((t1.deptID=$deptID AND t1.batchID = $batchID) OR (t1.deptID=$deptID AND t1.batchID = 0) OR (t1.deptID=0 AND t1.batchID = 0)) AND t1.flag IN (3,1) ";
        if(!empty($hrHoliday)){
            $sql .= " AND hrholiday = $hrHoliday";
        }
        try {
            
            $holidays = $this->executeQueryForList($sql);
            foreach ($holidays as $holiday)
            {
                
                if($holiday->start==$holiday->end)
                {
                    $dates[]=$holiday->start;
                }
                else
                {
                    $period = new \DatePeriod(new \DateTime($holiday->start), new \DateInterval('P1D'), new \DateTime($holiday->end));
                    foreach ($period as $holidayDate) {
                        $dates[] = $holidayDate->format("Y-m-d");
                    }
                    $dates[]=$holiday->end;
                }
            }
        } catch (\Exception $e) {
            
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        
        return $dates;
        
    }
    
    public function copyTimetableFromCalendar($copyTimetables, $deptId, $batchId, $eventId)
    {
        $copiedTimetables = [];
        foreach ($copyTimetables as $copyTimetable) 
        {
            if(TimetableService::getInstance()->copyTimetable($copyTimetable, $deptId, $batchId))
            {
                $copiedTimetables[] = $copyTimetable;
            }
        }
        $this->updateCopiedTimetableFromCalendar($copiedTimetables, $eventId);
    }
    
    /**
     * update copied timetable
     * @param CopyTimetable[] $copiedTimetableFromCalendar
     * @return \com\linways\base\dto\MySqlResult
     */
    public function updateCopiedTimetableFromCalendar($copiedTimetables, $eventId)
    {
        //Deleting copied timetable entries if any.
        $this->deleteCopiedTimetableFromCalendarByEventId($eventId);
        foreach ($copiedTimetables as $copiedTimetable)
        {
            $sql = "insert into copied_timetable_from_holidays (lms_calenderId,    fromDate, toDate, createdBy, createdDate) values ($eventId, '$copiedTimetable->fromDate', '$copiedTimetable->toDate', $copiedTimetable->createdBy, utc_timestamp())";
            try {
                    $this->executeQuery($sql);
    
            } catch (\Exception $e) {
    
                throw new ProfessionalException($e->getCode(),$e->getMessage());
            }
        }
    }
    
    
    public function deleteCopiedTimetableFromCalendarByEventId($eventId)
    {
        try 
        {
            $sql = "DELETE FROM copied_timetable_from_holidays WHERE lms_calenderId=$eventId";
            return $this->executeQuery($sql);
        }
        catch (\Exception $e) {
        
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    public function getCopiedTimetableFromCalendarByEventId($eventId)
    {
        try
        {
            $sql = "SELECT * FROM copied_timetable_from_holidays WHERE lms_calenderId=$eventId";
            return $this->executeQueryForList($sql);
        }
        catch (\Exception $e) {
             
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    
    public function hasCopiedHolidayTimetable($eventId)
    {
        try
        {
            $sql = "SELECT id FROM copied_timetable_from_holidays WHERE lms_calenderId=$eventId";
            return $this->executeQueryForObject($sql);
        }
        catch (\Exception $e) {
             
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
    /**
     * 
     *
     * @return void
     */
    public function getAllCollegeWideHrHolidaysBetweenADateRange($fromDate, $toDate){
        $sql = "";
        $holidays = [];
        $fromDate = $this->realEscapeString($fromDate);
        $toDate = $this->realEscapeString($toDate);
        try
        {
            $holidays = $this->getHolidaysBetweenDate(0, 0, $fromDate, $toDate , 1);
        }
        catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
        return $holidays;
    }
    /**
     * Get all departmentwise holidays
     * @author Vishnu M
     */
    public function getAllDepartmentHolidayBetweenDateRange ( $fromDate, $toDate, $hrHoliday = NULL ) {
        $sql = null;
        $holidays = [];
        $dates = [];
        $fromDate = $this->realEscapeString($fromDate);
        $toDate = $this->realEscapeString($toDate);
        $fromDate = date('Y-m-d',strtotime($fromDate));
        $toDate = date('Y-m-d',strtotime($toDate));
        $sql = "SELECT from_unixtime(lms.timeStart,'%Y-%m-%d') as start, from_unixtime(lms.timeEnd,'%Y-%m-%d') as end, lms.deptID FROM lms_calender lms WHERE ( lms.timeStart BETWEEN unix_timestamp('$fromDate') AND unix_timestamp('$toDate') OR lms.timeEnd BETWEEN unix_timestamp('$fromDate') AND unix_timestamp('$toDate') ) AND lms.flag IN (3, 1) ";
        
        if ( !empty ( $hrHoliday ) ) {
            $sql .= " AND lms.hrholiday = 1 ";
        }
        try {
            $holidays = $this->executeQueryForList($sql);
            foreach ($holidays as $holiday) {
                if($holiday->start == $holiday->end) {
                    $dates[$holiday->deptID][] = $holiday->start;
                }
                else {
                    $period = new \DatePeriod(new \DateTime($holiday->start), new \DateInterval('P1D'), new \DateTime($holiday->end));
                    foreach ($period as $holidayDate) {
                        $dates[$holiday->deptID][] = $holidayDate->format("Y-m-d");
                    }
                    $dates[$holiday->deptID][] = $holiday->end;
                }
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $dates; 
    }
    public function getAllHolidaysWithGivenDateRange($dayStart,$dayEnd,$HrHoliday = true, $batchID = null)
    {
        $holidays = [];
        $cond = $HrHoliday?" and hrholiday = 1 ":"";
        $cond .=  $batchID ? "and batchID IN (0,$batchID": "";
        $sql = "SELECT from_unixtime(t1.timeStart,'%Y-%m-%d') as start,
                                            from_unixtime(t1.timeEnd,
                                            '%Y-%m-%d') as end,
                                             hrholiday,
                                            hr_holiday_session,
                                            deptID,
                                            batchID,
                                            t1.eventTitle,
                                            t1.eventDesc 
                                            FROM lms_calender t1 
                                            WHERE ( t1.timeStart BETWEEN unix_timestamp('$dayStart') AND unix_timestamp('$dayEnd') OR t1.timeEnd BETWEEN unix_timestamp('$dayStart') AND unix_timestamp('$dayEnd')) AND t1.flag IN (3,1)".$cond.";";
        try {
            $holidayList = $this->executeQueryForList($sql);
            if($holidayList)
            {
                $periods = CommonUtil::getDatePeriodIterator($dayStart, CommonUtil::addOneDay($dayEnd));
                foreach($periods as $period)
                {
                    $date = $period->format('Y-m-d');
                    foreach($holidayList as $holiday)
                    {
                        if(strtotime($date) >= strtotime($holiday->start) && strtotime($date) <= strtotime($holiday->end) || (strtotime($holiday->start) == strtotime($holiday->end) && strtotime($holiday->start) == $date))
                        {
                            if($holidays[$date]->session)
                            {
                                if('FD' == $holiday->hr_holiday_session || empty ( $holiday->hr_holiday_session ) )
                                {
                                    $holidays[$date]->session = "FD";
                                }
                                else
                                {
                                    if($holidays[$date]->session != $holiday->hr_holiday_session)
                                    {
                                        $holidays[$date]->session = 'FD';
                                    }
                                }
                            }
                            else
                            {
                                $holidays[$date]->session = $holiday->hr_holiday_session?$holiday->hr_holiday_session:"FD";
                            }
                            if($holidays[$date])
                            {
                                $holidays[$date]->title[] = $holiday->eventTitle.($holiday->eventDesc?'-'.$holiday->eventDesc:'');
                                $holidays[$date]->dept[] = $holiday->deptID;
                            }
                        }
                    }
                }
            }
        } catch (\Exception $th) {
            throw new ProfessionalException($th->getCode(), $th->getMessage());
        }
        return $holidays;
    }
    public function getAllEvents($request){
        $request = $this->realEscapeObject($request);
        $cond = "";
        if($request->fromDate && $request->toDate){
            $cond .= " AND timeStart >= '".strtotime($request->fromDate)."' AND timeEnd <= '".strtotime($request->toDate)."' ";
        }else if($request->fromDate && !$request->toDate){
            $cond .= " AND timeStart >= '".strtotime($request->fromDate)."' ";
        }
        if($request->calendarFlag){
            $cond .= " AND flag IN(".implode(',',$request->calendarFlag).") ";
        }
        $sql = "SELECT eventTitle, eventDesc, from_unixtime(timeStart,'%Y-%m-%d') as timeStart, from_unixtime(timeEnd,'%Y-%m-%d') as timeEnd, examID, assignmentID FROM lms_calender WHERE ((deptID=0 and batchID=0) || (deptID='$request->deptId' and batchID=0) || (deptID='$request->deptId' and batchID='$request->batchId')) $cond ORDER BY timeStart ASC";
        try
        {
            return $this->executeQueryForList($sql);
        }
        catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(),$e->getMessage());
        }
    }
}