-
Notifications
You must be signed in to change notification settings - Fork 0
/
formation.pm
316 lines (239 loc) · 5.96 KB
/
formation.pm
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use strict;
use utf8;
use Text::CSV;
use open qw( :encoding(utf8) :std );
use MyLogger;
package HaveFiles;
sub getFile {
my $self = shift;
my $type = shift;
my $files = $self->{files};
my $file = $$files{$type};
return $file;
}
sub setFile {
my $self = shift;
my $file = shift;
my $type = shift;
my $files = $self->{files};
$$files{$type} = $file;
}
package Etape;
use base qw(HaveFiles);
my %codeEtap2etap;
sub init {
%codeEtap2etap = ();
}
sub getByCodeEtap {
my $codeEtap = shift;
return $codeEtap2etap{$codeEtap};
}
#cohorte = $typeFormation_$sigleFormation_$parcoursFormation_$anneeFormation
#new cohorte = ${typeFormation}_*${sigleFormation}_*${parcoursFormation}_${anneeFormation}~~ (on applique le formatage défini) ~~ou~~
#new cohorte = ${libelleEtape}
#formation_code = $typeFormation_$sigleFormation
#new formation*code = ${formation*label}
#formation_label = $libelle_etape je me demande si on ne fera pas = formation_code
# new formation_label = ${typeFormation} ${sigleFormation} (attention à l'espace entre les deux variables)
sub new {
# Attention on peut créer plusieurs etap on renvoie donc le nombre d'étap créés
# on creer aussi les formations correspondantes aux etapes.
# my ($class, $codeEtap , $libEtap, $libCourt, $site) = @_;
my ($class, $codesEtaps, $libEtap, $codeSISE, $typeDiplome, $intituleDiplome, $site) = @_;
my $cohorte;
unless ($site) {
ERROR! "Etape ($codesEtaps) sans site\n";
foreach my $elem (@_) {
DEBUG! "newEtap: $elem";
}
return 0;
}
my $label = uc("${typeDiplome} ${intituleDiplome}");
$label =~ s/\//-/;
$cohorte = $libEtap;
$cohorte =~ s/(\W|_)+/_/g;
my $codeFormation = $label;
$codeFormation =~ s/(\W|_)+/_/g;
$site =~ s/\W+/-/g;
my $self;
if ($codeFormation =~ m/\w+/) {
my $formation = new Formation($codeFormation, $label, $site);
if ($formation) {
my $nbEtap = 0;
foreach my $codeEtap (split('@',$codesEtaps)) {
$self = {
etap => $codeEtap,
lib => $libEtap,
site => $site,
cohorte => $cohorte,
formation => $formation,
files => {}
};
bless $self, $class;
$formation->etapes($self);
$codeEtap2etap{$codeEtap} = $self;
$nbEtap++;
}
return $nbEtap;
}
return 0;
} else {
ERROR! " codeFormation ", $codeFormation;
return 0;
}
}
sub site {
my $self = shift;
return $self->{site};
}
sub cohorte {
my $self = shift;
return $self->{cohorte}
}
sub formation {
my $self = shift;
return $self->{formation}
}
package Formation;
use base qw(HaveFiles);
my %code2Formation;
my $csv = Text::CSV->new({ sep_char => ',', binary => 1, auto_diag => 0, always_quote => 1 });
# ATTENTION code donne une et une seule formation , mais une formation peut avoir plusieurs etapes.
sub init {
%code2Formation = ();
Etape::init();
}
#la cle est $site_$code
sub getByCle {
my $cle = shift;
return $code2Formation{$cle};
}
sub readFile {
my $path = shift;
my $fileName = shift;
my $sepChar = shift;
init();
my $fileNameLog = "${path}.log";
DEBUG! "open $path/$fileName \n";
open (FORMATION, "<$path/$fileName") || FATAL! "$path/$fileName " . $! . "\n";
binmode(FORMATION, ":encoding(utf8)");
<FORMATION>; # 1er ligne : nom de colonne
open (LOG, ">>$fileNameLog") || FATAL! "$fileNameLog " . $!;
my $nbline = 1;
while (<FORMATION>) {
$nbline++;
s/\"\;\"/\"\,\"/g; #on force les ,
s/(;|\s)+$//;
if ($csv->parse($_) ){
my @fields = $csv->fields();
unless (new Etape(@fields)){
WARN! "formation ligne $nbline : create object error !";
foreach my $elem (@fields) {
INFO! $elem;
}
print LOG "formation $nbline rejet : $_\n";
}
} else {
WARN! "formation ligne $nbline could not be parsed: $_";
$csv->error_diag ();
print LOG "formation ($nbline) rejet : $_\n";
}
}
close LOG;
}
sub writeFile {
my $univ = shift;
my $dateFile = shift;
my $tmp = shift; #le repertoire temporaire de tavail
my $file;
my $path = $univ->path;
my $fileName = sprintf("%s_%s_%s.csv", $univ->id, 'FORMATIONS', $dateFile);
my $fullFileName = $tmp . $fileName;
INFO! "write $fullFileName";
unless ( -d $tmp) {
mkdir $tmp, 0775;
}
open ($file , "> $fullFileName") || FATAL! "$fullFileName " . $!;
$csv->print($file, ['formation_code', 'formation_label']);
print $file "\n";
foreach my $formation (values %code2Formation) {
my @info = ($univ->id() . '_' . $formation->site(). '_' . $formation->code(), $univ->id() .'_' . $formation->site(). ' - ' . $formation->label );
$csv->print($file, \@info );
print $file "\n";
}
close $file;
return $fileName;
}
sub new {
my ($class, $code , $label, $site) = @_;
my $cle="${site}_${code}";
my $formation = getByCle($cle);
if ($formation) {
if ($formation->label ne $label) {
WARN! "formation ($cle) avec plusieurs label: $label ", $formation->label;
}
return $formation;
}
if ($code =~ m/\S/) {
# on peut rencontrer plusieurs fois la même formation avec des codes etape differents
$formation = {
code => $code,
label => $label,
etapes => [],
site => $site,
files => {}
};
} else {
WARN! ("Erreur codeForamation $code : $label");
return 0;
}
bless $formation, $class;
$code2Formation{$cle} = $formation;
return $formation;
}
sub etapes {
my $self = shift;
if (@_ > 0) {
push @{$self->{etapes}}, @_;
}
}
sub code {
my $self = shift;
return $self->{code};
}
sub site {
my $self = shift;
return $self->{site};
}
sub label {
my $self = shift;
return $self->{label};
}
=begin
sub DESTROY{};
sub AUTOLOAD {
our $AUTOLOAD;
my $self = shift;
my $type = ref ($self);
my $field = $AUTOLOAD;
if ($type) {
# print "$field \n";
$field =~ s/.*:://;
if (exists $self->{$field}) {
if (@_) {
return $self->{$field} = shift;
} else {
return $self->{$field};
}
}
}
use Carp ();
local $Carp::CarpLevel = 1;
if ($type) {
Carp::croak ("$field does not exist in object/class $type");
} else {
Carp::croak ("$field($self) is not an object");
}
}
=cut
1;