浏览代码

failed stage: score for playlist

Bastien Sevajol 11 年前
父节点
当前提交
e31583ffec

+ 51 - 6
src/Muzich/CoreBundle/Command/RecalculateReputationCommand.php 查看文件

@@ -8,14 +8,15 @@ use Symfony\Component\Console\Input\InputInterface;
8 8
 use Symfony\Component\Console\Input\InputOption;
9 9
 use Symfony\Component\Console\Output\OutputInterface;
10 10
 use Muzich\CoreBundle\Entity\EventArchive;
11
+use Muzich\CoreBundle\Entity\Element;
11 12
 
12 13
 class RecalculateReputationCommand extends ContainerAwareCommand
13 14
 {
14 15
   protected function configure()
15 16
   {
16 17
     $this
17
-      ->setName('users:reputation:recalculate')
18
-      ->setDescription('Recalcul des scores de reputation')
18
+      ->setName('score:recalculate')
19
+      ->setDescription('Recalcul des scores')
19 20
       ->addOption('user', null, InputOption::VALUE_REQUIRED, 'username du compte a traiter')
20 21
 //      ->addArgument('sites', InputArgument::OPTIONAL, 'Liste exhaustive des site a traiter')
21 22
 //      ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
@@ -26,12 +27,24 @@ class RecalculateReputationCommand extends ContainerAwareCommand
26 27
   {
27 28
     $doctrine = $this->getContainer()->get('doctrine');
28 29
     $em = $doctrine->getEntityManager();
29
-
30
+    
30 31
     $output->writeln('#');
31
-    $output->writeln('## Script de recalcul des scores de reputation ##');
32
+    $output->writeln('## Script de recalcul des scores ##');
32 33
     $output->writeln('#');
33 34
 
34 35
     $output->writeln('<info>Début du traitement ...</info>');
36
+    $this->recalculateUserScores($input, $output);
37
+    $this->recalculateElementScores($input, $output);
38
+    
39
+    $output->writeln('<info>Saving in database ...</info>');
40
+    $em->flush();
41
+    $output->writeln('<info>Terminé !</info>');
42
+  }
43
+  
44
+  protected function recalculateUserScores(InputInterface $input, OutputInterface $output)
45
+  {
46
+    $doctrine = $this->getContainer()->get('doctrine');
47
+    $em = $doctrine->getEntityManager();
35 48
     
36 49
     if (($username = $input->getOption('user')))
37 50
     {
@@ -79,6 +92,8 @@ class RecalculateReputationCommand extends ContainerAwareCommand
79 92
       foreach ($elements as $element)
80 93
       {
81 94
         $element_points += $element->getPoints();
95
+        // Point déjà ajoutés a l'user
96
+        $element_points -= ($element->getCountFavorited()*$this->getContainer()->getParameter('reputation_element_favorite_value'));
82 97
       }
83 98
       
84 99
       /*
@@ -153,9 +168,39 @@ class RecalculateReputationCommand extends ContainerAwareCommand
153 168
       
154 169
       $user->setReputation($points);
155 170
       $em->persist($user);
156
-      $em->flush();
171
+      $output->writeln('<info>User "'.$user->getUsername().'": '.$points.' score</info>');
157 172
     }
158 173
     
159
-    $output->writeln('<info>Terminé !</info>');
160 174
   }
175
+    
176
+  protected function recalculateElementScores(InputInterface $input, OutputInterface $output)
177
+  {
178
+    $doctrine = $this->getContainer()->get('doctrine');
179
+    $em = $doctrine->getEntityManager();
180
+    
181
+    $elements = $em->createQuery(
182
+      "SELECT element FROM MuzichCoreBundle:Element element"
183
+    )->getResult();
184
+    
185
+    foreach ($elements as $element)
186
+    {
187
+      $element->setPoints($this->getElementScore($element));
188
+      $em->persist($element);
189
+    }
190
+    
191
+  }
192
+  
193
+  protected function getElementScore(Element $element)
194
+  {
195
+    $element_score = 0;
196
+    
197
+    $element_score += (count($element->getVoteGoodIds())*$this->getContainer()->getParameter('reputation_element_point_value'));
198
+    
199
+    $element_score += ($element->getCountFavorited()*$this->getContainer()->getParameter('reputation_element_favorite_value'));
200
+    
201
+    $element_score += ($element->getCountPlaylisted()*$this->getContainer()->getParameter('reputation_element_added_to_playlist'));
202
+    
203
+    return $element_score;
204
+  }
205
+  
161 206
 }

+ 10 - 0
src/Muzich/CoreBundle/DataFixtures/ORM/LoadUsersElementsFavoritesData.php 查看文件

@@ -10,6 +10,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
10 10
 use Muzich\CoreBundle\Entity\UsersElementsFavorites;
11 11
 use Muzich\CoreBundle\Entity\User;
12 12
 use Muzich\CoreBundle\Managers\PlaylistManager;
13
+use Muzich\CoreBundle\Propagator\EventElement;
13 14
 
14 15
 class LoadUsersElementsFavoritesData  extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
15 16
 {
@@ -48,6 +49,15 @@ class LoadUsersElementsFavoritesData  extends AbstractFixture implements Ordered
48 49
     
49 50
     $playlist_manager->addElementsToPlaylist($elements, $playlist);
50 51
     $this->entity_manager->persist($playlist);
52
+    $this->entity_manager->flush();
53
+    
54
+    foreach ($elements as $element)
55
+    {
56
+      $event = new EventElement($this->container);
57
+      $event->addedToPlaylist($element, $user, $playlist);
58
+      $this->entity_manager->persist($element);
59
+    }
60
+    
51 61
     return $playlist;
52 62
   }
53 63
   

+ 61 - 0
src/Muzich/CoreBundle/Entity/Element.php 查看文件

@@ -260,6 +260,21 @@ class Element
260 260
   protected $count_report;
261 261
   
262 262
   /**
263
+<<<<<<< Updated upstream
264
+=======
265
+   * @ORM\Column(type="integer", nullable=true)
266
+   * @var int 
267
+   */
268
+  protected $count_favorited;
269
+  
270
+  /**
271
+   * @ORM\Column(type="integer", nullable=true)
272
+   * @var int 
273
+   */
274
+  protected $count_playlisted;
275
+  
276
+  /**
277
+>>>>>>> Stashed changes
263 278
    * @ORM\Column(type="boolean", nullable=false)
264 279
    * @var int 
265 280
    */
@@ -1052,4 +1067,50 @@ class Element
1052 1067
     $this->slug = $slug;
1053 1068
   }
1054 1069
   
1070
+  public function getCountFavorited()
1071
+  {
1072
+    if (!$this->count_favorited)
1073
+      return 0;
1074
+    
1075
+    return $this->count_favorited;
1076
+  }
1077
+  
1078
+  public function setCountFavorited($count_favorited)
1079
+  {
1080
+    $this->count_favorited = $count_favorited;
1081
+  }
1082
+  
1083
+  public function increaseCountFavorited()
1084
+  {
1085
+    $this->setCountFavorited($this->getCountFavorited()+1);
1086
+  }
1087
+  
1088
+  public function uncreaseCountFavorited()
1089
+  {
1090
+    $this->setCountFavorited($this->getCountFavorited()-1);
1091
+  }
1092
+  
1093
+  public function getCountPlaylisted()
1094
+  {
1095
+    if (!$this->count_playlisted)
1096
+      return 0;
1097
+    
1098
+    return $this->count_playlisted;
1099
+  }
1100
+  
1101
+  public function setCountPlaylisted($count_playlisted)
1102
+  {
1103
+    $this->count_playlisted = $count_playlisted;
1104
+  }
1105
+  
1106
+  public function increaseCountPlaylisted()
1107
+  {
1108
+    $this->setCountPlaylisted($this->getCountPlaylisted()+1);
1109
+  }
1110
+  
1111
+  public function uncreaseCountPlaylisted()
1112
+  {
1113
+    $this->setCountPlaylisted($this->getCountPlaylisted()-1);
1114
+  }
1115
+  
1055 1116
 }

+ 12 - 0
src/Muzich/CoreBundle/Entity/Playlist.php 查看文件

@@ -264,12 +264,24 @@ class Playlist
264 264
     return $elements_manager->have($element);
265 265
   }
266 266
   
267
+  public function getCountElement(Element $element)
268
+  {
269
+    $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
270
+    return $elements_manager->count($element);
271
+  }
272
+  
267 273
   public function haveElementId($element_id)
268 274
   {
269 275
     $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
270 276
     return $elements_manager->haveRefId($element_id);
271 277
   }
272 278
   
279
+  public function getElementDataWithIndex($index)
280
+  {
281
+    $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
282
+    return $elements_manager->getLineWithIndex($index);
283
+  }
284
+  
273 285
   public function haveTagId($tag_id)
274 286
   {
275 287
     $tags_manager = new TagCollectionManager(json_decode($this->tags, true));

+ 8 - 0
src/Muzich/CoreBundle/Managers/PlaylistManager.php 查看文件

@@ -239,4 +239,12 @@ class PlaylistManager
239 239
     $this->entity_manager->persist($playlist);
240 240
   }
241 241
   
242
+  /** @return Element */
243
+  public function getElementWithIndex(Playlist $playlist, $index)
244
+  {
245
+    $element_data = $playlist->getElementDataWithIndex($index);
246
+    return $this->entity_manager->getRepository('MuzichCoreBundle:Element')
247
+      ->findOneById($element_data['id']);
248
+  }
249
+  
242 250
 }

+ 62 - 0
src/Muzich/CoreBundle/Propagator/EventElement.php 查看文件

@@ -13,6 +13,7 @@ use Muzich\CoreBundle\Entity\ElementTagsProposition;
13 13
 use Muzich\CoreBundle\Managers\EventArchiveManager;
14 14
 use Muzich\CoreBundle\Entity\EventArchive;
15 15
 use Muzich\CoreBundle\Security\Context as SecurityContext;
16
+use Muzich\CoreBundle\Entity\Playlist;
16 17
 
17 18
 /**
18 19
  * Propagateur d'événement concernant les éléments
@@ -136,6 +137,67 @@ class EventElement extends EventPropagator
136 137
     }
137 138
   }
138 139
   
140
+  public function addedToPlaylist(Element $element, User $added_by_user, Playlist $playlist)
141
+  {
142
+    // On persiste les modifs pour compter correctement
143
+    $this->getEntityManager()->persist($playlist);
144
+    $this->getEntityManager()->flush();
145
+    
146
+    $ur = new UserReputation($element->getOwner());
147
+    $security_context = new SecurityContext($added_by_user);
148
+    if (
149
+        !$security_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_PLAYLIST_ADD_ELEMENT) 
150
+        // Qu'une seule fois dans la playlist
151
+        && $playlist->getCountElement($element) == 1
152
+        // Element dans une seule playlist (par la suite ca ne rapporte plus de points)
153
+        && $this->countUserPlaylistWithElement($added_by_user, $element) == 1
154
+        && $element->getOwner()->getId() != $added_by_user->getId()
155
+    )
156
+    {
157
+      $score_action = $this->container->getParameter('reputation_element_added_to_playlist');
158
+      $ur->addPoints($score_action);
159
+      $element->addPoints($score_action);
160
+      $element->increaseCountPlaylisted();
161
+    }
162
+  }
163
+  
164
+  protected function countUserPlaylistWithElement(User $user, Element $element)
165
+  {
166
+    return $this->getEntityManager()->createQueryBuilder()
167
+      ->select('COUNT(p)')
168
+      ->from('MuzichCoreBundle:Playlist', 'p')
169
+      ->where('p.elements LIKE :element_id AND p.owner = :owner_id')
170
+      ->setParameter('owner_id', $user->getId())
171
+      ->setParameter('element_id', '%"id":"'.$element->getId().'"%')
172
+      ->getQuery()
173
+      ->getSingleScalarResult()
174
+    ;
175
+  }
176
+  
177
+  public function removedFromPlaylist(Element $element, User $removed_by_user, Playlist $playlist)
178
+  {
179
+    // On persiste les modifs pour compter correctement
180
+    $this->getEntityManager()->persist($playlist);
181
+    $this->getEntityManager()->flush();
182
+    
183
+    $ur = new UserReputation($element->getOwner());
184
+    $security_context = new SecurityContext($removed_by_user);
185
+    if (
186
+        !$security_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_PLAYLIST_REMOVE_ELEMENT) 
187
+        // L'element n'est plus dans la playlist
188
+        && $playlist->getCountElement($element) == 0
189
+        // On ne trouve plus l'element dans ses playlists
190
+        && $this->countUserPlaylistWithElement($removed_by_user, $element) == 0
191
+        && $element->getOwner()->getId() != $removed_by_user->getId()
192
+    )
193
+    {
194
+      $score_action = $this->container->getParameter('reputation_element_added_to_playlist');
195
+      $ur->removePoints($score_action);
196
+      $element->removePoints($score_action);
197
+      $element->uncreaseCountPlaylisted();
198
+    }
199
+  }
200
+  
139 201
   /**
140 202
    * Des tags viennent d'être proposé a un élément
141 203
    *

+ 8 - 0
src/Muzich/CoreBundle/Propagator/EventPropagator.php 查看文件

@@ -3,6 +3,7 @@
3 3
 namespace Muzich\CoreBundle\Propagator;
4 4
 
5 5
 use Symfony\Component\DependencyInjection\Container;
6
+use Doctrine\ORM\EntityManager;
6 7
 
7 8
 /**
8 9
  * Description of EventPropagator
@@ -17,4 +18,11 @@ class EventPropagator
17 18
   {
18 19
     $this->container = $container;
19 20
   }
21
+  
22
+  /** @return EntityManager */
23
+  protected function getEntityManager()
24
+  {
25
+    return $this->container->get('doctrine')->getEntityManager();
26
+  }
27
+  
20 28
 }

+ 1 - 1
src/Muzich/CoreBundle/Twig/Extensions/MyTwigExtension.php 查看文件

@@ -216,7 +216,7 @@ class MyTwigExtension extends \Twig_Extension {
216 216
     return $this->container->get('form.csrf_provider')->generateCsrfToken($intention);
217 217
   }
218 218
   
219
-  public function path_token($route, $parameters = array(), $intention = '', $absolute = false)
219
+  public function path_token($route, $parameters = array(), $intention = 'unknown', $absolute = false)
220 220
   {
221 221
     $parameters = array_merge($parameters, array('token' => $this->token($intention)));
222 222
     return $this->container->get('router')->generate($route, $parameters, $absolute);

+ 23 - 0
src/Muzich/CoreBundle/lib/Collection/CollectionManager.php 查看文件

@@ -41,6 +41,21 @@ abstract class CollectionManager
41 41
     }
42 42
   }
43 43
   
44
+  public function count($object)
45
+  {
46
+    $count = 0;
47
+    $method_name = 'get' . $this->object_reference_attribute;
48
+    foreach ($this->content as $content_line)
49
+    {
50
+      if ($object->$method_name() == $content_line[lcfirst($this->object_reference_attribute)])
51
+      {
52
+        $count++;
53
+      }
54
+    }
55
+    
56
+    return $count;
57
+  }
58
+  
44 59
   public function have($object)
45 60
   {
46 61
     $method_name = 'get' . $this->object_reference_attribute;
@@ -92,6 +107,14 @@ abstract class CollectionManager
92 107
     }
93 108
   }
94 109
   
110
+  public function getLineWithIndex($index)
111
+  {
112
+    if (array_key_exists($index, $this->content))
113
+    {
114
+      return $this->content[$index];
115
+    }
116
+  }
117
+  
95 118
   public function index($object)
96 119
   {
97 120
     $method_name = 'get' . $this->object_reference_attribute;

+ 22 - 0
src/Muzich/PlaylistBundle/Controller/EditController.php 查看文件

@@ -5,6 +5,7 @@ namespace Muzich\PlaylistBundle\Controller;
5 5
 use Muzich\CoreBundle\lib\Controller;
6 6
 use Symfony\Component\HttpFoundation\Request;
7 7
 use Muzich\CoreBundle\Security\Context as SecurityContext;
8
+use Muzich\CoreBundle\Propagator\EventElement;
8 9
 
9 10
 class EditController extends Controller
10 11
 {
@@ -31,7 +32,13 @@ class EditController extends Controller
31 32
     if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
32 33
       return $this->jsonNotFoundResponse();
33 34
     
35
+    $element = $playlist_manager->getElementWithIndex($index);
34 36
     $playlist_manager->removePlaylistElementWithIndex($playlist, $index);
37
+    
38
+    $event = new EventElement($this->container);
39
+    $event->removedFromPlaylist($element, $this->getUser(), $playlist);
40
+    
41
+    $this->persist($element);
35 42
     $this->flush();
36 43
     return $this->jsonSuccessResponse();
37 44
   }
@@ -47,6 +54,11 @@ class EditController extends Controller
47 54
       return $this->jsonNotFoundResponse();
48 55
     
49 56
     $playlist_manager->addElementToPlaylist($element, $playlist);
57
+    
58
+    $event = new EventElement($this->container);
59
+    $event->addedFromPlaylist($element, $this->getUser(), $playlist);
60
+    
61
+    $this->persist($element);
50 62
     $this->flush();
51 63
     return $this->jsonSuccessResponse();
52 64
   }
@@ -64,6 +76,11 @@ class EditController extends Controller
64 76
     if ($form->isValid())
65 77
     {
66 78
       $this->getPlaylistManager()->addElementToPlaylist($element, $form->getData());
79
+      
80
+      $event = new EventElement($this->container);
81
+      $event->addedFromPlaylist($element, $this->getUser(), $form->getData());
82
+
83
+      $this->persist($element);
67 84
       $this->flush();
68 85
       return $this->jsonSuccessResponse();
69 86
     }
@@ -90,6 +107,11 @@ class EditController extends Controller
90 107
     $new_playlist = $this->getPlaylistManager()->copyPlaylist($this->getUser(), $playlist);
91 108
     $this->getPlaylistManager()->addElementToPlaylist($element, $new_playlist);
92 109
     $this->getPlaylistManager()->removePickedPlaylistToUser($this->getUser(), $playlist);
110
+    
111
+    $event = new EventElement($this->container);
112
+    $event->addedFromPlaylist($element, $this->getUser(), $new_playlist);
113
+    
114
+    $this->persist($element);
93 115
     $this->flush();
94 116
     
95 117
     return $this->jsonSuccessResponse();