So jetzt wäre das Problem gelöst. Letztendlich half nur die class.tx_ttrating.php umzuschreiben.
Zusätzlich zu den Schritten wie oben beschrieben muss Folgendes getan werden:
1. ein Feld uid_tt_news in der Datenbanktabelle tt_rating hinzufügen.
2. die function getAddParams aus der Extension ve_guestbook kopieren um die News Parameter auszulesen.
3. Die Voteform musste händisch zusammengebastelt werden, damit die news uid übergeben wird.
4. die uid_tt_news muss in die Datenbanktabelle eingetragen werden
5. statt der page id wird die uid_tt_news überprüft um einen neuen tt_rating Datensatz anzulegen
Anbei der gesamte php Code von class.tx_ttrating.php, Veränderungen sind mit // GS ganz links kommentiert. Ich habe es bei 2 Projekten getestet. Bei mir funktioniert es jedenfalls. Allerdings sollte das File nur verwendet werden, wenn auschließlich News Artikel bewertet werden sollen. Normale Artikel können damit nicht mehr so leicht bewertet werden.
<?php
/***************************************************************
* Copyright notice
*
* (c) 2001-2005 René Fritz (r.fritz@colorcube.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Rating plugin
* @author René Fritz <r.fritz@colorcube.de>
*/
require_once(PATH_tslib.'class.tslib_pibase.php');
class tx_ttrating_pi1 extends tslib_pibase {
var $enableFields =''; // The enablefields of the tt_rating table.
var $config=array();
var $conf=array();
var $rateTable = '';
var $rateTableUid = '';
var $rateTableTitle = '';
//GS für tt_news hinzugefügt
var $tt_news=array();
var $tt_news_url_params_list='tx_ttnews[tt_news],tx_ttnews[backPid],tx_ttnews[year],tx_ttnews[month],tx_ttnews[day],cHash';
/**
* Main method. Call this from TypoScript by a USER cObject.
*/
function main_rating($content,$conf) {
// getting configuration values:
$this->conf=$conf;
//debug($this->conf);
$this->enableFields = $this->cObj->enableFields('tt_rating');
if ($this->conf['pid_list'] OR $this->conf['pid_list.']) {
$this->config['pid_list'] = trim($this->cObj->stdWrap($this->conf['pid_list'],$this->conf['pid_list.']));
$this->config['pid_list'] = $this->config['pid_list'] ? implode(t3lib_div::intExplode(',',$this->config['pid_list']),',') : $GLOBALS['TSFE']->id;
list($pid) = explode(',',$this->config['pid_list']);
}
// "CODE" decides what will be rendered:
$this->config['code'] = $this->cObj->stdWrap($this->conf['code'],$this->conf['code.']);
if (!$this->config['code']) { $this->config['code']=$this->conf['defaultCode']; }
if (!$this->config['code']) { $this->config['code']='SHOWHELP'; } // dummy code to display help
$this->config['maxRating'] = max ($this->conf['highestRating'],$this->conf['lowestRating']);
$this->config['minRating'] = min ($this->conf['highestRating'],$this->conf['lowestRating']);
$this->config['defaultTitle'] = $this->cObj->stdWrap($this->conf['defaultTitle'],$this->conf['defaultTitle.']);
$this->config['defaultDescription'] = $this->cObj->stdWrap($this->conf['defaultDescription'],$this->conf['defaultDescription.']);
$this->processSubmit(t3lib_div::_GP('tx_ttrating'));
// we decide if we are in content.list or content.shortcut
$items=array();
//GS holt alle News Paramater mit der Funktion getAddParams
$this->tt_news = $this->getAddParams($this->tt_news_url_params_list);
//debug($this->tt_news_url_params_list);
//GS speichert die News ID in $uid_tt_news
$uid_tt_news =$this->tt_news['tx_ttnews[tt_news]'];
if (isset($pid) AND $this->cObj->data['CType']=='list') { // we are in tt_content.list
//$items = $this->getItemList($pid);
//GS statt den Datensatz mit der passenden Page Id wird jener mit der passenden News Id geholt
$items = $this->getItemList($uid_tt_news);
//debug($this->getItemList($pid));
//debug($pid);
if (!count($items) AND $this->conf['allowNew']) { // there is no record so we create one if we are allowed to
$items[] = $this->createNewItem($pid);
}
} elseif (isset($this->cObj->data['rating'])) { // we have the data already - good
$items[] = $this->cObj->data;
} elseif ( $this->conf['rateTable'] ) {
$this->rateTable = $this->conf['rateTable'];
$this->rateTableUid = intval($this->cObj->stdWrap($this->conf['rateTableUid'],$this->conf['rateTableUid.']));
if (!$this->rateTableUid) {
list (,$this->rateTableUid) = explode(':',$this->cObj->currentRecord);
}
$item = $this->getItemRatingLink($this->rateTable.'_'.$this->rateTableUid, $this->rateTablePid);
//debug($item);
$this->rateTableTitle = $this->cObj->data['title'] ? $this->cObj->data['title'] : '';
$pid = intval($this->cObj->stdWrap($this->conf['rateTablePid'],$this->conf['rateTablePid.']));
$pid = $pid ? $pid : $GLOBALS['TSFE']->id;
$item = $this->getItemRatingLink($this->rateTable.'_'.$this->rateTableUid, $pid);
if (is_array($item)) {
$items[] = $item;
} elseif ($this->conf['allowNew']) { // there is no record so we create one if we are allowed to
// I'm not sure if this would be good $items[] = $this->createNewItem($pid, $this->rateTable.'_'.$this->rateTableUid);
}
} else {
// no data - what to do?
// should we output an error?
}
// *************************************
// *** Let's go
// *************************************
$codes=t3lib_div::trimExplode(',', strtoupper($this->config['code']),1);
$content='';
foreach($items as $item) {
// check for double rating - is a cookie set, is a rating in register and are we not logged in as be user
$cookieName = 't3_tt_rating_rated_'.$item['uid'];
$doubleRating = (isset($GLOBALS['HTTP_COOKIE_VARS'][$cookieName]) AND isset($GLOBALS['register']['tx_ttrating'][$item['uid']]['triedRating']));
$doubleRating = $GLOBALS['TSFE']->beUserLogin ? false : $doubleRating;
//debug($GLOBALS['HTTP_POST_VARS']);
//debug($GLOBALS['HTTP_COOKIE_VARS']);
//debug($GLOBALS['register']['tx_ttrating'][$item['uid']]['submittedRating']);
//debug($GLOBALS['register']['tx_ttrating'][$item['uid']]['triedRating']);
//debug($doubleRating);
foreach($codes as $theCode) {
$theCode = (string)trim($theCode);
switch($theCode) {
case 'VOTEFORM':
if (!$GLOBALS['register']['tx_ttrating'][$item['uid']]['submittedRating']) {
$lConf = $this->conf['voteform.'];
//we have to do it this way because [EDIT] is set also in the typoscript template
if (is_array($lConf['dataArray.'])) {
foreach($lConf['dataArray.'] as $key => $formEntry) {
$lConf['dataArray.'][$key]['type'] = str_replace ( '[EDIT]', '['.$item['uid'].']', $formEntry['type']);
}
$lConf['dataArray.']['9990.'] = array(
'type' => 'tx_ttrating['.$item['uid']."][maxRating]=hidden",
'value' => $this->config['maxRating']
);
$lConf['dataArray.']['9991.'] = array(
'type' => 'tx_ttrating['.$item['uid']."][minRating]=hidden",
'value' => $this->config['minRating']
);
$lConf['dataArray.']['9992.'] = array(
'type' => 'tx_ttrating['.$item['uid']."][ratingStatSteps]=hidden",
'value' => $this->conf['ratingStatSteps']
);
$lConf['dataArray.']['9993.'] = array(
'type' => 'clearCachePid=hidden',
'value' => $GLOBALS['TSFE']->id
);
$lConf['dataArray.']['9994.'] = array(
'type' => 'newsID=hidden',
'value' => $url
);
} else {
$lConf['data'] = str_replace ( '[EDIT]', '['.$item['uid'].']', $lConf['data']);
$lConf['data'] .= ' || | tx_ttrating['.$item['uid'].'][maxRating]=hidden | '.$this->config['maxRating'];
$lConf['data'] .= ' || | tx_ttrating['.$item['uid'].'][minRating]=hidden | '.$this->config['minRating'];
$lConf['data'] .= ' || | tx_ttrating['.$item['uid'].'][ratingStatSteps]=hidden | '.$this->conf['ratingStatSteps'];
$lConf['data'] .= ' || | clearCachePid=hidden | '.$GLOBALS['TSFE']->id;
}
//GS 05.10. holt den URL mit der News ID
$url = $_SERVER['QUERY_STRING'];
//debug($url);
//$content .= $this->cObj->FORM($lConf );
// GS 05.10.06 das Formular wird jetzt hier zusammengebastelt und übergibt die News ID bei der Abstimmung
$content .= '<form action="index.php?amp;no_cache=1&'.$url.'" name="a338a2042dee98fb1bb74359fc1976446" enctype="multipart/form-data" method="post"><div style="display:none;"><input type="hidden" name="tx_ttrating['.$item['uid'].'][maxRating]" value="5" /><input type="hidden" name="tx_ttrating['.$item['uid'].'][minRating]" value="1" /><input type="hidden" name="tx_ttrating['.$item['uid'].'][ratingStatSteps]" value="5" /><input type="hidden" name="clearCachePid" value="'.$pid.'" /></div> <!-- Rating form: [begin] --> <table border="0" cellspacing="1" cellpadding="1"><tr><td><select name="tx_ttrating['.$item['uid'].'][rating]" size="1"><option value="0">0 - Dieser Artikel ist</option><option value="5">5 - Exzellent</option><option value="4">4 - Gut</option><option value="3">3 - In Ordnung</option><option value="2">2 - Sollte noch dran gearbeitet werden</option><option value="1">1 - Mangelhaft</option></select></td><td> </td><td><input type="submit" name="formtype_db" value="Bewerten" /></td><td> </td></tr></table> </form>';
//print $content;
}
break;
case 'RESULT':
if ($this->for_tt_news){
$temp_where=' uid_tt_news='.intval($this->tt_news['tx_ttnews[tt_news]']);
}
//debug($temp_where);
if ($item['votes'] == 0) {
if (isset ($this->conf['noRatingObj.'])) {
$lConf = $this->conf['noRatingObj.'];
$content .= $this->cObj->cObjGetSingle ($this->conf['noRatingObj'], $lConf);
}
} else {
if (isset ($this->conf['renderObj.'])) {
$lConf = $this->conf['renderObj.'];
$markContentArray = array();
$markContentArray['###VOTES###'] = $item['votes'];
$markContentArray['###RATING###'] = $item['rating'];
$markContentArray['###HIGHEST_RATING###'] = $this->conf['highestRating'];
$markContentArray['###LOWEST_RATING###'] = $this->conf['lowestRating'];
$markContentArray['###RATING_OUTPUT_STEPS###'] = $this->conf['ratingOutputSteps'];
$markContentArray['###RATING_MUL###'] = (string)((int)($item['rating'] * 100.0 / (double)$this->conf['highestRating'])).'/100';
$markContentArray['###RATING_FULL_WIDTH###'] = $this->conf['ratingOutputWidth'];
$markContentArray['###RATING_WIDTH###'] = (int)((double)$this->conf['ratingOutputWidth']*($item['rating'] / (double)$this->conf['highestRating']));
$markContentArray['###RATING_REMAIN_WIDTH###'] = (int)$markContentArray['###RATING_FULL_WIDTH###'] - (int)$markContentArray['###RATING_WIDTH###'];
// rating feedback messages
if($doubleRating) {
//double rating
$markContentArray['###RATING_MSG###'] = $this->conf['doubleRatingMsg'];
$GLOBALS['TSFE']->set_no_cache();
} elseif ($GLOBALS['register']['tx_ttrating'][$item['uid']]['submittedRating']) {
//submitted rating
$GLOBALS['TSFE']->set_no_cache();
$markContentArray['###RATING_MSG###'] = $this->conf['submittedRatingMsg'];
$markContentArray['###RATING_MSG###'] = str_replace('###SUBMITTED_RATING###', $GLOBALS['register']['tx_ttrating'][$item['uid']]['submittedRating'], $markContentArray['###RATING_MSG###']);
} else {
$markContentArray['###RATING_MSG###'] = '';
}
$this->cObj->substituteMarkerInObject ($lConf, $markContentArray);
$content .= $this->cObj->cObjGetSingle ($this->conf['renderObj'], $lConf);
}
}
break;
default:
$langKey = strtoupper($GLOBALS['TSFE']->config['config']['language']);
$helpTemplate = $this->cObj->fileResource('EXT:tt_rating/res/rating_help.tmpl');
// Get language version
$helpTemplate_lang='';
if ($langKey) {$helpTemplate_lang = $this->cObj->getSubpart($helpTemplate,"###TEMPLATE_".$langKey.'###');}
$helpTemplate = $helpTemplate_lang ? $helpTemplate_lang : $this->cObj->getSubpart($helpTemplate,'###TEMPLATE_DEFAULT###');
// Markers and substitution:
$markerArray['###RESPATH###'] = t3lib_extMgm::siteRelPath('tt_rating').'res/';
$markerArray['###CODE###'] = $theCode;
$markerArray['###DEFAULTCODE###'] = $this->conf['defaultCode'];
$content.=$this->cObj->substituteMarkerArray($helpTemplate,$markerArray);
break;
}
}
}
return $this->cObj->wrap($content, $this->conf['wrap']);
}
/**
* get a list of rating record from uid_tt_news
*
* @param integer uid_tt_news id
*/
//GS 06.10. alle $pid wurden durch $uid_tt_news ersetzt
function getItemList($uid_tt_news) {
if (is_array($uid_tt_news)) {
$uid_tt_news = implode(',',$uid_tt_news);
}
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tt_rating', 'uid_tt_news IN ('.$uid_tt_news.')'.$this->enableFields, '', 'crdate DESC');
$out = array();
while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
$out[] = $row;
//print_r($out);
}
return $out;
}
/**
* get a 'linked' rating record
*
* @param string identificator to other record
* @param integer page id
*/
function getItemRatingLink($recordlink, $pid) {
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tt_rating', 'recordlink="'.$GLOBALS['TYPO3_DB']->quoteStr($recordlink, 'tt_rating').'" AND pid='.intval($pid).$this->enableFields);
return $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
}
/**
* get a single rating record from uid
*
* @param integer tt_rating uid
*/
function getItem($uid) {
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tt_rating', 'uid='.intval($uid).$this->enableFields);
return $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
}
/**
* create a new rating record
*
* @param integer page id
*/
function createNewItem($pid=0) {
$pid = $pid ? $pid : $GLOBALS['TSFE']->id;
$recordLink = '';
if ($this->rateTable AND $this->rateTableUid) {
$recordLink = $this->rateTable .':'. $this->rateTableUid;
}
if (!$this->config['defaultTitle'] AND $this->rateTable AND $this->rateTableUid) {
if (!$this->rateTableTitle) {
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->rateTable, 'uid='.intval($this->rateTableUid));
$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
$this->rateTableTitle = $row['title'] ? $row['title'] : $recordLink;
}
$title = 'rating: '.$this->rateTableTitle;
} else {
$title = $this->config['defaultTitle'] ? $this->config['defaultTitle'] : 'rating: '.$GLOBALS['TSFE']->page['title'];
}
//GS 06.10. Variable für die UID News und diese auch in die Datenbank schreiben
$uid_tt_news =$this->tt_news['tx_ttnews[tt_news]'];
$insertFields = array(
'pid' => $pid,
'recordlink' => $recordLink,
'title' => $title,
'description' => $this->config['defaultDescription'],
'rating' => '0',
'votes' => '0',
'tstamp' => time(),
'crdate' => time(),
'uid_tt_news' => $uid_tt_news
);
$GLOBALS['TYPO3_DB']->exec_INSERTquery('tt_rating', $insertFields);
return $this->getItem($GLOBALS['TYPO3_DB']->sql_insert_id());
}
/**
* used for submission of the rating value to the database.
*
* @param array form data
*/
function processSubmit($data) {
if (is_array($data)) {
$uid = key($data);
$cookieName = 't3_tt_rating_rated_'.$uid;
// rating is allowed if cookie not set or the user is logged in the backend
if (((!isset($GLOBALS['HTTP_COOKIE_VARS'][$cookieName])) OR ($GLOBALS['TSFE']->beUserLogin)) && ($data[$uid]['rating'])) {
// we need the old data to calc the new one
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tt_rating', 'uid='.intval($uid));
$curRating = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
// we got the data so let's start
if (is_array ($curRating)) {
#TODO this is no longer needed and can be replaced by TS conf values
// this is a trick to get the configuration
// we passed these config values like record fields
// we need these values for calculation
$ratingConfig['minRating'] = $data[$uid]['minRating'];
unset ($data[$uid]['minRating']);
$ratingConfig['maxRating'] = $data[$uid]['maxRating'];
unset ($data[$uid]['maxRating']);
$ratingConfig['ratingStatSteps'] = $data[$uid]['ratingStatSteps'];
unset ($data[$uid]['ratingStatSteps']);
// calc the new data
$submittedRating = max($ratingConfig['minRating'],min($data[$uid]['rating'],$ratingConfig['maxRating']));
if ($submittedRating) {
$data[$uid]['rating'] = (($curRating['rating'] * $curRating['votes'])+ $submittedRating) / ++$curRating['votes']; // calc the new rating
$data[$uid]['rating'] = max ($data[$uid]['rating'] ,$ratingConfig['minRating']); // check it - it's maybe better to do so
$data[$uid]['rating'] = min ($data[$uid]['rating'] ,$ratingConfig['maxRating']);
$data[$uid]['rating'] = number_format ($data[$uid]['rating'] ,2 ,'.' ,''); // set the number format to xxx.xx
$data[$uid]['votes'] = $curRating['votes'];
$GLOBALS['register']['tx_ttrating'][$uid]['submittedRating'] = $submittedRating;
// this makes a statistic how many times each rating is voted
if ($curRating['votes']) {
$ratingStat = explode ('|', $curRating['ratingstat']);
} else {
$ratingStat = '';
}
for ($i = 0; $i < $ratingConfig['ratingStatSteps']; $i++) {
$ratingStat[$i]=$ratingStat[$i]? $ratingStat[$i]: 0;
}
$a = ((double)($ratingConfig['maxRating']-$ratingConfig['minRating']+1)/$ratingConfig['ratingStatSteps']);
$b = (double)($submittedRating - $ratingConfig['minRating']+1);
$ratingStat[ (int)round ($b/$a)-1 ]++;
$data[$uid]['ratingstat'] = implode('|',$ratingStat);
// prepare for database update
$updateFields = array();
unset($data[$uid]['uid']);
unset($data[$uid]['pid']);
$data[$uid]['tstamp'] = time();
t3lib_div::loadTCA('tt_rating');
foreach($data[$uid] as $f => $v) {
if (t3lib_div::inList('tstamp',$f) || isset($GLOBALS['TCA']['tt_rating']['columns'][$f])) {
$updateFields[$f] = $v;
}
}
// write to database
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_rating', 'uid='.intval($uid), $updateFields);
// set a cookie to prevent double ratings
setcookie($cookieName,$submittedRating,time()+(3600*24*10)); // delete cookie after 10 days
// clear the page cache
if ( $id = intval(t3lib_div::_GP('clearCachePid')) ) {
$id = $GLOBALS['TSFE']->id;
}
$GLOBALS['TSFE']->clearPageCacheContent_pidList($id);
}
}
} else {
// user did already a rating - the display code have to know that he tried again
$GLOBALS['register']['tx_ttrating'][$uid]['triedRating'] = $data[$uid]['rating'];
}
}
}
/**
* Returns an array with additional Link parameters
* from chc-forum @author Zach Davis <zach@crito.org>
* added by GS 05.10.2006
* @param string $addParamsList: comma-seperated list of parameters that will be added to all links.
* @return array additional link parameters in an array
*/
//GS aus der Extension ve_guestbook kopiert
function getAddParams($addParamsList){
$queryString = explode('&', t3lib_div::implodeArrayForUrl('', $GLOBALS['_GET'])) ;
if ($queryString) {
while (list(, $val) = each($queryString)) {
$tmp = explode('=', $val);
$paramArray[$tmp[0]] = $tmp[1];
}
while (list($pk, $pv) = each($paramArray)) {
if (t3lib_div::inList($addParamsList, $pk)) {
$addParamArray[$pk]=$pv ;
}
}
}
return $addParamArray;
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/tt_rating/pi1/class.tx_ttrating_pi1.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/tt_rating/pi1/class.tx_ttrating_pi1.php']);
}
?>
vG
mistakanista