bastien 12 years ago
parent
commit
d8d09aceb7

+ 2 - 2
src/Muzich/CommentBundle/Controller/CommentController.php View File

@@ -4,7 +4,7 @@ namespace Muzich\CommentBundle\Controller;
4 4
 
5 5
 use Muzich\CoreBundle\lib\Controller;
6 6
 use Muzich\CoreBundle\Managers\CommentsManager;
7
-use Muzich\CoreBundle\Propagator\EventElementComment;
7
+use Muzich\CoreBundle\Propagator\EventElement;
8 8
 
9 9
 class CommentController extends Controller
10 10
 {
@@ -66,7 +66,7 @@ class CommentController extends Controller
66 66
     $cm = new CommentsManager($element->getComments());
67 67
     $cm->add($this->getUser(), $comment);
68 68
     $element->setComments($cm->get());
69
-    $event = new EventElementComment($this->container);
69
+    $event = new EventElement($this->container);
70 70
     
71 71
     // Event pour user d'un nouveau comment
72 72
     if ($this->getUserId() != $element->getOwner()->getId())

+ 44 - 3
src/Muzich/CoreBundle/Command/RecalculateReputationCommand.php View File

@@ -78,12 +78,53 @@ class RecalculateReputationCommand extends ContainerAwareCommand
78 78
         $element_points += $element->getPoints();
79 79
       }
80 80
       
81
-      $user->setReputation($element_points * $coef_element_point);
81
+      // On calcule pour les favoris
82
+      $coef_element_fav = $this->getContainer()->getParameter('reputation_element_favorite_value');
83
+      $count_favs = 0;
84
+      $fav = $em->createQuery(
85
+        "SELECT COUNT(f) FROM MuzichCoreBundle:UsersElementsFavorites f"
86
+        . " JOIN f.element e"
87
+        . " WHERE e.owner = :uid AND f.user != :uid"
88
+      )->setParameter('uid', $user->getId())
89
+       ->getScalarResult()      
90
+      ;
91
+      
92
+      if (count($fav))
93
+      {
94
+        if (count($fav[0]))
95
+        {
96
+          $count_favs = $fav[0][1];
97
+        }
98
+      }
99
+      
100
+      // Calcul pour les utilisateurs suivis
101
+      $coef_follow = $this->getContainer()->getParameter('reputation_element_follow_value');
102
+      $count_follow = 0;
103
+      $fol = $em->createQuery(
104
+        "SELECT COUNT(f) FROM MuzichCoreBundle:FollowUser f"
105
+        . " WHERE f.followed = :uid"
106
+      )->setParameter('uid', $user->getId())
107
+       ->getScalarResult()      
108
+      ;
109
+      
110
+      if (count($fol))
111
+      {
112
+        if (count($fol[0]))
113
+        {
114
+          $count_follow = $fol[0][1];
115
+        }
116
+      }
117
+      
118
+      $points = ($element_points * $coef_element_point)
119
+        + ($count_favs * $coef_element_fav)
120
+        + ($count_follow * $coef_follow)
121
+      ;
122
+      
123
+      $user->setReputation($points);
82 124
       $em->persist($user);
125
+      $em->flush();
83 126
     }
84 127
     
85
-    $output->writeln('<info>Enregistrement en base ...</info>');
86
-    $em->flush();
87 128
     $output->writeln('<info>Terminé !</info>');
88 129
   }
89 130
 }

+ 18 - 3
src/Muzich/CoreBundle/Controller/CoreController.php View File

@@ -17,6 +17,7 @@ use Muzich\CoreBundle\Entity\Tag;
17 17
 use Muzich\CoreBundle\Managers\TagManager;
18 18
 use Muzich\CoreBundle\Entity\UsersTagsFavorites;
19 19
 use Muzich\CoreBundle\Managers\ElementReportManager;
20
+use Muzich\CoreBundle\Propagator\EventUser;
20 21
 
21 22
 class CoreController extends Controller
22 23
 {
@@ -86,7 +87,11 @@ class CoreController extends Controller
86 87
     }
87 88
     
88 89
     // Vérifications préléminaires
89
-    if ($user->getPersonalHash() != $token || !in_array($type, array('user', 'group')) || !is_numeric($id))
90
+    if ($user->getPersonalHash() != $token 
91
+            || !in_array($type, array('user', 'group')) 
92
+            || !is_numeric($id)
93
+            || $user->getId() == $id
94
+    )
90 95
     {
91 96
       throw $this->createNotFoundException();
92 97
     }
@@ -107,6 +112,10 @@ class CoreController extends Controller
107 112
     if ($Follow)
108 113
     {
109 114
       // L'utilisateur suis déjà, on doit détruire l'entité
115
+      $event = new EventUser($this->container);
116
+      $event->removeFromFollow($Follow->getFollowed());
117
+      $em->persist($Follow->getFollowed());
118
+      
110 119
       $em->remove($Follow);
111 120
       $em->flush();
112 121
       $following = false;
@@ -125,10 +134,16 @@ class CoreController extends Controller
125 134
       if ($type == 'user') { $Follow = new FollowUser(); }
126 135
       else { $Follow = new FollowGroup(); }
127 136
       $Follow->setFollower($user);
128
-      if ($type == 'user') { $Follow->setFollowed($followed); }
137
+      if ($type == 'user')
138
+      { 
139
+        $Follow->setFollowed($followed); 
140
+        
141
+        $event = new EventUser($this->container);
142
+        $event->addToFollow($followed);
143
+        $em->persist($followed);
144
+      }
129 145
       else { $Follow->setGroup($followed); }
130 146
       
131
-      
132 147
       $em->persist($Follow);
133 148
       $em->flush();
134 149
       $following = true;

+ 3 - 3
src/Muzich/CoreBundle/Controller/ElementController.php View File

@@ -4,7 +4,7 @@ namespace Muzich\CoreBundle\Controller;
4 4
 
5 5
 use Muzich\CoreBundle\lib\Controller;
6 6
 use Muzich\CoreBundle\ElementFactory\ElementManager;
7
-use Muzich\CoreBundle\Propagator\EventElementScore;
7
+use Muzich\CoreBundle\Propagator\EventElement;
8 8
 
9 9
 class ElementController extends Controller
10 10
 {
@@ -406,7 +406,7 @@ class ElementController extends Controller
406 406
     // On ajoute un vote a l'élément
407 407
     $element->addVoteGood($this->getUser()->getId());
408 408
     // Puis on lance les actions propagés par ce vote
409
-    $event = new EventElementScore($this->container);
409
+    $event = new EventElement($this->container);
410 410
     $event->onePointAdded($element);
411 411
     
412 412
     $this->getDoctrine()->getEntityManager()->persist($element);
@@ -465,7 +465,7 @@ class ElementController extends Controller
465 465
     // Retrait du vote good
466 466
     $element->removeVoteGood($this->getUser()->getId());
467 467
     // Puis on lance les actions propagés par retrait de vote
468
-    $event = new EventElementScore($this->container);
468
+    $event = new EventElement($this->container);
469 469
     $event->onePointRemoved($element);
470 470
     
471 471
     $this->getDoctrine()->getEntityManager()->persist($element);

+ 121 - 0
src/Muzich/CoreBundle/Propagator/EventElement.php View File

@@ -0,0 +1,121 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Propagator;
4
+
5
+use Muzich\CoreBundle\Propagator\EventPropagator;
6
+use Muzich\CoreBundle\Entity\Element;
7
+use Muzich\CoreBundle\Actions\User\Event as UserEventAction;
8
+use Muzich\CoreBundle\Actions\User\Reputation as UserReputation;
9
+use Muzich\CoreBundle\Entity\Event;
10
+
11
+/**
12
+ * Propagateur d'événement concernant les éléments
13
+ *
14
+ * @author bux
15
+ */
16
+class EventElement extends EventPropagator
17
+{  
18
+  
19
+  /**
20
+   * Cette procédure doit être appelé après l'ajout d'un commentaire sur un 
21
+   * événement. Actuellement il:
22
+   * * Met a jour ou créer un objet événement (nouveau commentaire) pour le
23
+   *   propriétaire de l'élément.
24
+   * 
25
+   * @param Element $element 
26
+   */
27
+  public function commentAdded(Element $element)
28
+  {
29
+    $em = $this->container->get('doctrine')->getEntityManager();
30
+    
31
+    try
32
+    {
33
+      $event = $em->createQuery(
34
+        'SELECT e FROM MuzichCoreBundle:Event e
35
+        WHERE e.user = :uid AND e.type = :type'
36
+      )->setParameters(array(
37
+        'uid' => $element->getOwner()->getId(),
38
+        'type' => Event::TYPE_COMMENT_ADDED_ELEMENT
39
+      ))->getSingleResult()
40
+      ;
41
+      $new = false;
42
+    } 
43
+    catch (\Doctrine\ORM\NoResultException $e)
44
+    {
45
+      $event = new Event();
46
+      $new = true;
47
+    }
48
+    
49
+    $uea = new UserEventAction($element->getOwner(), $event);
50
+    if ($new)
51
+    {
52
+      $uea->createEvent(
53
+        Event::TYPE_COMMENT_ADDED_ELEMENT,
54
+        $element->getId()
55
+      );
56
+    }
57
+    else
58
+    {
59
+      $uea->updateEvent($element->getId());
60
+    }
61
+    
62
+    $em->persist($event);
63
+  }
64
+  
65
+  /**
66
+   * Un point a été ajouté par quelqu'un a cet élément
67
+   * Conséquences:
68
+   *  * L'auteur du partage gagne x point de reputation
69
+   *
70
+   * @param Element $element 
71
+   */
72
+  public function onePointAdded(Element $element)
73
+  {
74
+    $ur = new UserReputation($element->getOwner());
75
+    $ur->addPoints(
76
+      $this->container->getParameter('reputation_element_point_value')
77
+    );
78
+  }
79
+  
80
+  /**
81
+   * Un point a été retiré par quelqu'un a cet élément
82
+   * Conséquences:
83
+   *  * L'auteur du partage perd x point de reputation
84
+   *
85
+   * @param Element $element 
86
+   */
87
+  public function onePointRemoved(Element $element)
88
+  {
89
+    $ur = new UserReputation($element->getOwner());
90
+    $ur->removePoints(
91
+      $this->container->getParameter('reputation_element_point_value')
92
+    );
93
+  }
94
+  
95
+  /**
96
+   * L'élément a été ajouté aux favoris d'un utilisateur
97
+   * 
98
+   * @param Element $element 
99
+   */
100
+  public function addedToFavorites(Element $element)
101
+  {
102
+    $ur = new UserReputation($element->getOwner());
103
+    $ur->addPoints(
104
+      $this->container->getParameter('reputation_element_favorite_value')
105
+    );
106
+  }
107
+  
108
+  /**
109
+   * L'élément a été retiré des favoris d'un utilisateur
110
+   * 
111
+   * @param Element $element 
112
+   */
113
+  public function removedFromFavorites(Element $element)
114
+  {
115
+    $ur = new UserReputation($element->getOwner());
116
+    $ur->removePoints(
117
+      $this->container->getParameter('reputation_element_favorite_value')
118
+    );
119
+  }
120
+  
121
+}

+ 0 - 64
src/Muzich/CoreBundle/Propagator/EventElementComment.php View File

@@ -1,64 +0,0 @@
1
-<?php
2
-
3
-namespace Muzich\CoreBundle\Propagator;
4
-
5
-use Muzich\CoreBundle\Propagator\EventPropagator;
6
-use Muzich\CoreBundle\Entity\Element;
7
-use Muzich\CoreBundle\Actions\User\Event as UserEventAction;
8
-use Muzich\CoreBundle\Entity\Event;
9
-
10
-/**
11
- * Propagateur d'événement concernant les Commentaires d'éléments
12
- *
13
- * @author bux
14
- */
15
-class EventElementComment extends EventPropagator
16
-{  
17
-  
18
-  /**
19
-   * Cette procédure doit être appelé après l'ajout d'un commentaire sur un 
20
-   * événement. Actuellement il:
21
-   * * Met a jour ou créer un objet événement (nouveau commentaire) pour le
22
-   *   propriétaire de l'élément.
23
-   * 
24
-   * @param Element $element 
25
-   */
26
-  public function commentAdded(Element $element)
27
-  {    
28
-    $em = $this->container->get('doctrine')->getEntityManager();
29
-    
30
-    try
31
-    {
32
-      $event = $em->createQuery(
33
-        'SELECT e FROM MuzichCoreBundle:Event e
34
-        WHERE e.user = :uid AND e.type = :type'
35
-      )->setParameters(array(
36
-        'uid' => $element->getOwner()->getId(),
37
-        'type' => Event::TYPE_COMMENT_ADDED_ELEMENT
38
-      ))->getSingleResult()
39
-      ;
40
-      $new = false;
41
-    } 
42
-    catch (\Doctrine\ORM\NoResultException $e)
43
-    {
44
-      $event = new Event();
45
-      $new = true;
46
-    }
47
-    
48
-    $uea = new UserEventAction($element->getOwner(), $event);
49
-    if ($new)
50
-    {
51
-      $uea->createEvent(
52
-        Event::TYPE_COMMENT_ADDED_ELEMENT,
53
-        $element->getId()
54
-      );
55
-    }
56
-    else
57
-    {
58
-      $uea->updateEvent($element->getId());
59
-    }
60
-    
61
-    $em->persist($event);
62
-  }
63
-  
64
-}

+ 0 - 46
src/Muzich/CoreBundle/Propagator/EventElementScore.php View File

@@ -1,46 +0,0 @@
1
-<?php
2
-
3
-namespace Muzich\CoreBundle\Propagator;
4
-
5
-use Muzich\CoreBundle\Propagator\EventPropagator;
6
-use Muzich\CoreBundle\Entity\Element;
7
-use Muzich\CoreBundle\Actions\User\Reputation as UserReputation;
8
-
9
-/**
10
- * Propagateur d'événement liés aux score des éléments
11
- *
12
- * @author bux
13
- */
14
-class EventElementScore extends EventPropagator
15
-{  
16
-  /**
17
-   * Un point a été ajouté par quelqu'un a cet élément
18
-   * Conséquences:
19
-   *  * L'auteur du partage gagne x point de reputation
20
-   *
21
-   * @param Element $element 
22
-   */
23
-  public function onePointAdded(Element $element)
24
-  {
25
-    $ur = new UserReputation($element->getOwner());
26
-    $ur->addPoints(
27
-      $this->container->getParameter('reputation_element_point_value')
28
-    );
29
-  }
30
-  
31
-  /**
32
-   * Un point a été retiré par quelqu'un a cet élément
33
-   * Conséquences:
34
-   *  * L'auteur du partage perd x point de reputation
35
-   *
36
-   * @param Element $element 
37
-   */
38
-  public function onePointRemoved(Element $element)
39
-  {
40
-    $ur = new UserReputation($element->getOwner());
41
-    $ur->removePoints(
42
-      $this->container->getParameter('reputation_element_point_value')
43
-    );
44
-  }
45
-  
46
-}

+ 1 - 3
src/Muzich/CoreBundle/Propagator/EventPropagator.php View File

@@ -17,6 +17,4 @@ class EventPropagator
17 17
   {
18 18
     $this->container = $container;
19 19
   }
20
-}
21
-
22
-?>
20
+}

+ 45 - 0
src/Muzich/CoreBundle/Propagator/EventUser.php View File

@@ -0,0 +1,45 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Propagator;
4
+
5
+use Muzich\CoreBundle\Propagator\EventPropagator;
6
+use Muzich\CoreBundle\Entity\User;
7
+use Muzich\CoreBundle\Actions\User\Event as UserEventAction;
8
+use Muzich\CoreBundle\Actions\User\Reputation as UserReputation;
9
+use Muzich\CoreBundle\Entity\Event;
10
+
11
+/**
12
+ * Propagateur d'événement concernant les users
13
+ *
14
+ * @author bux
15
+ */
16
+class EventUser extends EventPropagator
17
+{  
18
+  
19
+  /**
20
+   * L'utilisateur est suivis par un autre utilisateur
21
+   * 
22
+   * @param User $user Utilisateur suivis
23
+   */
24
+  public function addToFollow(User $user)
25
+  {
26
+    $ur = new UserReputation($user);
27
+    $ur->addPoints(
28
+      $this->container->getParameter('reputation_element_follow_value')
29
+    );
30
+  }
31
+  
32
+  /**
33
+   * L'utilisateur n'est plus suivit par un autre utilisateur
34
+   * 
35
+   * @param User $user Utilisateur plus suivis
36
+   */
37
+  public function removeFromFollow(User $user)
38
+  {
39
+    $ur = new UserReputation($user);
40
+    $ur->removePoints(
41
+      $this->container->getParameter('reputation_element_follow_value')
42
+    );
43
+  }
44
+  
45
+}

+ 18 - 0
src/Muzich/FavoriteBundle/Controller/FavoriteController.php View File

@@ -6,6 +6,7 @@ use Muzich\CoreBundle\lib\Controller;
6 6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7 7
 use Muzich\CoreBundle\Entity\UsersElementsFavorites;
8 8
 use Muzich\CoreBundle\Searcher\ElementSearcher;
9
+use Muzich\CoreBundle\Propagator\EventElement;
9 10
 //use Muzich\CoreBundle\Entity\Group;
10 11
 //use Muzich\CoreBundle\Form\Group\GroupForm;
11 12
 //use Symfony\Component\HttpFoundation\Request;
@@ -61,6 +62,15 @@ class FavoriteController extends Controller
61 62
       $favorite = new UsersElementsFavorites();
62 63
       $favorite->setUser($user);
63 64
       $favorite->setElement($element);
65
+      
66
+      if ($user->getId() != $element->getOwner()->getId())
67
+      {
68
+        // On déclenche les événements liés a cette action
69
+        $event = new EventElement($this->container);
70
+        $event->addedToFavorites($element);
71
+        $em->persist($user);
72
+      }
73
+      
64 74
       $em->persist($favorite);
65 75
       $em->flush();
66 76
     }
@@ -113,6 +123,14 @@ class FavoriteController extends Controller
113 123
         'element' => $id
114 124
       ))))
115 125
     {
126
+      if ($user->getId() != $element->getOwner()->getId())
127
+      {
128
+        // On déclenche les événements liés a cette action
129
+        $event = new EventElement($this->container);
130
+        $event->removedFromFavorites($element);
131
+      }
132
+      
133
+      $em->persist($element->getOwner());
116 134
       $em->remove($fav);
117 135
       $em->flush();
118 136
     }