[system] / trunk / webwork-modperl / lib / Apache / WeBWorK.pm Repository:
ViewVC logotype

View of /trunk/webwork-modperl/lib/Apache/WeBWorK.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 446 - (download) (as text) (annotate)
Wed Jul 31 19:50:53 2002 UTC (10 years, 10 months ago) by malsyned
File size: 4496 byte(s)
CGI.pm cleanups
-dennis

    1 # Apache::WeBWorK - The WeBWorK dispatcher module
    2 # Place something like the following in your Apache configuration to load the
    3 # WeBWorK module and install it as a handler for the WeBWorK system
    4 
    5 # PerlRequire /path/to/webwork/conf/init.pl
    6 # PerlSetVar webwork_root /path/to/webwork
    7 # <Location /webwork>
    8 # SetHandler perl-script
    9 # PerlHandler Apache::WeBWorK
   10 # </Location>
   11 
   12 # In addition, you will have to edit init.pl in what should be obvious ways.
   13 
   14 package Apache::WeBWorK;
   15 
   16 use strict;
   17 use warnings;
   18 use Apache::Constants qw(:common REDIRECT);
   19 use Apache::Request;
   20 use WeBWorK::CourseEnvironment;
   21 use WeBWorK::Authen;
   22 use WeBWorK::Authz;
   23 use WeBWorK::ContentGenerator::Test;
   24 use WeBWorK::ContentGenerator::Login;
   25 use WeBWorK::ContentGenerator::ProblemSets;
   26 use WeBWorK::ContentGenerator::ProblemSet;
   27 use WeBWorK::ContentGenerator::Problem;
   28 
   29 # Sets up the common environment needed for every subsystem and then dispatches
   30 # the page request to the appropriate content generator.
   31 
   32 # This function has MANY MANY points of exit (return statements)!  woo!
   33 # call it a quirk of my coding style.  I think it makes it easier to read in this case.
   34 
   35 sub handler() {
   36   my $r = Apache::Request->new(shift); # have to deal with unpredictable GET or POST data, and sift through it for the key.  So use Apache::Request
   37 
   38   # This stuff is pretty much copied out of the O'Reilly mod_perl book.
   39   # It's for figuring out the basepath.  I may change this up if I
   40   # find a better way to do it.
   41   my $path_info = $r->path_info;
   42   my $path_translated = $r->lookup_uri($path_info)->filename;
   43   my $current_uri = $r->uri;
   44   my $args = $r->args;
   45 
   46   # If it's a valid WeBWorK URI, it ends in a /.  This is assumed
   47   # alllll over the place.
   48   unless (substr($current_uri,-1) eq '/') {
   49     $r->header_out(Location => "$current_uri/" . ($args ? "?$args" : ""));
   50     return REDIRECT;
   51   }
   52 
   53   # Create the @components array, which contains the path specified in the URL
   54   my($junk, @components) = split "/", $path_info;
   55   my $webwork_root = $r->dir_config('webwork_root'); # From a PerlSetVar in httpd.conf
   56   my $course = shift @components;
   57 
   58   # Try to get the course environment.
   59   my $course_env = eval {WeBWorK::CourseEnvironment->new($webwork_root, $course);};
   60   if ($@) { # If there was an error getting the requested course
   61     # TODO: display an error page.  For now, 404 it.
   62     warn $@;
   63     return DECLINED;
   64   }
   65 
   66   # Freak out if the requested course doesn't exist.  For now, this is just a
   67   # check to see if the course directory exists.
   68   if (!-e $course_env->{webworkDirs}->{courses} . "/$course") {
   69     return DECLINED;
   70   }
   71 
   72   ### Begin dispatching ###
   73 
   74   # WeBWorK::Authen::verify erases the passwd field and sets the key field
   75   # if login is successful.
   76   if (!WeBWorK::Authen->new($r, $course_env)->verify) {
   77     return WeBWorK::ContentGenerator::Login->new($r, $course_env)->go;
   78   } else {
   79     # After we are authenticated, there are some things that need to be
   80     # sorted out, Authorization-wize, before we start dispatching to individual
   81     # content generators.
   82     my $effectiveUser = $r->param("effectiveUser");
   83     my $user = $r->param("user");
   84     my $su_authorized = WeBWorK::Authz->new($r, $course_env)->hasPermissions($user, "become_student", $effectiveUser);
   85     # This hoary statement has the effect of forcing effectiveUser to equal user unless
   86     # the user is otherwise authorized.
   87     if (!($user ne $effectiveUser && $su_authorized) || !defined $effectiveUser) {
   88       $r->param("effectiveUser",$user);
   89     }
   90 
   91     my $arg = shift @components;
   92     if (!defined $arg) { # We want the list of problem sets
   93       return WeBWorK::ContentGenerator::ProblemSets->new($r, $course_env)->go;
   94     } elsif ($arg eq "prof") {
   95       ###
   96     } elsif ($arg eq "prefs") {
   97       ###
   98     } elsif ($arg eq "test") {
   99       return WeBWorK::ContentGenerator::Test->new($r, $course_env)->go;
  100     } else { # We've got the name of a problem set.
  101       my $problem_set = $arg;
  102       my $ps_arg = shift @components;
  103 
  104       if (!defined $ps_arg) {
  105         # list the problems in the problem set
  106         return WeBWorK::ContentGenerator::ProblemSet->new($r, $course_env)->go($problem_set);
  107       } elsif ($ps_arg eq "hardcopy") {
  108         ###
  109       }
  110       else {
  111         # We've got the name of a problem
  112         my $problem = $ps_arg;
  113         return WeBWorK::ContentGenerator::Problem->new($r, $course_env)->go($problem_set, $problem);
  114       }
  115     }
  116 
  117   }
  118 
  119   # If the dispatcher doesn't know any modules that want to handle
  120   # the current path, it'll claim that the path does not exist by
  121   # declining the request.
  122   return DECLINED;
  123 }
  124 
  125 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9