exportcsv.php 3.2 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. 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. {
  31. $numsondage = GETPOST("id", 'alpha');
  32. }
  33. $object = new Opensurveysondage($db);
  34. $result = $object->fetch(0, $numsondage);
  35. if ($result <= 0) dol_print_error('', 'Failed to get survey id '.$numsondage);
  36. // Security check
  37. if (empty($user->rights->opensurvey->read)) {
  38. accessforbidden();
  39. }
  40. /*
  41. * Actions
  42. */
  43. /*
  44. * View
  45. */
  46. $now = dol_now();
  47. $nbcolonnes = substr_count($object->sujet, ',') + 1;
  48. $toutsujet = explode(",", $object->sujet);
  49. // affichage des sujets du sondage
  50. $input .= $langs->trans("Name").";";
  51. for ($i = 0; $toutsujet[$i]; $i++)
  52. {
  53. if ($object->format == "D")
  54. {
  55. $input .= ''.dol_print_date($toutsujet[$i], 'dayhour').';';
  56. } else {
  57. $input .= ''.$toutsujet[$i].';';
  58. }
  59. }
  60. $input .= "\r\n";
  61. if (strpos($object->sujet, '@') !== false)
  62. {
  63. $input .= ";";
  64. for ($i = 0; $toutsujet[$i]; $i++)
  65. {
  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. {
  78. $num = $db->num_rows($resql);
  79. $i = 0;
  80. while ($i < $num)
  81. {
  82. $obj = $db->fetch_object($resql);
  83. // Le name de l'utilisateur
  84. $nombase = str_replace("°", "'", $obj->name);
  85. $input .= $nombase.';';
  86. //affichage des resultats
  87. $ensemblereponses = $obj->reponses;
  88. for ($k = 0; $k < $nbcolonnes; $k++)
  89. {
  90. $car = substr($ensemblereponses, $k, 1);
  91. if ($car == "1")
  92. {
  93. $input .= 'OK;';
  94. $somme[$k]++;
  95. } elseif ($car == "2")
  96. {
  97. $input .= 'KO;';
  98. $somme[$k]++;
  99. } else {
  100. $input .= ';';
  101. }
  102. }
  103. $input .= "\r\n";
  104. $i++;
  105. }
  106. } else dol_print_error($db);
  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;