From d89a395889a58983b352fcc64640b157c9615cb5 Mon Sep 17 00:00:00 2001 From: disappearer Date: Wed, 4 Jan 2006 10:07:50 +0000 Subject: [PATCH] Adding initial rating stuff. git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/terfe/trunk@2447 735d13b6-9817-0410-8766-e36946ffe9aa --- ChangeLog | 3 + ext_tables.sql | 17 +++ pi1/class.tx_terfe_pi1.php | 15 ++- pi1/class.tx_terfe_ratings.php | 216 +++++++++++++++++++++++++++++++++ pi1/locallang.php | 3 +- 5 files changed, 250 insertions(+), 4 deletions(-) create mode 100644 pi1/class.tx_terfe_ratings.php diff --git a/ChangeLog b/ChangeLog index f4e7c30e..aeb6f11c 100755 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ (add new changes on top of this file) +07-01-02 Michael Scharkow + * Initial upload of rating framework (in p1) + 06-01-02 Michael Scharkow * Initial upload of /pi3 (Reviewing framework) diff --git a/ext_tables.sql b/ext_tables.sql index 4f19b64d..105f534c 100755 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -80,6 +80,22 @@ CREATE TABLE tx_terfe_reviewnotes ( PRIMARY KEY (uid) ); + +# Table structure for review ratings + +CREATE TABLE `tx_terfe_ratings` ( + `uid` int(11) NOT NULL auto_increment, + `extensionkey` varchar(255) NOT NULL default '', + `version` varchar(255) NOT NULL default '', + `username` varchar(255) NOT NULL default '', + `rating` int(1) NOT NULL default '0', + `notes` text NOT NULL, + `tstamp` int(11) NOT NULL default '0', + PRIMARY KEY (`uid`) +) TYPE = MYISAM ; + + + # # Table structure for table 'tx_terfe_reviewemails' # @@ -96,3 +112,4 @@ CREATE TABLE tx_terfe_reviewemails ( subject tinytext NOT NULL, PRIMARY KEY (uid) ); + diff --git a/pi1/class.tx_terfe_pi1.php b/pi1/class.tx_terfe_pi1.php index 7149957a..f88c5738 100755 --- a/pi1/class.tx_terfe_pi1.php +++ b/pi1/class.tx_terfe_pi1.php @@ -432,7 +432,7 @@ class tx_terfe_pi1 extends tslib_pibase { // Prepare the top menu items: if (!$this->piVars['extView']) $this->piVars['extView'] = 'info'; - $menuItems = array ('info', 'details', 'feedback'); + $menuItems = array ('info', 'details', 'feedback','rating'); // Render the top menu $topMenu = ''; @@ -449,7 +449,15 @@ class tx_terfe_pi1 extends tslib_pibase { break; case 'feedback' : $subContent = $this->renderSingleView_feedbackForm ($extRow); - break; + break; + + // ADDED BY MICHAEL SCHARKOW, just using another class for all the rating stuff! + case 'rating': + require_once('class.tx_terfe_ratings.php'); + $rating = new tx_terfe_ratings($extRow,$this); + $subContent = $rating->renderSingleView_rating(); + + break; case 'info': default: $subContent = $this->renderSingleView_extensionInfo ($extRow); @@ -578,6 +586,7 @@ class tx_terfe_pi1 extends tslib_pibase { return $content; } + /** * Renders the feedback sub view of an extension single view * @@ -1337,4 +1346,4 @@ if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/ter_fe/ include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/ter_fe/pi1/class.tx_terfe_pi1.php']); } -?> \ No newline at end of file +?> diff --git a/pi1/class.tx_terfe_ratings.php b/pi1/class.tx_terfe_ratings.php new file mode 100644 index 00000000..d163b2a3 --- /dev/null +++ b/pi1/class.tx_terfe_ratings.php @@ -0,0 +1,216 @@ + 'useless',2=> 'ok',3=>'average',4=>'good',5=>'excellent'); + + function __construct($extRow,&$backref){ + global $TSFE; + $this->extRow = $extRow; + $this->extensionKey = $extRow['extensionkey']; + $this->version = $extRow['version']; + $this->username = $TSFE->fe_user->user['username']; + $this->backRef = &$backref; + } + + + + + function renderSingleView_rating() { + global $TSFE; + // return $this->db_getRatings($this->extensionKey,$this->version,$this->username); + + if (t3lib_div::_GP('rating') && $this->can_be_rated()){ + $res = $this->db_saveRating(t3lib_div::_GP('rating'),t3lib_div::_GP('notes'),$this->username); + if ($res) { + tslib_fe::clearPageCacheContent_pidList($TSFE->id); + $justsaved = 1; + } else { + $output .= "Could not save!"; + } + + } + // Rendering current rating + $output = 'Average rating: '; + +// $rating = $this->db_getAvgRating($this->extensionKey); +// $output .= $this->num_to_star($rating['average']); + + // Rendering form + if (!$justsaved && $this->can_be_rated()){ + $output .= $this->render_rating_form(); + } + + // Rendering rating history + $ratings = $this->db_getRatings($this->extensionKey,$this->version); + if (is_array($ratings)){ + + $output .= $this->render_rating($rating_summary[$this->version]['avg'],$rating_summary[$this->version]['count']); + + $output .= '

Ratings for this extension version

'; + + foreach ($ratings as $rating){ + $output .= ''; + } + $output .= '
UserRatingNotes
'.$rating['username'].''.$this->num_to_star($rating['rating']).''.htmlspecialchars($rating['notes']). + '
'; + } + return $output; + } + + function render_rating($avg, $num){ + $output = ''; + $output .= $avg .'('.$num.' ratings)'; + $output .= ''; + return $output; + + } + + + function can_be_rated(){ + + if(!$this->username){ + return False; + } + + if ($this->extRow['authorname'] == $this->username){ + return False; + } + if ($ratings = $this->db_getRatings($this->extensionKey, $this->version, $this->username)){ + return False; + } + return True; + + } + + + function render_rating_form(){ + + $output = '
'; + + // $output .= '
'; + $outrows = array(); + for ($i=$this->minRating;$i <= $this->maxRating; $i++){ + $outrows[] = '
'; + } + + $output .= implode('',array_reverse($outrows)); + // $output .= '
'; + + $output .= '
'; + $output .= ''; + $output .= '
'; + return $output; + + + } + + function rating_summary($ratings){ + if (!is_array($ratings)){ + return False; + } + + $rating_counter = array(); + + foreach ($ratings as $rating_row){ + $rating_counter[$rating_row['version']][] = $rating_row['rating']; + } + + $rating_avgs = array(); + + foreach ($rating_counter as $version => $rating_arr){ + $rating_avgs[$version]['avg'] = array_sum($rating_arr)/count($rating_arr); + $rating_avgs[$version]['count'] = count($rating_arr); + } + return $rating_avgs; + } + + function get_valid_avg($rating_summary,$version){ + /* wenn genug Einträge, dann aktuelle Version, sonst ältere mit genug oder alle? */ + + } + + + + function num_to_star($int){ + $roundedvalue = round(($int*2), 0)/2; + $img = ''; + return $img; + + } + + + + + function db_saveRating($rating, $notes=''){ + + if ( $rating > $this->maxRating || $rating < $this->minRating) {return False;} + global $TYPO3_DB; + $table = 'tx_terfe_ratings'; + $insertArr = array( + 'extensionkey' => $TYPO3_DB->quoteStr($this->extensionKey,$table), + 'version' => $TYPO3_DB->quoteStr($this->version,$table), + 'rating' => intval($rating), + 'notes' => $TYPO3_DB->quoteStr($notes,$table), + 'username' => $TYPO3_DB->quoteStr($this->username,$table), + 'tstamp' => time() + ); + + $query = $TYPO3_DB->INSERTquery($table,$insertArr); + + return $TYPO3_DB->sql(TYPO3_db,$query) ? True : $query; + } + + + function db_getRatings($extensionkey,$version='',$username=''){ + + global $TYPO3_DB, $TSFE; + $res = $TYPO3_DB->exec_SELECTquery ( + '*', + 'tx_terfe_ratings', + 'extensionkey =' . $TYPO3_DB->fullQuoteStr($extensionkey,'tx_terfe_ratings') + .($version ? ' AND version = ' . $TYPO3_DB->fullQuoteStr($version,'tx_terfe_ratings') : '') + .($username ? ' AND username = ' . $TYPO3_DB->fullQuoteStr($username,'tx_terfe_ratings') : ''), + '', + 'version' + ); + + + if ($res) { + $ratings = array(); + while ($ratingRow = $TYPO3_DB->sql_fetch_assoc($res)) + { + $ratings[]=$ratingRow; + } + + return $ratings; + } + } + + + function db_getAvgRating($extensionkey,$version=''){ + + global $TYPO3_DB, $TSFE; + $res = $TYPO3_DB->exec_SELECTquery ( + 'AVG(rating) as average, COUNT(rating) as num_ratings', + 'tx_terfe_ratings', + 'extensionkey =' . $TYPO3_DB->fullQuoteStr($extensionkey,'tx_terfe_ratings') + .($version ? ' AND version = ' . $TYPO3_DB->fullQuoteStr($version,'tx_terfe_ratings') : '') + ); + + + if ($res) { + $ratings = $TYPO3_DB->sql_fetch_assoc($res); + return $ratings; + } + } + +} + + +?> diff --git a/pi1/locallang.php b/pi1/locallang.php index 36bd59b3..d1e41606 100755 --- a/pi1/locallang.php +++ b/pi1/locallang.php @@ -80,6 +80,7 @@ $LOCAL_LANG = Array ( 'extensioninfo_views_bugs' => 'Bugs', 'extensioninfo_views_feedback' => 'Feedback', 'extensioninfo_views_edit' => 'Edit', + 'extensioninfo_views_rating' => 'Ratings', 'extensioninfo_documentation' => 'Documentation', @@ -187,4 +188,4 @@ $LOCAL_LANG = Array ( 'my' => Array ( ), ); -?> \ No newline at end of file +?> -- GitLab