#!/usr/bin/perl # This code takes marmoset .csv output files, and produces output # that can be used as a public-marks.txt file (to post marks online). # Requires a config file gen_public_marks_config in the same directory # This script requires no command line arguments, and outputs to stdout use Shell; sub FormatDate { # Eliminates all spaces, dashes, and colons from the date and returns my($arg1) = @_; # them in YYYYMMDDHHMM format which can be compared numerically $arg1 = substr($arg1, 0, 16); $arg1 =~ s/://g; $arg1 =~ s/-//g; $arg1 =~ s/ //g; return $arg1 } %students = (); # Stores (userid -> userid) pairs %assignments = (); # Stores (Assignment Name -> comma delimited list of file names) pairs %marks = (); # Stores (FileName,userid -> mark) pairs %header = (); # Stores (Assignment Name -> Assignment Out of) pairs open(config, "gen_public_marks_config"); while($config = ) { if ( !(substr($config,0,1) eq "#") ) { @config = split(/,/, $config); @files = `ls @config[1]/*.csv`; # Store the header information for printing later $header{@config[0]} += @config[2]; foreach $file_name (@files) { $file_name =~ s/\n//; # Eliminate newlines $assignments{@config[0]} = $file_name . "," . $assignments{@config[0]}; open(IN, $file_name); while($line = ) { @info = split(/,/, $line); if ( !(@info[0] eq "classAccount")) { $students{@info[0]} = @info[0]; # Calculate the mark, or the late mark depending on submitted date if ( FormatDate(@info[1]) < FormatDate(@config[3]) ) { $mark = @info[3]; } elsif ( FormatDate(@info[1]) < FormatDate(@config[4]) ) { $mark = @info[3] * @config[5]; } else { $mark = 0; } if ( !(exists($marks{$file_name . "," . @info[0]})) || ( exists($marks{$file_name . "," . @info[0]}) && $marks{$file_name . "," . @info[0]} < $mark ) ) { $marks{$file_name . "," . @info[0]} = $mark; } } # if != classAccount } # while $line = close(IN); } # foreach @file } # if char != "#" } # while config = close(config); # Print the two header rows foreach $head ( sort keys %header ) { $assignment_string = $assignment_string . "," . $head; $mark_string = $mark_string . "," . $header{$head}; } print substr($assignment_string, 1, length($assignment_string) - 1) . "\n"; print substr($mark_string, 1, length($mark_string) - 1) . "\n"; # Print all of the data, sorted by student name, in correct assignment order foreach $userid (sort keys %students) { print $userid; foreach $assign_name (sort keys %assignments) { $file_names = substr($assignments{$assign_name},0, length($assignments{$assign_name}) - 1); @file_names = split(/,/, $file_names); $sum = 0; for($i = 0; $i < scalar(@file_names); $i++) { if ( exists($marks{@file_names[$i] . "," . $userid}) ) { $sum += $marks{@file_names[$i] . "," . $userid}; } } print "," . $sum; } print "\n"; }