Parent Directory
|
Revision Log
Revision 353 - (view) (download) (as text)
| 1 : | malsyned | 313 | package WeBWorK::ContentGenerator; |
| 2 : | malsyned | 305 | |
| 3 : | malsyned | 323 | use CGI qw(-compile :html :form); |
| 4 : | use Apache::Constants qw(:common); | ||
| 5 : | |||
| 6 : | malsyned | 313 | # This is a superclass for Apache::WeBWorK's content generators. |
| 7 : | # You are /definitely/ encouraged to read this file, since there are | ||
| 8 : | # "abstract" functions here which show aproximately what form you would | ||
| 9 : | # want over-ridden sub-classes to follow. go() is a particularly pertinent | ||
| 10 : | # example. | ||
| 11 : | |||
| 12 : | malsyned | 305 | # new(Apache::Request, WeBWorK::CourseEnvironment) |
| 13 : | sub new($$$) { | ||
| 14 : | malsyned | 323 | my $invocant = shift; |
| 15 : | my $class = ref($invocant) || $invocant; | ||
| 16 : | malsyned | 305 | my $self = {}; |
| 17 : | ($self->{r}, $self->{courseEnvironment}) = @_; | ||
| 18 : | bless $self, $class; | ||
| 19 : | return $self; | ||
| 20 : | } | ||
| 21 : | |||
| 22 : | malsyned | 323 | |
| 23 : | malsyned | 313 | # This is a quick and dirty function to print out all (or almost all) of the |
| 24 : | # fields in a form in a specified format. As you can see from the print | ||
| 25 : | # statement, it just prints out $begining$name$middle$value$end for every | ||
| 26 : | # field who's name doesn't match $qr_omit, a quoted regex. | ||
| 27 : | # In it's current incarnation, it should be called from subclasses only, | ||
| 28 : | # by saying $self->print_form_data. Of course, you could construct a | ||
| 29 : | # hashref with ->{r} being an Apache::Request, I suppose. | ||
| 30 : | malsyned | 305 | |
| 31 : | malsyned | 313 | sub print_form_data { |
| 32 : | my ($self, $begin, $middle, $end, $qr_omit) = @_; | ||
| 33 : | malsyned | 353 | my $return_string = ""; |
| 34 : | malsyned | 313 | |
| 35 : | $r=$self->{r}; | ||
| 36 : | my @form_data = $r->param; | ||
| 37 : | foreach my $name (@form_data) { | ||
| 38 : | next if ($qr_omit and $name =~ /$qr_omit/); | ||
| 39 : | my @values = $r->param($name); | ||
| 40 : | foreach my $value (@values) { | ||
| 41 : | malsyned | 353 | $return_string .= "$begin$name$middle$value$end"; |
| 42 : | malsyned | 313 | } |
| 43 : | } | ||
| 44 : | malsyned | 353 | |
| 45 : | return $return_string; | ||
| 46 : | malsyned | 313 | } |
| 47 : | |||
| 48 : | malsyned | 323 | sub hidden_authen_fields { |
| 49 : | my $self = shift; | ||
| 50 : | my $r = $self->{r}; | ||
| 51 : | my $courseEnvironment = $self->{courseEnvironment}; | ||
| 52 : | my $html = ""; | ||
| 53 : | |||
| 54 : | foreach $param ("user","key") { | ||
| 55 : | my $value = $r->param($param); | ||
| 56 : | $html .= input({-type=>"hidden",-name=>"$param",-value=>"$value"}); | ||
| 57 : | } | ||
| 58 : | return $html; | ||
| 59 : | } | ||
| 60 : | |||
| 61 : | malsyned | 349 | sub pre_header_initialize {} |
| 62 : | malsyned | 313 | |
| 63 : | malsyned | 349 | sub header { |
| 64 : | malsyned | 305 | my $self = shift; |
| 65 : | malsyned | 349 | my $r=$self->{r}; |
| 66 : | $r->content_type('text/html'); | ||
| 67 : | $r->send_http_header(); | ||
| 68 : | } | ||
| 69 : | |||
| 70 : | sub initialize {} | ||
| 71 : | |||
| 72 : | sub title { | ||
| 73 : | malsyned | 353 | return "Superclass"; |
| 74 : | malsyned | 349 | } |
| 75 : | |||
| 76 : | sub body { | ||
| 77 : | print "Generated content"; | ||
| 78 : | malsyned | 353 | ""; |
| 79 : | malsyned | 349 | } |
| 80 : | |||
| 81 : | malsyned | 353 | sub logo { |
| 82 : | my $self = shift; | ||
| 83 : | return $self->{courseEnvironment}->{urls}->{logo}; | ||
| 84 : | } | ||
| 85 : | |||
| 86 : | sub htdocs_base { | ||
| 87 : | my $self = shift; | ||
| 88 : | return $self->{courseEnvironment}->{urls}->{base}; | ||
| 89 : | } | ||
| 90 : | |||
| 91 : | malsyned | 349 | sub go { |
| 92 : | my $self = shift; | ||
| 93 : | malsyned | 323 | my $r = $self->{r}; |
| 94 : | my $courseEnvironment = $self->{courseEnvironment}; | ||
| 95 : | |||
| 96 : | malsyned | 349 | $self->pre_header_initialize(@_); |
| 97 : | $self->header(@_); return OK if $r->header_only; | ||
| 98 : | $self->initialize(@_); | ||
| 99 : | malsyned | 305 | |
| 100 : | malsyned | 349 | my $templateFile = $courseEnvironment->{templates}->{system}; |
| 101 : | |||
| 102 : | open(TEMPLATE, $templateFile) or die "Couldn't open template $templateFile"; | ||
| 103 : | my @template = <TEMPLATE>; | ||
| 104 : | close TEMPLATE; | ||
| 105 : | |||
| 106 : | foreach my $line (@template) { | ||
| 107 : | malsyned | 353 | # This is incremental regex processing. |
| 108 : | # the /c is so that pos($line) doesn't die when the regex fails. | ||
| 109 : | while ($line =~ m/\G(.*?)<!--#(.*?)\s*-->/gc) { | ||
| 110 : | malsyned | 349 | print "$1"; |
| 111 : | print $self->$2(@_) if $self->can($2); | ||
| 112 : | } | ||
| 113 : | # I thought I could use pos($line) here, but /noooooo/ | ||
| 114 : | malsyned | 353 | print substr $line, pos($line); |
| 115 : | malsyned | 349 | } |
| 116 : | |||
| 117 : | return OK; | ||
| 118 : | malsyned | 313 | } |
| 119 : | |||
| 120 : | 1; |
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |