François 6 роки тому
батько
коміт
4c9278df03

+ 9 - 1
app/config/services.yml

@@ -68,4 +68,12 @@ services:
             - "@AppBundle\\Service\\Mattermost"
             - "@session"
         tags:
-        - { name: doctrine.event_listener, event: postPersist }
+        - { name: doctrine.event_listener, event: postPersist }
+
+    AppBundle\Service\NoteListener:
+        arguments:
+            - "@AppBundle\\Service\\NoteMoyenne"
+        tags:
+        - { name: doctrine.event_listener, event: postPersist }
+        - { name: doctrine.event_listener, event: postUpdate }
+        - { name: doctrine.event_listener, event: postRemove }

+ 25 - 0
src/AppBundle/Entity/Film.php

@@ -52,6 +52,13 @@ class Film
      */
     private $lien;
 
+    /**
+     * @var float
+     *
+     * @ORM\Column(name="note", type="float", nullable=true)
+     */
+    private $note;
+
     /**
      * @return \DateTime
      */
@@ -404,4 +411,22 @@ class Film
         return $this;
     }
 
+    /**
+     * @return float
+     */
+    public function getNote()
+    {
+        return $this->note;
+    }
+
+    /**
+     * @param float $note
+     */
+    public function setNote($note)
+    {
+        $this->note = $note;
+    }
+
+
+
 }

+ 15 - 0
src/AppBundle/Resources/views/videotheque/liste.html.twig

@@ -20,6 +20,7 @@
 			<th>Titre du film</th>
 			<th>Genre</th>
 			<th>Réalisateur</th>
+			<th>Note moyenne</th>
 			<th>Année</th>
 			<th>Lien</th>
 			<th>Actions</th>
@@ -74,6 +75,20 @@
 				{%  endfor %}
 			{% endif %}
 			</td>
+			<td>
+				{% if film.note > 0 %}
+					<input class="rating"
+						   data-disabled="true"
+						   data-show-clear="false"
+						   data-show-caption="false"
+						   data-theme="krajee-fa"
+						   min=0
+						   max=5
+						   data-step=0.5
+						   data-size="xs"
+						   value="{{ film.note }}">
+				{% endif %}
+			</td>
 			<td>{{ film.annee | date('Y') }}</td>
 			<td>{% if not film.lien == "" %}<a target="_blank" href="{{ film.lien }}"><i class="fa fa-external-link fa-lg"></i></a>{% endif %}</td>
 			<td>

+ 14 - 0
src/AppBundle/Resources/views/videotheque/voirfilm.html.twig

@@ -15,6 +15,20 @@
         <a href="#" class="boutonVu" data-content="{{ film.id }}"><i class="badge badge-danger">Pas vu</i><i class="badge badge-secondary">Vu</i></a>
         {#<a href="#" class="boutonVu" data-content="{{ film.id }}"><i class="badge badge-secondary">Pas vu</i></a>#}
     {%  endif %}
+    {% if film.note > 0 %}
+        <p>
+            <input class="rating"
+               data-disabled="true"
+               data-show-clear="false"
+               data-show-caption="false"
+               data-theme="krajee-fa"
+               min=0
+               max=5
+               data-step=0.5
+               data-size="xs"
+               value="{{ film.note }}">
+        </p>
+    {% endif %}
 {% endblock %}
 
 {%  block body %}

+ 44 - 0
src/AppBundle/Service/NoteListener.php

@@ -0,0 +1,44 @@
+<?php
+
+namespace AppBundle\Service;
+
+use AppBundle\Entity\Commentaire;;
+use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
+
+
+class NoteListener
+{
+    private $noteMoyenne;
+
+    public function __construct(NoteMoyenne $noteMoyenne)
+    {
+        $this->noteMoyenne = $noteMoyenne;
+    }
+
+    public function postPersist(LifecycleEventArgs $args)
+    {
+        $this->calculer($args);
+
+    }
+
+    public function postUpdate (LifecycleEventArgs $args)
+    {
+        $this->calculer($args);
+    }
+
+    public function postRemove (LifecycleEventArgs $args)
+    {
+        $this->calculer($args);
+    }
+
+    private function calculer (LifecycleEventArgs $args) {
+        $entity = $args->getObject();
+
+        if (!$entity instanceof Commentaire) {
+            return;
+        }
+
+        $this->noteMoyenne->calculerMoyenne($entity->getFilm());
+    }
+
+}

+ 45 - 0
src/AppBundle/Service/NoteMoyenne.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace AppBundle\Service;
+
+use AppBundle\Entity\Film;
+use Doctrine\ORM\EntityManagerInterface;
+
+/**
+ * Calculer moyenne pour un film
+ */
+class NoteMoyenne {
+    private $em;
+
+    public function __construct(EntityManagerInterface $em)
+    {
+        $this->em = $em;
+    }
+
+    public function calculerMoyenne (Film $film)
+    {
+        $commentaires = $this->em
+            ->getRepository('AppBundle:Commentaire')
+            ->findBy(array('film' => $film));
+
+        $notes[] = array();
+        $total = 0;
+        $nb = 0;
+
+        foreach ($commentaires as $commentaire) {
+            $nb ++;
+            $total += $commentaire->getNote();
+        }
+
+        $note = 0;
+
+        if ($nb > 0) {
+            $note = $total / $nb;
+        }
+        if ($note == 0)
+            $note = null;
+
+        $film->setNote($note);
+        $this->em->flush();
+    }
+}