[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 746 - (view) (download) (as text)

1 : sh002i 448 ################################################################################
2 : sh002i 494 # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project
3 : sh002i 448 # $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 : sh002i 699 use WeBWorK::Constants qw(WEBWORK_HOME);
21 : sh002i 667 use WeBWorK::ContentGenerator::Feedback;
22 : malsyned 353 use WeBWorK::ContentGenerator::Login;
23 : sh002i 646 use WeBWorK::ContentGenerator::Logout;
24 : sh002i 476 use WeBWorK::ContentGenerator::Hardcopy;
25 : sh002i 644 use WeBWorK::ContentGenerator::Options;
26 : sh002i 455 use WeBWorK::ContentGenerator::Problem;
27 :     use WeBWorK::ContentGenerator::ProblemSet;
28 : malsyned 353 use WeBWorK::ContentGenerator::ProblemSets;
29 : malsyned 670 use WeBWorK::ContentGenerator::Professor;
30 : sh002i 455 use WeBWorK::ContentGenerator::Test;
31 :     use WeBWorK::CourseEnvironment;
32 : malsyned 283
33 : sh002i 476 # This module should be installed as a Handler for the location selected for
34 :     # WeBWorK on your webserver. Here is an example of a stanza that can be added
35 :     # to your httpd.conf file to achieve this:
36 :     #
37 :     # <IfModule mod_perl.c>
38 : sh002i 746 # PerlFreshRestart On
39 :     # <Location /webwork>
40 :     # SetHandler perl-script
41 :     # PerlHandler Apache::WeBWorK
42 :     # PerlSetVar webwork_root /path/to/webwork-modperl
43 :     # <Perl>
44 :     # use lib '/path/to/webwork-modperl/lib';
45 :     # use lib '/path/to/webwork-modperl/pglib';
46 :     # </Perl>
47 :     # </Location>
48 : sh002i 476 # </IfModule>
49 : sh002i 455
50 : malsyned 283 sub handler() {
51 : 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
52 : sh002i 476
53 : malsyned 323 # This stuff is pretty much copied out of the O'Reilly mod_perl book.
54 :     # It's for figuring out the basepath. I may change this up if I
55 :     # find a better way to do it.
56 : sh002i 657 my $path_info = $r->path_info || "";
57 : malsyned 283 my $current_uri = $r->uri;
58 : malsyned 329 my $args = $r->args;
59 :    
60 : sh002i 695 $current_uri =~ m/^(.*)$path_info/;
61 :     my $urlRoot = $1;
62 :    
63 : malsyned 329 # If it's a valid WeBWorK URI, it ends in a /. This is assumed
64 :     # alllll over the place.
65 :     unless (substr($current_uri,-1) eq '/') {
66 : malsyned 343 $r->header_out(Location => "$current_uri/" . ($args ? "?$args" : ""));
67 : malsyned 283 return REDIRECT;
68 : sh002i 667 # *** any post data gets lost here -- fix that.
69 : malsyned 283 }
70 :    
71 : malsyned 390 # Create the @components array, which contains the path specified in the URL
72 : malsyned 283 my($junk, @components) = split "/", $path_info;
73 :     my $webwork_root = $r->dir_config('webwork_root'); # From a PerlSetVar in httpd.conf
74 :     my $course = shift @components;
75 : malsyned 323
76 :     # Try to get the course environment.
77 : sh002i 695 my $course_env = eval {WeBWorK::CourseEnvironment->new($webwork_root, $urlRoot, $course);};
78 : malsyned 390 if ($@) { # If there was an error getting the requested course
79 : malsyned 323 # TODO: display an error page. For now, 404 it.
80 : malsyned 353 warn $@;
81 : malsyned 290 return DECLINED;
82 : malsyned 283 }
83 : sh002i 671
84 : sh002i 700 # If no course was specified, redirect to the URL specified by the constant WEBWORK_HOME
85 :     # (this is typically just "/".)
86 :     unless (defined $course) {
87 :     $r->header_out(Location => $course_env->{webworkURLs}->{home});
88 :     return REDIRECT;
89 :     }
90 :    
91 : malsyned 390 # Freak out if the requested course doesn't exist. For now, this is just a
92 :     # check to see if the course directory exists.
93 :     if (!-e $course_env->{webworkDirs}->{courses} . "/$course") {
94 : sh002i 657 warn "Course directory for $course not found at "
95 :     . $course_env->{webworkDirs}->{courses} . "/$course" ."\n";
96 : malsyned 390 return DECLINED;
97 :     }
98 : malsyned 283
99 : malsyned 390 ### Begin dispatching ###
100 :    
101 : malsyned 306 # WeBWorK::Authen::verify erases the passwd field and sets the key field
102 :     # if login is successful.
103 :     if (!WeBWorK::Authen->new($r, $course_env)->verify) {
104 : malsyned 353 return WeBWorK::ContentGenerator::Login->new($r, $course_env)->go;
105 : malsyned 306 } else {
106 : malsyned 390 # After we are authenticated, there are some things that need to be
107 :     # sorted out, Authorization-wize, before we start dispatching to individual
108 :     # content generators.
109 :     my $user = $r->param("user");
110 : sh002i 683 my $effectiveUser = $r->param("effectiveUser") || $user;
111 : malsyned 390 my $su_authorized = WeBWorK::Authz->new($r, $course_env)->hasPermissions($user, "become_student", $effectiveUser);
112 : sh002i 683 $effectiveUser = $user unless $su_authorized;
113 :     $r->param("effectiveUser", $effectiveUser);
114 : malsyned 390
115 : malsyned 323 my $arg = shift @components;
116 :     if (!defined $arg) { # We want the list of problem sets
117 : malsyned 353 return WeBWorK::ContentGenerator::ProblemSets->new($r, $course_env)->go;
118 : sh002i 476 } elsif ($arg eq "hardcopy") {
119 : sh002i 636 my $hardcopyArgument = shift @components;
120 :     $hardcopyArgument = "" unless defined $hardcopyArgument;
121 : sh002i 476 return WeBWorK::ContentGenerator::Hardcopy->new($r, $course_env)->go($hardcopyArgument);
122 : malsyned 323 } elsif ($arg eq "prof") {
123 : malsyned 670 return WeBWorK::ContentGenerator::Professor->new($r, $course_env)->go;
124 : sh002i 644 } elsif ($arg eq "options") {
125 :     return WeBWorK::ContentGenerator::Options->new($r, $course_env)->go;
126 : sh002i 667 } elsif ($arg eq "feedback") {
127 :     return WeBWorK::ContentGenerator::Feedback->new($r, $course_env)->go;
128 : sh002i 646 } elsif ($arg eq "logout") {
129 :     return WeBWorK::ContentGenerator::Logout->new($r, $course_env)->go;
130 : malsyned 353 } elsif ($arg eq "test") {
131 :     return WeBWorK::ContentGenerator::Test->new($r, $course_env)->go;
132 : malsyned 323 } else { # We've got the name of a problem set.
133 :     my $problem_set = $arg;
134 :     my $ps_arg = shift @components;
135 :    
136 :     if (!defined $ps_arg) {
137 :     # list the problems in the problem set
138 : malsyned 353 return WeBWorK::ContentGenerator::ProblemSet->new($r, $course_env)->go($problem_set);
139 : sh002i 699 } else {
140 : malsyned 323 # We've got the name of a problem
141 :     my $problem = $ps_arg;
142 : sh002i 425 return WeBWorK::ContentGenerator::Problem->new($r, $course_env)->go($problem_set, $problem);
143 : malsyned 323 }
144 :     }
145 :    
146 : malsyned 306 }
147 : sh002i 667
148 : malsyned 323 # If the dispatcher doesn't know any modules that want to handle
149 :     # the current path, it'll claim that the path does not exist by
150 :     # declining the request.
151 : malsyned 306 return DECLINED;
152 : malsyned 283 }
153 :    
154 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9