-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassignments.html
381 lines (325 loc) · 14.3 KB
/
assignments.html
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Assignments - StudyGrind</title>
<!-- Bootstrap CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<style>
/* Custom styles for the color scheme */
body {
background-color: rgb(234, 234, 234);
color: rgb(0, 0, 0);
}
.navbar {
background-color: rgb(0, 0, 0);
}
.navbar-dark .navbar-nav .nav-link {
color: rgb(20, 112, 175); /* Dark blue */
}
.navbar-dark .navbar-nav .nav-link:hover {
color: rgb(142, 182, 220); /* Light blue on hover */
}
.jumbotron {
background-color: rgb(234, 234, 234);
}
.jumbotron h1, .jumbotron p {
color: rgb(20, 112, 175); /* Dark blue */
}
.mq-editable-field {
border: 0.5px solid #ccc;
padding: 5px;
min-height: 40px;
}
.mathquill-container {
margin-top: 20px; /* Adjust this value to move the MathQuill field down */
}
</style>
<!-- MathQuill CSS -->
<link rel="stylesheet" href="./mathquill.css"/> <!-- Relative path for MathQuill CSS -->
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark">
<a class="navbar-brand" href="index.html">Tutoring</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.html">Contact</a>
</li>
<li class="nav-item">
<a class="nav-link" href="lessons.html">Lessons/Booking</a>
</li>
<li class="nav-item">
<a class="nav-link" href="assignments.html">Assignments</a>
</li>
<li class="nav-item">
<a class="nav-link" href="login.html">Sign Up / Log In</a>
</li>
</ul>
</div>
</nav>
<section id="lessons" class="py-5">
<div class="container">
<div class="row">
<div class="col-md-8">
<h2>Upload Assignment File</h2>
<input type="file" id="file" accept=".sga">
<div id="output" class="mt-3"></div>
</div>
</div>
</div>
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<script src="./mathquill.js"></script> <!-- Relative path for MathQuill JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.6.0/math.min.js"></script> <!-- Math.js library for math operations -->
<script>
let correctAnswers = {}; // Store correct answers here
let mathFieldMap = new Map(); // Map to store MathQuill instances
document.getElementById('file').onchange = function() {
var file = this.files[0];
var reader = new FileReader();
reader.onload = function(progressEvent) {
const text = this.result;
var sections = text.split('<section>');
var outputHTML = '';
for (var i = 1; i < sections.length; i++) {
var section = sections[i];
var endIndex = section.indexOf('</section>');
var sectionContent = section.substring(0, endIndex).trim();
var variables = {};
var lines = sectionContent.split('\n');
for (var line = 0; line < lines.length; line++) {
var currentLine = lines[line].trim();
if (currentLine.startsWith('int ')) {
var variableName = currentLine.substring(4, currentLine.indexOf('=')).trim();
var variableValue = evaluateVariable(currentLine.substring(currentLine.indexOf('=') + 1).trim(), variables);
variables[variableName] = variableValue;
}
}
var sectionTitleIndex = sectionContent.indexOf('section-title:');
var sectionTitle = '';
if (sectionTitleIndex !== -1) {
var titleEndIndex = sectionContent.indexOf('\n', sectionTitleIndex);
sectionTitle = sectionContent.substring(sectionTitleIndex + 14, titleEndIndex).trim();
}
var questions = sectionContent.split('<question>');
var questionsHTML = '';
for (var j = 1; j < questions.length; j++) {
var question = questions[j];
var endQuestionIndex = question.indexOf('</question>');
var questionContent = question.substring(0, endQuestionIndex).trim();
var textIndex = questionContent.indexOf('text:');
var correctAnswerIndex = questionContent.indexOf('correct-answer:');
var textValue = questionContent.substring(textIndex + 5, correctAnswerIndex).trim();
var correctAnswer = '';
if (correctAnswerIndex !== -1) {
correctAnswer = questionContent.substring(correctAnswerIndex + 15).trim();
correctAnswer = evaluateAndSimplifyExpression(correctAnswer, variables);
}
textValue = replaceVariables(textValue, variables);
// Handle LaTeX rendering
textValue = textValue.replace(/\$(.*?)(?<! )\$/g, function(match, latexContent) {
return '<span class="mathquill-noneditable">' + latexContent.trim() + '</span>';
});
var uniqueQuestionId = i + '-' + (j - 1);
var answerField = findAnswerField(sectionContent, j - 1);
var answerHTML = generateAnswerHTML(answerField, variables, uniqueQuestionId);
// Store the correct answer
correctAnswers[uniqueQuestionId] = correctAnswer;
var checkButtonHTML = generateCheckButtonHTML(uniqueQuestionId);
questionsHTML += '<p>' + textValue + '</p>' + answerHTML + checkButtonHTML;
}
var sectionHTML = '<div class="subpage">' +
'<h3>' + sectionTitle + '</h3>' +
questionsHTML +
'</div>';
outputHTML += sectionHTML;
}
document.getElementById('output').innerHTML = outputHTML;
initializeMathQuillFields();
};
reader.readAsText(file);
};
function evaluateVariable(expression, variables) {
expression = expression.replace(/\s+/g, '');
if (expression.startsWith('int.random[') && expression.endsWith(']')) {
var options = expression.substring(expression.indexOf('[') + 1, expression.lastIndexOf(']')).split(',');
options = options.map(option => parseInt(option.trim()));
var randomIndex = Math.floor(Math.random() * options.length);
return options[randomIndex];
}
else if (expression.startsWith('int.random')) {
var args = expression.substring(expression.indexOf('(') + 1, expression.indexOf(')')).split(',');
var min = parseInt(args[0].trim());
var max = parseInt(args[1].trim());
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Check if the expression contains variables in {}
if (expression.includes("{") && expression.includes("}")) {
expression = expression.replace(/\{(.*?)\}/g, function(match, variableName) {
var variableValue = variables[variableName.trim()];
if (variableValue !== undefined) {
return variableValue.toString(); // Replace with variable value
} else {
return match; // Return original placeholder if variable not found
}
});
// Recursively evaluate again in case there are nested variables
return evaluateVariable(expression, variables);
}
expression = evaluateAndSimplifyExpression(expression, variables);
return expression;
}
function replaceVariables(text, variables) {
return text.replace(/\{\{(.*?)\}\}/g, function(match, variableName) {
return '{' + (variables[variableName.trim()] || match) + '}';
}).replace(/\{(.*?)\}/g, function(match, variableName) {
return variables[variableName.trim()] || match;
});
}
function evaluateAndSimplifyExpression(expression, variables) {
// Replace variables in the expression
expression = replaceVariables(expression, variables);
// Use math.js for evaluation and simplification
try {
return math.evaluate(expression);
} catch (error) {
return expression; // Return original expression on error
}
}
function generateAnswerHTML(answerField, variables, uniqueQuestionId) {
var typeIndex = answerField.indexOf('type:');
var typeValue = answerField.substring(typeIndex + 5).trim();
if (typeValue.startsWith('short-answer')) {
return '<div class="mathquill-container"><span id="answer-' + uniqueQuestionId + '" class="mathquill-editable mq-editable-field"></span></div>';
} else if (typeValue.startsWith('multiple-choice')) {
var optionsStartIndex = typeValue.indexOf('[') + 1;
var optionsEndIndex = typeValue.indexOf(']');
var optionsString = typeValue.substring(optionsStartIndex, optionsEndIndex);
var options = optionsString.split(',').map(option => evaluateAndSimplifyExpression(option.trim(), variables));
var randomizeIndex = typeValue.indexOf('randomize=true');
if (randomizeIndex !== -1) {
options = shuffleArray(options);
}
var choicesHTML = options.map(option => {
// Ensure option is a string before splitting
if (typeof option === 'string') {
var rawAnswer = option;
var parts = option.split('$');
var renderedOption = parts.map((part, index) => {
if (index % 2 === 1) {
return '<span class="mathquill-noneditable">' + part + '</span>';
} else {
return part;
}
}).join('');
return '<label><input type="radio" name="multiple-choice-' + uniqueQuestionId + '"> ' + renderedOption + '</label>';
} else {
// Handle non-string options gracefully
return '<label><input type="radio" name="multiple-choice-' + uniqueQuestionId + '"> ' + option + '</label>';
}
}).join('<br>');
return '<div>' + choicesHTML + '</div>';
}
return '';
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function findAnswerField(sectionContent, questionId) {
var answerFieldRegex = new RegExp('<answer-field>\\s*id: ' + questionId + '.*?<\\/answer-field>', 's');
var match = sectionContent.match(answerFieldRegex);
if (match) {
return match[0];
}
return null;
}
function generateCheckButtonHTML(uniqueQuestionId) {
return '<button type="button" class="btn btn-primary btn-sm mt-2" id="check-button-' + uniqueQuestionId + '">Check Answer</button>';
}
function initializeMathQuillFields() {
var MQ = MathQuill.getInterface(2);
document.querySelectorAll('.mathquill-editable').forEach(function(field) {
var mathField = MQ.MathField(field, {
spaceBehavesLikeTab: true,
handlers: {
edit: function() {
var enteredMath = mathField.latex();
}
}
});
mathFieldMap.set(field.id, mathField); // Store MathQuill instance in Map
});
document.querySelectorAll('.mathquill-noneditable').forEach(function(field) {
MQ.StaticMath(field);
});
document.querySelectorAll('button[id^="check-button-"]').forEach(function(button) {
button.addEventListener('click', function() {
var uniqueQuestionId = this.id.split('-').slice(2).join('-');
checkAnswer(uniqueQuestionId);
});
});
}
function checkAnswer(uniqueQuestionId) {
var answerType = document.querySelector('input[name="multiple-choice-' + uniqueQuestionId + '"]') ? 'multiple-choice' : 'short-answer';
if (answerType === 'multiple-choice') {
var correctAnswer = correctAnswers[uniqueQuestionId];
var selectedAnswer = document.querySelector('input[name="multiple-choice-' + uniqueQuestionId + '"]:checked');
if (selectedAnswer) {
var selectedAnswerValue = selectedAnswer.nextSibling.textContent.trim();
console.log(selectedAnswerValue)
console.log(correctAnswer)
if (selectedAnswerValue === correctAnswer.toString()) {
alert('Correct!');
} else {
alert('Incorrect. Please select the correct answer.');
}
} else {
alert('Please select an answer.');
}
} else if (answerType === 'short-answer') {
var correctAnswer = correctAnswers[uniqueQuestionId];
var mathField = mathFieldMap.get('answer-' + uniqueQuestionId); // Retrieve MathQuill instance from Map
if (mathField) {
console.log('mathField:', mathField); // Debugging line
console.log('typeof mathField:', typeof mathField); // Debugging line
var enteredAnswer;
try {
enteredAnswer = mathField.latex();
} catch (error) {
console.error('Error accessing latex method:', error);
alert('Error accessing the answer field.');
return;
}
if (enteredAnswer === correctAnswer.toString()) {
alert('Correct!');
} else {
alert('Incorrect. Please enter the correct answer.');
}
} else {
alert('MathQuill field not found.');
}
}
}
</script>
</body>
</html>