dolibarr 21.0.0-beta
exportcsv.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.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// Load Dolibarr environment
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
30require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
31require_once DOL_DOCUMENT_ROOT."/opensurvey/class/opensurveysondage.class.php";
32
41$action = GETPOST('action', 'aZ09');
42$numsondage = '';
43if (GETPOST('id')) {
44 $numsondage = GETPOST("id", 'alpha');
45}
46
47// Initialize Objects
48$object = new Opensurveysondage($db);
49$result = $object->fetch(0, $numsondage);
50if ($result <= 0) {
51 dol_print_error(null, 'Failed to get survey id '.$numsondage);
52}
53
54// Security check
55if (!$user->hasRight('opensurvey', 'read')) {
57}
58
59
60/*
61 * Actions
62 */
63
64
65
66/*
67 * View
68 */
69
70$now = dol_now();
71
72$nbcolonnes = substr_count($object->sujet, ',') + 1;
73$toutsujet = explode(",", $object->sujet);
74$somme = array();
75// affichage des sujets du sondage
76$input = $langs->trans("Name").";";
77for ($i = 0; $toutsujet[$i]; $i++) {
78 if ($object->format == "D") {
79 $input .= dol_print_date($toutsujet[$i], 'dayhour').';';
80 } else {
81 $input .= $toutsujet[$i].';';
82 }
83}
84
85$input .= "\r\n";
86
87if (strpos($object->sujet, '@') !== false) {
88 $input .= ";";
89 for ($i = 0; $toutsujet[$i]; $i++) {
90 $heures = explode("@", $toutsujet[$i]);
91 $input .= $heures[1].';';
92 }
93
94 $input .= "\r\n";
95}
96
97
98$sql = 'SELECT nom as name, reponses';
99$sql .= ' FROM '.MAIN_DB_PREFIX."opensurvey_user_studs";
100$sql .= " WHERE id_sondage='".$db->escape($numsondage)."'";
101$sql .= " ORDER BY id_users";
102$resql = $db->query($sql);
103if ($resql) {
104 $num = $db->num_rows($resql);
105 $i = 0;
106 while ($i < $num) {
107 $obj = $db->fetch_object($resql);
108
109 // Le name de l'utilisateur
110 $nombase = str_replace("°", "'", $obj->name);
111 $input .= $nombase.';';
112
113 //affichage des resultats
114 $ensemblereponses = $obj->reponses;
115 for ($k = 0; $k < $nbcolonnes; $k++) {
116 if (empty($somme[$k])) {
117 $somme[$k] = 0;
118 }
119 $car = substr($ensemblereponses, $k, 1);
120 if ($car == "1") {
121 $input .= 'OK;';
122 $somme[$k]++;
123 } elseif ($car == "2") {
124 $input .= ';';
125 $somme[$k]++;
126 } else {
127 $input .= 'KO;';
128 }
129 }
130
131 $input .= "\r\n";
132 $i++;
133 }
134} else {
135 dol_print_error($db);
136}
137
138
139$filesize = strlen($input);
140$filename = $numsondage."_".dol_print_date($now, '%Y%m%d%H%M').".csv";
141
142
143
144header('Content-Type: text/csv; charset=utf-8');
145header('Content-Length: '.$filesize);
146header('Content-Disposition: attachment; filename="'.$filename.'"');
147header('Cache-Control: max-age=10');
148echo $input;
149
150exit;
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Put here description of your class.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.