Skip to content

Commit

Permalink
Update assignments.html
Browse files Browse the repository at this point in the history
  • Loading branch information
donutStudio authored Jul 14, 2024
1 parent 2d55583 commit f3a0a60
Showing 1 changed file with 73 additions and 37 deletions.
110 changes: 73 additions & 37 deletions assignments.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ <h2>Upload Assignment File</h2>

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);
Expand Down Expand Up @@ -180,22 +185,40 @@ <h2>Upload Assignment File</h2>
};

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];
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
}
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;
}
return expression;
}
});

// 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) {
Expand All @@ -217,28 +240,40 @@ <h2>Upload Assignment File</h2>
}

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 => '<label><input type="radio" name="multiple-choice-' + uniqueQuestionId + '"> ' + option + '</label>').join('<br>');
return '<div>' + choicesHTML + '</div>';
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(',');

// Evaluate and simplify each option
options = options.map(option => {
option = option.trim();
if (option.startsWith('$') && option.endsWith('$')) {
// Content between $ signs should be rendered as LaTeX noneditable MathQuill field
var mathContent = option.substring(1, option.length - 1).trim();
return '<span class="mathquill-noneditable">' + mathContent + '</span>';
} else {
return evaluateAndSimplifyExpression(option, variables);
}
return '';
});

var randomizeIndex = typeValue.indexOf('randomize=true');
if (randomizeIndex !== -1) {
options = shuffleArray(options);
}

var choicesHTML = options.map(option => '<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));
Expand Down Expand Up @@ -268,13 +303,16 @@ <h2>Upload Assignment File</h2>
handlers: {
edit: function() {
var enteredMath = mathField.latex();
console.log('Current LaTeX: ' + enteredMath);
}
}
});
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('-');
Expand Down Expand Up @@ -305,8 +343,6 @@ <h2>Upload Assignment File</h2>
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 {
Expand Down

0 comments on commit f3a0a60

Please sign in to comment.