From 7a1af8656a004d57cfed726b932cba3d6457649b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?=
 <marcosgdf@gmail.com>
Date: Fri, 3 Jan 2014 14:52:31 +0100
Subject: [PATCH] Removed duplicate code

---
 htdocs/opensurvey/adminstuds.php              | 23 ++++++-----------
 .../class/opensurveysondage.class.php         | 25 +++++++++++++++++++
 htdocs/opensurvey/public/studs.php            | 17 ++++---------
 3 files changed, 37 insertions(+), 28 deletions(-)

diff --git a/htdocs/opensurvey/adminstuds.php b/htdocs/opensurvey/adminstuds.php
index 2ab74e99922..0d84caeeb7b 100644
--- a/htdocs/opensurvey/adminstuds.php
+++ b/htdocs/opensurvey/adminstuds.php
@@ -293,21 +293,12 @@ print '<form name="formulaire5" action="#" method="POST">'."\n";
 print_fiche_titre($langs->trans("CommentsOfVoters"),'','');
 
 // Comment list
-$sql = 'SELECT id_comment, usercomment, comment';
-$sql.= ' FROM '.MAIN_DB_PREFIX.'opensurvey_comments';
-$sql.= " WHERE id_sondage='".$db->escape($numsondage)."'";
-$sql.= " ORDER BY id_comment";
-$resql = $db->query($sql);
-$num_rows=$db->num_rows($resql);
-if ($num_rows > 0)
-{
-	$i = 0;
-	while ( $i < $num_rows)
-	{
-		$obj=$db->fetch_object($resql);
-		print '<a href="'.dol_buildpath('/opensurvey/adminstuds.php',1).'?deletecomment='.$obj->id_comment.'&id='.$numsondageadmin.'"> '.img_picto('', 'delete.png').'</a> ';
-		print $obj->usercomment.' : '.dol_nl2br($obj->comment)." <br>";
-		$i++;
+$comments = $object->getComments();
+
+if ($comments) {
+	foreach ($comments as $comment) {
+		print '<a href="'.dol_buildpath('/opensurvey/adminstuds.php',1).'?deletecomment='.$comment->id_comment.'&id='.$numsondageadmin.'"> '.img_picto('', 'delete.png').'</a> ';
+		print $comment->usercomment.': '.dol_nl2br($comment->comment)." <br>";
 	}
 }
 else
@@ -320,7 +311,7 @@ print '<br>';
 // Add comment
 print $langs->trans("AddACommentForPoll") . '<br>';
 print '<textarea name="comment" rows="2" cols="80"></textarea><br>'."\n";
-print $langs->trans("Name") .' : <input type=text name="commentuser"><br>'."\n";
+print $langs->trans("Name") .': <input type="text" name="commentuser" value="'.$user->getFullName($langs).'"><br>'."\n";
 print '<input type="submit" class="button" name="ajoutcomment" value="'.dol_escape_htmltag($langs->trans("AddComment")).'"><br>'."\n";
 if (isset($erreur_commentaire_vide) && $erreur_commentaire_vide=="yes") {
 	print "<font color=#FF0000>" . $langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Name")) . "</font>";
diff --git a/htdocs/opensurvey/class/opensurveysondage.class.php b/htdocs/opensurvey/class/opensurveysondage.class.php
index b0895e466f4..870fd17ce76 100644
--- a/htdocs/opensurvey/class/opensurveysondage.class.php
+++ b/htdocs/opensurvey/class/opensurveysondage.class.php
@@ -517,5 +517,30 @@ class Opensurveysondage extends CommonObject
 		$this->canedit=0;
 	}
 
+	/**
+	 * Returns all comments for the current opensurvey poll
+	 * 
+	 * @return Object[]
+	 */
+	public function getComments() {
+		
+		$sql = 'SELECT id_comment, usercomment, comment';
+		$sql.= ' FROM '.MAIN_DB_PREFIX.'opensurvey_comments';
+		$sql.= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
+		$sql.= " ORDER BY id_comment";
+		$resql = $this->db->query($sql);
+		
+		$num_rows=$this->db->num_rows($resql);
+		
+		$comments = array();
+		
+		if ($num_rows > 0) {
+			while ($obj = $this->db->fetch_object($resql)) {
+				$comments[] = $obj;
+			}
+		}
+		
+		return $comments;
+	}
 }
 ?>
diff --git a/htdocs/opensurvey/public/studs.php b/htdocs/opensurvey/public/studs.php
index 62e78f172ee..fb01e43c8d0 100644
--- a/htdocs/opensurvey/public/studs.php
+++ b/htdocs/opensurvey/public/studs.php
@@ -697,23 +697,16 @@ print '<br>';
 
 
 // Comment list
-$sql = 'SELECT id_comment, usercomment, comment';
-$sql.= ' FROM '.MAIN_DB_PREFIX.'opensurvey_comments';
-$sql.= " WHERE id_sondage='".$db->escape($numsondage)."'";
-$sql.= " ORDER BY id_comment";
-$resql = $db->query($sql);
-$num_rows=$db->num_rows($resql);
-if ($num_rows > 0)
+$comments = $object->getComments();
+
+if ($comments)
 {
-	$i = 0;
 	print "<br><b>" . $langs->trans("CommentsOfVoters") . " :</b><br>\n";
-	while ( $i < $num_rows)
-	{
-		$obj=$db->fetch_object($resql);
+	
+	foreach ($comments as $obj) {
 		print '<div class="comment"><span class="usercomment">';
 		if (in_array($obj->usercomment, $listofvoters)) print '<a href="'.$_SERVER["PHP_SELF"].'?deletecomment='.$obj->id_comment.'&sondage='.$numsondage.'"> '.img_picto('', 'delete.png').'</a> ';
 		print $obj->usercomment.' :</span> <span class="comment">'.dol_nl2br($obj->comment)."</span></div>";
-		$i++;
 	}
 }
 
-- 
GitLab