summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKristina Hänninen <khannine@local>2023-11-21 12:52:55 +0200
committerKristina Hänninen <khannine@local>2023-11-21 12:52:55 +0200
commit4c5fd0076b4903320ba470ebf0cd454e5f7a2995 (patch)
tree8765cb09df85d08b4a6207e0dc959901fddcc4b2
parentb868d0ba58384fce5a9a45ae153dee9815327d55 (diff)
Initial anwsering form and related
-rw-r--r--app.py1
-rw-r--r--db_actions.py11
-rw-r--r--routes/set/answers.py32
-rw-r--r--static/answer.js68
-rw-r--r--static/create.js1
-rw-r--r--templates/answer.html4
6 files changed, 115 insertions, 2 deletions
diff --git a/app.py b/app.py
index 996be9b..b01f013 100644
--- a/app.py
+++ b/app.py
@@ -11,4 +11,5 @@ import routes.base
import routes.set.nick
import routes.set.quiz
import routes.set.question
+import routes.set.answers
import routes.get.quiz
diff --git a/db_actions.py b/db_actions.py
index b604870..5a76b00 100644
--- a/db_actions.py
+++ b/db_actions.py
@@ -85,4 +85,15 @@ def get_questions(quiz_id):
WHERE a.question_id = q.id AND quiz.id = (:quiz_id);"
return db.session.execute( text(sql), { "quiz_id":quiz_id } ).fetchall()
+
+def get_user_answer(user_id, question_id):
+ sql = "SELECT answer \
+ FROM answers \
+ WHERE question_id = (:question_id) AND user_id = (:user_id);"
+ result = db.session.execute( text(sql), {
+ 'question_id': question_id,
+ 'user_id': user_id
+ } ).fetchone()
+ return result[0] if result else result
+
\ No newline at end of file
diff --git a/routes/set/answers.py b/routes/set/answers.py
new file mode 100644
index 0000000..859d65c
--- /dev/null
+++ b/routes/set/answers.py
@@ -0,0 +1,32 @@
+from app import app
+from flask import render_template, session, request, redirect
+import db_actions as D
+
+def validate_answer(ans):
+ if len(ans)<1:
+ return False
+ try:
+ value=int(ans)
+ if value<0 or value>1000:
+ return False
+ except ValueError:
+ return False
+ return True
+
+@app.route("/set/answers",methods=["POST"])
+def set_answers():
+ if "id" not in session.keys():
+ return "KUOLETTAVA: Nimimerkkiä ei ole vielä valittu!"
+ if "quiz_id" not in session.keys():
+ return "KUOLETTAVA: Yrität vastata kyselyyn ilman sen valintaa!"
+
+ user_id = session["id"]
+ for id, answer in request.form.items():
+ question_id = int(id)
+ if not validate_answer(answer):
+ return "KUOLETTAVA: Epäkelpo vastaus!"
+ if D.get_user_answer(user_id,question_id):
+ return "KUOLETTAVA: On jo vastattu!"
+ D.answer_new(user_id, question_id, answer)
+
+ return redirect("/#analyse")
diff --git a/static/answer.js b/static/answer.js
new file mode 100644
index 0000000..61882a8
--- /dev/null
+++ b/static/answer.js
@@ -0,0 +1,68 @@
+var questions = {}
+
+createQuestionDiv = ( question ) => {
+ const questionDiv = document.createElement('div')
+ questionDiv.className = 'kysQuestion'
+
+ const qDiv = document.createElement('div')
+ qDiv.appendChild( document.createTextNode( question.q ) )
+ qDiv.className = 'kysText'
+ questionDiv.appendChild( qDiv )
+
+ const npDiv = document.createElement('div')
+ npDiv.className = 'kysScale'
+
+ const nDiv = document.createElement('div')
+ nDiv.appendChild( document.createTextNode( question.n ) )
+ nDiv.className = 'kysNegative'
+ npDiv.appendChild( nDiv )
+
+ const sDiv = document.createElement('div')
+ sDiv.className = 'kysScaleSpacer'
+ npDiv.appendChild( sDiv )
+
+ const pDiv = document.createElement('div')
+ pDiv.appendChild( document.createTextNode( question.p ) )
+ pDiv.className = 'kysPositive'
+ npDiv.appendChild( pDiv )
+
+ questionDiv.appendChild( npDiv )
+
+ const aInput = document.createElement('input')
+ aInput.className = 'kysAnswer'
+ aInput.type = 'range'
+ aInput.min = 0
+ aInput.max = 999
+ aInput.value = 500
+ aInput.name = question.i
+ questionDiv.appendChild( aInput )
+
+ return questionDiv
+}
+
+createQuestions = () => {
+ const kysForm = document.getElementById('questionForm')
+ const questionsDiv = document.createElement('div')
+ Object.keys(questions).forEach(k => {
+ questionsDiv.appendChild( createQuestionDiv( questions[k] ) )
+ })
+ kysForm.appendChild( questionsDiv )
+ const submitInput = document.createElement('input')
+ submitInput.type='submit'
+ submitInput.value='Vastaa kyselyyn'
+ submitInput.className = 'kysSubmitAnswers'
+ kysForm.appendChild( submitInput )
+}
+
+loadQuestions = async() => {
+ await fetch( 'get/quiz_creator' )
+ .then( response => response.json() )
+ .then( json => questions = json )
+ .catch( error => {
+ alert("dkd")
+ } )
+
+ createQuestions()
+}
+
+loadQuestions()
diff --git a/static/create.js b/static/create.js
index d64073f..d34a2da 100644
--- a/static/create.js
+++ b/static/create.js
@@ -29,7 +29,6 @@ createQuestionDiv = ( question ) => {
questionDiv.appendChild( npDiv )
const aDiv = document.createElement('input')
- aDiv.appendChild( document.createTextNode( question.a ) )
aDiv.className = 'kysAnswer'
aDiv.type = 'range'
aDiv.min = 0
diff --git a/templates/answer.html b/templates/answer.html
index 3870e6b..0ed0a05 100644
--- a/templates/answer.html
+++ b/templates/answer.html
@@ -1,3 +1,5 @@
+<script src="answer.js"></script>
<div id="nickbox">{{ nick }}</div>
<div id="alertbox">{{ alert }}</div>
-<h1>answer</h1>
+<h1>answer NOW!</h1>
+<form id=questionForm action="/set/answers" method="POST"></form>