<commit_before><?php
set_include_path( '..' );

include_once 'header.php';
include_once 'methods.php';
include_once 'database.php';
require_once 'vendor/autoload.php';


/**
 * NCBS google calendar.
 */
class NCBSCalendar
{
    public $client = null;

    public $redirectURL = null;

    public $oauthFile = null;

    public $service = null;

    public $calID = null;

    public $timeZone = 'Asia/Kolkata';

    // NOTE: This is needed to add to datetime before we send it to GOOGLE. 
    // Google automatically add the timezone offset which we send to it. 
    public $offset = null;

    /**
        * @brief Format used by google-API.
     */
    public $format = 'Y-m-d\TH:i:s';

    public function __construct( $oauth_file, $calID )
    {
        $this->calID = $calID;
        $this->offset = 0.0; // (new DateTime())->format( 'Z' );
        $this->client = new Google_Client( );
        if( file_exists($oauth_file) )
            $this->oauthFile =  $oauth_file;
        else
        {
            $ret = "
                   <h3 class='warn'>
                   Warning: You need to set the location of your OAuth2 Client Credentials from the
                   <a href='http://developers.google.com/console'>Google API console</a>.
                   </h3>
                   ";
            return None;
        }

        $this->client->setAuthConfig( $this->oauthFile );
        $this->client->setScopes( 'https://www.googleapis.com/auth/calendar');
        $this->redirectURL = $this->client->createAuthUrl();
    }

    public function service( )
    {
        if( ! $this->service )
            $this->service = new Google_Service_Calendar( $this->client );

        return $this->service;
    }

    public function setAccessToken( $token )
    {
        $token = $this->client->fetchAccessTokenWithAuthCode($token );
        try {
            $this->client->setAccessToken($token);
        } catch (InvalidArgumentException $e) {
            echo printWarning( "Token expired! You must try again ..." );
            echo goBackToPageLink( "bookmyvenue_admin.php", "Go back"  );
            exit;
        }
    }

    /**
        * @brief Return all events on this calendar.
        *
        * @return 
     */
    public function getEvents( )
    {
        //echo "Getting list of events from " . $this->calID;
        $eventGen = $this->service()->events->listEvents( $this->calID );
        $events = array();
        while( true ) {
            foreach( $eventGen->getItems() as $event ) {
                array_push( $events, $event );
            }

            $pageToken = $eventGen->getNextPageToken();
            if ($pageToken) {
                $optParams = array('pageToken' => $pageToken);
                $eventGen = $this->service->events->listEvents( $this->calID,  $optParams);
            } else {
                break;
            }
        }
        return $events;
    }

    /**
        * @brief Insert an event into public calendar.
        *
        * @param $option
        *
        * @return
     */
    public function _insertEvent( $option )
    {
        $event = new Google_Service_Calendar_Event( $option );
        try
        {
            $createEvent = $this->service( )->events->insert( $this->calID, $event );
            return $createEvent;
        }
        catch (Google_ServiceException $e)
        {
            echo printWarning( "Failed to create a new event" );
            echo printWarning( "Error was : " . $e->getMessage( ) );
            return FALSE;
        }
        catch ( InvalidArgumentException $e )
        {
            echo minionEmbarrassed( 
                "I could not update public calendar"
                , "Error was " .  $e->getMessage() 
            );
        }
        flush();
        ob_flush( );
        return null;
    }

    public function getEvent( $calendarId, $eventId )
    {
        return $this->service( )->events->get( $calendarId, $eventId );
    }

    /**
        * @brief Update event on NCBS public calendar.
        *
        * @param $event This is our event from database.
        *
        * @return  TRUE on success, FALSE otherwise.
     */
    public function updateEvent( $event )
    {
        if( trim($event['calendar_event_id']) == '' )
        {
            echo printWarning( "You tried to update an event without valid event id");
            echo printWarning( "... I am ignoring your update request" );
            return;
        }

        $gevent = $this->getEvent( $event['calendar_id' ] , $event['calendar_event_id'] );

        // Now update the summary and description of event. Changing time is not
        // allowed in any case.
        $gevent->setSummary( $event['short_description' ] );
        $gevent->setDescription( $event['description'] );
        $gevent->setHtmlLink( $event['url'] );
        
        $startTimeUTC = strtotime( 
            $event['date'] . ' ' . $event['start_time'] ) - $this->offset;
        $endTimeUTC = strtotime( 
            $event['date'] . ' ' . $event['end_time'] ) - $this->offset;

        $startDateTime = date( $this->format, $startTimeUTC );
        $endDateTime = date( $this->format, $endTimeUTC );

        $gStartDateTime = new Google_Service_Calendar_EventDateTime( );
        $gStartDateTime->setDateTime( $startDateTime );
        $gStartDateTime->setTimeZone( $this->timeZone );

        $gEndDateTime = new Google_Service_Calendar_EventDateTime( );
        $gEndDateTime->setDateTime( $endDateTime );
        $gEndDateTime->setTimeZone( $this->timeZone );

        $gevent->setStart( $gStartDateTime );
        $gevent->setEnd( $gEndDateTime );


        // I don't know why but this is neccessary. Not everything is returned
        // by GET request.
        if( $event['status'] == 'VALID' )
            $gevent->setStatus( 'confirmed' );
        else
            $gevent->setStatus( 'cancelled' );

        try
        {
            $gevent = $this->service( )->events->update( $event['calendar_id']
                , $gevent->getId( )
                , $gevent 
            );
        }
        catch ( Google_ServiceException $e )
        {
            echo printWarning(
                "This is embarassing! I could not update public calendar"
            );
            echo printWarning( "Error was : " . $e->getMessage( ) );
        }
        catch ( InvalidArgumentException $e )
        {
            echo minionEmbarrassed( 
                "I could not update public calendar"
                , "Error was " .  $e->getMessage() 
            );
        }

        echo str_repeat( ' ', 1024 * 64 );
        flush();
        return $gevent;
    }

    /**
        * @brief Insert database entry into google calendar.
        *
        * @param $event Datebase row.
        *
        * @return  new event on sucess, null otherwise
     */
    public function addNewEvent( $event )
    {
        $startTime = strtotime( $event['date'] . ' ' . $event[ 'start_time' ] );
        $startTime = $startTime - $this->offset;
        $endTime = strtotime( $event['date'] . ' ' . $event[ 'end_time' ] );
        $endTime = $endTime - $this->offset;

        $entry = array(
                     "summary" => $event['short_description']
                     , "description" => $event['description']
                     , 'location' => venueSummary( getVenueById( $event['venue' ] ) )
                     , 'start' => array(
                         "dateTime" => date( $this->format, $startTime )
                         , "timeZone" => $this->timeZone
                     )
                     , 'end' => array(
                         "dateTime" => date( $this->format, $endTime )
                         , "timeZone" => $this->timeZone
                     )
                     , "htmlLink" => $event['url']
                     , "anyoneCanAddSelf" => True
                     , "extendedProperties" => array( "shared" => array(
                         "gid" => $event['gid'], 'eid' => $event[ 'eid' ] ) )
                 );

        $gevent = $this->_insertEvent( $entry );

        if( $gevent )
        {
            $event[ 'calendar_id' ] = $this->calID;
            $event[ 'calendar_event_id'] = $gevent->getId( );

            $res = updateTable( "events"
                                , array("gid","eid")
                                , array( "calendar_event_id","calendar_id")
                                , $event );
            return $res;<commit_msg>short_description => title.<commit_after><?php
set_include_path( '..' );

include_once 'header.php';
include_once 'methods.php';
include_once 'database.php';
require_once 'vendor/autoload.php';


/**
 * NCBS google calendar.
 */
class NCBSCalendar
{
    public $client = null;

    public $redirectURL = null;

    public $oauthFile = null;

    public $service = null;

    public $calID = null;

    public $timeZone = 'Asia/Kolkata';

    // NOTE: This is needed to add to datetime before we send it to GOOGLE. 
    // Google automatically add the timezone offset which we send to it. 
    public $offset = null;

    /**
        * @brief Format used by google-API.
     */
    public $format = 'Y-m-d\TH:i:s';

    public function __construct( $oauth_file, $calID )
    {
        $this->calID = $calID;
        $this->offset = 0.0; // (new DateTime())->format( 'Z' );
        $this->client = new Google_Client( );
        if( file_exists($oauth_file) )
            $this->oauthFile =  $oauth_file;
        else
        {
            $ret = "
                   <h3 class='warn'>
                   Warning: You need to set the location of your OAuth2 Client Credentials from the
                   <a href='http://developers.google.com/console'>Google API console</a>.
                   </h3>
                   ";
            return None;
        }

        $this->client->setAuthConfig( $this->oauthFile );
        $this->client->setScopes( 'https://www.googleapis.com/auth/calendar');
        $this->redirectURL = $this->client->createAuthUrl();
    }

    public function service( )
    {
        if( ! $this->service )
            $this->service = new Google_Service_Calendar( $this->client );

        return $this->service;
    }

    public function setAccessToken( $token )
    {
        $token = $this->client->fetchAccessTokenWithAuthCode($token );
        try {
            $this->client->setAccessToken($token);
        } catch (InvalidArgumentException $e) {
            echo printWarning( "Token expired! You must try again ..." );
            echo goBackToPageLink( "bookmyvenue_admin.php", "Go back"  );
            exit;
        }
    }

    /**
        * @brief Return all events on this calendar.
        *
        * @return 
     */
    public function getEvents( )
    {
        //echo "Getting list of events from " . $this->calID;
        $eventGen = $this->service()->events->listEvents( $this->calID );
        $events = array();
        while( true ) {
            foreach( $eventGen->getItems() as $event ) {
                array_push( $events, $event );
            }

            $pageToken = $eventGen->getNextPageToken();
            if ($pageToken) {
                $optParams = array('pageToken' => $pageToken);
                $eventGen = $this->service->events->listEvents( $this->calID,  $optParams);
            } else {
                break;
            }
        }
        return $events;
    }

    /**
        * @brief Insert an event into public calendar.
        *
        * @param $option
        *
        * @return
     */
    public function _insertEvent( $option )
    {
        $event = new Google_Service_Calendar_Event( $option );
        try
        {
            $createEvent = $this->service( )->events->insert( $this->calID, $event );
            return $createEvent;
        }
        catch (Google_ServiceException $e)
        {
            echo printWarning( "Failed to create a new event" );
            echo printWarning( "Error was : " . $e->getMessage( ) );
            return FALSE;
        }
        catch ( InvalidArgumentException $e )
        {
            echo minionEmbarrassed( 
                "I could not update public calendar"
                , "Error was " .  $e->getMessage() 
            );
        }
        flush();
        ob_flush( );
        return null;
    }

    public function getEvent( $calendarId, $eventId )
    {
        return $this->service( )->events->get( $calendarId, $eventId );
    }

    /**
        * @brief Update event on NCBS public calendar.
        *
        * @param $event This is our event from database.
        *
        * @return  TRUE on success, FALSE otherwise.
     */
    public function updateEvent( $event )
    {
        if( trim($event['calendar_event_id']) == '' )
        {
            echo printWarning( "You tried to update an event without valid event id");
            echo printWarning( "... I am ignoring your update request" );
            return;
        }

        $gevent = $this->getEvent( $event['calendar_id' ] , $event['calendar_event_id'] );

        // Now update the summary and description of event. Changing time is not
        // allowed in any case.
        $gevent->setSummary( $event['title' ] );
        $gevent->setDescription( $event['description'] );
        $gevent->setHtmlLink( $event['url'] );
        
        $startTimeUTC = strtotime( 
            $event['date'] . ' ' . $event['start_time'] ) - $this->offset;
        $endTimeUTC = strtotime( 
            $event['date'] . ' ' . $event['end_time'] ) - $this->offset;

        $startDateTime = date( $this->format, $startTimeUTC );
        $endDateTime = date( $this->format, $endTimeUTC );

        $gStartDateTime = new Google_Service_Calendar_EventDateTime( );
        $gStartDateTime->setDateTime( $startDateTime );
        $gStartDateTime->setTimeZone( $this->timeZone );

        $gEndDateTime = new Google_Service_Calendar_EventDateTime( );
        $gEndDateTime->setDateTime( $endDateTime );
        $gEndDateTime->setTimeZone( $this->timeZone );

        $gevent->setStart( $gStartDateTime );
        $gevent->setEnd( $gEndDateTime );


        // I don't know why but this is neccessary. Not everything is returned
        // by GET request.
        if( $event['status'] == 'VALID' )
            $gevent->setStatus( 'confirmed' );
        else
            $gevent->setStatus( 'cancelled' );

        try
        {
            $gevent = $this->service( )->events->update( $event['calendar_id']
                , $gevent->getId( )
                , $gevent 
            );
        }
        catch ( Google_ServiceException $e )
        {
            echo printWarning(
                "This is embarassing! I could not update public calendar"
            );
            echo printWarning( "Error was : " . $e->getMessage( ) );
        }
        catch ( InvalidArgumentException $e )
        {
            echo minionEmbarrassed( 
                "I could not update public calendar"
                , "Error was " .  $e->getMessage() 
            );
        }

        echo str_repeat( ' ', 1024 * 64 );
        flush();
        return $gevent;
    }

    /**
        * @brief Insert database entry into google calendar.
        *
        * @param $event Datebase row.
        *
        * @return  new event on sucess, null otherwise
     */
    public function addNewEvent( $event )
    {
        $startTime = strtotime( $event['date'] . ' ' . $event[ 'start_time' ] );
        $startTime = $startTime - $this->offset;
        $endTime = strtotime( $event['date'] . ' ' . $event[ 'end_time' ] );
        $endTime = $endTime - $this->offset;

        $entry = array(
                     "summary" => $event['title']
                     , "description" => $event['description']
                     , 'location' => venueSummary( getVenueById( $event['venue' ] ) )
                     , 'start' => array(
                         "dateTime" => date( $this->format, $startTime )
                         , "timeZone" => $this->timeZone
                     )
                     , 'end' => array(
                         "dateTime" => date( $this->format, $endTime )
                         , "timeZone" => $this->timeZone
                     )
                     , "htmlLink" => $event['url']
                     , "anyoneCanAddSelf" => True
                     , "extendedProperties" => array( "shared" => array(
                         "gid" => $event['gid'], 'eid' => $event[ 'eid' ] ) )
                 );

        $gevent = $this->_insertEvent( $entry );

        if( $gevent )
        {
            $event[ 'calendar_id' ] = $this->calID;
            $event[ 'calendar_event_id'] = $gevent->getId( );

            $res = updateTable( "events"
                                , array("gid","eid")
                                , array( "calendar_event_id","calendar_id")
                                , $event );
            return $res;
{'fraction_non_alphanumeric': 0.13427999057270798, 'fraction_numerical': 0.001296252651425878, 'mean_word_length': 1.6241496598639455, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 192, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5416401', 'n_tokens_mistral': 4847, 'n_tokens_neox': 4645, 'n_words': 1102}