aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYingtong Li <runassudo@yingtongli.me>2017-03-23 21:35:14 +1100
committerYingtong Li <runassudo@yingtongli.me>2017-03-23 21:35:14 +1100
commit585aa006af66382b6426a879e44fae302e9e242b (patch)
tree6d2771e80e0c4b3db6a10e7841f3093fa911aafd
parent57faf03fa919fcaf85550c7bdf9ff3d9fab1f212 (diff)
Implement short answer questions and make minor bug fixes to admin
display
-rw-r--r--.gitignore1
-rw-r--r--pblive/__init__.py9
-rw-r--r--pblive/data.py17
-rw-r--r--pblive/templates/question_admin.html3
-rw-r--r--pblive/templates/question_type.html36
-rw-r--r--pblive/templates/question_type_admin.html34
6 files changed, 96 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index f45850b..c8e795b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
/data
/venv
__pycache__
+.#*
diff --git a/pblive/__init__.py b/pblive/__init__.py
index 7e93c70..911cbf8 100644
--- a/pblive/__init__.py
+++ b/pblive/__init__.py
@@ -31,7 +31,7 @@ socketio = flask_socketio.SocketIO(app)
# Load session data
for f in os.listdir('data'):
- if f.endswith('.yaml'):
+ if f.endswith('.yaml') and not f.startswith('.'):
session_name = f[:-5]
with open(os.path.join('data', f)) as fh:
pblive.data.sessions[session_name] = data.Session.from_dict(yaml.load(fh), session_name)
@@ -102,7 +102,7 @@ def socket_disconnect():
# Relay change
for _, other_user in pblive.data.users.items():
- if other_user != user and 'colour' not in other_user and other_user.session == user.session:
+ if other_user != user and not other_user.colour and other_user.session == user.session:
flask_socketio.emit('update', flask.render_template('colour_picker.html', session=user.session), room=other_user.sid)
del pblive.data.users[flask.request.sid]
@@ -134,7 +134,8 @@ def socket_answer(question_num, answer):
# Relay change
for _, admin in pblive.data.admins.items():
- flask_socketio.emit('update', render_question_admin(user.session, user.session.question_num), room=admin.sid)
+ if admin.session == user.session:
+ flask_socketio.emit('update', render_question_admin(user.session, user.session.question_num), room=admin.sid)
@socketio.on('reveal_answers')
def socket_reveal_answers(question_num):
@@ -159,6 +160,7 @@ def socket_goto_question(question_num):
if other_user.session == user.session and other_user.colour:
flask_socketio.emit('update', render_question(other_user, other_user.session, other_user.session.question_num), room=other_user.sid)
for _, admin in pblive.data.admins.items():
+ if admin.session == user.session:
flask_socketio.emit('update', render_question_admin(admin.session, admin.session.question_num), room=admin.sid)
@socketio.on('pass_question')
@@ -174,4 +176,5 @@ def socket_pass_question():
if other_user.session == user.session and other_user.colour:
flask_socketio.emit('update', render_question(other_user, other_user.session, other_user.session.question_num), room=other_user.sid)
for _, admin in pblive.data.admins.items():
+ if admin.session == user.session:
flask_socketio.emit('update', render_question_admin(admin.session, admin.session.question_num), room=admin.sid)
diff --git a/pblive/data.py b/pblive/data.py
index 3c8a1b9..5c90b68 100644
--- a/pblive/data.py
+++ b/pblive/data.py
@@ -50,7 +50,8 @@ class Question:
question_types = {
'mcq': MCQQuestion,
'draw': DrawQuestion,
- 'random': RandomQuestion
+ 'random': RandomQuestion,
+ 'type': TypeQuestion
}
question = question_types[obj['type']]()
question.load_dict(obj)
@@ -79,6 +80,10 @@ class RandomQuestion(Question):
def __init__(self):
self.answerer = None
+class TypeQuestion(Question):
+ template = 'question_type.html'
+ template_admin = 'question_type_admin.html'
+
class User:
def __init__(self, sid=None, session=None, answers=None, colour=None):
if answers is None:
@@ -94,3 +99,13 @@ class Admin(User):
def responses_for_question(session, question_num):
return len([user for _, user in users.items() if user.session == session and question_num in user.answers])
+
+def unique_answers_for_question(session, question_num):
+ answers = {}
+ for _, user in users.items():
+ if user.session == session and question_num in user.answers and user.answers[question_num] != '' and user.answers[question_num] != None:
+ if user.answers[question_num] in answers:
+ answers[user.answers[question_num]].append(user)
+ else:
+ answers[user.answers[question_num]] = [user]
+ return answers
diff --git a/pblive/templates/question_admin.html b/pblive/templates/question_admin.html
index 4367df7..17d14e7 100644
--- a/pblive/templates/question_admin.html
+++ b/pblive/templates/question_admin.html
@@ -37,6 +37,9 @@
<button class="ui primary button" onclick="socket.emit('reveal_answers', {{ question_num }});">Reveal responses</button>
{% endif %}
{% endblock %}
+ {% if question_num > 0 %}
+ <button class="ui button" onclick="socket.emit('goto_question', {{ question_num - 1 }});">Previous question</button>
+ {% endif %}
{% if question_num < session.questions|length - 1 %}
<button class="ui button" onclick="socket.emit('goto_question', {{ question_num + 1 }});">Next question</button>
{% endif %}
diff --git a/pblive/templates/question_type.html b/pblive/templates/question_type.html
new file mode 100644
index 0000000..73b4ad9
--- /dev/null
+++ b/pblive/templates/question_type.html
@@ -0,0 +1,36 @@
+{#
+ PBLive
+ Copyright © 2017 RunasSudo (Yingtong Li)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+#}
+
+{% extends 'question.html' %}
+
+{% block base %}
+ <div class="ui input">
+ <input type="text" id="answer" name="answer" value="{{ user.answers[question_num] }}">
+ </div>
+
+ <script>
+ var submitAnswerTimer = 0;
+ function submitAnswer() {
+ socket.emit('answer', {{ question_num }}, $('#answer').val());
+ }
+ $('#answer').change(function() {
+ window.clearTimeout(submitAnswerTimer);
+ submitAnswerTimer = window.setTimeout(submitAnswer, 500);
+ });
+ </script>
+{% endblock %}
diff --git a/pblive/templates/question_type_admin.html b/pblive/templates/question_type_admin.html
new file mode 100644
index 0000000..6788f71
--- /dev/null
+++ b/pblive/templates/question_type_admin.html
@@ -0,0 +1,34 @@
+{#
+ PBLive
+ Copyright © 2017 RunasSudo (Yingtong Li)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+#}
+
+{% extends 'question_admin.html' %}
+
+{% block base %}
+ {% if session.questions[question_num].revealed %}
+ <ul class="ui list">
+ {% for answer, users in data.unique_answers_for_question(session, question_num).items() %}
+ <li style="line-height: 32px;">
+ {{ answer }}:
+ {% for user in users %}
+ <button class="ui button" style="background-color: {{ user.colour[1] }}; width: 32px; height: 32px; padding: 0 0;">{{ user.colour[0] }}</button>
+ {% endfor %}
+ </li>
+ {% endfor %}
+ </ul>
+ {% endif %}
+{% endblock base %}
Contact (issues, pull requests, etc.) at git@yingtongli.me. Generated by cgit.