Browse files

Preliminary results display.

  • Loading branch information...
1 parent 5dabbcd commit 8495e5d3a03d5e1b6b3284210f26ff1bc3e4e48f @technomancy committed Jan 22, 2012
Showing with 43 additions and 1 deletion.
  1. +39 −1 src/lein_survey/results.clj
  2. +4 −0 src/lein_survey/web.clj
View
40 src/lein_survey/results.clj
@@ -25,4 +25,42 @@
(defn total []
(clojure.java.jdbc/with-connection (System/getenv "DATABASE_URL")
(clojure.java.jdbc/with-query-results res ["select count(*) from answers"]
- (println res))))
+ (println res))))
+
+(defmulti summarize-question (fn [answers question] (second question)))
+
+(defmethod summarize-question :radio [answers [q _ choices]]
+ (let [freqs (frequencies (for [r results] (get r q)))]
+ [:div.answer
+ [:h4.question q]
+ [:dl (apply concat (for [choice choices]
+ [[:dt choice] [:dd (freqs choice)]]))]]))
+
+(defmethod summarize-question :check [answers [q _ choices]]
+ (let [answers (apply concat (for [r results] (setize (get r q))))
+ freqs (frequencies answers)]
+ [:div.answer
+ [:h4.question q]
+ [:dl (apply concat (for [choice choices]
+ [[:dt choice] [:dd (freqs choice)]]))]]))
+
+(defmethod summarize-question :textarea [answers [q _ choices]])
+
+(defmethod summarize-question :rank [answers [q _ choices]]
+ (let [freqs #(sort-by (comp first key)
+ (frequencies (for [r results]
+ (setize (get r (str q " " %))))))]
+ [:div.answer
+ [:h4.question q]
+ [:ul (for [choice choices]
+ [:li choice
+ (for [[rank count] (freqs choice)
+ :when (not= rank #{nil})]
+ (str " | " (first rank) " - " count))])]]))
+
+
+
+(defn summary []
+ (let [results (get-results)]
+ (into [:div.summary]
+ (map (partial summarize-question results) q/questions))))
View
4 src/lein_survey/web.clj
@@ -31,6 +31,10 @@
{:status 200
:headers {"Content-type" "application/x-clojure"}
:body (results/results-str)}
+ (= "/results" (:uri req))
+ {:status 200
+ :headers {"Content-type" "text/html"}
+ :body (render/layout (results/summary))}
:else
{:status 200
:headers {"Content-type" "text/html"}

0 comments on commit 8495e5d

Please sign in to comment.