Browse Source

Evolution #783: Scores Element: favoris, Users playlists

Bastien Sevajol 11 years ago
parent
commit
7fdef15a55

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

8
 use Symfony\Component\Console\Input\InputOption;
8
 use Symfony\Component\Console\Input\InputOption;
9
 use Symfony\Component\Console\Output\OutputInterface;
9
 use Symfony\Component\Console\Output\OutputInterface;
10
 use Muzich\CoreBundle\Entity\EventArchive;
10
 use Muzich\CoreBundle\Entity\EventArchive;
11
+use Muzich\CoreBundle\Entity\Element;
11
 
12
 
12
 class RecalculateReputationCommand extends ContainerAwareCommand
13
 class RecalculateReputationCommand extends ContainerAwareCommand
13
 {
14
 {
14
   protected function configure()
15
   protected function configure()
15
   {
16
   {
16
     $this
17
     $this
17
-      ->setName('users:reputation:recalculate')
18
-      ->setDescription('Recalcul des scores de reputation')
18
+      ->setName('score:recalculate')
19
+      ->setDescription('Recalcul des scores')
19
       ->addOption('user', null, InputOption::VALUE_REQUIRED, 'username du compte a traiter')
20
       ->addOption('user', null, InputOption::VALUE_REQUIRED, 'username du compte a traiter')
20
 //      ->addArgument('sites', InputArgument::OPTIONAL, 'Liste exhaustive des site a traiter')
21
 //      ->addArgument('sites', InputArgument::OPTIONAL, 'Liste exhaustive des site a traiter')
21
 //      ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
22
 //      ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
26
   {
27
   {
27
     $doctrine = $this->getContainer()->get('doctrine');
28
     $doctrine = $this->getContainer()->get('doctrine');
28
     $em = $doctrine->getEntityManager();
29
     $em = $doctrine->getEntityManager();
29
-
30
+    
30
     $output->writeln('#');
31
     $output->writeln('#');
31
-    $output->writeln('## Script de recalcul des scores de reputation ##');
32
+    $output->writeln('## Script de recalcul des scores ##');
32
     $output->writeln('#');
33
     $output->writeln('#');
33
 
34
 
34
     $output->writeln('<info>Début du traitement ...</info>');
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
     if (($username = $input->getOption('user')))
49
     if (($username = $input->getOption('user')))
37
     {
50
     {
79
       foreach ($elements as $element)
92
       foreach ($elements as $element)
80
       {
93
       {
81
         $element_points += $element->getPoints();
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
       
168
       
154
       $user->setReputation($points);
169
       $user->setReputation($points);
155
       $em->persist($user);
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
     $favorite = new UsersElementsFavorites();
35
     $favorite = new UsersElementsFavorites();
36
     $favorite->setUser($user);
36
     $favorite->setUser($user);
37
     $favorite->setElement($element);
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
     $this->entity_manager->persist($favorite);
45
     $this->entity_manager->persist($favorite);
46
+    $this->entity_manager->persist($element);
39
     //$this->addReference('user_tag_'.$user->getId().'_'.$tag->getId(), $userTag);
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
   protected $count_report;
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
    * @ORM\Column(type="boolean", nullable=false)
269
    * @ORM\Column(type="boolean", nullable=false)
264
    * @var int 
270
    * @var int 
265
    */
271
    */
819
     $this->points = $points;
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
   public function getVoteGoodIds()
838
   public function getVoteGoodIds()
823
   {
839
   {
824
     return json_decode($this->vote_good_ids, true);
840
     return json_decode($this->vote_good_ids, true);
1052
     $this->slug = $slug;
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
     $security_context = new SecurityContext($added_by_user);
109
     $security_context = new SecurityContext($added_by_user);
110
     if (!$security_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES))
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
     $uea = new UserEventAction($element->getOwner(), $this->container);
118
     $uea = new UserEventAction($element->getOwner(), $this->container);
130
     $security_context = new SecurityContext($removed_by_user);
131
     $security_context = new SecurityContext($removed_by_user);
131
     if (!$security_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES))
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
         $event = new EventElement($this->container);
60
         $event = new EventElement($this->container);
61
         $event->addedToFavorites($element, $user);
61
         $event->addedToFavorites($element, $user);
62
         $em->persist($user);
62
         $em->persist($user);
63
+        $em->persist($element);
63
       }
64
       }
64
       
65
       
65
       // On signale que cet user a modifié sa liste de favoris
66
       // On signale que cet user a modifié sa liste de favoris
129
       $user->setData(User::DATA_FAV_UPDATED, true);
130
       $user->setData(User::DATA_FAV_UPDATED, true);
130
       
131
       
131
       $em->persist($element->getOwner());
132
       $em->persist($element->getOwner());
133
+      $em->persist($element);
132
       $em->remove($fav);
134
       $em->remove($fav);
133
       $em->flush();
135
       $em->flush();
134
     }
136
     }