RecalculateReputationCommand.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Muzich\CoreBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Muzich\CoreBundle\Entity\EventArchive;
  9. use Muzich\CoreBundle\Entity\Element;
  10. class RecalculateReputationCommand extends ContainerAwareCommand
  11. {
  12. protected function configure()
  13. {
  14. $this
  15. ->setName('score:recalculate')
  16. ->setDescription('Recalcul des scores')
  17. ->addOption('user', null, InputOption::VALUE_REQUIRED, 'username du compte a traiter')
  18. // ->addArgument('sites', InputArgument::OPTIONAL, 'Liste exhaustive des site a traiter')
  19. // ->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
  20. ;
  21. }
  22. protected function execute(InputInterface $input, OutputInterface $output)
  23. {
  24. $doctrine = $this->getContainer()->get('doctrine');
  25. $em = $doctrine->getEntityManager();
  26. $output->writeln('#');
  27. $output->writeln('## Script de recalcul des scores ##');
  28. $output->writeln('#');
  29. $output->writeln('<info>Début du traitement ...</info>');
  30. $this->recalculateElementScores($input, $output);
  31. $this->recalculateUserScores($input, $output);
  32. $output->writeln('<info>Saving in database ...</info>');
  33. $em->flush();
  34. $output->writeln('<info>Terminé !</info>');
  35. }
  36. protected function recalculateUserScores(InputInterface $input, OutputInterface $output)
  37. {
  38. $doctrine = $this->getContainer()->get('doctrine');
  39. $em = $doctrine->getEntityManager();
  40. if (($username = $input->getOption('user')))
  41. {
  42. $output->writeln('<info>Utilisateur choisis: '.$username.'</info>');
  43. $user = $em->createQuery(
  44. "SELECT u FROM MuzichCoreBundle:User u"
  45. . " WHERE u.username = :username"
  46. )->setParameter('username', $username)
  47. ->getResult()
  48. ;
  49. if (count($user))
  50. {
  51. $output->writeln('<info>Utilisateur trouvé</info>');
  52. $users = array($user[0]);
  53. }
  54. }
  55. else
  56. {
  57. // Sinon on traite tout les utilisateurs
  58. $users = $em->createQuery(
  59. "SELECT u FROM MuzichCoreBundle:User u"
  60. )
  61. ->getResult()
  62. ;
  63. }
  64. // Pour chaque utilisateur a traiter
  65. foreach ($users as $user)
  66. {
  67. $user_points = 0;
  68. /*
  69. * On calcule pour les éléments
  70. */
  71. $elements = $em->createQuery(
  72. "SELECT e FROM MuzichCoreBundle:Element e"
  73. . " WHERE e.owner = :uid"
  74. )->setParameter('uid', $user->getId())
  75. ->getResult()
  76. ;
  77. //$coef_element_point = $this->getContainer()->getParameter('reputation_element_point_value');
  78. $element_points = 0;
  79. foreach ($elements as $element)
  80. {
  81. $element_points += $element->getPoints();
  82. }
  83. /*
  84. * Calcul pour les utilisateurs suivis
  85. */
  86. $coef_follow = $this->getContainer()->getParameter('reputation_element_follow_value');
  87. $count_follow = 0;
  88. $fol = $em->createQuery(
  89. "SELECT COUNT(f) FROM MuzichCoreBundle:FollowUser f"
  90. . " JOIN f.follower fu"
  91. . " WHERE f.followed = :uid AND fu.email_confirmed = 1"
  92. )->setParameter('uid', $user->getId())
  93. ->getScalarResult()
  94. ;
  95. if (count($fol))
  96. {
  97. if (count($fol[0]))
  98. {
  99. $count_follow = $fol[0][1];
  100. }
  101. }
  102. /*
  103. * Calcul pour les tags proposés sur des éléments
  104. */
  105. $coef_tag_prop = $this->getContainer()->getParameter('reputation_element_tags_element_prop_value');
  106. try {
  107. $count_tag_prop = $em->createQuery(
  108. "SELECT a.count FROM MuzichCoreBundle:EventArchive a"
  109. . " WHERE a.user = :uid AND a.type = :type"
  110. )->setParameters(array(
  111. 'uid' => $user->getId(),
  112. 'type' => EventArchive::PROP_TAGS_ELEMENT_ACCEPTED
  113. ))
  114. ->getSingleScalarResult()
  115. ;
  116. } catch (\Doctrine\ORM\NoResultException $exc) {
  117. $count_tag_prop = 0;
  118. }
  119. $points =
  120. $element_points
  121. + ($count_follow * $coef_follow)
  122. + ($count_tag_prop * $coef_tag_prop)
  123. ;
  124. $user->setReputation($points);
  125. $em->persist($user);
  126. $output->writeln('<info>User "'.$user->getUsername().'": '.$points.' score</info>');
  127. }
  128. }
  129. protected function recalculateElementScores(InputInterface $input, OutputInterface $output)
  130. {
  131. $doctrine = $this->getContainer()->get('doctrine');
  132. $em = $doctrine->getEntityManager();
  133. $elements = $em->createQuery(
  134. "SELECT element FROM MuzichCoreBundle:Element element"
  135. )->getResult();
  136. foreach ($elements as $element)
  137. {
  138. $element_score = $this->getElementScore($element);
  139. $element->setPoints($element_score);
  140. $output->writeln('<info>Element "'.$element->getName().'": '.$element_score.' score</info>');
  141. $em->persist($element);
  142. }
  143. }
  144. protected function getElementScore(Element $element)
  145. {
  146. $element_score = 0;
  147. $element_score += (count($element->getVoteGoodIds())*$this->getContainer()->getParameter('reputation_element_point_value'));
  148. $element_score += ($element->getCountFavorited()*$this->getContainer()->getParameter('reputation_element_favorite_value'));
  149. $element_score += ($element->getCountPlaylisted()*$this->getContainer()->getParameter('reputation_element_added_to_playlist'));
  150. return $element_score;
  151. }
  152. }