From 3a03f227ce6a7da0b69c6e205e5b3e3a6864bdd5 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@git-test.afi-sa.fr>
Date: Thu, 12 Jul 2012 17:05:36 +0000
Subject: [PATCH] =?UTF-8?q?Multimedia:=20Un=20abonn=C3=A9=20ne=20peut=20pa?=
 =?UTF-8?q?s=20r=C3=A9server=20deux=20postes=20diff=C3=A9rent=20dans=20le?=
 =?UTF-8?q?=20m=C3=AAme=20cr=C3=A9neau=20horaire?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../opac/controllers/AbonneController.php     | 16 ++++--
 .../abonne/multimedia-hold-hours.phtml        |  6 +++
 library/Class/Multimedia/DeviceHold.php       | 54 ++++++++++++++++---
 .../AbonneControllerMultimediaTest.php        | 25 +++++++++
 4 files changed, 91 insertions(+), 10 deletions(-)

diff --git a/application/modules/opac/controllers/AbonneController.php b/application/modules/opac/controllers/AbonneController.php
index d89b97e03be..aa85d3992c9 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 44b7ca3f544..a17e50d10d6 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 4199ad0366b..30bd68a3cfe 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 a5f9cdb6585..651fbc90657 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")]');
+	}
 }
 
 
-- 
GitLab