dolibarr  13.0.2
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 
89  public $status = 1;
90 
94  public $format;
95 
99  public $mailsonde;
100 
104  public $sujet;
105 
109  public $allow_comments;
110 
114  public $allow_spy;
115 
116 
120  const STATUS_DRAFT = 0;
124  const STATUS_VALIDATED = 1;
128  const STATUS_CLOSED = 2;
129 
130 
136  public function __construct($db)
137  {
138  $this->db = $db;
139  }
140 
141 
149  public function create(User $user, $notrigger = 0)
150  {
151  global $conf;
152 
153  $error = 0;
154 
155  // Clean parameters
156  $this->cleanParameters();
157 
158  // Check parameters
159  if (!$this->date_fin > 0) {
160  $this->error = 'BadValueForEndDate';
161  dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);
162  return -1;
163  }
164 
165  // Insert request
166  $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_sondage(";
167  $sql .= "id_sondage,";
168  $sql .= "commentaires,";
169  $sql .= "fk_user_creat,";
170  $sql .= "titre,";
171  $sql .= "date_fin,";
172  $sql .= "status,";
173  $sql .= "format,";
174  $sql .= "mailsonde,";
175  $sql .= "allow_comments,";
176  $sql .= "allow_spy,";
177  $sql .= "sujet,";
178  $sql .= "entity";
179  $sql .= ") VALUES (";
180  $sql .= "'".$this->db->escape($this->id_sondage)."',";
181  $sql .= " ".(empty($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").",";
182  $sql .= " ".(int) $user->id.",";
183  $sql .= " '".$this->db->escape($this->title)."',";
184  $sql .= " '".$this->db->idate($this->date_fin)."',";
185  $sql .= " ".(int) $this->status.",";
186  $sql .= " '".$this->db->escape($this->format)."',";
187  $sql .= " ".((int) $this->mailsonde).",";
188  $sql .= " ".((int) $this->allow_comments).",";
189  $sql .= " ".((int) $this->allow_spy).",";
190  $sql .= " '".$this->db->escape($this->sujet)."',";
191  $sql .= " ".((int) $conf->entity);
192  $sql .= ")";
193 
194  $this->db->begin();
195 
196  dol_syslog(get_class($this)."::create", LOG_DEBUG);
197  $resql = $this->db->query($sql);
198  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
199 
200  if (!$error && !$notrigger) {
201  global $langs, $conf;
202 
203  // Call trigger
204  $result = $this->call_trigger('OPENSURVEY_CREATE', $user);
205  if ($result < 0) $error++;
206  // End call triggers
207  }
208 
209  // Commit or rollback
210  if ($error) {
211  foreach ($this->errors as $errmsg) {
212  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
213  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
214  }
215  $this->db->rollback();
216  return -1 * $error;
217  } else {
218  $this->db->commit();
219  return $this->id;
220  }
221  }
222 
223 
231  public function fetch($id, $numsurvey = '')
232  {
233  $sql = "SELECT";
234  $sql .= " t.id_sondage,";
235  $sql .= " t.titre as title,";
236  $sql .= " t.commentaires as description,";
237  $sql .= " t.mail_admin,";
238  $sql .= " t.nom_admin,";
239  $sql .= " t.fk_user_creat,";
240  $sql .= " t.date_fin,";
241  $sql .= " t.status,";
242  $sql .= " t.format,";
243  $sql .= " t.mailsonde,";
244  $sql .= " t.allow_comments,";
245  $sql .= " t.allow_spy,";
246  $sql .= " t.sujet,";
247  $sql .= " t.tms";
248  $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_sondage as t";
249  $sql .= " WHERE t.id_sondage = '".$this->db->escape($id ? $id : $numsurvey)."'";
250 
251  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
252  $resql = $this->db->query($sql);
253  if ($resql)
254  {
255  if ($this->db->num_rows($resql))
256  {
257  $obj = $this->db->fetch_object($resql);
258 
259  $this->id_sondage = $obj->id_sondage;
260  $this->ref = $this->id_sondage; //For compatibility
261 
262  $this->description = $obj->description;
263  $this->mail_admin = $obj->mail_admin;
264  $this->nom_admin = $obj->nom_admin;
265  $this->title = $obj->title;
266  $this->date_fin = $this->db->jdate($obj->date_fin);
267  $this->status = $obj->status;
268  $this->format = $obj->format;
269  $this->mailsonde = $obj->mailsonde;
270  $this->allow_comments = $obj->allow_comments;
271  $this->allow_spy = $obj->allow_spy;
272  $this->sujet = $obj->sujet;
273  $this->fk_user_creat = $obj->fk_user_creat;
274 
275  $this->date_m = $this->db->jdate($obj->tls);
276  $ret = 1;
277  } else {
278  $sondage = ($id ? 'id='.$id : 'sondageid='.$numsurvey);
279  $this->error = 'Fetch no poll found for '.$sondage;
280  dol_syslog($this->error, LOG_ERR);
281  $ret = 0;
282  }
283 
284  $this->db->free($resql);
285  } else {
286  $this->error = "Error ".$this->db->lasterror();
287  $ret = -1;
288  }
289 
290  return $ret;
291  }
292 
293 
301  public function update(User $user, $notrigger = 0)
302  {
303  global $conf, $langs;
304  $error = 0;
305 
306  // Clean parameters
307  $this->cleanParameters();
308 
309  // Check parameters
310  // Put here code to add a control on parameters values
311 
312  // Update request
313  $sql = "UPDATE ".MAIN_DB_PREFIX."opensurvey_sondage SET";
314  $sql .= " id_sondage=".(isset($this->id_sondage) ? "'".$this->db->escape($this->id_sondage)."'" : "null").",";
315  $sql .= " commentaires=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
316  $sql .= " mail_admin=".(isset($this->mail_admin) ? "'".$this->db->escape($this->mail_admin)."'" : "null").",";
317  $sql .= " nom_admin=".(isset($this->nom_admin) ? "'".$this->db->escape($this->nom_admin)."'" : "null").",";
318  $sql .= " titre=".(isset($this->title) ? "'".$this->db->escape($this->title)."'" : "null").",";
319  $sql .= " date_fin=".(dol_strlen($this->date_fin) != 0 ? "'".$this->db->idate($this->date_fin)."'" : 'null').",";
320  $sql .= " status=".(isset($this->status) ? "'".$this->db->escape($this->status)."'" : "null").",";
321  $sql .= " format=".(isset($this->format) ? "'".$this->db->escape($this->format)."'" : "null").",";
322  $sql .= " mailsonde=".(isset($this->mailsonde) ? $this->db->escape($this->mailsonde) : "null").",";
323  $sql .= " allow_comments=".$this->db->escape($this->allow_comments).",";
324  $sql .= " allow_spy=".$this->db->escape($this->allow_spy);
325  $sql .= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
326 
327  $this->db->begin();
328 
329  dol_syslog(get_class($this)."::update", LOG_DEBUG);
330  $resql = $this->db->query($sql);
331  if (!$resql) {
332  $error++;
333  $this->errors[] = "Error ".$this->db->lasterror();
334  }
335 
336  if (!$error && !$notrigger) {
337  // Call trigger
338  $result = $this->call_trigger('OPENSURVEY_MODIFY', $user);
339  if ($result < 0) $error++;
340  // End call triggers
341  }
342 
343  // Commit or rollback
344  if ($error)
345  {
346  foreach ($this->errors as $errmsg)
347  {
348  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
349  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
350  }
351  $this->db->rollback();
352  return -1 * $error;
353  } else {
354  $this->db->commit();
355  return 1;
356  }
357  }
358 
367  public function delete(User $user, $notrigger = 0, $numsondage = '')
368  {
369  global $conf, $langs;
370  $error = 0;
371 
372  if (empty($numsondage))
373  {
374  $numsondage = $this->id_sondage;
375  }
376 
377  $this->db->begin();
378 
379  if (!$error && !$notrigger) {
380  // Call trigger
381  $result = $this->call_trigger('OPENSURVEY_DELETE', $user);
382  if ($result < 0) $error++;
383  // End call triggers
384  }
385 
386  if (!$error)
387  {
388  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."opensurvey_comments WHERE id_sondage = '".$this->db->escape($numsondage)."'";
389  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
390  $resql = $this->db->query($sql);
391  $sql = 'DELETE FROM '.MAIN_DB_PREFIX."opensurvey_user_studs WHERE id_sondage = '".$this->db->escape($numsondage)."'";
392  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
393  $resql = $this->db->query($sql);
394 
395  $sql = "DELETE FROM ".MAIN_DB_PREFIX."opensurvey_sondage";
396  $sql .= " WHERE id_sondage = '".$this->db->escape($numsondage)."'";
397 
398  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
399  $resql = $this->db->query($sql);
400  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
401  }
402 
403  // Commit or rollback
404  if ($error)
405  {
406  foreach ($this->errors as $errmsg)
407  {
408  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
409  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
410  }
411  $this->db->rollback();
412  return -1 * $error;
413  } else {
414  $this->db->commit();
415  return 1;
416  }
417  }
418 
428  public function getNomUrl($withpicto = 0, $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
429  {
430  global $db, $conf, $langs;
431  global $dolibarr_main_authentication, $dolibarr_main_demo;
432  global $menumanager;
433 
434  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
435 
436  $result = '';
437 
438  $label = img_picto('', $this->picto).' <u>'.$langs->trans("ShowSurvey").'</u>';
439  $label .= '<br>';
440  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref.'<br>';
441  $label .= '<b>'.$langs->trans('Title').':</b> '.$this->title.'<br>';
442 
443  $url = DOL_URL_ROOT.'/opensurvey/card.php?id='.$this->id;
444 
445  // Add param to save lastsearch_values or not
446  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
447  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
448  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
449 
450  $linkclose = '';
451  if (empty($notooltip))
452  {
453  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
454  {
455  $label = $langs->trans("ShowMyObject");
456  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
457  }
458  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
459  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
460  } else {
461  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
462  }
463 
464  $linkstart = '<a href="'.$url.'"';
465  $linkstart .= $linkclose.'>';
466  $linkend = '</a>';
467 
468  $result .= $linkstart;
469  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
470  if ($withpicto != 2) $result .= $this->ref;
471  $result .= $linkend;
472 
473  return $result;
474  }
475 
476  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
482  public function fetch_lines()
483  {
484  // phpcs:enable
485  $ret = array();
486 
487  $sql = "SELECT id_users, nom as name, reponses FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
488  $sql .= " WHERE id_sondage = '".$this->db->escape($this->id_sondage)."'";
489  $resql = $this->db->query($sql);
490 
491  if ($resql)
492  {
493  $num = $this->db->num_rows($resql);
494  $i = 0;
495  while ($i < $num)
496  {
497  $obj = $this->db->fetch_object($resql);
498  $tmp = array('id_users'=>$obj->id_users, 'nom'=>$obj->name, 'reponses'=>$obj->reponses);
499 
500  $ret[] = $tmp;
501  $i++;
502  }
503  } else dol_print_error($this->db);
504 
505  $this->lines = $ret;
506 
507  return count($this->lines);
508  }
509 
516  public function initAsSpecimen()
517  {
518  $this->id = 0;
519 
520  $this->id_sondage = 'a12d5g';
521  $this->description = 'Description of the specimen survey';
522  $this->mail_admin = 'email@email.com';
523  $this->nom_admin = 'surveyadmin';
524  $this->title = 'This is a specimen survey';
525  $this->date_fin = dol_now() + 3600 * 24 * 10;
526  $this->status = 1;
527  $this->format = 'classic';
528  $this->mailsonde = 0;
529  }
530 
536  public function getComments()
537  {
538  $comments = array();
539 
540  $sql = 'SELECT id_comment, usercomment, comment';
541  $sql .= ' FROM '.MAIN_DB_PREFIX.'opensurvey_comments';
542  $sql .= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
543  $sql .= " ORDER BY id_comment";
544  $resql = $this->db->query($sql);
545 
546  if ($resql)
547  {
548  $num_rows = $this->db->num_rows($resql);
549 
550  if ($num_rows > 0)
551  {
552  while ($obj = $this->db->fetch_object($resql))
553  {
554  $comments[] = $obj;
555  }
556  }
557  }
558 
559  return $comments;
560  }
561 
569  public function addComment($comment, $comment_user)
570  {
571  $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment)";
572  $sql .= " VALUES ('".$this->db->escape($this->id_sondage)."','".$this->db->escape($comment)."','".$this->db->escape($comment_user)."')";
573  $resql = $this->db->query($sql);
574 
575  if (!$resql) {
576  return false;
577  }
578 
579  return true;
580  }
581 
588  public function deleteComment($id_comment)
589  {
590  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'opensurvey_comments WHERE id_comment = '.$id_comment.' AND id_sondage = "'.$this->db->escape($this->id_sondage).'"';
591  $resql = $this->db->query($sql);
592 
593  if (!$resql) {
594  return false;
595  }
596 
597  return true;
598  }
599 
605  private function cleanParameters()
606  {
607  $this->id_sondage = trim($this->id_sondage);
608  $this->description = trim($this->description);
609  $this->mail_admin = trim($this->mail_admin);
610  $this->nom_admin = trim($this->nom_admin);
611  $this->title = trim($this->title);
612  $this->status = (int) $this->status;
613  $this->format = trim($this->format);
614  $this->mailsonde = ($this->mailsonde ? 1 : 0);
615  $this->allow_comments = ($this->allow_comments ? 1 : 0);
616  $this->allow_spy = ($this->allow_spy ? 1 : 0);
617  $this->sujet = trim($this->sujet);
618  }
619 
620 
627  public function getLibStatut($mode)
628  {
629  return $this->LibStatut($this->status, $mode);
630  }
631 
632  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
640  public function LibStatut($status, $mode)
641  {
642  // phpcs:enable
643  global $langs, $conf;
644 
645  if (empty($this->labelStatus) || empty($this->labelStatusShort))
646  {
647  global $langs;
648  //$langs->load("mymodule");
649  $this->labelStatus[self::STATUS_DRAFT] = $langs->trans('Draft');
650  $this->labelStatus[self::STATUS_VALIDATED] = $langs->trans('Opened');
651  $this->labelStatus[self::STATUS_CLOSED] = $langs->trans('Closed');
652  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->trans('Draft');
653  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->trans('Opened');
654  $this->labelStatusShort[self::STATUS_CLOSED] = $langs->trans('Closed');
655  }
656 
657  $statusType = 'status'.$status;
658  if ($status == self::STATUS_VALIDATED) {
659  if (0) $statusType = 'status1';
660  else $statusType = 'status4';
661  }
662  if ($status == self::STATUS_CLOSED) $statusType = 'status6';
663 
664  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
665  }
666 }
LibStatut($status, $mode)
Return label of status.
create(User $user, $notrigger=0)
Create object into database.
fetch_lines()
Return array of lines.
const STATUS_DRAFT
Draft status (not used)
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
update(User $user, $notrigger=0)
Update object into database.
fetch($id, $numsurvey= '')
Load object in memory from the database.
dol_now($mode= 'auto')
Return date for now.
cleanParameters()
Cleans all the class variables before doing an update or an insert.
Class to manage Dolibarr users.
Definition: user.class.php:44
deleteComment($id_comment)
Deletes a comment of the poll.
__construct($db)
Constructor.
$conf db
API class for accounts.
Definition: inc.php:54
getNomUrl($withpicto=0, $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
getComments()
Returns all comments for the current opensurvey poll.
getLibStatut($mode)
Return status label of Order.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
const STATUS_VALIDATED
Validated/Opened status.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
addComment($comment, $comment_user)
Adds a comment to the poll.
Put here description of your class.
print $_SERVER["PHP_SELF"]
Edit parameters.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
call_trigger($triggerName, $user)
Call trigger based on this instance.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)