-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExamCtrl.coffee
153 lines (117 loc) · 4.89 KB
/
ExamCtrl.coffee
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
angular.module('infish').controller 'ExamCtrl', ($scope, $stateParams, $state, $interval, userExam, UserExam) ->
if !userExam.repeat || !userExam.repeat_wrong
return $state.go 'exams.repeats', id: userExam.exam.id
$scope.userExam = userExam
$scope.exam = userExam.exam
# here will go questions that have repeat = 0 so it's more efficient to
# draw next question
$scope.exam.masteredQuestions = []
init = ->
angular.forEach $scope.userExam.user_answers, (userAnswer) ->
question = $scope.exam.questions[userAnswer.question_id]
return unless question?
question.repeat = $scope.userExam.repeat unless question.repeat?
if wasQuestionAnsweredCorrectly(question, userAnswer.answers)
question.repeat -= 1
else
question.repeat += $scope.userExam.repeat_wrong
conditionallyMoveQuestionToMastered(question)
angular.forEach $scope.exam.questions, (question) ->
return if question.repeat?
question.repeat = $scope.userExam.repeat
drawNextQuestion() if $scope.questionsLeftCount() > 0
$scope.current =
question: null # init() draws next questions
answers: []
check: false
# TODO: it probably doesn't have to be on $scope, just debugging
# "new" user answers are the ones not yet saved on back-end
$scope.newUserAnswers = []
$scope.checkAnswers = ->
return if $scope.current.answers.length == 0
$scope.current.check = true
$scope.newUserAnswers.push buildUserAnswer()
updateRepeatForQuestion()
conditionallyMoveQuestionToMastered()
# HACK: it should also be disabled on $scope.current.check, but this
# way it doesn't break our animation
$scope.shouldDisableCheckAnswersButton = ->
$scope.current.answers.length == 0
$scope.nextQuestion = ->
$scope.current.check = false
$scope.current.answers = []
drawNextQuestion()
$scope.shouldDisableNextQuestionButton = ->
!$scope.current.check
# TODO: probably should be a directive
$scope.answerClass = (answer) ->
classes = []
answerInAnswers = wasAnswerInAnswers(answer)
classes.push 'selected-answer' if answerInAnswers
return classes unless $scope.current.check
if answer.correct
classes.push 'correct-answer'
else if !answer.correct && answerInAnswers
classes.push 'incorrect-answer'
classes
$scope.questionClass = (question) ->
return unless $scope.current.check
if wasQuestionAnsweredCorrectly() then 'question-correct' else 'question-incorrect'
$scope.questionsLeftCount = ->
Object.keys($scope.exam.questions).length
$scope.isExamSolved = ->
$scope.questionsLeftCount() == 0
$scope.percentageProgress = ->
Math.floor(($scope.exam.masteredQuestions.length / ($scope.questionsLeftCount() + $scope.exam.masteredQuestions.length)) * 100)
$scope.startOver = ->
if confirm('Na pewno chcesz zacząć od zera?')
UserExam.startOver($scope.userExam.id)
.success ->
$state.go 'exams.repeats', id: $scope.exam.id
.error ->
# TODO: something went wrong
buildUserAnswer = ->
{
id: UUIDjs.create().toString()
question_id: $scope.current.question.id
answers: angular.copy($scope.current.answers)
}
wasAnswerInAnswers = (answer, answers = $scope.current.answers) ->
answer.id in answers
updateRepeatForQuestion = ->
if wasQuestionAnsweredCorrectly()
$scope.current.question.repeat -= 1
else
$scope.current.question.repeat += $scope.userExam.repeat_wrong
conditionallyMoveQuestionToMastered = (question = $scope.current.question) ->
if question.repeat == 0
$scope.exam.masteredQuestions.push question
delete $scope.exam.questions[question.id]
wasQuestionAnsweredCorrectly = (question = $scope.current.question, answers = $scope.current.answers) ->
correctly = true
angular.forEach question.answers, (answer) ->
return unless correctly
answerInAnswers = wasAnswerInAnswers(answer, answers)
correctly = false if (!answer.correct && answerInAnswers) || (answer.correct && !answerInAnswers)
return correctly
drawNextQuestion = ->
# TODO this probably should be called only once, not every draw
keys = Object.keys($scope.exam.questions)
randomlyPickedID = keys[Math.floor(Math.random() * keys.length)]
question = $scope.exam.questions[randomlyPickedID]
question.answers = question.answers.sort -> 0.5 - Math.random()
question.id = randomlyPickedID
$scope.current.question = question
# SYNC
syncUserAnswers = ->
return if $scope.newUserAnswers.length == 0
answers = angular.copy($scope.newUserAnswers)
$scope.newUserAnswers = []
UserExam.syncUserAnswers($scope.userExam, answers)
.error (e) ->
$scope.newUserAnswers = $scope.newUserAnswers.concat answers
syncAnswersInterval = $interval syncUserAnswers, 5000
$scope.$on '$stateChangeStart', ->
$interval.cancel(syncAnswersInterval)
# INIT
init()