-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquizParse.js
More file actions
180 lines (149 loc) · 4.75 KB
/
quizParse.js
File metadata and controls
180 lines (149 loc) · 4.75 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
document.getElementById("submit").onclick = submit;
var name2num = {
multi : 0,
number : 1,
text : 2
}
function submit() {
var questionTexts = document.getElementById("something").value.split("Question");
var headerText = questionTexts[0]
var headerLines = headerText.split("\n");
var QuizNameArray = headerLines[0].split(":");
var quizName = QuizNameArray[1].trim();
var QuizCodeArray = headerLines[1].split(":");
var quizCode = QuizCodeArray[1];
insertQuiz(quizName, quizCode, questionTexts);
}
function submitQuestions(quizName, quizCode, questionTexts) {
var questions = [];
for (var i = 1; i < questionTexts.length; i++) {
var question = {};
var questionLines = questionTexts[i].split("\n");
var currentIndex = 2;// the first element after <body> tag
var bodyText = "";
while (questionLines[++currentIndex].indexOf("<BodyEnd>") == -1) {
bodyText = bodyText + questionLines[currentIndex]+"\n";
}
question["body"] = bodyText;
var modelAnswerText = "";
currentIndex++;
while (questionLines[++currentIndex].indexOf("<ModelAnswerEnd>") == -1) {
modelAnswerText = modelAnswerText + questionLines[currentIndex]+"\n";
}
question["modelanswer"] = modelAnswerText;
// skip past body end
currentIndex++;
while (currentIndex < questionLines.length) {
var line = questionLines[currentIndex];
var lineArray = line.split(":");
if (lineArray.length < 2) {
question[lineArray[0].trim()] = "";
} else {
// if it's an image combine the first and second part
question[lineArray[0].trim()] = lineArray[1].trim();
if (lineArray[0].indexOf("Image Name") != -1) {
if (lineArray.length > 2) {
question[lineArray[0].trim()] = lineArray[1].trim()
+ ":" + (lineArray[2].trim());
}
}
}
currentIndex++;
}
questions.push(question);
}
for (var k = 0; k < questions.length; k++) {
var q = questions[k];
var type = parseInt(name2num[q["Type"]]);
// check if q is multi
// q["Correct Answer"] needs ot be formatted into lower case for text
// entry questions
var originalText = q["Correct Answer"];
originalText = originalText.toLowerCase();
originalText = originalText.trim();
q["Correct Answer"] = originalText;
if (q["Possible Answers"] != "") {
insertPossibleAnswers(q, quizName);
// TODO search for possible answer id
// q["Possible Answers"] =
} else {
// convert type
q["Possible Answers"] = -1;
insert(q["body"], q["Possible Answers"], q["Correct Answer"], type,
q["Tolerance"], quizName, q["Image Name"], q["modelanswer"]);
}
}
}
// use ajax function to insert into databsae - question
function insert(body, panswerid, canswer, type, tolerance, quizname, imagename,modelanswer) {
// call PHP function
$.ajax({
url : 'http://shrouded-earth-7234.herokuapp.com/processQuizEntry.php',
type : 'post',
data : {
"funcName" : "InsertQuestion",
"quizname" : quizname,
"body" : body,
"canswer" : canswer,
"type" : type,
"tolerance" : tolerance,
"imagename" : imagename,
"panswerid" : panswerid,
"modelanswer" : modelanswer
},
success : function(response) {
if (response.indexOf("success") == -1) {
alert("question not added");
} else {
alert("question entered");
}
}
});
}
// Deal with the case of having possible answers,
// We must insert the possible answers first, then search for the id
function insertPossibleAnswers(possibleAnswerQuestion, quizName) {
// TODO
var panswerLine = possibleAnswerQuestion["Possible Answers"];
var panswerArray = panswerLine.split(",");
$
.ajax({
url : 'http://shrouded-earth-7234.herokuapp.com/insertNewPossibleAnswers.php',
type : 'post',
data : {
"funcName" : "insertNewPossibleAnswers",
"p1" : panswerArray[0],
"p2" : panswerArray[1],
"p3" : panswerArray[2],
"p4" : panswerArray[3],
},
success : function(response) {
var panswerid = parseInt(response);
var type = parseInt(name2num[possibleAnswerQuestion["Type"]]);
insert(possibleAnswerQuestion["body"], panswerid,
possibleAnswerQuestion["Correct Answer"], type,
possibleAnswerQuestion["Tolerance"], quizName,
possibleAnswerQuestion["Image Name"],possibleAnswerQuestion["modelanswer"]);
}
});
}
function insertQuiz(quizname, coursecode, questionTexts) {
$.ajax({
url : 'http://shrouded-earth-7234.herokuapp.com/processQuizEntry.php',
type : 'post',
data : {
"funcName" : "InsertQuiz",
"quizname" : quizname.trim(),
"coursecode" : coursecode.trim()
},
success : function(response) {
if (response.indexOf("success") == -1) {
alert("quiz updated");
submitQuestions(quizname, coursecode, questionTexts);
} else {
alert("quiz entered");
submitQuestions(quizname, coursecode, questionTexts);
}
}
});
}