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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : malsyned 283 # 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 :     # PerlModule Apache::WeBWorK
6 : malsyned 323 # PerlRequire /path/to/webwork/conf/init.pl
7 :     # PerlSetVar webwork_root /path/to/webwork
8 : malsyned 283 # <Location /webwork>
9 :     # SetHandler perl-script
10 : malsyned 323 # PerlHandler Apache::WeBWorK
11 : malsyned 283 # </Location>
12 :    
13 :     package Apache::WeBWorK;
14 :    
15 :     use strict;
16 :     use Apache::Constants qw(:common REDIRECT);
17 : malsyned 290 use Apache::Request;
18 : malsyned 283 use WeBWorK::CourseEnvironment;
19 : malsyned 306 use WeBWorK::Authen;
20 : malsyned 353 use WeBWorK::ContentGenerator::Test;
21 :     use WeBWorK::ContentGenerator::Login;
22 :     use WeBWorK::ContentGenerator::ProblemSets;
23 :     use WeBWorK::ContentGenerator::ProblemSet;
24 :     use WeBWorK::ContentGenerator::Problem;
25 : malsyned 283
26 :     # registering discontent: wanted to call this dispatch, but mod_perl gave me lip
27 :     sub handler() {
28 :     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
29 : malsyned 323
30 :     # This stuff is pretty much copied out of the O'Reilly mod_perl book.
31 :     # It's for figuring out the basepath. I may change this up if I
32 :     # find a better way to do it.
33 : malsyned 283 my $path_info = $r->path_info;
34 :     my $path_translated = $r->lookup_uri($path_info)->filename;
35 :     my $current_uri = $r->uri;
36 : malsyned 329 my $args = $r->args;
37 :    
38 :     # If it's a valid WeBWorK URI, it ends in a /. This is assumed
39 :     # alllll over the place.
40 :     unless (substr($current_uri,-1) eq '/') {
41 : malsyned 343 $r->header_out(Location => "$current_uri/" . ($args ? "?$args" : ""));
42 : malsyned 283 return REDIRECT;
43 :     }
44 :    
45 :     return OK if $r->header_only;
46 : malsyned 323
47 : malsyned 283 my($junk, @components) = split "/", $path_info;
48 :     my $webwork_root = $r->dir_config('webwork_root'); # From a PerlSetVar in httpd.conf
49 :     my $course = shift @components;
50 : malsyned 323
51 :     # Try to get the course environment.
52 : malsyned 283 my $course_env = eval {WeBWorK::CourseEnvironment->new($webwork_root, $course);};
53 : malsyned 323 if ($@) { # If no course exists matching the requested course
54 :     # TODO: display an error page. For now, 404 it.
55 : malsyned 353 warn $@;
56 : malsyned 290 return DECLINED;
57 : malsyned 283 }
58 :    
59 : malsyned 306 # WeBWorK::Authen::verify erases the passwd field and sets the key field
60 :     # if login is successful.
61 :     if (!WeBWorK::Authen->new($r, $course_env)->verify) {
62 : malsyned 353 return WeBWorK::ContentGenerator::Login->new($r, $course_env)->go;
63 : malsyned 306 } else {
64 : malsyned 323 my $arg = shift @components;
65 :     if (!defined $arg) { # We want the list of problem sets
66 : malsyned 353 return WeBWorK::ContentGenerator::ProblemSets->new($r, $course_env)->go;
67 : malsyned 323 } elsif ($arg eq "prof") {
68 :     ###
69 :     } elsif ($arg eq "prefs") {
70 :     ###
71 : malsyned 353 } elsif ($arg eq "test") {
72 :     return WeBWorK::ContentGenerator::Test->new($r, $course_env)->go;
73 : malsyned 323 } else { # We've got the name of a problem set.
74 :     my $problem_set = $arg;
75 :     my $ps_arg = shift @components;
76 :    
77 :     if (!defined $ps_arg) {
78 :     # list the problems in the problem set
79 : malsyned 353 return WeBWorK::ContentGenerator::ProblemSet->new($r, $course_env)->go($problem_set);
80 : malsyned 323 } elsif ($ps_arg eq "hardcopy") {
81 :     ###
82 :     }
83 :     else {
84 :     # We've got the name of a problem
85 :     my $problem = $ps_arg;
86 : malsyned 353 return WeBWorK::ContentGenerator::Problem->new($r, $course_env)->go($problem_set, $problem);
87 : malsyned 323 }
88 :     }
89 :    
90 : malsyned 290 if (1) {
91 : malsyned 353 return WeBWorK::ContentGenerator::Test->new($r, $course_env)->go;
92 : malsyned 290 }
93 : malsyned 306 }
94 : malsyned 283
95 : malsyned 323 # If the dispatcher doesn't know any modules that want to handle
96 :     # the current path, it'll claim that the path does not exist by
97 :     # declining the request.
98 : malsyned 306 return DECLINED;
99 : malsyned 283 }
100 :    
101 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9