-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrishab
269 lines (218 loc) · 7.4 KB
/
rishab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// meteor add session accounts-ui accounts-password reactive-var
ThisQuiz = new Mongo.Collection("quizzes");
ThisResponse = new Mongo.Collection("responses");
if (Meteor.isClient) {
var quizQuestions = [];
changeAnswer = function(question){
Meteor.call("getResponse", question, function(error, result){
if (error)
console.log(error.reason);
else {
if (result)
$('input[name=questionAnswer][value=' + result + ']').prop('checked',true);
else
$('input[name=questionAnswer]').prop('checked',false);
}
});
}
/* **************************************************************************************************** */
/*Template.scorecard.onRendered(function(){
Meteor.call("checkAnswer")
})*/
/* ****************************************************************************************************** */
Template.questionSelect.onCreated( function() {
this.time_left = new ReactiveVar(1200);
});
Template.questionSelect.onRendered( function() {
var self = this;
Meteor.call("getQuiz", function(error, result){
if (error)
console.log(error.reason);
else {
quizQuestions = result;
Session.set('selectedQuestion', '1');
changeAnswer('1');
}
});
Meteor.call("getTimer", function(error, result){
if (error)
console.log(error.reason);
else {
self.time_left.set(result);
}
});
Meteor.setInterval( function() {self.time_left.set(self.time_left.get() - 1);} , 1000 );
Meteor.setInterval( function() {Meteor.call('setTimer', self.time_left.get())} , 5000 );
});
Template.questionSelect.events({
'click input': function( event,template ) {
Session.set('selectedQuestion', event.target.value);
changeAnswer(event.target.value);
}
});
Template.questionSelect.helpers({
time_left: function() {
var time = Template.instance().time_left.get();
var minutes = ((Math.floor(time/60) < 10) ? '0' : '') + Math.floor(time/60);
var seconds = ((time%60 < 10) ? '0' : '') + (time%60);
return minutes+":"+seconds;
}
});
/* **************************************************************************************************** */
Template.questionShow.events({
'click input': function( event,template ) {
Meteor.call('setResponse', parseInt(Session.get('selectedQuestion')) , event.target.value );
},
'click #onsubmitbutton' :function(){
var responseID = Meteor.user().profile.current_response_id;
var response = ThisResponse.findOne(responseID);
var userId = Meteor.userId();
var answer = eval('response.answers.' + userId);
console.log(response);
console.log(userId);
console.log(answer);
console.log(answer[1]);
var currentQuiz=response.quizID;
console.log(currentQuiz);
var correctOption=(ThisQuiz.findOne(currentQuiz));
var cor_array=[];
for(var i=0;i<2;i++){
var correct=correctOption.quiz[i];
var cor=correct.answer;
console.log(correct);
var cor='option'+cor;
cor_array[i]=correct.cor;
console.log(cor);
}
//var correct=correctOption.quiz[0].answer;
for(var i=0;i<2;i++)
{
if(answer[i+1]===cor_array[i])
{
response.score+=10;
}
}
console.log(response.score);
/*var userAnswer= getResponse(question);
if(userAnswer===correctOption)
{
response.score+=10;
console.log(response.score);
}*/
}
});
Template.questionShow.helpers({
questionData: function() {
return quizQuestions[parseInt(Session.get('selectedQuestion')) - 1];
},
});
/* **************************************************************************************************** */
Template.quizSelect.events({
"submit .quizParameters": function (event) {
event.preventDefault();
var selectedType = event.target.quizType.value;
var selectedCategory = event.target.quizCategory.value;
Meteor.call("createResponse", selectedType, selectedCategory);
}
});
/* **************************************************************************************************** */
}
Router.configure({
layoutTemplate: 'ApplicationLayout'
});
//Router.route('/addquiz', {
// name: 'addquiz',
//template: 'addquiz'
//});
Router.route('/', function () {
if (! Meteor.userId()) {
this.render('login', {to:"template1"});
this.render('emptyTemplate', {to:"template2"});
}
else if (Meteor.user().profile.current_response_id == '') {
this.render('quizSelect', {to:"template1"});
this.render('emptyTemplate', {to:"template2"});
}
else {
this.render('questionSelect', {to:"template1"});
this.render('questionShow', {to:"template2"});
}
});
/* **************************************************************************************************** */
if(Meteor.isServer){
Accounts.onCreateUser(function(options, user) {
user.profile = {};
user.profile.current_response_id = "";
user.stats = [];
if (options.profile)
user.profile = options.profile;
return user;
});
Meteor.methods({
getQuiz: function (){
var responseID = Meteor.user().profile.current_response_id;
var quizID = ThisResponse.findOne(responseID).quizID;
var randomQuiz = ThisQuiz.findOne(quizID);
var quizQuestions = randomQuiz.quiz;
for (var i=0 ; i<quizQuestions.length ; i++){
delete quizQuestions[i]["answer"];
}
return quizQuestions;
},
getRandomQuizID: function(category){
var randomQuiz = ThisQuiz.find({category:category}).fetch();
randomQuiz = randomQuiz[Math.floor(Math.random() * randomQuiz.length)];
return randomQuiz._id;
},
createResponse: function(type, category){
if (! Meteor.userId()) { throw new Meteor.Error('not-authorized'); }
var quizID = Meteor.call('getRandomQuizID', category);
var responseID = ThisResponse.insert({
userID: [Meteor.userId()],
quizID: quizID,
type: type,
answers: {[Meteor.userId()]: {}},
time_left: 1200,
score: {},
conductedAt: new Date()
});
Meteor.users.update(Meteor.user(), {$set: {"profile.current_response_id": responseID }});
},
setResponse: function (question, answer){
var responseID = Meteor.user().profile.current_response_id;
var user = Meteor.userId();
ThisResponse.update(responseID, {$set: {['answers.' + user + '.' + question]: answer}});
},
getResponse: function(question){
var responseID = Meteor.user().profile.current_response_id;
var user = Meteor.userId();
var response = ThisResponse.findOne(responseID).answers;
var answer = eval('response.' + user + '[' + question + ']');
console.log(answer);
return answer;
},
setTimer: function(new_time){
var responseID = Meteor.user().profile.current_response_id;
var response = ThisResponse.findOne(responseID);
if (response.time_left > new_time)
ThisResponse.update(response._id, {$set: {time_left: new_time}});
},
getTimer: function(){
var responseID = Meteor.user().profile.current_response_id;
return ThisResponse.findOne(responseID).time_left;
}
/*checkAnswer: function(question){
var responseID = Meteor.user().profile.current_response_id;
var response = ThisResponse.findOne(responseID);
var answer = eval('response.' + user)
var currentQuiz=response.quizID;
var correctOption=(quizzes.find(currentQuiz)).quiz[0].answer;
var userAnswer= getResponse(question);
if(userAnswer===correctOption)
{
response.score+=10;
return true;
}
}*/
});
}