generate a csv file for the results of a survey
authorx2004laborde <x2004laborde@839d8a87-29fc-0310-9880-83ba4fa771e5>
Wed, 11 Apr 2007 21:21:03 +0000 (21:21 +0000)
committerx2004laborde <x2004laborde@839d8a87-29fc-0310-9880-83ba4fa771e5>
Wed, 11 Apr 2007 21:21:03 +0000 (21:21 +0000)
git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1686 839d8a87-29fc-0310-9880-83ba4fa771e5

modules/survey.php
modules/survey/survey.inc.php
templates/survey/show_root.tpl

index 1051117..70dad6a 100644 (file)
@@ -92,7 +92,7 @@ class SurveyModule extends PLModule
     // }}}
 
     // {{{ function handler_result() : show the results of the votes to a survey
-    function handler_result($page, $id = -1, $qid = 'all')
+    function handler_result($page, $id = -1, $show = 'all')
     {
         $id = intval($id);
         if ($id == -1) {
@@ -108,8 +108,14 @@ class SurveyModule extends PLModule
         if (!$this->check_surveyPerms($page, $survey)) {
             return;
         }
-        $page->assign('survey_resultmode', true);
-        $this->show_survey($page, $survey);
+        if ($show == 'csv') {
+            header('Content-Type: text/csv; charset="UTF-8"');
+            echo $survey->toCSV();
+            exit;
+        } else {
+            $page->assign('survey_resultmode', true);
+            $this->show_survey($page, $survey);
+        }
     }
     // }}}
 
index 9b92360..f1f8fe5 100644 (file)
@@ -98,6 +98,9 @@ class Survey
 
     public function checkPromo($promo)
     {
+        if ($this->promos == '') {
+            return true;
+        }
         $promos = explode('|', $this->promos);
         foreach ($promos as $p) {
             if ((preg_match('#^\d{4}$#', $p) && $p == $promo) ||
@@ -174,6 +177,70 @@ class Survey
     }
     // }}}
 
+    // {{{ function toCSV() : builds a CSV file containing all the results of the survey
+    public function toCSV($sep = ',', $enc = '"', $asep='|')
+    {
+        $nbq = count($this->questions);
+        //require_once dirname(__FILE__) . '/../../classes/varstream.php';
+        VarStream::init();
+        global $csv_output;
+        $csv_output = '';
+        $csv = fopen('var://csv_output', 'w');
+        $line = ($this->isMode(self::MODE_XIDENT))? array('id', 'Nom', 'Prenom', 'Promo') : array('id');
+        for ($qid = 0; $qid < $nbq; $qid++) {
+            $line[] = $this->questions[$qid]->getCSVColumn(); // the fist line contains the questions
+        }
+        $users = array();
+        if ($this->isMode(self::MODE_XIDENT)) { // if the mode is non anonymous
+            $sql = 'SELECT v.id AS vid, a.nom, a.prenom, a.promo
+                      FROM survey_votes AS v
+                INNER JOIN auth_user_md5 AS a
+                        ON a.user_id=v.user_id
+                     WHERE v.survey_id={?}
+                  ORDER BY vid ASC;';
+            $res = XDB::iterator($sql, $this->id); // retrieves all users data
+            for ($u = $res->next(); $u != null; $u = $res->next()) {
+                $users[$u['vid']] = array('nom' => $u['nom'], 'prenom' => $u['prenom'], 'promo' => $u['promo']);
+            }
+        }
+        $sql = 'SELECT v.id AS vid, a.question_id AS qid, a.answer
+                  FROM survey_votes AS v
+             LEFT JOIN survey_answers AS a
+                    ON a.vote_id=v.id
+                 WHERE v.survey_id={?}
+              ORDER BY vid ASC, qid ASC;';
+        $res = XDB::iterator($sql, $this->id); // retrieves all answers from database
+        $cur = $res->next();
+        $vid = -1;
+        $vid_ = 0;
+        $first = ($this->isMode(self::MODE_XIDENT))? 4 : 1;
+        while ($cur != null) {
+            if ($vid != $cur['vid']) { // if the vote id changes, then starts a new line
+                fputcsv($csv, $line, $sep, $enc); // stores the former line into $csv_output
+                $vid = $cur['vid'];
+                $line = array_fill(0, $first + $nbq, ''); // creates an array full of empty string
+                $line[0] = $vid_; // the first field is a 'clean' vote id (not the one stored in database)
+                if ($this->isMode(self::MODE_XIDENT)) { // if the mode is non anonymous
+                    if (array_key_exists($vid, $users) && is_array($users[$vid])) { // and if the user data can be found
+                        $line[1] = $users[$vid]['nom']; // adds the user data (in the first fields of the line)
+                        $line[2] = $users[$vid]['prenom'];
+                        $line[3] = $users[$vid]['promo'];
+                    }
+                }
+                $vid_++;
+            }
+            $fid = $first + $cur['qid']; // computes the field id
+            if ($line[$fid] != '') {  // if this field already contains something
+                $line[$fid] .= $asep; // then adds a separator before adding the new answer
+            }
+            $line[$fid] .= $cur['answer']; // adds the current answer to the correct field
+            $cur = $res->next(); // gets next answer
+        }
+        fputcsv($csv, $line, $sep, $enc); // stores the last line into $csv_output
+        return $csv_output;
+    }
+    // }}}
+
     // {{{ function factory($type, $args) : builds a question according to the given type
     public function factory($t, $args)
     {
@@ -461,11 +528,16 @@ abstract class SurveyQuestion
     }
     // }}}
 
-    // {{{ function buildResultRequest() : statistics on the results of the survey
+    // {{{ functions regarding the results of a survey
     public function buildResultRequest()
     {
         return '';
     }
+
+    public function getCSVColumn()
+    {
+        return $this->question;
+    }
     // }}}
 }
 // }}}
@@ -557,6 +629,20 @@ abstract class SurveyList extends SurveyQuestion
               GROUP BY answer ASC';
         return $sql;
     }
+
+    public function getCSVColumn()
+    {
+        $r = parent::getCSVColumn();
+        if (empty($this->choices)) {
+            return $r;
+        }
+        $r .= ' [0 => '.$this->choices[0];
+        for ($k = 1; $k < count($this->choices); $k++) {
+            $r .= ', '.$k.' => '.$this->choices[$k];
+        }
+        $r .= ']';
+        return $r;
+    }
 }
 // }}}
 
index 32e9357..8945444 100644 (file)
@@ -48,6 +48,7 @@
 {/if}
 {if $survey_resultmode}
   <br/>{$survey.votes} personnes ont r&#233;pondu &#224; ce sondage.
+  <br/>R&#233;cup&#233;rer <a href="./survey/result/{$survey.id}/csv">l'ensemble des r&#233;sultats</a> au format csv
 {/if}
 <br/>
 {if $survey_editmode}