Commit d89a3958 authored by disappearer's avatar disappearer

Adding initial rating stuff.


git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/terfe/trunk@2447 735d13b6-9817-0410-8766-e36946ffe9aa
parent 8078354c
(add new changes on top of this file)
07-01-02 Michael Scharkow <michael@underused.org>
* Initial upload of rating framework (in p1)
06-01-02 Michael Scharkow <michael@underused.org>
* Initial upload of /pi3 (Reviewing framework)
......
......@@ -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)
);
......@@ -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
?>
<?php
class tx_terfe_ratings{
var $minRating = 1;
var $maxRating = 5;
var $ratingItems = array (1 => '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 = '<em>Average rating:</em> ';
// $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 .= '<h2>Ratings for this extension version</h2><table><thead><tr><th>User</th><th>Rating</th><th>Notes</th></tr></thead>';
foreach ($ratings as $rating){
$output .= '<tr><td>'.$rating['username'].'</td><td>'.$this->num_to_star($rating['rating']).'</td><td>'.htmlspecialchars($rating['notes']).
'</td></tr>';
}
$output .= '</table>';
}
return $output;
}
function render_rating($avg, $num){
$output = '<span class="t3rating_summary">';
$output .= $avg .'('.$num.' ratings)';
$output .= '</span>';
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 = '<form action="'.t3lib_div::linkThisScript().'" method="POST" class="rating">';
// $output .= '<fieldset style="float:left;">';
$outrows = array();
for ($i=$this->minRating;$i <= $this->maxRating; $i++){
$outrows[] = '<label for="rating'.$i.'"><input type="radio" name="rating" id="rating'.$i.'" value="'.$i.'"/>'
.$i.' - '.$this->ratingItems[$i].' </label><br/>';
}
$output .= implode('',array_reverse($outrows));
// $output .= '</fieldset>';
$output .= '<label for="notes">Add your review notes</label><br/><textarea name="notes" id="notes" cols="50" rows="5"></textarea>';
$output .= '<input type="hidden" name="no_cache" value="1"/>';
$output .= '<input type="submit" name="submit" value="Submit rating"/></form>';
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 = '<img src="fileadmin/stars/' . $roundedvalue. '.png"'.
'alt="'.$roundedvalue.' stars out of 5" '.
'title="'.$roundedvalue.' stars out of 5"/>';
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;
}
}
}
?>
......@@ -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
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment