Advertisement
Guest User

moyenne

a guest
Dec 9th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1.  
  2. // ==/UserScript==
  3.  
  4. (function() {
  5. 'use strict';
  6.  
  7. class Note {
  8. constructor(htmlNote) {
  9. this.nom = parseFloat($(htmlNote.children()[2]).text());
  10. this.note = parseFloat($(htmlNote.children()[4]).text());
  11. let coeffString = $(htmlNote.children()[6]).text();
  12. this.coeff = parseFloat(coeffString.substr(1, coeffString.length-2));
  13.  
  14. if(isNaN(this.note) || isNaN(this.coeff)) {
  15. this.note = 0;
  16. this.coeff = 0;
  17. }
  18.  
  19. htmlNote.append('<td class="notesAdded"></td>');
  20. }
  21. }
  22.  
  23. class Module {
  24. constructor(htmlModule) {
  25. this.number = parseFloat($(htmlModule.children()[1]).text());
  26. this.name = $(htmlModule.children()[2]).text();
  27. this.coeff = parseFloat($(htmlModule.children()[6]).text());
  28. this.notes = [];
  29.  
  30. let nextRow = $(htmlModule);
  31. while(nextRow.next().length > 0 && !$(nextRow.next()[0]).hasClass('notes_bulletin_row_mod')) {
  32. nextRow = nextRow.next();
  33. if(nextRow.hasClass('notes_bulletin_row_eval')) {
  34. this.notes.push(new Note(nextRow));
  35. }
  36. }
  37.  
  38. htmlModule.append(`<td class="notesAdded">${(this.notes.length > 0) ? (Math.round(this.getMoyenne()*100)/100) : 'N.A.'}</td>`);
  39. }
  40.  
  41. getMoyenne() {
  42. let notesSum = this.notes
  43. .reduce((sum, note) => sum+note.note*note.coeff, 0);
  44. let coeffSum = this.notes
  45. .map((note) => note.coeff)
  46. .reduce((sum, coeff) => sum+coeff, 0);
  47.  
  48. if(coeffSum === 0 || notesSum === 0) {
  49. return 0;
  50. } else {
  51. return notesSum / coeffSum;
  52. }
  53. }
  54. }
  55.  
  56. class UE {
  57. constructor(htmlUE) {
  58. this.numero = null;
  59. this.coeff = parseFloat($(htmlUE.children()[6]).text());
  60. this.modules = [];
  61.  
  62. let nextRow = $(htmlUE);
  63. while(nextRow.next().length > 0 && !$(nextRow.next()[0]).hasClass('notes_bulletin_row_ue')) {
  64. nextRow = nextRow.next();
  65. if(nextRow.hasClass('notes_bulletin_row_mod')) {
  66. this.modules.push(new Module(nextRow));
  67. }
  68. }
  69.  
  70. htmlUE.append(`<td class="notesAdded">${Math.round(this.getMoyenne()*100)/100}</td>`);
  71. }
  72.  
  73. getMoyenne() {
  74. let moyennesSum = this.modules
  75. .reduce((sum, module) => {
  76. return sum+module.getMoyenne()*module.coeff;
  77. }, 0);
  78. let coeffSum = this.modules
  79. .filter((module) => module.getMoyenne() > 0)
  80. .map((module) => module.coeff)
  81. .reduce((sum, coeff) => sum+coeff, 0);
  82.  
  83. if(coeffSum === 0) {
  84. return 0;
  85. } else {
  86. return moyennesSum / coeffSum;
  87. }
  88. }
  89. }
  90.  
  91. class Tableau {
  92. constructor(htmlTable) {
  93. this.ues = [];
  94. this.moyenne = -1;
  95.  
  96. htmlTable.find('thead tr:first').append('<th class="notesAdded"></th>');
  97.  
  98. let body = htmlTable.find('tbody');
  99.  
  100. body.children().each((i, child) => {
  101. child = $(child);
  102. if(child.hasClass('notes_bulletin_row_ue')) {
  103. this.ues.push(new UE(child));
  104. }
  105. });
  106. }
  107.  
  108. getMoyenne() {
  109. let moyennesSum = this.ues
  110. .reduce((sum, ue) => sum+ue.getMoyenne()*ue.coeff, 0);
  111. let coeffSum = this.ues
  112. .map((ue) => ue.coeff)
  113. .reduce((sum, coeff) => sum+coeff, 0);
  114.  
  115. return moyennesSum / coeffSum;
  116. }
  117. }
  118.  
  119. let tab;
  120. function render() {
  121. $('.notesAdded').remove();
  122. tab = new Tableau($('table.notes_bulletin'));
  123. console.log(tab);
  124. $('table').before(`<span class="notesAdded"style="text-align: center; padding: 20px; border: 3px solid red; display: block; margin: 0 20px 20px;"><b>Moyenne générale: ${Math.round(tab.getMoyenne()*100)/100}</b></span>`);
  125. // alert('Votre moyenne est ' + Math.round(tab.getMoyenne()*100)/100);
  126. }
  127. render();
  128.  
  129. if($('table.notes_bulletin')) {
  130. render();
  131. } else {
  132. console.log('Le tableau de note n\'à pas été trouvé');
  133. }
  134.  
  135. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement