 ;;; Dump raw
 ;;;
 
-(defn- extract-answers [value]
+(defn- extract-answers [survey-questions value]
   (if value
-    (reduce (fn [acc [k v]] (merge acc {(name k) (:answer v)})) {} value)
+    (reduce (fn [acc [k v]]
+              (let [{:keys [question answers]} (get survey-questions k)]
+                (merge acc {question
+                            (get-in answers [(str (:answerId v)) :answer])})))
+            {}
+            value)
     ""))
 
 (def sample-base-headers [:plotid

                                                                     (update :analysis_duration #(when % (str % " secs"))))
                                                                 (prefix-keys "pl_" extra-plot-info)
                                                                 (prefix-keys "smpl_" extra-sample-info)
                                                                 (extract-answers saved-answers))
                                                          text-headers
                                                          ""))))
                                  samples)]


(defn- get-value-distribution
  "Count the answers given, and return a map of {'question:answers' count}"
  [survey-questions samples]
  (count-answer (count samples)
                (mapcat (fn [sample]
                          (map (fn [[question-id {:keys [answerId]}]]
                                 (let [{:keys [question answers]} (get survey-questions question-id)]
                                   (str question ":" (get-in answers [(str answerId) :answer]))))
                               (:saved_answers sample)))
                        samples)))

