[system] / trunk / webwork2 / lib / WeBWorK / DB / Utils.pm Repository:
ViewVC logotype

View of /trunk/webwork2/lib/WeBWorK/DB/Utils.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 839 - (download) (as text) (annotate)
Thu May 15 00:55:49 2003 UTC (10 years ago) by sh002i
File size: 3411 byte(s)
cruft removal.
-sam

    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 
   18 our @EXPORT    = ();
   19 our @EXPORT_OK = qw(
   20   record2hash
   21   hash2record
   22   hash2string
   23   string2hash
   24 );
   25 
   26 ################################################################################
   27 # WWDBv2 record <-> WWDBv1 hash
   28 #  not in the Record classes, since they are for legacy support
   29 ################################################################################
   30 
   31 # RECORDHASH defines the correspondance between WWDBv1 hash keys and WWDBv2
   32 # record fields.
   33 
   34 use constant RECORDHASH => {
   35   "WeBWorK::DB::Record::User" => [
   36     ['stfn', "first_name"   ],
   37     ['stln', "last_name"    ],
   38     ['stea', "email_address"],
   39     ['stid', "student_id"   ],
   40     ['stst', "status"       ],
   41     ['clsn', "section"      ],
   42     ['clrc', "recitation"   ],
   43     ['comt', "comment"      ],
   44   ],
   45   "WeBWorK::DB::Record::UserSet" => [
   46     ['stlg', "user_id"       ],
   47     ['stnm', "set_id"        ],
   48     ['shfn', "set_header"    ],
   49     ['phfn', "problem_header"],
   50     ['opdt', "open_date"     ],
   51     ['dudt', "due_date"      ],
   52     ['andt', "answer_date"   ],
   53   ],
   54   # a hash destined to be converted into a UserProblem must be converted
   55   # so that the hash keys, rather than containing the problem number,
   56   # contain the character '#'. Also, a new hash key '#' must be added
   57   # which contains the problem number.
   58   "WeBWorK::DB::Record::UserProblem" => [
   59     ['stlg',  "user_id"      ],
   60     ['stnm',  "set_id"       ],
   61     ['#',     "problem_id"   ],
   62     ['pfn#',  "source_file"  ],
   63     ['pva#',  "value"        ],
   64     ['pmia#', "max_attempts" ],
   65     ['pse#',  "problem_seed" ],
   66     ['pst#',  "status"       ],
   67     ['pat#',  "attempted"    ],
   68     ['pan#',  "last_answer"  ],
   69     ['pca#',  "num_correct"  ],
   70     ['pia#',  "num_incorrect"],
   71   ],
   72   # *** add tables for the rest of the record types
   73 };
   74 
   75 sub record2hash($) {
   76   my ($record) = @_;
   77   my $map = RECORDHASH->{ref $record};
   78   die ref $record, ": unknown record type" unless defined $map;
   79   my %hash;
   80   for (my $i = 0; $i < @$map; $i++) {
   81     my ($v1, $v2) = @{$map->[$i]};
   82     $hash{$v1} = $record->$v2;
   83   }
   84   return %hash;
   85 }
   86 
   87 sub hash2record($@) {
   88   my ($type, %hash) = @_;
   89   my $map = RECORDHASH->{$type};
   90   die $type, ": unknown record type" unless defined $map;
   91   my $record = $type->new();
   92   for (my $i = 0; $i < @$map; $i++) {
   93     my ($v1, $v2) = @{$map->[$i]};
   94     $record->$v2($hash{$v1});
   95   }
   96   return $record;
   97 }
   98 
   99 ################################################################################
  100 # WWDBv1 hash <-> WWDBv1 string
  101 ################################################################################
  102 
  103 sub hash2string(@) {
  104   my %hash = @_;
  105   my $string;
  106   return "" unless keys %hash;
  107   foreach (keys %hash) {
  108     $hash{$_} = "" unless defined $hash{$_}; # promote undef to ""
  109     $hash{$_} =~ s/(=|&)/\\$1/g; # escape & and =
  110     $string .= "$_=$hash{$_}&";
  111   }
  112   chop $string; # remove final '&' from string for old code :p
  113   return $string;
  114 }
  115 
  116 sub string2hash($) {
  117   my $string = shift;
  118   return unless defined $string and $string;
  119   my %hash = $string =~ /(.*?)(?<!\\)=(.*?)(?:(?<!\\)&|$)/g;
  120   $hash{$_} =~ s/\\(&|=)/$1/g foreach keys %hash; # unescape & and =
  121   return %hash;
  122 }
  123 
  124 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9