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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1224 - (download) (as text) (annotate)
Fri Jun 20 01:16:09 2003 UTC (9 years, 11 months ago) by sh002i
File size: 7659 byte(s)
commented out timing code

    1 ################################################################################
    2 # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project
    3 # $Id$
    4 ################################################################################
    5 
    6 package WeBWorK;
    7 
    8 =head1 NAME
    9 
   10 WeBWorK - Dispatch requests to the appropriate ContentGenerator.
   11 
   12 =cut
   13 
   14 use strict;
   15 use warnings;
   16 use Apache::Constants qw(:common REDIRECT);
   17 use Apache::Request;
   18 use WeBWorK::Authen;
   19 use WeBWorK::Authz;
   20 use WeBWorK::ContentGenerator::Feedback;
   21 use WeBWorK::ContentGenerator::GatewayQuiz;
   22 use WeBWorK::ContentGenerator::Hardcopy;
   23 use WeBWorK::ContentGenerator::Instructor::Assigner;
   24 use WeBWorK::ContentGenerator::Instructor::Index;
   25 use WeBWorK::ContentGenerator::Instructor::PGProblemEditor;
   26 use WeBWorK::ContentGenerator::Instructor::ProblemList;
   27 use WeBWorK::ContentGenerator::Instructor::ProblemSetEditor;
   28 use WeBWorK::ContentGenerator::Instructor::ProblemSetList;
   29 use WeBWorK::ContentGenerator::Instructor::UserList;
   30 use WeBWorK::ContentGenerator::Instructor::UserList;
   31 use WeBWorK::ContentGenerator::Login;
   32 use WeBWorK::ContentGenerator::Logout;
   33 use WeBWorK::ContentGenerator::Options;
   34 use WeBWorK::ContentGenerator::Problem;
   35 use WeBWorK::ContentGenerator::ProblemSet;
   36 use WeBWorK::ContentGenerator::ProblemSets;
   37 use WeBWorK::ContentGenerator::Test;
   38 use WeBWorK::CourseEnvironment;
   39 use WeBWorK::DB;
   40 use WeBWorK::Timing;
   41 
   42 #sub dispatch($) {
   43 # print STDERR "Executing &WeBWorK::dispatch\n";
   44 # return DECLINED;
   45 #}
   46 #1;
   47 #__END__
   48 
   49 sub dispatch($) {
   50   my ($apache) = @_;
   51   my $r = Apache::Request->new($apache);
   52     # have to deal with unpredictable GET or POST data, and sift
   53     # through it for the key. So use Apache::Request
   54 
   55   # This stuff is pretty much copied out of the O'Reilly mod_perl book.
   56   # It's for figuring out the basepath. I may change this up if I find a
   57   # better way to do it.
   58   my $path_info = $r->path_info || "";
   59   $path_info =~ s!/+!/!g; # strip multiple forward slashes
   60   my $current_uri = $r->uri;
   61   my $args = $r->args;
   62 
   63   my ($urlRoot) = $current_uri =~ m/^(.*)$path_info/;
   64 
   65   # If it's a valid WeBWorK URI, it ends in a /.  This is assumed
   66   # alllll over the place.
   67   unless (substr($current_uri,-1) eq '/') {
   68     $r->header_out(Location => "$current_uri/" . ($args ? "?$args" : ""));
   69     return REDIRECT;
   70     # *** any post data gets lost here -- fix that.
   71     # (actually, it's not a problem, since all URLs generated
   72     # from within the system have trailing slashes, and we don't
   73     # need POST data from outside the system anyway!)
   74   }
   75 
   76   # Create the @components array, which contains the path specified in the URL
   77   my($junk, @components) = split "/", $path_info;
   78   my $webwork_root = $r->dir_config('webwork_root'); # From a PerlSetVar in httpd.conf
   79   my $pg_root = $r->dir_config('pg_root'); # From a PerlSetVar in httpd.conf
   80   my $course = shift @components;
   81 
   82   # Try to get the course environment.
   83   my $ce = eval {WeBWorK::CourseEnvironment->new($webwork_root, $urlRoot, $pg_root, $course);};
   84   if ($@) { # If there was an error getting the requested course
   85     die "Failed to read course environment for $course: $@";
   86   }
   87 
   88   # If no course was specified, redirect to the home URL
   89   unless (defined $course) {
   90     $r->header_out(Location => $ce->{webworkURLs}->{home});
   91     return REDIRECT;
   92   }
   93 
   94   # Freak out if the requested course doesn't exist.  For now, this is just a
   95   # check to see if the course directory exists.
   96   my $courseDir = $ce->{webworkDirs}->{courses} . "/$course";
   97   unless (-e $courseDir) {
   98     die "Course directory for $course ($courseDir) not found. Perhaps the course does not exist?";
   99   }
  100 
  101   # Bring up a connection to the database (for Authen/Authz, and eventually
  102   # to be passed to content generators, when we clean this file up).
  103   my $db = WeBWorK::DB->new($ce);
  104 
  105   ### Begin dispatching ###
  106 
  107   #my $dispatchTimer = WeBWorK::Timing->new(__PACKAGE__."::dispatch");
  108   #$dispatchTimer->start;
  109 
  110   my $result;
  111   # WeBWorK::Authen::verify erases the passwd field and sets the key field
  112   # if login is successful.
  113   if (!WeBWorK::Authen->new($r, $ce, $db)->verify) {
  114     $result = WeBWorK::ContentGenerator::Login->new($r, $ce, $db)->go;
  115   } else {
  116     # After we are authenticated, there are some things that need to be
  117     # sorted out, Authorization-wize, before we start dispatching to individual
  118     # content generators.
  119     my $user = $r->param("user");
  120     my $effectiveUser = $r->param("effectiveUser") || $user;
  121     my $su_authorized = WeBWorK::Authz->new($r, $ce, $db)->hasPermissions($user, "become_student", $effectiveUser);
  122     $effectiveUser = $user unless $su_authorized;
  123     $r->param("effectiveUser", $effectiveUser);
  124 
  125     my $arg = shift @components;
  126     if (!defined $arg) { # We want the list of problem sets
  127       $result = WeBWorK::ContentGenerator::ProblemSets->new($r, $ce, $db)->go;
  128     } elsif ($arg eq "hardcopy") {
  129 
  130       my $hardcopyArgument = shift @components;
  131       #$WeBWorK::timer1 = WeBWorK::Timing->new("hardcopy: $hardcopyArgument");
  132       #$WeBWorK::timer1->start;
  133       $hardcopyArgument = "" unless defined $hardcopyArgument;
  134       my $result = WeBWorK::ContentGenerator::Hardcopy->new($r, $ce, $db)->go($hardcopyArgument);
  135       #$WeBWorK::timer1 ->stop;
  136       #$WeBWorK::timer1 ->save;
  137       return $result;
  138     } elsif ($arg eq "instructor") {
  139       my $instructorArgument = shift @components;
  140       if (!defined $instructorArgument) {
  141         $result = WeBWorK::ContentGenerator::Instructor::Index->new($r, $ce, $db)->go;
  142       } elsif ($instructorArgument eq "users") {
  143         $result = WeBWorK::ContentGenerator::Instructor::UserList->new($r, $ce, $db)->go;
  144       } elsif ($instructorArgument eq "sets") {
  145         my $setID = shift @components;
  146         if (defined $setID) {
  147           my $setArg = shift @components;
  148           if (!defined $setArg) {
  149             $result = WeBWorK::ContentGenerator::Instructor::ProblemSetEditor->new($r, $ce, $db)->go($setID);
  150           } elsif ($setArg eq "problems") {
  151             $result = WeBWorK::ContentGenerator::Instructor::ProblemList->new($r, $ce, $db)->go($setID);
  152           } elsif ($setArg eq "users") {
  153             $result = WeBWorK::ContentGenerator::Instructor::Assigner->new($r, $ce, $db)->go($setID);
  154           }
  155         } else {
  156           $result = WeBWorK::ContentGenerator::Instructor::ProblemSetList->new($r, $ce, $db)->go;
  157         }
  158       } elsif ($instructorArgument eq "pgProblemEditor") {
  159         $result = WeBWorK::ContentGenerator::Instructor::PGProblemEditor->new($r, $ce, $db)->go(@components);
  160       }
  161     } elsif ($arg eq "options") {
  162       $result = WeBWorK::ContentGenerator::Options->new($r, $ce, $db)->go;
  163     } elsif ($arg eq "feedback") {
  164       $result = WeBWorK::ContentGenerator::Feedback->new($r, $ce, $db)->go;
  165     } elsif ($arg eq "logout") {
  166       $result = WeBWorK::ContentGenerator::Logout->new($r, $ce, $db)->go;
  167     } elsif ($arg eq "test") {
  168       $result = WeBWorK::ContentGenerator::Test->new($r, $ce, $db)->go;
  169     } elsif ($arg eq "quiz_mode" ) {
  170       # Gateway quiz capability -- very similar to problem set (initially)
  171       $result = WeBWorK::ContentGenerator::GatewayQuiz->new($r, $ce, $db)->go(@components);
  172     } else { # We've got the name of a problem set.
  173       my $problem_set = $arg;
  174       my $ps_arg = shift @components;
  175 
  176       if (!defined $ps_arg) {
  177         # list the problems in the problem set
  178         $result = WeBWorK::ContentGenerator::ProblemSet->new($r, $ce, $db)->go($problem_set);
  179       } else {
  180         # We've got the name of a problem
  181         my $problem = $ps_arg;
  182 
  183         #$WeBWorK::timer0 = WeBWorK::Timing->new("Problem $course:$problem_set/$problem");
  184         #$WeBWorK::timer0->start;
  185         my $result = WeBWorK::ContentGenerator::Problem->new($r, $ce, $db)->go($problem_set, $problem);
  186         #$WeBWorK::timer0->stop;
  187         #$WeBWorK::timer0->save;
  188         return $result;
  189 
  190 
  191       }
  192     }
  193   }
  194 
  195   #$dispatchTimer->stop;
  196 
  197   return $result;
  198 }
  199 
  200 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9