<commit_before><?php
/**
 * Created by PhpStorm.
 * User: kchapple
 * Date: 2/5/16
 * Time: 9:44 AM
 */

namespace LibreEHR\Core\Emr\Repositories;

use Carbon\Carbon;
use LibreEHR\Core\Contracts\DocumentRepositoryInterface;
use Illuminate\Support\Facades\DB;
use LibreEHR\Core\Contracts\AppointmentInterface;
use LibreEHR\Core\Contracts\AppointmentRepositoryInterface;
use Illuminate\Support\Facades\App;
use LibreEHR\Core\Emr\Criteria\DocumentByPid;
use LibreEHR\Core\Emr\Eloquent\AppointmentData as Appointment;
use LibreEHR\Core\Emr\Finders\Finder;
use LibreEHR\Core\Lib\Date\DateCalc;

class AppointmentRepository extends AbstractRepository implements AppointmentRepositoryInterface
{

    const REPEAT_EVERY_DAY      = 0;
    const REPEAT_EVERY_WEEK     = 1;
    const REPEAT_EVERY_MONTH    = 2;
    const REPEAT_EVERY_YEAR     = 3;
    const REPEAT_EVERY_WORK_DAY = 4;

    protected $weekend_days = [ 'Sat', 'Sun' ];

    public function model()
    {
        return '\LibreEHR\Core\Contracts\AppointmentInterface';
    }

    public function find()
    {
        return parent::find();
    }

    public function create(AppointmentInterface $appointmentInterface)
    {
        if (is_a($appointmentInterface, $this->model())) {
            $appointmentInterface->setConnection($this->connection);
            $appointmentInterface->save();
            $appointmentInterface = $this->get($appointmentInterface->pc_eid);
        }

        return $appointmentInterface;
    }

    public function update($id, $data)
    {

        $appointment = new Appointment();
        $appointment->setConnectionName($this->connection);
        $appointmentInterface = $appointment->find($id);
        foreach ($data as $k => $ln) {
            if ($k == 'status') {
                $appointmentInterface->setPcApptStatus($ln);
            }
            if ($k == 'description') {
                $appointmentInterface->setDescription($ln);
            }
            if ($k == 'start') {
                $appointmentInterface->setStartTime($ln);
            }
            if ($k == 'end') {
                $appointmentInterface->setEndTime($ln);
            }
            if ($k == 'extension') {
                $extensions = $ln[0]['extension'];
                $location = [];
                foreach ($extensions as $extension) {
                    $url = $extension['url'];
                    if ($url =="#portal-uri") {
                        $location['portalUri'] = $extension['valueString'];
                    }
                    if ($url =="#room-key") {
                        $location['roomKey'] = $extension['valueString'];
                    }
                    if ($url =="#pin") {
                        $location['pin'] = $extension['valueString'];
                    }
                    if ($url =="#provider-id") {
                        $location['providerId'] = $extension['valueString'];
                    }
                    if ($url =="#patient-id") {
                        $appointmentInterface->setPatientId($extension['valueString']);
                    }
                }
                $appointmentInterface->setLocation(json_encode($location, true));
            }
        }

        $appointmentInterface->save();
        return $appointmentInterface;
    }

    protected function checkConstraints( $slotStartTime, $slotEndTime, $constraints )
    {
        $pass = true;
        foreach ( $constraints as $constraint ) {
            $constraintTimestamp = strtotime($constraint[2]);
            $constraintOperator = $constraint[1];

            // slot must start before constraint timestamp
            if ($constraintOperator == '<=' &&
                $slotStartTime > $constraintTimestamp ) {
                $pass = false;
                break;
            } else if ($constraintOperator == '<' &&
                $slotStartTime >= $constraintTimestamp ) {
                $pass = false;
                break;
            } else if ($constraintOperator == '>=' &&
                $slotStartTime < $constraintTimestamp ) {
                $pass = false;
                break;
            } else if ($constraintOperator == '>' &&
                $slotStartTime <= $constraintTimestamp ) {
                $pass = false;
                break;
            }
        }

        return $pass;<commit_msg>fix to appointment slot fetching<commit_after><?php
/**
 * Created by PhpStorm.
 * User: kchapple
 * Date: 2/5/16
 * Time: 9:44 AM
 */

namespace LibreEHR\Core\Emr\Repositories;

use Carbon\Carbon;
use LibreEHR\Core\Contracts\DocumentRepositoryInterface;
use Illuminate\Support\Facades\DB;
use LibreEHR\Core\Contracts\AppointmentInterface;
use LibreEHR\Core\Contracts\AppointmentRepositoryInterface;
use Illuminate\Support\Facades\App;
use LibreEHR\Core\Emr\Criteria\DocumentByPid;
use LibreEHR\Core\Emr\Eloquent\AppointmentData as Appointment;
use LibreEHR\Core\Emr\Finders\Finder;
use LibreEHR\Core\Lib\Date\DateCalc;

class AppointmentRepository extends AbstractRepository implements AppointmentRepositoryInterface
{

    const REPEAT_EVERY_DAY      = 0;
    const REPEAT_EVERY_WEEK     = 1;
    const REPEAT_EVERY_MONTH    = 2;
    const REPEAT_EVERY_YEAR     = 3;
    const REPEAT_EVERY_WORK_DAY = 4;

    protected $weekend_days = [ 'Sat', 'Sun' ];

    public function model()
    {
        return '\LibreEHR\Core\Contracts\AppointmentInterface';
    }

    public function find()
    {
        return parent::find();
    }

    public function create(AppointmentInterface $appointmentInterface)
    {
        if (is_a($appointmentInterface, $this->model())) {
            $appointmentInterface->setConnection($this->connection);
            $appointmentInterface->save();
            $appointmentInterface = $this->get($appointmentInterface->pc_eid);
        }

        return $appointmentInterface;
    }

    public function update($id, $data)
    {

        $appointment = new Appointment();
        $appointment->setConnectionName($this->connection);
        $appointmentInterface = $appointment->find($id);
        foreach ($data as $k => $ln) {
            if ($k == 'status') {
                $appointmentInterface->setPcApptStatus($ln);
            }
            if ($k == 'description') {
                $appointmentInterface->setDescription($ln);
            }
            if ($k == 'start') {
                $appointmentInterface->setStartTime($ln);
            }
            if ($k == 'end') {
                $appointmentInterface->setEndTime($ln);
            }
            if ($k == 'extension') {
                $extensions = $ln[0]['extension'];
                $location = [];
                foreach ($extensions as $extension) {
                    $url = $extension['url'];
                    if ($url =="#portal-uri") {
                        $location['portalUri'] = $extension['valueString'];
                    }
                    if ($url =="#room-key") {
                        $location['roomKey'] = $extension['valueString'];
                    }
                    if ($url =="#pin") {
                        $location['pin'] = $extension['valueString'];
                    }
                    if ($url =="#provider-id") {
                        $location['providerId'] = $extension['valueString'];
                    }
                    if ($url =="#patient-id") {
                        $appointmentInterface->setPatientId($extension['valueString']);
                    }
                }
                $appointmentInterface->setLocation(json_encode($location, true));
            }
        }

        $appointmentInterface->save();
        return $appointmentInterface;
    }

    /**
     * @param $slotStartTime
     * @param $slotEndTime
     * @param $constraints
     * @return bool
     *
     * Check the start time and end time against the constraints and make sure
     * that the start time and end break any constraints
     *
     */
    protected function checkConstraints( $slotStartTime, $slotEndTime, $constraints )
    {
        $pass = true;
        foreach ( $constraints as $constraint ) {
            $constraintTimestamp = strtotime($constraint[2]);
            $constraintOperator = $constraint[1];

            // slot must start before constraint timestamp
            if ($constraintOperator == '<=' &&
                $slotStartTime > $constraintTimestamp ) {
                $pass = false;
                break;
            } else if ($constraintOperator == '<' &&
                $slotStartTime >= $constraintTimestamp ) {
                $pass = false;
                break;
            } else if ($constraintOperator == '>=' &&
                $slotEndTime < $constraintTimestamp ) {
                $pass = false;
                break;
            } else if ($constraintOperator == '>' &&
                $slotEndTime <= $constraintTimestamp ) {
                $pass = false;
                break;
            }
        }

        return $pass;
{'fraction_non_alphanumeric': 0.12230854605993341, 'fraction_numerical': 0.003329633740288568, 'mean_word_length': 1.8106674984404243, 'pattern_counts': {'":': 0, '<': 13, '<?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': '4369893', 'n_tokens_mistral': 2452, 'n_tokens_neox': 2352, 'n_words': 453}