Documentation TYPO3 par Ameos

class.tx_ttrating.php

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 ***************************************************************/
00044 /***************************************************************
00045 TODO
00046 
00047 > The rating idea is quite good - I'd like to see it extended to be a survey tool too if possible.. it
00048 >is often useful to see a little bargraph of the proportions of responses... rather than just the mean,
00049 >or a single figure answer. Of course you'll need to keep a total for each of the responses... but this
00050 >could realistically be limited to 10 at most.. perhaps less. The same information can be used to
00051 >generate either a mini bar chart - or a single figure overview.. perhaps a single bar chart..
00052 
00053 stats are written but there's no output yet
00054 ---
00055 check double ratings with IPs - are we paranoid?
00056 ---
00057 .ratingOutputSteps - Usefull if you don't want bars with values like 4.26 but 4.5
00058 ---
00059 sorting in list mode ???
00060 
00061 ****************************************************************/
00062 
00063 
00064 
00065 require_once(PATH_tslib."class.tslib_pibase.php");
00066 
00067 class tx_ttrating extends tslib_pibase {
00068         var $enableFields ="";          // The enablefields of the tt_rating table.
00069 
00070         var $config=array();
00071         var $conf=array();
00072 
00073         var $rateTable = "";
00074         var $rateTableUid = "";
00075         var $rateTableTitle = "";
00076 
00080         function main_rating($content,$conf)    {
00081 
00082                         // getting configuration values:
00083 
00084                 $this->conf=$conf;
00085 
00086                 $this->enableFields = $this->cObj->enableFields("tt_rating");
00087 
00088                 if ($this->conf["pid_list"] OR $this->conf["pid_list."]) {
00089                         $this->config["pid_list"] = trim($this->cObj->stdWrap($this->conf["pid_list"],$this->conf["pid_list."]));
00090                         $this->config["pid_list"] = $this->config["pid_list"] ? implode(t3lib_div::intExplode(",",$this->config["pid_list"]),",") : $GLOBALS["TSFE"]->page[uid];
00091                         list($pid) = explode(",",$this->config["pid_list"]);
00092                 }
00093 
00094                         // "CODE" decides what will be rendered:
00095                 $this->config["code"] = $this->cObj->stdWrap($this->conf["code"],$this->conf["code."]);
00096                 if (!$this->config["code"]) { $this->config["code"]=$this->conf["defaultCode"]; }
00097                 if (!$this->config["code"]) { $this->config["code"]="SHOWHELP"; } // dummy code to display help
00098 
00099                 $this->config["maxRating"] = max ($this->conf["highestRating"],$this->conf["lowestRating"]);
00100                 $this->config["minRating"] = min ($this->conf["highestRating"],$this->conf["lowestRating"]);
00101                 $this->config["defaultTitle"] = $this->cObj->stdWrap($this->conf["defaultTitle"],$this->conf["defaultTitle."]);
00102                 $this->config["defaultDescription"] = $this->cObj->stdWrap($this->conf["defaultDescription"],$this->conf["defaultDescription."]);
00103 
00104 
00105                         //  we decide if we are in content.list or content.shortcut
00106 
00107                 $items=array();
00108                 if (isset($pid) AND $this->cObj->data[CType]=="list") { // we are in tt_content.list
00109                         $items = $this->getItemList($pid);
00110                         if (!count($items) AND $this->conf["allowNew"]) { // there is no record so we create one if we are allowed to
00111                                 $items[] = $this->createNewItem($pid);
00112                         }
00113                 } elseif (isset($this->cObj->data[rating])) { // we have the data already - good
00114                                 $items[] = $this->cObj->data;
00115                 } elseif ( $this->conf["rateTable"]  ) {
00116                         $this->rateTable = $this->conf["rateTable"];
00117                         $this->rateTableUid = intval($this->cObj->stdWrap($this->conf["rateTableUid"],$this->conf["rateTableUid."]));
00118                         if (!$this->rateTableUid) {
00119                                 list (,$this->rateTableUid) = explode(":",$this->cObj->currentRecord);
00120                         }
00121                         $item = $this->getItempollLink($this->pollTable."_".$this->pollTableUid, $this->pollTablePid);
00122                         $this->rateTableTitle = $this->cObj->data["title"] ? $this->cObj->data["title"] : "";
00123                         $pid = intval($this->cObj->stdWrap($this->conf["rateTablePid"],$this->conf["rateTablePid."]));
00124                         $pid = $pid ? $pid : $GLOBALS["TSFE"]->id;
00125                         $item = $this->getItempollLink($this->pollTable."_".$this->pollTableUid, $pid);
00126                         if (is_array($item)) {
00127                                 $items[] = $item;
00128                         } elseif ($this->conf["allowNew"]) { // there is no record so we create one if we are allowed to
00129 // I'm not sure if this would be good                           $items[] = $this->createNewItem($pid, $this->pollTable."_".$this->pollTableUid);
00130 
00131                         }
00132                 } else {
00133                         // no data - what to do?
00134                         // should we output an error?
00135                 }
00136 
00137 
00138                         // *************************************
00139                         // *** Let's go
00140                         // *************************************
00141 
00142                 $codes=t3lib_div::trimExplode(",", strtoupper($this->config["code"]),1);
00143 
00144                 $content="";
00145                 reset($items);
00146                 while(list(,$item)=each($items))        {
00147 
00148                                 // check for double rating - is a cookie set, is a rating in register and are we not logged in as be user
00149                         $cookieName = "t3_tt_rating_rated_".$item["uid"];
00150                         $doubleRating = (isset($GLOBALS["HTTP_COOKIE_VARS"][$cookieName]) AND isset($GLOBALS["register"]["tt_rating"][$item["uid"]]["triedRating"]));
00151                         $doubleRating =  $GLOBALS["TSFE"]->beUserLogin ? FALSE : $doubleRating;
00152 //debug($GLOBALS["HTTP_COOKIE_VARS"]);
00153 //debug($GLOBALS["register"]["tt_rating"][$item["uid"]]["submittedRating"]);
00154 //debug($GLOBALS["register"]["tt_rating"][$item["uid"]]["triedRating"]);
00155 //debug($doubleRating);
00156 
00157                         reset($codes);
00158                         while(list(,$theCode)=each($codes))     {
00159                                 $theCode = (string)trim($theCode);
00160                                 switch($theCode)        {
00161                                         case "VOTEFORM":
00162                                                 $lConf = $this->conf["voteform."];
00163                                                         //we have to do it this way because [EDIT] is set also in the typoscript template
00164                                                 if (is_array($lConf["dataArray."]))     {
00165                                                         reset($lConf["dataArray."]);
00166                                                         while(list($key,$formEntry)=each($lConf["dataArray."])) {
00167                                                                 $lConf["dataArray."][$key]["type"] = str_replace ( "[EDIT]", '['.$item["uid"].']', $formEntry["type"]);
00168                                                         }
00169 
00170                                                         $lConf["dataArray."]["9990."] = array(
00171                                                                 "type" => "data[tt_rating][".$item["uid"]."][maxRating]=hidden",
00172                                                                 "value" => $this->config["maxRating"]
00173                                                         );
00174                                                         $lConf["dataArray."]["9991."] = array(
00175                                                                 "type" => "data[tt_rating][".$item["uid"]."][minRating]=hidden",
00176                                                                 "value" => $this->config["minRating"]
00177                                                         );
00178                                                         $lConf["dataArray."]["9992."] = array(
00179                                                                 "type" => "data[tt_rating][".$item["uid"]."][ratingStatSteps]=hidden",
00180                                                                 "value" => $this->conf["ratingStatSteps"]
00181                                                         );
00182                                                         $lConf["dataArray."]["9993."] = array(
00183                                                                 "type" => "clearCachePid=hidden",
00184                                                                 "value" => $GLOBALS["TSFE"]->id
00185                                                         );
00186                                                 } else {
00187                                                         $lConf["data"] = str_replace ( "[EDIT]", '['.$item["uid"].']', $lConf["data"]);
00188                                                         $lConf["data"] .= " ||  | data[tt_rating][".$item["uid"]."][maxRating]=hidden | ".$this->config["maxRating"];
00189                                                         $lConf["data"] .= " ||  | data[tt_rating][".$item["uid"]."][minRating]=hidden | ".$this->config["minRating"];
00190                                                         $lConf["data"] .= " ||  | data[tt_rating][".$item["uid"]."][ratingStatSteps]=hidden | ".$this->conf["ratingStatSteps"];
00191                                                         $lConf["data"] .= " ||  | clearCachePid=hidden | ".$GLOBALS["TSFE"]->id;
00192                                                 }
00193 
00194                                                 $content .= $this->cObj->FORM($lConf);
00195 
00196                                         break;
00197                                         case "RESULT":
00198                                                 if ($item["votes"] == 0) {
00199                                                         if (isset ($this->conf["noRatingObj."])) {
00200                                                                 $lConf = $this->conf["noRatingObj."];
00201                                                                 $content .= $this->cObj->cObjGetSingle ($this->conf["noRatingObj"], $lConf);
00202                                                         }
00203                                                 } else {
00204                                                         if (isset ($this->conf["renderObj."])) {
00205                                                                 $lConf = $this->conf["renderObj."];
00206                                                                 $markContentArray = array();
00207                                                                 $markContentArray["###VOTES###"] = $item["votes"];
00208                                                                 $markContentArray["###RATING###"] = $item["rating"];
00209                                                                 $markContentArray["###HIGHEST_RATING###"] = $this->conf["highestRating"];
00210                                                                 $markContentArray["###LOWEST_RATING###"] = $this->conf["lowestRating"];
00211                                                                 $markContentArray["###RATING_OUTPUT_STEPS###"] = $this->conf["ratingOutputSteps"];
00212                                                                 $markContentArray["###RATING_MUL###"] = (string)((int)($item["rating"] * 100.0 / (double)$this->conf["highestRating"]))."/100";
00213                                                                 $markContentArray["###RATING_FULL_WIDTH###"] = $this->conf["ratingOutputWidth"];
00214                                                                 $markContentArray["###RATING_WIDTH###"] = (int)((double)$this->conf["ratingOutputWidth"]*($item["rating"] / (double)$this->conf["highestRating"]));
00215                                                                 $markContentArray["###RATING_REMAIN_WIDTH###"] = (int)$markContentArray["###RATING_FULL_WIDTH###"] - (int)$markContentArray["###RATING_WIDTH###"];
00216                                                                         
00217                                                                         // rating feedback messages
00218                                                                 if($doubleRating) {
00219                                                                         //double rating
00220                                                                         $markContentArray["###RATING_MSG###"] = $this->conf["doubleRatingMsg"];
00221                                                                         $GLOBALS["TSFE"]->set_no_cache();
00222                                                                 } elseif ($GLOBALS["register"]["tt_rating"][$item["uid"]]["submittedRating"]) {
00223                                                                         //submitted rating
00224                                                                         $GLOBALS["TSFE"]->set_no_cache();
00225                                                                         $markContentArray["###RATING_MSG###"] = $this->conf["submittedRatingMsg"];
00226                                                                         $markContentArray["###RATING_MSG###"] = str_replace("###SUBMITTED_RATING###", $GLOBALS["register"]["tt_rating"][$item["uid"]]["submittedRating"], $markContentArray["###RATING_MSG###"]);
00227                                                                 } else {
00228                                                                         $markContentArray["###RATING_MSG###"] = "";
00229                                                                 }
00230                                                                 
00231                                                                 $this->cObj->substituteMarkerInObject ($lConf, $markContentArray);
00232                                                                 $content .= $this->cObj->cObjGetSingle ($this->conf["renderObj"], $lConf);
00233                                                         }
00234                                                 }
00235                                         break;
00236                                         default:
00237                                                 $langKey = strtoupper($GLOBALS["TSFE"]->config["config"]["language"]);
00238                                                 $helpTemplate = $this->cObj->fileResource("EXT:tt_rating/pi/rating_help.tmpl");
00239 
00240                                                         // Get language version
00241                                                 $helpTemplate_lang="";
00242                                                 if ($langKey)   {$helpTemplate_lang = $this->cObj->getSubpart($helpTemplate,"###TEMPLATE_".$langKey."###");}
00243                                                 $helpTemplate = $helpTemplate_lang ? $helpTemplate_lang : $this->cObj->getSubpart($helpTemplate,"###TEMPLATE_DEFAULT###");
00244 
00245                                                         // Markers and substitution:
00246                                                 $markerArray["###CODE###"] = $theCode;
00247                                                 $markerArray["###DEFAULTCODE###"] = $this->conf["defaultCode"];
00248                                                 $content.=$this->cObj->substituteMarkerArray($helpTemplate,$markerArray);
00249                                         break;
00250                                 }
00251                         }
00252                 }
00253                 return $this->cObj->wrap($content, $this->conf["wrap"]);
00254         }
00255 
00256 
00257 
00258                 // get a list of rating record from pid
00259         function getItemList($pid)      {
00260                 if (is_array($pid)) {
00261                         $pid = implode(',',$pid);
00262                 }
00263                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tt_rating', 'pid IN ('.$pid.')'.$this->enableFields, '', 'crdate DESC');
00264                 $out = array();
00265                 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))       {
00266                         $out[] = $row;
00267                 }
00268                 return $out;
00269         }
00270 
00271                 // get a 'linked' rating record
00272         function getItemRatingLink($recordlink, $pid)           {
00273                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tt_rating', 'recordlink="'.$GLOBALS['TYPO3_DB']->quoteStr($recordlink, 'tt_rating').'" AND pid='.intval($pid).$this->enableFields);
00274                 return $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
00275         }
00276 
00277 
00278                 // get a single rating record from uid
00279         function getItem($uid)          {
00280                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tt_rating', 'uid='.intval($uid).$this->enableFields);
00281                 return $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
00282         }
00283 
00284                 // create a new rating record
00285         function createNewItem($pid=0)  {
00286                 $pid = $pid ? $pid : $GLOBALS["TSFE"]->id;
00287                 $recordLink = "";
00288                 if ($this->rateTable AND $this->rateTableUid) {
00289                         $recordLink = $this->rateTable .":". $this->rateTableUid;
00290                 }
00291                 if (!$this->config["defaultTitle"] AND $this->rateTable AND $this->rateTableUid) {
00292                         if (!$this->rateTableTitle) {
00293                                 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->rateTable, 'uid='.intval($this->rateTableUid));
00294                                 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
00295                                 $this->rateTableTitle = $row['title'] ? $row['title'] : $recordLink;
00296                         }
00297                         $title = "rating: ".$this->rateTableTitle;
00298                 } else {
00299                         $title = $this->config["defaultTitle"] ? $this->config["defaultTitle"] : "rating: ".$GLOBALS["TSFE"]->page["title"];
00300                 }
00301 
00302                 $insertFields = array(
00303                         'pid' => $pid,
00304                         'recordlink' => $recordLink,
00305                         'title' => $title,
00306                         'description' => $this->config['defaultDescription'],
00307                         'rating' => '0',
00308                         'votes' => '0',
00309                         'tstamp' => time(),
00310                         'crdate' => time()
00311                 );
00312 
00313                 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tt_rating', $insertFields);
00314 
00315                 return $this->getItem($GLOBALS['TYPO3_DB']->sql_insert_id());
00316         }
00317 
00318 
00319 }
00320 
00321 
00322 
00323 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/tt_rating/pi/class.tx_ttrating.php"])  {
00324         include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/tt_rating/pi/class.tx_ttrating.php"]);
00325 }
00326 
00327 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6