-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (127 loc) · 4.03 KB
/
index.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
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
// Define the array of exercise SSNs
let exerciseSSNs = []; // e.g. ["00000-26001",...,"00000-26010"]
// User ID and section ID
let userId = "yourUserId";
let sectionId = "yourSectionId";
// Option mapping
let optionMapping = {
1: "A",
2: "B",
3: "C",
4: "D",
5: "E",
6: "F",
};
// Function to fetch submissions and check correctness
async function fetchSubmissionsAndCheck(exSSNs, userId, sectionId) {
let submissions = {};
for (let exSSN of exSSNs) {
try {
let data = await $.ajax({
data: {
exssn: exSSN,
userId: userId,
sectionId: sectionId,
approvalsOnly: false,
},
url: "/codelab/api/submissions",
contentType: "application/json",
suppressGlobalErrorHandler: true,
});
// Filter and process only correct submissions
let correctSubmissions = data
.filter((submission) => submission.isCorrect)
.map((submission) => {
return {
id: submission.id,
exerciseSSN: submission.exerciseSSN,
isCorrect: submission.isCorrect,
submissionText: submission.submissionText,
};
});
submissions[exSSN] = correctSubmissions;
} catch (error) {
console.error(`Error fetching submissions for ${exSSN}:`, error);
}
}
return submissions;
}
// Define the callback function to process the XML response
function handleInstructions(xml) {
let questions = {};
$(xml)
.find("Exercise")
.each(function () {
let ssn = $(this).attr("SSN");
let question = $(this)
.text()
.replace(/<\s*br[^>]?>/, "")
.replace(/(<([^>]+)>)/g, "")
.replace(/\s+/g, " ")
.trim();
questions[ssn] = question;
});
return questions;
}
// Function to fetch questions using the provided handleInstructions callback
async function fetchQuestions(exSSNs) {
return new Promise((resolve, reject) => {
let options = {
args: { exSSNs: exSSNs },
callback: function (xml) {
try {
let questions = handleInstructions(xml);
resolve(questions);
} catch (error) {
reject(error);
}
},
dataType: "xml",
};
TCAPI.getInstructions(options);
});
}
// Function to clean up submission text and map options
function cleanSubmissionText(text) {
return text
.replace(/currentPage=[^:]+::::/g, "")
.replace(/c(\d)=([^:]+)::::c\1isCorrect=true::::/g, (match, p1, p2) => {
return optionMapping[p2] ? optionMapping[p2] : p2;
})
.replace(/::::/g, "")
.trim();
}
// Fetch submissions, check correctness, fetch questions, and combine them
async function fetchAndCombine(exSSNs, userId, sectionId) {
try {
let submissions = await fetchSubmissionsAndCheck(exSSNs, userId, sectionId);
let questions = await fetchQuestions(exSSNs);
let combinedResults = exSSNs.map((exSSN, index) => {
let correctSubmissions = submissions[exSSN] || [];
let question = questions[exSSN] || "Question not found";
return {
index: index + 1,
exSSN: exSSN,
question: question,
submissions: correctSubmissions,
};
});
// Create and download the markdown file with the combined information
let mdContent = combinedResults
.map((result) => {
return `## Question ${result.index} (${result.exSSN})\n\`\`\`\n${result.question}\n\`\`\`\n## Submissions:\n\`\`\`java\n${result.submissions.map((submission) => cleanSubmissionText(submission.submissionText)).join("\n\n")}\n\`\`\`\n`;
})
.join("\n");
let blob = new Blob([mdContent], { type: "text/markdown;charset=utf-8" });
let link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "submissions_with_questions.md";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch (error) {
console.error("Error in fetchAndCombine:", error);
}
}
// Call the function to fetch and combine data
fetchAndCombine(exerciseSSNs, userId, sectionId);