Browse Source

Evolution #171: Suppression d'un élément: impact sur reputation

bastien 12 years ago
parent
commit
9c234d8169

+ 5 - 0
src/Muzich/AdminBundle/Controller/ModerateController.php View File

@@ -8,6 +8,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8 8
 use Muzich\CoreBundle\Entity\UsersTagsFavorites;
9 9
 use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
10 10
 use Muzich\CoreBundle\Managers\TagManager;
11
+use Muzich\CoreBundle\Propagator\EventElement;
11 12
 
12 13
 class ModerateController extends Controller
13 14
 {
@@ -163,6 +164,10 @@ class ModerateController extends Controller
163 164
       ));
164 165
     }
165 166
     
167
+    $event = new EventElement($this->container);
168
+    $event->elementRemoved($element);
169
+
170
+    $this->getDoctrine()->getEntityManager()->persist($element->getOwner());
166 171
     $this->getDoctrine()->getEntityManager()->remove($element);
167 172
     $this->getDoctrine()->getEntityManager()->flush();
168 173
     

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

@@ -199,6 +199,11 @@ class ElementController extends Controller
199 199
     try {
200 200
       $element = $this->checkExistingAndOwned($element_id);
201 201
       $em = $this->getDoctrine()->getEntityManager();
202
+      
203
+      $event = new EventElement($this->container);
204
+      $event->elementRemoved($element);
205
+      
206
+      $em->persist($element->getOwner());
202 207
       $em->remove($element);
203 208
       $em->flush();
204 209
       

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

@@ -153,4 +153,45 @@ class EventElement extends EventPropagator
153 153
     $this->container->get('doctrine')->getEntityManager()->persist($proposition->getUser());
154 154
   }
155 155
   
156
+  /**
157
+   * Intervien lorsque l'évènement est supprimé.
158
+   * Il doit retirer au propriétaire:
159
+   * * Les points de l'élément
160
+   * * Les points du aux mises en favoris
161
+   * 
162
+   * @param Element $element 
163
+   */
164
+  public function elementRemoved(Element $element)
165
+  {
166
+    // L'utilisateur n'a plus droits aux points de l'élément.
167
+    $ur = new UserReputation($element->getOwner());
168
+    $ur->removePoints(
169
+      $element->getPoints() * $this->container->getParameter('reputation_element_point_value')
170
+    );
171
+    
172
+    // Ni aux points liés aux mises en favoris
173
+    $fav = $this->container->get('doctrine')->getEntityManager()->createQuery(
174
+      "SELECT COUNT(f) FROM MuzichCoreBundle:UsersElementsFavorites f"
175
+      . " JOIN f.element e"
176
+      . " WHERE e.owner = :uid AND f.user != :uid AND e.id = :eid"
177
+    )->setParameters(array(
178
+      'uid' => $element->getOwner()->getId(),
179
+      'eid' => $element->getId()
180
+    ))
181
+     ->getScalarResult()      
182
+    ;
183
+
184
+    if (count($fav))
185
+    {
186
+      if (count($fav[0]))
187
+      {
188
+        $count_favs = $fav[0][1];
189
+        $ur = new UserReputation($element->getOwner());
190
+        $ur->removePoints(
191
+          $count_favs * $this->container->getParameter('reputation_element_favorite_value')
192
+        );
193
+      }
194
+    }
195
+  }
196
+  
156 197
 }

+ 51 - 2
src/Muzich/CoreBundle/Tests/Controller/ReputationTest.php View File

@@ -42,14 +42,63 @@ class ReputationTest extends FunctionalTest
42 42
   /**
43 43
    * Test de l'impact sur la reputation lorsque il y a vote sur element
44 44
    */
45
-  public function testImpactElementVote()
45
+  public function testElementDelete()
46 46
   {
47 47
     $this->client = self::createClient();
48 48
     $this->connectUser('paul', 'toor');
49 49
     $paul = $this->getUser();
50 50
     
51
-    //
51
+    // paul vote sur l'élément de bux
52
+    // // Comme ça on a un point de vote (fixtures = 0 points)
53
+    // // et une mise ne favoris
54
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
55
+      ->findOneByName('Heretik System Popof - Resistance')
56
+    ;
52 57
     
58
+    $crawler = $this->client->request(
59
+      'GET', 
60
+      $this->generateUrl('ajax_element_add_vote_good', array(
61
+        'element_id' => $element->getId(),
62
+        'token' => $paul->getPersonalHash()
63
+      )), 
64
+      array(), 
65
+      array(), 
66
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
67
+    );
68
+    
69
+    $this->isResponseSuccess();
70
+    
71
+    $response = json_decode($this->client->getResponse()->getContent(), true);
72
+    $this->assertEquals($response['status'], 'success');
73
+    
74
+    // bux va supprimer un de ses éléments
75
+    $this->disconnectUser();
76
+    $this->connectUser('bux', 'toor');
77
+    $bux = $this->getUser();
78
+    
79
+    // D'aprés les fixtures plus le vote de paul: 23
80
+    $this->assertEquals(23, $bux->getReputation());
81
+    
82
+    // On effectue la demande ajax d'edition
83
+    $crawler = $this->client->request(
84
+      'GET', 
85
+      $this->generateUrl('element_remove', array(
86
+        'element_id' => $element->getId()
87
+      )), 
88
+      array(), 
89
+      array(), 
90
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
91
+    );
92
+    $this->isResponseSuccess();
93
+    
94
+    $response = json_decode($this->client->getResponse()->getContent(), true);
95
+    $this->assertEquals($response['status'], 'success');
96
+    
97
+    $coef_element_fav = $this->getContainer()->getParameter('reputation_element_favorite_value');
98
+    $coef_element_point = $this->getContainer()->getParameter('reputation_element_point_value');
99
+    
100
+    $bux = $this->getUser('bux');
101
+    $this->assertEquals(23 - ($coef_element_fav * 1) - ($coef_element_point * 1), $bux->getReputation()); 
53 102
   }
54 103
   
55 104
 }