Kaynağa Gözat

failed stage: score for playlist

Bastien Sevajol 11 yıl önce
ebeveyn
işleme
e31583ffec

+ 51 - 6
src/Muzich/CoreBundle/Command/RecalculateReputationCommand.php Dosyayı Görüntüle

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
+    $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 Dosyayı Görüntüle

10
 use Muzich\CoreBundle\Entity\UsersElementsFavorites;
10
 use Muzich\CoreBundle\Entity\UsersElementsFavorites;
11
 use Muzich\CoreBundle\Entity\User;
11
 use Muzich\CoreBundle\Entity\User;
12
 use Muzich\CoreBundle\Managers\PlaylistManager;
12
 use Muzich\CoreBundle\Managers\PlaylistManager;
13
+use Muzich\CoreBundle\Propagator\EventElement;
13
 
14
 
14
 class LoadUsersElementsFavoritesData  extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
15
 class LoadUsersElementsFavoritesData  extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
15
 {
16
 {
48
     
49
     
49
     $playlist_manager->addElementsToPlaylist($elements, $playlist);
50
     $playlist_manager->addElementsToPlaylist($elements, $playlist);
50
     $this->entity_manager->persist($playlist);
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
     return $playlist;
61
     return $playlist;
52
   }
62
   }
53
   
63
   

+ 61 - 0
src/Muzich/CoreBundle/Entity/Element.php Dosyayı Görüntüle

260
   protected $count_report;
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
    * @ORM\Column(type="boolean", nullable=false)
278
    * @ORM\Column(type="boolean", nullable=false)
264
    * @var int 
279
    * @var int 
265
    */
280
    */
1052
     $this->slug = $slug;
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 Dosyayı Görüntüle

264
     return $elements_manager->have($element);
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
   public function haveElementId($element_id)
273
   public function haveElementId($element_id)
268
   {
274
   {
269
     $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
275
     $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
270
     return $elements_manager->haveRefId($element_id);
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
   public function haveTagId($tag_id)
285
   public function haveTagId($tag_id)
274
   {
286
   {
275
     $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
287
     $tags_manager = new TagCollectionManager(json_decode($this->tags, true));

+ 8 - 0
src/Muzich/CoreBundle/Managers/PlaylistManager.php Dosyayı Görüntüle

239
     $this->entity_manager->persist($playlist);
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 Dosyayı Görüntüle

13
 use Muzich\CoreBundle\Managers\EventArchiveManager;
13
 use Muzich\CoreBundle\Managers\EventArchiveManager;
14
 use Muzich\CoreBundle\Entity\EventArchive;
14
 use Muzich\CoreBundle\Entity\EventArchive;
15
 use Muzich\CoreBundle\Security\Context as SecurityContext;
15
 use Muzich\CoreBundle\Security\Context as SecurityContext;
16
+use Muzich\CoreBundle\Entity\Playlist;
16
 
17
 
17
 /**
18
 /**
18
  * Propagateur d'événement concernant les éléments
19
  * Propagateur d'événement concernant les éléments
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
    * Des tags viennent d'être proposé a un élément
202
    * Des tags viennent d'être proposé a un élément
141
    *
203
    *

+ 8 - 0
src/Muzich/CoreBundle/Propagator/EventPropagator.php Dosyayı Görüntüle

3
 namespace Muzich\CoreBundle\Propagator;
3
 namespace Muzich\CoreBundle\Propagator;
4
 
4
 
5
 use Symfony\Component\DependencyInjection\Container;
5
 use Symfony\Component\DependencyInjection\Container;
6
+use Doctrine\ORM\EntityManager;
6
 
7
 
7
 /**
8
 /**
8
  * Description of EventPropagator
9
  * Description of EventPropagator
17
   {
18
   {
18
     $this->container = $container;
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 Dosyayı Görüntüle

216
     return $this->container->get('form.csrf_provider')->generateCsrfToken($intention);
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
     $parameters = array_merge($parameters, array('token' => $this->token($intention)));
221
     $parameters = array_merge($parameters, array('token' => $this->token($intention)));
222
     return $this->container->get('router')->generate($route, $parameters, $absolute);
222
     return $this->container->get('router')->generate($route, $parameters, $absolute);

+ 23 - 0
src/Muzich/CoreBundle/lib/Collection/CollectionManager.php Dosyayı Görüntüle

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
   public function have($object)
59
   public function have($object)
45
   {
60
   {
46
     $method_name = 'get' . $this->object_reference_attribute;
61
     $method_name = 'get' . $this->object_reference_attribute;
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
   public function index($object)
118
   public function index($object)
96
   {
119
   {
97
     $method_name = 'get' . $this->object_reference_attribute;
120
     $method_name = 'get' . $this->object_reference_attribute;

+ 22 - 0
src/Muzich/PlaylistBundle/Controller/EditController.php Dosyayı Görüntüle

5
 use Muzich\CoreBundle\lib\Controller;
5
 use Muzich\CoreBundle\lib\Controller;
6
 use Symfony\Component\HttpFoundation\Request;
6
 use Symfony\Component\HttpFoundation\Request;
7
 use Muzich\CoreBundle\Security\Context as SecurityContext;
7
 use Muzich\CoreBundle\Security\Context as SecurityContext;
8
+use Muzich\CoreBundle\Propagator\EventElement;
8
 
9
 
9
 class EditController extends Controller
10
 class EditController extends Controller
10
 {
11
 {
31
     if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
32
     if (!$this->tokenIsCorrect() || !($playlist = $playlist_manager->findOwnedPlaylistWithId($playlist_id, $this->getUser())))
32
       return $this->jsonNotFoundResponse();
33
       return $this->jsonNotFoundResponse();
33
     
34
     
35
+    $element = $playlist_manager->getElementWithIndex($index);
34
     $playlist_manager->removePlaylistElementWithIndex($playlist, $index);
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
     $this->flush();
42
     $this->flush();
36
     return $this->jsonSuccessResponse();
43
     return $this->jsonSuccessResponse();
37
   }
44
   }
47
       return $this->jsonNotFoundResponse();
54
       return $this->jsonNotFoundResponse();
48
     
55
     
49
     $playlist_manager->addElementToPlaylist($element, $playlist);
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
     $this->flush();
62
     $this->flush();
51
     return $this->jsonSuccessResponse();
63
     return $this->jsonSuccessResponse();
52
   }
64
   }
64
     if ($form->isValid())
76
     if ($form->isValid())
65
     {
77
     {
66
       $this->getPlaylistManager()->addElementToPlaylist($element, $form->getData());
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
       $this->flush();
84
       $this->flush();
68
       return $this->jsonSuccessResponse();
85
       return $this->jsonSuccessResponse();
69
     }
86
     }
90
     $new_playlist = $this->getPlaylistManager()->copyPlaylist($this->getUser(), $playlist);
107
     $new_playlist = $this->getPlaylistManager()->copyPlaylist($this->getUser(), $playlist);
91
     $this->getPlaylistManager()->addElementToPlaylist($element, $new_playlist);
108
     $this->getPlaylistManager()->addElementToPlaylist($element, $new_playlist);
92
     $this->getPlaylistManager()->removePickedPlaylistToUser($this->getUser(), $playlist);
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
     $this->flush();
115
     $this->flush();
94
     
116
     
95
     return $this->jsonSuccessResponse();
117
     return $this->jsonSuccessResponse();