exportcsv.php 3.2 KB

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