Commit c49167bb authored by Vlad Dumitru's avatar Vlad Dumitru
Browse files

move questions and stuff into JSON files

parent d5e6734a
{
"title": "Wir hören nicht alle alles gleich!",
"image": "Ohr.jpeg",
"questions": [
{
"type": "multiple-choice",
"text": "Hörst du „DA“ oder „BA“?",
"choices": [ "DA", "BA" ],
"media": {
"type": "video",
"file": "mcgurk.mp4"
}
},
{
"type": "multiple-choice",
"text": "Hörst du „DA“ oder „GA“?",
"choices": [ "DA", "GA" ],
"media": {
"type": "audio",
"file": "BADAGA_17_GADA_b.wav"
}
},
{
"type": "multiple-choice",
"text": "Hörst du „DA“ oder „BA“?",
"choices": [ "DA", "BA" ],
"media": {
"type": "audio",
"file": "BADAGA_06_DABA_a.wav"
}
},
{
"type": "multiple-choice",
"text": "Hörst du „DA“ oder „GA“?",
"choices": [ "DA", "GA" ],
"media": {
"type": "audio",
"file": "BADAGA_08_DAGA_b.wav"
}
},
{
"type": "multiple-choice",
"text": "Hörst du „GA“ oder „DA“?",
"choices": [ "GA", "DA" ],
"media": {
"type": "audio",
"file": "BADAGA_17_GADA_b_AN5sEHB.wav"
}
}
]
}
{
"title": "Wir hören Dinge die nicht gesagt wurden!",
"image": "headphones.jpg",
"questions": [
{
"type": "free-form",
"text": "'In diesem Reim fehlt ein Wort. Welches Wort ist es?",
"media": {
"type": "audio",
"file": "Stefan_Katze.wav"
}
},
{
"type": "free-form",
"text": "'In diesem Reim fehlt ein Wort. Welches Wort ist es?",
"media": {
"type": "audio",
"file": "Eric_Messer.wav"
}
},
{
"type": "free-form",
"text": "'In diesem Reim fehlt ein Wort. Welches Wort ist es?",
"media": {
"type": "audio",
"file": "Saskia_Backe.wav"
}
},
{
"type": "free-form",
"text": "'In diesem Reim fehlt ein Wort. Welches Wort ist es?",
"media": {
"type": "audio",
"file": "Stefan_Bayern.wav"
}
},
{
"type": "free-form",
"text": "'In diesem Reim fehlt ein Wort. Welches Wort ist es?",
"media": {
"type": "audio",
"file": "Eric_Hocker.wav"
}
}
]
}
{
"title": "Was denkst du, wie fühlen sie sich?",
"image": "luisa.jpg",
"questions": [
{
"type": "multiple-choice",
"image": "lucas.jpg",
"text": "Wie fühlt sich der Sprecher?",
"choices": [ "traurig", "wütend", "froh" ],
"media": {
"type": "audio",
"file": "Eric_Schwimmen.wav"
}
},
{
"type": "multiple-choice",
"image": "luisa.jpg",
"text": "Wie fühlt sich die Sprecherin?",
"choices": [ "traurig", "wütend", "froh" ],
"media": {
"type": "audio",
"file": "Saskia_Helfen.wav"
}
},
{
"type": "multiple-choice",
"image": "lucas.jpg",
"text": "Wie fühlt sich der Sprecher?",
"choices": [ "traurig", "wütend", "froh" ],
"media": {
"type": "audio",
"file": "Eric_Eis.wav"
}
},
{
"type": "multiple-choice",
"image": "luisa.jpg",
"text": "Wie fühlt sich die Sprecherin?",
"choices": [ "traurig", "wütend", "froh" ],
"media": {
"type": "audio",
"file": "Saskia_Bus.wav"
}
},
{
"type": "multiple-choice",
"image": "lucas.jpg",
"text": "Wie fühlt sich der Sprecher?",
"choices": [ "traurig", "wütend", "froh" ],
"media": {
"type": "audio",
"file": "Stefan_Schreiben.wav"
}
}
]
}
{
"title": "McGurk: Unsere Augen hören mit!",
"image": "cucumber.jpg",
"questions": [
{
"type": "multiple-choice",
"image": "cucumber.jpg",
"text": "Hör Genau Hin, mache dabei die Augen zu! Was hörst du?",
"choices": [ "DADA", "DATA", "TADA", "BABA" ],
"media": {
"type": "video",
"file": "mcgurk.mp4"
}
},
{
"type": "multiple-choice",
"image": "cucumber.jpg",
"text": "Hör Genau Hin, und schaue den Jungen dabei gut zu! Was hörst du?",
"choices": [ "DADA", "DATA", "TADA", "BABA" ],
"media": {
"type": "video",
"file": "mcgurk.mp4"
}
}
]
}
[
"01-wir-hoeren-nicht-alle-alles-gleich.json",
"02-wir-hoeren-dinge-die-nicht-gesagt-wurden.json",
"03-was-denkst-du-wie-fuehlen-sie-sich.json",
"04-mcgurk-unsere-augen-hoeren-mit.json"
]
......@@ -5,13 +5,17 @@
(def spec
{:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname "db.sqlite3"})
:subname ":memory:"})
(declare db)
(defn on-start []
(let [conn (jdbc/get-connection spec)]
(assoc spec :connection conn)))
(assoc spec :connection conn)
(jdbc/execute! spec
"create table answer (id integer primary key autoincrement, quiz integer, question integer, answer text)")
(jdbc/execute! spec
"create table recording (id integer primary key autoincrement, description text, data blob)")))
(defn on-stop []
(-> db :connection .close)
......@@ -23,42 +27,9 @@
:start (on-start)
:stop (on-stop))
(defn image [id]
(->> ["select * from image where id = ?" id]
(jdbc/query db)
first))
(defn- hydrate-quiz [quiz]
(let [img (image (:image_id quiz))]
(-> quiz (dissoc :image_id) (assoc :image img))))
(defn quizzes []
(->> ["select * from quiz"]
(jdbc/query db)
(map hydrate-quiz)))
(defn quiz [id]
(->> ["select * from quiz where id = ?" id]
(jdbc/query db)
first))
(defn- hydrate-question [question]
(let [image (image (:image_id question))]
(-> question (dissoc :image_id) (assoc :image image))))
(defn questions [quiz-id]
(map hydrate-question (jdbc/query db ["select * from question where quiz_id = ?" quiz-id])))
(defn question [id]
(->> ["select * from question where id = ?" id]
(jdbc/query db)
first))
(defn choices-for [question-id]
(jdbc/query db ["select * from choice where question_id = ?" question-id]))
(defn choice [id]
(first (jdbc/query db ["select * from choice where id = ?" id])))
(defn answer! [question-id choice]
(jdbc/insert! db :answer {:question_id question-id
......@@ -78,8 +49,3 @@
(jdbc/query db)
first))
(defn media [id]
(->> ["select * from media where id = ?" id]
(jdbc/query db)
first))
......@@ -8,7 +8,7 @@
[tunein.middleware :as middleware]
[tunein.layout :refer [error-page]]
[tunein.routes.home :refer [home-routes]]
[tunein.routes.quiz :refer [quiz-routes]]
[tunein.routes.quiz :refer [routes]]
[tunein.routes.record :refer [record-routes]]
[tunein.db.core :as db]))
......@@ -17,8 +17,7 @@
:stop ((or (:stop defaults) (fn []))))
(defn all-quiz-routes []
(let [quizzes (db/quizzes)]
["/quiz" (vec (map quiz-routes quizzes))]))
["/quiz" routes])
(mount/defstate app-routes
:start
......
(ns tunein.routes.home
(:require
[clojure.data.json :as json]
[clojure.java.io :as io]
[ring.util.response]
[ring.util.http-response :as response]
......@@ -7,9 +8,19 @@
[tunein.middleware :as middleware]
[tunein.db.core :as db]))
(defn- read-json-resource [path]
(let [res (io/resource path)
content (slurp res)]
(json/read-str content :key-fn keyword)))
(def quizzes
(let [listing (read-json-resource "quizzes/listing.json")
read (fn [filename] (read-json-resource (str "quizzes/" filename)))]
(into [] (map read listing))))
(defn home-page [request]
(layout/render request "things.html"
{:quizzes (db/quizzes)
{:quizzes quizzes
:title "Stationen"}))
(defn about-page [request]
......
......@@ -9,69 +9,79 @@
[tunein.middleware :as middleware]
[tunein.db.core :as db]))
(defn- list-questions [quiz]
(defn- read-json-resource [path]
(let [res (io/resource path)
content (slurp res)]
(json/read-str content :key-fn keyword)))
(def quizzes
(let [listing (read-json-resource "quizzes/listing.json")
read (fn [filename] (read-json-resource (str "quizzes/" filename)))]
(into [] (map read listing))))
(defn- render-question-list [quiz]
(fn [request]
(let [questions (db/questions (:id quiz))]
(layout/render request "quiz/question-list.html"
(assoc quiz :questions questions)))))
(layout/render request "quiz/question-list.html" quiz)))
(defn- ask-question [quiz]
(defn- render-question [quiz question]
(fn [request]
(let [id (->> request :path-params :id)
question (db/question id)
choices (db/choices-for id)
media (db/media (:media_id question))]
(layout/render request "quiz/question.html"
(assoc quiz
:question question
:choices choices
:media media)))))
{:title (:title quiz)
:question question})))
(defn- answer-multiple-choice-question [quiz question answer]
(let [choices (db/choices-for (:id question))
(let [choices (:choices question)
value (try (Integer/parseInt answer)
(catch NumberFormatException _ nil))]
(if-let [choice (first (filter #(= value (:id %)) choices))]
(do
(db/answer! (:id question) (:text choice))
{:status 301
(db/answer! (hash question) (:text choice))
{:status 301
:headers {"Location"
(str "/quiz/" (:id quiz) "/results/" (:id question))}})
{:status 500
(str "/quiz/" (hash quiz) "/results/" (hash question))}})
{:status 500
:headers {"Content-Type" "text/plain"}
:body "unacceptable answer"})))
:body "unacceptable answer"})))
(defn- answer-free-form-question [quiz question answer]
(db/answer! (:id question) answer)
(db/answer! (hash question) answer)
{:status 301
:headers {"Location"
(str "/quiz/" (:id quiz) "/results/" (:id question))}})
(str "/quiz/" (hash quiz) "/results/" (hash question))}})
(defn- answer-question [quiz]
(defn- answer-question [quiz question]
(fn [request]
(let [id (->> request :path-params :id)
question (db/question id)
answer (-> request :form-params (get "choice"))]
(case (:kind question)
(let [answer (-> request :form-params (get "choice"))]
(case (:type question)
"multiple-choice" (answer-multiple-choice-question quiz question answer)
"free-form" (answer-free-form-question quiz question answer)))))
"free-form" (answer-free-form-question quiz question answer)))))
(defn- show-results [quiz]
(defn- render-question-results [quiz question]
(fn [request]
(let [id (->> request :path-params :id)
question (db/question id)
answers (db/answers-for id)
results (->> (map :choice answers) frequencies)
results-as-json (json/write-str results)]
(let [answers (db/answers-for (hash question))
results (->> (map :choice answers) frequencies)]
(layout/render request "quiz/results.html"
(assoc quiz
:question question
:results results)))))
{:title (:title quiz)
:question (:text question)
:results results}))))
(defn- question-routes [quiz question]
(let [base-path (str "/question/" (hash question))]
[[base-path
{:get (render-question quiz question)
:post (answer-question quiz question)}]
[(str base-path "/results")
{:get render-question-results quiz question}]]))
(defn quiz-routes [quiz]
[(str "/" (:id quiz))
["" {:get (list-questions quiz)}]
["/question/:id" {:get (ask-question quiz)
:post (answer-question quiz)}]
["/results/:id" {:get (show-results quiz)}]])
(concat
[(str "/" (hash quiz)) ["" {:get (render-question-list quiz)}]]
(map (fn [question] (question-routes quiz question)) (:questions quiz))))
(defn routes []
(into [] (map quiz-routes quizzes)))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment