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
     title:          Elements
9
     title:          Elements
10
     action:
10
     action:
11
       delete:       Supprimer
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
 use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
9
 use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
10
 use Muzich\CoreBundle\Managers\TagManager;
10
 use Muzich\CoreBundle\Managers\TagManager;
11
 use Muzich\CoreBundle\Propagator\EventElement;
11
 use Muzich\CoreBundle\Propagator\EventElement;
12
+use Muzich\CoreBundle\Managers\CommentsManager;
12
 
13
 
13
 class ModerateController extends Controller
14
 class ModerateController extends Controller
14
 {
15
 {
23
       ->countToModerate();
24
       ->countToModerate();
24
     $count_elements = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
25
     $count_elements = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
25
       ->countToModerate();
26
       ->countToModerate();
27
+    $count_comments = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
28
+      ->countForCommentToModerate();
26
     
29
     
27
     return array(
30
     return array(
28
       'count_tags' => $count_tags,
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
     ));
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
   
32
   
33
 moderate_element_clean:
33
 moderate_element_clean:
34
   pattern:  /admin/moderate/element/clean/{element_id}
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

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
     </li>
26
     </li>
27
   </ul>
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
 {% endblock %}
39
 {% endblock %}

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

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

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

200
    * Booléen permettant de savoir si un des commentaires de cet élément
200
    * Booléen permettant de savoir si un des commentaires de cet élément
201
    * a été signalé a la modération.
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
    * Get id
209
    * Get id
528
     $this->tags_propositions = $propositions;
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
       return false;
535
       return false;
536
     }
536
     }
537
     return $this->has_comment_report;
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
     return $found;
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
    * Permet de récupérer l'index d'un commentaire dans le tableau de commentaires.
156
    * Permet de récupérer l'index d'un commentaire dans le tableau de commentaires.
144
    * Si le commentaire n'est pas trouvé on retourne null.
157
    * Si le commentaire n'est pas trouvé on retourne null.
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
    * @return boolean
309
    * @return boolean
297
    */
310
    */
298
-  public function hasCommentAlert()
311
+  public function countCommentAlert()
299
   {
312
   {
313
+    $count = 0;
300
     foreach ($this->comments as $i => $comment)
314
     foreach ($this->comments as $i => $comment)
301
     {
315
     {
302
       if (count($comment['a']))
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
     ;
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
 $(document).ready(function(){
1
 $(document).ready(function(){
2
    
2
    
3
   $('ul#moderate_tags li.tag a.accept, ul#moderate_tags li.tag a.refuse').click(function(){
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
     $.getJSON($(this).attr('href'), function(response) {
5
     $.getJSON($(this).attr('href'), function(response) {
6
      
6
      
7
       if (response.status == 'mustbeconnected')
7
       if (response.status == 'mustbeconnected')
26
   
26
   
27
    
27
    
28
   $('ul#moderate_tags li.tag a.replace').click(function(){
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
     $.getJSON($(this).attr('href')+'/'+newtag, function(response) {
33
     $.getJSON($(this).attr('href')+'/'+newtag, function(response) {
34
      
34
      
53
   });
53
   });
54
   
54
   
55
   $('ul#moderate_elements li.element div.controls a.delete').live('click', function(){
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
      $.getJSON($(this).attr('href'), function(response) {
57
      $.getJSON($(this).attr('href'), function(response) {
58
        if (response.status == 'success')
58
        if (response.status == 'success')
59
        {
59
        {
68
   });
68
   });
69
   
69
   
70
   $('ul#moderate_elements li.element div.controls a.clean').live('click', function(){
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
     $.getJSON($(this).attr('href'), function(response) {
72
     $.getJSON($(this).attr('href'), function(response) {
73
        if (response.status == 'success')
73
        if (response.status == 'success')
74
        {
74
        {
81
      });
81
      });
82
      return false;
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
 });