-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbarchive.pm
More file actions
484 lines (326 loc) · 11.5 KB
/
bbarchive.pm
File metadata and controls
484 lines (326 loc) · 11.5 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package bbarchive;
#use strict;
use Carp qw(confess);
use XML::Simple;
use Data::Dumper;
use constant
{
GRADEBOOK_TYPE => 'course/x-bb-gradebook',
USER_TYPE => 'course/x-bb-user',
ATTEMPT_TYPE => 'course/x-bb-attemptfiles',
COURSEMEMBER_TYPE => 'membership/x-bb-coursemembership',
RUBRIC_DEF => 'course/x-bb-rubrics',
RUBRIC_EVAL => 'course/x-bb-crsrubriceval',
RUBRIC_ASSOC => 'course/x-bb-crsrubricassocation',
MANIFEST_FILE => 'imsmanifest.xml'
};
## a blessed reference
sub new {
return bless {};
}
## configure self to load archive at $path
sub load {
my ($self, $path) = @_;
$self->{'path'} = $path . '/';
# process manifest file
my $xml_parser = XML::Simple->new();
my $manifest = $xml_parser->XMLin($self->{'path'} . MANIFEST_FILE)
or confess;
## extract manifest resources
my @resources = @{ $manifest->{'resources'}->{'resource'} };
foreach my $resource (@resources) {
my $type = $resource->{'type'};
if ($type eq GRADEBOOK_TYPE) {
$self->{'gradebook.xml'} = $resource->{'bb:file'};
}
if ($type eq USER_TYPE) {
$self->{'user.xml'} = $resource->{'bb:file'};
}
if ($type eq ATTEMPT_TYPE) {
## multiple course/x-bb-attemptfiles elements can be expected
push @{$self->{'attempt.xml'}}, $resource->{'xml:base'};
}
if ($type eq COURSEMEMBER_TYPE) {
$self->{'coursemember.xml'} = $resource->{'bb:file'};
}
if ($type eq RUBRIC_DEF) {
$self->{'rubricdef.xml'} = $resource->{'bb:file'};
}
if ($type eq RUBRIC_ASSOC) {
$self->{'rubricassoc.xml'} = $resource->{'bb:file'};
}
if ($type eq RUBRIC_EVAL) {
$self->{'rubriceval.xml'} = $resource->{'bb:file'};
}
}
## load rubrics
my $rubricdefs = $xml_parser->XMLin($self->{'path'} . $self->{'rubricdef.xml'})
or confess;
# my $rubricassoc = $xml_parser->XMLin($self->{'path'} . $self->{'rubricassoc.xml'})
# or confess;
my $rubriceval = $xml_parser->XMLin($self->{'path'} . $self->{'rubriceval.xml'})
or confess;
## build description of rubric cells
for my $rubric_id (keys %{$rubricdefs->{'Rubric'}}) {
my $rubx = $rubricdefs->{'Rubric'}->{$rubric_id};
my $new_rubric = $self->{'rubrics'}->{$rubric_id} = {};
$new_rubric->{'title'} = $rubx->{'Title'};
$new_rubric->{'desc'} = $rubx->{'Description'};
my $rowid;
my $rowhash;
while (($rowid,$rowhash) = each %{$rubx->{'RubricRows'}->{'Row'}}) {
my $rowhdr = $rowhash->{'Header'}->{'value'};
# print STDERR "$rowhdr\n";
my $colarray;
## make sure it's an array
if ((ref $rowhash->{'RubricColumns'}->{'Column'}) eq 'ARRAY') {
$colarray = $rowhash->{'RubricColumns'}->{'Column'};
}
else {
$colarray = [ $rowhash->{'RubricColumns'}->{'Column'} ];
}
for my $col (@$colarray) {
my $celldesc = $self->{'rubriccells'}->{$col->{'Cell'}->{'id'}} = {};
$celldesc->{'desc'} = $col->{'Cell'}->{'CellDescription'}->{'value'};
$celldesc->{'title'} = $rowhdr;
}
}
}
## build mapping from attempts to rubric evaluations
for my $rub_eval (values %{$rubriceval->{'RUBRIC_EVALUATION'}})
{
# print Dumper($rub_eval);
my $attempt = $rub_eval->{'ATTEMPT_ID'}->{'value'};
next unless $attempt;
my $rubattempt = $self->{'attempt_rubric'}->{$attempt} = {};
my $text =
$rub_eval->{'RUBRIC_EVAL'}->{'COMMENTS'}->{'COMMENTS_TEXT'};
if (ref $text) {
$rubattempt->{'comment'} = $text->{'value'};
}
else {
$rubattempt->{'comment'} = $text;
}
for my $rubcell (values %{$rub_eval->{'RUBRIC_EVAL'}->{'RUBRIC_CELL_EVAL'}})
{
my $fdbk = $rubcell->{'FEEDBACK'};
if (ref $fdbk)
{
$fdbk = $fdbk->{'FEEDBACK_TEXT'};
}
push @{$rubattempt->{'cells'}},
{ cell => $rubcell->{'RUBRIC_CELL_ID'}->{'value'},
message => $fdbk };
}
}
# create user hash
my $users = $xml_parser->XMLin($self->{'path'} . $self->{'user.xml'})
or confess;
# print Dumper($users);
for my $bb_id (keys %{$users->{'USER'}}) {
my $student = $users->{'USER'}->{$bb_id};
my $last = $student->{'NAMES'}->{'FAMILY'}->{'value'};
my $first = $student->{'NAMES'}->{'GIVEN'}->{'value'};
my $full = "$first $last";
my $uid = $student->{'BATCHUID'}->{'value'};
## blackboard id --> name, school id and blackboard id
$self->{'users'}->{$bb_id} = { name => $full,
first => $first,
last => $last,
id => $uid,
bb_id => $bb_id };
}
# build course membership map
my $coursemem = $xml_parser->XMLin($self->{'path'} . $self->{'coursemember.xml'})
or confess;
foreach my $memberid (keys %{$coursemem->{'COURSEMEMBERSHIP'}}) {
## apparently each user is assigned a class-specific userid
## value for associating grade artifacts
my $uid = $coursemem->{'COURSEMEMBERSHIP'}->{$memberid}->{'USERID'}->{'value'};
# print "$memberid -> $uid\n";
$self->{'membermap'}->{$memberid} = $uid;
}
# process attempts
my %attempt_files;
foreach my $attempt_base (@{$self->{'attempt.xml'}}) {
my $attempt = $xml_parser->XMLin($self->{'path'} . $attempt_base . '.dat')
or confess;
foreach my $file_xml (keys %{$attempt->{'ATTEMPTFILE'}}) {
my $file_entry = $attempt->{'ATTEMPTFILE'}->{$file_xml};
my $filetype = $file_entry->{'FILETYPE'}->{'value'};
# print Dumper($file_entry) if ($attempt_base =~ /00064/ and $file_xml =~ /138186/);
my $filename = $file_entry->{'FILE'}->{'NAME'};
my $attempt_id = $file_entry->{'ATTEMPTID'}->{'value'};
my $fullpath = $self->{'path'} . "$attempt_base/$file_xml/$filename";
## find the files that were renamed by blackboard
unless (-e $fullpath)
{
# invalid filename in archive: $full_path
my @actual_files = glob($self->{'path'} . "$attempt_base/$file_xml/!*");
#hack?
$fullpath = $actual_files[0];
}
push @{$attempt_files{$attempt_id}}, { path => $fullpath, type=> $filetype }
}
}
# build grade hash
my $gradebook_entries = $xml_parser->XMLin($self->{'path'} . $self->{'gradebook.xml'})
or confess;
my $outcomes = $gradebook_entries->{'OUTCOMEDEFINITIONS'}->{'OUTCOMEDEFINITION'};
foreach my $outcome_id (keys %$outcomes) {
my $outcome = $outcomes->{$outcome_id};
my $outcome_title = $outcome->{'TITLE'}->{'value'};
my $maxpoints = $outcome->{'POINTSPOSSIBLE'}->{'value'};
## skip calculated columns in gradebook
next if $outcome->{'ISCALCULATED'}->{'value'} eq 'true';
my $thisoutcome =
$self->{'outcomes'}->{$outcome_id} = { id => $outcome_id,
title => $outcome_title,
points => $maxpoints };
my $student_outcomes = $outcome->{'OUTCOMES'}->{'OUTCOME'};
## why was this here?
## next if defined $student_outcomes->{'ATTEMPTS'}
## for each student outcome id
foreach my $soid (keys %$student_outcomes) {
# get student and ultimate grade
my $memberid = $student_outcomes->{$soid}->{'COURSEMEMBERSHIPID'}->{'value'};
my $override = $student_outcomes->{$soid}->{'OVERRIDE_GRADE'};
# skip invalid students
next unless defined $self->{'membermap'}->{$memberid};
my $this_student_outcome = { student => $self->{'users'}->{ $self->{'membermap'}->{$memberid} },
override => $override };
my @attempts;
if (defined $student_outcomes->{$soid}->{'ATTEMPTS'}->{'ATTEMPT'}->{'GRADE'})
{
#single attempt
push @attempts, $student_outcomes->{$soid}->{'ATTEMPTS'}->{'ATTEMPT'};
}
else
{
# multiple attempts
foreach my $k (keys %{$student_outcomes->{$soid}->{'ATTEMPTS'}->{'ATTEMPT'}}) {
#store key as grade id
my $tmp = $student_outcomes->{$soid}->{'ATTEMPTS'}->{'ATTEMPT'}->{$k};
$tmp->{'id'} = $k;
push @attempts, $tmp;
}
}
## collect and crossref actual grade info
my $attempt_obj = [];
foreach my $attempt (@attempts) {
## each grade is an attempt for the current outcome
## each grade's attempt_id should have some components in the attempt file hash
# create a graded attempt object
my $thisattempt = {};
my $attempt_id = $attempt->{'id'};
my $date_submitted = $attempt->{'DATEATTEMPTED'}->{'value'};
my $student_submission = $attempt->{'STUDENTSUBMISSION'}->{'TEXT'};
my $student_comments = $attempt->{'STUDENTCOMMENTS'};
my $instructor_comments = $attempt->{'INSTRUCTORCOMMENTS'}->{'TEXT'};
my $instructor_notes = $attempt->{'INSTRUCTORNOTES'}->{'TEXT'};
my $attempt_grade;
my $files = $attempt_files{$attempt->{'id'}};
my $rubric=undef;
if (exists $self->{'attempt_rubric'}->{$attempt->{'id'}}) {
my $rubattempt = $self->{'attempt_rubric'}->{$attempt->{'id'}};
$rubric->{'comment'} = $rubattempt->{'text'};
$rubric->{'cells'} = [];
# print STDERR $rubric->{'comment'}, "\n";
for my $note (@{$rubattempt->{'cells'}}) {
# print STDERR Dumper($note);
push @{$rubric->{'cells'}},
{ cell => $self->{'rubriccells'}->{$note->{'cell'}},
comment => $note->{'message'} };
}
}
# skip empty grades
if (defined $attempt->{'GRADE'}->{'value'} and $attempt->{'GRADE'}->{'value'} ne "") {
$attempt_grade= $attempt->{'GRADE'}->{'value'};
} else {
next;
}
{
no warnings; # students may have been removed
my $archive_dir = $self->{'path'};
$instructor_comments =~ s/\@X\@EmbeddedFile\.cslocation\@X\@/$archive_dir\/csfiles\/privatedoc\/gradebook\/notesAndFeedback\/attempt\/$attempt_id\//g;
%$thisattempt= (
attempt => $attempt_id,
member => $memberid,
outcome => $outcome_id,
date => $date_submitted,
submission => $student_submission,
comments => $student_comments,
instructor_comments => $instructor_comments,
instructor_notes => $instructor_notes,
grade => $attempt_grade,
files => $files,
rubric => $rubric
);
# if ($instructor_comments =~ /EmbeddedFile/) {
# print STDERR Dumper %$thisattempt;
# }
push @$attempt_obj, $thisattempt;
}
}
next unless @$attempt_obj;
# finish building *this* student outcome object
$this_student_outcome->{'attempts'}=$attempt_obj;
# add reference to outcome
push @{$thisoutcome->{'grades'}}, $this_student_outcome;
# add reference to student
{
no warnings; # students may have been removed
my $student_bbid = $self->{'membermap'}->{$memberid};
$self->{'users'}->{$student_bbid}->{'grades'}->{$outcome_id} = $this_student_outcome;
}
}
}
}
## this sorts outcome names in a way that makes sense to me!
## alphabetic sort unless numbers show up
sub outcome_sorter($$)
{
my ($n1, $n2) = @_;
my @s1 = split /\s+/, $n1;
my @s2 = split /\s+/, $n2;
while (@s1 && @s2)
{
my $r;
my $w1 = shift @s1;
my $w2 = shift @s2;
no warnings;
$r = $w1 <=> $w2;
return $r if $r;
$r = $w1 cmp $w2;
return $r if $r;
}
return $n1 cmp $n2;
}
sub outcome($) {
my ($self, $title) = @_;
foreach my $o (values %{ $self->{'outcomes'} })
{
if ($o->{'title'} eq $title)
{
return bless $o;
}
}
return undef;
}
sub outcomes() {
my ($self) = @_;
return
map { bless $_ } sort { outcome_sorter $a->{'title'}, $b->{'title'} }
values %{ $self->{'outcomes'} };
}
sub student_outcomes() {
my ($self) = @_;
return map { bless $_ }
sort { $a->{'student'}->{'last'} cmp $b->{'student'}->{'last'} }
@{ $self->{'grades'} };
}
sub grades() {
my ($self) = @_;
return map { bless $_ } @{ $self->{'grades'} };
}
1;