Difference between revisions of "Widget:CodeExplorerGroupGetSubmissions"
From Coder Merlin
(2 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
* Make the API request and return the sorted list of attempts | * Make the API request and return the sorted list of attempts | ||
* | * | ||
* @return {Promise<Array>} The attempts sorted with the newest attempts first | * @return {Promise<Array>} The attempts sorted with the newest attempts first | ||
*/ | */ | ||
async function getAttempts( | async function getAttempts() | ||
{ | { | ||
// Fetch the data | // Fetch the data | ||
Line 16: | Line 14: | ||
// submissions: [ | // submissions: [ | ||
// { | // { | ||
// "username": "john-williams", | |||
// "experienceID": "W1037", | // "experienceID": "W1037", | ||
// "exerciseID": 12, | // "exerciseID": 12, | ||
// " | // "submissionTimestamp": "2022-12-30T16:45:46.785Z", | ||
// " | // "submissionIntervalSince1970": 1672418746.784748, | ||
// | // "sourceFiles": [ | ||
// | // { | ||
// } | // "contents": "print(\"Main, world!\")}", | ||
// "path": "main.swift" | |||
// }, | |||
// { | |||
// "contents": "print(\"Main 2, world!\")}", | |||
// "path": "main2.swift" | |||
// } | |||
// ] | |||
// } | |||
// ] | // ] | ||
// } | // } | ||
// | // Execute the request | ||
let $dataDiv = $('#code-explorer-group-data'); | let $dataDiv = $('#code-explorer-group-data'); | ||
var url = languageServerURL(); | |||
url += `codeExplorerGroups/guides/${$dataDiv.data('username')}/${$dataDiv.data('group-id')}`; | |||
let | let ignoreAtOrBeforeTimeIntervalSince1970 = undefined; | ||
if ((ignoreAtOrBeforeTimeIntervalSince1970 = sessionStorage.getItem("ignoreAtOrBeforeTimeIntervalSince1970")) && ignoreAtOrBeforeTimeIntervalSince1970 != "0") { | |||
url += "?ignoreAtOrBeforeTimeIntervalSince1970=" + ignoreAtOrBeforeTimeIntervalSince1970; | |||
if (( | |||
url += "? | |||
} | } | ||
// Fetch the data | // Fetch the data | ||
let data = await $.ajax(url, { | let data = await $.ajax(url, { | ||
Line 51: | Line 55: | ||
let attempts = data.submissions; | let attempts = data.submissions; | ||
attempts.sort(function (left, right) { | |||
const leftInterval = left.submissionIntervalSince1970; | |||
const rightInterval = right.submissionIntervalSince1970; | |||
attempts.sort(function ( | |||
const | |||
const | |||
if ( | if (leftInterval > rightInterval) { | ||
return 1; | return 1; | ||
} else if ( | } else if (leftInterval < rightInterval) { | ||
return -1; | return -1; | ||
} else { | } else { | ||
Line 80: | Line 80: | ||
let attempts = await getAttempts(); | let attempts = await getAttempts(); | ||
let | let ignoreAtOrBeforeTimeIntervalSince1970 = sessionStorage.getItem('ignoreAtOrBeforeTimeIntervalSince1970') || 0; | ||
// We now have the attempts as an array | // We now have the attempts as an array | ||
Line 86: | Line 86: | ||
// Get the attempt | // Get the attempt | ||
const attempt = attempts[i]; | const attempt = attempts[i]; | ||
// Update time interval | |||
if (attempt.submissionIntervalSince1970 > ignoreAtOrBeforeTimeIntervalSince1970) { | |||
ignoreAtOrBeforeTimeIntervalSince1970 = attempt.submissionIntervalSince1970; | |||
} | |||
const localTimestamp = new Date(attempt.submissionTimestamp); | |||
if ( | const localFormattedTimestamp = localTimestamp.toLocaleDateString() + " " + localTimestamp.toLocaleTimeString(); | ||
// Create text areas for each file present in the submission | |||
let sourceFiles = ""; | |||
attempt.sourceFiles.forEach( submission => { | |||
sourceFiles += `<h4>${submission.path}</h4>`; | |||
sourceFiles += `<textarea rows="5" class="source">${submission.contents}</textarea>`; | |||
}); | |||
let supplementalOutput = ""; | |||
if (typeof attempt.compilationStatus != 'undefined' && typeof attempt.compilationStatus.terminationStatus == 'number' && attempt.compilationStatus.terminationStatus != 0) { | |||
if (typeof attempt.compilationStatus.standardOutput == 'string') { | |||
supplementalOutput += `<textarea rows="5" class="compilation-status-standard-output">${attempt.compilationStatus.standardOutput}</textarea>`; | |||
} | |||
if (typeof attempt.compilationStatus.standardError == 'string') { | |||
supplementalOutput += `<textarea rows="5" class="compilation-status-standard-error">${attempt.compilationStatus.standardError}</textarea>`; | |||
} | |||
} | } | ||
if (typeof attempt.executionStatus != 'undefined') { | |||
if (typeof attempt.executionStatus.standardOutput == 'string') { | |||
if ( | supplementalOutput += `<textarea rows="5" class="execution-status-standard-output">${attempt.executionStatus.standardOutput}</textarea>`; | ||
} | |||
if (typeof attempt.executionStatus.standardError == 'string' && typeof attempt.executionStatus.terminationStatus == 'number' && attempt.executionStatus.terminationStatus != 0) { | |||
supplementalOutput += `<textarea rows="5" class="execution-status-standard-error">${attempt.executionStatus.standardError}</textarea>`; | |||
} | |||
} | } | ||
let $attemptElem = $(` | let $attemptElem = $(` | ||
<div class="card card-body | <div class="card card-body" style="display: none;"> | ||
< | <h3 class="card-title">${attempt.username} | ${attempt.experienceID} Exercise: ${attempt.exerciseID} <span class="submission-time">${localFormattedTimestamp}</span></h3> | ||
${sourceFiles} | |||
${supplementalOutput} | |||
</div> | </div> | ||
`); | `); | ||
Line 124: | Line 132: | ||
} | } | ||
sessionStorage.setItem(' | sessionStorage.setItem('ignoreAtOrBeforeTimeIntervalSince1970', ignoreAtOrBeforeTimeIntervalSince1970); | ||
} | } | ||
Line 152: | Line 160: | ||
// Clear the cache on the first go | // Clear the cache on the first go | ||
sessionStorage.removeItem(' | sessionStorage.removeItem('ignoreAtOrBeforeTimeIntervalSince1970'); | ||
// Start the loop | // Start the loop | ||
Line 168: | Line 176: | ||
</script> | </script> | ||
<div id="attempts-wrapper"></div> | <div id="attempts-wrapper" class="merlin-code-explorer-submissions"></div> | ||
</includeonly> | </includeonly> |