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
     *
     *
    * @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');   
        var url = languageServerURL();
        url += `codeExplorerGroups/guides/${$dataDiv.data('username')}/${$dataDiv.data('group-id')}`;
          
          
         let stg = window.location.hostname == "stg.codermerlin.com" ? "-stg" : '';
         let ignoreAtOrBeforeTimeIntervalSince1970 = undefined;
       
         if ((ignoreAtOrBeforeTimeIntervalSince1970 = sessionStorage.getItem("ignoreAtOrBeforeTimeIntervalSince1970")) && ignoreAtOrBeforeTimeIntervalSince1970 != "0") {
        let url = `https://language-server${stg}.codermerlin.com/codeExplorerGroups/guides/${$dataDiv.data('username')}/${$dataDiv.data('group-id')}`;
             url += "?ignoreAtOrBeforeTimeIntervalSince1970=" + ignoreAtOrBeforeTimeIntervalSince1970;
       
        let earliestTimestamp = undefined;
       
         if ((earliestTimestamp = sessionStorage.getItem("earliestTimestamp")) && earliestTimestamp != "0") {
             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];
           
            // Update time interval
            if (attempt.submissionIntervalSince1970 > ignoreAtOrBeforeTimeIntervalSince1970) {
                ignoreAtOrBeforeTimeIntervalSince1970 = attempt.submissionIntervalSince1970;
            }


          // If it's before the time stamp, skip it
            const localTimestamp = new Date(attempt.submissionTimestamp);
             if (Date.parse(sessionStorage.getItem('earliestTimestamp')) >= Date.parse(attempt.submissionTimestamp)) {
            const localFormattedTimestamp = localTimestamp.toLocaleDateString() + " " + localTimestamp.toLocaleTimeString();
                earliestTimestamp = attempt.submissionTimestamp;
 
                 continue;
            // 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') {
            // Update timestamp
                if (typeof attempt.executionStatus.standardOutput == 'string') {
             if (Date.parse(attempt.submissionTimestamp) > Date.parse(earliestTimestamp)) {
                    supplementalOutput += `<textarea rows="5" class="execution-status-standard-output">${attempt.executionStatus.standardOutput}</textarea>`;
                earliestTimestamp = attempt.submissionTimestamp;
                }
                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>`;
                }
             }
             }
           
            // Write
            // let attemptElem = `
            // <div class="merlin-code-explorer-container" data-lang="swift" data-experience-id="${attempt.experienceID}" style="display: none;">
            //    <div class="merlin-code-explorer-banner">
            //        <img class="merlin-code-explorer-banner-merlin-icon" src="/wiki/resources/assets/MerlinRoundIcon.png">
            //        <span class="merlin-code-explorer-banner-text">CoderMerlin™ Code Explorer: ${attempt.experienceID} (${attempt.date})</span>
            //        <img class="merlin-code-explorer-banner-language-icon" src="/wiki/images/thumb/3/3b/Swift-og.png/600px-Swift-og.png">
            //    </div>
            //    <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}
            ${supplementalOutput}
             </div>
             </div>
             `);
             `);
Line 124: Line 132:
         }
         }
          
          
         sessionStorage.setItem('earliestTimestamp', earliestTimestamp);
         sessionStorage.setItem('ignoreAtOrBeforeTimeIntervalSince1970', ignoreAtOrBeforeTimeIntervalSince1970);
     }
     }
      
      
Line 152: Line 160:
                  
                  
                 // 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 176:
</script>
</script>


<div id="attempts-wrapper"></div>
<div id="attempts-wrapper" class="merlin-code-explorer-submissions"></div>
</includeonly>
</includeonly>

Latest revision as of 21:30, 8 March 2023