| 1 | package WeBWorK::ContentGenerator |
1 | package WeBWorK::ContentGenerator; |
|
|
2 | |
|
|
3 | # This is a superclass for Apache::WeBWorK's content generators. |
|
|
4 | # You are /definitely/ encouraged to read this file, since there are |
|
|
5 | # "abstract" functions here which show aproximately what form you would |
|
|
6 | # want over-ridden sub-classes to follow. go() is a particularly pertinent |
|
|
7 | # example. |
| 2 | |
8 | |
| 3 | # new(Apache::Request, WeBWorK::CourseEnvironment) |
9 | # new(Apache::Request, WeBWorK::CourseEnvironment) |
| 4 | sub new($$$) { |
10 | sub new($$$) { |
| 5 | my $class = shift; |
11 | my $proto = shift; |
|
|
12 | my $class = ref($proto) || $proto; |
| 6 | my $self = {}; |
13 | my $self = {}; |
| 7 | ($self->{r}, $self->{courseEnvironment}) = @_; |
14 | ($self->{r}, $self->{courseEnvironment}) = @_; |
| 8 | bless $self, $class; |
15 | bless $self, $class; |
| 9 | return $self; |
16 | return $self; |
| 10 | } |
17 | } |
| 11 | |
18 | |
| 12 | # standard_header(Apache::Request, Content-type, header => "value" ...) |
19 | # This is a quick and dirty function to print out all (or almost all) of the |
| 13 | #sub headers($$%) { |
20 | # fields in a form in a specified format. As you can see from the print |
| 14 | # ($r, $ct, %headers) = @_; |
21 | # statement, it just prints out $begining$name$middle$value$end for every |
| 15 | # $r->content_type($ct); |
22 | # field who's name doesn't match $qr_omit, a quoted regex. |
| 16 | # foreach my $key (keys %headers) { |
23 | # In it's current incarnation, it should be called from subclasses only, |
| 17 | # $r->header_out($key, $headers{$key} |
24 | # by saying $self->print_form_data. Of course, you could construct a |
| 18 | # } |
25 | # hashref with ->{r} being an Apache::Request, I suppose. |
| 19 | # $r->send_http_header; |
|
|
| 20 | # |
|
|
| 21 | # return 1 if $r->header_only; |
|
|
| 22 | # return 0; |
|
|
| 23 | #} |
|
|
| 24 | |
26 | |
|
|
27 | sub print_form_data { |
|
|
28 | my ($self, $begin, $middle, $end, $qr_omit) = @_; |
|
|
29 | |
|
|
30 | $r=$self->{r}; |
|
|
31 | my @form_data = $r->param; |
|
|
32 | foreach my $name (@form_data) { |
|
|
33 | next if ($qr_omit and $name =~ /$qr_omit/); |
|
|
34 | my @values = $r->param($name); |
|
|
35 | foreach my $value (@values) { |
|
|
36 | print $begin, $name, $middle, $value, $end; |
|
|
37 | } |
|
|
38 | } |
|
|
39 | } |
|
|
40 | |
|
|
41 | # Abstract as they get, this go() is meant to be over-ridden by |
|
|
42 | # absolutely /anything/ that subclasses it. Most subclasses, however, |
|
|
43 | # will find it a useful thing to copy and modify, rather than writing from |
|
|
44 | # scratch. |
|
|
45 | |
| 25 | sub go($) { |
46 | sub go() { |
| 26 | my $self = shift; |
47 | my $self = shift; |
| 27 | ($r, $ct, %headers) = @_; |
48 | $r = shift; |
| 28 | $r->content_type($ct); |
49 | $r->content_type($ct); |
| 29 | foreach $key (keys %headers) { |
50 | foreach $key (keys %headers) { |
| 30 | $r->header_out($key, $headers{$key} |
51 | $r->header_out($key, $headers{$key}); |
| 31 | } |
52 | } |
| 32 | $r->send_http_header; |
53 | $r->send_http_header; |
| 33 | |
54 | |
| 34 | return OK if $r->header_only; |
55 | return OK if $r->header_only; |
| 35 | |
56 | |
| 36 | |
57 | print "You shouldn't see this. This is only a prototype."; |
|
|
58 | } |
|
|
59 | |
|
|
60 | 1; |