<commit_before><?php

namespace PFCD\TourismBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

use PFCD\TourismBundle\Constants;

use PFCD\TourismBundle\Entity\Session;
use PFCD\TourismBundle\Form\SessionType;
use PFCD\TourismBundle\Entity\SessionGenerator;
use PFCD\TourismBundle\Form\SessionGeneratorType;
use PFCD\TourismBundle\Entity\SessionFilter;
use PFCD\TourismBundle\Form\SessionFilterType;
use PFCD\TourismBundle\Entity\Reservation;

/**
 * Session controller
 */
class SessionController extends Controller
{
    
    /**************************************************************************
     ***** BACK AREA **********************************************************
     **************************************************************************/
    
    /**
     * Lists all Session entities
     */
    public function backIndexAction()
    {
        $options['domain'] = $this->get('security.context')->isGranted('ROLE_ADMIN') ? Constants::ADMIN : Constants::BACK;
        
        if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
        {
            // parameter used to filter and show only the activies that belong to the logged organization
            $options['organization'] = $this->get('security.context')->getToken()->getUser()->getId();
        }
        
        $sessionFilter = new SessionFilter();
        
        $request = $this->getRequest();
        
        if ($request->getMethod() == 'GET')
        {
            $activityId = $request->get('activityId', null);
            
            if ($activityId)
            {
                $filter['id'] = $activityId;

                if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
                {
                    $filter['organization'] = $this->get('security.context')->getToken()->getUser()->getId();
                }

                $em = $this->getDoctrine()->getEntityManager();

                $activity = $em->getRepository('PFCDTourismBundle:Activity')->findOneBy($filter);

                if (!$activity) throw $this->createNotFoundException('Unable to find Activity entity.');
                
                $sessionFilter->setActivity($activity);
            }
        }
        
        $form = $this->createForm(new SessionFilterType(), $sessionFilter, $options);
        
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);

            if ($form->isValid())
            {
                if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
                {
                    $id = $this->get('security.context')->getToken()->getUser()->getId();
                    
                    // verify that the reserved activity belong to the logged organization
                    if ($sessionFilter->getActivity() && $sessionFilter->getActivity()->getOrganization()->getId() != $id)
                    {
                        throw new AccessDeniedException();
                    }
                }
            }
        }
        
        $em = $this->getDoctrine()->getEntityManager();
        
        if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
        {
            $organization = $this->get('security.context')->getToken()->getUser()->getId();
        }
        else
        {
            $organization = null;
        }

        $activity = $sessionFilter->getActivity();
        $dateStart = $sessionFilter->getDateStart();
        $dateEnd = $sessionFilter->getDateEnd();
        $time = $sessionFilter->getStartTime();
        $daysWeek = $sessionFilter->getDaysWeek();
        $status = $sessionFilter->getStatus();

        $sessions = $em->getRepository('PFCDTourismBundle:Session')->findAllFiltered($organization, $activity, $dateStart, $dateEnd, $time, $daysWeek, $status);

        return $this->render('PFCDTourismBundle:Back/Session:index.html.twig', array(
            'sessions' => $sessions,
            'form'     => $form->createView()
        ));
    }

    /**
     * Displays a form to create a new Session entity and store it when the form is submitted and valid
     */
    public function backCreateAction()
    {
        $options['domain'] = $this->get('security.context')->isGranted('ROLE_ADMIN') ? Constants::ADMIN : Constants::BACK;
        $options['type'] = Constants::FORM_CREATE;
        
        if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
        {
            // parameter used to filter and show only the activies that belong to the logged organization
            $options['organization'] = $this->get('security.context')->getToken()->getUser()->getId();
        }
        
        $session = new Session();
        $form = $this->createForm(new SessionType(), $session, $options);
        
        $request = $this->getRequest();
        
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);

            if ($form->isValid())
            {
                $em = $this->getDoctrine()->getEntityManager();

                if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
                {
                    $id = $this->get('security.context')->getToken()->getUser()->getId();
                    
                    // verify that the reserved activity belong to the logged organization
                    if ($session->getActivity()->getOrganization()->getId() != $id)
                    {
                        throw new AccessDeniedException();
                    }
                }
<commit_msg>defined the start datetime and end datetime to make easier to check conflicts with resources assigned to reservations<commit_after><?php

namespace PFCD\TourismBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

use PFCD\TourismBundle\Constants;

use PFCD\TourismBundle\Entity\Session;
use PFCD\TourismBundle\Form\SessionType;
use PFCD\TourismBundle\Entity\SessionGenerator;
use PFCD\TourismBundle\Form\SessionGeneratorType;
use PFCD\TourismBundle\Entity\SessionFilter;
use PFCD\TourismBundle\Form\SessionFilterType;
use PFCD\TourismBundle\Entity\Reservation;

/**
 * Session controller
 */
class SessionController extends Controller
{
    
    /**************************************************************************
     ***** BACK AREA **********************************************************
     **************************************************************************/
    
    /**
     * Lists all Session entities
     */
    public function backIndexAction()
    {
        $options['domain'] = $this->get('security.context')->isGranted('ROLE_ADMIN') ? Constants::ADMIN : Constants::BACK;
        
        if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
        {
            // parameter used to filter and show only the activies that belong to the logged organization
            $options['organization'] = $this->get('security.context')->getToken()->getUser()->getId();
        }
        
        $sessionFilter = new SessionFilter();
        
        $request = $this->getRequest();
        
        if ($request->getMethod() == 'GET')
        {
            $activityId = $request->get('activityId', null);
            
            if ($activityId)
            {
                $filter['id'] = $activityId;

                if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
                {
                    $filter['organization'] = $this->get('security.context')->getToken()->getUser()->getId();
                }

                $em = $this->getDoctrine()->getEntityManager();

                $activity = $em->getRepository('PFCDTourismBundle:Activity')->findOneBy($filter);

                if (!$activity) throw $this->createNotFoundException('Unable to find Activity entity.');
                
                $sessionFilter->setActivity($activity);
            }
        }
        
        $form = $this->createForm(new SessionFilterType(), $sessionFilter, $options);
        
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);

            if ($form->isValid())
            {
                if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
                {
                    $id = $this->get('security.context')->getToken()->getUser()->getId();
                    
                    // verify that the reserved activity belong to the logged organization
                    if ($sessionFilter->getActivity() && $sessionFilter->getActivity()->getOrganization()->getId() != $id)
                    {
                        throw new AccessDeniedException();
                    }
                }
            }
        }
        
        $em = $this->getDoctrine()->getEntityManager();
        
        if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
        {
            $organization = $this->get('security.context')->getToken()->getUser()->getId();
        }
        else
        {
            $organization = null;
        }

        $activity = $sessionFilter->getActivity();
        $dateStart = $sessionFilter->getDateStart();
        $dateEnd = $sessionFilter->getDateEnd();
        $time = $sessionFilter->getStartTime();
        $daysWeek = $sessionFilter->getDaysWeek();
        $status = $sessionFilter->getStatus();

        $sessions = $em->getRepository('PFCDTourismBundle:Session')->findAllFiltered($organization, $activity, $dateStart, $dateEnd, $time, $daysWeek, $status);

        return $this->render('PFCDTourismBundle:Back/Session:index.html.twig', array(
            'sessions' => $sessions,
            'form'     => $form->createView()
        ));
    }

    /**
     * Displays a form to create a new Session entity and store it when the form is submitted and valid
     */
    public function backCreateAction()
    {
        $options['domain'] = $this->get('security.context')->isGranted('ROLE_ADMIN') ? Constants::ADMIN : Constants::BACK;
        $options['type'] = Constants::FORM_CREATE;
        
        if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
        {
            // parameter used to filter and show only the activies that belong to the logged organization
            $options['organization'] = $this->get('security.context')->getToken()->getUser()->getId();
        }
        
        $session = new Session();
        $form = $this->createForm(new SessionType(), $session, $options);
        
        $request = $this->getRequest();
        
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);

            if ($form->isValid())
            {
                $em = $this->getDoctrine()->getEntityManager();

                if ($this->get('security.context')->isGranted('ROLE_ORGANIZATION'))
                {
                    $id = $this->get('security.context')->getToken()->getUser()->getId();
                    
                    // verify that the reserved activity belong to the logged organization
                    if ($session->getActivity()->getOrganization()->getId() != $id)
                    {
                        throw new AccessDeniedException();
                    }
                }
                
                // define the start datetime and end datetime to make easier to check conflicts
                $date = $session->getDate();
                $time = $session->getTime();
                
                $startDatetime = $date->setTime($time->format('H'), $time->format('i'));
                $session->setStartDatetime($startDatetime);
                
                $durationDays = $session->getActivity()->getDurationDays();
                $durationTime = $session->getActivity()->getDurationTime();
               
                $endDatetime = clone $startDatetime;
                $endDatetime->add(new \DateInterval('P' . $durationDays . 'DT' . $durationTime->format('H') . 'H' . $durationTime->format('i') . 'M'));
                $session->setEndDatetime($endDatetime);

{'fraction_non_alphanumeric': 0.17286260473440168, 'fraction_numerical': 0.0, 'mean_word_length': 2.1282442748091603, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 179, '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': '7656436', 'n_tokens_mistral': 3194, 'n_tokens_neox': 3012, 'n_words': 546}