<commit_before><?php

namespace SynchWeb\Page\EM;

trait Session
{
    /**
     * Fetch a session from a dataCollectionId
     *
     * Also check the dataCollection and proposal exist and match
     *
     * @param string $proposal
     * @param string $dataCollectionId
     */
    private function sessionFromDataCollection($proposal, $dataCollectionId)
    {
        $selects = $this->sessionSelects();

        $rows = $this->db->pq(
            "SELECT $selects
            FROM DataCollection
            INNER JOIN DataCollectionGroup
                ON DataCollectionGroup.dataCollectionGroupId = DataCollection.dataCollectionGroupId
            INNER JOIN BLSession
                ON BLSession.sessionId = DataCollectionGroup.sessionId
            INNER JOIN Proposal
                ON Proposal.proposalId = BLSession.proposalId
            WHERE CONCAT(Proposal.proposalCode, Proposal.proposalNumber) = :1
            AND DataCollection.dataCollectionId = :2",
            array(
                $proposal,
                $dataCollectionId
            )
        );

        if (sizeof($rows) == 0) {
            $this->_error('Session not found');
        }

        return $rows[0];
    }

    private function sessionFetch($sessionReference)
    {
        $selects = $this->sessionSelects();
        $rows = $this->db->pq(
            "SELECT $selects
            FROM BLSession
            INNER JOIN Proposal ON Proposal.proposalId = BLSession.proposalId
            WHERE CONCAT(
                Proposal.proposalCode,
                Proposal.proposalNumber,
                '-',
                BLSession.visit_number
            ) LIKE :1",
            array($sessionReference),
            false
        );

        if (sizeof($rows) == 0) {
            $this->_error('Session not found');
        }

        return $rows[0];
    }

    ///////////////////////////////////////////////////////////////////////////

    private function sessionSelects()
    {
        return implode(', ', array(
            'BLSession.sessionId',
            'BLSession.beamLineName',
            'YEAR(BLSession.startDate) AS year',
            'CONCAT(
                Proposal.proposalCode,
                Proposal.proposalNumber,
                "-",
                BLSession.visit_number
            ) AS session',
            'CONCAT(
                Proposal.proposalCode,
                Proposal.proposalNumber,
                "-",
                BLSession.visit_number
            ) AS visit',
            'BLSession.startDate',
            'BLSession.endDate',
            'CURRENT_TIMESTAMP BETWEEN BLSession.startDate AND BLSession.endDate AS active',
        ));
    }

    /**
     * Substitute session values in file or directory path
     *
     * Session keys are converted to upper case
     * i.e. BEAMLINENAME, YEAR, and SESSION / VISIT
     * so it's not required to use "old synchweb style all-upper-case"
     * query results
     *
     * @param array $session
     * @param string $path
     *
     * @return string
     */
    private function sessionSubstituteValuesInPath($session, $path)
    {
        foreach ($session as $key => $value) {
            $path = str_replace(
                strtoupper("<%={$key}%>"),
                $value,
                $path
            );
        }

        return $path;
    }

    /**
     * Do not permit processing before session has started or after session has ended
     *
     * @param array $session
     */
    private function sessionExitIfNotActive($session)
    {
        if (!$session['active']) {
            $message = 'This visit ended at ' . date(
                'H:i:s \o\n jS F Y',
                strtotime($session['endDate'])
            ) . '.';
            error_log($message);
            $this->_error($message, 400);
        }
    }
}
<commit_msg>fixed session handling for sessionExitIfNotActive<commit_after><?php

namespace SynchWeb\Page\EM;

trait Session
{
    /**
     * Fetch a session from a dataCollectionId
     *
     * Also check the dataCollection and proposal exist and match
     *
     * @param string $proposal
     * @param string $dataCollectionId
     */
    private function sessionFromDataCollection($proposal, $dataCollectionId)
    {
        $selects = $this->sessionSelects();

        $rows = $this->db->pq(
            "SELECT $selects
            FROM DataCollection
            INNER JOIN DataCollectionGroup
                ON DataCollectionGroup.dataCollectionGroupId = DataCollection.dataCollectionGroupId
            INNER JOIN BLSession
                ON BLSession.sessionId = DataCollectionGroup.sessionId
            INNER JOIN Proposal
                ON Proposal.proposalId = BLSession.proposalId
            WHERE CONCAT(Proposal.proposalCode, Proposal.proposalNumber) = :1
            AND DataCollection.dataCollectionId = :2",
            array(
                $proposal,
                $dataCollectionId
            ),
            false
        );

        if (sizeof($rows) == 0) {
            $this->_error('Session not found');
        }

        return $rows[0];
    }

    private function sessionFetch($sessionReference)
    {
        $selects = $this->sessionSelects();
        $rows = $this->db->pq(
            "SELECT $selects
            FROM BLSession
            INNER JOIN Proposal ON Proposal.proposalId = BLSession.proposalId
            WHERE CONCAT(
                Proposal.proposalCode,
                Proposal.proposalNumber,
                '-',
                BLSession.visit_number
            ) LIKE :1",
            array($sessionReference),
            false
        );

        if (sizeof($rows) == 0) {
            $this->_error('Session not found');
        }

        return $rows[0];
    }

    ///////////////////////////////////////////////////////////////////////////

    private function sessionSelects()
    {
        return implode(', ', array(
            'BLSession.sessionId',
            'BLSession.beamLineName',
            'YEAR(BLSession.startDate) AS year',
            'CONCAT(
                Proposal.proposalCode,
                Proposal.proposalNumber,
                "-",
                BLSession.visit_number
            ) AS session',
            'CONCAT(
                Proposal.proposalCode,
                Proposal.proposalNumber,
                "-",
                BLSession.visit_number
            ) AS visit',
            'BLSession.startDate',
            'BLSession.endDate',
            'CURRENT_TIMESTAMP BETWEEN BLSession.startDate AND BLSession.endDate AS active',
        ));
    }

    /**
     * Substitute session values in file or directory path
     *
     * Session keys are converted to upper case
     * i.e. BEAMLINENAME, YEAR, and SESSION / VISIT
     * so it's not required to use "old synchweb style all-upper-case"
     * query results
     *
     * @param array $session
     * @param string $path
     *
     * @return string
     */
    private function sessionSubstituteValuesInPath($session, $path)
    {
        foreach ($session as $key => $value) {
            $path = str_replace(
                strtoupper("<%={$key}%>"),
                $value,
                $path
            );
        }

        return $path;
    }

    /**
     * Do not permit processing before session has started or after session has ended
     *
     * @param array $session
     */
    private function sessionExitIfNotActive($session)
    {
        if (!$session['active']) {
            $message = 'This visit ended at ' . date(
                'H:i:s \o\n jS F Y',
                strtotime($session['endDate'])
            ) . '.';
            error_log($message);
            $this->_error($message, 400);
        }
    }
}

{'fraction_non_alphanumeric': 0.11400231869122762, 'fraction_numerical': 0.002576323586242432, 'mean_word_length': 1.4726114649681528, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, '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': '2458808', 'n_tokens_mistral': 2086, 'n_tokens_neox': 1995, 'n_words': 471}