Skip to content

Commit c5d67da

Browse files
Quiz preview - submit and results
1 parent a7892b0 commit c5d67da

File tree

11 files changed

+302
-273
lines changed

11 files changed

+302
-273
lines changed

app/helpers/mark-option.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {helper} from '@ember/component/helper';
2+
3+
export function markOption([questionId, choiceId, answers]) {
4+
let index = answers.findIndex(obj => obj.id == questionId)
5+
if (index > -1) {
6+
if (answers[index].markedChoices.includes(choiceId)) {
7+
return true;
8+
}
9+
}
10+
return false
11+
}
12+
13+
export default helper(markOption);
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
import Component from '@ember/component';
2+
import Ember from 'ember';
23

3-
export default Component.extend({})
4+
export default Component.extend({
5+
actions: {
6+
selectChoiceAndMark(questionNumber, choiceId) {
7+
this.get('selectChoice')(questionNumber, choiceId)
8+
}
9+
},
10+
selectedChoice: Ember.computed('questionNumber', 'answers', function () {
11+
let questionNumber = this.get('questionNumber')
12+
let answers = this.get('answers')
13+
let index = answers.findIndex(obj => obj.id == questionNumber)
14+
if (index > -1) {
15+
return answers[index].markedChoices[0]
16+
}
17+
return null
18+
})
19+
})

app/pods/components/question-preview/template.hbs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
<br>
55
{{#each question.choices as |choice|}}
66
{{#if question.multicorrect}}
7-
<input type="checkbox" name="questionChoices" value="{{choice.id}}"> {{choice.title}} <br>
7+
{{input type="checkbox" checked=(mark-option question.id choice.id answers) name="questionChoices"
8+
click=(action "selectChoiceAndMark" question.id choice.id)}}
9+
{{choice.title}}
10+
<br>
811
{{else}}
9-
<input type="radio" name="questionChoices" value="{{choice.id}}"> {{choice.title}} <br>
12+
{{#radio-button value=choice.id groupValue=selectedChoice name="questionChoices"
13+
changed=(action "selectChoiceAndMark" question.id choice.id)}}
14+
{{choice.title}}
15+
{{/radio-button}}
16+
<br>
1017
{{/if}}
1118
{{choice.description}}<br><br>
1219
{{/each}}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Component from '@ember/component';
2+
import Ember from 'ember';
3+
import {inject as service} from '@ember/service';
4+
5+
export default Component.extend({
6+
store: service(),
7+
question: Ember.computed('res', function () {
8+
return this.store.findRecord('question', this.get('res').id)
9+
})
10+
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<div style="border-color: black">
2+
Question ID : {{res.id}}<br>
3+
Question Score : {{res.score}}<br>
4+
Question Title : {{question.title}}<br>
5+
{{#if res.correctlyAnswered}}
6+
Correctly marked choices :
7+
<ul style="list-style-type: square">
8+
{{#each res.correctlyAnswered as |choice|}}
9+
<li>{{choice.title}}</li>
10+
{{/each}}
11+
</ul>
12+
{{/if}}
13+
14+
{{#if res.incorrectlyAnswered}}
15+
Incorrectly marked choices :
16+
<ul style="list-style-type: square">
17+
{{#each res.incorrectlyAnswered as |choice|}}
18+
<li>{{choice.title}}</li>
19+
{{/each}}
20+
</ul>
21+
{{/if}}
22+
23+
</div>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,66 @@
11
import Controller from '@ember/controller';
2+
import {inject as service} from '@ember/service';
23

34
export default Controller.extend({
45
queryParams: ['questionId'],
6+
answers: [],
7+
api: service(),
58
actions: {
69
navigate(questionNumber) {
710
this.set('question', questionNumber)
11+
},
12+
async selectChoice(questionNumber, choiceId) {
13+
let answers = this.answers
14+
let idx = answers.findIndex(obj => obj.id == questionNumber)
15+
if (idx > -1) {
16+
let ans = answers[idx];
17+
//If question's entry is present then modify it
18+
const question = await this.store.findRecord('question', questionNumber)
19+
if (question.multicorrect) {
20+
if (ans.markedChoices.includes(choiceId)) {
21+
//If choice is already listed then it must have been removed
22+
let index = ans.markedChoices.indexOf(choiceId);
23+
if (index > -1) {
24+
ans.markedChoices.splice(index, 1);
25+
}
26+
} else {
27+
//Else add the choice to current marked choices
28+
ans.markedChoices = [...ans.markedChoices, choiceId]
29+
}
30+
} else {
31+
//For non-multicorrect questions, replace the previous choice
32+
ans.markedChoices = [choiceId]
33+
}
34+
} else {
35+
//No previous record of this question. Create a new entry and append to answers.
36+
answers.push({
37+
id: questionNumber,
38+
markedChoices: [choiceId]
39+
})
40+
}
41+
this.answers = answers
42+
},
43+
fetchingResult: false,
44+
submitQuiz() {
45+
if (this.fetchingResult)
46+
return;
47+
let quiz = this.get('quiz')
48+
let body = {
49+
questions: this.answers
50+
}
51+
this.set('fetchingResult', true);
52+
this.api.request('/quizzes/' + quiz.id + '/submit', {
53+
method: 'POST',
54+
mode: 'cors',
55+
data: JSON.stringify(body),
56+
}).then((response) => {
57+
console.log(response)
58+
this.set('results', response)
59+
}).catch(console.error)
60+
.finally(() => {
61+
this.set('fetchingResult', false)
62+
})
63+
864
}
965
},
1066
})

app/pods/quiz/id/preview/template.hbs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{{#if question}}
1010
<div class="row">
1111
<div class="col container-fluid">
12-
{{question-preview question=question}}
12+
{{question-preview question=question selectChoice=(action 'selectChoice') answers=answers}}
1313
</div>
1414
<div class="col">
1515
{{question-navigator quiz=quiz navigateToQuestion=(action (mut questionId))}}
@@ -23,10 +23,36 @@
2323
{{/if}}
2424

2525
<div class="container-fluid">
26-
<div class="button-solid">
27-
<a href="/quiz/{{quiz.id}}" class="white">
28-
<button>Edit Quiz</button>
29-
</a>
26+
<div class="row">
27+
<div class="col-2"/>
28+
<div class="col">
29+
<div class="button-solid">
30+
<a href="/quiz/{{quiz.id}}" class="white">
31+
<button>Edit Quiz</button>
32+
</a>
33+
</div>
34+
</div>
35+
<div class="col-4"/>
36+
<div class="col">
37+
<div class="button-solid">
38+
<button {{action 'submitQuiz'}}>Submit</button>
39+
</div>
40+
</div>
3041
</div>
42+
43+
{{#if fetchingResult}}
44+
<h3> Fetching results ... wait a moment</h3>
45+
{{/if}}
46+
{{#if results}}
47+
<div class="container-fluid">
48+
<h3 align="center">Results</h3>
49+
<b>Total Score : {{results.score}}</b>
50+
<br>
51+
{{#each results.questions as |quesResult|}}
52+
{{question-result res=quesResult}}
53+
<br>
54+
{{/each}}
55+
</div>
56+
{{/if}}
3157
</div>
3258
</div>

0 commit comments

Comments
 (0)