Skip to content

Commit 2ff18f4

Browse files
committed
social + indicators
1 parent 47d2261 commit 2ff18f4

7 files changed

+142
-28
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ First, create a quiz text file using the following format (named `unicorns.quiz`
1313
// @bsouthga
1414
// <- (this is a comment and will be ignored)
1515
16+
// this is the url for the parent
17+
url: http://urbaninstitute.github.io/quick-quiz/
18+
1619
// this is the title of the quiz
1720
# How well do you know real creatures?
1821
@@ -75,7 +78,8 @@ This produces a formatted json file like this...
7578
},
7679
{
7780
"answers": [
78-
"True"
81+
"True",
82+
"False"
7983
],
8084
"correct": {
8185
"index": 0
@@ -97,7 +101,8 @@ This produces a formatted json file like this...
97101
"prompt": "What shade of white is this unicorn?"
98102
}
99103
],
100-
"title": "How well do you know real creatures?"
104+
"title": "How well do you know real creatures?",
105+
"url": "http://urbaninstitute.github.io/quick-quiz/"
101106
}
102107
```
103108

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
});
3030
</script>
3131
</body>
32-
</html>
32+
</html>

quiz.css

+43-8
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ html, body {
3838
width: 100%;
3939
}
4040

41-
.question-image img {
41+
.question-image img,
42+
.results-ratio,
43+
.results-social,
44+
.quiz-title
45+
{
4246
max-width: 600px;
4347
display: block;
4448
margin: 0 auto;
49+
text-align: center;
50+
}
51+
52+
.question-image img {
4553
margin-top: 20px;
4654
}
4755

@@ -51,21 +59,48 @@ html, body {
5159
}
5260

5361
.quiz-title {
54-
display: block;
55-
max-width: 600px;
5662
font-size: 60px;
5763
font-weight: 300;
58-
text-align: center;
59-
margin: 0 auto;
6064
margin-top: 100px;
6165
margin-bottom: 30px;
6266
}
6367

6468
.results-ratio {
65-
display: block;
66-
max-width: 600px;
6769
margin: 20px auto;
6870
font-size: 24px;
6971
font-weight: bold;
70-
text-align: center;
72+
}
73+
74+
.results-social {
75+
margin-top: 30px;
76+
}
77+
78+
.results-social a:not(:first-child) {
79+
margin-left: 20px;
80+
}
81+
82+
.progress-circles {
83+
position: absolute;
84+
padding: 0;
85+
margin: 0;
86+
left: 40px;
87+
top: 40px;
88+
display: none;
89+
}
90+
91+
.progress-circles.show {
92+
display: block;
93+
}
94+
95+
.progress-circles li{
96+
list-style: none;
97+
display: inline-block;
98+
background: #ccc;
99+
width: 15px;
100+
height: 15px;
101+
border-radius: 50%;
102+
margin-right: 10px;
103+
}
104+
.progress-circles li.dark{
105+
background: #6b6b6b;
71106
}

quiz.js

+73-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ $.fn.quiz = function(filename) {
2222

2323
// create html structure for quiz
2424
// using loaded questions json
25-
function render(opts) {
25+
function render(quiz_opts) {
2626

2727
// list of questions to insert into quiz
28-
var questions = opts.questions;
28+
var questions = quiz_opts.questions;
2929

3030
// keep track of the state of correct
3131
// answers to the quiz so far
@@ -45,12 +45,28 @@ function render(opts) {
4545

4646
var height = $quiz.height();
4747

48+
49+
/*
50+
Add carousel indicators
51+
*/
52+
var $indicators = $('<ol>')
53+
.attr('class', 'progress-circles')
54+
.appendTo($quiz);
55+
56+
$.each(questions, function(question_index, question) {
57+
$('<li>')
58+
.attr('class', question_index ? "" : "dark")
59+
.appendTo($indicators);
60+
});
61+
62+
/*
63+
Slides container div
64+
*/
4865
var $slides = $("<div>")
4966
.attr("class", "carousel-inner")
5067
.attr("role", "listbox")
5168
.appendTo($quiz);
5269

53-
5470
/*
5571
Create title slide
5672
*/
@@ -60,7 +76,7 @@ function render(opts) {
6076
.appendTo($slides);
6177

6278
$('<h1>')
63-
.text(opts.title)
79+
.text(quiz_opts.title)
6480
.attr('class', 'quiz-title')
6581
.appendTo($title_slide);
6682

@@ -73,6 +89,7 @@ function render(opts) {
7389
.text("Take the quiz!")
7490
.click(function() {
7591
$quiz.carousel('next');
92+
$indicators.addClass('show');
7693
})
7794
.appendTo($start_button);
7895

@@ -172,6 +189,9 @@ function render(opts) {
172189
// keep track in total
173190
correct && state.correct++;
174191
$quiz.carousel('next');
192+
193+
194+
175195
// if we've reached the final question
176196
// set the results text
177197
if (last_question) {
@@ -181,6 +201,20 @@ function render(opts) {
181201
Math.round(100*(state.correct/state.total)) +
182202
"% of the questions correct!"
183203
);
204+
$twitter_link.attr('href', tweet(state, quiz_opts));
205+
$facebook_link.attr('href', facebook(state, quiz_opts));
206+
$indicators.removeClass('show');
207+
// indicate the question number
208+
$indicators.find('li')
209+
.removeClass('dark')
210+
.eq(0)
211+
.addClass('dark');
212+
} else {
213+
// indicate the question number
214+
$indicators.find('li')
215+
.removeClass('dark')
216+
.eq(question_index+1)
217+
.addClass('dark');
184218
}
185219
});
186220
});
@@ -208,6 +242,18 @@ function render(opts) {
208242
.attr("class", "quiz-answers")
209243
.appendTo($results_slide);
210244

245+
var $social = $("<div>")
246+
.attr('class', 'results-social')
247+
.appendTo($results_slide);
248+
249+
var $twitter_link = $('<a>')
250+
.html('<span class="social social-twitter">tweet</span>')
251+
.appendTo($social);
252+
253+
var $facebook_link = $('<a>')
254+
.html('<span class="social social-facebook">share</span>')
255+
.appendTo($social);
256+
211257
$("<button>")
212258
.attr('class', 'quiz-button btn')
213259
.text("Try again?")
@@ -258,4 +304,26 @@ function resultsText(state) {
258304
}
259305

260306

261-
})(jQuery);
307+
function tweet(state, opts) {
308+
309+
var body = (
310+
"I got " + state.correct +
311+
" out of " + state.total +
312+
" on @urbaninstitute's \"" + opts.title +
313+
"\" quiz. Test your knowledge here: " + opts.url
314+
);
315+
316+
return (
317+
"http://twitter.com/intent/tweet?text=" +
318+
encodeURIComponent(body)
319+
);
320+
321+
}
322+
323+
function facebook(state, opts) {
324+
return "https://www.facebook.com/sharer/sharer.php?u=" + opts.url;
325+
}
326+
327+
328+
})(jQuery);
329+

quiz_questions.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
import sys
55
import json
66

7-
#
8-
# get title of quiz
9-
#
10-
title_rx = re.compile(r'\s+^#(.+)', re.M)
11-
127

138
#
149
# find all questions and metadata
@@ -42,23 +37,30 @@
4237
""", re.X | re.M)
4338

4439

45-
4640
def toJson(filename):
4741
with open(filename, 'r') as quiz_file:
48-
# remove comments from quiz file
49-
quiz_text = re.sub(r'//(.+)', "", quiz_file.read())
42+
quiz_text = quiz_file.read()
43+
44+
# find the parent url before comments are removed
45+
url = re.search(r'^url:(.+)', quiz_text, re.M)
46+
if url:
47+
url = url.group(1).strip()
5048

5149
# find title for quiz
52-
title = title_rx.match(quiz_text)
50+
title = re.search(r'^#(.+)', quiz_text, re.M)
5351
if title:
5452
title = title.group(1).strip()
5553

54+
# remove comments from quiz file
55+
quiz_text = re.sub(r'//(.+)', "", quiz_text)
56+
5657
# find all questions
5758
questions = [m.groupdict() for m in q_rx.finditer(quiz_text)]
5859

5960
results = []
6061

6162
for q in questions:
63+
6264
out = {}
6365

6466
out['prompt'] = q['prompt'].strip()
@@ -87,7 +89,8 @@ def toJson(filename):
8789
json.dump(
8890
{
8991
"questions" : sorted(results, key=lambda x: x['number']),
90-
"title" : title or ""
92+
"title" : title,
93+
"url" : url
9194
},
9295
outfile,
9396
sort_keys=True,
@@ -100,7 +103,6 @@ def toJson(filename):
100103
if __name__ == '__main__':
101104
if len(sys.argv) == 1:
102105
print("no quiz file given to script...")
103-
toJson("unicorns.quiz")
104106
else:
105107
quiz_file = sys.argv[1]
106108
print("generating {}.json".format(quiz_file.split('.')[0]))

unicorns.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@
3939
"prompt": "What shade of white is this unicorn?"
4040
}
4141
],
42-
"title": "How well do you know real creatures?"
42+
"title": "How well do you know real creatures?",
43+
"url": "http://urbaninstitute.github.io/quick-quiz/"
4344
}

unicorns.quiz

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// @bsouthga
33
// <- (this is a comment and will be ignored)
44

5+
// this is the url for the parent
6+
url: http://urbaninstitute.github.io/quick-quiz/
7+
58
// this is the title of the quiz
69
# How well do you know real creatures?
710

0 commit comments

Comments
 (0)