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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 362 - (download) (as text) (annotate)
Wed Jun 12 21:56:29 2002 UTC (17 years, 5 months ago) by sh002i
File size: 9244 byte(s)
wrote all the prototypes. no implementations yet tho. :(
-sam

    1 ################################################################################
    2 # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester
    3 # $Id$
    4 ################################################################################
    5 
    6 package WeBWorK::DB::Webwork;
    7 
    8 use strict;
    9 use warnings;
   10 use WeBWorK::Set;
   11 use WeBWorK::Problem;
   12 
   13 # there should be a `use' line for each database type
   14 use WeBWorK::DB::GDBM;
   15 
   16 # new($invocant, $courseEnv)
   17 # $invocant - implicitly set by caller
   18 # $courseEnv - an instance of CourseEnvironment
   19 sub new($$) {
   20   my $invocant = shift;
   21   my $class = ref($invocant) || $invocant;
   22   my $courseEnv = shift;
   23   my $dbModule = fullyQualifiedPackageName($courseEnv->{dbInfo}->{wwdb_type});
   24   my $self = {
   25     webwork_file => $courseEnv->{dbInfo}->{wwdb_file},
   26   };
   27   $self->{webwork_db} = $dbModule->new($self->{webwork_file});
   28   bless $self, $class;
   29   return $self;
   30 }
   31 
   32 sub fullyQualifiedPackageName($) {
   33   my $n = shift;
   34   my $package = __PACKAGE__;
   35   $package =~ s/([^:]*)$/$n/;
   36   return $package;
   37 }
   38 
   39 # -----
   40 
   41 # getSets($userID) - returns a list of sets in the current database for the
   42 #                    specified user
   43 # $userID - the user ID (a.k.a. login name) of the user to get sets for
   44 
   45 
   46 # -----
   47 
   48 # getSet($userID, $setID) - returns a WeBWorK::Set object containing data
   49 #                           from the specified set.
   50 # $userID - the user ID (a.k.a. login name) of the set to retrieve
   51 # $setID - the ID (a.k.a. name) of the set to retrieve
   52 
   53 # setSet($set) - if a set with the same ID for the specified user
   54 #                exists, it is replaced. If not, a new set is added.
   55 #                returns true on success, undef on failure.
   56 # $set - a WeBWorK::Set object containing the set data
   57 
   58 # deleteSet($userID, $setID) - removes the set with the specified userID and
   59 #                              setID. Returns true on success, undef on failure.
   60 # $userID - the user ID (a.k.a. login name) of the set to delete
   61 # $setID - the ID (a.k.a. name) of the set to delete
   62 
   63 # -----
   64 
   65 # getSetDefaults($setID) - returns a WeBWorK::Set object containing the default
   66 #                          values for a particular set. (See NOTE)
   67 # setID - id of set to fetch
   68 
   69 # setSetDefaults($set) - Replace set defaults with the given set. (See NOTE)
   70 # $set - a WeBWorK::Set object containing set defaults
   71 
   72 # deleteSetDefaults($setID) - Remove set defaults with the given ID. (See NOTE)
   73 # $setID - the ID of the set defaults to delete
   74 
   75 # -----
   76 
   77 # getProblems($userID, $setID) - returns a list of problems in the specified
   78 #                                set for the specified user.
   79 # $userID - the user ID of the user to get problems for
   80 # $setID - the set ID to get problems from
   81 
   82 # -----
   83 
   84 # getProblem($userID, $setID, $problemNumber) - returns a WeBWorK::Problem
   85 #                                               object containing the problem
   86 #                                               requested
   87 # $userID - the user for which to retrieve the problem
   88 # $setID - the set from which to retrieve the problem
   89 # $problemNumber - the number of the problem to retrieve
   90 
   91 # setProblem($problem) - if a problem with the same ID for the specified user
   92 #                        exists, it is replaced. If not, a new problem is added.
   93 #                        returns true on success, undef on failure.
   94 # $problem - a WeBWorK::Problem object containing the object data
   95 
   96 # deleteProblem($userID, $setID, $problemNumber) - removes a problem with the
   97 #                                                  specified parameters.
   98 # $userID - the user ID of the problem to delete
   99 # $setID - the ID of the problem's set
  100 # $problemNumber - the problem number of the problem to delete
  101 
  102 # -----
  103 
  104 # getProblemDefaults($setID, $problemNumber) - Returns a WeBWorK::Problem object
  105 #                                              containing the default values for
  106 #                                              a particular problem. (See NOTE)
  107 # $setID - set id of problem to retrieve
  108 # $problemNumber - problem number of problem to retrieve
  109 
  110 # setProblemDefaults($problem) - Replace or add problem defaults with the given
  111 #                                problem. (See NOTE)
  112 # $problem - a WeBWorK::Problem object containing problem defaults
  113 
  114 # deleteProblemDefaults($setID, $problemNumber) - remove problem defaults with
  115 #                                                 the given set and problem ID.
  116 #                                                 (See NOTE)
  117 # $setID - the set ID of the problem defaults to delete
  118 # $problemNumber - the problem number of the problem defaults to delete
  119 
  120 # -----
  121 
  122 # getPSVN($userID, $setID) - look up a PSVN given a user ID and set ID (PSVN
  123 #                            stands for Problem Set Version Number and
  124 #                            uniquely identifies a user's version of a set.)
  125 # $userID - the user ID to lookup
  126 # $serID - the set ID to lookup
  127 
  128 # -----
  129 
  130 # decode($string) - decodes a quasi-URL-encoded hash from a hash-based
  131 #                   webwork database. unescapes \& and \= in VALUES ONLY.
  132 # $string - string to decode
  133 sub decode($) {
  134   my $string = shift;
  135   my %hash = $string =~ /(.*?)(?<!\\)=(.*?)(?:(?<!\\)&|$)/g;
  136   $hash{$_} =~ s/\\(.)/$1/ foreach (keys %hash); # unescape anything
  137   return %hash;
  138 }
  139 
  140 # encode(%hash) - encodes a hash as a quasi-URL-encoded string for insertion
  141 #                 into a hash-based webwork database. Escapes & and = in
  142 #                 VALUES ONLY.
  143 # %hash - hash to encode
  144 sub encode(%) {
  145   my %hash = @_;
  146   my $string;
  147   foreach (keys %hash) {
  148     $hash{$_} =~ s/(=|&)/\\$1/; # escape & and =
  149     $string .= "$_=$hash{$_}&";
  150   }
  151   chop $string; # remove final '&' from string for old code :p
  152   return $string;
  153 }
  154 
  155 # -----
  156 
  157 # hash2set(%hash) - places selected fields from a webwork database record
  158 #                   in a WeBWorK::Set object, which is then returned.
  159 # %hash - a hash representing a database record
  160 sub hash2set(%) {
  161   my %hash = @_;
  162   my $set = WeBWorK::Set->new;
  163   $set->id             ( $hash{stnm} ) if defined $hash{stnm};
  164   $set->login_id       ( $hash{stlg} ) if defined $hash{stlg};
  165   $set->set_header     ( $hash{shfn} ) if defined $hash{shfn};
  166   $set->problem_header ( $hash{phfn} ) if defined $hash{phfn};
  167   $set->open_date      ( $hash{opdt} ) if defined $hash{opdt};
  168   $set->due_date       ( $hash{dudt} ) if defined $hash{dudt};
  169   $set->answer_date    ( $hash{andt} ) if defined $hash{andt};
  170   return $set;
  171 }
  172 
  173 # set2hash($set) - unpacks a WeBWorK::Set object and returns PART of a hash
  174 #                  suitable for storage in the webwork database.
  175 # $set - a WeBWorK::Set object.
  176 sub set2hash($) {
  177   my $set = shift;
  178   my %hash;
  179   $hash{stnm} = $set->id             if defined $set->id;
  180   $hash{stlg} = $set->login_id       if defined $set->login_id;
  181   $hash{shfn} = $set->set_header     if defined $set->set_header;
  182   $hash{phfn} = $set->problem_header if defined $set->problem_header;
  183   $hash{opdt} = $set->open_date      if defined $set->open_date;
  184   $hash{dudt} = $set->due_date       if defined $set->due_date;
  185   $hash{andt} = $set->answer_date    if defined $set->answer_date;
  186 }
  187 
  188 # hash@problem($n, %hash) - places selected fields from a webwork
  189 #                                       database record in a WeBWorK::Problem
  190 #                                       object, which is then returned.
  191 # $n - the problem number to extract
  192 # %hash - a hash representing a database record
  193 sub hash2problem($%) {
  194   my $n = shift;
  195   my %hash = @_;
  196   my $problem = WeBWorK::Problem->new(id => $n);
  197   $problem->set_id        ( $hash{stnm}    ) if defined $hash{stnm};
  198   $problem->source_file   ( $hash{"pfn$n"} ) if defined $hash{"pfn$n"};
  199   $problem->value         ( $hash{"pva$n"} ) if defined $hash{"pva$n"};
  200   $problem->max_attempts  ( $hash{"pmia$n"}) if defined $hash{"pmia$n"};
  201   $problem->problem_seed  ( $hash{"pse$n"} ) if defined $hash{"pse$n"};
  202   $problem->status        ( $hash{"pst$n"} ) if defined $hash{"pst$n"};
  203   $problem->attempted     ( $hash{"pat$n"} ) if defined $hash{"pat$n"};
  204   $problem->last_answer   ( $hash{"pan$n"} ) if defined $hash{"pan$n"};
  205   $problem->num_correct   ( $hash{"pca$n"} ) if defined $hash{"pca$n"};
  206   $problem->num_incorrect ( $hash{"pia$n"} ) if defined $hash{"pia$n"};
  207 
  208 }
  209 
  210 # problem2hash($problem) - unpacks a WeBWorK::Problem object and returns PART
  211 #                          of a hash suitable for storage in the webwork
  212 #                          database.
  213 # $problem - a WeBWorK::Problem object
  214 sub problem2hash($) {
  215   my $problem = shift;
  216   my $n = $problem->id;
  217   $hash{stnm}    = $problem->set_id        if defined $problem->set_id;
  218   $hash{"pfn$n"} = $problem->source_file   if defined $problem->source_file;
  219   $hash{"pva$n"} = $problem->value         if defined $problem->value;
  220   $hash{"pmia$n"}= $problem->max_attempts  if defined $problem->max_attempts;
  221   $hash{"pse$n"} = $problem->problem_seed  if defined $problem->problem_seed;
  222   $hash{"pst$n"} = $problem->status        if defined $problem->status;
  223   $hash{"pat$n"} = $problem->attempted     if defined $problem->attempted;
  224   $hash{"pan$n"} = $problem->last_answer   if defined $problem->last_answer;
  225   $hash{"pca$n"} = $problem->num_correct   if defined $problem->num_correct;
  226   $hash{"pia$n"} = $problem->num_incorrect if defined $problem->num_incorrect;
  227   return %hash;
  228 }
  229 
  230 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9