Skip to content

Commit 5c935a1

Browse files
committed
Quiz feature - prepared code for initial review
1 parent 14685b6 commit 5c935a1

37 files changed

+1001
-11
lines changed

both/collections/quizzes.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Quizzes = new Mongo.Collection('quizzes');

both/model/quizModel.js

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
Quiz = function(){
2+
var quiz = this;
3+
this.addNewQuestion = function(val){
4+
//check if the question already exists
5+
if (quiz.questions == undefined){
6+
quiz.questions = [];
7+
}
8+
for (var q in quiz.questions){
9+
10+
};
11+
12+
quiz.questions.push(val);
13+
};
14+
15+
};
16+
17+
Quiz.convertToQuizObject = function(object){
18+
19+
if (object == undefined) return;
20+
var quiz = new Quiz();
21+
22+
quiz._id = object._id;
23+
quiz.title = object.title;
24+
quiz.lessonID = object.lessonID;
25+
quiz.questions = object.questions;
26+
quiz.userAttempts = object.userAttempts;
27+
28+
return quiz;
29+
};
30+
31+
32+
Object.defineProperty(Quiz, "_title", {
33+
get: function title(){
34+
return this._title;
35+
},
36+
set: function title(val){
37+
this._title = val;
38+
}
39+
});
40+
Object.defineProperty(Quiz, "_questions", {
41+
get: function question(){
42+
return this._questions;
43+
},
44+
set: function questions(val){
45+
this._questions = val;
46+
}
47+
});
48+
49+
Object.defineProperty(Quiz, "_lessonID", {
50+
get: function lessonID(){
51+
return this._lessonIDs
52+
},
53+
set: function lessonID(val){
54+
this._lessonID = val;
55+
}
56+
});
57+
58+
Object.defineProperty(Quiz, "_userAttempts", { //an array of objects containig user ids, date and result
59+
get: function userAttempts(){
60+
return this._userAttempts;
61+
},
62+
set: function userAttempts(val){
63+
this._userAttempts = val;
64+
}
65+
})
66+
67+
68+
69+
70+
QuizOptions = {};
71+
72+
QuizOptions.MULTIPLE_CHOICE_SINGLE_ANSWER = "Multiple Choice - single answer";
73+
QuizOptions.MULTIPLE_CHOICE_MULTIPLE_ANSWERS = "Multiple Choice - multiple answers";
74+
QuizOptions.TRUE_OR_FALSE = "True or False";
75+
QuizOptions.QUESTION_TYPES =
76+
[ QuizOptions.MULTIPLE_CHOICE_SINGLE_ANSWER,
77+
QuizOptions.MULTIPLE_CHOICE_MULTIPLE_ANSWERS,
78+
QuizOptions.TRUE_OR_FALSE
79+
];
80+
81+
82+
83+
QuizQuestion = function(){};
84+
Object.defineProperty(QuizQuestion, "_questionType", {
85+
get: function questionType() {
86+
return this._quiztype;
87+
},
88+
set: function questionType(val) {
89+
this._quiztype = val;
90+
}
91+
});
92+
93+
//array of lesson IDs - the quiz can be used in more than one lesson
94+
Object.defineProperty(QuizQuestion, "_quizId", {
95+
get: function quizId(){
96+
return this._quizId
97+
},
98+
set: function quizId(val){
99+
this._lessonIDs = val;
100+
}
101+
});
102+
103+
Object.defineProperty(QuizQuestion, "_title", {
104+
get: function title(){
105+
return this._title;
106+
},
107+
set: function title(val){
108+
this._title = val;
109+
}
110+
});
111+
112+
Object.defineProperty(QuizQuestion, "_description", {
113+
get: function description(){
114+
return this._description;
115+
},
116+
set: function description(val){
117+
this._description = val;
118+
}
119+
});
120+
121+
Object.defineProperty(QuizQuestion, "_options", {
122+
get: function options(){
123+
return this._options;
124+
},
125+
set: function options(val){
126+
this._questions = val;
127+
}
128+
});
129+
130+
Object.defineProperty(QuizQuestion, "_saved",{
131+
get: function saved(){
132+
return this._saved;
133+
},
134+
set: function saved(val){
135+
this._saved = val;
136+
}
137+
});
138+
139+
Object.defineProperty(QuizQuestion, "_answered", {
140+
get: function answered(){
141+
return this._answered;
142+
},
143+
set: function answered(val){
144+
this._answered = val;
145+
}
146+
})
147+
148+
Object.defineProperty(QuizQuestion, "_isMultipleAnswer", {
149+
get: function isMultipleAnswer(){
150+
return this._questionType == QuizOptions.MULTIPLE_CHOICE_MULTIPLE_ANSWERS;
151+
}
152+
});
153+
154+
Object.defineProperty(QuizQuestion, "_isSingleAnswer", {
155+
get: function isSingleAnswer(){
156+
return this._questionType == QuizOptions.MULTIPLE_CHOICE_SINGLE_ANSWER;
157+
}
158+
});
159+
160+
Object.defineProperty(QuizQuestion, "_isTrueOrFalse", {
161+
get: function isTrueOrfalse(){
162+
return this._questionType == QuizOptions.TRUE_OR_FALSE;
163+
}
164+
})
165+
166+

client/helpers/editMode.js

+17
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,20 @@ Template.registerHelper('editMode', function () {
22
// get edit mode session variable
33
return Session.get('editMode');
44
});
5+
6+
Template.registerHelper('isEditingCurrentCourse', function() {
7+
// Get reference to current router
8+
var router = Router.current();
9+
10+
// Get Course ID from router
11+
var currentCourseId = router.params._id;
12+
13+
// Get value of editing course session variable
14+
var editingCourseId = Session.get('editingCourseId')
15+
16+
// See if user is editing current course
17+
var editingCurrentCourse = (editingCourseId === currentCourseId);
18+
19+
// return true if user is editing this course
20+
return editingCurrentCourse;
21+
});

client/templates/course/course.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Template.course.created = function () {
1313

1414
// Set the empty active lesson ID variable
1515
activeLessonID = new ReactiveVar(undefined);
16+
17+
//Set an ampty active quiz Id var
18+
activeQuizID = new ReactiveVar(undefined);
19+
1620
};
1721

1822
Template.course.helpers({
@@ -24,5 +28,6 @@ Template.course.helpers({
2428
var course = Courses.findOne(instance.courseId);
2529

2630
return course;
27-
}
31+
},
32+
2833
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#addQuizBtn {
2+
margin-left: 12px;
3+
margin-top: 5px;
4+
}
5+
.add-quiz-text {
6+
cursor: pointer;
7+
font-size: 11px;
8+
}
+16-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<template name="lesson">
2-
{{# with activeLesson }}
3-
<div class="col-xs-12 col-sm-7 col-md-9">
4-
<div class="panel panel-default">
5-
<div class="panel-heading">
6-
{{> lessonName name=name activeLesson=activeLesson }}
7-
</div>
8-
<div class="panel-body">
9-
{{> lessonText text=text activeLesson=activeLesson }}
2+
{{# if activeLesson }}
3+
{{# with activeLesson }}
4+
<div class="col-xs-12 col-sm-7 col-md-9">
5+
<div class="panel panel-default">
6+
<div class="panel-heading">
7+
{{> lessonName name=name activeLesson=activeLesson }}
8+
</div>
9+
<div class="panel-body">
10+
{{> lessonText text=text activeLesson=activeLesson }}
11+
</div>
1012
</div>
1113
</div>
12-
</div>
13-
{{/ with }}
14+
{{/ with }}
15+
{{/ if }}
16+
17+
{{# if activeQuiz }}
18+
{{> quizContent activeQuiz=activeQuiz }}
19+
{{/ if }}
1420
</template>

client/templates/course/lesson/lesson.js

+7
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@ Template.lesson.helpers({
77
var lesson = Lessons.findOne({_id: lessonID});
88

99
return lesson;
10+
},
11+
12+
'activeQuiz': function(){
13+
var quizId = activeQuizID.get();
14+
15+
var quiz = Quiz.convertToQuizObject(Quizzes.findOne({ _id: quizId }));
16+
return quiz;
1017
}
1118
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#addQuizBtn{
2+
margin-left: 12px;
3+
margin-top: 5px;
4+
}
5+
6+
.add-quiz-text{
7+
cursor: pointer;
8+
font-size: 11px;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template name="deleteQuizQuestionButton">
2+
<span><i class="fa fa-trash delete-template-button"></i></span>
3+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Template.deleteQuizQuestionButton.events({
2+
'click .delete-template-button': function(event){
3+
var question = Template.currentData().question;
4+
5+
//create a custom event
6+
//use document.createEvent as the CustomEvent is not supported by IE
7+
var deleteQuestionEvent = document.createEvent("HTMLEvents");
8+
deleteQuestionEvent.initEvent("deleteQuestion", true, true);
9+
deleteQuestionEvent.question = question;
10+
11+
//dispatch the event from the target button
12+
var btn = event.target;
13+
btn.dispatchEvent(deleteQuestionEvent);
14+
15+
}
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template name="editableTitleDescription">
2+
{{#if isEditingCurrentCourse}}
3+
<div class="form-group-sm">
4+
<input id="questionTitle"
5+
type="text" class="form-control"
6+
placeholder="Enter question title"
7+
value="{{question.title}}"
8+
/>
9+
</div>
10+
<div class="form-group-sm">
11+
<textarea id="questionDescription"
12+
class="form-control"
13+
rows="3"
14+
placeholder="Enter description"
15+
>{{question.description}}</textarea>
16+
</div>
17+
18+
{{else}}
19+
<div class="form-group-sm">
20+
<h4>
21+
{{question.title}}
22+
</h4>
23+
24+
</div>
25+
<div class="form-group-sm">
26+
<p id="questionDescription">{{question.description}}</p>
27+
</div>
28+
{{/if}}
29+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Template.editableTitleDescription.events({
2+
'keyup #questionTitle': function(event){
3+
var titleStr = $(event.target).val();
4+
var question = Template.currentData().question;
5+
question.title = titleStr;
6+
},
7+
8+
'keyup #questionDescription': function(event){
9+
var description = $(event.target).val();
10+
var question = Template.currentData().question;
11+
question.description = description;
12+
},
13+
});
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<template name="multipleChoiceTemplate">
2+
<div class="form ">
3+
4+
{{>editableTitleDescription}}
5+
6+
{{#if isEditingCurrentCourse }} <!-- global helper -->
7+
<div class="form-group-sm">
8+
<div class="form-group form-inline">
9+
Number of options:
10+
<select id="numQuestions" class="form-control small">
11+
{{#each opt in selectInputOptions }}
12+
<option value={{opt.value}} disabled={{opt.disabled}} selected={{opt.selected}}>{{opt.text}}</option>
13+
{{/each}}
14+
</select>
15+
</div>
16+
</div>
17+
{{/if}}
18+
19+
<div>
20+
{{#each option in selectedNumberOfOptions }}
21+
{{> multipleChoiceInputField
22+
multipleAnswersAllowed=isMultipleAnswers
23+
option=option
24+
question=question
25+
index=@index
26+
}}
27+
{{/each}}
28+
</div>
29+
</div>
30+
</template>

0 commit comments

Comments
 (0)