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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 646 Revision 683
15use warnings; 15use warnings;
16use Apache::Constants qw(:common REDIRECT); 16use Apache::Constants qw(:common REDIRECT);
17use Apache::Request; 17use Apache::Request;
18use WeBWorK::Authen; 18use WeBWorK::Authen;
19use WeBWorK::Authz; 19use WeBWorK::Authz;
20use WeBWorK::ContentGenerator::Feedback;
20use WeBWorK::ContentGenerator::Login; 21use WeBWorK::ContentGenerator::Login;
21use WeBWorK::ContentGenerator::Logout; 22use WeBWorK::ContentGenerator::Logout;
22use WeBWorK::ContentGenerator::Hardcopy; 23use WeBWorK::ContentGenerator::Hardcopy;
23use WeBWorK::ContentGenerator::Options; 24use WeBWorK::ContentGenerator::Options;
24use WeBWorK::ContentGenerator::Problem; 25use WeBWorK::ContentGenerator::Problem;
25use WeBWorK::ContentGenerator::ProblemSet; 26use WeBWorK::ContentGenerator::ProblemSet;
26use WeBWorK::ContentGenerator::ProblemSets; 27use WeBWorK::ContentGenerator::ProblemSets;
28use WeBWorK::ContentGenerator::Professor;
27use WeBWorK::ContentGenerator::Test; 29use WeBWorK::ContentGenerator::Test;
28use WeBWorK::CourseEnvironment; 30use WeBWorK::CourseEnvironment;
29 31
30# This module should be installed as a Handler for the location selected for 32# This module should be installed as a Handler for the location selected for
31# WeBWorK on your webserver. Here is an example of a stanza that can be added 33# WeBWorK on your webserver. Here is an example of a stanza that can be added
47 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 49 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
48 50
49 # This stuff is pretty much copied out of the O'Reilly mod_perl book. 51 # This stuff is pretty much copied out of the O'Reilly mod_perl book.
50 # It's for figuring out the basepath. I may change this up if I 52 # It's for figuring out the basepath. I may change this up if I
51 # find a better way to do it. 53 # find a better way to do it.
52 my $path_info = $r->path_info; 54 my $path_info = $r->path_info || "";
53 my $path_translated = $r->lookup_uri($path_info)->filename;
54 my $current_uri = $r->uri; 55 my $current_uri = $r->uri;
55 my $args = $r->args; 56 my $args = $r->args;
56 57
57 # If it's a valid WeBWorK URI, it ends in a /. This is assumed 58 # If it's a valid WeBWorK URI, it ends in a /. This is assumed
58 # alllll over the place. 59 # alllll over the place.
59 unless (substr($current_uri,-1) eq '/') { 60 unless (substr($current_uri,-1) eq '/') {
60 $r->header_out(Location => "$current_uri/" . ($args ? "?$args" : "")); 61 $r->header_out(Location => "$current_uri/" . ($args ? "?$args" : ""));
61 return REDIRECT; 62 return REDIRECT;
63 # *** any post data gets lost here -- fix that.
62 } 64 }
63 65
64 # Create the @components array, which contains the path specified in the URL 66 # Create the @components array, which contains the path specified in the URL
65 my($junk, @components) = split "/", $path_info; 67 my($junk, @components) = split "/", $path_info;
66 my $webwork_root = $r->dir_config('webwork_root'); # From a PerlSetVar in httpd.conf 68 my $webwork_root = $r->dir_config('webwork_root'); # From a PerlSetVar in httpd.conf
67 my $course = shift @components; 69 my $course = shift @components;
68 70
69 # If no course was specified, phreak out. 71 # If no course was specified, phreak out.
70 # Eventually, display a list of courses, or something. 72 # Eventually, display a list of courses, or something.
71 unless (defined $course) { 73 unless (defined $course) {
74 warn "No course specified.\n";
72 return DECLINED; 75 return DECLINED;
76 # *** we should either write a "Courses" module, or redirect to a static page.
73 } 77 }
74 78
75 # Try to get the course environment. 79 # Try to get the course environment.
76 my $course_env = eval {WeBWorK::CourseEnvironment->new($webwork_root, $course);}; 80 my $course_env = eval {WeBWorK::CourseEnvironment->new($webwork_root, $course);};
77 if ($@) { # If there was an error getting the requested course 81 if ($@) { # If there was an error getting the requested course
78 # TODO: display an error page. For now, 404 it. 82 # TODO: display an error page. For now, 404 it.
79 warn $@; 83 warn $@;
80 return DECLINED; 84 return DECLINED;
81 } 85 }
82 86
83 # Freak out if the requested course doesn't exist. For now, this is just a 87 # Freak out if the requested course doesn't exist. For now, this is just a
84 # check to see if the course directory exists. 88 # check to see if the course directory exists.
85 if (!-e $course_env->{webworkDirs}->{courses} . "/$course") { 89 if (!-e $course_env->{webworkDirs}->{courses} . "/$course") {
90 warn "Course directory for $course not found at "
91 . $course_env->{webworkDirs}->{courses} . "/$course" ."\n";
86 return DECLINED; 92 return DECLINED;
87 } 93 }
88 94
89 ### Begin dispatching ### 95 ### Begin dispatching ###
90 96
91 # WeBWorK::Authen::verify erases the passwd field and sets the key field 97 # WeBWorK::Authen::verify erases the passwd field and sets the key field
92 # if login is successful. 98 # if login is successful.
93 if (!WeBWorK::Authen->new($r, $course_env)->verify) { 99 if (!WeBWorK::Authen->new($r, $course_env)->verify) {
100 # *** &verify should throw a descriptive exception on weird failures
94 return WeBWorK::ContentGenerator::Login->new($r, $course_env)->go; 101 return WeBWorK::ContentGenerator::Login->new($r, $course_env)->go;
95 } else { 102 } else {
96 # After we are authenticated, there are some things that need to be 103 # After we are authenticated, there are some things that need to be
97 # sorted out, Authorization-wize, before we start dispatching to individual 104 # sorted out, Authorization-wize, before we start dispatching to individual
98 # content generators. 105 # content generators.
99 my $effectiveUser = $r->param("effectiveUser") || "";
100 my $user = $r->param("user"); 106 my $user = $r->param("user");
107 my $effectiveUser = $r->param("effectiveUser") || $user;
101 my $su_authorized = WeBWorK::Authz->new($r, $course_env)->hasPermissions($user, "become_student", $effectiveUser); 108 my $su_authorized = WeBWorK::Authz->new($r, $course_env)->hasPermissions($user, "become_student", $effectiveUser);
102 # This hoary statement has the effect of forcing effectiveUser to equal user unless 109 $effectiveUser = $user unless $su_authorized;
103 # the user is otherwise authorized.
104 if (!($user ne $effectiveUser && $su_authorized) || !defined $effectiveUser) {
105 $r->param("effectiveUser",$user); 110 $r->param("effectiveUser", $effectiveUser);
106 }
107 111
108 my $arg = shift @components; 112 my $arg = shift @components;
109 if (!defined $arg) { # We want the list of problem sets 113 if (!defined $arg) { # We want the list of problem sets
110 return WeBWorK::ContentGenerator::ProblemSets->new($r, $course_env)->go; 114 return WeBWorK::ContentGenerator::ProblemSets->new($r, $course_env)->go;
111 } elsif ($arg eq "hardcopy") { 115 } elsif ($arg eq "hardcopy") {
112 my $hardcopyArgument = shift @components; 116 my $hardcopyArgument = shift @components;
113 $hardcopyArgument = "" unless defined $hardcopyArgument; 117 $hardcopyArgument = "" unless defined $hardcopyArgument;
118 # *** can i say go(shift || "") here?
114 return WeBWorK::ContentGenerator::Hardcopy->new($r, $course_env)->go($hardcopyArgument); 119 return WeBWorK::ContentGenerator::Hardcopy->new($r, $course_env)->go($hardcopyArgument);
115 } elsif ($arg eq "prof") { 120 } elsif ($arg eq "prof") {
116 # *** 121 return WeBWorK::ContentGenerator::Professor->new($r, $course_env)->go;
117 } elsif ($arg eq "options") { 122 } elsif ($arg eq "options") {
118 return WeBWorK::ContentGenerator::Options->new($r, $course_env)->go; 123 return WeBWorK::ContentGenerator::Options->new($r, $course_env)->go;
124 } elsif ($arg eq "feedback") {
125 return WeBWorK::ContentGenerator::Feedback->new($r, $course_env)->go;
119 } elsif ($arg eq "logout") { 126 } elsif ($arg eq "logout") {
120 return WeBWorK::ContentGenerator::Logout->new($r, $course_env)->go; 127 return WeBWorK::ContentGenerator::Logout->new($r, $course_env)->go;
121 } elsif ($arg eq "test") { 128 } elsif ($arg eq "test") {
129 # *** we should change this name, or remove it altogether.
122 return WeBWorK::ContentGenerator::Test->new($r, $course_env)->go; 130 return WeBWorK::ContentGenerator::Test->new($r, $course_env)->go;
123 } else { # We've got the name of a problem set. 131 } else { # We've got the name of a problem set.
124 my $problem_set = $arg; 132 my $problem_set = $arg;
125 my $ps_arg = shift @components; 133 my $ps_arg = shift @components;
126 134
127 if (!defined $ps_arg) { 135 if (!defined $ps_arg) {
128 # list the problems in the problem set 136 # list the problems in the problem set
129 return WeBWorK::ContentGenerator::ProblemSet->new($r, $course_env)->go($problem_set); 137 return WeBWorK::ContentGenerator::ProblemSet->new($r, $course_env)->go($problem_set);
130 } elsif ($ps_arg eq "hardcopy") { 138 } elsif ($ps_arg eq "hardcopy") {
131 ### 139 # *** do we need this? hardcopy is not being called this way
132 } 140 }
133 else { 141 else {
134 # We've got the name of a problem 142 # We've got the name of a problem
135 my $problem = $ps_arg; 143 my $problem = $ps_arg;
136 return WeBWorK::ContentGenerator::Problem->new($r, $course_env)->go($problem_set, $problem); 144 return WeBWorK::ContentGenerator::Problem->new($r, $course_env)->go($problem_set, $problem);
137 } 145 }
138 } 146 }
139 147
140 } 148 }
141 149
142 # If the dispatcher doesn't know any modules that want to handle 150 # If the dispatcher doesn't know any modules that want to handle
143 # the current path, it'll claim that the path does not exist by 151 # the current path, it'll claim that the path does not exist by
144 # declining the request. 152 # declining the request.
145 return DECLINED; 153 return DECLINED;
146} 154}

Legend:
Removed from v.646  
changed lines
  Added in v.683

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9