Browse Source

Evolution #783: Scores Element: favoris, Users playlists

Bastien Sevajol 10 years ago
parent
commit
7fdef15a55

+ 49 - 6
src/Muzich/CoreBundle/Command/RecalculateReputationCommand.php View File

@@ -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,37 @@ 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
+    return $element_score;
202
+  }
203
+  
161 204
 }

+ 8 - 0
src/Muzich/CoreBundle/DataFixtures/ORM/LoadUsersElementsFavoritesData.php View File

@@ -35,7 +35,15 @@ class LoadUsersElementsFavoritesData  extends AbstractFixture implements Ordered
35 35
     $favorite = new UsersElementsFavorites();
36 36
     $favorite->setUser($user);
37 37
     $favorite->setElement($element);
38
+    
39
+    if ($element->getOwner()->getId() != $user->getId())
40
+    {
41
+      $element->addPoints($this->container->getParameter('reputation_element_favorite_value'));
42
+      $element->increaseCountFavorited();
43
+    }
44
+    
38 45
     $this->entity_manager->persist($favorite);
46
+    $this->entity_manager->persist($element);
39 47
     //$this->addReference('user_tag_'.$user->getId().'_'.$tag->getId(), $userTag);
40 48
   }
41 49
   

+ 39 - 0
src/Muzich/CoreBundle/Entity/Element.php View File

@@ -260,6 +260,12 @@ class Element
260 260
   protected $count_report;
261 261
   
262 262
   /**
263
+   * @ORM\Column(type="integer", nullable=true)
264
+   * @var int 
265
+   */
266
+  protected $count_favorited;
267
+  
268
+  /**
263 269
    * @ORM\Column(type="boolean", nullable=false)
264 270
    * @var int 
265 271
    */
@@ -819,6 +825,16 @@ class Element
819 825
     $this->points = $points;
820 826
   }
821 827
   
828
+  public function addPoints($points)
829
+  {
830
+    $this->points = $this->getPoints()+$points;
831
+  }
832
+  
833
+  public function removePoints($points)
834
+  {
835
+    $this->points = $this->getPoints()-$points;
836
+  }
837
+  
822 838
   public function getVoteGoodIds()
823 839
   {
824 840
     return json_decode($this->vote_good_ids, true);
@@ -1052,4 +1068,27 @@ class Element
1052 1068
     $this->slug = $slug;
1053 1069
   }
1054 1070
   
1071
+  public function getCountFavorited()
1072
+  {
1073
+    if (!$this->count_favorited)
1074
+      return 0;
1075
+    
1076
+    return $this->count_favorited;
1077
+  }
1078
+  
1079
+  public function setCountFavorited($count_favorited)
1080
+  {
1081
+    $this->count_favorited = $count_favorited;
1082
+  }
1083
+  
1084
+  public function increaseCountFavorited()
1085
+  {
1086
+    $this->setCountFavorited($this->getCountFavorited()+1);
1087
+  }
1088
+  
1089
+  public function uncreaseCountFavorited()
1090
+  {
1091
+    $this->setCountFavorited($this->getCountFavorited()-1);
1092
+  }
1093
+  
1055 1094
 }

+ 8 - 6
src/Muzich/CoreBundle/Propagator/EventElement.php View File

@@ -109,9 +109,10 @@ class EventElement extends EventPropagator
109 109
     $security_context = new SecurityContext($added_by_user);
110 110
     if (!$security_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES))
111 111
     {
112
-      $ur->addPoints(
113
-        $this->container->getParameter('reputation_element_favorite_value')
114
-      );
112
+      $score_action = $this->container->getParameter('reputation_element_favorite_value');
113
+      $ur->addPoints($score_action);
114
+      $element->addPoints($score_action);
115
+      $element->increaseCountFavorited();
115 116
     }
116 117
     
117 118
     $uea = new UserEventAction($element->getOwner(), $this->container);
@@ -130,9 +131,10 @@ class EventElement extends EventPropagator
130 131
     $security_context = new SecurityContext($removed_by_user);
131 132
     if (!$security_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES))
132 133
     {
133
-      $ur->removePoints(
134
-        $this->container->getParameter('reputation_element_favorite_value')
135
-      );
134
+      $score_action = $this->container->getParameter('reputation_element_favorite_value');
135
+      $ur->removePoints($score_action);
136
+      $element->removePoints($score_action);
137
+      $element->uncreaseCountFavorited();
136 138
     }
137 139
   }
138 140
   

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

@@ -60,6 +60,7 @@ class FavoriteController extends Controller
60 60
         $event = new EventElement($this->container);
61 61
         $event->addedToFavorites($element, $user);
62 62
         $em->persist($user);
63
+        $em->persist($element);
63 64
       }
64 65
       
65 66
       // On signale que cet user a modifié sa liste de favoris
@@ -129,6 +130,7 @@ class FavoriteController extends Controller
129 130
       $user->setData(User::DATA_FAV_UPDATED, true);
130 131
       
131 132
       $em->persist($element->getOwner());
133
+      $em->persist($element);
132 134
       $em->remove($fav);
133 135
       $em->flush();
134 136
     }