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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9