File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.5: download - view: text, annotated - select for diffs
Fri Sep 15 20:03:48 2000 UTC (23 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Now posts stuff

    1: # The LearningOnline Network with CAPA
    2: # Page Handler
    3: #
    4: # (TeX Content Handler
    5: #
    6: # 05/29/00,05/30 Gerd Kortemeyer)
    7: # 08/30,08/31,09/06,09/14,09/15 Gerd Kortemeyer
    8: 
    9: package Apache::lonpage;
   10: 
   11: use strict;
   12: use Apache::Constants qw(:common :http);
   13: use Apache::lonnet();
   14: use GDBM_File;
   15: 
   16: # -------------------------------------------------------------- Module Globals
   17: my %hash;
   18: my @rows;
   19:                   
   20: my %ssibody=();
   21: my %ssibgcolor=();
   22: my %ssitext=();
   23: my %ssilink=();
   24: my %ssivlink=();
   25: my %ssialink=();
   26: my %cellemb=();
   27: 
   28: # ------------------------------------------------------------ Build page table
   29: 
   30: sub tracetable {
   31:     my ($sofar,$rid,$beenhere)=@_;
   32:     my $further=$sofar;
   33:     unless ($beenhere=~/\&$rid\&/) {
   34:        $beenhere.=$rid.'&';  
   35: 
   36:        if (defined($hash{'is_map_'.$rid})) {
   37:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
   38:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
   39:               my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
   40: 	      $sofar=
   41:                 &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
   42:                 '&'.$frid.'&');
   43:               $sofar++;
   44:               if ($hash{'src_'.$frid}) {
   45:                my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
   46:                if (($brepriv eq '2') || ($brepriv eq 'F')) {
   47:                  if (defined($rows[$sofar])) {
   48:                    $rows[$sofar].='&'.$frid;
   49:                  } else {
   50:                    $rows[$sofar]=$frid;
   51:                  }
   52: 	       }
   53: 	      }
   54: 	   }
   55:        } else {
   56:           $sofar++;
   57:           if ($hash{'src_'.$rid}) {
   58:            my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
   59:            if (($brepriv eq '2') || ($brepriv eq 'F')) {
   60:              if (defined($rows[$sofar])) {
   61:                $rows[$sofar].='&'.$rid;
   62:              } else {
   63:                $rows[$sofar]=$rid;
   64:              }
   65: 	   }
   66:           }
   67:        }
   68: 
   69:        if (defined($hash{'to_'.$rid})) {
   70:           map {
   71:               my $now=&tracetable($sofar,$hash{'goesto_'.$_},$beenhere);
   72:               if ($now>$further) { $further=$now; }
   73:           } split(/\,/,$hash{'to_'.$rid});
   74:        }
   75:     }
   76:     return $further;
   77: }
   78: 
   79: sub cell {
   80:     my ($r,$colspan,$rid)=@_;
   81:     $r->print('<td colspan="'.$colspan.'"');
   82:     if ($cellemb{$rid} eq 'ssi') {
   83:        $r->print('>'.$ssibody{$rid});
   84:     } elsif ($cellemb{$rid} eq 'img') {
   85:        $r->print('><img src="'.$hash{'src_'.$rid}.'">');
   86:     }
   87:     $r->print('</td>');
   88: }
   89: 
   90: # ================================================================ Main Handler
   91: 
   92: sub handler {
   93:   my $r=shift;
   94: 
   95: # ------------------------------------------- Set document type for header only
   96: 
   97:   if ($r->header_only) {
   98:        if ($ENV{'browser.mathml'}) {
   99:            $r->content_type('text/xml');
  100:        } else {
  101:            $r->content_type('text/html');
  102:        }
  103:        $r->send_http_header;
  104:        return OK;
  105:    }
  106: 
  107:   my $requrl=$r->uri;
  108: # ----------------------------------------------------------------- Tie db file
  109:   if ($ENV{'request.course.fn'}) {
  110:       my $fn=$ENV{'request.course.fn'};
  111:       if (-e "$fn.db") {
  112:           if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_WRCREAT,0640)) {
  113: # ------------------------------------------------------------------- Hash tied
  114:               my $firstres=$hash{'map_start_'.$requrl};
  115:               my $lastres=$hash{'map_finish_'.$requrl};
  116:               if (($firstres) && ($lastres)) {
  117: # ----------------------------------------------------------------- Render page
  118: 
  119:                   @rows=();
  120: 
  121:                   &tracetable(0,$firstres,'&'.$lastres.'&');
  122:                   if ($hash{'src_'.$lastres}) {
  123:                      my $brepriv=
  124:                         &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
  125:                      if (($brepriv eq '2') || ($brepriv eq 'F')) {
  126:                         $rows[$#rows+1]=''.$lastres;
  127: 		     }
  128: 		  }
  129: 
  130:                   my $i;
  131:                   my $j;
  132:                   my $maxcols=-1;
  133: 
  134: # --------------------------------------------- Get SSI output, post parameters
  135: 
  136:                   for ($i=0;$i<=$#rows;$i++) {
  137: 		     if ($rows[$i]) {
  138:                       my @colcont=split(/\&/,$rows[$i]);
  139:                       $maxcols=$#colcont>$maxcols?$#colcont:$maxcols;
  140:                       map {
  141:                           my $src=$hash{'src_'.$_};
  142:                           $src=~/\.(\w+)$/;
  143:                           $cellemb{$_}=Apache::lonnet::fileembstyle($1);
  144:                           if ($cellemb{$_} eq 'ssi') {
  145: # --------------------------------------------------------- This is an SSI cell
  146: 			      my $prefix=$_.'_';
  147:                               my %posthash=('request.prefix' => $prefix);
  148:                               map {
  149: 				  if ($_=~/^form.$prefix/) {
  150: 				      my $name=$_;
  151:                                       $name=~s/^form.$prefix//;
  152:                                       $posthash{$name}=$ENV{$_};
  153:                                   }
  154:                               } keys %ENV;
  155:                               my $output=Apache::lonnet::ssi($src,%posthash);
  156: 
  157: 			      $ssibody{$_}=$output;
  158: 
  159: # ---------------------------------------------------------------- End SSI cell
  160:                           }
  161:                       } @colcont;
  162:                      } 
  163:                   }
  164:                   if ($maxcols<0) {
  165:                       $r->content_type('text/html');
  166:                       $r->send_http_header;
  167:                       $r->print('<html><body>Empty page.</body></html>');
  168:                   } else {
  169: # ------------------------------------------------------------------ Build page
  170:                       $maxcols++;
  171:                       $r->content_type('text/html');
  172:                       $r->send_http_header;
  173:                       $r->print('<html><body>');
  174:  
  175:                       $r->print('<table cols="'.$maxcols.'" rows="'.
  176:                                  $#rows.'" border="0">');
  177:                       for ($i=0;$i<=$#rows;$i++) {
  178: 			if ($rows[$i]) {
  179:                           $r->print("\n<tr>");
  180:                           my @colcont=split(/\&/,$rows[$i]);
  181:                           my $avespan=int($maxcols/($#colcont+1));
  182:                           my $lastspan=$maxcols-$avespan*$#colcont;
  183:                           for ($j=0;$j<$#colcont;$j++) {
  184:                               &cell($r,$avespan,$colcont[$j]);
  185:                           }
  186:                           &cell($r,$lastspan,$colcont[$#colcont]);
  187:                           $r->print('</tr>');
  188: 		        }
  189:                       }
  190:                       $r->print("\n</table>");
  191: 
  192:                       $r->print('</body></html>');
  193: # -------------------------------------------------------------------- End page
  194:                   }                  
  195: # ------------------------------------------------------------- End render page
  196:               } else {
  197:                   $r->content_type('text/html');
  198:                   $r->send_http_header;
  199: 		  $r->print('<html><body>Page undefined.</body></html>');
  200:               }
  201: # ------------------------------------------------------------------ Untie hash
  202:               unless (untie(%hash)) {
  203:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  204:                        "Could not untie coursemap $fn (browse).</font>"); 
  205:               }
  206: # -------------------------------------------------------------------- All done
  207: 	      return OK;
  208: # ----------------------------------------------- Errors, hash could no be tied
  209:           }
  210:       } 
  211:   }
  212:   $ENV{'user.error.msg'}="$requrl:bre:1:1:Course not initialized";
  213:   return HTTP_NOT_ACCEPTABLE; 
  214: }
  215: 
  216: 1;
  217: __END__
  218: 
  219: 
  220: 
  221: 
  222: 
  223: 
  224: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>