Browse Source

Evolution #163: Evenements: Prise en comptes des autres facteurs

bastien 12 years ago
parent
commit
6b3dc25bc1

+ 2 - 0
app/Resources/translations/elements.fr.yml View File

@@ -51,6 +51,8 @@ element:
51 51
       min:              Le commentaire doit être de %limit% caractéres minimum
52 52
       max:              Le commentaire doit être de %limit% caractéres maximum
53 53
       unknow:           Une erreur est survenue
54
+    follow:
55
+      label:            M'avertir en cas de nouveaux commentaires
54 56
   vote:
55 57
     good:               Ce partage mérite un point supplémentaire (cliquez a nouveau pour enlever)
56 58
   

+ 15 - 6
src/Muzich/CommentBundle/Controller/CommentController.php View File

@@ -62,17 +62,20 @@ class CommentController extends Controller
62 62
       )));
63 63
     }
64 64
     
65
+    $follow = false;
66
+    if ($this->getRequest()->request->get('follow') == true)
67
+    {
68
+      $follow = true;
69
+    }
70
+    
65 71
     // On met a jour les commentaires
66 72
     $cm = new CommentsManager($element->getComments());
67
-    $cm->add($this->getUser(), $comment);
73
+    $cm->add($this->getUser(), $comment, $follow);
68 74
     $element->setComments($cm->get());
69 75
     $event = new EventElement($this->container);
70 76
     
71 77
     // Event pour user d'un nouveau comment
72
-    if ($this->getUserId() != $element->getOwner()->getId())
73
-    {
74
-      $event->commentAdded($element);
75
-    }
78
+    $event->commentAdded($element, $this->getUser());
76 79
 
77 80
     $this->getDoctrine()->getEntityManager()->persist($element);
78 81
     $this->getDoctrine()->getEntityManager()->flush();
@@ -241,10 +244,16 @@ class CommentController extends Controller
241 244
         )
242 245
       )));
243 246
     }
247
+    
248
+    $follow = false;
249
+    if ($this->getRequest()->request->get('follow') == true)
250
+    {
251
+      $follow = true;
252
+    }
244 253
       
245 254
     // On met a jour les commentaires
246 255
     $cm = new CommentsManager($element->getComments());
247
-    $cm->update($this->getUser(), $date, $comment);
256
+    $cm->update($this->getUser(), $date, $comment, $follow);
248 257
     $element->setComments($cm->get());
249 258
 
250 259
     $this->getDoctrine()->getEntityManager()->persist($element);

+ 8 - 1
src/Muzich/CommentBundle/Resources/views/Comment/form.html.twig View File

@@ -1,4 +1,5 @@
1 1
 <textarea name="comment">{% if comment_value is defined %}{{ comment_value }}{% endif %}</textarea>
2
+
2 3
 <div class="buttons">
3 4
   <input 
4 5
     type="submit" 
@@ -11,4 +12,10 @@
11 12
     value="{{ cancel_value }}" 
12 13
     class="button cancel"
13 14
   />
14
-</div>
15
+</div>
16
+
17
+{% if not own %}
18
+  <br />
19
+  <input type="checkbox" name="follow" id="input_comment_follow" {% if following %}checked="checked"{% endif %} />
20
+  <label for="input_comment_follow">{{ 'element.comments.follow.label'|trans({}, 'elements') }}</label>
21
+{% endif %}

+ 16 - 16
src/Muzich/CoreBundle/DataFixtures/ORM/LoadElementData.php View File

@@ -217,7 +217,7 @@ class LoadElementData  extends AbstractFixture implements OrderedFixtureInterfac
217 217
     $cm = new CommentsManager();
218 218
     $cm->add($joelle, "J'aime bien quand ça tape. Ca rapelle ".
219 219
       "le grincement sinistre des volets de vieilles ".
220
-      "maisons. D'ailleur j'ai repeint mon mur des shiots !", $this->dateD(180));
220
+      "maisons. D'ailleur j'ai repeint mon mur des shiots !", false, $this->dateD(180));
221 221
     
222 222
     $this->createElement('azyd_azylum_1', 'AZYD AZYLUM Live au Café Provisoire', 
223 223
       'http://www.youtube.com/watch?v=8AXhRXAt2E4', 
@@ -228,19 +228,19 @@ class LoadElementData  extends AbstractFixture implements OrderedFixtureInterfac
228 228
     );
229 229
     
230 230
     $cm = new CommentsManager();
231
-    $cm->add($bux, "Je commenteuuh nanana 1", $this->dateD(180));
232
-    $cm->add($paul, "Je répond 2", $this->dateD(180));
233
-    $cm->add($bux, "Je répond 3", $this->dateD(179));
234
-    $cm->add($paul, "Je répond 4", $this->dateD(178));
235
-    $cm->add($bux, "Je répond 5", $this->dateD(177));
236
-    $cm->add($paul, "Je répond 6", $this->dateD(176));
237
-    $cm->add($bux, "Je répond 7", $this->dateD(175));
238
-    $cm->add($paul, "Je répond 8", $this->dateD(174));
239
-    $cm->add($bux, "Je répond 9", $this->dateD(173));
240
-    $cm->add($paul, "Je répond 10", $this->dateD(172));
241
-    $cm->add($bux, "Je répond 11", $this->dateD(161));
242
-    $cm->add($paul, "Je répond 12", $this->dateD(150));
243
-    $cm->add($bux, "Je répond 13", $this->dateD(140));
231
+    $cm->add($bux, "Je commenteuuh nanana 1", false, $this->dateD(180));
232
+    $cm->add($paul, "Je répond 2", false, $this->dateD(180));
233
+    $cm->add($bux, "Je répond 3", false, $this->dateD(179));
234
+    $cm->add($paul, "Je répond 4", false, $this->dateD(178));
235
+    $cm->add($bux, "Je répond 5", false, $this->dateD(177));
236
+    $cm->add($paul, "Je répond 6", false, $this->dateD(176));
237
+    $cm->add($bux, "Je répond 7", false, $this->dateD(175));
238
+    $cm->add($paul, "Je répond 8", false, $this->dateD(174));
239
+    $cm->add($bux, "Je répond 9", false, $this->dateD(173));
240
+    $cm->add($paul, "Je répond 10", false, $this->dateD(172));
241
+    $cm->add($bux, "Je répond 11", false, $this->dateD(161));
242
+    $cm->add($paul, "Je répond 12", false, $this->dateD(150));
243
+    $cm->add($bux, "Je répond 13", false, $this->dateD(140));
244 244
     
245 245
     $this->createElement('babylon_pression_1', 'Babylon Pression - Des Tasers et des Pauvres', 
246 246
       'http://www.youtube.com/watch?v=XWkbaHxRvds&feature=related', 
@@ -251,8 +251,8 @@ class LoadElementData  extends AbstractFixture implements OrderedFixtureInterfac
251 251
     );
252 252
     
253 253
     $cm = new CommentsManager();
254
-    $cm->add($bux, "C'est trop bon hein ?", $this->dateD(180));
255
-    $cm->add($paul, "C'est pas mal en effet", $this->dateD(180));
254
+    $cm->add($bux, "C'est trop bon hein ?", false, $this->dateD(180));
255
+    $cm->add($paul, "C'est pas mal en effet", false, $this->dateD(180));
256 256
         
257 257
     $this->createElement('ed_cox_1', 'Ed Cox - La fanfare des teuffeurs (Hardcordian)', 
258 258
       'http://www.youtube.com/watch?v=Lk1gnh-JCDs&feature=related', 

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

@@ -9,6 +9,7 @@ use Doctrine\ORM\EntityManager;
9 9
 use Symfony\Component\Validator\Constraints as Assert;
10 10
 use Muzich\CoreBundle\Validator as MuzichAssert;
11 11
 use Muzich\CoreBundle\Entity\Tag;
12
+use Muzich\CoreBundle\Managers\CommentsManager;
12 13
 
13 14
 /**
14 15
  * L'Element est l'Element central de l'application. C'est cet
@@ -625,4 +626,16 @@ class Element
625 626
     return false;
626 627
   }
627 628
   
629
+  /**
630
+   * Retourne vrai si l'utilisateur a demandé qa suivre les commentaires
631
+   *  
632
+   * @param int $user_id identifiant de l'utilisateur
633
+   * @return boolean 
634
+   */
635
+  public function userFollowComments($user_id)
636
+  {
637
+    $cm = new CommentsManager($this->getComments());
638
+    return $cm->userFollow($user_id);
639
+  }
640
+  
628 641
 }

+ 85 - 4
src/Muzich/CoreBundle/Managers/CommentsManager.php View File

@@ -17,6 +17,11 @@ class CommentsManager
17 17
   
18 18
   public function __construct($comments = array())
19 19
   {
20
+    if ($comments == null)
21
+    {
22
+      $comments = array();
23
+    }
24
+    
20 25
     $this->comments = $comments;
21 26
   }
22 27
   
@@ -24,10 +29,11 @@ class CommentsManager
24 29
    * Ajouter un commentaire au tableau.
25 30
    * 
26 31
    * @param \Muzich\CoreBundle\Entity\User $user
27
-   * @param String $comment
28
-   * @param String $date 
32
+   * @param String $comment Contenu du commentaire
33
+   * @param boolean $follow L'utilisateur désire être avertis des nouveaux commentaires
34
+   * @param String $date date de l'envoi
29 35
    */
30
-  public function add($user, $comment, $date = null)
36
+  public function add($user, $comment, $follow = false, $date = null)
31 37
   {
32 38
     if (!$date)
33 39
     {
@@ -40,9 +46,15 @@ class CommentsManager
40 46
         "s" => $user->getSlug(),
41 47
         "n" => $user->getName()
42 48
       ),
49
+      "f" => $follow,
43 50
       "d" => $date,
44 51
       "c" => $comment
45 52
     );
53
+    
54
+    if (!$follow)
55
+    {
56
+      $this->userSetFollowToFalse($user->getId());
57
+    }
46 58
   }
47 59
   
48 60
   /**
@@ -62,14 +74,17 @@ class CommentsManager
62 74
    * @param \Muzich\CoreBundle\Entity\User $user
63 75
    * @param date $date (Y-m-d H:i:s u)
64 76
    * @param string $comment_c 
77
+   * @param boolean $follow
65 78
    */
66
-  public function update($user, $date, $comment_c)
79
+  public function update($user, $date, $comment_c, $follow)
67 80
   {
68 81
     $comments = array();
82
+    $found = false;
69 83
     foreach ($this->comments as $comment)
70 84
     {
71 85
       if ($comment['u']['i'] == $user->getId() && $comment['d'] == $date)
72 86
       {
87
+        $found = true;
73 88
         $comments[] = array(
74 89
           "u" => array(
75 90
             "i" => $user->getId(),
@@ -77,6 +92,7 @@ class CommentsManager
77 92
             "n" => $user->getName()
78 93
           ),
79 94
           "e" => date('Y-m-d H:i:s u'),
95
+          "f" => $follow,
80 96
           "d" => $date,
81 97
           "c" => $comment_c
82 98
         );
@@ -88,6 +104,11 @@ class CommentsManager
88 104
     }
89 105
     
90 106
     $this->comments = $comments;
107
+    
108
+    if (!$follow && $found)
109
+    {
110
+      $this->userSetFollowToFalse($user->getId());
111
+    }
91 112
   }
92 113
   
93 114
   /**
@@ -151,4 +172,64 @@ class CommentsManager
151 172
     return $this->comments[$index];
152 173
   }
153 174
   
175
+  /**
176
+   * Regarde si il existe un commentaire pour cet utilisateur où il demande
177
+   * a suivre les commentaires.
178
+   * 
179
+   * @param int $user_id
180
+   * @return boolean 
181
+   */
182
+  public function userFollow($user_id)
183
+  {
184
+    foreach ($this->comments as $i => $comment)
185
+    {
186
+      if ($comment['u']['i'] == $user_id && $comment['f'] == true)
187
+      {
188
+        return true;
189
+      }
190
+    }
191
+    return false;
192
+  }
193
+  
194
+  /**
195
+   * Passe tout les commentaire de cet utilisateur en follow = false
196
+   * 
197
+   * @param int $user_id 
198
+   */
199
+  private function userSetFollowToFalse($user_id)
200
+  {
201
+    $comments = array();
202
+    foreach ($this->comments as $comment)
203
+    {
204
+      if ($comment['u']['i'] == $user_id)
205
+      {
206
+        $comment['f'] = false;
207
+      }
208
+      $comments[] = $comment;
209
+    }
210
+    $this->comments = $comments;
211
+  }
212
+  
213
+  /**
214
+   *  Retourne les ids utilisateurs ayant demandé a être avertis des nouveaux
215
+   * commentaires.
216
+   * 
217
+   * @return array ids des utilisateurs
218
+   */
219
+  public function getFollowersIds()
220
+  {
221
+    $ids = array();
222
+    foreach ($this->comments as $comment)
223
+    {
224
+      if ($comment['f'] == true)
225
+      {
226
+        if (!in_array($comment['u']['i'], $ids))
227
+        {
228
+          $ids[] = $comment['u']['i'];
229
+        }
230
+      }
231
+    }
232
+    return $ids;
233
+  }
234
+  
154 235
 }

+ 37 - 4
src/Muzich/CoreBundle/Propagator/EventElement.php View File

@@ -7,6 +7,8 @@ use Muzich\CoreBundle\Entity\Element;
7 7
 use Muzich\CoreBundle\Actions\User\Event as UserEventAction;
8 8
 use Muzich\CoreBundle\Actions\User\Reputation as UserReputation;
9 9
 use Muzich\CoreBundle\Entity\Event;
10
+use Muzich\CoreBundle\Entity\User;
11
+use Muzich\CoreBundle\Managers\CommentsManager;
10 12
 
11 13
 /**
12 14
  * Propagateur d'événement concernant les éléments
@@ -21,14 +23,45 @@ class EventElement extends EventPropagator
21 23
    * événement. Actuellement il:
22 24
    * * Met a jour ou créer un objet événement (nouveau commentaire) pour le
23 25
    *   propriétaire de l'élément.
26
+   * * Met a jour ou créer un objet événement (nouveau commentaire) pour les
27
+   *   utilisateurs qui follow cet élément.
24 28
    * 
25 29
    * @param Element $element 
26 30
    */
27
-  public function commentAdded(Element $element)
31
+  public function commentAdded(Element $element, User $user)
28 32
   {
29
-    $uea = new UserEventAction($element->getOwner(), $this->container);
30
-    $event = $uea->proceed(Event::TYPE_COMMENT_ADDED_ELEMENT, $element->getId());
31
-    $this->container->get('doctrine')->getEntityManager()->persist($event);
33
+    // On avertis le propriétaire si ce n'est pas lui même qui vient de commenter
34
+    if ($user->getId() != $element->getOwner()->getId())
35
+    {
36
+      $uea = new UserEventAction($element->getOwner(), $this->container);
37
+      $event = $uea->proceed(Event::TYPE_COMMENT_ADDED_ELEMENT, $element->getId());
38
+      $this->container->get('doctrine')->getEntityManager()->persist($event);
39
+    }
40
+    
41
+    // Pour chaque utilisateur qui a demandé a être avertis d'un nouveau commentaire
42
+    $cm = new CommentsManager($element->getComments());
43
+    $uids = $cm->getFollowersIds();
44
+    
45
+    if (count($uids))
46
+    {
47
+      $users = $this->container->get('doctrine')->getEntityManager()
48
+        ->getRepository('MuzichCoreBundle:User')
49
+        ->getUsersWithIds($uids)
50
+      ;
51
+      if (count($users))
52
+      {
53
+        foreach ($users as $user_c)
54
+        {
55
+          // On n'avertis pas l'utilisateur de son propre commentaire
56
+          if ($user->getId() != $user_c->getId())
57
+          {
58
+            $uea = new UserEventAction($user_c, $this->container);
59
+            $event = $uea->proceed(Event::TYPE_COMMENT_ADDED_ELEMENT, $element->getId());
60
+            $this->container->get('doctrine')->getEntityManager()->persist($event);
61
+          }
62
+        }
63
+      }
64
+    }
32 65
   }
33 66
   
34 67
   /**

+ 16 - 0
src/Muzich/CoreBundle/Repository/UserRepository.php View File

@@ -241,4 +241,20 @@ class UserRepository extends EntityRepository
241 241
     return 0;
242 242
   }
243 243
   
244
+  /**
245
+   * Retourne les objets utilisateurs correspondant aux ids transmis.
246
+   *
247
+   * @param array $ids
248
+   * @return Collection 
249
+   */
250
+  public function getUsersWithIds($ids)
251
+  {
252
+    return $this->getEntityManager()
253
+      ->createQuery('SELECT u FROM MuzichCoreBundle:User u'
254
+        .' WHERE u.id IN (:uids)')
255
+      ->setParameter('uids', $ids)
256
+      ->getResult()
257
+    ;
258
+  }
259
+  
244 260
 }  

+ 3 - 1
src/Muzich/CoreBundle/Resources/views/SearchElement/element.html.twig View File

@@ -220,7 +220,9 @@
220 220
     >
221 221
       {% include "MuzichCommentBundle:Comment:form.html.twig" with {
222 222
         'submit_value' : 'element.comments.add_submit'|trans({}, 'elements'),
223
-        'cancel_value' : 'element.comments.add_cancel'|trans({}, 'elements')
223
+        'cancel_value' : 'element.comments.add_cancel'|trans({}, 'elements'),
224
+        'following'    : element.userFollowComments(app.user.id),
225
+        'own'          : (app.user.id == element.owner.id)
224 226
       } %}
225 227
     </form>
226 228