dolibarr 21.0.0-alpha
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
26// Load Dolibarr environment
27require '../main.inc.php';
28require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
29require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
30require_once DOL_DOCUMENT_ROOT."/opensurvey/class/opensurveysondage.class.php";
31
32
33$action = GETPOST('action', 'aZ09');
34$numsondage = '';
35if (GETPOST('id')) {
36 $numsondage = GETPOST("id", 'alpha');
37}
38
39// Initialize Objects
40$object = new Opensurveysondage($db);
41$result = $object->fetch(0, $numsondage);
42if ($result <= 0) {
43 dol_print_error(null, 'Failed to get survey id '.$numsondage);
44}
45
46// Security check
47if (!$user->hasRight('opensurvey', 'read')) {
49}
50
51
52/*
53 * Actions
54 */
55
56
57
58/*
59 * View
60 */
61
62$now = dol_now();
63
64$nbcolonnes = substr_count($object->sujet, ',') + 1;
65$toutsujet = explode(",", $object->sujet);
66$somme = array();
67// affichage des sujets du sondage
68$input = $langs->trans("Name").";";
69for ($i = 0; $toutsujet[$i]; $i++) {
70 if ($object->format == "D") {
71 $input .= dol_print_date($toutsujet[$i], 'dayhour').';';
72 } else {
73 $input .= $toutsujet[$i].';';
74 }
75}
76
77$input .= "\r\n";
78
79if (strpos($object->sujet, '@') !== false) {
80 $input .= ";";
81 for ($i = 0; $toutsujet[$i]; $i++) {
82 $heures = explode("@", $toutsujet[$i]);
83 $input .= $heures[1].';';
84 }
85
86 $input .= "\r\n";
87}
88
89
90$sql = 'SELECT nom as name, reponses';
91$sql .= ' FROM '.MAIN_DB_PREFIX."opensurvey_user_studs";
92$sql .= " WHERE id_sondage='".$db->escape($numsondage)."'";
93$sql .= " ORDER BY id_users";
94$resql = $db->query($sql);
95if ($resql) {
96 $num = $db->num_rows($resql);
97 $i = 0;
98 while ($i < $num) {
99 $obj = $db->fetch_object($resql);
100
101 // Le name de l'utilisateur
102 $nombase = str_replace("°", "'", $obj->name);
103 $input .= $nombase.';';
104
105 //affichage des resultats
106 $ensemblereponses = $obj->reponses;
107 for ($k = 0; $k < $nbcolonnes; $k++) {
108 if (empty($somme[$k])) {
109 $somme[$k] = 0;
110 }
111 $car = substr($ensemblereponses, $k, 1);
112 if ($car == "1") {
113 $input .= 'OK;';
114 $somme[$k]++;
115 } elseif ($car == "2") {
116 $input .= ';';
117 $somme[$k]++;
118 } else {
119 $input .= 'KO;';
120 }
121 }
122
123 $input .= "\r\n";
124 $i++;
125 }
126} else {
127 dol_print_error($db);
128}
129
130
131$filesize = strlen($input);
132$filename = $numsondage."_".dol_print_date($now, '%Y%m%d%H%M').".csv";
133
134
135
136header('Content-Type: text/csv; charset=utf-8');
137header('Content-Length: '.$filesize);
138header('Content-Disposition: attachment; filename="'.$filename.'"');
139header('Cache-Control: max-age=10');
140echo $input;
141
142exit;
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.