Changeset View
Standalone View
src/activities/calendar/calendar.js
Show All 34 Lines | |||||
35 | var daySelected = 1 | 35 | var daySelected = 1 | ||
36 | var monthSelected = 2 | 36 | var monthSelected = 2 | ||
37 | var yearSelected = 2018 | 37 | var yearSelected = 2018 | ||
38 | var dayOfWeekSelected | 38 | var dayOfWeekSelected | ||
39 | var minRange //sum of min. visible month and year on calendar for navigation bar next/prev button visibility. | 39 | var minRange //sum of min. visible month and year on calendar for navigation bar next/prev button visibility. | ||
40 | var maxRange //sum of max. visible month and year on calendar for navigation bar next/prev button visibility. | 40 | var maxRange //sum of max. visible month and year on calendar for navigation bar next/prev button visibility. | ||
41 | var correctAnswer | 41 | var correctAnswer | ||
42 | var mode | 42 | var mode | ||
43 | var daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | ||||
43 | 44 | | |||
44 | function start(items_, dataset_) { | 45 | function start(items_) { | ||
45 | items = items_ | 46 | items = items_ | ||
46 | dataset = dataset_.get() | 47 | numberOfLevel = items.levels.length | ||
47 | numberOfLevel = dataset.length | | |||
48 | currentLevel = 0 | 48 | currentLevel = 0 | ||
49 | 49 | | |||
50 | if(Qt.locale(GCompris.ApplicationSettings.locale).firstDayOfWeek == Qml.Locale.Monday) { | 50 | if(Qt.locale(GCompris.ApplicationSettings.locale).firstDayOfWeek == Qml.Locale.Monday) { | ||
51 | items.daysOfTheWeekModel.move(0, items.daysOfTheWeekModel.count - 1, 1) | 51 | items.daysOfTheWeekModel.move(0, items.daysOfTheWeekModel.count - 1, 1) | ||
52 | } | 52 | } | ||
53 | initLevel(); | 53 | initLevel(); | ||
54 | } | 54 | } | ||
55 | 55 | | |||
56 | function stop() { | 56 | function stop() { | ||
57 | } | 57 | } | ||
58 | 58 | | |||
59 | function initLevel() { | 59 | function initLevel() { | ||
60 | currentSubLevel = 1; | 60 | currentSubLevel = 1; | ||
61 | items.bar.level = currentLevel + 1 | 61 | items.bar.level = currentLevel + 1 | ||
62 | currentLevelConfig = dataset[currentLevel][0][0] | 62 | currentLevelConfig = items.levels[currentLevel] | ||
63 | setCalendarConfigurations() | 63 | setCalendarConfigurations() | ||
64 | initQuestion(); | 64 | initQuestion(); | ||
65 | } | 65 | } | ||
66 | 66 | | |||
67 | function nextLevel() { | 67 | function nextLevel() { | ||
68 | if(numberOfLevel <= ++currentLevel) { | 68 | if(numberOfLevel <= ++currentLevel) { | ||
69 | currentLevel = 0 | 69 | currentLevel = 0 | ||
70 | } | 70 | } | ||
Show All 16 Lines | 82 | function setCalendarConfigurations() { | |||
87 | items.calendar.minimumDate = currentLevelConfig["minimumDate"] | 87 | items.calendar.minimumDate = currentLevelConfig["minimumDate"] | ||
88 | items.calendar.maximumDate = currentLevelConfig["maximumDate"] | 88 | items.calendar.maximumDate = currentLevelConfig["maximumDate"] | ||
89 | items.calendar.visibleYear = currentLevelConfig["visibleYear"] | 89 | items.calendar.visibleYear = currentLevelConfig["visibleYear"] | ||
90 | yearSelected = currentLevelConfig["visibleYear"] | 90 | yearSelected = currentLevelConfig["visibleYear"] | ||
91 | items.calendar.visibleMonth = currentLevelConfig["visibleMonth"] | 91 | items.calendar.visibleMonth = currentLevelConfig["visibleMonth"] | ||
92 | monthSelected = currentLevelConfig["visibleMonth"] | 92 | monthSelected = currentLevelConfig["visibleMonth"] | ||
93 | items.answerChoices.visible = (mode === "findDayOfWeek") ? true : false | 93 | items.answerChoices.visible = (mode === "findDayOfWeek") ? true : false | ||
94 | items.okButton.visible = !items.answerChoices.visible | 94 | items.okButton.visible = !items.answerChoices.visible | ||
95 | currentDataSet = dataset[currentLevel][1] | 95 | currentDataSet = currentLevelConfig["questionAnswers"] | ||
96 | currentDataSet = Core.shuffle(currentDataSet) | 96 | currentDataSet = Core.shuffle(currentDataSet) | ||
97 | items.score.numberOfSubLevels = currentDataSet.length | 97 | items.score.numberOfSubLevels = currentDataSet.length | ||
98 | items.score.currentSubLevel = currentSubLevel | 98 | items.score.currentSubLevel = currentSubLevel | ||
99 | } | 99 | } | ||
100 | 100 | | |||
101 | function isLeapYear(year) { | ||||
102 | if(year % 100 == 0 && year % 400 != 0) { | ||||
103 | return false | ||||
104 | } else if(year % 4 == 0) { | ||||
105 | return true | ||||
106 | } else { | ||||
107 | return false | ||||
108 | } | ||||
109 | } | ||||
110 | | ||||
111 | function getDateInLongFormat(date) { | ||||
112 | var months = [qsTr("January"), qsTr("February"), qsTr("March"), qsTr("April"), qsTr("May"), qsTr("June"), qsTr("July"), | ||||
jjazeix: qsTr() missing | |||||
scagarwal: Do maintain spaces between elements in an array. The formatting is wrong. | |||||
113 | qsTr("August"), qsTr("September"), qsTr("October"), qsTr("November"), qsTr("December")]; | ||||
amankumargupta: Indentation | |||||
114 | return date.day.toString() + " " + months[date.month] + " " + date.year.toString() + "?"; | ||||
this should be depending on the country. We may want to display month day year on some locales, it needs to be configurable for the translator (and have a comment) jjazeix: this should be depending on the country. We may want to display month day year on some locales… | |||||
115 | } | ||||
116 | | ||||
117 | function generateRandomYearMonthDay(minimumDate, maximumDate) { | ||||
118 | var minYear = Number(minimumDate.slice(0, 4)) | ||||
119 | var maxYear = Number(maximumDate.slice(0, 4)) | ||||
120 | var minMonth = Number(minimumDate.slice(5, 7)) | ||||
121 | var maxMonth = Number(maximumDate.slice(5, 7)) | ||||
122 | var minDate = Number(minimumDate.slice(8, 10)) | ||||
123 | var currentYear = minYear + Math.floor(Math.random() * Math.floor((maxYear - minYear + 1))) | ||||
124 | var currentMonth = minMonth + Math.floor(Math.random() * Math.floor((maxMonth - minMonth + 1))) | ||||
125 | var currentDate | ||||
Shouldn't Math.floor((maxMonth - minMonth)) be Math.floor(maxMonth - minMonth + 1)? Let's say minMonth is 3 and maxMonth is 5. I should get any random among 0, 1, 2 so that minMonth + Math.floor(Math.random() * Math.floor((maxMonth - minMonth))) can be anything between 3, 4, 5. But, Math.floor(Math.random() * Math.floor((maxMonth - minMonth))) limits the values among 0, 1 (as minMonth - maxMonth = 2 in our case). So you should probably do maxMonth - minMonth + 1? amankumargupta: Shouldn't Math.floor((maxMonth - minMonth)) be Math.floor(maxMonth - minMonth + 1)?
Let's say… | |||||
126 | daysInMonths[1] = (isLeapYear(currentYear)) ? 29 : 28; | ||||
127 | currentDate = minDate + Math.floor(Math.random() * Math.floor((daysInMonths[currentMonth - 1] - minDate + 1))) | ||||
128 | return { year: currentYear, month: currentMonth - 1, day: currentDate } | ||||
amankumargupta: Same comment about choosing random value as above. | |||||
129 | } | ||||
130 | | ||||
131 | function addOffsetToCurrentDate(currentDate) { | ||||
132 | var maxOffset = currentLevelConfig.questionAnswers.maxOffset | ||||
133 | var offset = Math.floor(maxOffset / 2) + Math.floor(Math.random() * Math.floor(maxOffset)) | ||||
134 | daysInMonths[1] = (isLeapYear(currentDate.year)) ? 29 : 28; | ||||
135 | var currentOffset = offset; | ||||
136 | currentOffset += currentDate.day | ||||
Why a new variable "currentOffset"? Why not directly use "offset"? It's not used anywhere in this function. amankumargupta: Why a new variable "currentOffset"? Why not directly use "offset"? It's not used anywhere in… | |||||
137 | var answerDate = 1; | ||||
138 | var answerMonth = currentDate.month | ||||
139 | var answerYear = currentDate.year | ||||
140 | while(currentOffset > 0) { | ||||
141 | if(currentOffset - daysInMonths[answerMonth] > 0) { | ||||
142 | currentOffset -= daysInMonths[answerMonth] | ||||
143 | answerMonth++; | ||||
144 | } else { | ||||
145 | answerDate = currentOffset; | ||||
146 | currentOffset = 0 | ||||
147 | } | ||||
148 | if(answerMonth > 12) { | ||||
149 | answerYear++; | ||||
150 | daysInMonths[1] = (isLeapYear(answerYear)) ? 29 : 28; | ||||
151 | answerMonth = 0; | ||||
152 | } | ||||
153 | } | ||||
154 | return { year: answerYear, month: answerMonth, day: answerDate, offset: offset } | ||||
155 | } | ||||
156 | | ||||
157 | function getTemplateQuestionText(mode, date) { | ||||
158 | var questionText | ||||
159 | if(mode == "findDayOfWeek") { | ||||
160 | questionText = qsTr("What day of the week is on %1 ?").arg(date.day) | ||||
jjazeix: no space before ? | |||||
161 | } else if(mode == "findDay") { | ||||
qsTr() + bad format jjazeix: qsTr() + bad format
Can you please check if the format is the same if we change locale? | |||||
@jjazeix When I change the locale only the names of months and years change, not the question text. As we used hardcoded strings for questions. AkshayCHD: @jjazeix When I change the locale only the names of months and years change, not the question… | |||||
162 | questionText = qsTr("Select day %1 ?").arg(date.day) | ||||
163 | } else if(mode == "findMonthOnly") { | ||||
164 | questionText = qsTr("Find month number %1").arg(date.month + 1) | ||||
165 | } else { | ||||
166 | if(date.offset) { | ||||
167 | questionText = qsTr("Find the date %1 days after %2").arg(date.offset).arg(getDateInLongFormat(date)) | ||||
168 | } else | ||||
Please add translators comment above explaining what the arguments will be. It'll be easier for them to understand while translating. amankumargupta: Please add translators comment above explaining what the arguments will be. It'll be easier for… | |||||
169 | questionText = qsTr("Find the date %1").arg(getDateInLongFormat(date)) | ||||
170 | } | ||||
171 | return questionText | ||||
172 | } | ||||
173 | | ||||
101 | function initQuestion() { | 174 | function initQuestion() { | ||
102 | if(currentDataSet.length < currentSubLevel) { | 175 | if(currentDataSet.length < currentSubLevel) { | ||
103 | items.bonus.good("lion") | 176 | items.bonus.good("lion") | ||
104 | } | 177 | } | ||
105 | else { | 178 | else { | ||
179 | if(!currentLevelConfig.questionsExplicitlyGiven) { | ||||
180 | var randomDate = generateRandomYearMonthDay(currentLevelConfig.minimumDate, currentLevelConfig.maximumDate) | ||||
181 | items.score.currentSubLevel = currentSubLevel | ||||
182 | if(currentLevelConfig.mode == "findDayOfWeek") { | ||||
183 | var selectedDate = new Date(randomDate.year, randomDate.month - 1, randomDate.day) | ||||
184 | correctAnswer.dayOfWeek = Number(selectedDate.getDay()) | ||||
185 | } else if(currentLevelConfig.mode == "findYearMonthDay" && currentLevelConfig.questionAnswers.maxOffset) { | ||||
186 | correctAnswer = addOffsetToCurrentDate(randomDate) | ||||
187 | randomDate.offset = correctAnswer.offset | ||||
188 | } else { | ||||
189 | correctAnswer = randomDate | ||||
190 | } | ||||
191 | items.questionItem.text = getTemplateQuestionText(currentLevelConfig.mode, randomDate) | ||||
192 | } else { | ||||
106 | items.score.currentSubLevel = currentSubLevel | 193 | items.score.currentSubLevel = currentSubLevel | ||
107 | items.questionItem.text = currentDataSet[currentSubLevel-1]["question"] | 194 | items.questionItem.text = currentDataSet[currentSubLevel-1]["question"] | ||
108 | correctAnswer = currentDataSet[currentSubLevel-1]["answer"] | 195 | correctAnswer = currentDataSet[currentSubLevel-1]["answer"] | ||
109 | } | 196 | } | ||
110 | } | 197 | } | ||
198 | } | ||||
111 | 199 | | |||
112 | function updateScore(isCorrectAnswer) { | 200 | function updateScore(isCorrectAnswer) { | ||
113 | if(isCorrectAnswer) { | 201 | if(isCorrectAnswer) { | ||
114 | items.questionDelay.start() | 202 | items.questionDelay.start() | ||
115 | items.okButtonParticles.burst(20) | 203 | items.okButtonParticles.burst(20) | ||
116 | items.score.playWinAnimation() | 204 | items.score.playWinAnimation() | ||
117 | currentSubLevel++; | 205 | currentSubLevel++; | ||
118 | } | 206 | } | ||
Show All 10 Lines | 214 | if(mode === "findDayOfWeek") { | |||
129 | } | 217 | } | ||
130 | } | 218 | } | ||
131 | // For levels having question based on month only. | 219 | // For levels having question based on month only. | ||
132 | else if(mode === "findMonthOnly") { | 220 | else if(mode === "findMonthOnly") { | ||
133 | if(correctAnswer["month"].indexOf(monthSelected) >= 0) { | 221 | if(correctAnswer["month"].indexOf(monthSelected) >= 0) { | ||
134 | isCorrectAnswer = true | 222 | isCorrectAnswer = true | ||
135 | } | 223 | } | ||
136 | } | 224 | } | ||
225 | // For levels having question based on day only. | ||||
226 | else if(mode === "findDay") { | ||||
227 | if(daySelected === correctAnswer["day"]) { | ||||
228 | isCorrectAnswer = true | ||||
229 | } | ||||
230 | } | ||||
137 | // For levels having questions based on dayOfWeek, month and year. | 231 | // For levels having questions based on dayOfWeek, month and year. | ||
138 | else if(mode !== "findDayOfWeek") { | 232 | else { | ||
139 | if(monthSelected === correctAnswer["month"] && daySelected === correctAnswer["day"] && yearSelected === correctAnswer["year"]) { | 233 | if(monthSelected === correctAnswer["month"] && daySelected === correctAnswer["day"] && yearSelected === correctAnswer["year"]) { | ||
140 | isCorrectAnswer = true | 234 | isCorrectAnswer = true | ||
141 | } | 235 | } | ||
142 | } | 236 | } | ||
143 | updateScore(isCorrectAnswer) | 237 | updateScore(isCorrectAnswer) | ||
144 | } | 238 | } | ||
145 | 239 | |
qsTr() missing