-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalcul-average.user.js
More file actions
287 lines (261 loc) · 10.1 KB
/
calcul-average.user.js
File metadata and controls
287 lines (261 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// ==UserScript==
// @name Calculation of averages [ECE Paris]
// @namespace ECE Paris Script
// @version 0.2
// @description Allows you to calculate course averages automatically and add the average box to the grades table.
// @author BragdonD
// @match https://campusonline.inseec.net/note/*
// @require https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
// @homepageURL https://github.com/bragdond/ECE-Scripts
// @supportURL https://github.com/bragdond/ECE-Scripts/issues
// @downloadURL https://raw.githubusercontent.com/bragdond/ECE-Scripts/main/calcul-average.user.js
// @updateURL https://raw.githubusercontent.com/bragdond/ECE-Scripts/main/calcul-average.user.js
// @grant none
// @updateURL https://github.com/BragdonD/ECE-Scripts/blob/main/averagecalcul.js
// ==/UserScript==
/**
* @typedef {Object} Grade
* @property {number} value - The value of the grade.
* @property {number} weight - The weight of the grade.
*/
/**
* @typedef {Object} CourseGradePart
* @property {string} name - The name of the course grade part.
* @property {number} weight - The weight of the course grade part.r}
* @property {Array.<Grade>} grades - The grades of the course grade part.
* @property {number} average - The average grade of the course grade part.
*/
/**
* @typedef {CourseGrade}
* @property {string} name - The name of the course.
* @property {Array.<CourseGradePart>} courseParts - The course grade parts.
* @property {number} coefficient - The coefficient of the course.
* @property {number} average
*/
/**
* @typedef {Object} Module
* @property {string} name - The name of the module.
* @property {Array.<Course>} courses - The courses of the module.
* @property {number} average - The average grade of the module.
*/
/**
* @typedef {Object} Semester
* @property {string} name - The name of the semester.
* @property {Array.<Module>} modules - The modules of the semester.
*/
/**
* @typedef {Object} Year
* @property {string} name - The name of the year.
* @property {Array.<Semester>} semesters - The semesters of the year.
*/
(function () {
'use strict';
const resultatsContainerId = "resultat-note";
const resultatsTableId = "table_note";
const averageLocalStorage = "compute-average"
const resultsContainer = document.querySelector(
"#".concat(resultatsContainerId)
);
if (!resultsContainer) {
return;
}
/**
* Homogenizes the weights of grades in a course part.
*
* @param {Object} coursePart - The course part object containing grades.
* @returns {Object} - The course part object with homogenized weights.
*/
const homogenizeCoursePartGradesWeight = (coursePart) => {
const maxPossibleWeight = 100; // for 100%
let cpTotalWeight = 0.0;
for (const grade of coursePart.grades) {
cpTotalWeight += grade.weight;
}
if (cpTotalWeight >= 100) {
return coursePart;
}
coursePart.grades.forEach((grade) => {
grade.weight = (grade.weight / cpTotalWeight) * maxPossibleWeight;
})
return coursePart;
}
/**
* Homogenizes the weight of each course part in a course.
* If the total weight of all course parts is less than 100, the weights are adjusted proportionally to make the total weight equal to 100.
* If the total weight is already 100 or more, the course is returned unchanged.
* @param {Object} course - The course object containing course parts.
* @returns {Object} - The course object with homogenized course part weights.
*/
const homogenizeCoursePartWeight = (course) => {
const maxPossibleWeight = 100;
let cpTotalWeight = 0.0;
for (const cp of course.courseParts) {
if (cp.grades.length > 0) {
cpTotalWeight += cp.weight;
}
}
if (cpTotalWeight >= 100) {
return course;
}
course.courseParts.forEach((cp) => {
if (cp.grades.length > 0) {
cp.weight = (cp.weight / cpTotalWeight) * maxPossibleWeight;
} else {
cp.weight = 0;
}
})
return course;
}
/**
* Homogenizes the coefficient of each course in the given module.
*
* @param {Object} module - The module object containing courses.
* @returns {Object} - The modified module object with homogenized coefficients.
*/
const homogenizeModuleCoursesCoeff = (module) => {
module.courses.forEach((course) => {
let cpWithGrades = 0;
for (const cp of course.courseParts) {
if (cp.grades.length > 0) {
cpWithGrades += 1;
}
}
if (cpWithGrades == 0) {
course.coefficient = 0;
}
})
return module;
}
/**
* Computes the average for a course part.
*
* @param {Object} cp - The course part object.
* @param {Array} cp.grades - The array of grades for the course part.
* @param {number} cp.average - The average of the course part.
* @param {number} cp.weight - The weight of the course part.
* @returns {Object} - The updated course part object with the computed average.
*/
const computeCoursePartAverage = (cp) => {
if (cp.grades.length == 0) {
cp.average = 0;
return cp;
}
for (const grade of cp.grades) {
if(grade.value == null) {
cp.average = undefined;
cp.weight = 0;
return cp;
}
cp.average += grade.value * (grade.weight / 100);
}
cp.average = Number(cp.average.toFixed(2));
return cp;
}
/**
* compute the average of a Course object
* @param {Course} course
*/
const computeCourseAverage = (course) => {
let availableCP = 0;
for (const coursePart of course.courseParts) {
if (coursePart.average && coursePart.weight) {
course.average += coursePart.average * (coursePart.weight / 100);
if (coursePart.weight > 0) {
availableCP += 1;
}
}
}
if (availableCP == 0) {
course.average = undefined
course.coefficient = 0
} else {
course.average = Number(course.average.toFixed(2));
}
if (course.resit != undefined && course.resit != null) {
course.average = course.resit
}
return course;
}
/**
* Computes the average for a given module.
*
* @param {Object} module - The module object containing courses.
* @returns {Object} - The module object with the computed average.
*/
const computeModuleAverage = (module) => {
let totalCoeff = 0;
module.average = 0;
for (const course of module.courses) {
if (course.average && course.coefficient) {
module.average += course.average * course.coefficient;
totalCoeff += course.coefficient
}
}
if (totalCoeff > 0) {
module.average = (module.average / totalCoeff)
module.average = Number(module.average.toFixed(2));
} else {
module.average = undefined
}
return module;
}
window.addEventListener("extract-grades", () => {
let grades = window.localStorage.getItem("grades")
if (grades == null) {
return;
}
grades = JSON.parse(grades)
console.log("STARTING TO COMPUTE AVERAGES")
// kinda forced due to composition
grades.forEach(
/**
*
* @param {Year} year
*/
(year) => {
year.semesters.forEach(
/**
*
* @param {Semester} semester
*/
(semester) => {
semester.modules.forEach(
/**
*
* @param {Module} module
*/
(module) => {
module.courses.forEach(
/**
*
* @param {Course} course
*/
(course) => {
course.courseParts.forEach(
/**
*
* @param {CourseGradePart} coursePart
*/
(cp) => {
cp = homogenizeCoursePartGradesWeight(cp);
cp = computeCoursePartAverage(cp);
return cp
}
);
course = homogenizeCoursePartWeight(course);
course = computeCourseAverage(course);
}
)
module = homogenizeModuleCoursesCoeff(module);
module = computeModuleAverage(module)
}
);
}
);
}
);
window.localStorage.setItem("grades", JSON.stringify(grades));
console.log("DONE WITH AVERAGE COMPUTING")
window.dispatchEvent(new Event("compute-averages"));
});
})();