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
     *
     *
    * @param  {string} startDate The date of the earliest attempts to return
    * @param  {string} endDate The date of the latest attempts to return
     * @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(startDate, endDate)
     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,
         //            "username": "john-williams",
         //            "submissionTimestamp": "2022-12-30T16:45:46.785Z",
         //            "submissionTimestamp": "2021-09-27T02:03:00Z",
         //            "submissionIntervalSince1970": 1672418746.784748,
         //             "filename": "main.swift",
        //            "sourceFiles": [
         //             "sourceCode": "print(\"Hello, World!\")"
        //                              {
         //        }
        //                                  "contents": "print(\"Main, world!\")}",
         //                                 "path": "main.swift"
        //                              },
         //                               {
        //                                  "contents": "print(\"Main 2, world!\")}",
        //                                  "path": "main2.swift"
         //                               }
        //                              ]
         //          }
         //    ]
         //    ]
         // };
         // }  
       
         // Extract the headers
         // 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 earliestTimestamp = undefined;
         let ignoreAtOrBeforeTimeIntervalSince1970 = undefined;
       
         if ((ignoreAtOrBeforeTimeIntervalSince1970 = sessionStorage.getItem("ignoreAtOrBeforeTimeIntervalSince1970")) && ignoreAtOrBeforeTimeIntervalSince1970 != "0") {
         if ((earliestTimestamp = sessionStorage.getItem("earliestTimestamp")) && earliestTimestamp != "0") {
             url += "?ignoreAtOrBeforeTimeIntervalSince1970=" + ignoreAtOrBeforeTimeIntervalSince1970;
             url += "?earliestTimestamp=" + earliestTimestamp;
         }
         }
       
 
         // 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;


console.log(url);
         attempts.sort(function (left, right) {
console.log(attempts);
             const leftInterval = left.submissionIntervalSince1970;
       
             const rightInterval = right.submissionIntervalSince1970;
         attempts.sort(function (f, s) {
            // Convert into timestamps
             const fDate = Date.parse(f.submissionTimestamp);
             const sDate = Date.parse(s.submissionTimestamp);
              
              
             if (fDate > sDate) {
             if (leftInterval > rightInterval) {
                 return 1;
                 return 1;
             } else if (fDate < sDate) {
             } else if (leftInterval < rightInterval) {
                 return -1;
                 return -1;
             } else {
             } else {
Line 80: Line 80:
         let attempts = await getAttempts();
         let attempts = await getAttempts();
          
          
         let earliestTimestamp = sessionStorage.getItem('earliestTimestamp') || 0;
         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];
          // If it's before the time stamp, skip it
            if (Date.parse(sessionStorage.getItem('earliestTimestamp')) >= Date.parse(attempt.submissionTimestamp)) {
                earliestTimestamp = attempt.submissionTimestamp;
                continue;
            }
              
              
             // Update timestamp
             // Update time interval
             if (Date.parse(attempt.submissionTimestamp) > Date.parse(earliestTimestamp)) {
             if (attempt.submissionIntervalSince1970 > ignoreAtOrBeforeTimeIntervalSince1970) {
                 earliestTimestamp = attempt.submissionTimestamp;
                 ignoreAtOrBeforeTimeIntervalSince1970 = attempt.submissionIntervalSince1970;
             }
             }
           
 
             // Write
             const localTimestamp = new Date(attempt.submissionTimestamp);
            // let attemptElem = `
             const localFormattedTimestamp = localTimestamp.toLocaleDateString() + " " + localTimestamp.toLocaleTimeString();
             // <div class="merlin-code-explorer-container" data-lang="swift" data-experience-id="${attempt.experienceID}" style="display: none;">
 
             //     <div class="merlin-code-explorer-banner">
             // Create text areas for each file present in the submission
             //        <img class="merlin-code-explorer-banner-merlin-icon" src="/wiki/resources/assets/MerlinRoundIcon.png">
            let sourceFiles = "";
            //        <span class="merlin-code-explorer-banner-text">CoderMerlin™ Code Explorer: ${attempt.experienceID} (${attempt.date})</span>
             attempt.sourceFiles.forEach( submission => {
            //        <img class="merlin-code-explorer-banner-language-icon" src="/wiki/images/thumb/3/3b/Swift-og.png/600px-Swift-og.png">
                sourceFiles += `<h4>${submission.path}</h4>`;
            //    </div>
                sourceFiles += `<textarea rows="5">${submission.contents}</textarea>`;
            //    <div class="merlin-code-explorer-code-panel">
             });
            //        <textarea class="merlin-code-explorer-default">${attempt.sourceCode}</textarea>
             //    </div>
            // </div>
            // `;
              
              
             let $attemptElem = $(`
             let $attemptElem = $(`
             <div class="card card-body merlin-code-beta-thing" style="display: none;">
             <div class="card card-body" style="display: none;">
             <h2 class="card-title">${attempt.username} | ${attempt.experienceID} (Exercise ID: ${attempt.exerciseID})</h2>
             <h3 class="card-title">${attempt.username} | ${attempt.experienceID} Exercise: ${attempt.exerciseID} <span class="submission-time">${localFormattedTimestamp}</span></h3>
            <textarea rows="5">${attempt.sourceCode}</textarea>
            ${sourceFiles}
             </div>
             </div>
             `);
             `);
Line 124: Line 114:
         }
         }
          
          
         sessionStorage.setItem('earliestTimestamp', earliestTimestamp);
         sessionStorage.setItem('ignoreAtOrBeforeTimeIntervalSince1970', ignoreAtOrBeforeTimeIntervalSince1970);
     }
     }
      
      
Line 152: Line 142:
                  
                  
                 // Clear the cache on the first go
                 // Clear the cache on the first go
                 sessionStorage.removeItem('earliestTimestamp');
                 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>

Revision as of 15:52, 30 December 2022