Browse Source

Evolution #176: Modération commentaire

bastien 12 years ago
parent
commit
a1b7a3d3d6

+ 4 - 1
app/Resources/translations/adminui.fr.yml View File

@@ -9,4 +9,7 @@ moderate:
9 9
     title:          Elements
10 10
     action:
11 11
       delete:       Supprimer
12
-      clean:         Tout est ok
12
+      clean:         Tout est ok
13
+  comments:
14
+    title:          Commentaires
15
+    to_moderate:    %count% éléments concernés

+ 137 - 1
src/Muzich/AdminBundle/Controller/ModerateController.php View File

@@ -9,6 +9,7 @@ use Muzich\CoreBundle\Entity\UsersTagsFavorites;
9 9
 use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
10 10
 use Muzich\CoreBundle\Managers\TagManager;
11 11
 use Muzich\CoreBundle\Propagator\EventElement;
12
+use Muzich\CoreBundle\Managers\CommentsManager;
12 13
 
13 14
 class ModerateController extends Controller
14 15
 {
@@ -23,10 +24,13 @@ class ModerateController extends Controller
23 24
       ->countToModerate();
24 25
     $count_elements = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
25 26
       ->countToModerate();
27
+    $count_comments = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
28
+      ->countForCommentToModerate();
26 29
     
27 30
     return array(
28 31
       'count_tags' => $count_tags,
29
-      'count_elements' => $count_elements
32
+      'count_elements' => $count_elements,
33
+      'count_comments' => $count_comments
30 34
     );
31 35
   }
32 36
     
@@ -220,4 +224,136 @@ class ModerateController extends Controller
220 224
     ));
221 225
   }
222 226
   
227
+  /**
228
+   *
229
+   * @Template()
230
+   */
231
+  public function commentsAction()
232
+  {
233
+    // Récupération des elements
234
+    $elements = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
235
+      ->getForCommentToModerate();
236
+    
237
+    $comments = array();
238
+    foreach ($elements as $element)
239
+    {
240
+      $cm = new CommentsManager($element->getComments());
241
+      foreach ($cm->getAlertedComments() as $comment)
242
+      {
243
+        $comments[] = array(
244
+          'element_id' => $element->getId(),
245
+          'comment'    => $comment
246
+        );
247
+      }
248
+      
249
+    }
250
+    
251
+    return array(
252
+      'comments' => $comments
253
+    );
254
+  }
255
+  
256
+  /**
257
+   * Considérer le commentaire signalé comme étant tout a fait acceptable
258
+   * 
259
+   * Ceci induit:
260
+   * * Que le commentaire en question ne soit plus signalé 
261
+   * * Que le ou les ids d'utilisateurs qui l'ont signalé comme soit "pénalisé
262
+   * 
263
+   * @param int $element_id
264
+   * @param date $date 
265
+   * 
266
+   * @return Response
267
+   */
268
+  public function commentCleanAction($element_id, $date)
269
+  {
270
+    if (($response = $this->mustBeConnected(true)))
271
+    {
272
+      return $response;
273
+    }
274
+    
275
+    if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
276
+      ->findOneById($element_id))
277
+    )
278
+    {
279
+      return $this->jsonResponse(array(
280
+        'status' => 'error',
281
+        'errors' => array('NotFound')
282
+      ));
283
+    }
284
+    
285
+    $cm = new CommentsManager($element->getComments());
286
+    // On nettoie le commentaire et on récupère les ids des "signaleurs"
287
+    $ids = $cm->cleanAlertsOnComment($date);
288
+    $element->setComments($cm->get());
289
+    $element->setCountCommentReport($cm->countCommentAlert());
290
+    
291
+    $this->getDoctrine()->getEntityManager()->persist($element);
292
+    
293
+    // On récupère les user qui ont signalés ce commentaire
294
+    $users = $this->getDoctrine()->getEntityManager()
295
+      ->createQuery('
296
+        SELECT u FROM MuzichCoreBundle:User u
297
+        WHERE u.id IN (:uids)'
298
+      )
299
+      ->setParameter('uids', $ids)
300
+      ->getResult()
301
+    ;
302
+    
303
+    // Pour chacun on augmente le compteur de signalements inutiles
304
+    foreach ($users as $user)
305
+    {
306
+      $user->addBadReport();
307
+      $this->getDoctrine()->getEntityManager()->persist($user);
308
+    }
309
+    
310
+    $this->getDoctrine()->getEntityManager()->flush();
311
+    
312
+    return $this->jsonResponse(array(
313
+      'status' => 'success'
314
+    ));
315
+  }
316
+  
317
+  /**
318
+   * Considérer le commentaire signalé comme étant tout a fait acceptable
319
+   * 
320
+   * Ceci induit:
321
+   * * Que le commentaire en question ne soit plus signalé 
322
+   * * Que le ou les ids d'utilisateurs qui l'ont signalé comme soit "pénalisé
323
+   * 
324
+   * @param int $element_id
325
+   * @param date $date 
326
+   * 
327
+   * @return Response
328
+   */
329
+  public function commentRefuseAction($element_id, $date)
330
+  {
331
+    if (($response = $this->mustBeConnected(true)))
332
+    {
333
+      return $response;
334
+    }
335
+    
336
+    if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
337
+      ->findOneById($element_id))
338
+    )
339
+    {
340
+      return $this->jsonResponse(array(
341
+        'status' => 'error',
342
+        'errors' => array('NotFound')
343
+      ));
344
+    }
345
+    
346
+    $cm = new CommentsManager($element->getComments());
347
+    // On supprime le commentaire
348
+    $cm->deleteWithDate($date);
349
+    $element->setComments($cm->get());
350
+    $element->setCountCommentReport($cm->countCommentAlert());
351
+    
352
+    $this->getDoctrine()->getEntityManager()->persist($element);
353
+    $this->getDoctrine()->getEntityManager()->flush();
354
+    
355
+    return $this->jsonResponse(array(
356
+      'status' => 'success'
357
+    ));
358
+  }
223 359
 }

+ 15 - 1
src/Muzich/AdminBundle/Resources/config/routing.yml View File

@@ -32,4 +32,18 @@ moderate_element_delete:
32 32
   
33 33
 moderate_element_clean:
34 34
   pattern:  /admin/moderate/element/clean/{element_id}
35
-  defaults: { _controller: MuzichAdminBundle:Moderate:cleanElement }
35
+  defaults: { _controller: MuzichAdminBundle:Moderate:cleanElement }
36
+  
37
+##
38
+
39
+moderate_comments_index:
40
+  pattern:  /admin/moderate/comments
41
+  defaults: { _controller: MuzichAdminBundle:Moderate:comments }
42
+
43
+moderate_comment_clean:
44
+  pattern:  /admin/moderate/comment/{element_id}/{date}/clean
45
+  defaults: { _controller: MuzichAdminBundle:Moderate:commentClean }
46
+
47
+moderate_comment_refuse:
48
+  pattern:  /admin/moderate/comment/{element_id}/{date}/refuse
49
+  defaults: { _controller: MuzichAdminBundle:Moderate:commentRefuse }

+ 34 - 0
src/Muzich/AdminBundle/Resources/views/Moderate/comments.html.twig View File

@@ -0,0 +1,34 @@
1
+{% extends "MuzichAdminBundle:Moderate:layout.html.twig" %}
2
+
3
+{% block title %}{% endblock %}
4
+
5
+{% block content %}
6
+
7
+  <h2>{{ 'moderate.comments.title'|trans({}, 'adminui') }}</h2>
8
+    
9
+  <ul id="moderate_comments">
10
+    {% for comment in comments %}
11
+      <li class="comment" id="mod_comment_{{ comment.element_id }}_{{ comment.comment.d|date_epurate }}">
12
+        
13
+        <a href="{{ path('moderate_comment_clean', {
14
+          'element_id' : comment.element_id,
15
+          'date'       : comment.comment.d
16
+        }) }}" class="accept">
17
+         [V]
18
+        </a>
19
+        <a href="{{ path('moderate_comment_refuse', {
20
+          'element_id' : comment.element_id,
21
+          'date'       : comment.comment.d
22
+        }) }}" class="refuse">
23
+         [X]
24
+        </a>
25
+        
26
+        <strong>{{ comment.comment.u.s }}</strong>: 
27
+        
28
+        {{ comment.comment.c }}
29
+        
30
+      </li>
31
+    {% endfor %}
32
+  </ul>
33
+
34
+{% endblock %}

+ 10 - 0
src/Muzich/AdminBundle/Resources/views/Moderate/index.html.twig View File

@@ -26,4 +26,14 @@
26 26
     </li>
27 27
   </ul>
28 28
   
29
+  <h3>{{ 'moderate.comments.title'|trans({}, 'adminui') }}</h3>
30
+  
31
+  <ul>
32
+    <li>
33
+      <a href="{{ path('moderate_comments_index') }}">
34
+        {{ 'moderate.comments.to_moderate'|trans({'%count%' : count_comments}, 'adminui') }}
35
+      </a>
36
+    </li>
37
+  </ul>
38
+  
29 39
 {% endblock %}

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

@@ -317,7 +317,7 @@ class CommentController extends Controller
317 317
     $cm = new CommentsManager($element->getComments());
318 318
     $cm->alertComment($this->getUserId(), $date);
319 319
     $element->setComments($cm->get());
320
-    $element->setHasCommentReport($cm->hasCommentAlert());
320
+    $element->setCountCommentReport($cm->countCommentAlert());
321 321
     
322 322
     $this->getDoctrine()->getEntityManager()->persist($element);
323 323
     $this->getDoctrine()->getEntityManager()->flush();
@@ -356,7 +356,7 @@ class CommentController extends Controller
356 356
     $cm = new CommentsManager($element->getComments());
357 357
     $cm->unAlertComment($this->getUserId(), $date);
358 358
     $element->setComments($cm->get());
359
-    $element->setHasCommentReport($cm->hasCommentAlert());
359
+    $element->setCountCommentReport($cm->countCommentAlert());
360 360
     
361 361
     $this->getDoctrine()->getEntityManager()->persist($element);
362 362
     $this->getDoctrine()->getEntityManager()->flush();

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

@@ -200,10 +200,10 @@ class Element
200 200
    * Booléen permettant de savoir si un des commentaires de cet élément
201 201
    * a été signalé a la modération.
202 202
    * 
203
-   * @ORM\Column(type="boolean", nullable = "true")
204
-   * @var type string
203
+   * @ORM\Column(type="integer", nullable=true)
204
+   * @var int
205 205
    */
206
-  protected $has_comment_report = false;
206
+  protected $count_comment_report = false;
207 207
 
208 208
   /**
209 209
    * Get id
@@ -528,18 +528,18 @@ class Element
528 528
     $this->tags_propositions = $propositions;
529 529
   }
530 530
   
531
-  public function getHasCommentReport()
531
+  public function getCountCommentReport()
532 532
   {
533
-    if (is_null($this->has_comment_report))
533
+    if (is_null($this->count_comment_report))
534 534
     {
535 535
       return false;
536 536
     }
537 537
     return $this->has_comment_report;
538 538
   }
539 539
   
540
-  public function setHasCommentReport($has)
540
+  public function setCountCommentReport($count)
541 541
   {
542
-    $this->has_comment_report = $has;
542
+    $this->count_comment_report = $count;
543 543
   }
544 544
   
545 545
   /**

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

@@ -139,6 +139,19 @@ class CommentsManager
139 139
     return $found;
140 140
   }
141 141
   
142
+  public function deleteWithDate($date)
143
+  {
144
+    foreach ($this->comments as $i => $comment)
145
+    {
146
+      if ($comment['d'] == $date)
147
+      {
148
+        unset($this->comments[$i]);
149
+        return true;
150
+      }
151
+    }
152
+    return false;
153
+  }
154
+  
142 155
   /**
143 156
    * Permet de récupérer l'index d'un commentaire dans le tableau de commentaires.
144 157
    * Si le commentaire n'est pas trouvé on retourne null.
@@ -291,20 +304,48 @@ class CommentsManager
291 304
   }
292 305
   
293 306
   /**
294
-   * Permet de savoir si un ou plusieurs des commentaires ont été signalé.
307
+   * Retourne le nombre de commentaires signalés.
295 308
    * 
296 309
    * @return boolean
297 310
    */
298
-  public function hasCommentAlert()
311
+  public function countCommentAlert()
299 312
   {
313
+    $count = 0;
300 314
     foreach ($this->comments as $i => $comment)
301 315
     {
302 316
       if (count($comment['a']))
303 317
       {
304
-        return true;
318
+        $count = $count+1;
305 319
       }
306 320
     }
307
-    return false;
321
+    return $count;
322
+  }
323
+  
324
+  public function getAlertedComments()
325
+  {
326
+    $comments = array();
327
+    foreach ($this->comments as $i => $comment)
328
+    {
329
+      if (count($comment['a']))
330
+      {
331
+        $comments[] = $comment;
332
+      }
333
+    }
334
+    return $comments;
335
+  }
336
+  
337
+  public function cleanAlertsOnComment($date)
338
+  {
339
+    foreach ($this->comments as $i => $comment)
340
+    {
341
+      if ($comment['d'] == $date)
342
+      {
343
+        $ids = $comment['a'];
344
+        $this->comments[$i]['a'] = array();
345
+        return $ids;
346
+      }
347
+    }
348
+    return array();
308 349
   }
309 350
   
310 351
 }

+ 18 - 0
src/Muzich/CoreBundle/Repository/ElementRepository.php View File

@@ -343,4 +343,22 @@ class ElementRepository extends EntityRepository
343 343
     ;
344 344
   }  
345 345
   
346
+  public function countForCommentToModerate()
347
+  {
348
+    return $this->getEntityManager()
349
+      ->createQuery("SELECT COUNT(e.id) FROM MuzichCoreBundle:Element e
350
+        WHERE e.count_comment_report IS NOT NULL AND e.count_comment_report != 0"
351
+      )->getSingleScalarResult()
352
+    ;
353
+  }
354
+  
355
+  public function getForCommentToModerate()
356
+  {
357
+    return $this->getEntityManager()
358
+      ->createQuery("SELECT e FROM MuzichCoreBundle:Element e
359
+        WHERE e.count_comment_report IS NOT NULL AND e.count_comment_report != 0"
360
+      )->getResult()
361
+    ;
362
+  }
363
+  
346 364
 }

+ 29 - 5
web/bundles/muzichmoderate/js/moderate.js View File

@@ -1,7 +1,7 @@
1 1
 $(document).ready(function(){
2 2
    
3 3
   $('ul#moderate_tags li.tag a.accept, ul#moderate_tags li.tag a.refuse').click(function(){
4
-    link = $(this);
4
+    var link = $(this);
5 5
     $.getJSON($(this).attr('href'), function(response) {
6 6
      
7 7
       if (response.status == 'mustbeconnected')
@@ -26,9 +26,9 @@ $(document).ready(function(){
26 26
   
27 27
    
28 28
   $('ul#moderate_tags li.tag a.replace').click(function(){
29
-    link = $(this);
29
+    var link = $(this);
30 30
     
31
-    newtag = link.parent('li').find('input.tagBox_tags_ids').val();
31
+    var newtag = link.parent('li').find('input.tagBox_tags_ids').val();
32 32
     
33 33
     $.getJSON($(this).attr('href')+'/'+newtag, function(response) {
34 34
      
@@ -53,7 +53,7 @@ $(document).ready(function(){
53 53
   });
54 54
   
55 55
   $('ul#moderate_elements li.element div.controls a.delete').live('click', function(){
56
-     li = $(this).parent('div.controls').parent('li.element');
56
+     var li = $(this).parent('div.controls').parent('li.element');
57 57
      $.getJSON($(this).attr('href'), function(response) {
58 58
        if (response.status == 'success')
59 59
        {
@@ -68,7 +68,7 @@ $(document).ready(function(){
68 68
   });
69 69
   
70 70
   $('ul#moderate_elements li.element div.controls a.clean').live('click', function(){
71
-    li = $(this).parent('div.controls').parent('li.element');
71
+    var li = $(this).parent('div.controls').parent('li.element');
72 72
     $.getJSON($(this).attr('href'), function(response) {
73 73
        if (response.status == 'success')
74 74
        {
@@ -81,5 +81,29 @@ $(document).ready(function(){
81 81
      });
82 82
      return false;
83 83
   });
84
+  
85
+  $('ul#moderate_comments li.comment a.accept, ul#moderate_comments li.comment a.refuse').click(function(){
86
+    var link = $(this);
87
+    $.getJSON($(this).attr('href'), function(response) {
88
+     
89
+      if (response.status == 'mustbeconnected')
90
+      {
91
+        $(location).attr('href', url_index);
92
+      }
93
+      
94
+      if (response.status == 'success')
95
+      {
96
+        link.parent('li').remove();
97
+      }
98
+      
99
+      if (response.status == 'error')
100
+      {
101
+        alert(response.message);
102
+      }
103
+      
104
+    });
105
+      
106
+    return false;
107
+  });
84 108
    
85 109
 });