/
opensurvey.lib.php
279 lines (235 loc) · 8.97 KB
/
opensurvey.lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/opensurvey/lib/opensurvey.lib.php
* \ingroup opensurvey
* \brief Functions for module
*/
/**
* Returns an array with the tabs for the "Opensurvey poll" section
* It loads tabs from modules looking for the entity Opensurveyso
*
* @param Opensurveysondage $object Current viewing poll
* @return array Tabs for the opensurvey section
*/
function opensurvey_prepare_head(Opensurveysondage $object)
{
global $langs, $conf;
$h = 0;
$head = array();
$head[0][0] = 'card.php?id='.$object->id_sondage;
$head[0][1] = $langs->trans("Survey");
$head[0][2] = 'general';
$h++;
$head[1][0] = 'results.php?id='.$object->id_sondage;
$head[1][1] = $langs->trans("SurveyResults");
$nbVotes = $object->countVotes();
if ($nbVotes > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbVotes).'</span>';
}
$head[1][2] = 'preview';
$h++;
// Show more tabs from modules
// Entries must be declared in modules descriptor with line
// $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
// $this->tabs = array('entity:-tabname); to remove a tab
complete_head_from_modules($conf, $langs, $object, $head, $h, 'opensurveypoll');
complete_head_from_modules($conf, $langs, $object, $head, $h, 'opensurveypoll', 'remove');
return $head;
}
/**
* Show header for new member
*
* @param string $title Title
* @param string $head Head array
* @param int $disablejs More content into html header
* @param int $disablehead More content into html header
* @param array $arrayofjs Array of complementary js files
* @param array $arrayofcss Array of complementary css files
* @param string $numsondage Num survey
* @return void
*/
function llxHeaderSurvey($title, $head = "", $disablejs = 0, $disablehead = 0, $arrayofjs = [], $arrayofcss = [], $numsondage = '')
{
global $conf, $langs, $mysoc;
global $dolibarr_main_url_root;
// $replacemainarea = (empty($conf->dol_hide_leftmenu) ? '<div>' : '').'<div>';
top_htmlhead($head, $title, $disablejs, $disablehead, $arrayofjs, $arrayofcss, 0, 1); // Show html headers
print '<body id="mainbody" class="publicnewmemberform">';
print '<span id="dolpaymentspan"></span>'."\n";
print '<div class="center">'."\n";
print '<form name="formulaire" action="studs.php?sondage='.urlencode($numsondage).'#bas" method="POST">'."\n";
print '<input type="hidden" name="sondage" value="'.$numsondage.'"/>';
print '<input type="hidden" name="token" value="'.newToken().'">'."\n";
print "\n";
// Show logo (search order: logo defined by PAYMENT_LOGO_suffix, then PAYMENT_LOGO, then small company logo, large company logo, theme logo, common logo)
// Define logo and logosmall
$logosmall = $mysoc->logo_small;
$logo = $mysoc->logo;
// print '<!-- Show logo (logosmall='.$logosmall.' logo='.$logo.') -->'."\n";
// Define urllogo
$urllogo = '';
$urllogofull = '';
if (!empty($logosmall) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$logosmall)) {
$urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/thumbs/'.$logosmall);
$urllogofull = $dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/thumbs/'.$logosmall);
} elseif (!empty($logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$logo)) {
$urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/'.$logo);
$urllogofull = $dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/'.$logo);
}
// Output html code for logo
if ($urllogo) {
print '<div class="backgreypublicpayment">';
print '<div class="logopublicpayment">';
print '<img id="dolpaymentlogo" src="'.$urllogo.'"';
print '>';
print '</div>';
if (!getDolGlobalString('MAIN_HIDE_POWERED_BY')) {
print '<div class="poweredbypublicpayment opacitymedium right"><a class="poweredbyhref" href="https://www.dolibarr.org?utm_medium=website&utm_source=poweredby" target="dolibarr" rel="noopener">'.$langs->trans("PoweredBy").'<br><img src="'.DOL_URL_ROOT.'/theme/dolibarr_logo.svg" width="80px"></a></div>';
}
print '</div>';
}
if (getDolGlobalString('OPENSURVEY_IMAGE_PUBLIC_INTERFACE')) {
print '<div class="backimagepublicopensurvey">';
print '<img id="idOPENSURVEY_IMAGE_PUBLIC_INTERFACE" src="' . getDolGlobalString('OPENSURVEY_IMAGE_PUBLIC_INTERFACE').'">';
print '</div>';
}
print '<div class="survey_borders"><br><br>';
}
/**
* Show footer for new member
*
* @return void
*/
function llxFooterSurvey()
{
print '</div>';
print '</form>';
print '</div>';
printCommonFooter('public');
dol_htmloutput_events();
print "</body>\n";
print "</html>\n";
}
/**
* get_server_name
*
* @return string URL to use
*/
function get_server_name()
{
global $dolibarr_main_url_root;
$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
//$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
$url = $urlwithouturlroot.dol_buildpath('/opensurvey/', 1);
if (!preg_match("|/$|", $url)) {
$url = $url."/";
}
return $url;
}
/**
* Fonction vérifiant l'existance et la valeur non vide d'une clé d'un tableau
*
* @param string $name Key to test
* @param array $tableau Array in which searching key ($_POST by default)
* @return bool True if key exists and return a non empty value
*/
function issetAndNoEmpty($name, $tableau = null)
{
if ($tableau === null) {
$tableau = $_POST;
}
return (isset($tableau[$name]) === true && empty($tableau[$name]) === false);
}
/**
* Fonction permettant de générer les URL pour les sondage
*
* @param string $id L'identifiant du sondage
* @param bool $admin True pour générer une URL pour l'administration d'un sondage, False pour un URL publique
* @return string L'url pour le sondage
*/
function getUrlSondage($id, $admin = false)
{
if ($admin === true) {
$url = get_server_name().'results.php?id='.$id;
} else {
$url = get_server_name().'/public/studs.php?sondage='.$id;
}
return $url;
}
/**
* Generate a random id
*
* @param int $car Length of string to generate key
* @return string
*/
function dol_survey_random($car)
{
$string = "";
$chaine = "abcdefghijklmnopqrstuvwxyz123456789";
mt_srand((int) ((float) microtime() * 1000000));
for ($i = 0; $i < $car; $i++) {
$string .= $chaine[mt_rand() % strlen($chaine)];
}
return $string;
}
/**
* Add a poll
*
* @return void
*/
function ajouter_sondage()
{
global $db, $user;
require_once DOL_DOCUMENT_ROOT.'/opensurvey/class/opensurveysondage.class.php';
$sondage = dol_survey_random(16);
$allow_comments = empty($_SESSION['allow_comments']) ? 0 : 1;
$allow_spy = empty($_SESSION['allow_spy']) ? 0 : 1;
// Insert survey
$opensurveysondage = new Opensurveysondage($db);
$opensurveysondage->id_sondage = $sondage;
$opensurveysondage->description = $_SESSION['description'];
$opensurveysondage->mail_admin = $_SESSION['adresse'];
$opensurveysondage->nom_admin = $_SESSION['nom'];
$opensurveysondage->title = $_SESSION['title'];
$opensurveysondage->date_fin = $_SESSION['champdatefin'];
$opensurveysondage->format = $_SESSION['formatsondage'];
$opensurveysondage->mailsonde = $_SESSION['mailsonde'];
$opensurveysondage->allow_comments = $allow_comments;
$opensurveysondage->allow_spy = $allow_spy;
$opensurveysondage->sujet = $_SESSION['toutchoix'];
$res = $opensurveysondage->create($user);
if ($res < 0) {
dol_print_error($db);
}
unset($_SESSION["title"]);
unset($_SESSION["nom"]);
unset($_SESSION["adresse"]);
unset($_SESSION["description"]);
unset($_SESSION["mailsonde"]);
unset($_SESSION['allow_comments']);
unset($_SESSION['allow_spy']);
unset($_SESSION['toutchoix']);
unset($_SESSION['totalchoixjour']);
unset($_SESSION['champdatefin']);
$urlback = dol_buildpath('/opensurvey/card.php', 1).'?id='.$sondage;
header("Location: ".$urlback);
exit();
}