[system] / branches / rel-2-1-a1 / webwork2 / lib / WeBWorK / ContentGenerator.pm Repository:
ViewVC logotype

View of /branches/rel-2-1-a1/webwork2/lib/WeBWorK/ContentGenerator.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 349 - (download) (as text) (annotate)
Fri Jun 7 21:37:33 2002 UTC (10 years, 11 months ago) by malsyned
Original Path: trunk/webwork2/lib/WeBWorK/ContentGenerator.pm
File size: 3204 byte(s)
The framework for the template system has been laid in ContentGenerator.
Login.pm is the first module converted to work with that framework.
--Dennis

    1 package WeBWorK::ContentGenerator;
    2 
    3 use CGI qw(-compile :html :form);
    4 use Apache::Constants qw(:common);
    5 
    6 # 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 # new(Apache::Request, WeBWorK::CourseEnvironment)
   13 sub new($$$) {
   14   my $invocant = shift;
   15   my $class = ref($invocant) || $invocant;
   16   my $self = {};
   17   ($self->{r}, $self->{courseEnvironment}) = @_;
   18   bless $self, $class;
   19   return $self;
   20 }
   21 
   22 
   23 # This generates the template code (eventually using a secondary storage
   24 # data source, I hope) for the common elements of all WeBWorK pages.
   25 # Arguments are substitutions for data points within the template.
   26 sub top {
   27   my (
   28     $self,      # invocant
   29     $title,     # Page title
   30   ) = @_;
   31 
   32   my $r = $self->{r};
   33 
   34   print start_html("WeBWorK - $title");
   35 
   36   print h1("WeBWorK $title");
   37 }
   38 
   39 # This generates the "bottom" of pages.  It'll probably be mostly for
   40 # closing <body> and stuff like that.
   41 sub bottom {
   42   my $self = @_;
   43   print end_html();
   44 }
   45 
   46 
   47 # This is a quick and dirty function to print out all (or almost all) of the
   48 # fields in a form in a specified format.  As you can see from the print
   49 # statement, it just prints out $begining$name$middle$value$end for every
   50 # field who's name doesn't match $qr_omit, a quoted regex.
   51 # In it's current incarnation, it should be called from subclasses only,
   52 # by saying $self->print_form_data.  Of course, you could construct a
   53 # hashref with ->{r} being an Apache::Request, I suppose.
   54 
   55 sub print_form_data {
   56   my ($self, $begin, $middle, $end, $qr_omit) = @_;
   57 
   58   $r=$self->{r};
   59   my @form_data = $r->param;
   60   foreach my $name (@form_data) {
   61     next if ($qr_omit and $name =~ /$qr_omit/);
   62     my @values = $r->param($name);
   63     foreach my $value (@values) {
   64       print $begin, $name, $middle, $value, $end;
   65     }
   66   }
   67 }
   68 
   69 sub hidden_authen_fields {
   70   my $self = shift;
   71   my $r = $self->{r};
   72   my $courseEnvironment = $self->{courseEnvironment};
   73   my $html = "";
   74 
   75   foreach $param ("user","key") {
   76     my $value = $r->param($param);
   77     $html .= input({-type=>"hidden",-name=>"$param",-value=>"$value"});
   78   }
   79   return $html;
   80 }
   81 
   82 sub pre_header_initialize {}
   83 
   84 sub header {
   85   my $self = shift;
   86   my $r=$self->{r};
   87   $r->content_type('text/html');
   88   $r->send_http_header();
   89 }
   90 
   91 sub initialize {}
   92 
   93 sub title {
   94   print "Superclass";
   95 }
   96 
   97 sub body {
   98   print "Generated content";
   99 }
  100 
  101 sub go {
  102   my $self = shift;
  103   my $r = $self->{r};
  104   my $courseEnvironment = $self->{courseEnvironment};
  105 
  106   $self->pre_header_initialize(@_);
  107   $self->header(@_); return OK if $r->header_only;
  108   $self->initialize(@_);
  109 
  110   my $templateFile = $courseEnvironment->{templates}->{system};
  111 
  112   open(TEMPLATE, $templateFile) or die "Couldn't open template $templateFile";
  113   my @template = <TEMPLATE>;
  114   close TEMPLATE;
  115 
  116   foreach my $line (@template) {
  117     my $pos = 0;
  118 
  119     while ($line =~ m/\G(.*?)<!--#(.*?)\s*-->/g) {
  120       print "$1";
  121       $pos = pos($line);
  122       print $self->$2(@_) if $self->can($2);
  123     }
  124     # I thought I could use pos($line) here, but /noooooo/
  125     print substr $line, $pos;
  126   }
  127 
  128   return OK;
  129 }
  130 
  131 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9