opensurveysondage.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/opensurvey/class/opensurveysondage.class.php
  20. * \ingroup opensurvey
  21. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  22. * Initialy built by build_class_from_table on 2013-03-10 00:32
  23. */
  24. // Put here all includes required by your class file
  25. require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  26. //require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php");
  27. //require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
  28. /**
  29. * Put here description of your class
  30. */
  31. class Opensurveysondage extends CommonObject
  32. {
  33. var $db; //!< To store db handler
  34. var $error; //!< To return error code (or message)
  35. var $errors=array(); //!< To return several error codes (or messages)
  36. var $element='opensurvey_sondage'; //!< Id that identify managed objects
  37. var $table_element='opensurvey_sondage'; //!< Name of table without prefix where object is stored
  38. var $id;
  39. var $id_sondage;
  40. var $commentaires;
  41. var $mail_admin;
  42. var $nom_admin;
  43. /**
  44. * Id of user author of the poll
  45. * @var int
  46. */
  47. public $fk_user_creat;
  48. var $titre;
  49. var $date_fin='';
  50. var $format;
  51. var $mailsonde;
  52. public $sujet;
  53. /**
  54. * Allow comments on this poll
  55. * @var bool
  56. */
  57. public $allow_comments;
  58. /**
  59. * Allow users see others vote
  60. * @var bool
  61. */
  62. public $allow_spy;
  63. /**
  64. * Constructor
  65. *
  66. * @param DoliDb $db Database handler
  67. */
  68. function __construct($db)
  69. {
  70. $this->db = $db;
  71. return 1;
  72. }
  73. /**
  74. * Create object into database
  75. *
  76. * @param User $user User that creates
  77. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  78. * @return int <0 if KO, Id of created object if OK
  79. */
  80. function create($user, $notrigger=0)
  81. {
  82. $error=0;
  83. // Clean parameters
  84. $this->cleanParameters();
  85. // Check parameters
  86. // Put here code to add control on parameters values
  87. // Insert request
  88. $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_sondage(";
  89. $sql.= "id_sondage,";
  90. $sql.= "commentaires,";
  91. $sql.= "fk_user_creat,";
  92. $sql.= "titre,";
  93. $sql.= "date_fin,";
  94. $sql.= "format,";
  95. $sql.= "mailsonde,";
  96. $sql.= "allow_comments,";
  97. $sql.= "allow_spy,";
  98. $sql.= "sujet";
  99. $sql.= ") VALUES (";
  100. $sql.= "'".$this->db->escape($this->id_sondage)."',";
  101. $sql.= " ".(empty($this->commentaires)?'NULL':"'".$this->db->escape($this->commentaires)."'").",";
  102. $sql.= " ".$user->id.",";
  103. $sql.= " '".$this->db->escape($this->titre)."',";
  104. $sql.= " '".$this->db->idate($this->date_fin)."',";
  105. $sql.= " '".$this->db->escape($this->format)."',";
  106. $sql.= " ".$this->db->escape($this->mailsonde).",";
  107. $sql.= " ".$this->db->escape($this->allow_comments).",";
  108. $sql.= " ".$this->db->escape($this->allow_spy).",";
  109. $sql.= " '".$this->db->escape($this->sujet)."'";
  110. $sql.= ")";
  111. $this->db->begin();
  112. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  113. $resql=$this->db->query($sql);
  114. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  115. if (! $error)
  116. {
  117. if (! $notrigger)
  118. {
  119. global $langs, $conf;
  120. //// Call triggers
  121. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  122. $interface=new Interfaces($this->db);
  123. $result=$interface->run_triggers('OPENSURVEY_CREATE',$this,$user,$langs,$conf);
  124. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  125. //// End call triggers
  126. }
  127. }
  128. // Commit or rollback
  129. if ($error)
  130. {
  131. foreach($this->errors as $errmsg)
  132. {
  133. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  134. $this->error.=($this->error?', '.$errmsg:$errmsg);
  135. }
  136. $this->db->rollback();
  137. return -1*$error;
  138. }
  139. else
  140. {
  141. $this->db->commit();
  142. return $this->id;
  143. }
  144. }
  145. /**
  146. * Load object in memory from the database
  147. *
  148. * @param int $id Id object
  149. * @param string $numsurvey Ref of survey (admin or not)
  150. * @return int <0 if KO, >0 if OK
  151. */
  152. function fetch($id,$numsurvey='')
  153. {
  154. $sql = "SELECT";
  155. $sql.= " t.id_sondage,";
  156. $sql.= " t.commentaires as description,";
  157. $sql.= " t.mail_admin,";
  158. $sql.= " t.nom_admin,";
  159. $sql.= " t.fk_user_creat,";
  160. $sql.= " t.titre,";
  161. $sql.= " t.date_fin,";
  162. $sql.= " t.format,";
  163. $sql.= " t.mailsonde,";
  164. $sql.= " t.allow_comments,";
  165. $sql.= " t.allow_spy,";
  166. $sql.= " t.sujet,";
  167. $sql.= " t.tms";
  168. $sql.= " FROM ".MAIN_DB_PREFIX."opensurvey_sondage as t";
  169. $sql.= " WHERE t.id_sondage = '".$this->db->escape($numsurvey)."'";
  170. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  171. $resql=$this->db->query($sql);
  172. if ($resql)
  173. {
  174. if ($this->db->num_rows($resql))
  175. {
  176. $obj = $this->db->fetch_object($resql);
  177. $this->id_sondage = $obj->id_sondage;
  178. //For compatibility
  179. $this->ref = $this->id_sondage;
  180. $this->commentaires = $obj->description; // deprecated
  181. $this->description = $obj->description;
  182. $this->mail_admin = $obj->mail_admin;
  183. $this->nom_admin = $obj->nom_admin;
  184. $this->titre = $obj->titre;
  185. $this->date_fin = $this->db->jdate($obj->date_fin);
  186. $this->format = $obj->format;
  187. $this->mailsonde = $obj->mailsonde;
  188. $this->allow_comments = $obj->allow_comments;
  189. $this->allow_spy = $obj->allow_spy;
  190. $this->sujet = $obj->sujet;
  191. $this->fk_user_creat = $obj->fk_user_creat;
  192. $this->date_m = $this->db->jdate($obj->tls);
  193. $ret=1;
  194. }
  195. else
  196. {
  197. $sondage = ($id ? 'id='.$id : 'sondageid='.$numsurvey);
  198. $this->error='Fetch no poll found for '.$sondage;
  199. dol_syslog($this->error, LOG_ERR);
  200. $ret = 0;
  201. }
  202. $this->db->free($resql);
  203. }
  204. else
  205. {
  206. $this->error="Error ".$this->db->lasterror();
  207. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  208. $ret=-1;
  209. }
  210. return $ret;
  211. }
  212. /**
  213. * Update object into database
  214. *
  215. * @param User $user User that modifies
  216. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  217. * @return int <0 if KO, >0 if OK
  218. */
  219. function update($user=0, $notrigger=0)
  220. {
  221. global $conf, $langs;
  222. $error=0;
  223. // Clean parameters
  224. $this->cleanParameters();
  225. // Check parameters
  226. // Put here code to add a control on parameters values
  227. // Update request
  228. $sql = "UPDATE ".MAIN_DB_PREFIX."opensurvey_sondage SET";
  229. $sql.= " id_sondage=".(isset($this->id_sondage)?"'".$this->db->escape($this->id_sondage)."'":"null").",";
  230. $sql.= " commentaires=".(isset($this->commentaires)?"'".$this->db->escape($this->commentaires)."'":"null").",";
  231. $sql.= " mail_admin=".(isset($this->mail_admin)?"'".$this->db->escape($this->mail_admin)."'":"null").",";
  232. $sql.= " nom_admin=".(isset($this->nom_admin)?"'".$this->db->escape($this->nom_admin)."'":"null").",";
  233. $sql.= " titre=".(isset($this->titre)?"'".$this->db->escape($this->titre)."'":"null").",";
  234. $sql.= " date_fin=".(dol_strlen($this->date_fin)!=0 ? "'".$this->db->idate($this->date_fin)."'" : 'null').",";
  235. $sql.= " format=".(isset($this->format)?"'".$this->db->escape($this->format)."'":"null").",";
  236. $sql.= " mailsonde=".(isset($this->mailsonde)?$this->db->escape($this->mailsonde):"null").",";
  237. $sql.= " allow_comments=".$this->db->escape($this->allow_comments).",";
  238. $sql.= " allow_spy=".$this->db->escape($this->allow_spy);
  239. $sql.= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
  240. $this->db->begin();
  241. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  242. $resql = $this->db->query($sql);
  243. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  244. if (! $error)
  245. {
  246. if (! $notrigger)
  247. {
  248. // Uncomment this and change MYOBJECT to your own tag if you
  249. // want this action calls a trigger.
  250. //// Call triggers
  251. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  252. //$interface=new Interfaces($this->db);
  253. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  254. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  255. //// End call triggers
  256. }
  257. }
  258. // Commit or rollback
  259. if ($error)
  260. {
  261. foreach($this->errors as $errmsg)
  262. {
  263. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  264. $this->error.=($this->error?', '.$errmsg:$errmsg);
  265. }
  266. $this->db->rollback();
  267. return -1*$error;
  268. }
  269. else
  270. {
  271. $this->db->commit();
  272. return 1;
  273. }
  274. }
  275. /**
  276. * Delete object in database
  277. *
  278. * @param User $user User that deletes
  279. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  280. * @param string $numsondage Num sondage admin to delete
  281. * @return int <0 if KO, >0 if OK
  282. */
  283. function delete($user, $notrigger, $numsondage)
  284. {
  285. global $conf, $langs;
  286. $error=0;
  287. $this->db->begin();
  288. if (! $error)
  289. {
  290. if (! $notrigger)
  291. {
  292. //// Call triggers
  293. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  294. $interface=new Interfaces($this->db);
  295. $result=$interface->run_triggers('OPENSURVEY_DELETE',$this,$user,$langs,$conf);
  296. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  297. //// End call triggers
  298. }
  299. }
  300. if (! $error)
  301. {
  302. $sql='DELETE FROM '.MAIN_DB_PREFIX."opensurvey_comments WHERE id_sondage = '".$this->db->escape($numsondage)."'";
  303. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  304. $resql=$this->db->query($sql);
  305. $sql='DELETE FROM '.MAIN_DB_PREFIX."opensurvey_user_studs WHERE id_sondage = '".$this->db->escape($numsondage)."'";
  306. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  307. $resql=$this->db->query($sql);
  308. $sql = "DELETE FROM ".MAIN_DB_PREFIX."opensurvey_sondage";
  309. $sql.= " WHERE id_sondage = '".$this->db->escape($numsondage)."'";
  310. dol_syslog(get_class($this)."::delete sql=".$sql);
  311. $resql = $this->db->query($sql);
  312. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  313. }
  314. // Commit or rollback
  315. if ($error)
  316. {
  317. foreach($this->errors as $errmsg)
  318. {
  319. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  320. $this->error.=($this->error?', '.$errmsg:$errmsg);
  321. }
  322. $this->db->rollback();
  323. return -1*$error;
  324. }
  325. else
  326. {
  327. $this->db->commit();
  328. return 1;
  329. }
  330. }
  331. /**
  332. * Return array of lines
  333. *
  334. * @return array Array of lines
  335. */
  336. function fetch_lines()
  337. {
  338. $ret=array();
  339. $sql = "SELECT id_users, nom, reponses FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
  340. $sql.= " WHERE id_sondage = '".$this->db->escape($this->id_sondage)."'";
  341. $resql=$this->db->query($sql);
  342. if ($resql)
  343. {
  344. $num=$this->db->num_rows($resql);
  345. $i=0;
  346. while ($i < $num)
  347. {
  348. $obj=$this->db->fetch_object($resql);
  349. $tmp=array('id_users'=>$obj->id_users, 'nom'=>$obj->nom, 'reponses'=>$obj->reponses);
  350. $ret[]=$tmp;
  351. $i++;
  352. }
  353. }
  354. else dol_print_error($this->db);
  355. $this->lines=$ret;
  356. return $this->lines;
  357. }
  358. /**
  359. * Initialise object with example values
  360. * Id must be 0 if object instance is a specimen
  361. *
  362. * @return void
  363. */
  364. function initAsSpecimen()
  365. {
  366. $this->id=0;
  367. $this->id_sondage='';
  368. $this->commentaires='';
  369. $this->mail_admin='';
  370. $this->nom_admin='';
  371. $this->titre='';
  372. $this->date_fin='';
  373. $this->format='';
  374. $this->mailsonde='';
  375. }
  376. /**
  377. * Returns all comments for the current opensurvey poll
  378. *
  379. * @return Object[]
  380. */
  381. public function getComments() {
  382. $sql = 'SELECT id_comment, usercomment, comment';
  383. $sql.= ' FROM '.MAIN_DB_PREFIX.'opensurvey_comments';
  384. $sql.= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
  385. $sql.= " ORDER BY id_comment";
  386. $resql = $this->db->query($sql);
  387. $num_rows=$this->db->num_rows($resql);
  388. $comments = array();
  389. if ($num_rows > 0) {
  390. while ($obj = $this->db->fetch_object($resql)) {
  391. $comments[] = $obj;
  392. }
  393. }
  394. return $comments;
  395. }
  396. /**
  397. * Adds a comment to the poll
  398. *
  399. * @param string $comment Comment content
  400. * @param string $comment_user Comment author
  401. * @return boolean False in case of the query fails, true if it was successful
  402. */
  403. public function addComment($comment, $comment_user) {
  404. $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment)";
  405. $sql.= " VALUES ('".$this->db->escape($this->id_sondage)."','".$this->db->escape($comment)."','".$this->db->escape($comment_user)."')";
  406. $resql = $this->db->query($sql);
  407. dol_syslog("sql=".$sql);
  408. if (!$resql) {
  409. return false;
  410. }
  411. return true;
  412. }
  413. /**
  414. * Deletes a comment of the poll
  415. *
  416. * @param int $id_comment Id of the comment
  417. * @return boolean False in case of the query fails, true if it was successful
  418. */
  419. public function deleteComment($id_comment) {
  420. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'opensurvey_comments WHERE id_comment = '.$id_comment.' AND id_sondage = '.$this->id_sondage;
  421. $resql = $this->db->query($sql);
  422. if (!$resql) {
  423. return false;
  424. }
  425. return true;
  426. }
  427. /**
  428. * Cleans all the class variables before doing an update or an insert
  429. *
  430. * @return void
  431. */
  432. private function cleanParameters() {
  433. $this->id_sondage = trim($this->id_sondage);
  434. $this->commentaires = trim($this->commentaires);
  435. $this->mail_admin = trim($this->mail_admin);
  436. $this->nom_admin = trim($this->nom_admin);
  437. $this->titre = trim($this->titre);
  438. $this->format = trim($this->format);
  439. $this->mailsonde = ($this->mailsonde ? 1 : 0);
  440. $this->allow_comments = ($this->allow_comments ? 1 : 0);
  441. $this->allow_spy = ($this->allow_spy ? 1 : 0);
  442. $this->sujet = trim($this->sujet);
  443. }
  444. }