Parent Directory
|
Revision Log
removed unneeded info for deprecated hash2records/records2hash functions -- these are not used by WW1Hash anymore, but they are used by Classlist1Hash. Fix that.
1 ################################################################################ 2 # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project 3 # $Id$ 4 ################################################################################ 5 6 package WeBWorK::DB::Utils; 7 use base qw(Exporter); 8 9 =head1 NAME 10 11 WeBWorK::DB::Utils - useful utilities for the database modules. 12 13 =cut 14 15 use strict; 16 use warnings; 17 use Data::Dumper; 18 19 our @EXPORT = (); 20 our @EXPORT_OK = qw( 21 record2hash 22 hash2record 23 hash2string 24 string2hash 25 global2user 26 user2global 27 initializeUserProblem 28 findDefaults 29 ); 30 31 ################################################################################ 32 # WWDBv2 record <-> WWDBv1 hash 33 # not in the Record classes, since they are for legacy support 34 ################################################################################ 35 36 # FIXME: this should go away -- let WW1Hash and Classlist1Hash handle their own 37 # stupid conversions! 38 39 # RECORDHASH defines the correspondance between WWDBv1 hash keys and WWDBv2 40 # record fields. 41 use constant RECORDHASH => { 42 "WeBWorK::DB::Record::User" => [ 43 ['stfn', "first_name" ], 44 ['stln', "last_name" ], 45 ['stea', "email_address"], 46 ['stid', "student_id" ], 47 ['stst', "status" ], 48 ['clsn', "section" ], 49 ['clrc', "recitation" ], 50 ['comt', "comment" ], 51 ], 52 # "WeBWorK::DB::Record::UserSet" => [ 53 # ['stlg', "user_id" ], 54 # ['stnm', "set_id" ], 55 # ['shfn', "set_header" ], 56 # ['phfn', "problem_header"], 57 # ['opdt', "open_date" ], 58 # ['dudt', "due_date" ], 59 # ['andt', "answer_date" ], 60 # ], 61 # # a hash destined to be converted into a UserProblem must be converted 62 # # so that the hash keys, rather than containing the problem number, 63 # # contain the character '#'. Also, a new hash key '#' must be added 64 # # which contains the problem number. 65 # "WeBWorK::DB::Record::UserProblem" => [ 66 # ['stlg', "user_id" ], 67 # ['stnm', "set_id" ], 68 # ['#', "problem_id" ], 69 # ['pfn#', "source_file" ], 70 # ['pva#', "value" ], 71 # ['pmia#', "max_attempts" ], 72 # ['pse#', "problem_seed" ], 73 # ['pst#', "status" ], 74 # ['pat#', "attempted" ], 75 # ['pan#', "last_answer" ], 76 # ['pca#', "num_correct" ], 77 # ['pia#', "num_incorrect"], 78 # ], 79 }; 80 81 sub record2hash($) { 82 my ($record) = @_; 83 my $map = RECORDHASH->{ref $record}; 84 die ref $record, ": unknown record type" unless defined $map; 85 my %hash; 86 for (my $i = 0; $i < @$map; $i++) { 87 my ($v1, $v2) = @{$map->[$i]}; 88 $hash{$v1} = $record->$v2; 89 } 90 return %hash; 91 } 92 93 sub hash2record($@) { 94 my ($type, %hash) = @_; 95 my $map = RECORDHASH->{$type}; 96 die $type, ": unknown record type" unless defined $map; 97 my $record = $type->new(); 98 for (my $i = 0; $i < @$map; $i++) { 99 my ($v1, $v2) = @{$map->[$i]}; 100 $record->$v2($hash{$v1}); 101 } 102 return $record; 103 } 104 105 ################################################################################ 106 # WWDBv1 hash <-> WWDBv1 string 107 ################################################################################ 108 109 sub hash2string(@) { 110 my (%hash) = @_; 111 my $string; 112 return "" unless keys %hash; 113 foreach (keys %hash) { 114 $hash{$_} = "" unless defined $hash{$_}; # promote undef to "" 115 $hash{$_} =~ s/(=|&)/\\$1/g; # escape & and = 116 $string .= "$_=$hash{$_}&"; 117 } 118 chop $string; # remove final '&' from string for old code :p 119 return $string; 120 } 121 122 sub string2hash($) { 123 my $string = shift; 124 return unless defined $string and $string; 125 my %hash = $string =~ /(.*?)(?<!\\)=(.*?)(?:(?<!\\)&|$)/g; 126 $hash{$_} =~ s/\\(&|=)/$1/g foreach keys %hash; # unescape & and = 127 return %hash; 128 } 129 130 ################################################################################ 131 # global <-> user record conversion 132 ################################################################################ 133 134 sub global2user($$) { 135 my ($userRecordClass, $GlobalRecord) = @_; 136 my $UserRecord = $userRecordClass->new(); 137 foreach my $field ($GlobalRecord->FIELDS()) { 138 $UserRecord->$field($GlobalRecord->$field()); 139 } 140 return $UserRecord; 141 } 142 143 sub user2global($$) { 144 my ($globalRecordClass, $UserRecord) = @_; 145 my $GlobalRecord = $globalRecordClass->new(); 146 foreach my $field ($GlobalRecord->FIELDS()) { 147 $GlobalRecord->$field($UserRecord->$field()); 148 } 149 return $GlobalRecord; 150 } 151 152 # Populate a user record with sane defaults and a random seed 153 # This function edits the record in place, so you can discard 154 # the return value. 155 sub initializeUserProblem { 156 my ($userProblem) = @_; 157 $userProblem->status(0.0); 158 $userProblem->attempted(0); 159 $userProblem->num_correct(0); 160 $userProblem->num_incorrect(0); 161 $userProblem->problem_seed(int(rand(5000))); 162 163 return $userProblem; 164 } 165 166 ################################################################################ 167 # default generation 168 ################################################################################ 169 170 sub findDefaults($@) { 171 my ($globalClass, @Records) = @_; 172 173 my %fields = map { $_ => {} } $globalClass->FIELDS(); 174 #delete $fields{$_} foreach $globalClass->KEYFIELDS(); 175 176 foreach my $Record (@Records) { 177 foreach my $field (keys %fields) { 178 my $value = $Record->$field(); 179 if ($value eq "UNDEFINED") { 180 die "Uh oh... value eq \"UNDEFINED\"\n"; 181 } 182 unless (defined $value) { 183 $value = "UNDEFINED"; 184 } 185 $fields{$field}{$value}++; 186 } 187 } 188 189 #warn "Frequencies: ", Dumper(\%fields); 190 191 my $Defaults = $globalClass->new(); 192 foreach my $field (keys %fields) { 193 my $maxFreq = 0; 194 my $maxValue; 195 foreach my $value (keys %{$fields{$field}}) { 196 my $freq = $fields{$field}{$value}; 197 if ($freq > $maxFreq) { 198 $maxFreq = $freq; 199 $maxValue = $value; 200 } 201 } 202 undef $maxValue if $maxValue eq "UNDEFINED"; 203 $Defaults->$field($maxValue); 204 } 205 206 #warn "Consensus defaults: ", Dumper($Defaults); 207 208 return $Defaults; 209 } 210 211 1;
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |