Browse Source

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

bastien 13 years ago
parent
commit
6b3dc25bc1

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

51
       min:              Le commentaire doit être de %limit% caractéres minimum
51
       min:              Le commentaire doit être de %limit% caractéres minimum
52
       max:              Le commentaire doit être de %limit% caractéres maximum
52
       max:              Le commentaire doit être de %limit% caractéres maximum
53
       unknow:           Une erreur est survenue
53
       unknow:           Une erreur est survenue
54
+    follow:
55
+      label:            M'avertir en cas de nouveaux commentaires
54
   vote:
56
   vote:
55
     good:               Ce partage mérite un point supplémentaire (cliquez a nouveau pour enlever)
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
       )));
62
       )));
63
     }
63
     }
64
     
64
     
65
+    $follow = false;
66
+    if ($this->getRequest()->request->get('follow') == true)
67
+    {
68
+      $follow = true;
69
+    }
70
+    
65
     // On met a jour les commentaires
71
     // On met a jour les commentaires
66
     $cm = new CommentsManager($element->getComments());
72
     $cm = new CommentsManager($element->getComments());
67
-    $cm->add($this->getUser(), $comment);
73
+    $cm->add($this->getUser(), $comment, $follow);
68
     $element->setComments($cm->get());
74
     $element->setComments($cm->get());
69
     $event = new EventElement($this->container);
75
     $event = new EventElement($this->container);
70
     
76
     
71
     // Event pour user d'un nouveau comment
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
     $this->getDoctrine()->getEntityManager()->persist($element);
80
     $this->getDoctrine()->getEntityManager()->persist($element);
78
     $this->getDoctrine()->getEntityManager()->flush();
81
     $this->getDoctrine()->getEntityManager()->flush();
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
     // On met a jour les commentaires
254
     // On met a jour les commentaires
246
     $cm = new CommentsManager($element->getComments());
255
     $cm = new CommentsManager($element->getComments());
247
-    $cm->update($this->getUser(), $date, $comment);
256
+    $cm->update($this->getUser(), $date, $comment, $follow);
248
     $element->setComments($cm->get());
257
     $element->setComments($cm->get());
249
 
258
 
250
     $this->getDoctrine()->getEntityManager()->persist($element);
259
     $this->getDoctrine()->getEntityManager()->persist($element);

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

1
 <textarea name="comment">{% if comment_value is defined %}{{ comment_value }}{% endif %}</textarea>
1
 <textarea name="comment">{% if comment_value is defined %}{{ comment_value }}{% endif %}</textarea>
2
+
2
 <div class="buttons">
3
 <div class="buttons">
3
   <input 
4
   <input 
4
     type="submit" 
5
     type="submit" 
11
     value="{{ cancel_value }}" 
12
     value="{{ cancel_value }}" 
12
     class="button cancel"
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
     $cm = new CommentsManager();
217
     $cm = new CommentsManager();
218
     $cm->add($joelle, "J'aime bien quand ça tape. Ca rapelle ".
218
     $cm->add($joelle, "J'aime bien quand ça tape. Ca rapelle ".
219
       "le grincement sinistre des volets de vieilles ".
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
     $this->createElement('azyd_azylum_1', 'AZYD AZYLUM Live au Café Provisoire', 
222
     $this->createElement('azyd_azylum_1', 'AZYD AZYLUM Live au Café Provisoire', 
223
       'http://www.youtube.com/watch?v=8AXhRXAt2E4', 
223
       'http://www.youtube.com/watch?v=8AXhRXAt2E4', 
228
     );
228
     );
229
     
229
     
230
     $cm = new CommentsManager();
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
     $this->createElement('babylon_pression_1', 'Babylon Pression - Des Tasers et des Pauvres', 
245
     $this->createElement('babylon_pression_1', 'Babylon Pression - Des Tasers et des Pauvres', 
246
       'http://www.youtube.com/watch?v=XWkbaHxRvds&feature=related', 
246
       'http://www.youtube.com/watch?v=XWkbaHxRvds&feature=related', 
251
     );
251
     );
252
     
252
     
253
     $cm = new CommentsManager();
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
     $this->createElement('ed_cox_1', 'Ed Cox - La fanfare des teuffeurs (Hardcordian)', 
257
     $this->createElement('ed_cox_1', 'Ed Cox - La fanfare des teuffeurs (Hardcordian)', 
258
       'http://www.youtube.com/watch?v=Lk1gnh-JCDs&feature=related', 
258
       'http://www.youtube.com/watch?v=Lk1gnh-JCDs&feature=related', 

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

9
 use Symfony\Component\Validator\Constraints as Assert;
9
 use Symfony\Component\Validator\Constraints as Assert;
10
 use Muzich\CoreBundle\Validator as MuzichAssert;
10
 use Muzich\CoreBundle\Validator as MuzichAssert;
11
 use Muzich\CoreBundle\Entity\Tag;
11
 use Muzich\CoreBundle\Entity\Tag;
12
+use Muzich\CoreBundle\Managers\CommentsManager;
12
 
13
 
13
 /**
14
 /**
14
  * L'Element est l'Element central de l'application. C'est cet
15
  * L'Element est l'Element central de l'application. C'est cet
625
     return false;
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
   
17
   
18
   public function __construct($comments = array())
18
   public function __construct($comments = array())
19
   {
19
   {
20
+    if ($comments == null)
21
+    {
22
+      $comments = array();
23
+    }
24
+    
20
     $this->comments = $comments;
25
     $this->comments = $comments;
21
   }
26
   }
22
   
27
   
24
    * Ajouter un commentaire au tableau.
29
    * Ajouter un commentaire au tableau.
25
    * 
30
    * 
26
    * @param \Muzich\CoreBundle\Entity\User $user
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
     if (!$date)
38
     if (!$date)
33
     {
39
     {
40
         "s" => $user->getSlug(),
46
         "s" => $user->getSlug(),
41
         "n" => $user->getName()
47
         "n" => $user->getName()
42
       ),
48
       ),
49
+      "f" => $follow,
43
       "d" => $date,
50
       "d" => $date,
44
       "c" => $comment
51
       "c" => $comment
45
     );
52
     );
53
+    
54
+    if (!$follow)
55
+    {
56
+      $this->userSetFollowToFalse($user->getId());
57
+    }
46
   }
58
   }
47
   
59
   
48
   /**
60
   /**
62
    * @param \Muzich\CoreBundle\Entity\User $user
74
    * @param \Muzich\CoreBundle\Entity\User $user
63
    * @param date $date (Y-m-d H:i:s u)
75
    * @param date $date (Y-m-d H:i:s u)
64
    * @param string $comment_c 
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
     $comments = array();
81
     $comments = array();
82
+    $found = false;
69
     foreach ($this->comments as $comment)
83
     foreach ($this->comments as $comment)
70
     {
84
     {
71
       if ($comment['u']['i'] == $user->getId() && $comment['d'] == $date)
85
       if ($comment['u']['i'] == $user->getId() && $comment['d'] == $date)
72
       {
86
       {
87
+        $found = true;
73
         $comments[] = array(
88
         $comments[] = array(
74
           "u" => array(
89
           "u" => array(
75
             "i" => $user->getId(),
90
             "i" => $user->getId(),
77
             "n" => $user->getName()
92
             "n" => $user->getName()
78
           ),
93
           ),
79
           "e" => date('Y-m-d H:i:s u'),
94
           "e" => date('Y-m-d H:i:s u'),
95
+          "f" => $follow,
80
           "d" => $date,
96
           "d" => $date,
81
           "c" => $comment_c
97
           "c" => $comment_c
82
         );
98
         );
88
     }
104
     }
89
     
105
     
90
     $this->comments = $comments;
106
     $this->comments = $comments;
107
+    
108
+    if (!$follow && $found)
109
+    {
110
+      $this->userSetFollowToFalse($user->getId());
111
+    }
91
   }
112
   }
92
   
113
   
93
   /**
114
   /**
151
     return $this->comments[$index];
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
 use Muzich\CoreBundle\Actions\User\Event as UserEventAction;
7
 use Muzich\CoreBundle\Actions\User\Event as UserEventAction;
8
 use Muzich\CoreBundle\Actions\User\Reputation as UserReputation;
8
 use Muzich\CoreBundle\Actions\User\Reputation as UserReputation;
9
 use Muzich\CoreBundle\Entity\Event;
9
 use Muzich\CoreBundle\Entity\Event;
10
+use Muzich\CoreBundle\Entity\User;
11
+use Muzich\CoreBundle\Managers\CommentsManager;
10
 
12
 
11
 /**
13
 /**
12
  * Propagateur d'événement concernant les éléments
14
  * Propagateur d'événement concernant les éléments
21
    * événement. Actuellement il:
23
    * événement. Actuellement il:
22
    * * Met a jour ou créer un objet événement (nouveau commentaire) pour le
24
    * * Met a jour ou créer un objet événement (nouveau commentaire) pour le
23
    *   propriétaire de l'élément.
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
    * @param Element $element 
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
     return 0;
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
     >
220
     >
221
       {% include "MuzichCommentBundle:Comment:form.html.twig" with {
221
       {% include "MuzichCommentBundle:Comment:form.html.twig" with {
222
         'submit_value' : 'element.comments.add_submit'|trans({}, 'elements'),
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
     </form>
227
     </form>
226
       
228