11487 sujets

JavaScript, DOM et API Web HTML5

Bonjour tout le monde! J'espère que quelqu'un pourra m'aider sur ce forum.
Voila je débute dans la creation de site et je me prends la tete depuis quelques jours sur mon blog.
J'ai un système de vote,"5 star-rating sytem" qui marche très bien mais que j'aimerai paramétrer à ma façon pour qu'il n'y ait que 2 étoiles. Si vous pouviez jeter un coup d'oeil pour voir ce que je peux changer ce serait cool!!

Voici tous les codes des fichiers:
vote.php
<?php


// no direct access
defined( '_JEXEC' ) or  die('Restricted access');
jimport( 'joomla.event.plugin' );



class plgContentExtraVote extends JPlugin
{

	function plgContentExtraVote( &$subject )
	{
        parent::__construct($subject);
		
        $this->plugin = &JPluginHelper::getPlugin('content', 'extravote');
        $this->params = new JParameter($this->plugin->params);
	}
		
	function onBeforeDisplayContent(&$article, &$params)
	{
        if ( $this->params->get('display') == 0 && !$params->get( 'intro_only' )  )
		{
		    $article->xid = 'x';
		    return $this->ContentExtraVote($article, $params);
 	    }
 	}
	
	function ContentExtraVote(&$article, &$params)
	{ 
           
		$id  = $article->id;
		$xid = $article->xid;
		
		$rating_count=$rating_sum=0;
		$html='';

		if ($params->get( 'show_vote' ) && !$params->get( 'popup' ))
		{
			$db	=& JFactory::getDBO();
			$query='SELECT * FROM #__content_rating WHERE content_id='. $id;
			$db->setQuery($query);
			$vote=$db->loadObject();
		
			if($vote) {
				$rating_sum = intval($vote->rating_sum);
				$rating_count = intval($vote->rating_count);
			}
		
				$html .= $this->plgContentExtraVoteStars( $id, $rating_sum, $rating_count, $xid );
		}
		return $html;
 	}
	
  
 	function plgContentExtraVoteStars( $id, $rating_sum, $rating_count, $xid )
	{
	 	if ( $this->params->get('css', 1) ) :
			JHTML::stylesheet('extravote.css','plugins/content/extravote/',false);
		endif;
     	JHTML::script('extravote.js','plugins/content/extravote/',false);
        JPlugin::loadLanguage('plg_content_extravote', JPATH_ADMINISTRATOR);
	
        $live_path = JURI::base();

     	global $plgContentExtraVoteAddScript;
		
		$counter = $this->params->get('counter',1);
		$unrated = $this->params->get('unrated',1);
		$stars = '';
		
	 	if(!$plgContentExtraVoteAddScript){ 
         	echo "
<script type=\"text/javascript\" language=\"javascript\">
<!--
var sfolder = '".JURI::base(true)."';
var extravote_text=Array('".JTEXT::_('Your browser does not support AJAX')."','".JTEXT::_('Loading')."','".JTEXT::_('Thank you for voting')."','".JTEXT::_('You need to login')."','".JTEXT::_('You have already rated this item')."','".JTEXT::_('Votes')."','".JTEXT::_('Vote')."');
-->
</script>";
     	$plgContentExtraVoteAddScript = 1;
	 	}
		
		if($rating_count!=0) {
			$percent = number_format((intval($rating_sum) / intval( $rating_count ))*20,2);
		} elseif ($unrated == 0) {
			$counter = -1;
		}
		
		if ( (int)$xid ) { 
			$stars = '-small';
			if ( $counter == 2 ) $counter = 0;
		} else {
			if ( $counter == 3 ) $counter = 0;
		}
								
	 	$html="
<div class=\"extravote-container".$stars."\"".( $xid != 'x' ? "" : " style=\"margin-top:5px;\"" ).">
  <ul class=\"extravote-stars".$stars."\">
    <li id=\"rating_".$id."_".$xid."\" class=\"current-rating\" style=\"width:".(int)$percent."%;\"></li>
    <li><a href=\"javascript:void(null)\" onclick=\"javascript:JVXVote(".$id.",1,".$rating_sum.",".$rating_count.",'".$xid."',".$counter.");\" title=\"".JTEXT::_('Very Poor')."\" class=\"one-star\">1</a></li>
    <li><a href=\"javascript:void(null)\" onclick=\"javascript:JVXVote(".$id.",2,".$rating_sum.",".$rating_count.",'".$xid."',".$counter.");\" title=\"".JTEXT::_('Poor')."\" class=\"two-stars\">2</a></li>
    
  </ul>
</div>
  <span id=\"extravote_".$id."_".$xid."\" class=\"extravote-count\"><small>";
  
  		if ( $counter != -1 ) {
  			if ( $counter != 0 ) {
				$html .= "( ";
			 		if($rating_count!=1) {
				 		$html .= $rating_count." ".JTEXT::_('Votes');
			 		} else { 
			 			$html .= $rating_count." ".JTEXT::_('Vote');
     				}
 	 			$html .=" )";
			}
		}
 	 	$html .="</small></span>";
		
	 	return $html;
 	}
	
 	function onPrepareContent( &$article, &$params ) 
	{
	    if (isset($article->id)) {
		
	        $extra = $this->params->get('extra', 1);
			$main  = $this->params->get('main', 1);
			
 	 	    if ( $extra != 0 ) {
			
   	 		    $regex = "#{extravote\s*([0-9]+)}#s";
				
			    if ( $extra == 2 && JRequest::getCmd('view') != 'article')
			    {
   	 			    $article->text = preg_replace( $regex, '', $article->text );
			    } else {
				    $this->article_id = $article->id;
   	 			    $article->text = preg_replace_callback( $regex, array($this,'plgContentExtraVoteReplacer'), $article->text );
			    }
		    }
			
 	 	    if ( $main != 0 ) {
			
			    if ( $main == 2 && JRequest::getCmd('view') != 'article')
			    {
   	 			    $article->text = preg_replace( '#{mainvote}#', '', $article->text );
			    } else {
				    $this->article_id = $article->id;
   	 			    $article->text = preg_replace_callback( '#{mainvote}#', array($this,'plgContentExtraVoteReplacer'), $article->text );
			    }
		    }
		
		    if ( $this->params->get('display') == 1 )  {
			
		        $article->xid = 'x';
		        $article->text .= '<br />'.$this->ContentExtraVote($article, $params);
		    }
 	    }
 	}
 
	function plgContentExtraVoteReplacer(&$matches ) 
	{
  		$db	=& JFactory::getDBO();
  		$cid=$this->article_id;
  		$rating_sum = 0;
  		$rating_count = 0;
		if ($matches[0] == '{mainvote}') {
			global $mainvote;
			$mainvote .= 'x';
  			$xid .= 'x'.$mainvote;
  			$db->setQuery('SELECT * FROM #__content_rating WHERE content_id='. (int)$cid);
		} else {
  			$xid = (int)$matches[1];
  			$db->setQuery('SELECT * FROM #__content_extravote WHERE content_id='.(int)$cid.' AND extra_id='.(int)$xid);
		}
  		$vote = $db->loadObject();
  		if($vote) {
	 		if($vote->rating_count!=0)
				$rating_sum = intval($vote->rating_sum);
				$rating_count = intval($vote->rating_count);
	 	}
  		return $this->plgContentExtraVoteStars( $cid, $rating_sum, $rating_count, $xid );
	}
	
}
?>



ajax.php
<?php


// Set flag that this is a parent file
define('_JEXEC', 1);

// no direct access
defined('_JEXEC') or die('Restricted access');

define( 'DS', DIRECTORY_SEPARATOR );

define('JPATH_BASE', dirname(__FILE__).DS.'..'.DS.'..'.DS.'..' );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

jimport('joomla.database.database');
jimport('joomla.database.table');

$mainframe = &JFactory::getApplication('site');
$mainframe->initialise();

$user = &JFactory::getUser();

$plugin = &JPluginHelper::getPlugin('content', 'extravote');
$params = new JParameter($plugin->params);

if ( $params->get('access') == 1 && !$user->get('id') ) {
	echo 'login';
} else {
	$user_rating = JRequest::getInt('user_rating');
	$cid = JRequest::getInt('cid');
	$xid = JRequest::getVar('xid');
	$db  = &JFactory::getDBO();
	if (($user_rating >= 1) and ($user_rating <= 2)) {
		$currip = ( phpversion() <= '4.2.1' ? @getenv( 'REMOTE_ADDR' ) : $_SERVER['REMOTE_ADDR'] );
		if ( !(int)$xid ){
			$query = "SELECT * FROM #__content_rating WHERE content_id = " . $cid;
			$db->setQuery( $query );
			$votesdb = $db->loadObject();
			if ( !$votesdb ) {
				$query = "INSERT INTO #__content_rating ( content_id, lastip, rating_sum, rating_count )"
				. "\n VALUES ( " . $cid . ", " . $db->Quote( $currip ) . ", " . $user_rating . ", 1 )";
				$db->setQuery( $query );
				$db->query() or die( $db->stderr() );;
			} else {
				if ($currip != ($votesdb->lastip)) {
					$query = "UPDATE #__content_rating"
					. "\n SET rating_count = rating_count + 1, rating_sum = rating_sum + " .   $user_rating . ", lastip = " . $db->Quote( $currip )
					. "\n WHERE content_id = " . $cid;
					$db->setQuery( $query );
					$db->query() or die( $db->stderr() );
				} else {
					echo 'voted';
					exit();
				}
			}
		} else {
			$query = "SELECT * FROM #__content_extravote WHERE content_id=".$cid." AND extra_id=".$xid;
			$db->setQuery( $query );
			$votesdb = $db->loadObject();
			if ( !$votesdb ) {
				$query = "INSERT INTO #__content_extravote  (content_id,extra_id,lastip,rating_sum,rating_count)"
				. "\n VALUES (".$cid.",".$xid.",".$db->Quote($currip).",".$user_rating.",1)";
				$db->setQuery( $query );
				$db->query() or die( $db->stderr() );;
			} else {
				if ($currip != ($votesdb->lastip)) {
					$query = "UPDATE #__content_extravote"
					. "\n SET rating_count = rating_count + 1, rating_sum = rating_sum + " .  $user_rating . ", lastip = " . $db->Quote( $currip )
					. "\n WHERE content_id=".$cid." AND extra_id=".$xid;
					$db->setQuery( $query );
					$db->query() or die( $db->stderr() );
				} else {
					echo 'voted';
					exit();
				}
			}
		}
		echo 'thanks';
	}
}


vote.js

function JVXVote(id,i,total,total_count,xid,counter){
	var currentURL = window.location;
	var live_site = currentURL.protocol+'//'+currentURL.host+sfolder;
	var lsXmlHttp;
	
	var div = document.getElementById('extravote_'+id+'_'+xid);
	div.innerHTML='<img src="'+live_site+'/plugins/content/extravote/loading.gif" border="0" align="absmiddle" /> '+'<small>'+extravote_text[1]+'</small>';
	try	{
		lsXmlHttp=new XMLHttpRequest();
	} catch (e) {
		try	{ lsXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try { lsXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert(extravote_text[0]);
				return false;
			}
		}
	}
	lsXmlHttp.onreadystatechange=function() {
		var response;
		if(lsXmlHttp.readyState==4){
			setTimeout(function(){ 
				response = lsXmlHttp.responseText; 
				if(response=='thanks') div.innerHTML='<small>'+extravote_text[2]+'</small>';
				if(response=='login') div.innerHTML='<small>'+extravote_text[3]+'</small>';
				if(response=='voted') div.innerHTML='<small>'+extravote_text[4]+'</small>';
			},500);
			setTimeout(function(){
				if(response=='thanks'){
					var newtotal = total_count+1;
					var percentage = ((total + i)/(newtotal));
					document.getElementById('rating_'+id+'_'+xid).style.width=parseInt(percentage*20)+'%';
				}
				if(counter!=0){
					if(response=='thanks'){
						if(newtotal!=1)	
							var newvotes=newtotal+' '+extravote_text[5];
						else
							var newvotes=newtotal+' '+extravote_text[6];
						div.innerHTML='<small>( '+newvotes+' )</small>';
					} else {
						if(total_count!=0 || counter!=-1) {
							if(total_count!=1)
								var votes=total_count+' '+extravote_text[5];
							else
								var votes=total_count+' '+extravote_text[6];
							div.innerHTML='<small>( '+votes+' )</small>';
						} else {
							div.innerHTML='';
						}
					}
				} else {
					div.innerHTML='';
				}
			},2000);
		}
	}
	lsXmlHttp.open("GET",live_site+"/plugins/content/extravote/ajax.php?task=vote&user_rating="+i+"&cid="+id+"&xid="+xid,true);
	lsXmlHttp.send(null);
}



vote.css
div.extravote-container{display:inline-block;line-height:25px;vertical-align:middle;}
div.extravote-container-small{display:inline-block;line-height:22px;vertical-align:middle}
div.extravote-container ul.extravote-stars{height:25px;width:125px;padding:0!important;margin:0!important;}
div.extravote-container-small ul.extravote-stars-small{height:22px;width:110px;padding:0!important;margin:0!important;}
div.extravote-container ul.extravote-stars, div.extravote-container ul.extravote-stars li a:hover, div.extravote-container ul.extravote-stars li.current-rating {background-image:url(star.gif);background-color:transparent;background-repeat:repeat-x;background-position:left -1000px;padding:0!important;margin:0!important;}
div.extravote-container-small ul.extravote-stars-small, div.extravote-container-small ul.extravote-stars-small li a:hover, div.extravote-container-small ul.extravote-stars-small li.current-rating {background-image:url(star_small.gif);background-color:transparent;background-repeat:repeat-x;background-position:left -1000px;padding:0!important;margin:0!important;}
div.extravote-container ul.extravote-stars,div.extravote-container-small ul.extravote-stars-small {background-position:left top;list-style-image:none;list-style-position:outside;list-style-type:none;margin:0pt;overflow:hidden;padding:0pt;position:relative;}
div.extravote-container ul.extravote-stars li, div.extravote-container-small ul.extravote-stars-small li {background-image:none;display:inline;padding:0pt;}
div.extravote-container ul.extravote-stars li a, div.extravote-container ul.extravote-stars li.current-rating,div.extravote-container-small ul.extravote-stars-small li a, div.extravote-container-small ul.extravote-stars-small li.current-rating {border:medium none;cursor:pointer;left:0pt;outline-color:-moz-use-text-color;outline-style:none;outline-width:medium;overflow:hidden;position:absolute;text-indent:-1000em;top:0pt;}
div.extravote-container ul.extravote-stars li a, div.extravote-container ul.extravote-stars li.current-rating {height:25px;line-height:25px;}
div.extravote-container-small ul.extravote-stars-small li a, div.extravote-container-small ul.extravote-stars-small li.current-rating {height:22px;line-height:22px;}
div.extravote-container ul.extravote-stars li a:hover,div.extravote-container-small ul.extravote-stars-small li a:hover {background-position:left bottom;}
div.extravote-container ul.extravote-stars li.current-rating,div.extravote-container-small  ul.extravote-stars-small li.current-rating {background-position:left center;z-index:1;}
div.extravote-container ul.extravote-stars li a.one-star, div.extravote-container-small ul.extravote-stars-small li a.one-star {width:20%;padding:0;margin:0;z-index:6;}
div.extravote-container ul.extravote-stars li a.two-stars, div.extravote-container-small ul.extravote-stars-small li a.two-stars {width:40%;padding:0;margin:0;z-index:5;}
div.extravote-container ul.extravote-stars li a.three-stars, div.extravote-container-small ul.extravote-stars-small li a.three-stars {width:60%;padding:0;margin:0;z-index:4;}
div.extravote-container ul.extravote-stars li a.four-stars, div.extravote-container-small ul.extravote-stars-small li a.four-stars {width:80%;padding:0;margin:0;z-index:3;}
div.extravote-container ul.extravote-stars li a.five-stars, div.extravote-container-small ul.extravote-stars-small li a.five-stars {width:100%;padding:0;margin:0;z-index:2;}
.extravote-count {vertical-align:middle;}



Il y a quatre fichiers en tout.Merci d'avance! Smiley smile
Modifié par Fletch91 (14 Jan 2009 - 12:43)