-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnam-userscript-formation.user.js
138 lines (130 loc) · 4.55 KB
/
cnam-userscript-formation.user.js
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
// ==UserScript==
// @name CNAM Formation Helper
// @version 0.1
// @description Permet de sélectionner et voir les UE choisies, ainsi que les restantes
// @author Chuckame
// @homepage https://github.com/Chuckame/cnam-userscript-formation
// @match http://formation.cnam.fr/*
// @require http://rawgit.com/notifyjs/notifyjs/master/dist/notify.js
// ==/UserScript==
(function() {
'use strict';
$('body').append('<script type="text/javascript" src="https://rawgit.com/notifyjs/notifyjs/master/dist/notify.js"></script>');
var checked = new Set();
var taken = new Set();
var fulled = new Set();
var checkedColor = '#7fd867';
var takenColor = '#ff8b8b';
var fulledColor = '#62ceff';
var defaultColor = '#efefef';
function getCodeOfUeDiv(div){
if (!($(div).is('div') && $(div).hasClass('ue'))){
return;
}
return $(div).find('div.code > a[target="ue"]').html();
}
function getCreditsOfUeDiv(div){
if (!($(div).is('div') && $(div).hasClass('ue'))){
return;
}
return parseInt($(div).find('div.credits')[0].innerText.match(/.*(\d+)\s+ect.*/i)[1]);
}
function getCreditsOfSectionDiv(div){
if (!($(div).is('div') && $(div).hasClass('suite'))){
return;
}
return parseInt($(div).find('span.credits-equivalents').first()[0].innerText.match(/.*\s+(\d+)\s+ect.*/i)[1]);
}
function computeUsedCreditsOfSectionDiv(div){
if (!($(div).is('div') && $(div).hasClass('suite'))){
return;
}
var result = 0;
$(div).find('div.ue').each(function (){
if (checked.has(this)){
result += getCreditsOfUeDiv(this);
}
});
return result;
}
function getSectionDivFromUeDiv(div){
if (!($(div).is('div') && $(div).hasClass('ue'))){
return;
}
return $(div).parent('div.suite');
}
function getSimilarsUeFromCode(code){
var result = new Set();
$('div.ue').each(function (){
if (getCodeOfUeDiv(this) === code){
result.add(this);
}
});
return result;
}
function resetUeDiv(div){
checked.delete(div);
taken.delete(div);
fulled.delete(div);
$(div).css('background-color', defaultColor);
}
function checkUeDiv(div){
checked.add(div);
$(div).css('background-color', checkedColor);
}
function takeUeDiv(div){
taken.add(div);
$(div).css('background-color', takenColor);
}
function fullUeDiv(div){
fulled.add(div);
$(div).css('background-color', fulledColor);
}
function refreshSection(section){
var requiredCredits = getCreditsOfSectionDiv(section);
console.log('Crédits requis pour cette section:', requiredCredits);
var restantCredits = requiredCredits - computeUsedCreditsOfSectionDiv(section);
console.log('Crédits restants pour cette section:', restantCredits);
$(section).find('div.ue').each(function (){
if (checked.has(this) || taken.has(this)){
return;
}
var credits = getCreditsOfUeDiv(this);
console.log("Crédits de l'UE", getCodeOfUeDiv(this), ':', credits);
if (credits > restantCredits){
fullUeDiv(this);
}else{
resetUeDiv(this);
}
});
}
$('div.ue').click(function(event){
if ($(event.target).hasClass('cra_opener') || $(event.target).is('a')){
return;
}
var div = event.currentTarget;
var code = getCodeOfUeDiv(div);
if (fulled.has(div)){
$.notify('warn', "Cette section d'UE(s) est complète.");
} else if (checked.has(div)){
resetUeDiv(div);
getSimilarsUeFromCode(code).forEach(function(similar){
if (taken.has(similar)){
resetUeDiv(similar);
refreshSection(getSectionDivFromUeDiv(similar));
}
});
} else if (taken.has(div)){
$.notify('error', 'Cette UE est déjà prise.');
} else{
checkUeDiv(div);
getSimilarsUeFromCode(code).forEach(function(similar){
if (similar !== div){
takeUeDiv(similar);
refreshSection(getSectionDivFromUeDiv(similar));
}
});
}
refreshSection(getSectionDivFromUeDiv(div));
});
})();