Guest User

CITS3403

a guest
May 19th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. $(document).ready(function () {
  2. document.getElementById("attemptNo").value = "{{ attemptsList[-1] }}";
  3. $(".bubble ul:not(.attempt{{attemptsList[-1]}})").addClass("is-hidden");
  4. $(".attempt{{attemptsList[-1]}}").removeClass("is-hidden");
  5. $(".attemptBlock").removeClass("is-hidden");
  6. var marks = {{ marksList| safe}};
  7. document.getElementById("attemptMark").innerHTML = marks[marks.length - 1];
  8. var num = marks[marks.length - 1] / {{ noQuizQuestions }} * 100;
  9. document.getElementById("attemptPercent").innerHTML = getMark(num);
  10.  
  11. apiRequestHandler();
  12. });
  13.  
  14. function updateVisibleAttempts(apiAttempts) {
  15. console.log(apiAttempts)
  16. var mapObj = { '"': "'", "'": '"' }
  17. apiAttempts = apiAttempts.replace(/"|'/gi, function (matched) {
  18. return mapObj[matched];
  19. });
  20. var jsonObj = JSON.parse(apiAttempts);
  21. var visibleAttempts = {};
  22. // update feedback, marks and question
  23.  
  24. $("ul.block").each(function (index, item) {
  25. var attemptId = item.classList[2];
  26.  
  27. var feedbackBlock = $(item).find("div span.feedback")[0];
  28. if (jsonObj[attemptId]['feedback'] != feedbackBlock.innerHTML) {
  29. feedbackBlock.innerHTML = jsonObj[attemptId]['feedback']
  30. };
  31.  
  32. var markBlock = $(item).find("div.markDisplay")[0];
  33. var mark = markBlock.classList[2];
  34. var questionBlock = $(item).find("div.questionBlock")[0]
  35. if (jsonObj[attemptId]["mark"] != mark) {
  36. if (jsonObj[attemptId]["mark"] == 0) {
  37. markBlock.innerHTML = '<span class="incorrect"> Incorrect </span>';
  38. questionBlock.innerHTML = `<li> Question: ` + jsonObj[attemptId]['question'] + `</li>
  39.  
  40. <li>&emsp;Answer: ` + jsonObj[attemptId]["answer"] + ` </li>
  41. <li>&emsp;Submitted: ` + jsonObj[attemptId]["ansSubmit"] + ` </li>
  42.  
  43. </div>`
  44. } else {
  45. markBlock.innerHTML = '<span class="correct"> Correct! </span>'
  46. questionBlock.innerHTML = `<li> Question: ` + jsonObj[attemptId]['question'] + `</li>
  47.  
  48. <li>&emsp;Submitted: ` + jsonObj[attemptId]["ansSubmit"] + ` </li>
  49.  
  50. </div>`
  51. };
  52. };
  53.  
  54. });
  55. };
  56.  
  57. function retrieveNewAttempts(token) {
  58. var xhttp = new XMLHttpRequest();
  59. xhttp.open("GET", "/api/quizAttempts/{{current_user}}/{{ quiz.quizName }}", true)
  60. xhttp.setRequestHeader('Authorization', 'Bearer ' + token);
  61. xhttp.onload = function () {
  62. var status = xhttp.status;
  63. if (status == 200) {
  64. var jsonObj = JSON.parse(this.responseText);
  65. console.log(jsonObj)
  66. updateVisibleAttempts(jsonObj);
  67. } else {
  68. console.log("Error", statusText);
  69. }
  70. };
  71.  
  72. xhttp.send()
  73. };
  74.  
  75. function retrieveToken() {
  76. return new Promise(function (resolve, reject) {
  77. var xhttp1 = new XMLHttpRequest();
  78. xhttp1.open("POST", "/api/tokens", true)
  79. xhttp1.setRequestHeader('Authorization', 'Basic ' + btoa("{{current_user}}:{{current_user.password}}"));
  80. xhttp1.onload = function () {
  81. var status = xhttp1.status;
  82. if (status == 200) {
  83. var jsonObj = JSON.parse(this.responseText);
  84. resolve(jsonObj["token"])
  85. } else {
  86. reject(status)
  87. };
  88. };
  89. xhttp1.send()
  90. });
  91.  
  92. };
  93.  
  94. async function apiRequestHandler() {
  95. var token = await retrieveToken()
  96. setInterval(retrieveNewAttempts(token), 10000) //need to change this call
  97. };
Add Comment
Please, Sign In to add comment