-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed the quiz controller, now named as app.js
Check Todo comments for further issue
- Loading branch information
Skarvion
committed
Jan 25, 2017
1 parent
39fe3ed
commit a537ea3
Showing
6 changed files
with
221 additions
and
38 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
var app = angular.module('gameApp', ['ngRoute']); | ||
|
||
// Global variables for score tracking | ||
// Todo: Decide whether edit the global variables directly or create factory method | ||
app.run(function ($rootScope) { | ||
$rootScope.quizScore = 0; | ||
$rootScope.matchScore = 0; | ||
$rootScope.typedScore = 0; | ||
}); | ||
|
||
app.factory('JSONData', ['$http', '$q', function ($http, $q) { | ||
var JSONData = {}; | ||
|
||
JSONData.getQuiz = function() { | ||
return $http.get("json/quizdata.json"); | ||
}; | ||
|
||
JSONData.getMatching = function() { | ||
return $http.get("json/matchquizdata.json"); | ||
}; | ||
|
||
JSONData.shuffleArray = function(array) { | ||
var m = array.length, t, i; | ||
|
||
// While there remain elements to shuffle… | ||
while (m) { | ||
// Pick a remaining element… | ||
i = Math.floor(Math.random() * m--); | ||
// And swap it with the current element. | ||
t = array[m]; | ||
array[m] = array[i]; | ||
array[i] = t; | ||
} | ||
return array; | ||
} | ||
|
||
return JSONData; | ||
}]); | ||
|
||
app.controller("MCQGameCtrl", ['$scope', '$rootScope', '$http', '$q', 'JSONData', function ($scope, $rootScope, $http, $q, JSONData) { | ||
$scope.rawData = []; | ||
$scope.rawQuestions = []; // The raw array of questions that have yet to be processed | ||
$scope.quizQuestions = []; | ||
|
||
function ExtractData() { | ||
|
||
JSONData.getQuiz() | ||
.then(function (response) { | ||
$scope.quizQuestions = response.data; | ||
// $scope.rawData = response.data; | ||
}, | ||
function (status) { | ||
alert("Data quiz tidak bisa diambil"); | ||
}); | ||
} | ||
|
||
// Refresh all kind of updates to any changes to array of objects | ||
$scope.refreshList = function () { | ||
$scope.$apply(); | ||
} | ||
|
||
function prepareQuizQuestions() { | ||
|
||
var m = $scope.quizQuestions.length, t, i; | ||
|
||
// While there remain elements to shuffle… | ||
while (m) { | ||
// Pick a remaining element… | ||
i = Math.floor(Math.random() * m--); | ||
// And swap it with the current element. | ||
t = angular.copy($scope.quizQuestions[m]); | ||
$scope.quizQuestions[m] = angular.copy($scope.quizQuestions[i]); | ||
$scope.quizQuestions[i] = angular.copy(t); | ||
|
||
// Prepare the question objects with new fields | ||
$scope.quizQuestions[m].selectedAnswer = 0; // Determines selected answer by student | ||
$scope.quizQuestions[m].isDisabled = false; // Disable the input control | ||
} | ||
|
||
$scope.refreshList(); | ||
} | ||
|
||
$scope.checkAnswer = function (question) { | ||
if (question.selectedAnswer == 0) { | ||
alert("Mohon diisi jawabannya"); | ||
return; | ||
} | ||
|
||
if (question.selectedAnswer == question.corrAnsInt) { | ||
$rootScope.quizScore += 10; | ||
alert("Correct score: " + $rootScope.quizScore); | ||
} | ||
else alert("Wrong"); | ||
|
||
question.isDisabled = true; | ||
} | ||
|
||
$scope.random = function() { | ||
return 0.5 - Math.random(); | ||
}; | ||
|
||
ExtractData(); | ||
|
||
// Todo: A VERY DANGEROUS SHIT HERE, WHAT HAPPEN IF THE DATA NOT LOADED BY THE TIME THE TIMEOUT FINISH? | ||
// MAKE IT SYNCHRONOUS ONCE THE WHOLE THING DONE ASAP | ||
setTimeout(prepareQuizQuestions, 1000); | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,56 @@ | ||
[{ | ||
"qid": 0, // question id - cannot be repeated | ||
"question":"How do you say 'name' in English?", // required | ||
"qpicture": "/link/image.jpg", // optional img to go along w question | ||
"qtype": "oneAns", // either: "oneAns", "typedAns", "matchAns", "dragOneAns", "None" | ||
"corrAnswer":["Nama", ""], // correct answer - may have alternate answer | ||
// "corrAnsWiki": "Synopsis of wikipedia page", /// possible implementation | ||
"corrAnsDesc": "Description of the answer", // elaboration of ans | ||
"othAnswer1": "Ans1", // other incorrect answers | ||
"othAnswer2": "Ans2", | ||
"othAnswer3": "Ans3" | ||
}, | ||
{ | ||
"qid": 1, // question id - cannot be repeated | ||
"question":"How do you say 'name' in English?", // required | ||
"qpicture": "/link/image.jpg", // optional img to go along w question | ||
"qtype": "oneAns", // either: "oneAns", "typedAns", "matchAns", "dragOneAns", "None" | ||
"corrAnswer":["Nama", ""], // correct answer - may have alternate answer | ||
// "corrAnsWiki": "Synopsis of wikipedia page", /// possible implementation | ||
"corrAnsDesc": "Description of the answer", // elaboration of ans | ||
"othAnswer1": "Ans1", // other incorrect answers | ||
"othAnswer2": "Ans2", | ||
"othAnswer3": "Ans3" | ||
} | ||
] | ||
"qid": "0", | ||
"question": "How do you say 'name' in English", | ||
"qpicture": "linkimage.jpg", | ||
"qtype": "quizAns", | ||
"corrAnsDesc": "Description of the answer", "othAnswer1": "Ans1", | ||
"Answer1":"Nama", | ||
"Answer2": "Ans2", | ||
"Answer3": "Ans3", | ||
"Answer4": "Ans4", | ||
"corrAnsInt": 1 | ||
},{ | ||
"qid": "1", | ||
"question": "How do you say 'name' in English", | ||
"qpicture": "linkimage.jpg", | ||
"qtype": "quizAns", | ||
"corrAnsDesc": "Description of the answer", "othAnswer1": "Ans1", | ||
"Answer1":"Nama", | ||
"Answer2": "Ans2", | ||
"Answer3": "Ans3", | ||
"Answer4": "Ans4", | ||
"corrAnsInt": 1 | ||
},{ | ||
"qid": "3", | ||
"question": "How do you say 'name' in English", | ||
"qpicture": "linkimage.jpg", | ||
"qtype": "quizAns", | ||
"corrAnsDesc": "Description of the answer", "othAnswer1": "Ans1", | ||
"Answer1":"Nama", | ||
"Answer2": "Ans2", | ||
"Answer3": "Ans3", | ||
"Answer4": "Ans4", | ||
"corrAnsInt": 1 | ||
},{ | ||
"qid": "4", | ||
"question": "How do you say 'name' in English", | ||
"qpicture": "linkimage.jpg", | ||
"qtype": "quizAns", | ||
"corrAnsDesc": "Description of the answer", "othAnswer1": "Ans1", | ||
"Answer1":"Nama", | ||
"Answer2": "Ans2", | ||
"Answer3": "Ans3", | ||
"Answer4": "Ans4", | ||
"corrAnsInt": 1 | ||
},{ | ||
"qid": "5", | ||
"question": "How do you say 'name' in English", | ||
"qpicture": "linkimage.jpg", | ||
"qtype": "quizAns", | ||
"corrAnsDesc": "Description of the answer", "othAnswer1": "Ans1", | ||
"Answer1":"Nama", | ||
"Answer2": "Ans2", | ||
"Answer3": "Ans3", | ||
"Answer4": "Ans4", | ||
"corrAnsInt": 1 | ||
}] |