Skip to content
Snippets Groups Projects
exportcsv.php 3.94 KiB
Newer Older
Simon Leblanc's avatar
Simon Leblanc committed
<?php
/**
 * This software is governed by the CeCILL-B license. If a copy of this license
 * is not distributed with this file, you can obtain one at
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
 *
 * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
 * Authors of Framadate/OpenSondate: Framasoft (https://github.com/framasoft)
 *
 *
 * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
 * ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
 *
 * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
 * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
Simon Leblanc's avatar
Simon Leblanc committed

include_once __DIR__ . '/app/inc/init.php';
Simon Leblanc's avatar
Simon Leblanc committed

if(!isset($_GET['numsondage']) || ! preg_match(";^[\w\d]{16}$;i", $_GET['numsondage'])) {
    header('Location: studs.php');
Simon Leblanc's avatar
Simon Leblanc committed

$sql = 'SELECT * FROM user_studs WHERE id_sondage='.$connect->Param('numsondage').' ORDER BY id_users';
$sql = $connect->Prepare($sql);
$user_studs = $connect->Execute($sql, array($_GET['numsondage']));
Simon Leblanc's avatar
Simon Leblanc committed

$dsondage = Utils::get_sondage_from_id($_GET['numsondage']);
Simon Leblanc's avatar
Simon Leblanc committed
$nbcolonnes=substr_count($dsondage->sujet,',')+1;

$toutsujet=explode(",",$dsondage->sujet);

//affichage des sujets du sondage
FramaJosephK's avatar
FramaJosephK committed
$input =",";
    if ($dsondage->format=="D"||$dsondage->format=="D+") {
FramaJosephK's avatar
FramaJosephK committed
        if (strpos($dsondage->sujet,'@') !== false) {
            $days=explode("@",$value);
            $input.= '"'.date("j/n/Y",$days[0]).'",';
        } else {
            $input.= '"'.date("j/n/Y",$values).'",';
        }
FramaJosephK's avatar
FramaJosephK committed

        preg_match_all('/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/',$value,$md_a_img);  // Markdown [![alt](src)](href)
        preg_match_all('/!\[(.*?)\]\((.*?)\)/',$value,$md_img);                 // Markdown ![alt](src)
        preg_match_all('/\[(.*?)\]\((.*?)\)/',$value,$md_a);                    // Markdown [text](href)
        if (isset($md_a_img[2][0]) && $md_a_img[2][0]!='' && isset($md_a_img[3][0]) && $md_a_img[3][0]!='') { // [![alt](src)](href)
            $subject_text = (isset($md_a_img[1][0]) && $md_a_img[1][0]!='') ? stripslashes($md_a_img[1][0]) : _("Choice") .' '.($i+1);
        } elseif (isset($md_img[2][0]) && $md_img[2][0]!='') { // ![alt](src)
            $subject_text = (isset($md_img[1][0]) && $md_img[1][0]!='') ? stripslashes($md_img[1][0]) : _("Choice") .' '.($i+1);
        } elseif (isset($md_a[2][0]) && $md_a[2][0]!='') { // [text](href)
            $subject_text = (isset($md_a[1][0]) && $md_a[1][0]!='') ? stripslashes($md_a[1][0]) : _("Choice") .' '.($i+1);
        } else { // text only
            $subject_text = stripslashes($value);
        }
        $input.= '"'.html_entity_decode($subject_text).'",';
Simon Leblanc's avatar
Simon Leblanc committed
}
Simon Leblanc's avatar
Simon Leblanc committed
$input.="\r\n";

if (strpos($dsondage->sujet,'@') !== false) {
FramaJosephK's avatar
FramaJosephK committed
    $input.=",";
    foreach ($toutsujet as $value) {
        $heures=explode("@",$value);
FramaJosephK's avatar
FramaJosephK committed
        $input.= '"'.$heures[1].'",';
Simon Leblanc's avatar
Simon Leblanc committed
}

FramaJosephK's avatar
FramaJosephK committed
while ( $data=$user_studs->FetchNextObject(false)) {
    // Le nom de l'utilisateur
    $nombase=html_entity_decode(str_replace("°","'",$data->nom));
FramaJosephK's avatar
FramaJosephK committed
    $input.= '"'.$nombase.'",';
    //affichage des resultats
    $ensemblereponses=$data->reponses;
    for ($k=0;$k<$nbcolonnes;$k++) {
        $car=substr($ensemblereponses,$k,1);
        switch ($car) {
FramaJosephK's avatar
FramaJosephK committed
            case "1": $input .= '"'._('Yes').'",'; $somme[$k]++; break;
            case "2": $input .= '"'._('Ifneedbe').'",'; break;
            default: $input .= '"'._('No').'",'; break;
Simon Leblanc's avatar
Simon Leblanc committed
}

$filesize = strlen( $input );
$filename=$_GET["numsondage"].".csv";

header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Length: '.$filesize );
header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
header( 'Cache-Control: max-age=10' );
FramaJosephK's avatar
FramaJosephK committed
echo str_replace('&quot;','""',$input);