-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquestions-api.js
101 lines (86 loc) · 3.5 KB
/
questions-api.js
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
var questionsService = require('./questions-service');
exports.getQuestionForNumber = function (request, questionNumber) {
var questionToReturn = questionsService.getQuestionForNumber(questionNumber);
if ( questionToReturn) {
questionToReturn.didQuestionChange = false;
if ( request.nevex.isQuizMaster ) {
// Change the question in play
questionToReturn.didQuestionChange = questionsService.setCurrentQuestion(questionNumber);
}
return questionToReturn;
} else {
return { error: "Could not find question "+questionNumber};
}
};
exports.getAnswerForQuestion = function (request, response) {
if ( ! request.nevex.isQuizMaster ) {
return response.status(403).json({"error": "You are not authorized to view answers"});
}
var questionNumber = request.query.number;
if ( questionNumber ) {
var answerResponse = questionsService.getAnswerForQuestion(questionNumber);
if ( answerResponse ) {
return response.status(200).json( { "answer": answerResponse.answer} );
} else {
return response.status(422).json({"error": "Could not find answer to question "+questionNumber});
}
} else {
return response.status(422).json({"error": "You must provide a question number"});
}
};
exports.setQuestions = function (request, response) {
if ( request.nevex.isQuizMaster ) {
var result = questionsService.loadQuestionsFromJson(JSON.stringify(request.body));
if ( !result || result.isError ) {
return response.status(422).json(result);
} else {
return response.status(201).json({"message": "Successfully updated the questions"})
}
} else {
return response.status(403).json({"error": "You are not allowed to set the questions"});
}
};
exports.getCurrentScores = function () {
return questionsService.getCurrentScores();
};
exports.reduceScoreBonus = function (request, response) {
if ( request.nevex.isQuizMaster ) {
// By default we decrease the score...
var newScoreAmount = questionsService.changeAnswersLeftDown();
return response.status(200).json({"newScoreAmount": newScoreAmount});
} else {
return response.status(403).json( { "error": "You are not authorized to reduce rhe score bonus"});
}
};
exports.startQuiz = function (request) {
if ( request.nevex.isQuizMaster ) {
return questionsService.startQuiz();
}
return false; // not authorized
};
exports.stopQuiz = function (request) {
if ( request.nevex.isQuizMaster ) {
return { isStopped: questionsService.stopQuiz() }
} else {
return { error: "You are not authorized to stop the quiz"};
}
};
exports.pauseQuiz = function (request, response) {
if ( request.nevex.isQuizMaster ) {
var isPaused = questionsService.pauseQuiz();
return response.status(200).json({"isPaused": isPaused});
} else {
return response.status(403).json({"error": "You are not authorized to pause the quiz"});
}
};
exports.unPauseQuiz = function (request, response) {
if ( request.nevex.isQuizMaster ) {
var isPaused = questionsService.unPauseQuiz();
return response.status(200).json({"isUnPaused": isPaused});
} else {
return response.status(403).json({"error": "You are not authorized to un-pause the quiz"});
}
};
exports.getStatisticsForQuestion = function (questionNumber) {
return questionsService.getStatisticsForQuestion(questionNumber);
};