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

Annotation of /trunk/webwork2/lib/Apache/WeBWorK.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 455 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9