opensurveysondage.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. <?php
  2. /* Copyright (C) 2013-2014 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  4. * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/opensurvey/class/opensurveysondage.class.php
  21. * \ingroup opensurvey
  22. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  23. * Initialy built by build_class_from_table on 2013-03-10 00:32
  24. */
  25. // Put here all includes required by your class file
  26. require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
  27. //require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
  28. //require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
  29. /**
  30. * Put here description of your class
  31. */
  32. class Opensurveysondage extends CommonObject
  33. {
  34. /**
  35. * @var string ID to identify managed object
  36. */
  37. public $element = 'opensurvey_sondage';
  38. /**
  39. * @var string Name of table without prefix where object is stored
  40. */
  41. public $table_element = 'opensurvey_sondage';
  42. /**
  43. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  44. */
  45. public $picto = 'poll';
  46. /**
  47. * @var string ID survey
  48. */
  49. public $id_sondage;
  50. /**
  51. * @var string description
  52. */
  53. public $description;
  54. /**
  55. * @var string email admin
  56. */
  57. public $mail_admin;
  58. /**
  59. * @var string admin name
  60. */
  61. public $nom_admin;
  62. /**
  63. * Id of user author of the poll
  64. * @var int
  65. */
  66. public $fk_user_creat;
  67. /**
  68. * @var string Title
  69. */
  70. public $title;
  71. public $date_fin = '';
  72. public $date_m;
  73. /**
  74. * @var int status
  75. */
  76. public $status = 1;
  77. /**
  78. * @var string format of survey
  79. */
  80. public $format;
  81. /**
  82. * @var int mailsonde
  83. */
  84. public $mailsonde;
  85. /**
  86. * @var string subject
  87. */
  88. public $sujet;
  89. /**
  90. * @var int Allow comments on this poll
  91. */
  92. public $allow_comments;
  93. /**
  94. * @var int Allow users see others vote
  95. */
  96. public $allow_spy;
  97. /**
  98. * Draft status (not used)
  99. */
  100. const STATUS_DRAFT = 0;
  101. /**
  102. * Validated/Opened status
  103. */
  104. const STATUS_VALIDATED = 1;
  105. /**
  106. * Closed
  107. */
  108. const STATUS_CLOSED = 2;
  109. /**
  110. * Constructor
  111. *
  112. * @param DoliDb $db Database handler
  113. */
  114. public function __construct($db)
  115. {
  116. $this->db = $db;
  117. }
  118. /**
  119. * Create object into database
  120. *
  121. * @param User $user User that creates
  122. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  123. * @return int <0 if KO, Id of created object if OK
  124. */
  125. public function create(User $user, $notrigger = 0)
  126. {
  127. global $conf;
  128. $error = 0;
  129. // Clean parameters
  130. $this->cleanParameters();
  131. // Check parameters
  132. if (!$this->date_fin > 0) {
  133. $this->error = 'BadValueForEndDate';
  134. dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
  135. return -1;
  136. }
  137. // Insert request
  138. $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_sondage(";
  139. $sql .= "id_sondage,";
  140. $sql .= "commentaires,";
  141. $sql .= "fk_user_creat,";
  142. $sql .= "titre,";
  143. $sql .= "date_fin,";
  144. $sql .= "status,";
  145. $sql .= "format,";
  146. $sql .= "mailsonde,";
  147. $sql .= "allow_comments,";
  148. $sql .= "allow_spy,";
  149. $sql .= "sujet,";
  150. $sql .= "entity";
  151. $sql .= ") VALUES (";
  152. $sql .= "'".$this->db->escape($this->id_sondage)."',";
  153. $sql .= " ".(empty($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").",";
  154. $sql .= " ".(int) $user->id.",";
  155. $sql .= " '".$this->db->escape($this->title)."',";
  156. $sql .= " '".$this->db->idate($this->date_fin)."',";
  157. $sql .= " ".(int) $this->status.",";
  158. $sql .= " '".$this->db->escape($this->format)."',";
  159. $sql .= " ".((int) $this->mailsonde).",";
  160. $sql .= " ".((int) $this->allow_comments).",";
  161. $sql .= " ".((int) $this->allow_spy).",";
  162. $sql .= " '".$this->db->escape($this->sujet)."',";
  163. $sql .= " ".((int) $conf->entity);
  164. $sql .= ")";
  165. $this->db->begin();
  166. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  167. $resql = $this->db->query($sql);
  168. if (!$resql) {
  169. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  170. }
  171. if (!$error && !$notrigger) {
  172. global $langs, $conf;
  173. // Call trigger
  174. $result = $this->call_trigger('OPENSURVEY_CREATE', $user);
  175. if ($result < 0) {
  176. $error++;
  177. }
  178. // End call triggers
  179. }
  180. // Commit or rollback
  181. if ($error) {
  182. foreach ($this->errors as $errmsg) {
  183. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  184. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  185. }
  186. $this->db->rollback();
  187. return -1 * $error;
  188. } else {
  189. $this->db->commit();
  190. return $this->id;
  191. }
  192. }
  193. /**
  194. * Load object in memory from the database
  195. *
  196. * @param int $id Id object
  197. * @param string $numsurvey Ref of survey (admin or not)
  198. * @return int <0 if KO, >0 if OK
  199. */
  200. public function fetch($id, $numsurvey = '')
  201. {
  202. $sql = "SELECT";
  203. $sql .= " t.id_sondage,";
  204. $sql .= " t.titre as title,";
  205. $sql .= " t.commentaires as description,";
  206. $sql .= " t.mail_admin,";
  207. $sql .= " t.nom_admin,";
  208. $sql .= " t.fk_user_creat,";
  209. $sql .= " t.date_fin,";
  210. $sql .= " t.status,";
  211. $sql .= " t.format,";
  212. $sql .= " t.mailsonde,";
  213. $sql .= " t.allow_comments,";
  214. $sql .= " t.allow_spy,";
  215. $sql .= " t.sujet,";
  216. $sql .= " t.tms";
  217. $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_sondage as t";
  218. $sql .= " WHERE t.id_sondage = '".$this->db->escape($id ? $id : $numsurvey)."'";
  219. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  220. $resql = $this->db->query($sql);
  221. if ($resql) {
  222. if ($this->db->num_rows($resql)) {
  223. $obj = $this->db->fetch_object($resql);
  224. $this->id_sondage = $obj->id_sondage;
  225. $this->ref = $this->id_sondage; //For compatibility
  226. $this->description = $obj->description;
  227. $this->mail_admin = $obj->mail_admin;
  228. $this->nom_admin = $obj->nom_admin;
  229. $this->title = $obj->title;
  230. $this->date_fin = $this->db->jdate($obj->date_fin);
  231. $this->status = $obj->status;
  232. $this->format = $obj->format;
  233. $this->mailsonde = $obj->mailsonde;
  234. $this->allow_comments = $obj->allow_comments;
  235. $this->allow_spy = $obj->allow_spy;
  236. $this->sujet = $obj->sujet;
  237. $this->fk_user_creat = $obj->fk_user_creat;
  238. $this->date_m = $this->db->jdate(!empty($obj->tls) ? $obj->tls : "");
  239. $ret = 1;
  240. } else {
  241. $sondage = ($id ? 'id='.$id : 'sondageid='.$numsurvey);
  242. $this->error = 'Fetch no poll found for '.$sondage;
  243. dol_syslog($this->error, LOG_ERR);
  244. $ret = 0;
  245. }
  246. $this->db->free($resql);
  247. } else {
  248. $this->error = "Error ".$this->db->lasterror();
  249. $ret = -1;
  250. }
  251. return $ret;
  252. }
  253. /**
  254. * Update object into database
  255. *
  256. * @param User $user User that modifies
  257. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  258. * @return int <0 if KO, >0 if OK
  259. */
  260. public function update(User $user, $notrigger = 0)
  261. {
  262. global $conf, $langs;
  263. $error = 0;
  264. // Clean parameters
  265. $this->cleanParameters();
  266. // Check parameters
  267. // Put here code to add a control on parameters values
  268. // Update request
  269. $sql = "UPDATE ".MAIN_DB_PREFIX."opensurvey_sondage SET";
  270. $sql .= " id_sondage=".(isset($this->id_sondage) ? "'".$this->db->escape($this->id_sondage)."'" : "null").",";
  271. $sql .= " commentaires=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
  272. $sql .= " mail_admin=".(isset($this->mail_admin) ? "'".$this->db->escape($this->mail_admin)."'" : "null").",";
  273. $sql .= " nom_admin=".(isset($this->nom_admin) ? "'".$this->db->escape($this->nom_admin)."'" : "null").",";
  274. $sql .= " titre=".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "null").",";
  275. $sql .= " date_fin=".(dol_strlen($this->date_fin) != 0 ? "'".$this->db->idate($this->date_fin)."'" : 'null').",";
  276. $sql .= " status=".(isset($this->status) ? "'".$this->db->escape($this->status)."'" : "null").",";
  277. $sql .= " format=".(isset($this->format) ? "'".$this->db->escape($this->format)."'" : "null").",";
  278. $sql .= " mailsonde=".(isset($this->mailsonde) ? ((int) $this->mailsonde) : "null").",";
  279. $sql .= " allow_comments=".((int) $this->allow_comments).",";
  280. $sql .= " allow_spy=".((int) $this->allow_spy);
  281. $sql .= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
  282. $this->db->begin();
  283. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  284. $resql = $this->db->query($sql);
  285. if (!$resql) {
  286. $error++;
  287. $this->errors[] = "Error ".$this->db->lasterror();
  288. }
  289. if (!$error && !$notrigger) {
  290. // Call trigger
  291. $result = $this->call_trigger('OPENSURVEY_MODIFY', $user);
  292. if ($result < 0) {
  293. $error++;
  294. }
  295. // End call triggers
  296. }
  297. // Commit or rollback
  298. if ($error) {
  299. foreach ($this->errors as $errmsg) {
  300. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  301. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  302. }
  303. $this->db->rollback();
  304. return -1 * $error;
  305. } else {
  306. $this->db->commit();
  307. return 1;
  308. }
  309. }
  310. /**
  311. * Delete object in database
  312. *
  313. * @param User $user User that deletes
  314. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  315. * @param string $numsondage Num sondage admin to delete
  316. * @return int <0 if KO, >0 if OK
  317. */
  318. public function delete(User $user, $notrigger = 0, $numsondage = '')
  319. {
  320. global $conf, $langs;
  321. $error = 0;
  322. if (empty($numsondage)) {
  323. $numsondage = $this->id_sondage;
  324. }
  325. $this->db->begin();
  326. if (!$error && !$notrigger) {
  327. // Call trigger
  328. $result = $this->call_trigger('OPENSURVEY_DELETE', $user);
  329. if ($result < 0) {
  330. $error++;
  331. }
  332. // End call triggers
  333. }
  334. if (!$error) {
  335. $sql = 'DELETE FROM '.MAIN_DB_PREFIX."opensurvey_comments WHERE id_sondage = '".$this->db->escape($numsondage)."'";
  336. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  337. $resql = $this->db->query($sql);
  338. $sql = 'DELETE FROM '.MAIN_DB_PREFIX."opensurvey_user_studs WHERE id_sondage = '".$this->db->escape($numsondage)."'";
  339. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  340. $resql = $this->db->query($sql);
  341. $sql = "DELETE FROM ".MAIN_DB_PREFIX."opensurvey_sondage";
  342. $sql .= " WHERE id_sondage = '".$this->db->escape($numsondage)."'";
  343. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  344. $resql = $this->db->query($sql);
  345. if (!$resql) {
  346. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  347. }
  348. }
  349. // Commit or rollback
  350. if ($error) {
  351. foreach ($this->errors as $errmsg) {
  352. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  353. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  354. }
  355. $this->db->rollback();
  356. return -1 * $error;
  357. } else {
  358. $this->db->commit();
  359. return 1;
  360. }
  361. }
  362. /**
  363. * Return a link to the object card (with optionaly the picto)
  364. *
  365. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  366. * @param int $notooltip 1=Disable tooltip
  367. * @param string $morecss Add more css on link
  368. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  369. * @return string String with URL
  370. */
  371. public function getNomUrl($withpicto = 0, $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  372. {
  373. global $db, $conf, $langs;
  374. global $dolibarr_main_authentication, $dolibarr_main_demo;
  375. global $menumanager;
  376. if (!empty($conf->dol_no_mouse_hover)) {
  377. $notooltip = 1; // Force disable tooltips
  378. }
  379. $result = '';
  380. $label = img_picto('', $this->picto).' <u>'.$langs->trans("ShowSurvey").'</u>';
  381. $label .= '<br>';
  382. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
  383. $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
  384. $url = DOL_URL_ROOT.'/opensurvey/card.php?id='.$this->id;
  385. // Add param to save lastsearch_values or not
  386. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  387. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  388. $add_save_lastsearch_values = 1;
  389. }
  390. if ($add_save_lastsearch_values) {
  391. $url .= '&save_lastsearch_values=1';
  392. }
  393. $linkclose = '';
  394. if (empty($notooltip)) {
  395. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  396. $label = $langs->trans("ShowMyObject");
  397. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  398. }
  399. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  400. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  401. } else {
  402. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  403. }
  404. $linkstart = '<a href="'.$url.'"';
  405. $linkstart .= $linkclose.'>';
  406. $linkend = '</a>';
  407. $result .= $linkstart;
  408. if ($withpicto) {
  409. $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  410. }
  411. if ($withpicto != 2) {
  412. $result .= $this->ref;
  413. }
  414. $result .= $linkend;
  415. return $result;
  416. }
  417. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  418. /**
  419. * Return array of lines
  420. *
  421. * @return int <0 if KO, >0 if OK
  422. */
  423. public function fetch_lines()
  424. {
  425. // phpcs:enable
  426. $this->lines = array();
  427. $sql = "SELECT id_users, nom as name, reponses";
  428. $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
  429. $sql .= " WHERE id_sondage = '".$this->db->escape($this->id_sondage)."'";
  430. $resql = $this->db->query($sql);
  431. if ($resql) {
  432. $num = $this->db->num_rows($resql);
  433. $i = 0;
  434. while ($i < $num) {
  435. $obj = $this->db->fetch_object($resql);
  436. $tmp = array('id_users'=>$obj->id_users, 'nom'=>$obj->name, 'reponses'=>$obj->reponses);
  437. $this->lines[] = $tmp;
  438. $i++;
  439. }
  440. } else {
  441. dol_print_error($this->db);
  442. }
  443. return count($this->lines);
  444. }
  445. /**
  446. * Initialise object with example values
  447. * Id must be 0 if object instance is a specimen
  448. *
  449. * @return void
  450. */
  451. public function initAsSpecimen()
  452. {
  453. $this->id = 0;
  454. $this->id_sondage = 'a12d5g';
  455. $this->description = 'Description of the specimen survey';
  456. $this->mail_admin = 'email@email.com';
  457. $this->nom_admin = 'surveyadmin';
  458. $this->title = 'This is a specimen survey';
  459. $this->date_fin = dol_now() + 3600 * 24 * 10;
  460. $this->status = 1;
  461. $this->format = 'classic';
  462. $this->mailsonde = 0;
  463. }
  464. /**
  465. * Returns all comments for the current opensurvey poll
  466. *
  467. * @return Object[]
  468. */
  469. public function getComments()
  470. {
  471. $comments = array();
  472. $sql = 'SELECT id_comment, usercomment, comment';
  473. $sql .= ' FROM '.MAIN_DB_PREFIX.'opensurvey_comments';
  474. $sql .= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
  475. $sql .= " ORDER BY id_comment";
  476. $resql = $this->db->query($sql);
  477. if ($resql) {
  478. $num_rows = $this->db->num_rows($resql);
  479. if ($num_rows > 0) {
  480. while ($obj = $this->db->fetch_object($resql)) {
  481. $comments[] = $obj;
  482. }
  483. }
  484. }
  485. return $comments;
  486. }
  487. /**
  488. * Adds a comment to the poll
  489. *
  490. * @param string $comment Comment content
  491. * @param string $comment_user Comment author
  492. * @param string $user_ip Comment author IP
  493. * @return boolean False in case of the query fails, true if it was successful
  494. */
  495. public function addComment($comment, $comment_user, $user_ip = '')
  496. {
  497. $now = dol_now();
  498. $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment, date_creation, ip)";
  499. $sql .= " VALUES ('".$this->db->escape($this->id_sondage)."','".$this->db->escape($comment)."','".$this->db->escape($comment_user)."','".$this->db->idate($now)."'".($user_ip ? ",'".$this->db->escape($user_ip)."'" : '').")";
  500. $resql = $this->db->query($sql);
  501. if (!$resql) {
  502. return false;
  503. }
  504. return true;
  505. }
  506. /**
  507. * Deletes a comment of the poll
  508. *
  509. * @param int $id_comment Id of the comment
  510. * @return boolean False in case of the query fails, true if it was successful
  511. */
  512. public function deleteComment($id_comment)
  513. {
  514. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'opensurvey_comments WHERE id_comment = '.((int) $id_comment).' AND id_sondage = "'.$this->db->escape($this->id_sondage).'"';
  515. $resql = $this->db->query($sql);
  516. if (!$resql) {
  517. return false;
  518. }
  519. return true;
  520. }
  521. /**
  522. * Cleans all the class variables before doing an update or an insert
  523. *
  524. * @return void
  525. */
  526. private function cleanParameters()
  527. {
  528. $this->id_sondage = trim($this->id_sondage);
  529. $this->description = trim($this->description);
  530. $this->mail_admin = trim($this->mail_admin);
  531. $this->nom_admin = trim($this->nom_admin);
  532. $this->title = trim($this->title);
  533. $this->status = (int) $this->status;
  534. $this->format = trim($this->format);
  535. $this->mailsonde = ($this->mailsonde ? 1 : 0);
  536. $this->allow_comments = ($this->allow_comments ? 1 : 0);
  537. $this->allow_spy = ($this->allow_spy ? 1 : 0);
  538. $this->sujet = trim($this->sujet);
  539. }
  540. /**
  541. * Return status label of Order
  542. *
  543. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  544. * @return string Libelle
  545. */
  546. public function getLibStatut($mode)
  547. {
  548. return $this->LibStatut($this->status, $mode);
  549. }
  550. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  551. /**
  552. * Return label of status
  553. *
  554. * @param int $status Id statut
  555. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  556. * @return string Label of status
  557. */
  558. public function LibStatut($status, $mode)
  559. {
  560. // phpcs:enable
  561. global $langs, $conf;
  562. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  563. global $langs;
  564. //$langs->load("mymodule");
  565. $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  566. $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Opened');
  567. $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
  568. $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  569. $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Opened');
  570. $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
  571. }
  572. $statusType = 'status'.$status;
  573. if ($status == self::STATUS_VALIDATED) {
  574. if (0) {
  575. $statusType = 'status1';
  576. } else {
  577. $statusType = 'status4';
  578. }
  579. }
  580. if ($status == self::STATUS_CLOSED) {
  581. $statusType = 'status6';
  582. }
  583. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  584. }
  585. /**
  586. * Return number of votes done for this survey.
  587. *
  588. * @return int Number of votes
  589. */
  590. public function countVotes()
  591. {
  592. $result = 0;
  593. $sql = " SELECT COUNT(id_users) as nb FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
  594. $sql .= " WHERE id_sondage = '".$this->db->escape($this->ref)."'";
  595. $resql = $this->db->query($sql);
  596. if ($resql) {
  597. $obj = $this->db->fetch_object($resql);
  598. if ($obj) {
  599. $result = $obj->nb;
  600. }
  601. } else {
  602. $this->error = $this->db->lasterror();
  603. $this->errors[] = $this->error;
  604. }
  605. return $result;
  606. }
  607. }