diff --git a/application/modules/opac/controllers/AbonneController.php b/application/modules/opac/controllers/AbonneController.php index d89b97e03bef6bb94d57aefe268a8664c71d1464..aa85d3992c9e9e6f838fe8149f8ab69cbac6ec26 100644 --- a/application/modules/opac/controllers/AbonneController.php +++ b/application/modules/opac/controllers/AbonneController.php @@ -580,10 +580,18 @@ class AbonneController extends Zend_Controller_Action { } if ($this->_getParam('time') && $this->_getParam('duration')) { - $bean->time = $this->_getParam('time'); - $bean->duration = (int)$this->_getParam('duration'); - $this->_redirect('/abonne/multimedia-hold-device'); - return; + $holdLoader = Class_Multimedia_DeviceHold::getLoader(); + $start = $holdLoader->getTimeFromDayAndTime($bean->day, $this->_getParam('time')); + $end = $holdLoader->getTimeFromStartAndDuration($start, $this->_getParam('duration')); + + if (0 == $holdLoader->countBetweenTimesForUser($start, $end, $this->_user)) { + $bean->time = $this->_getParam('time'); + $bean->duration = (int)$this->_getParam('duration'); + $this->_redirect('/abonne/multimedia-hold-device'); + return; + } + + $this->view->error = $this->view->_('Vous avez déjà une réservation dans ce créneau horaire'); } $this->view->timelineActions = $this->_getTimelineActions('hours'); diff --git a/application/modules/opac/views/scripts/abonne/multimedia-hold-hours.phtml b/application/modules/opac/views/scripts/abonne/multimedia-hold-hours.phtml index 44b7ca3f5446857ce38675d1f40131b64e7095a1..a17e50d10d6a1fbae07c1dd4984ce209fa9ec568 100644 --- a/application/modules/opac/views/scripts/abonne/multimedia-hold-hours.phtml +++ b/application/modules/opac/views/scripts/abonne/multimedia-hold-hours.phtml @@ -1,6 +1,12 @@ <?php $this->openBoite($this->_("Réserver un poste multimédia")); ?> <?php echo $this->timeline($this->timelineActions);?> +<?php if ($this->error) { ?> +<div class="error"> +<?php echo $this->error;?> +</div> +<?php } ?> + <form method="get"> <select id="time" name="time"> <?php foreach ($this->times as $k => $v) { ?> diff --git a/library/Class/Multimedia/DeviceHold.php b/library/Class/Multimedia/DeviceHold.php index 4199ad0366bf00d036e04ee1f1a2ee280a0cbfe4..30bd68a3cfec323f634bf2f8748b6f746cb47d91 100644 --- a/library/Class/Multimedia/DeviceHold.php +++ b/library/Class/Multimedia/DeviceHold.php @@ -26,8 +26,8 @@ class Multimedia_DeviceHoldloader extends Storm_Model_Loader { public function newFromBean($bean) { $device = Class_Multimedia_Device::getLoader()->find((int)$bean->device); $user = Class_Users::getLoader()->getIdentity(); - $start = strtotime($bean->day . ' ' . $bean->time . ':00'); - $end = $start + ($bean->duration * 60); + $start = $this->getTimeFromDayAndTime($bean->day, $bean->time); + $end = $this->getTimeFromStartAndDuration($start, $bean->duration); return $this->newInstance() ->setDevice($device) @@ -37,6 +37,26 @@ class Multimedia_DeviceHoldloader extends Storm_Model_Loader { } + /** + * @param $day string + * @param $time string + * @return int + */ + public function getTimeFromDayAndTime($day, $time) { + return strtotime($day . ' ' . $time . ':00'); + } + + + /** + * @param $start int + * @param $duration int minutes + * @return int + */ + public function getTimeFromStartAndDuration($start, $duration) { + return $start + ($duration * 60); + } + + /** * @param $user Class_Users * @return array @@ -56,13 +76,35 @@ class Multimedia_DeviceHoldloader extends Storm_Model_Loader { * @return int */ public function countBetweenTimesForDevice($start, $end, $device) { - return $this->countBy(array( - 'role' => 'device', - 'model' => $device, + return $this->_countBetweenTimesWithOptions($start, $end, array('role' => 'device', + 'model' => $device)); + } + + + /** + * @param $start int + * @param $end int + * @param $user Class_Users + * @return int + */ + public function countBetweenTimesForUser($start, $end, $user) { + return $this->_countBetweenTimesWithOptions($start, $end, array('role' => 'user', + 'model' => $user)); + } + + + /** + * @param $start int + * @param $end int + * @param $options array + * @return int + */ + protected function _countBetweenTimesWithOptions($start, $end, $options) { + return $this->countBy(array_merge($options, array( 'where' => '(start <= ' . $start . ' and end >= ' . $end . ')' . ' or (start > ' . $start . ' and end < ' . $end . ')' . ' or (start < ' . $end . ' and end > ' . $end . ')' - . ' or (start < ' . $start . ' and end > ' . $start . ')')); + . ' or (start < ' . $start . ' and end > ' . $start . ')'))); } } diff --git a/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php b/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php index a5f9cdb6585c38cd65c0de224b9f56dc608dd6fb..651fbc906574d19bc3e0f994d7f68c91802de851 100644 --- a/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php +++ b/tests/application/modules/opac/controllers/AbonneControllerMultimediaTest.php @@ -443,6 +443,25 @@ class AbonneControllerMultimediaHoldHoursChoiceTest extends AbonneControllerMult } +class AbonneControllerMultimediaHoldHoursChooseAlreadyHeldTest extends AbonneControllerMultimediaHoldTestCase { + public function setUp() { + parent::setUp(); + $this->_prepareLocationInSession(); + $this->_prepareDayInSession(); + Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Multimedia_DeviceHold') + ->whenCalled('countBetweenTimesForUser') + ->answers(1); + $this->dispatch('/abonne/multimedia-hold-hours/time/' . urlencode('9:45') . '/duration/45', true); + } + + + /** @test */ + public function errorMessageShouldBePresent() { + $this->assertXPathContentContains('//div[@class="error"]', 'Vous avez déjà une réservation dans ce créneau horaire'); + } +} + + class AbonneControllerMultimediaHoldDeviceTest extends AbonneControllerMultimediaHoldTestCase { public function setUp() { parent::setUp(); @@ -635,6 +654,12 @@ class AbonneControllerMultimediaHoldViewTest extends AbonneControllerMultimediaH public function deviceShouldBePoste34() { $this->assertXPathContentContains('//li', 'Poste : Poste 34 - Archlinux'); } + + + /** @test */ + public function cancelationLinkShouldBePresent() { + $this->assertXPath('//a[contains(@href, "multimedia-hold-view/id/455/delete/1")]'); + } }