From 8727daeb7737dc8502d4e6573eacab17d42d1b16 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 16:38:02 +0100
Subject: [PATCH] Unified comment addition in OpenSurveySondage::addComment and
 removed duplicate code

---
 htdocs/opensurvey/adminstuds.php              | 26 +++++++------------
 .../class/opensurveysondage.class.php         | 21 +++++++++++++++
 htdocs/opensurvey/public/studs.php            | 15 ++---------
 3 files changed, 32 insertions(+), 30 deletions(-)

diff --git a/htdocs/opensurvey/adminstuds.php b/htdocs/opensurvey/adminstuds.php
index 0d84caeeb7b..2700ea5c518 100644
--- a/htdocs/opensurvey/adminstuds.php
+++ b/htdocs/opensurvey/adminstuds.php
@@ -44,6 +44,13 @@ if (GETPOST('id')) {
 
 $object=new Opensurveysondage($db);
 
+$result=$object->fetch(0, $numsondageadmin);
+if ($result <= 0)
+{
+	dol_print_error($db,$object->error);
+	exit;
+}
+
 $expiredate=dol_mktime(0, 0, 0, GETPOST('expiremonth'), GETPOST('expireday'), GETPOST('expireyear'));
 
 
@@ -74,12 +81,6 @@ if ($action == 'update')
 		$action = 'edit';
 	}
 
-	if (! $error)
-	{
-		$res=$object->fetch(0,$numsondageadmin);
-		if ($res < 0) dol_print_error($db,$object->error);
-	}
-
 	if (! $error)
 	{
 		$object->titre = GETPOST('nouveautitre');
@@ -120,10 +121,8 @@ if (GETPOST('ajoutcomment'))
 		$comment = GETPOST("comment");
 		$comment_user = GETPOST('commentuser');
 
-		$sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment)";
-		$sql.= " VALUES ('".$db->escape($object->id_sondage)."','".$db->escape($comment)."','".$db->escape($comment_user)."')";
-		$resql = $db->query($sql);
-		dol_syslog("sql=".$sql);
+		$resql = $object->addComment($comment, $comment_user);
+		
 		if (! $resql)
 		{
 			$err |= COMMENT_INSERT_FAILED;
@@ -146,13 +145,6 @@ if ($idcomment)
 
 $form=new Form($db);
 
-$result=$object->fetch(0, $numsondageadmin);
-if ($result <= 0)
-{
-	dol_print_error($db,$object->error);
-	exit;
-}
-
 $arrayofjs=array();
 $arrayofcss=array('/opensurvey/css/style.css');
 llxHeader('',$object->titre, 0, 0, 0, 0, $arrayofjs, $arrayofcss);
diff --git a/htdocs/opensurvey/class/opensurveysondage.class.php b/htdocs/opensurvey/class/opensurveysondage.class.php
index 870fd17ce76..6db64139971 100644
--- a/htdocs/opensurvey/class/opensurveysondage.class.php
+++ b/htdocs/opensurvey/class/opensurveysondage.class.php
@@ -542,5 +542,26 @@ class Opensurveysondage extends CommonObject
 		
 		return $comments;
 	}
+	
+	/**
+	 * Adds a comment to the poll
+	 * 
+	 * @param string $comment Comment content
+	 * @param string $comment_user Comment author
+	 * @return boolean False in case of the query fails, true if it was successful
+	 */
+	public function addComment($comment, $comment_user) {
+		
+		$sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment)";
+		$sql.= " VALUES ('".$this->db->escape($this->id_sondage)."','".$this->db->escape($comment)."','".$this->db->escape($comment_user)."')";
+		$resql = $this->db->query($sql);
+		dol_syslog("sql=".$sql);
+		
+		if (!$resql) {
+			return false;
+		}
+		
+		return true;
+	}
 }
 ?>
diff --git a/htdocs/opensurvey/public/studs.php b/htdocs/opensurvey/public/studs.php
index e2241482ac4..2c84dead099 100644
--- a/htdocs/opensurvey/public/studs.php
+++ b/htdocs/opensurvey/public/studs.php
@@ -84,10 +84,8 @@ if (GETPOST('ajoutcomment'))
 		$comment = GETPOST("comment");
 		$comment_user = GETPOST('commentuser');
 
-		$sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment)";
-		$sql.= " VALUES ('".$db->escape($numsondage)."','".$db->escape($comment)."','".$db->escape($comment_user)."')";
-		$resql = $db->query($sql);
-		dol_syslog("sql=".$sql);
+		$resql = $object->addComment($comment, $comment_user);
+		
 		if (! $resql) dol_print_error($db);
 	}
 }
@@ -227,15 +225,6 @@ if ($idcomment)
  */
 
 $form=new Form($db);
-$object=new OpenSurveySondage($db);
-
-$result=$object->fetch(0,$numsondage);
-if ($result <= 0)
-{
-	print $langs->trans("ErrorRecordNotFound");
-	llxFooterSurvey();
-	exit;
-}
 
 $arrayofjs=array();
 $arrayofcss=array('/opensurvey/css/style.css');
-- 
GitLab