exportcsv.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /**
  19. * \file htdocs/opensurvey/exportcsv.php
  20. * \ingroup opensurvey
  21. * \brief Page to list surveys
  22. */
  23. // Load Dolibarr environment
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
  26. require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
  27. require_once DOL_DOCUMENT_ROOT."/opensurvey/class/opensurveysondage.class.php";
  28. $action = GETPOST('action', 'aZ09');
  29. $numsondage = '';
  30. if (GETPOST('id')) {
  31. $numsondage = GETPOST("id", 'alpha');
  32. }
  33. // Initialize Objects
  34. $object = new Opensurveysondage($db);
  35. $result = $object->fetch(0, $numsondage);
  36. if ($result <= 0) {
  37. dol_print_error('', 'Failed to get survey id '.$numsondage);
  38. }
  39. // Security check
  40. if (!$user->hasRight('opensurvey', 'read')) {
  41. accessforbidden();
  42. }
  43. /*
  44. * Actions
  45. */
  46. /*
  47. * View
  48. */
  49. $now = dol_now();
  50. $nbcolonnes = substr_count($object->sujet, ',') + 1;
  51. $toutsujet = explode(",", $object->sujet);
  52. $somme = array();
  53. // affichage des sujets du sondage
  54. $input = $langs->trans("Name").";";
  55. for ($i = 0; $toutsujet[$i]; $i++) {
  56. if ($object->format == "D") {
  57. $input .= dol_print_date($toutsujet[$i], 'dayhour').';';
  58. } else {
  59. $input .= $toutsujet[$i].';';
  60. }
  61. }
  62. $input .= "\r\n";
  63. if (strpos($object->sujet, '@') !== false) {
  64. $input .= ";";
  65. for ($i = 0; $toutsujet[$i]; $i++) {
  66. $heures = explode("@", $toutsujet[$i]);
  67. $input .= $heures[1].';';
  68. }
  69. $input .= "\r\n";
  70. }
  71. $sql = 'SELECT nom as name, reponses';
  72. $sql .= ' FROM '.MAIN_DB_PREFIX."opensurvey_user_studs";
  73. $sql .= " WHERE id_sondage='".$db->escape($numsondage)."'";
  74. $sql .= " ORDER BY id_users";
  75. $resql = $db->query($sql);
  76. if ($resql) {
  77. $num = $db->num_rows($resql);
  78. $i = 0;
  79. while ($i < $num) {
  80. $obj = $db->fetch_object($resql);
  81. // Le name de l'utilisateur
  82. $nombase = str_replace("°", "'", $obj->name);
  83. $input .= $nombase.';';
  84. //affichage des resultats
  85. $ensemblereponses = $obj->reponses;
  86. for ($k = 0; $k < $nbcolonnes; $k++) {
  87. if (empty($somme[$k])) {
  88. $somme[$k] = 0;
  89. }
  90. $car = substr($ensemblereponses, $k, 1);
  91. if ($car == "1") {
  92. $input .= 'OK;';
  93. $somme[$k]++;
  94. } elseif ($car == "2") {
  95. $input .= ';';
  96. $somme[$k]++;
  97. } else {
  98. $input .= 'KO;';
  99. }
  100. }
  101. $input .= "\r\n";
  102. $i++;
  103. }
  104. } else {
  105. dol_print_error($db);
  106. }
  107. $filesize = strlen($input);
  108. $filename = $numsondage."_".dol_print_date($now, '%Y%m%d%H%M').".csv";
  109. header('Content-Type: text/csv; charset=utf-8');
  110. header('Content-Length: '.$filesize);
  111. header('Content-Disposition: attachment; filename="'.$filename.'"');
  112. header('Cache-Control: max-age=10');
  113. echo $input;
  114. exit;