Browse Source

Evolution #150: Reputation

bastien 12 years ago
parent
commit
282be2b107
1 changed files with 89 additions and 0 deletions
  1. 89 0
      src/Muzich/CoreBundle/Command/RecalculateReputationCommand.php

+ 89 - 0
src/Muzich/CoreBundle/Command/RecalculateReputationCommand.php View File

@@ -0,0 +1,89 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Command;
4
+
5
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
+use Symfony\Component\Console\Input\InputArgument;
7
+use Symfony\Component\Console\Input\InputInterface;
8
+use Symfony\Component\Console\Input\InputOption;
9
+use Symfony\Component\Console\Output\OutputInterface;
10
+
11
+class RecalculateReputationCommand extends ContainerAwareCommand
12
+{
13
+  protected function configure()
14
+  {
15
+    $this
16
+      ->setName('users:reputation:recalculate')
17
+      ->setDescription('Recalcul des scores de reputation')
18
+      ->addOption('user', null, InputOption::VALUE_REQUIRED, 'username du compte a traiter')
19
+//      ->addArgument('sites', InputArgument::OPTIONAL, 'Liste exhaustive des site a traiter')
20
+//      ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
21
+    ;
22
+  }
23
+
24
+  protected function execute(InputInterface $input, OutputInterface $output)
25
+  {
26
+    $doctrine = $this->getContainer()->get('doctrine');
27
+    $em = $doctrine->getEntityManager();
28
+
29
+    $output->writeln('#');
30
+    $output->writeln('## Script de recalcul des scores de reputation ##');
31
+    $output->writeln('#');
32
+
33
+    $output->writeln('<info>Début du traitement ...</info>');
34
+    
35
+    if (($username = $input->getOption('user')))
36
+    {
37
+      $output->writeln('<info>Utilisateur choisis: '.$username.'</info>');
38
+      $user = $em->createQuery(
39
+        "SELECT u FROM MuzichCoreBundle:User u"
40
+        . " WHERE u.username = :username"
41
+      )->setParameter('username', $username)
42
+       ->getResult()      
43
+      ;
44
+      
45
+      if (count($user))
46
+      {
47
+        $output->writeln('<info>Utilisateur trouvé</info>');
48
+        $users = array($user[0]);
49
+      }
50
+    }
51
+    else
52
+    {
53
+      // Sinon on traite tout les utilisateurs
54
+      $users = $em->createQuery(
55
+        "SELECT u FROM MuzichCoreBundle:User u"
56
+      )
57
+       ->getResult()      
58
+      ;
59
+    }
60
+    
61
+    // Pour chaque utilisateur a traiter
62
+    foreach ($users as $user)
63
+    {
64
+      $user_points = 0;
65
+      
66
+      // On calcule pour les éléments
67
+      $elements = $em->createQuery(
68
+        "SELECT e FROM MuzichCoreBundle:Element e"
69
+        . " WHERE e.owner = :uid"
70
+      )->setParameter('uid', $user->getId())
71
+       ->getResult()      
72
+      ;
73
+      
74
+      $coef_element_point = $this->getContainer()->getParameter('reputation_element_point_value');
75
+      $element_points = 0;
76
+      foreach ($elements as $element)
77
+      {
78
+        $element_points += $element->getPoints();
79
+      }
80
+      
81
+      $user->setReputation($element_points * $coef_element_point);
82
+      $em->persist($user);
83
+    }
84
+    
85
+    $output->writeln('<info>Enregistrement en base ...</info>');
86
+    $em->flush();
87
+    $output->writeln('<info>Terminé !</info>');
88
+  }
89
+}