Quellcode durchsuchen

Evolution #149: Notation

bastien vor 12 Jahren
Ursprung
Commit
32b141eefe

+ 2 - 0
app/Resources/translations/elements.fr.yml Datei anzeigen

@@ -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
+  vote:
55
+    good:               Ce partage mérite un point supplémentaire (cliquez a nouveau pour enlever)
54 56
   
55 57
 comment:
56 58
   edit:

+ 94 - 0
src/Muzich/CoreBundle/Controller/ElementController.php Datei anzeigen

@@ -344,4 +344,98 @@ class ElementController extends Controller
344 344
     ));
345 345
   }
346 346
   
347
+  public function addVoteGoodAction($element_id, $token)
348
+  {
349
+    if (($response = $this->mustBeConnected(true)))
350
+    {
351
+      return $response;
352
+    }
353
+    
354
+    if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
355
+      ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
356
+    {
357
+      return $this->jsonResponse(array(
358
+        'status' => 'error',
359
+        'errors' => array('NotFound')
360
+      ));
361
+    }
362
+    
363
+    if ($element->getOwner()->getId() == $this->getUserId())
364
+    {
365
+      return $this->jsonResponse(array(
366
+        'status' => 'error',
367
+        'errors' => array('NotAllowed')
368
+      ));
369
+    }
370
+    
371
+    $element->addVoteGood($this->getUser()->getId());
372
+    $this->getDoctrine()->getEntityManager()->persist($element);
373
+    $this->getDoctrine()->getEntityManager()->flush();
374
+    
375
+    return $this->jsonResponse(array(
376
+      'status' => 'success',
377
+      'data'   => array(
378
+        'a' => array(
379
+          'href' => $this->generateUrl('ajax_element_remove_vote_good', array(
380
+            'element_id' => $element->getId(),
381
+            'token'      => $this->getUser()->getPersonalHash()
382
+          ))
383
+        ),
384
+        'img' => array(
385
+          'src'  => $this->getAssetUrl('bundles/muzichcore/img/up_b.png')
386
+        ),
387
+        'element' => array(
388
+          'points' => $element->getPoints()
389
+        )
390
+      )
391
+    ));
392
+  }
393
+  
394
+  public function removeVoteGoodAction($element_id, $token)
395
+  {
396
+    if (($response = $this->mustBeConnected(true)))
397
+    {
398
+      return $response;
399
+    }
400
+    
401
+    if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
402
+      ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
403
+    {
404
+      return $this->jsonResponse(array(
405
+        'status' => 'error',
406
+        'errors' => array('NotFound')
407
+      ));
408
+    }
409
+    
410
+    if ($element->getOwner()->getId() == $this->getUserId())
411
+    {
412
+      return $this->jsonResponse(array(
413
+        'status' => 'error',
414
+        'errors' => array('NotAllowed')
415
+      ));
416
+    }
417
+    
418
+    $element->removeVoteGood($this->getUser()->getId());
419
+    $this->getDoctrine()->getEntityManager()->persist($element);
420
+    $this->getDoctrine()->getEntityManager()->flush();
421
+    
422
+    return $this->jsonResponse(array(
423
+      'status' => 'success',
424
+      'data'   => array(
425
+        'a' => array(
426
+          'href' => $this->generateUrl('ajax_element_add_vote_good', array(
427
+            'element_id' => $element->getId(),
428
+            'token'      => $this->getUser()->getPersonalHash()
429
+          ))
430
+        ),
431
+        'img' => array(
432
+          'src'  => $this->getAssetUrl('bundles/muzichcore/img/up_bw.png')
433
+        ),
434
+        'element' => array(
435
+          'points' => $element->getPoints()
436
+        )
437
+      )
438
+    ));
439
+  }
440
+  
347 441
 }

+ 90 - 0
src/Muzich/CoreBundle/Entity/Element.php Datei anzeigen

@@ -162,6 +162,22 @@ class Element
162 162
    * @var string 
163 163
    */
164 164
   protected $report_ids;
165
+  
166
+  /**
167
+   * array json des id users ayant voté +1
168
+   * 
169
+   * @ORM\Column(type="text", nullable=true)
170
+   * @var string 
171
+   */
172
+  protected $vote_good_ids;
173
+  
174
+  /**
175
+   * Compteur de points
176
+   * 
177
+   * @ORM\Column(type="integer", nullable=true)
178
+   * @var int 
179
+   */
180
+  protected $points;
165 181
 
166 182
   /**
167 183
    * Get id
@@ -508,4 +524,78 @@ class Element
508 524
     return false;
509 525
   }
510 526
   
527
+  public function getPoints()
528
+  {
529
+    if ($this->points === null)
530
+    {
531
+      return '0';
532
+    }
533
+    
534
+    return $this->points;
535
+  }
536
+  
537
+  public function setPoints($points)
538
+  {
539
+    $this->points = $points;
540
+  }
541
+  
542
+  public function getVoteGoodIds()
543
+  {
544
+    return json_decode($this->vote_good_ids, true);
545
+  }
546
+  
547
+  public function setVoteGoodIds($votes_ids)
548
+  {
549
+    $this->vote_good_ids = json_encode($votes_ids);
550
+  }
551
+  
552
+  public function addVoteGood($user_id)
553
+  {
554
+    $votes = $this->getVoteGoodIds();
555
+    if (!count($votes))
556
+    {
557
+      $votes = array();
558
+    }
559
+    
560
+    if (!$this->hasVoteGood($user_id))
561
+    {
562
+      $votes[] = (string)$user_id;
563
+    }
564
+    $this->setPoints($this->getPoints()+1);
565
+    $this->setVoteGoodIds($votes);
566
+  }
567
+  
568
+  public function removeVoteGood($user_id)
569
+  {
570
+    if (count($votes = $this->getVoteGoodIds()))
571
+    {
572
+      $votes_n = array();
573
+      foreach ($votes as $id)
574
+      {
575
+        if ($id != $user_id)
576
+        {
577
+          $votes_n[] = (string)$id;
578
+        }
579
+      }
580
+      
581
+      $this->setPoints($this->getPoints()-1);
582
+      $this->setVoteGoodIds($votes_n);
583
+    }
584
+  }
585
+  
586
+  public function hasVoteGood($user_id)
587
+  {
588
+    if (count($votes = $this->getVoteGoodIds()))
589
+    {
590
+      foreach ($votes as $id)
591
+      {
592
+        if ($id == $user_id)
593
+        {
594
+          return true;
595
+        }
596
+      }
597
+    }
598
+    return false;
599
+  }
600
+  
511 601
 }

+ 7 - 0
src/Muzich/CoreBundle/Resources/config/routing.yml Datei anzeigen

@@ -52,6 +52,13 @@ ajax_add_tag_arguments:
52 52
   pattern: /ajax/add-tag/{name}/{arguments}
53 53
   defaults: { _controller: MuzichCoreBundle:Core:addTag, name: null, arguments: null }
54 54
   
55
+ajax_element_add_vote_good:
56
+  pattern: /ajax/element/vote/add/good/{element_id}/{token}
57
+  defaults: { _controller: MuzichCoreBundle:Element:addVoteGood }
58
+  
59
+ajax_element_remove_vote_good:
60
+  pattern: /ajax/element/vote/remove/good/{element_id}/{token}
61
+  defaults: { _controller: MuzichCoreBundle:Element:removeVoteGood }
55 62
 
56 63
 ####
57 64
 

+ 45 - 10
src/Muzich/CoreBundle/Resources/views/SearchElement/element.html.twig Datei anzeigen

@@ -35,16 +35,6 @@
35 35
     </td>
36 36
     <td class="element_content">
37 37
       
38
-      {% if element.getCountFavorite %}
39
-        <a class="favorite_link" href="{{ path('favorite_remove', { 'id': element.id, 'token': app.user.personalHash }) }}" >
40
-          <img src="{{ asset('bundles/muzichcore/img/favorite.png') }}" title="{{ 'element.favorite.remove'|trans({}, 'elements') }}" alt="{{ 'element.favorite.remove'|trans({}, 'elements') }}"/>
41
-        </a>
42
-      {% else %}
43
-        <a class="favorite_link" href="{{ path('favorite_add', { 'id': element.id, 'token': app.user.personalHash }) }}" >
44
-          <img src="{{ asset('bundles/muzichcore/img/favorite_bw.png') }}" title="{{ 'element.favorite.add'|trans({}, 'elements') }}" alt="{{ 'element.favorite.add'|trans({}, 'elements') }}" />
45
-        </a>
46
-      {% endif %}
47
-      
48 38
       <span class="element_name">
49 39
         {% if element.embed %}
50 40
           <a href="#" class="element_open element_name_embed_open_link">
@@ -151,6 +141,51 @@
151 141
         {{ 'element.comments.add'|trans({}, 'elements') }}
152 142
       </a>
153 143
       
144
+      
145
+    </td>
146
+    <td class="right">
147
+      
148
+      <ul>
149
+        {% if element.owner.id != app.user.id %}
150
+          <li class="vote">
151
+            {% if element.hasVoteGood(app.user.id) %}
152
+              <a class="vote" href="{{ path('ajax_element_remove_vote_good', {
153
+                'element_id' : element.id,
154
+                'token'      : app.user.getPersonalHash
155
+              }) }}" title="{{ 'element.vote.good'|trans({}, 'elements') }}">
156
+                <img src="{{ asset('/bundles/muzichcore/img/up_b.png') }}" alt="vote" />
157
+              </a>
158
+            {% else %}
159
+              <a class="vote" href="{{ path('ajax_element_add_vote_good', {
160
+                'element_id' : element.id,
161
+                'token'      : app.user.getPersonalHash
162
+              }) }}" title="{{ 'element.vote.good'|trans({}, 'elements') }}">
163
+                <img src="{{ asset('/bundles/muzichcore/img/up_bw.png') }}" alt="vote" />
164
+              </a>
165
+            {% endif %}
166
+          </li>
167
+      {% endif %}
168
+      <li class="score">
169
+
170
+        <span class="score">
171
+          {{ element.points }}
172
+        </span>
173
+
174
+      </li>
175
+      <li>
176
+
177
+      {% if element.getCountFavorite %}
178
+        <a class="favorite_link" href="{{ path('favorite_remove', { 'id': element.id, 'token': app.user.personalHash }) }}" >
179
+          <img src="{{ asset('bundles/muzichcore/img/favorite.png') }}" title="{{ 'element.favorite.remove'|trans({}, 'elements') }}" alt="{{ 'element.favorite.remove'|trans({}, 'elements') }}"/>
180
+        </a>
181
+      {% else %}
182
+        <a class="favorite_link" href="{{ path('favorite_add', { 'id': element.id, 'token': app.user.personalHash }) }}" >
183
+          <img src="{{ asset('bundles/muzichcore/img/favorite_bw.png') }}" title="{{ 'element.favorite.add'|trans({}, 'elements') }}" alt="{{ 'element.favorite.add'|trans({}, 'elements') }}" />
184
+        </a>
185
+      {% endif %}
186
+
187
+        </li>
188
+      </ul>
154 189
     </td>
155 190
   </tr>
156 191
 </table>

+ 2 - 0
src/Muzich/CoreBundle/Resources/views/layout.html.twig Datei anzeigen

@@ -53,6 +53,8 @@
53 53
     url_add_tag = "{{ path('ajax_add_tag') }}";
54 54
     url_element_new_count = "{{ path('element_new_count') }}";
55 55
     url_element_new_get = "{{ path('element_new_get') }}";
56
+    
57
+    url_img_ajax_loader = "{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}";
56 58
   </script>
57 59
   {% block js %}{% endblock %}
58 60
   

+ 37 - 3
web/bundles/muzichcore/css/main.css Datei anzeigen

@@ -494,12 +494,12 @@ li.element
494 494
   padding: 5px;
495 495
 }
496 496
 
497
-li.element a.favorite_link
497
+/*li.element a.favorite_link
498 498
 {
499 499
   position: relative;
500 500
   top: 0px;
501 501
   left: 483px;
502
-}
502
+}*/
503 503
 
504 504
 #container li.element a.favorite_link:hover
505 505
 {
@@ -521,11 +521,12 @@ li.element td.element_content
521 521
 {
522 522
   vertical-align: top;
523 523
   padding-left: 5px;
524
+  width: 468px;
524 525
 }
525 526
 
526 527
 li.element span.element_name
527 528
 {
528
-  margin-left: -20px;
529
+  
529 530
 }
530 531
 
531 532
 li.element td.element_thumbnail
@@ -1051,4 +1052,37 @@ input.intext
1051 1052
   margin-left: 15px;
1052 1053
   margin-right: 15px;
1053 1054
   font-weight: bold;
1055
+}
1056
+
1057
+li.element table
1058
+{
1059
+  width: 100%;
1060
+}
1061
+
1062
+li.element td.right
1063
+{
1064
+  text-align: center;
1065
+  vertical-align: top;
1066
+}
1067
+
1068
+li.element td.right ul, li.element td.right ul li
1069
+{
1070
+  margin: 0px;
1071
+  padding: 0px;
1072
+}
1073
+
1074
+li.element td.right ul li
1075
+{
1076
+  list-style-type: none;
1077
+}
1078
+
1079
+li.element td.right ul li.vote
1080
+{
1081
+  height: 25px;
1082
+}
1083
+
1084
+li.element td.right span.score
1085
+{
1086
+  font-size: 20px;
1087
+  font-weight: bold;
1054 1088
 }

BIN
web/bundles/muzichcore/img/up_b.png Datei anzeigen


BIN
web/bundles/muzichcore/img/up_bw.png Datei anzeigen


+ 32 - 2
web/bundles/muzichcore/js/muzich.js Datei anzeigen

@@ -1653,5 +1653,35 @@ $(document).ready(function(){
1653 1653
       
1654 1654
     }
1655 1655
   });
1656
-   
1657
- });
1656
+
1657
+  /*
1658
+   * Vote sur element
1659
+   */
1660
+  
1661
+  $('li.element a.vote').live('click', function(){
1662
+    
1663
+    img = $(this).find('img');
1664
+    link = $(this);
1665
+    img.attr('src', url_img_ajax_loader);
1666
+    
1667
+    $.getJSON(link.attr('href'), function(response){
1668
+        
1669
+      if (response.status == 'mustbeconnected')
1670
+      {
1671
+        $(location).attr('href', url_index);
1672
+      }
1673
+      
1674
+      if (response.status == 'success')
1675
+      {
1676
+        link.attr('href', response.data.a.href);
1677
+        img.attr('src', response.data.img.src);
1678
+        link.parent('li').parent('ul').find('li.score span.score').html(response.data.element.points);
1679
+      }
1680
+      
1681
+    });
1682
+    
1683
+    return false;
1684
+  });
1685
+  
1686
+
1687
+});