Parent Directory
|
Revision Log
Revision 2880 -
(view)
(download)
(as text)
Original Path: trunk/webwork2/lib/WeBWorK/File/Classlist.pm
| 1 : | sh002i | 2880 | ################################################################################ |
| 2 : | # WeBWorK Online Homework Delivery System | ||
| 3 : | # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/ | ||
| 4 : | # $CVSHeader: webwork2/lib/WeBWorK/Authz.pm,v 1.17 2004/09/02 22:52:02 sh002i Exp $ | ||
| 5 : | # | ||
| 6 : | # This program is free software; you can redistribute it and/or modify it under | ||
| 7 : | # the terms of either: (a) the GNU General Public License as published by the | ||
| 8 : | # Free Software Foundation; either version 2, or (at your option) any later | ||
| 9 : | # version, or (b) the "Artistic License" which comes with this package. | ||
| 10 : | # | ||
| 11 : | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 : | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 13 : | # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the | ||
| 14 : | # Artistic License for more details. | ||
| 15 : | ################################################################################ | ||
| 16 : | |||
| 17 : | package WeBWorK::File::Classlist; | ||
| 18 : | use base qw/Exporter/; | ||
| 19 : | |||
| 20 : | =head1 NAME | ||
| 21 : | |||
| 22 : | WeBWorK::Authz - parse and write classlist files. | ||
| 23 : | |||
| 24 : | =cut | ||
| 25 : | |||
| 26 : | use strict; | ||
| 27 : | use warnings; | ||
| 28 : | use IO::File; | ||
| 29 : | |||
| 30 : | use constant FIELD_ORDER => qw/student_id last_name first_name status comment section recitation email_address user_id/; | ||
| 31 : | our $nfields = FIELD_ORDER; | ||
| 32 : | |||
| 33 : | our @EXPORT = qw/parse_classlist write_classlist/; | ||
| 34 : | |||
| 35 : | sub parse_classlist($) { | ||
| 36 : | my ($file) = @_; | ||
| 37 : | |||
| 38 : | my $fh = new IO::File($file, "<") | ||
| 39 : | or die "Failed to open classlist '$file' for reading: $!\n"; | ||
| 40 : | |||
| 41 : | my (@records); | ||
| 42 : | |||
| 43 : | while (<$fh>) { | ||
| 44 : | chomp; | ||
| 45 : | s/#.*$//; | ||
| 46 : | next unless /\S/; | ||
| 47 : | s/^\s*//; | ||
| 48 : | s/\s*$//; | ||
| 49 : | |||
| 50 : | my @fields = split /\s*,\s*/; | ||
| 51 : | my $fields = @fields; | ||
| 52 : | unless ($fields == $nfields) { | ||
| 53 : | warn "Skipped invalid line $. of classlist '$file': expected $nfields fields, got $fields fields.\n"; | ||
| 54 : | next; | ||
| 55 : | } | ||
| 56 : | |||
| 57 : | my %record; | ||
| 58 : | @record{FIELD_ORDER()} = @fields; | ||
| 59 : | |||
| 60 : | push @records, \%record; | ||
| 61 : | } | ||
| 62 : | |||
| 63 : | $fh->close; | ||
| 64 : | |||
| 65 : | return @records; | ||
| 66 : | } | ||
| 67 : | |||
| 68 : | sub write_classlist($@) { | ||
| 69 : | my ($file, @records) = @_; | ||
| 70 : | |||
| 71 : | my $fh = new IO::File($file, ">") | ||
| 72 : | or die "Failed to open classist '$file' for writing: $!\n"; | ||
| 73 : | |||
| 74 : | foreach my $i (0 .. $#records) { | ||
| 75 : | my $record = $records[$i]; | ||
| 76 : | unless (ref $record eq "HASH") { | ||
| 77 : | warn "Skipping record $i: not a reference to a hash.\n"; | ||
| 78 : | next; | ||
| 79 : | } | ||
| 80 : | |||
| 81 : | my %record = %$record; | ||
| 82 : | my @fields = @record{FIELD_ORDER()}; | ||
| 83 : | my $fields = @fields; | ||
| 84 : | |||
| 85 : | my $string = join ",", @fields; | ||
| 86 : | |||
| 87 : | print $fh "$string\n"; | ||
| 88 : | } | ||
| 89 : | |||
| 90 : | $fh->close; | ||
| 91 : | } |
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |