<commit_before><?php

class Application_Model_Schedule
{
    /**
     * Return TRUE if file is going to be played in the future.
     *
     * @param string $p_fileId
     */
    public function IsFileScheduledInTheFuture($p_fileId)
    {
        global $CC_CONFIG;
        $con = Propel::getConnection();
        $sql = "SELECT COUNT(*) FROM ".$CC_CONFIG["scheduleTable"]
        ." WHERE file_id = {$p_fileId} AND ends > NOW() AT TIME ZONE 'UTC'";
        $count = $con->query($sql)->fetchColumn(0);
        if (is_numeric($count) && ($count != '0')) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * Returns data related to the scheduled items.
     *
     * @param  int  $p_prev
     * @param  int  $p_next
     * @return date
     */
    public static function GetPlayOrderRange($p_prev = 1, $p_next = 1)
    {
        if (!is_int($p_prev) || !is_int($p_next)) {
            //must enter integers to specify ranges
            Logging::log("Invalid range parameters: $p_prev or $p_next");
            return array();
        }

        $date = new Application_Common_DateHelper;
        $timeNow = $date->getTimestamp();
        $utcTimeNow = $date->getUtcTimestamp();

        $shows = Application_Model_Show::getPrevCurrentNext($utcTimeNow);
        $previousShowID = count($shows['previousShow'])>0?$shows['previousShow'][0]['instance_id']:null;
        $currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['instance_id']:null;
        $nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null;
        $results = Application_Model_Schedule::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcTimeNow);

        $range = array("env"=>APPLICATION_ENV,
            "schedulerTime"=>$timeNow,
            "previous"=>$results['previous'] !=null?$results['previous']:(count($shows['previousShow'])>0?$shows['previousShow'][0]:null),
            "current"=>$results['current'] !=null?$results['current']:((count($shows['currentShow'])>0 && $shows['currentShow'][0]['record'] == 1)?$shows['currentShow'][0]:null),
            "next"=> $results['next'] !=null?$results['next']:(count($shows['nextShow'])>0?$shows['nextShow'][0]:null),
            "currentShow"=>$shows['currentShow'],
            "nextShow"=>$shows['nextShow'],
            "timezone"=> date("T"),
            "timezoneOffset"=> date("Z"));

        return $range;
    }

    /**
     * Queries the database for the set of schedules one hour before and after the given time.
     * If a show starts and ends within that time that is considered the current show. Then the
     * scheduled item before it is the previous show, and the scheduled item after it is the next
     * show. This way the dashboard getCurrentPlaylist is very fast. But if any one of the three
     * show types are not found through this mechanism a call is made to the old way of querying
     * the database to find the track info.
    **/
    public static function GetPrevCurrentNext($p_previousShowID, $p_currentShowID, $p_nextShowID, $p_timeNow)
    {
        if ($p_previousShowID == null && $p_currentShowID == null && $p_nextShowID == null)

            return;

        global $CC_CONFIG;
        $con = Propel::getConnection();
        $sql = "SELECT %%columns%% st.starts as starts, st.ends as ends, st.media_item_played as media_item_played, si.ends as show_ends
 FROM cc_schedule st JOIN %%tables%% LEFT JOIN cc_show_instances si ON st.instance_id = si.id
 WHERE ";
                
        $fileColumns = "ft.artist_name, ft.track_title, ";
        $streamColumns = "ws.login as artist_name, ws.name as track_title, ";
        
        $fileJoin = "cc_files ft ON st.file_id = ft.id";
        $streamJoin = "cc_webstream ws ON st.stream_id = ws.id";
        
        $predicateArr = array(); 
        if (isset($p_previousShowID)) {
        	$predicateArr[] = 'st.instance_id = '.$p_previousShowID;
        } 
        if (isset($p_currentShowID)) {
        	$predicateArr[] = 'st.instance_id = '.$p_currentShowID;
        } 
        if (isset($p_nextShowID)) {
        	$predicateArr[] = 'st.instance_id = '.$p_nextShowID;
        }
        
        $sql .= " (".implode(" OR ", $predicateArr).") ";
        $sql .= ' AND st.playout_status > 0 ORDER BY st.starts';
        
        $filesSql = str_replace("%%columns%%", $fileColumns, $sql);<commit_msg>CC-1665: Scheduled stream rebroadcasting and recording<commit_after><?php

class Application_Model_Schedule
{
    /**
     * Return TRUE if file is going to be played in the future.
     *
     * @param string $p_fileId
     */
    public function IsFileScheduledInTheFuture($p_fileId)
    {
        global $CC_CONFIG;
        $con = Propel::getConnection();
        $sql = "SELECT COUNT(*) FROM ".$CC_CONFIG["scheduleTable"]
        ." WHERE file_id = {$p_fileId} AND ends > NOW() AT TIME ZONE 'UTC'";
        $count = $con->query($sql)->fetchColumn(0);
        if (is_numeric($count) && ($count != '0')) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * Returns data related to the scheduled items.
     *
     * @param  int  $p_prev
     * @param  int  $p_next
     * @return date
     */
    public static function GetPlayOrderRange($p_prev = 1, $p_next = 1)
    {
        if (!is_int($p_prev) || !is_int($p_next)) {
            //must enter integers to specify ranges
            Logging::log("Invalid range parameters: $p_prev or $p_next");
            return array();
        }

        $date = new Application_Common_DateHelper;
        $timeNow = $date->getTimestamp();
        $utcTimeNow = $date->getUtcTimestamp();

        $shows = Application_Model_Show::getPrevCurrentNext($utcTimeNow);
        $previousShowID = count($shows['previousShow'])>0?$shows['previousShow'][0]['instance_id']:null;
        $currentShowID = count($shows['currentShow'])>0?$shows['currentShow'][0]['instance_id']:null;
        $nextShowID = count($shows['nextShow'])>0?$shows['nextShow'][0]['instance_id']:null;
        $results = Application_Model_Schedule::GetPrevCurrentNext($previousShowID, $currentShowID, $nextShowID, $utcTimeNow);

        $range = array("env"=>APPLICATION_ENV,
            "schedulerTime"=>$timeNow,
            "previous"=>$results['previous'] !=null?$results['previous']:(count($shows['previousShow'])>0?$shows['previousShow'][0]:null),
            "current"=>$results['current'] !=null?$results['current']:((count($shows['currentShow'])>0 && $shows['currentShow'][0]['record'] == 1)?$shows['currentShow'][0]:null),
            "next"=> $results['next'] !=null?$results['next']:(count($shows['nextShow'])>0?$shows['nextShow'][0]:null),
            "currentShow"=>$shows['currentShow'],
            "nextShow"=>$shows['nextShow'],
            "timezone"=> date("T"),
            "timezoneOffset"=> date("Z"));

        return $range;
    }

    /**
     * Queries the database for the set of schedules one hour before and after the given time.
     * If a show starts and ends within that time that is considered the current show. Then the
     * scheduled item before it is the previous show, and the scheduled item after it is the next
     * show. This way the dashboard getCurrentPlaylist is very fast. But if any one of the three
     * show types are not found through this mechanism a call is made to the old way of querying
     * the database to find the track info.
    **/
    public static function GetPrevCurrentNext($p_previousShowID, $p_currentShowID, $p_nextShowID, $p_timeNow)
    {
        if ($p_previousShowID == null && $p_currentShowID == null && $p_nextShowID == null)

            return;

        global $CC_CONFIG;
        $con = Propel::getConnection();
        $sql = "SELECT %%columns%% st.starts as starts, st.ends as ends, st.media_item_played as media_item_played, si.ends as show_ends
 FROM cc_schedule st JOIN %%tables%% LEFT JOIN cc_show_instances si ON st.instance_id = si.id
 WHERE ";
                
        $fileColumns = "ft.artist_name, ft.track_title, ";
        $streamColumns = "ws.creator_id as artist_name, ws.name as track_title, ";
        
        $fileJoin = "cc_files ft ON st.file_id = ft.id";
        $streamJoin = "cc_webstream ws ON st.stream_id = ws.id";
        
        $predicateArr = array(); 
        if (isset($p_previousShowID)) {
        	$predicateArr[] = 'st.instance_id = '.$p_previousShowID;
        } 
        if (isset($p_currentShowID)) {
        	$predicateArr[] = 'st.instance_id = '.$p_currentShowID;
        } 
        if (isset($p_nextShowID)) {
        	$predicateArr[] = 'st.instance_id = '.$p_nextShowID;
        }
        
        $sql .= " (".implode(" OR ", $predicateArr).") ";
        $sql .= ' AND st.playout_status > 0 ORDER BY st.starts';
        
        $filesSql = str_replace("%%columns%%", $fileColumns, $sql);
{'fraction_non_alphanumeric': 0.14725719424460432, 'fraction_numerical': 0.004721223021582734, 'mean_word_length': 2.8299612569952646, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4218482', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2735, 'n_words': 648}