dolibarr  17.0.4
opensurveysondage.class.php
Go to the documentation of this file.
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 
27 // Put here all includes required by your class file
28 require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
29 //require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
30 //require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
31 
32 
37 {
41  public $element = 'opensurvey_sondage';
42 
46  public $table_element = 'opensurvey_sondage';
47 
51  public $picto = 'poll';
52 
56  public $id_sondage;
57 
61  public $description;
62 
66  public $mail_admin;
67 
71  public $nom_admin;
72 
77  public $fk_user_creat;
78 
82  public $title;
83 
84  public $date_fin = '';
85 
86  public $date_m;
87 
91  public $status = 1;
92 
96  public $format;
97 
101  public $mailsonde;
102 
106  public $sujet;
107 
111  public $allow_comments;
112 
116  public $allow_spy;
117 
118 
122  const STATUS_DRAFT = 0;
126  const STATUS_VALIDATED = 1;
130  const STATUS_CLOSED = 2;
131 
132 
138  public function __construct($db)
139  {
140  $this->db = $db;
141  }
142 
143 
151  public function create(User $user, $notrigger = 0)
152  {
153  global $conf;
154 
155  $error = 0;
156 
157  // Clean parameters
158  $this->cleanParameters();
159 
160  // Check parameters
161  if (!$this->date_fin > 0) {
162  $this->error = 'BadValueForEndDate';
163  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
164  return -1;
165  }
166 
167  // Insert request
168  $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_sondage(";
169  $sql .= "id_sondage,";
170  $sql .= "commentaires,";
171  $sql .= "fk_user_creat,";
172  $sql .= "titre,";
173  $sql .= "date_fin,";
174  $sql .= "status,";
175  $sql .= "format,";
176  $sql .= "mailsonde,";
177  $sql .= "allow_comments,";
178  $sql .= "allow_spy,";
179  $sql .= "sujet,";
180  $sql .= "entity";
181  $sql .= ") VALUES (";
182  $sql .= "'".$this->db->escape($this->id_sondage)."',";
183  $sql .= " ".(empty($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").",";
184  $sql .= " ".(int) $user->id.",";
185  $sql .= " '".$this->db->escape($this->title)."',";
186  $sql .= " '".$this->db->idate($this->date_fin)."',";
187  $sql .= " ".(int) $this->status.",";
188  $sql .= " '".$this->db->escape($this->format)."',";
189  $sql .= " ".((int) $this->mailsonde).",";
190  $sql .= " ".((int) $this->allow_comments).",";
191  $sql .= " ".((int) $this->allow_spy).",";
192  $sql .= " '".$this->db->escape($this->sujet)."',";
193  $sql .= " ".((int) $conf->entity);
194  $sql .= ")";
195 
196  $this->db->begin();
197 
198  dol_syslog(get_class($this)."::create", LOG_DEBUG);
199  $resql = $this->db->query($sql);
200  if (!$resql) {
201  $error++; $this->errors[] = "Error ".$this->db->lasterror();
202  }
203 
204  if (!$error && !$notrigger) {
205  global $langs, $conf;
206 
207  // Call trigger
208  $result = $this->call_trigger('OPENSURVEY_CREATE', $user);
209  if ($result < 0) {
210  $error++;
211  }
212  // End call triggers
213  }
214 
215  // Commit or rollback
216  if ($error) {
217  foreach ($this->errors as $errmsg) {
218  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
219  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
220  }
221  $this->db->rollback();
222  return -1 * $error;
223  } else {
224  $this->db->commit();
225  return $this->id;
226  }
227  }
228 
229 
237  public function fetch($id, $numsurvey = '')
238  {
239  $sql = "SELECT";
240  $sql .= " t.id_sondage,";
241  $sql .= " t.titre as title,";
242  $sql .= " t.commentaires as description,";
243  $sql .= " t.mail_admin,";
244  $sql .= " t.nom_admin,";
245  $sql .= " t.fk_user_creat,";
246  $sql .= " t.date_fin,";
247  $sql .= " t.status,";
248  $sql .= " t.format,";
249  $sql .= " t.mailsonde,";
250  $sql .= " t.allow_comments,";
251  $sql .= " t.allow_spy,";
252  $sql .= " t.sujet,";
253  $sql .= " t.tms";
254  $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_sondage as t";
255  $sql .= " WHERE t.id_sondage = '".$this->db->escape($id ? $id : $numsurvey)."'";
256 
257  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
258  $resql = $this->db->query($sql);
259  if ($resql) {
260  if ($this->db->num_rows($resql)) {
261  $obj = $this->db->fetch_object($resql);
262 
263  $this->id_sondage = $obj->id_sondage;
264  $this->ref = $this->id_sondage; //For compatibility
265 
266  $this->description = $obj->description;
267  $this->mail_admin = $obj->mail_admin;
268  $this->nom_admin = $obj->nom_admin;
269  $this->title = $obj->title;
270  $this->date_fin = $this->db->jdate($obj->date_fin);
271  $this->status = $obj->status;
272  $this->format = $obj->format;
273  $this->mailsonde = $obj->mailsonde;
274  $this->allow_comments = $obj->allow_comments;
275  $this->allow_spy = $obj->allow_spy;
276  $this->sujet = $obj->sujet;
277  $this->fk_user_creat = $obj->fk_user_creat;
278 
279  $this->date_m = $this->db->jdate(!empty($obj->tls) ? $obj->tls : "");
280  $ret = 1;
281  } else {
282  $sondage = ($id ? 'id='.$id : 'sondageid='.$numsurvey);
283  $this->error = 'Fetch no poll found for '.$sondage;
284  dol_syslog($this->error, LOG_ERR);
285  $ret = 0;
286  }
287 
288  $this->db->free($resql);
289  } else {
290  $this->error = "Error ".$this->db->lasterror();
291  $ret = -1;
292  }
293 
294  return $ret;
295  }
296 
297 
305  public function update(User $user, $notrigger = 0)
306  {
307  global $conf, $langs;
308  $error = 0;
309 
310  // Clean parameters
311  $this->cleanParameters();
312 
313  // Check parameters
314  // Put here code to add a control on parameters values
315 
316  // Update request
317  $sql = "UPDATE ".MAIN_DB_PREFIX."opensurvey_sondage SET";
318  $sql .= " id_sondage=".(isset($this->id_sondage) ? "'".$this->db->escape($this->id_sondage)."'" : "null").",";
319  $sql .= " commentaires=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
320  $sql .= " mail_admin=".(isset($this->mail_admin) ? "'".$this->db->escape($this->mail_admin)."'" : "null").",";
321  $sql .= " nom_admin=".(isset($this->nom_admin) ? "'".$this->db->escape($this->nom_admin)."'" : "null").",";
322  $sql .= " titre=".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "null").",";
323  $sql .= " date_fin=".(dol_strlen($this->date_fin) != 0 ? "'".$this->db->idate($this->date_fin)."'" : 'null').",";
324  $sql .= " status=".(isset($this->status) ? "'".$this->db->escape($this->status)."'" : "null").",";
325  $sql .= " format=".(isset($this->format) ? "'".$this->db->escape($this->format)."'" : "null").",";
326  $sql .= " mailsonde=".(isset($this->mailsonde) ? ((int) $this->mailsonde) : "null").",";
327  $sql .= " allow_comments=".((int) $this->allow_comments).",";
328  $sql .= " allow_spy=".((int) $this->allow_spy);
329  $sql .= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
330 
331  $this->db->begin();
332 
333  dol_syslog(get_class($this)."::update", LOG_DEBUG);
334  $resql = $this->db->query($sql);
335  if (!$resql) {
336  $error++;
337  $this->errors[] = "Error ".$this->db->lasterror();
338  }
339 
340  if (!$error && !$notrigger) {
341  // Call trigger
342  $result = $this->call_trigger('OPENSURVEY_MODIFY', $user);
343  if ($result < 0) {
344  $error++;
345  }
346  // End call triggers
347  }
348 
349  // Commit or rollback
350  if ($error) {
351  foreach ($this->errors as $errmsg) {
352  dol_syslog(get_class($this)."::update ".$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 
371  public function delete(User $user, $notrigger = 0, $numsondage = '')
372  {
373  global $conf, $langs;
374  $error = 0;
375 
376  if (empty($numsondage)) {
377  $numsondage = $this->id_sondage;
378  }
379 
380  $this->db->begin();
381 
382  if (!$error && !$notrigger) {
383  // Call trigger
384  $result = $this->call_trigger('OPENSURVEY_DELETE', $user);
385  if ($result < 0) {
386  $error++;
387  }
388  // End call triggers
389  }
390 
391  if (!$error) {
392  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."opensurvey_comments WHERE id_sondage = '".$this->db->escape($numsondage)."'";
393  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
394  $resql = $this->db->query($sql);
395  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."opensurvey_user_studs WHERE id_sondage = '".$this->db->escape($numsondage)."'";
396  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
397  $resql = $this->db->query($sql);
398 
399  $sql = "DELETE FROM ".MAIN_DB_PREFIX."opensurvey_sondage";
400  $sql .= " WHERE id_sondage = '".$this->db->escape($numsondage)."'";
401 
402  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
403  $resql = $this->db->query($sql);
404  if (!$resql) {
405  $error++; $this->errors[] = "Error ".$this->db->lasterror();
406  }
407  }
408 
409  // Commit or rollback
410  if ($error) {
411  foreach ($this->errors as $errmsg) {
412  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
413  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
414  }
415  $this->db->rollback();
416  return -1 * $error;
417  } else {
418  $this->db->commit();
419  return 1;
420  }
421  }
422 
432  public function getNomUrl($withpicto = 0, $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
433  {
434  global $db, $conf, $langs;
435  global $dolibarr_main_authentication, $dolibarr_main_demo;
436  global $menumanager;
437 
438  if (!empty($conf->dol_no_mouse_hover)) {
439  $notooltip = 1; // Force disable tooltips
440  }
441 
442  $result = '';
443 
444  $label = img_picto('', $this->picto).' <u>'.$langs->trans("ShowSurvey").'</u>';
445  $label .= '<br>';
446  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
447  $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
448 
449  $url = DOL_URL_ROOT.'/opensurvey/card.php?id='.$this->id;
450 
451  // Add param to save lastsearch_values or not
452  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
453  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
454  $add_save_lastsearch_values = 1;
455  }
456  if ($add_save_lastsearch_values) {
457  $url .= '&save_lastsearch_values=1';
458  }
459 
460  $linkclose = '';
461  if (empty($notooltip)) {
462  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
463  $label = $langs->trans("ShowMyObject");
464  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
465  }
466  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
467  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
468  } else {
469  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
470  }
471 
472  $linkstart = '<a href="'.$url.'"';
473  $linkstart .= $linkclose.'>';
474  $linkend = '</a>';
475 
476  $result .= $linkstart;
477  if ($withpicto) {
478  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
479  }
480  if ($withpicto != 2) {
481  $result .= $this->ref;
482  }
483  $result .= $linkend;
484 
485  return $result;
486  }
487 
488  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
494  public function fetch_lines()
495  {
496  // phpcs:enable
497  $this->lines = array();
498 
499  $sql = "SELECT id_users, nom as name, reponses";
500  $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
501  $sql .= " WHERE id_sondage = '".$this->db->escape($this->id_sondage)."'";
502 
503  $resql = $this->db->query($sql);
504 
505  if ($resql) {
506  $num = $this->db->num_rows($resql);
507  $i = 0;
508  while ($i < $num) {
509  $obj = $this->db->fetch_object($resql);
510  $tmp = array('id_users'=>$obj->id_users, 'nom'=>$obj->name, 'reponses'=>$obj->reponses);
511 
512  $this->lines[] = $tmp;
513  $i++;
514  }
515  } else {
516  dol_print_error($this->db);
517  }
518 
519  return count($this->lines);
520  }
521 
528  public function initAsSpecimen()
529  {
530  $this->id = 0;
531 
532  $this->id_sondage = 'a12d5g';
533  $this->description = 'Description of the specimen survey';
534  $this->mail_admin = 'email@email.com';
535  $this->nom_admin = 'surveyadmin';
536  $this->title = 'This is a specimen survey';
537  $this->date_fin = dol_now() + 3600 * 24 * 10;
538  $this->status = 1;
539  $this->format = 'classic';
540  $this->mailsonde = 0;
541  }
542 
548  public function getComments()
549  {
550  $comments = array();
551 
552  $sql = 'SELECT id_comment, usercomment, comment';
553  $sql .= ' FROM '.MAIN_DB_PREFIX.'opensurvey_comments';
554  $sql .= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
555  $sql .= " ORDER BY id_comment";
556  $resql = $this->db->query($sql);
557 
558  if ($resql) {
559  $num_rows = $this->db->num_rows($resql);
560 
561  if ($num_rows > 0) {
562  while ($obj = $this->db->fetch_object($resql)) {
563  $comments[] = $obj;
564  }
565  }
566  }
567 
568  return $comments;
569  }
570 
579  public function addComment($comment, $comment_user, $user_ip = '')
580  {
581  $now = dol_now();
582  $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment, date_creation, ip)";
583  $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)."'" : '').")";
584  $resql = $this->db->query($sql);
585 
586  if (!$resql) {
587  return false;
588  }
589 
590  return true;
591  }
592 
599  public function deleteComment($id_comment)
600  {
601  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'opensurvey_comments WHERE id_comment = '.((int) $id_comment).' AND id_sondage = "'.$this->db->escape($this->id_sondage).'"';
602  $resql = $this->db->query($sql);
603 
604  if (!$resql) {
605  return false;
606  }
607 
608  return true;
609  }
610 
616  private function cleanParameters()
617  {
618  $this->id_sondage = trim($this->id_sondage);
619  $this->description = trim($this->description);
620  $this->mail_admin = trim($this->mail_admin);
621  $this->nom_admin = trim($this->nom_admin);
622  $this->title = trim($this->title);
623  $this->status = (int) $this->status;
624  $this->format = trim($this->format);
625  $this->mailsonde = ($this->mailsonde ? 1 : 0);
626  $this->allow_comments = ($this->allow_comments ? 1 : 0);
627  $this->allow_spy = ($this->allow_spy ? 1 : 0);
628  $this->sujet = trim($this->sujet);
629  }
630 
631 
638  public function getLibStatut($mode)
639  {
640  return $this->LibStatut($this->status, $mode);
641  }
642 
643  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
651  public function LibStatut($status, $mode)
652  {
653  // phpcs:enable
654  global $langs, $conf;
655 
656  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
657  global $langs;
658  //$langs->load("mymodule");
659  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
660  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Opened');
661  $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
662  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
663  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Opened');
664  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
665  }
666 
667  $statusType = 'status'.$status;
668  if ($status == self::STATUS_VALIDATED) {
669  if (0) {
670  $statusType = 'status1';
671  } else {
672  $statusType = 'status4';
673  }
674  }
675  if ($status == self::STATUS_CLOSED) {
676  $statusType = 'status6';
677  }
678 
679  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
680  }
681 
682 
688  public function countVotes()
689  {
690  $result = 0;
691 
692  $sql = " SELECT COUNT(id_users) as nb FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
693  $sql .= " WHERE id_sondage = '".$this->db->escape($this->ref)."'";
694 
695  $resql = $this->db->query($sql);
696  if ($resql) {
697  $obj = $this->db->fetch_object($resql);
698  if ($obj) {
699  $result = $obj->nb;
700  }
701  } else {
702  $this->error = $this->db->lasterror();
703  $this->errors[] = $this->error;
704  }
705 
706  return $result;
707  }
708 }
$object ref
Definition: info.php:78
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Put here description of your class.
__construct($db)
Constructor.
cleanParameters()
Cleans all the class variables before doing an update or an insert.
getLibStatut($mode)
Return status label of Order.
deleteComment($id_comment)
Deletes a comment of the poll.
create(User $user, $notrigger=0)
Create object into database.
fetch($id, $numsurvey='')
Load object in memory from the database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
const STATUS_VALIDATED
Validated/Opened status.
update(User $user, $notrigger=0)
Update object into database.
getComments()
Returns all comments for the current opensurvey poll.
fetch_lines()
Return array of lines.
const STATUS_DRAFT
Draft status (not used)
getNomUrl($withpicto=0, $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
countVotes()
Return number of votes done for this survey.
LibStatut($status, $mode)
Return label of status.
addComment($comment, $comment_user, $user_ip='')
Adds a comment to the poll.
Class to manage Dolibarr users.
Definition: user.class.php:47
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.
Definition: inc.php:41