Commits

moumar  committed e1d4f7c Draft

first version

  • Participants
  • Tags v0.1

Comments (0)

Files changed (8)

+pour modifier les categories, ajouter une correspondance dans la variable $MMR_RATING_CATEGORIES.
+Pour la première valeur, il ne faut utiliser que les caractères a à z, 0 à 9, _ ou -. Cette première
+valeur ne doit jamais changer une fois qu'elle a été fixée.
+
+La deuxième valeur est libre.
+L'ordre peut être changé.
+
+pour afficher tous les critères sur la page d'un article: 
+<?php mmr_show() ?>
+
+pour afficher un critère particulier sur la page d'un article: 
+<?php mmr_show("hebergement") ?>
+
+
+pour afficher les top 10 les meilleurs articles dans toutes les categories:
+<?php mmr_top(10) ?>
+
+pour afficher le top 10 des meilleurs articles tous critères confondus:
+<?php mmr_top_overall(10) ?>
+
+pour effacer toutes les notes dans tous les articles:
+<?php mmr_clear_all() ?>

File images/rating_half.gif

Added
New image

File images/rating_off.gif

Added
New image

File images/rating_on.gif

Added
New image

File images/rating_over.gif

Added
New image

File moumar_multi_rating.css

+.mmr-rating.editable > img {
+  cursor: pointer;
+}
+
+.mmr-rating .title {
+  float :left;
+  width: 14em;
+}

File moumar_multi_rating.js

+jQuery(function($) {
+  var rating_names = [
+    "Nul",
+    "A la limite",
+    "Assez bien",
+    "Très bien",
+    "Excellent!"
+  ];
+
+  function restore_image_src() {
+    var img = $(this);
+    var text = img.parent(".mmr-rating").find(".text");
+    img.attr("src", img.data("original_src"));
+    if (!text.data("sticky")) {
+      text.text("");
+    }
+  }
+
+  $(".mmr-rating.editable > img")
+    .hover(function() {
+      var img = $(this);
+      var mmr_rating = img.parent(".mmr-rating");
+      var imgs = img.siblings("img").andSelf();
+      var i = imgs.index(img);
+      imgs
+        .each(function(index) {
+          var img = $(this);
+          var bn = img.attr("src").replace(/_\w+\.gif/, '');
+          var suffix = "off";
+          if (i >= index) {
+            suffix = "over";
+          }
+          img.attr("src", bn + "_" + suffix + ".gif");
+        });
+
+      mmr_rating
+        .find(".text")
+        .text(rating_names[i])
+        .data("sticky", false);
+    }, restore_image_src)
+    .click(function() {
+      var img = $(this);
+      var mmr_rating = img.parent(".mmr-rating");
+      var text = mmr_rating.find(".text");
+      var imgs = img.siblings("img").andSelf();
+      var i = imgs.index(img);
+
+      text.text("envoi...")
+          .data("sticky", true);
+      imgs.each(function() {
+        var img = $(this);
+        img.data("original_src", img.attr("src"));
+      });
+      $.post(mmr_rating.data("post_url"), {
+        post_id: mmr_rating.data("post_id"),
+        rating_category: mmr_rating.data("rating_category"),
+        rating: i+1
+      }).complete(function(e) {
+        text.text(e.responseText);
+      });
+    });
+
+  $(".mmr-rating.editable").mouseout(function() {
+    $(this).find("> img").each(restore_image_src);
+  });
+});

File moumar_multi_rating.php

+<?php
+/*
+Plugin Name: Moumar Multi Rating
+Plugin URI: https://github.com/moumar/multi_rating
+Description: Multi rating
+Author: moumar
+Version: 0.1
+Author URI: http://moumar.net/
+*/
+
+$MMR_RATING_CATEGORIES = array(
+  'hebergement'         => 'Hébergement',
+  'gastronomie'         => 'Gastronomie',
+  'accueil'             => 'Accueil & hospitalité',
+  'meteo'               => 'Météo',
+  'night-life'          => 'Bars/boîtes/night life',
+  'paysages'            => 'Paysages',
+  'culture'             => 'Culture/Monuments/musées',
+  'activites'           => 'Activités/loisirs/sport',
+  'shopping'            => 'Shopping',
+  'sante'               => 'Hygiène/Santé',
+  'securite'            => 'Sécurité',
+  'gay'                 => 'Gay-Friendly',
+  'photo'               => 'Intérêt photographique',
+  'deplacements'        => 'Déplacements intérieurs'
+);
+
+if (__FILE__ == realpath($_SERVER["SCRIPT_FILENAME"])) {
+  $base_path = str_replace("/wp-content/plugins/moumar_multi_rating", "", dirname(__FILE__));
+  session_start();
+  
+  require($base_path . '/wp-load.php');
+
+  $post_id = $_POST["post_id"];
+  $rating_category = $_POST["rating_category"];
+  $rating = $_POST["rating"];
+  $session_key = "moumar-multi-rating-$post_id-$rating_category";
+
+  $previous_rating = $_SESSION[$session_key] ;
+  //echo "rating: '$rating', previous '$previous_rating'";
+
+  if ($previous_rating) {
+    //mmr_rate_post($post_id, $rating_category, -1 * $previous_rating) ;
+    echo "vous avez déjà voté!";
+  } else {
+    mmr_rate_post($post_id, $rating_category, $rating);
+    $_SESSION[$session_key] = $_POST["rating"];
+    echo "Merci pour votre vote.";
+  }
+
+  exit();
+}
+
+function mmr_rate_post($post_id, $rating_category, $rating) {
+  $ratings_for_country = get_post_meta($post_id, "moumar_multi_rating", true);
+  
+  if (!$ratings_for_country) {
+    $ratings_for_country = array();
+  }
+
+  if (!$ratings_for_country[$rating_category]) {
+    $ratings_for_country[$rating_category] = array("count" => 0, "total" => 0);
+  }
+
+  $ratings_for_country[$rating_category]["count"] += ($rating < 0 ? -1 : 1);
+  $ratings_for_country[$rating_category]["total"] += $rating;
+  //echo( "count: " . $ratings_for_country[$rating_category]["count"] . " total : " . $ratings_for_country[$rating_category]["total"]);
+
+  add_post_meta($post_id, "moumar_multi_rating", $ratings_for_country, true) or update_post_meta($post_id, "moumar_multi_rating", $ratings_for_country);
+}
+
+function mmr_pluralize($root, $count) {
+  $s = "$count $root";
+  if ($count != 1) {
+    $s .= "s";
+  }
+  return $s;
+}
+
+function mmr_show($rating_category = null) {
+  global $post, $MMR_RATING_CATEGORIES;
+  if ($rating_category) {
+    $category_full_name = $MMR_RATING_CATEGORIES[$rating_category];
+    if (!$category_full_name) {
+      echo "<big> la catégorie " . htmlspecialchars($rating_category) . " n'existe pas!</big>";
+      return;
+    }
+
+    $ratings_for_country = get_post_meta($post->ID, 'moumar_multi_rating', true);
+    $count = 0;
+    $total = 0;
+
+    if ($ratings_for_country[$rating_category]) {
+      $count = $ratings_for_country[$rating_category]["count"] or 0;
+      $total = $ratings_for_country[$rating_category]["total"] or 0;
+    }
+    if ($total > 0)
+      $note = round($total/$count, 1);
+    else
+      $note = 0;
+
+    mmr_show_rating($note, htmlspecialchars($category_full_name), ", " . mmr_pluralize("vote", $count), array("post_id" => $post->ID, "rating_category" => $rating_category), true);
+  } else {
+    foreach($MMR_RATING_CATEGORIES as $key => $value) {
+      mmr_show($key);
+    }
+  }
+}
+
+function mmr_show_rating($note, $title, $suffix, $datas, $is_editable) {
+  $editable_class = $is_editable ? "editable" : "";
+  $post_url = plugins_url(basename(__FILE__), __FILE__);
+  $data_string = "data-post_url=\"" . $post_url . "\"";
+  foreach($datas as $k => $v) {
+    $data_string .= " data-$k=\"$v\" ";
+  }
+
+  echo "<div class=\"mmr-rating $editable_class\" $data_string >";
+  echo "<span class=\"title\">" . $title ."</span>";
+  $image_path = plugin_dir_url(__FILE__ ) . 'images';
+  for($i = 0; $i < 5; $i++) {
+    if ((int)$note > $i)
+      $base = "rating_on";
+    else if ($note > $i and fmod($note, 1) > 0.1)
+      $base = "rating_half";
+    else
+      $base = "rating_off";
+    $src = "$image_path/$base.gif";
+
+    echo "<img src=\"$src\" data-index=\"$i\" data-original_src=\"$src\" />";
+  }
+  echo mmr_pluralize("étoile", $note) . $suffix;
+  echo ' <span class="text"></span>';
+  echo '</div>';
+}
+
+function mmr_clear_all() {
+  foreach(get_posts() as $post) {
+    setup_postdata($post);
+    delete_post_meta($post->ID, "moumar_multi_rating"); 
+  }
+}
+
+function mmr_top_overall($limit = 10) {
+  $ratings = array();
+
+  foreach(get_posts() as $post) {
+    setup_postdata($post);
+    $count = 0;
+    $total = 0;
+    $ratings_for_country = get_post_meta($post->ID, "moumar_multi_rating", true);
+    if (!$ratings_for_country)
+      continue;
+    foreach($ratings_for_country as $rating) {
+      $count += $rating["count"];
+      $total += $rating["total"];
+    }
+    $ratings[$post->ID] = 0;
+    if ($count > 0) {
+      $ratings[$post->ID] = $total / $count;
+    }
+  }
+  
+  mmr_internal_show_ratings($ratings, $limit, "global");
+}
+
+function mmr_top($rating_category, $limit = 10) {
+  global $MMR_RATING_CATEGORIES;
+
+  if (is_numeric($rating_category)) {
+    $limit = $rating_category;
+    foreach($MMR_RATING_CATEGORIES as $key => $value) {
+      mmr_top($key, $limit);
+    }
+  } else {
+    if (!$MMR_RATING_CATEGORIES[$rating_category]) {
+      echo "<big> la catégorie " . htmlspecialchars($rating_category) . " n'existe pas!</big>";
+      return;
+    }
+    #$args = array('category' => -9); // exclude category 9
+    #$custom_posts = get_posts($args);
+    $custom_posts = get_posts();
+    $ratings = array();
+    foreach($custom_posts as $post) {
+      setup_postdata($post);
+      $ratings_for_country = get_post_meta($post->ID, "moumar_multi_rating", true);
+      if ($ratings_for_country and $ratings_for_country[$rating_category]) {
+        $count = $ratings_for_country[$rating_category]["count"];
+        if ($count) 
+          $ratings[$post->ID] = $ratings_for_country[$rating_category]["total"] / $count;
+      }
+    }
+    mmr_internal_show_ratings($ratings, $limit, $MMR_RATING_CATEGORIES[$rating_category]);
+  }
+}
+
+function mmr_internal_show_ratings($ratings, $limit, $title) {
+  arsort($ratings);
+  $ratings = array_slice($ratings, 0, $limit);
+  //print_r($ratings);
+
+  echo "<div class=\"mmr-rating-top\"><h3>Top " . htmlspecialchars($limit). " " . htmlspecialchars($title) . "</h3>";
+  echo "<ol>";
+  foreach ($ratings as $post_id => $note) {
+    if ($note == 0)
+      continue;
+    echo "<li>";
+    $permalink = htmlspecialchars(get_permalink($post_id));
+    $title = htmlspecialchars(get_the_title($post_id));
+    $html = "<a href=\"$permalink\" title=\"$title\">$title</a>";
+    
+    mmr_show_rating(round($note, 1), $html, "", array(), false);
+    echo "</li>";
+  }
+  echo "</ol>";
+  echo "</div>";
+}
+
+function mmr_init() {
+  wp_enqueue_script( 'jquery' );
+  wp_enqueue_script( 'moumar_multi_rating', plugins_url('moumar_multi_rating/moumar_multi_rating.js') );
+  wp_enqueue_style( 'moumar_multi_rating', plugins_url('moumar_multi_rating/moumar_multi_rating.css') );
+}
+
+add_action('init', 'mmr_init');
+?>