dolibarr 18.0.6
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
28require_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;
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 $conf, $langs;
461
462 if (!empty($conf->dol_no_mouse_hover)) {
463 $notooltip = 1; // Force disable tooltips
464 }
465
466 $result = '';
467 $params = [
468 'id' => $this->id,
469 'objecttype' => $this->element,
470 ];
471 $classfortooltip = 'classfortooltip';
472 $dataparams = '';
473 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
474 $classfortooltip = 'classforajaxtooltip';
475 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
476 $label = '';
477 } else {
478 $label = implode($this->getTooltipContentArray($params));
479 }
480
481 $url = DOL_URL_ROOT.'/opensurvey/card.php?id='.$this->id;
482
483 // Add param to save lastsearch_values or not
484 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
485 if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
486 $add_save_lastsearch_values = 1;
487 }
488 if ($add_save_lastsearch_values) {
489 $url .= '&save_lastsearch_values=1';
490 }
491
492 $linkclose = '';
493 if (empty($notooltip)) {
494 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
495 $label = $langs->trans("ShowMyObject");
496 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
497 }
498 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
499 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
500 } else {
501 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
502 }
503
504 $linkstart = '<a href="'.$url.'"';
505 $linkstart .= $linkclose.'>';
506 $linkend = '</a>';
507
508 $result .= $linkstart;
509 if ($withpicto) {
510 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
511 }
512 if ($withpicto != 2) {
513 $result .= $this->ref;
514 }
515 $result .= $linkend;
516
517 return $result;
518 }
519
520 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
526 public function fetch_lines()
527 {
528 // phpcs:enable
529 $this->lines = array();
530
531 $sql = "SELECT id_users, nom as name, reponses";
532 $sql .= " FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
533 $sql .= " WHERE id_sondage = '".$this->db->escape($this->id_sondage)."'";
534
535 $resql = $this->db->query($sql);
536
537 if ($resql) {
538 $num = $this->db->num_rows($resql);
539 $i = 0;
540 while ($i < $num) {
541 $obj = $this->db->fetch_object($resql);
542 $tmp = array('id_users'=>$obj->id_users, 'nom'=>$obj->name, 'reponses'=>$obj->reponses);
543
544 $this->lines[] = $tmp;
545 $i++;
546 }
547 } else {
548 dol_print_error($this->db);
549 }
550
551 return count($this->lines);
552 }
553
560 public function initAsSpecimen()
561 {
562 $this->id = 0;
563
564 $this->id_sondage = 'a12d5g';
565 $this->description = 'Description of the specimen survey';
566 $this->mail_admin = 'email@email.com';
567 $this->nom_admin = 'surveyadmin';
568 $this->title = 'This is a specimen survey';
569 $this->date_fin = dol_now() + 3600 * 24 * 10;
570 $this->status = 1;
571 $this->format = 'classic';
572 $this->mailsonde = 0;
573 }
574
580 public function getComments()
581 {
582 $comments = array();
583
584 $sql = 'SELECT id_comment, usercomment, comment';
585 $sql .= ' FROM '.MAIN_DB_PREFIX.'opensurvey_comments';
586 $sql .= " WHERE id_sondage='".$this->db->escape($this->id_sondage)."'";
587 $sql .= " ORDER BY id_comment";
588 $resql = $this->db->query($sql);
589
590 if ($resql) {
591 $num_rows = $this->db->num_rows($resql);
592
593 if ($num_rows > 0) {
594 while ($obj = $this->db->fetch_object($resql)) {
595 $comments[] = $obj;
596 }
597 }
598 }
599
600 return $comments;
601 }
602
611 public function addComment($comment, $comment_user, $user_ip = '')
612 {
613 $now = dol_now();
614 $sql = "INSERT INTO ".MAIN_DB_PREFIX."opensurvey_comments (id_sondage, comment, usercomment, date_creation, ip)";
615 $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)."'" : '').")";
616 $resql = $this->db->query($sql);
617
618 if (!$resql) {
619 return false;
620 }
621
622 return true;
623 }
624
631 public function deleteComment($id_comment)
632 {
633 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'opensurvey_comments WHERE id_comment = '.((int) $id_comment).' AND id_sondage = "'.$this->db->escape($this->id_sondage).'"';
634 $resql = $this->db->query($sql);
635
636 if (!$resql) {
637 return false;
638 }
639
640 return true;
641 }
642
648 private function cleanParameters()
649 {
650 $this->id_sondage = trim($this->id_sondage);
651 $this->description = trim($this->description);
652 $this->mail_admin = trim($this->mail_admin);
653 $this->nom_admin = trim($this->nom_admin);
654 $this->title = trim($this->title);
655 $this->status = (int) $this->status;
656 $this->format = trim($this->format);
657 $this->mailsonde = ($this->mailsonde ? 1 : 0);
658 $this->allow_comments = ($this->allow_comments ? 1 : 0);
659 $this->allow_spy = ($this->allow_spy ? 1 : 0);
660 $this->sujet = trim($this->sujet);
661 }
662
663
670 public function getLibStatut($mode)
671 {
672 return $this->LibStatut($this->status, $mode);
673 }
674
675 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
683 public function LibStatut($status, $mode)
684 {
685 // phpcs:enable
686 global $langs, $conf;
687
688 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
689 global $langs;
690 //$langs->load("mymodule");
691 $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
692 $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Opened');
693 $this->labelStatus[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
694 $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
695 $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Opened');
696 $this->labelStatusShort[self::STATUS_CLOSED] = $langs->transnoentitiesnoconv('Closed');
697 }
698
699 $statusType = 'status'.$status;
700 if ($status == self::STATUS_VALIDATED) {
701 if (0) {
702 $statusType = 'status1';
703 } else {
704 $statusType = 'status4';
705 }
706 }
707 if ($status == self::STATUS_CLOSED) {
708 $statusType = 'status6';
709 }
710
711 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
712 }
713
714
720 public function countVotes()
721 {
722 $result = 0;
723
724 $sql = " SELECT COUNT(id_users) as nb FROM ".MAIN_DB_PREFIX."opensurvey_user_studs";
725 $sql .= " WHERE id_sondage = '".$this->db->escape($this->ref)."'";
726
727 $resql = $this->db->query($sql);
728 if ($resql) {
729 $obj = $this->db->fetch_object($resql);
730 if ($obj) {
731 $result = $obj->nb;
732 }
733 } else {
734 $this->error = $this->db->lasterror();
735 $this->errors[] = $this->error;
736 }
737
738 return $result;
739 }
740}
$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.
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.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after 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)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.