dolibarr  19.0.0-dev
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 
121  public $fields = array();
122 
123 
127  const STATUS_DRAFT = 0;
131  const STATUS_VALIDATED = 1;
135  const STATUS_CLOSED = 2;
136 
137 
143  public function __construct($db)
144  {
145  $this->db = $db;
146  }
147 
148 
156  public function create(User $user, $notrigger = 0)
157  {
158  global $conf;
159 
160  $error = 0;
161 
162  // Clean parameters
163  $this->cleanParameters();
164 
165  // Check parameters
166  if (!$this->date_fin > 0) {
167  $this->error = 'BadValueForEndDate';
168  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
169  return -1;
170  }
171 
172  // Insert request
173  $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_sondage(";
174  $sql .= "id_sondage,";
175  $sql .= "commentaires,";
176  $sql .= "fk_user_creat,";
177  $sql .= "titre,";
178  $sql .= "date_fin,";
179  $sql .= "status,";
180  $sql .= "format,";
181  $sql .= "mailsonde,";
182  $sql .= "allow_comments,";
183  $sql .= "allow_spy,";
184  $sql .= "sujet,";
185  $sql .= "entity";
186  $sql .= ") VALUES (";
187  $sql .= "'".$this->db->escape($this->id_sondage)."',";
188  $sql .= " ".(empty($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").",";
189  $sql .= " ".(int) $user->id.",";
190  $sql .= " '".$this->db->escape($this->title)."',";
191  $sql .= " '".$this->db->idate($this->date_fin)."',";
192  $sql .= " ".(int) $this->status.",";
193  $sql .= " '".$this->db->escape($this->format)."',";
194  $sql .= " ".((int) $this->mailsonde).",";
195  $sql .= " ".((int) $this->allow_comments).",";
196  $sql .= " ".((int) $this->allow_spy).",";
197  $sql .= " '".$this->db->escape($this->sujet)."',";
198  $sql .= " ".((int) $conf->entity);
199  $sql .= ")";
200 
201  $this->db->begin();
202 
203  dol_syslog(get_class($this)."::create", LOG_DEBUG);
204  $resql = $this->db->query($sql);
205  if (!$resql) {
206  $error++; $this->errors[] = "Error ".$this->db->lasterror();
207  }
208 
209  if (!$error && !$notrigger) {
210  global $langs, $conf;
211 
212  // Call trigger
213  $result = $this->call_trigger('OPENSURVEY_CREATE', $user);
214  if ($result < 0) {
215  $error++;
216  }
217  // End call triggers
218  }
219 
220  // Commit or rollback
221  if ($error) {
222  foreach ($this->errors as $errmsg) {
223  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
224  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
225  }
226  $this->db->rollback();
227  return -1 * $error;
228  } else {
229  $this->db->commit();
230  return $this->id;
231  }
232  }
233 
234 
242  public function fetch($id, $numsurvey = '')
243  {
244  $sql = "SELECT";
245  $sql .= " t.id_sondage,";
246  $sql .= " t.titre as title,";
247  $sql .= " t.commentaires as description,";
248  $sql .= " t.mail_admin,";
249  $sql .= " t.nom_admin,";
250  $sql .= " t.fk_user_creat,";
251  $sql .= " t.date_fin,";
252  $sql .= " t.status,";
253  $sql .= " t.format,";
254  $sql .= " t.mailsonde,";
255  $sql .= " t.allow_comments,";
256  $sql .= " t.allow_spy,";
257  $sql .= " t.sujet,";
258  $sql .= " t.tms";
259  $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_sondage as t";
260  $sql .= " WHERE t.id_sondage = '".$this->db->escape($id ? $id : $numsurvey)."'";
261 
262  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
263  $resql = $this->db->query($sql);
264  if ($resql) {
265  if ($this->db->num_rows($resql)) {
266  $obj = $this->db->fetch_object($resql);
267 
268  $this->id_sondage = $obj->id_sondage;
269  $this->ref = $this->id_sondage; //For compatibility
270 
271  $this->description = $obj->description;
272  $this->mail_admin = $obj->mail_admin;
273  $this->nom_admin = $obj->nom_admin;
274  $this->title = $obj->title;
275  $this->date_fin = $this->db->jdate($obj->date_fin);
276  $this->status = $obj->status;
277  $this->format = $obj->format;
278  $this->mailsonde = $obj->mailsonde;
279  $this->allow_comments = $obj->allow_comments;
280  $this->allow_spy = $obj->allow_spy;
281  $this->sujet = $obj->sujet;
282  $this->fk_user_creat = $obj->fk_user_creat;
283 
284  $this->date_m = $this->db->jdate(!empty($obj->tls) ? $obj->tls : "");
285  $ret = 1;
286  } else {
287  $sondage = ($id ? 'id='.$id : 'sondageid='.$numsurvey);
288  $this->error = 'Fetch no poll found for '.$sondage;
289  dol_syslog($this->error, LOG_ERR);
290  $ret = 0;
291  }
292 
293  $this->db->free($resql);
294  } else {
295  $this->error = "Error ".$this->db->lasterror();
296  $ret = -1;
297  }
298 
299  return $ret;
300  }
301 
302 
310  public function update(User $user, $notrigger = 0)
311  {
312  global $conf, $langs;
313  $error = 0;
314 
315  // Clean parameters
316  $this->cleanParameters();
317 
318  // Check parameters
319  // Put here code to add a control on parameters values
320 
321  // Update request
322  $sql = "UPDATE ".MAIN_DB_PREFIX."opensurvey_sondage SET";
323  $sql .= " id_sondage=".(isset($this->id_sondage) ? "'".$this->db->escape($this->id_sondage)."'" : "null").",";
324  $sql .= " commentaires=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
325  $sql .= " mail_admin=".(isset($this->mail_admin) ? "'".$this->db->escape($this->mail_admin)."'" : "null").",";
326  $sql .= " nom_admin=".(isset($this->nom_admin) ? "'".$this->db->escape($this->nom_admin)."'" : "null").",";
327  $sql .= " titre=".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "null").",";
328  $sql .= " date_fin=".(dol_strlen($this->date_fin) != 0 ? "'".$this->db->idate($this->date_fin)."'" : 'null').",";
329  $sql .= " status=".(isset($this->status) ? "'".$this->db->escape($this->status)."'" : "null").",";
330  $sql .= " format=".(isset($this->format) ? "'".$this->db->escape($this->format)."'" : "null").",";
331  $sql .= " mailsonde=".(isset($this->mailsonde) ? ((int) $this->mailsonde) : "null").",";
332  $sql .= " allow_comments=".((int) $this->allow_comments).",";
333  $sql .= " allow_spy=".((int) $this->allow_spy);
334  $sql .= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
335 
336  $this->db->begin();
337 
338  dol_syslog(get_class($this)."::update", LOG_DEBUG);
339  $resql = $this->db->query($sql);
340  if (!$resql) {
341  $error++;
342  $this->errors[] = "Error ".$this->db->lasterror();
343  }
344 
345  if (!$error && !$notrigger) {
346  // Call trigger
347  $result = $this->call_trigger('OPENSURVEY_MODIFY', $user);
348  if ($result < 0) {
349  $error++;
350  }
351  // End call triggers
352  }
353 
354  // Commit or rollback
355  if ($error) {
356  foreach ($this->errors as $errmsg) {
357  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
358  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
359  }
360  $this->db->rollback();
361  return -1 * $error;
362  } else {
363  $this->db->commit();
364  return 1;
365  }
366  }
367 
376  public function delete(User $user, $notrigger = 0, $numsondage = '')
377  {
378  global $conf, $langs;
379  $error = 0;
380 
381  if (empty($numsondage)) {
382  $numsondage = $this->id_sondage;
383  }
384 
385  $this->db->begin();
386 
387  if (!$error && !$notrigger) {
388  // Call trigger
389  $result = $this->call_trigger('OPENSURVEY_DELETE', $user);
390  if ($result < 0) {
391  $error++;
392  }
393  // End call triggers
394  }
395 
396  if (!$error) {
397  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."opensurvey_comments WHERE id_sondage = '".$this->db->escape($numsondage)."'";
398  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
399  $resql = $this->db->query($sql);
400  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."opensurvey_user_studs WHERE id_sondage = '".$this->db->escape($numsondage)."'";
401  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
402  $resql = $this->db->query($sql);
403 
404  $sql = "DELETE FROM ".MAIN_DB_PREFIX."opensurvey_sondage";
405  $sql .= " WHERE id_sondage = '".$this->db->escape($numsondage)."'";
406 
407  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
408  $resql = $this->db->query($sql);
409  if (!$resql) {
410  $error++; $this->errors[] = "Error ".$this->db->lasterror();
411  }
412  }
413 
414  // Commit or rollback
415  if ($error) {
416  foreach ($this->errors as $errmsg) {
417  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
418  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
419  }
420  $this->db->rollback();
421  return -1 * $error;
422  } else {
423  $this->db->commit();
424  return 1;
425  }
426  }
427 
435  public function getTooltipContentArray($params)
436  {
437  global $conf, $langs;
438 
439  $langs->load('opensurvey');
440 
441  $datas = [];
442  $datas['picto'] = img_picto('', $this->picto).' <u>'.$langs->trans("ShowSurvey").'</u>';
443  $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
444  $datas['title'] = '<br><b>'.$langs->trans('Title').':</b> '.$this->title;
445 
446  return $datas;
447  }
448 
458  public function getNomUrl($withpicto = 0, $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
459  {
460  global $db, $conf, $langs;
461  global $dolibarr_main_authentication, $dolibarr_main_demo;
462  global $menumanager;
463 
464  if (!empty($conf->dol_no_mouse_hover)) {
465  $notooltip = 1; // Force disable tooltips
466  }
467 
468  $result = '';
469  $params = [
470  'id' => $this->id,
471  'objecttype' => $this->element,
472  ];
473  $classfortooltip = 'classfortooltip';
474  $dataparams = '';
475  if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
476  $classfortooltip = 'classforajaxtooltip';
477  $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
478  $label = '';
479  } else {
480  $label = implode($this->getTooltipContentArray($params));
481  }
482 
483  $url = DOL_URL_ROOT.'/opensurvey/card.php?id='.$this->id;
484 
485  // Add param to save lastsearch_values or not
486  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
487  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
488  $add_save_lastsearch_values = 1;
489  }
490  if ($add_save_lastsearch_values) {
491  $url .= '&save_lastsearch_values=1';
492  }
493 
494  $linkclose = '';
495  if (empty($notooltip)) {
496  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
497  $label = $langs->trans("ShowMyObject");
498  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
499  }
500  $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
501  $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
502  } else {
503  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
504  }
505 
506  $linkstart = '<a href="'.$url.'"';
507  $linkstart .= $linkclose.'>';
508  $linkend = '</a>';
509 
510  $result .= $linkstart;
511  if ($withpicto) {
512  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
513  }
514  if ($withpicto != 2) {
515  $result .= $this->ref;
516  }
517  $result .= $linkend;
518 
519  return $result;
520  }
521 
522  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
528  public function fetch_lines()
529  {
530  // phpcs:enable
531  $this->lines = array();
532 
533  $sql = "SELECT id_users, nom as name, reponses";
534  $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
535  $sql .= " WHERE id_sondage = '".$this->db->escape($this->id_sondage)."'";
536 
537  $resql = $this->db->query($sql);
538 
539  if ($resql) {
540  $num = $this->db->num_rows($resql);
541  $i = 0;
542  while ($i < $num) {
543  $obj = $this->db->fetch_object($resql);
544  $tmp = array('id_users'=>$obj->id_users, 'nom'=>$obj->name, 'reponses'=>$obj->reponses);
545 
546  $this->lines[] = $tmp;
547  $i++;
548  }
549  } else {
550  dol_print_error($this->db);
551  }
552 
553  return count($this->lines);
554  }
555 
562  public function initAsSpecimen()
563  {
564  $this->id = 0;
565 
566  $this->id_sondage = 'a12d5g';
567  $this->description = 'Description of the specimen survey';
568  $this->mail_admin = 'email@email.com';
569  $this->nom_admin = 'surveyadmin';
570  $this->title = 'This is a specimen survey';
571  $this->date_fin = dol_now() + 3600 * 24 * 10;
572  $this->status = 1;
573  $this->format = 'classic';
574  $this->mailsonde = 0;
575  }
576 
582  public function getComments()
583  {
584  $comments = array();
585 
586  $sql = 'SELECT id_comment, usercomment, comment';
587  $sql .= ' FROM '.MAIN_DB_PREFIX.'opensurvey_comments';
588  $sql .= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
589  $sql .= " ORDER BY id_comment";
590  $resql = $this->db->query($sql);
591 
592  if ($resql) {
593  $num_rows = $this->db->num_rows($resql);
594 
595  if ($num_rows > 0) {
596  while ($obj = $this->db->fetch_object($resql)) {
597  $comments[] = $obj;
598  }
599  }
600  }
601 
602  return $comments;
603  }
604 
613  public function addComment($comment, $comment_user, $user_ip = '')
614  {
615  $now = dol_now();
616  $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment, date_creation, ip)";
617  $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)."'" : '').")";
618  $resql = $this->db->query($sql);
619 
620  if (!$resql) {
621  return false;
622  }
623 
624  return true;
625  }
626 
633  public function deleteComment($id_comment)
634  {
635  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'opensurvey_comments WHERE id_comment = '.((int) $id_comment).' AND id_sondage = "'.$this->db->escape($this->id_sondage).'"';
636  $resql = $this->db->query($sql);
637 
638  if (!$resql) {
639  return false;
640  }
641 
642  return true;
643  }
644 
650  private function cleanParameters()
651  {
652  $this->id_sondage = trim($this->id_sondage);
653  $this->description = trim($this->description);
654  $this->mail_admin = trim($this->mail_admin);
655  $this->nom_admin = trim($this->nom_admin);
656  $this->title = trim($this->title);
657  $this->status = (int) $this->status;
658  $this->format = trim($this->format);
659  $this->mailsonde = ($this->mailsonde ? 1 : 0);
660  $this->allow_comments = ($this->allow_comments ? 1 : 0);
661  $this->allow_spy = ($this->allow_spy ? 1 : 0);
662  $this->sujet = trim($this->sujet);
663  }
664 
665 
672  public function getLibStatut($mode)
673  {
674  return $this->LibStatut($this->status, $mode);
675  }
676 
677  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
685  public function LibStatut($status, $mode)
686  {
687  // phpcs:enable
688  global $langs, $conf;
689 
690  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
691  global $langs;
692  //$langs->load("mymodule");
693  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
694  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Opened');
695  $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
696  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
697  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Opened');
698  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
699  }
700 
701  $statusType = 'status'.$status;
702  if ($status == self::STATUS_VALIDATED) {
703  if (0) {
704  $statusType = 'status1';
705  } else {
706  $statusType = 'status4';
707  }
708  }
709  if ($status == self::STATUS_CLOSED) {
710  $statusType = 'status6';
711  }
712 
713  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
714  }
715 
716 
722  public function countVotes()
723  {
724  $result = 0;
725 
726  $sql = " SELECT COUNT(id_users) as nb FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
727  $sql .= " WHERE id_sondage = '".$this->db->escape($this->ref)."'";
728 
729  $resql = $this->db->query($sql);
730  if ($resql) {
731  $obj = $this->db->fetch_object($resql);
732  if ($obj) {
733  $result = $obj->nb;
734  }
735  } else {
736  $this->error = $this->db->lasterror();
737  $this->errors[] = $this->error;
738  }
739 
740  return $result;
741  }
742 }
$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.
getTooltipContentArray($params)
getTooltipContentArray
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:48
if(isModEnabled('facture') && $user->hasRight('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') && $user->hasRight('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)) $sql
Social contributions to pay.
Definition: index.php:746
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.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
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.