Difference between revisions of "Widget:CodeExplorerGroupGetSubmissions"
From Coder Merlin
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'); | ||
let stg = window.location.hostname == "stg.codermerlin.com" ? "-stg" : ''; | let stg = window.location.hostname == "stg.codermerlin.com" ? "-stg" : ''; | ||
let url = `https://language-server${stg}.codermerlin.com/codeExplorerGroups/guides/${$dataDiv.data('username')}/${$dataDiv.data('group-id')}`; | let url = `https://language-server${stg}.codermerlin.com/codeExplorerGroups/guides/${$dataDiv.data('username')}/${$dataDiv.data('group-id')}`; | ||
let | let ignoreAtOrBeforeTimeIntervalSince1970 = undefined; | ||
if ((ignoreAtOrBeforeTimeIntervalSince1970 = sessionStorage.getItem("ignoreAtOrBeforeTimeIntervalSince1970")) && ignoreAtOrBeforeTimeIntervalSince1970 != "0") { | |||
if (( | url += "?ignoreAtOrBeforeTimeIntervalSince1970=" + ignoreAtOrBeforeTimeIntervalSince1970; | ||
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 | // Update time interval | ||
if | if (attempt.submissionIntervalSince1970 > ignoreAtOrBeforeTimeIntervalSince1970) { | ||
ignoreAtOrBeforeTimeIntervalSince1970 = attempt.submissionIntervalSince1970; | |||
} | } | ||
const localTimestamp = new Date(attempt.submissionTimestamp); | |||
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">${submission.contents}</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} | |||
</div> | </div> | ||
`); | `); | ||
Line 124: | Line 114: | ||
} | } | ||
sessionStorage.setItem(' | sessionStorage.setItem('ignoreAtOrBeforeTimeIntervalSince1970', ignoreAtOrBeforeTimeIntervalSince1970); | ||
} | } | ||
Line 152: | Line 142: | ||
// 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 158: | ||
</script> | </script> | ||
<div id="attempts-wrapper"></div> | <div id="attempts-wrapper" class="merlin-code-explorer-submissions"></div> | ||
</includeonly> | </includeonly> |