exportcsv.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <http://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. /*
  37. * Actions
  38. */
  39. /*
  40. * View
  41. */
  42. $now=dol_now();
  43. $nbcolonnes=substr_count($object->sujet,',')+1;
  44. $toutsujet=explode(",",$object->sujet);
  45. // affichage des sujets du sondage
  46. $input.=$langs->trans("Name").";";
  47. for ($i=0;$toutsujet[$i];$i++)
  48. {
  49. if ($object->format=="D")
  50. {
  51. $input.=''.dol_print_date($toutsujet[$i],'dayhour').';';
  52. } else {
  53. $input.=''.$toutsujet[$i].';';
  54. }
  55. }
  56. $input.="\r\n";
  57. if (strpos($object->sujet,'@') !== false)
  58. {
  59. $input.=";";
  60. for ($i=0;$toutsujet[$i];$i++)
  61. {
  62. $heures=explode("@",$toutsujet[$i]);
  63. $input.=''.$heures[1].';';
  64. }
  65. $input.="\r\n";
  66. }
  67. $sql ='SELECT nom as name, reponses';
  68. $sql.=' FROM '.MAIN_DB_PREFIX."opensurvey_user_studs";
  69. $sql.=" WHERE id_sondage='" . $db->escape($numsondage) . "'";
  70. $sql.=" ORDER BY id_users";
  71. $resql=$db->query($sql);
  72. if ($resql)
  73. {
  74. $num=$db->num_rows($resql);
  75. $i=0;
  76. while ($i < $num)
  77. {
  78. $obj=$db->fetch_object($resql);
  79. // Le name de l'utilisateur
  80. $nombase=str_replace("°","'",$obj->name);
  81. $input.=$nombase.';';
  82. //affichage des resultats
  83. $ensemblereponses=$obj->reponses;
  84. for ($k=0;$k<$nbcolonnes;$k++)
  85. {
  86. $car=substr($ensemblereponses,$k,1);
  87. if ($car == "1")
  88. {
  89. $input.='OK;';
  90. $somme[$k]++;
  91. }
  92. else if ($car == "2")
  93. {
  94. $input.='KO;';
  95. $somme[$k]++;
  96. }
  97. else
  98. {
  99. $input.=';';
  100. }
  101. }
  102. $input.="\r\n";
  103. $i++;
  104. }
  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;