How to Moderate feedback in PrestaShop

You would often be concerned about online visitors writing feedback on your shopping portal which probably is inappropriate. How about a functionality which allows you to moderate posted feedback?

Following article explains how to achieve this:

This article applies to standard Feedback module which can be installed from:

http://www.prestashop.com/forums/viewthread/20071

How feedback works in PrestaShop?

Each feedback is a text file which is created when a user submits a feedback which includes user details, time and feedback content. The name of each file is unique. The text of all these files is rendered when feedback page is shown.

How reviewing logic works?

Reviewing feedback can be accomplished by suffixing new feedback entries with an identification mark (explained later).  Now, all the files which have this identification signature will not be displayed in the feedback.

What changes are required?

You’ll need to make changes in two places.

1. feedback.class.php – Change method  insertMessage() add a suffix to file name which ensures that it is not reviewed when feedback is being posted by user. I have used “_N” here

function insertMessage(){
 $name=isset($_POST['name']) ? $_POST['name'] : 'Anonymous';
 $product=isset($_POST['product']) ? $_POST['product'] : '';
 $submitDate=date($this->dateFormat);
 $content=isset($_POST['message']) ? $_POST['message'] : '';
 if (trim($name) == '') $name = 'Anonymous';

 if (strlen($content)<5){
 exit();
 }

 $filename=date('YmdHis');
 $isReviewed='_N';
 if (!file_exists($this->messageDir)){
 mkdir($this->messageDir);
 }

 $f=fopen ($this->messageDir.DIRECTORY_SEPARATOR.$filename.$isReviewed.".txt","w+");
 fwrite($f,$name."\n");
 fwrite($f,$product."\n");
 fwrite($f,$submitDate."\n");
 fwrite($f,$content."\n");
 fclose($f);
 }
}

Change displayFeedback method to display only those feedbacks which do not have our suffix “_N”.

function displayFeedback($page=1){
	$list = $this->getMessageList();
	if($_POST['submit'] == 'Leave Feedback'){
		echo "<h3>Thank you, it will take a while for your feedback to appear.</h3>";
	}

	echo "<h2>Feedback Received:</h2>";
	echo "<table class='newsList'>";

	//Get start point and end point
	$startItem = ($page-1)*$this->itemsPerPage;

	if (($startItem + $this->itemsPerPage) > sizeof($list)) $endItem = sizeof($list);
	else $endItem = $startItem + $this->itemsPerPage;

	for ($i=$startItem;$i<$endItem;$i++){
		//foreach ($list as $value) {
		$value = $list[$i];
		$myfilename = $value;
		$data = file($this->messageDir.DIRECTORY_SEPARATOR.$value);
		$name&nbsp; = trim($data[0]);
		$product = trim($data[1]);
		$submitDate = trim($data[2]);
		unset ($data['0']);
		unset ($data['1']);
		unset ($data['2']);
		$content = "";

		foreach ($data as $value) {
			$content .= $value;
		}

		if (strlen(strstr($myfilename,'_N')) === 0){
			echo '<div id="nameid" align="left">posted by <b style="font-size: 12px" align="left">'.$name.'</b> who bought <b style="font-size: 12px" align="left">'.$product.'</b> </div> </p>';
			echo '<div id="feedbackbox"><span style="font-size:14px;">'.nl2br(htmlspecialchars($content)).'</span></div><br/>';
		}
	}

	echo "</table>";
	if (sizeof($list) == 0){
		echo "<center><p>No feedback at the moment, be the first to leave us feedback!</p></center>";
	}

	// Create the pages (pagination)
	if (sizeof($list) > $this->itemsPerPage){
		echo "<div id=\"navigation\">";
		if ($startItem == 0) {
			if ($endItem < sizeof($list)){
				echo "<div id=\"nright\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\" >Next &raquo;</a></div>";
			} else {
				// if nothing to display:
			}
		} else {
			if ($endItem < sizeof($list)){
				echo "<div id=\"nleft\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\" >&laquo; Prev</a></div>";
				echo "<div id=\"nright\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\" >Next &raquo;</a></div>";
			} else {
				echo "<div id=\"nleft\"><a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\" >&laquo; Prev</a></div>";
			}
		}
		echo "<br/></div><br/>";
	}
	$this->displayAddForm();
}

2. Now you’ll need a place in admin section where you can view feedbacks which are pending for review. I have chosen tools tab home area, you can choose yours and use following code:

Open Admin/Tabs/AdminTools.php and change function display to:

public function display(){
	echo '<form id="frmFeedBackReview" method="post" action="">';
	echo '<fieldset><legend><img src="../img/admin/tab-tools.gif" />'.$this->l('Shop Tools').'</legend>';
	echo '<p>'.$this->l('Several tools are available to manage your shop.').'</p>';
	echo '<br />';
	echo '<p>'.$this->l('Please choose a tool by selecting a Tools sub-tab above.').'</p>';
	echo '</fieldset>';
	echo '<fieldset><legend><img src="../ img/admin/tab-tools.gif " />'.$this->l('Review feedback').'</legend>';
	$this->list = array();

	//Do processing for reviews.....
	$selectedFiles = array();

	//To display no review message..
	$isFileForReview = 'False';

	//////////////////////////////////////////////////////////////
	//THIS SECTION IS TO PERFORM ACTIONS WHEN ANY OF THE BUTTONS
	// ARE CLICKED IN REVIEW SECTION
	//////////////////////////////////////////////////////////////
	if ($handle = @opendir(_PS_ROOT_DIR_.'/feedbacktxt')){
		// Read all file from the feedback directory
		while ($file = readdir($handle)){
			if (!is_dir($file)) {
				//Remove .txt from file to get file name without extension
				$rest1 = substr($file, 0, -4);
				//Check if any of radio buttons was checked
				if($_POST[$rest1] == 'on'){
					//Add to list of selected files
					$selectedFiles[count($selectedFiles)] = $rest1;
				}
			}
		}
	}

	//Traverse through all selected files
	foreach($selectedFiles as $selectedFile){
		$thisfile = _PS_ROOT_DIR_.'/feedbacktxt//'.$selectedFile.'.txt';
		//If save clicked
		if($_POST['save'] === 'Approve feedback'){
			//Remove _N which is for marking unreviewed feedback.
			rename($thisfile,substr($thisfile, 0, -6).'.txt');
		}
		//If delete clicked
		if($_POST['delete'] ==='Delete'){
			//Delete file
			unlink($thisfile);
		}
	}

	////////////////////////////////////////////////////////////
	//THIS SECTION IS TO DISPLAY UNREVIEWED COMMENTS
	////////////////////////////////////////////////////////////

	// Open the directory
	if ($handle = @opendir(_PS_ROOT_DIR_.'/feedbacktxt')){
		// Read all file from the actual directory
		while ($file = readdir($handle)){
			if (!is_dir($file)){
				//Add to list
				$this->list[] = $file;
				//Remove extension
				$rest1 = substr($file, 0, -4);
			}
		}
	}
	rsort($this->list);
	$list1 = $this->list;
	$page = 1;
	$startItem = ($page-1)*$this->itemsPerPage;

	if (($startItem + $this->itemsPerPage) > sizeof($list1)) $endItem = sizeof($list1);
	else $endItem = $startItem + $this->itemsPerPage;

	for ($i=$startItem;$i<$endItem;$i++){
		$value = $list1[$i];
		$myfilename = $value;
		$data = file(_PS_ROOT_DIR_.'/feedbacktxt//'.$value);
		$name&nbsp; = trim($data[0]);
		$product = trim($data[1]);

		if($product ===''){
			$product = 'None';
		}

		$submitDate = trim($data[2]);
		unset ($data['0']);
		unset ($data['1']);
		unset ($data['2']);
		$content = "";

		foreach ($data as $value) {
			$content .= $value;
		}

		$rest = substr($myfilename, 0, -4);
		if (strlen(strstr($myfilename,'_N')) > 0){
			//If a file with “_N” found, it is for review, hence display it. The ID of radio button will be filename without extension to identify the button later..
			$isFileForReview = 'True';
			echo '<br /><br /><table width ="100%"><tr><td><div id="nameid" align="left">Feedback posted by : <b style="font-size: 12px" align="left">'.$name.'</b> for product : <b style="font-size: 12px" align="left">'.$product.'</b></td><td><input type="checkbox"font-size: 10pt; font-family: "Courier New"; color: black;">.$rest.'"font-size: 10pt; font-family: "Courier New"; color: black;">.$rest.'" /> </div></td></tr></table> </p>';

			//echo '<input type="checkbox" />';
			echo '<br /><div><span style="font-size:14px;">'.nl2br(htmlspecialchars($content)).'</span></div><br/>';
		}
	}
	//Display message if no feedback is available for review.
	if($isFileForReview === 'False'){
		echo '<br />';
		echo 'No feedback pending for review.';
	}

	echo '<br /> <br /><table width="100%"><tr><td><input type ="submit" class = "button" id ="save" value = "Approve feedback" onclick= "document.frmFeedBackReview.submit();"></td><td><input type ="submit" class = "button" id ="delete" value = "Delete" onclick= "document.frmFeedBackReview.submit();"></td></tr></table>';
	echo '</fieldset>';
	echo '</form>';
}
Rating:
51.0
172 votes
1 2 3 4 5

Leave a Reply