Mein erster Mod, My first Mod
EN: This Mod will make it possible to rate a picture without that the picture reloads.
DE: Durch diesen Mod können Bilder ohne reload bewertet werden.
DEMO:
http://www.konradin.net/http://www.konradin.net/galerie/img879.htmor something that makes the same but don't know if uses the same script ist the rating form on Vanos page.
auf Vanos Page wird glaube ich durch das gleiche Prinzip gehandelt, dort kann man sich also das Ergebnis ansehen.
STEP 1: Create a rating.php in your 4images folder with the following content (Please check Gurus!)
<?php
/**************************************************************************
* *
* 4images - A Web Based Image Gallery Management System *
* ---------------------------------------------------------------- *
* *
* File: rateimage.php *
* Copyright: (C) 2002 Jan Sorgalla *
* Email: jan@4homepages.de *
* Web: http://www.4homepages.de *
* Scriptversion: 1.7.2 *
* *
* Never released without support from: Nicky (http://www.nicky.net) *
* *
**************************************************************************
* *
* Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz- *
* bedingungen (Lizenz.txt) fьr weitere Informationen. *
* --------------------------------------------------------------- *
* This script is NOT freeware! Please read the Copyright Notice *
* (Licence.txt) for further information. *
* *
*************************************************************************/
$rating = $_REQUEST['rating'];
$id = $_REQUEST['id'];
$action = "rateimage";
$main_template = '';
$nozip = 1;
define('GET_CACHES', 1);
define('ROOT_PATH', './');
include(ROOT_PATH.'global.php');
require(ROOT_PATH.'includes/sessions.php');
$user_access = get_permission();
function update_image_rating_new($image_id, $rating, $image_row, &$new_rating, &$old_votes) {
global $site_db;
$old_votes = $image_row['image_votes'];
$old_rating = $image_row['image_rating'];
$new_rating = (($old_rating * $old_votes) + $rating) / ($old_votes + 1);
$new_rating = sprintf("%.2f", $new_rating);
$sql = "UPDATE ".IMAGES_TABLE."
SET image_votes = ($old_votes + 1), image_rating = '$new_rating'
WHERE image_id = $image_id";
return $site_db->query($sql);
}
//-----------------------------------------------------
//--- Save Rating -------------------------------------
//-----------------------------------------------------
if ($id) {
$rating = intval($rating);
$cookie_name = (defined("COOKIE_NAME")) ? COOKIE_NAME : "4images_";
$cookie_rated = isset($HTTP_COOKIE_VARS[$cookie_name.'rated']) ? unserialize(stripslashes($HTTP_COOKIE_VARS[$cookie_name.'rated'])) : array();
$sql = "SELECT cat_id, image_votes, image_rating
FROM ".IMAGES_TABLE."
WHERE image_id = $id";
$image_row = $site_db->query_firstrow($sql);
if (check_permission("auth_vote", $image_row['cat_id']))
{
if ($rating && $rating <= MAX_RATING && $id) {
if (!isset($session_info['rated_imgs'])) {
$session_info['rated_imgs'] = $site_sess->get_session_var("rated_imgs");
}
$split_list = array();
if (!empty($session_info['rated_imgs'])) {
$split_list = explode(" ", $session_info['rated_imgs']);
}
if (!in_array($id, $split_list) && !in_array($id, $cookie_rated)) {
if (update_image_rating_new($id, $rating, $image_row, $new_rating, $old_votes))
{
$session_info['rated_imgs'] .= " ".$id;
$session_info['rated_imgs'] = trim($session_info['rated_imgs']);
$site_sess->set_session_var("rated_imgs", $session_info['rated_imgs']);
$cookie_rated[] = $id;
$cookie_expire = time() + 60 * 60 * 24 * 4;
setcookie($cookie_name.'rated', serialize($cookie_rated), $cookie_expire, COOKIE_PATH, COOKIE_DOMAIN, COOKIE_SECURE);
$msg = $lang['voting_success'];
$votes = $old_votes + 1;
echo "kbdisplay||".$new_rating." (".$votes." ".$lang['votes'].")||";
}
else
{
$msg = $lang['voting_error'];
}
}
else {
$msg = $lang['already_voted'];
}
}
else {
$msg = $lang['voting_error'];
}
}
else
{
$msg = $lang['no_permission'];
}
}
else
{
$msg = "Security violation";
}
die("kbrating||".$msg);
?>
STEP 2Open rate_form.html and delete everything. After that insert the following:
<span id="kbrating">
<a href="javascript:rate('1');">Rate 1</a><br />
<a href="javascript:rate('2');">Rate 2</a><br />
<a href="javascript:rate('3');">Rate 3</a><br />
<a href="javascript:rate('4');">Rate 4</a><br />
<a href="javascript:rate('5');">Rate 5</a><br />
</span>
STEP 3Open header.html of your templates and insert in the HEAD-Section the following Code:
<script type="text/javascript" language="Javascript">
function createXMLHttpRequest() {
var ua;
if(window.XMLHttpRequest) {
try {
ua = new XMLHttpRequest();
} catch(e) {
ua = false;
}
} else if(window.ActiveXObject) {
try {
ua = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
ua = false;
}
}
return ua;
}
var req = createXMLHttpRequest();
function rate(id) {
req.open('get', '{rating_url}&rating=' + id);
req.onreadystatechange = handleResponse;
req.send(null);
}
function handleResponse() {
if(req.readyState <= 3){
var text = '{lang_loading}';
var divid = 'kbrating';
document.getElementById(divid).innerHTML = text;
}
if(req.readyState == 4){
var response = req.responseText;
var update = new Array();
if(response.indexOf('||' != -1)) {
update = response.split('||');
document.getElementById(update[0]).innerHTML = update[1];
document.getElementById(update[2]).innerHTML = update[3];
}
}
}
</script>
STEP 4in details.html of your templates replace
{image_rating} ({image_votes} {lang_votes})
with
<span id="kbdisplay">{image_rating} ({image_votes} {lang_votes})</span>
STEP 5open page_header.php in folder /includes/ and look for:
"direction" => $lang['direction']
replace with:
"direction" => $lang['direction'],
"lang_loading" => $lang['loading']
add to your language files in your language:
$lang['loading'] = "wird geladen...";
or
$lang['loading'] = "loading...";
STEP 6add anywhere in the details.php (or in the page_header.php if you want to use the rate form in other pages too) before the template-printout:
$site_template->register_vars("rating_url", ROOT_PATH."rating.php?id=".$image_id);
Done.
That should work - without support for rating scalas or Rating Stars but surely there`s somebody that helps me to integrate this too in a few seconds.
You can modify "Rate 1", "Rate 2", etc... with everything you want - stars, another text, etc.
Bye Konradin
Thx too Vano for piece of code
Edit 9. May, 23.50 -> radom.php to rating.php
-> Add {rating_url} registering to details.php