Documentation TYPO3 par Ameos

rating_submit.inc

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2001-2004 René Fritz (r.fritz@colorcube.de)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00039 if (is_object($this))   {
00040 
00041         if (is_array($this->newData["tt_rating"]))      {
00042                 list($uid,)=each($this->newData["tt_rating"]);
00043 
00044                 $cookieName = "t3_tt_rating_rated_".$uid;
00045 
00046                         // rating is allowed if cookie not set or the user is logged in the backend
00047                 if (((!isset($GLOBALS["HTTP_COOKIE_VARS"][$cookieName])) OR ($GLOBALS["TSFE"]->beUserLogin)) && ($this->newData["tt_rating"][$uid]["rating"])) {
00048 
00049                                 // we need the old data to calc the new one
00050                         $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tt_rating', 'uid='.intval($uid));
00051                         $actRating = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
00052 
00053                                 // we got the data so let's start
00054                         if (is_array ($actRating)) {
00055 
00056                                         // this is a trick to get the configuration
00057                                         // we passed these config values like record fields
00058                                         // we need these values for calculation
00059                                 $ratingConfig["minRating"] = $this->newData["tt_rating"][$uid]["minRating"];
00060                                 unset ($this->newData["tt_rating"][$uid]["minRating"]);
00061                                 $ratingConfig["maxRating"] = $this->newData["tt_rating"][$uid]["maxRating"];
00062                                 unset ($this->newData["tt_rating"][$uid]["maxRating"]);
00063                                 $ratingConfig["ratingStatSteps"] = $this->newData["tt_rating"][$uid]["ratingStatSteps"];
00064                                 unset ($this->newData["tt_rating"][$uid]["ratingStatSteps"]);
00065 
00066                                         // calc the new data
00067                                 $submittedRating = max($ratingConfig["minRating"],min($this->newData["tt_rating"][$uid]["rating"],$ratingConfig["maxRating"]));
00068                                 if ($submittedRating) {
00069                                         $this->newData["tt_rating"][$uid]["rating"] = (($actRating["rating"] * $actRating["votes"])+ $submittedRating) / ++$actRating["votes"]; // calc the new rating
00070                                         $this->newData["tt_rating"][$uid]["rating"] = max ($this->newData["tt_rating"][$uid]["rating"] ,$ratingConfig["minRating"]); // check it - it's maybe better to do so
00071                                         $this->newData["tt_rating"][$uid]["rating"] = min ($this->newData["tt_rating"][$uid]["rating"] ,$ratingConfig["maxRating"]);
00072                                         $this->newData["tt_rating"][$uid]["rating"] = number_format ($this->newData["tt_rating"][$uid]["rating"] ,2 ,"." ,""); // set the number format to xxx.xx
00073                                         $this->newData["tt_rating"][$uid]["votes"] = $actRating["votes"];
00074 
00075                                         $GLOBALS["register"]["tt_rating"][$uid]["submittedRating"] = $submittedRating;
00076                                 
00077                                                 // this makes a statistic how many times each rating is voted
00078                                         if ($actRating["votes"]) {
00079                                                 $ratingStat = explode ("|", $actRating["ratingstat"]);
00080                                         } else {
00081                                                 $ratingStat = "";
00082                                         }
00083                                         for ($i = 0; $i < $ratingConfig["ratingStatSteps"]; $i++) {
00084                                                 $ratingStat[$i]=$ratingStat[$i]? $ratingStat[$i]: 0;
00085                                         }
00086                                         $a = ((double)($ratingConfig["maxRating"]-$ratingConfig["minRating"]+1)/$ratingConfig["ratingStatSteps"]);
00087                                         $b = (double)($submittedRating - $ratingConfig["minRating"]+1);
00088                                         $ratingStat[ (int)round ($b/$a)-1 ]++;
00089                                         $this->newData["tt_rating"][$uid]["ratingstat"] = implode("|",$ratingStat);
00090 
00091                                                 // prepare for database update
00092                                         $updateFields = array();
00093                                         
00094                                         unset($this->newData["tt_rating"][$uid]["uid"]);
00095                                         unset($this->newData["tt_rating"][$uid]["pid"]);
00096                                         $this->newData["tt_rating"][$uid]["tstamp"] = time();
00097 
00098                                         while(list($f,$v)=each($this->newData["tt_rating"][$uid]))      {
00099                                                 if (t3lib_div::inList("tstamp",$f) || isset($GLOBALS["TCA"]["tt_rating"]["columns"][$f]))       {
00100                                                         $updateFields[$f] = $v;
00101                                                 }
00102                                         }
00103 
00104                                                 // write to database
00105                                         $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_rating', 'uid='.intval($uid), $updateFields);
00106 
00107                                                 // set a cookie to prevent double ratings
00108                                         setcookie($cookieName,$submittedRating,time()+(3600*24*10)); // delete cookie after 10 days
00109 
00110                                                 // clear the page cache
00111                         if ( $id = t3lib_div::_GP("clearCachePid") )    {
00112                                                 $this->clear_cacheCmd(intval($id));
00113                                         }
00114                                 }
00115                         }
00116                 } else {
00117                                 // user did already a rating - the display code have to know that he tried again
00118                         $GLOBALS["register"]["tt_rating"][$uid]["triedRating"] = $this->newData["tt_rating"][$uid]["rating"];
00119                 }
00120         }
00121 }
00122 
00123 
00124 
00125 
00126 ?>


Généré par Les experts TYPO3 avec  doxygen 1.4.6