Skip to content

Commit

Permalink
Completed the quiz controller, now named as app.js
Browse files Browse the repository at this point in the history
Check Todo comments for further issue
  • Loading branch information
Skarvion committed Jan 25, 2017
1 parent 39fe3ed commit a537ea3
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 38 deletions.
6 changes: 6 additions & 0 deletions css/bootstrap.min.css

Large diffs are not rendered by default.

28 changes: 23 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="id" data-ng-app="quizApp">
<html lang="id" ng-app="gameApp">
<head>
<title></title>
<meta charset="utf-8" />
Expand All @@ -15,14 +15,32 @@
<script src="js/respond.min.js"></script>
<![endif]-->
</head>
<body data-ng-app="quizCtrl">
<div data-ng-view></div>

<body ng-controller="MCQGameCtrl">
<!-- <div data-ng-view></div>
<p ng-bind="variable"></p>
-->
<ul>
<li data-ng-repeat="question in quizQuestions">
{{question.qid}} </br>
{{question.question}}
<form action="">
<input type="radio" name="select1" ng-model="question.selectedAnswer" ng-disabled="question.isDisabled" value="1">{{question.Answer1}}</input></br>
<input type="radio" name="select2" ng-model="question.selectedAnswer" ng-disabled="question.isDisabled" value="2">{{question.Answer2}}</input></br>
<input type="radio" name="select3" ng-model="question.selectedAnswer" ng-disabled="question.isDisabled" value="3">{{question.Answer3}}</input></br>
<input type="radio" name="select4" ng-model="question.selectedAnswer" ng-disabled="question.isDisabled" value="4">{{question.Answer4}}</input></br>
<input type="button" ng-click="checkAnswer(question)" ng-disabled="question.isDisabled" value="Submit" />
</form>
{{question.status}}
</li>
</ul>

<!-- jQuery - required for Bootstrap's JavaScript plugins) -->
<script src="js/jquery.min.js"></script>
<!-- All Bootstrap plug-ins file -->
<script src="js/bootstrap.min.js"></script>
<!- Basic AngularJS -->
<!-- Basic AngularJS -->
<script src="js/angular.min.js"></script>
<script src="js/angular-route.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions js/angular-route.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions js/app.js
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);
}]);
24 changes: 15 additions & 9 deletions js/quizAnsApp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var app = angular.module('myApp', ['ngRoute', 'bootstrap-modal', 'ngSanitize', 'ngCsv']);
var app = angular.module('gameApp', ['ngRoute', 'bootstrap-modal', 'ngSanitize', 'ngCsv']);


// Todo: Edit the url page later to accomodate with the other people's work
Expand All @@ -24,13 +24,13 @@ app.config(['$routeProvider', function ($routeProvider) {
templateUrl: "templates/Items.html"
, controller: "itemsCtrl"
});
}]);
}]);

// Factory methods that acts as the global variable for the score and its functions to get, modify and clear score.
// All actions that relating with the scoring method must call from this factory methods

app.factory('ScoreTracker', function() {
var currentScore;
var currentScore = 0;
var ScoreTracker = [];

ScoreTracker.getScore = function() {
Expand All @@ -49,12 +49,13 @@ app.factory('ScoreTracker', function() {
currentScore = 0;
}

return ScoreTracker;
});

app.factory('JSONData', ['$http', function ($http) {
var JSONData = [];
var quizURL = "/link";
var matchURL = "/link";
var quizURL = "json/quizdata.json";
var matchURL = "json/matchquizdata.json";

JSONData.getQuiz = function() {
return $http.get(quizURL);
Expand All @@ -64,12 +65,15 @@ app.factory('JSONData', ['$http', function ($http) {
return $http.get(matchURL);
}

return JSONData;

}]);

app.controller('multipleChoiceGameCtrl', function ($scope, $http) {
var questionID = [];
var $scope.rawQuestions = []; // The raw array of questions that have yet to be processed
var $scope.quizQuestions = [];
alert("Work");

$scope.rawQuestions = []; // The raw array of questions that have yet to be processed
$scope.quizQuestions = [];

// Selecting a random number of questions from the database
function questionSelect(quantity) {
Expand Down Expand Up @@ -102,6 +106,7 @@ app.controller('multipleChoiceGameCtrl', function ($scope, $http) {
for (var j = 0; j < selectedQuestionIndex.length; j++) {
if (rand == selectedQuestionIndex[j]) {
duplicate = true;
restart = true;
break;
}
}
Expand Down Expand Up @@ -189,4 +194,5 @@ app.controller('multipleChoiceGameCtrl', function ($scope, $http) {
}


}
}

79 changes: 55 additions & 24 deletions json/quizdata.json
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
}]

0 comments on commit a537ea3

Please sign in to comment.