File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.27: download - view: text, annotated - select for diffs
Mon Oct 1 19:58:05 2001 UTC (22 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Attempt to put grade and submission buttons on page.

    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,09/16,09/19,09/20,09/21,09/23,
    8: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16,
    9: # 08/13/01,08/30,10/1 Gerd Kortemeyer
   10: 
   11: package Apache::lonpage;
   12: 
   13: use strict;
   14: use Apache::Constants qw(:common :http);
   15: use Apache::lonnet();
   16: use Apache::lonxml();
   17: use HTML::TokeParser;
   18: use GDBM_File;
   19: 
   20: # -------------------------------------------------------------- Module Globals
   21: my %hash;
   22: my @rows;
   23: 
   24: # ------------------------------------------------------------------ Euclid gcd
   25: 
   26: sub euclid {
   27:     my ($e,$f)=@_;
   28:     my $a; my $b; my $r;
   29:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
   30:     while ($r!=0) {
   31: 	$a=$b; $b=$r;
   32:         $r=$a%$b;
   33:     }
   34:     return $b;
   35: }
   36: 
   37: # ------------------------------------------------------------ Build page table
   38: 
   39: sub tracetable {
   40:     my ($sofar,$rid,$beenhere)=@_;
   41:     my $further=$sofar;
   42:     unless ($beenhere=~/\&$rid\&/) {
   43:        $beenhere.=$rid.'&';  
   44: 
   45:        if (defined($hash{'is_map_'.$rid})) {
   46:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
   47:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
   48:               my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
   49: 	      $sofar=
   50:                 &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
   51:                 '&'.$frid.'&');
   52:               $sofar++;
   53:               if ($hash{'src_'.$frid}) {
   54:                my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
   55:                if (($brepriv eq '2') || ($brepriv eq 'F')) {
   56:                  if (defined($rows[$sofar])) {
   57:                    $rows[$sofar].='&'.$frid;
   58:                  } else {
   59:                    $rows[$sofar]=$frid;
   60:                  }
   61: 	       }
   62: 	      }
   63: 	   }
   64:        } else {
   65:           $sofar++;
   66:           if ($hash{'src_'.$rid}) {
   67:            my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
   68:            if (($brepriv eq '2') || ($brepriv eq 'F')) {
   69:              if (defined($rows[$sofar])) {
   70:                $rows[$sofar].='&'.$rid;
   71:              } else {
   72:                $rows[$sofar]=$rid;
   73:              }
   74: 	   }
   75:           }
   76:        }
   77: 
   78:        if (defined($hash{'to_'.$rid})) {
   79: 	  my $mincond=1;
   80:           my $next='';
   81:           map {
   82:               my $thiscond=
   83:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
   84:               if ($thiscond>=$mincond) {
   85: 		  if ($next) {
   86: 		      $next.=','.$_.':'.$thiscond;
   87:                   } else {
   88:                       $next=$_.':'.$thiscond;
   89: 		  }
   90:                   if ($thiscond>$mincond) { $mincond=$thiscond; }
   91: 	      }
   92:           } split(/\,/,$hash{'to_'.$rid});
   93:           map {
   94:               my ($linkid,$condval)=split(/\:/,$_);
   95:               if ($condval>=$mincond) {
   96:                 my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
   97:                 if ($now>$further) { $further=$now; }
   98: 	      }
   99:           } split(/\,/,$next);
  100: 
  101:        }
  102:     }
  103:     return $further;
  104: }
  105: 
  106: # ================================================================ Main Handler
  107: 
  108: sub handler {
  109:   my $r=shift;
  110: 
  111: # ------------------------------------------- Set document type for header only
  112: 
  113:   if ($r->header_only) {
  114:        if ($ENV{'browser.mathml'}) {
  115:            $r->content_type('text/xml');
  116:        } else {
  117:            $r->content_type('text/html');
  118:        }
  119:        $r->send_http_header;
  120:        return OK;
  121:    }
  122: 
  123:   my $requrl=$r->uri;
  124: # ----------------------------------------------------------------- Tie db file
  125:   if ($ENV{'request.course.fn'}) {
  126:       my $fn=$ENV{'request.course.fn'};
  127:       if (-e "$fn.db") {
  128:           if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
  129: # ------------------------------------------------------------------- Hash tied
  130:               my $firstres=$hash{'map_start_'.$requrl};
  131:               my $lastres=$hash{'map_finish_'.$requrl};
  132:               if (($firstres) && ($lastres)) {
  133: # ----------------------------------------------------------------- Render page
  134: 
  135:                   @rows=();
  136: 
  137:                   &tracetable(0,$firstres,'&'.$lastres.'&');
  138:                   if ($hash{'src_'.$lastres}) {
  139:                      my $brepriv=
  140:                         &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
  141:                      if (($brepriv eq '2') || ($brepriv eq 'F')) {
  142:                         $rows[$#rows+1]=''.$lastres;
  143: 		     }
  144: 		  }
  145: 
  146: # ------------------------------------------------------------ Add to symb list
  147: 
  148:                   my $i;
  149:                   my %symbhash=();
  150:                   for ($i=0;$i<=$#rows;$i++) {
  151: 		     if ($rows[$i]) {
  152:                         my @colcont=split(/\&/,$rows[$i]);
  153:                         map {
  154:                            $symbhash{$hash{'src_'.$_}}='';
  155: 		        } @colcont;
  156: 		     }
  157: 		  }
  158:                   &Apache::lonnet::symblist($requrl,%symbhash);
  159: 
  160: # ------------------------------------------------------------------ Page parms
  161: 
  162:                   my $j;
  163:                   my $lcm=1;
  164:                   my $contents=0;
  165:                   my $nforms=0;
  166:                   
  167:                   my %ssibody=();
  168:                   my %ssibgcolor=();
  169:                   my %ssitext=();
  170:                   my %ssilink=();
  171:                   my %ssivlink=();
  172:                   my %ssialink=();
  173:      
  174:                   my %metalink=();
  175: 
  176:                   my %cellemb=();
  177: 
  178:                   my $allscript='';
  179:                   my $allmeta='';
  180: 
  181:                   my $isxml=0;
  182:                   my $xmlheader='';
  183:                   my $xmlbody='';
  184: 
  185: # --------------------------------------------- Get SSI output, post parameters
  186: 
  187:                   for ($i=0;$i<=$#rows;$i++) {
  188: 		     if ($rows[$i]) {
  189: 		      $contents++;
  190:                       my @colcont=split(/\&/,$rows[$i]);
  191:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  192:                       map {
  193:                           my $src=$hash{'src_'.$_};
  194:                           $src=~/\.(\w+)$/;
  195:                           $metalink{$_}=$src.'.meta';
  196:                           $cellemb{$_}=Apache::lonnet::fileembstyle($1);
  197:                           if ($cellemb{$_} eq 'ssi') {
  198: # --------------------------------------------------------- This is an SSI cell
  199: 			      my $prefix=$_.'_';
  200:                               my %posthash=('request.prefix' => $prefix);
  201:                               if (($ENV{'form.'.$prefix.'submit'}) 
  202:                                || ($ENV{'form.all_submit'})) {
  203:                                map {
  204: 				  if ($_=~/^form.$prefix/) {
  205: 				      my $name=$_;
  206:                                       $name=~s/^form.$prefix//;
  207:                                       $posthash{$name}=$ENV{$_};
  208:                                   }
  209:                                } keys %ENV;
  210: 			      }
  211:                               my $output=Apache::lonnet::ssi($src,%posthash);
  212:                               my $parser=HTML::TokeParser->new(\$output);
  213:                               my $token;
  214:                               my $thisdir=$src;
  215:                               my $bodydef=0;
  216:                               my $thisxml=0;
  217:                               my @rlinks=();
  218:                               if ($output=~/\?xml/) {
  219:                                  $isxml=1;
  220:                                  $thisxml=1;
  221:                                  $output=~
  222:          /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
  223:                                  $xmlheader=$1;
  224: 			      }
  225:                               while ($token=$parser->get_token) {
  226: 				if ($token->[0] eq 'S') {
  227:                                   if ($token->[1] eq 'a') {
  228: 				      if ($token->[2]->{'href'}) {
  229:                                          $rlinks[$#rlinks+1]=
  230: 					     $token->[2]->{'href'};
  231: 				      }
  232: 				  } elsif ($token->[1] eq 'img') {
  233:                                          $rlinks[$#rlinks+1]=
  234: 					     $token->[2]->{'src'};
  235: 				  } elsif ($token->[1] eq 'embed') {
  236:                                          $rlinks[$#rlinks+1]=
  237: 					     $token->[2]->{'src'};
  238: 				  } elsif ($token->[1] eq 'base') {
  239: 				      $thisdir=$token->[2]->{'href'};
  240: 				  } elsif ($token->[1] eq 'body') {
  241: 				      $bodydef=1;
  242:                                       $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
  243:                                       $ssitext{$_}=$token->[2]->{'text'};
  244:                                       $ssilink{$_}=$token->[2]->{'link'};
  245:                                       $ssivlink{$_}=$token->[2]->{'vlink'};
  246:                                       $ssialink{$_}=$token->[2]->{'alink'};
  247:                                       if ($thisxml) {
  248: 					  $xmlbody=$token->[4];
  249:                                       }
  250:                                   } elsif ($token->[1] eq 'meta') {
  251: 				      $allmeta.="\n".$token->[4].'</meta>';
  252:                                   } elsif (($token->[1] eq 'script') &&
  253:                                            ($bodydef==0)) {
  254: 				      $allscript.="\n\n"
  255:                                                 .$parser->get_text('/script');
  256:                                   }
  257: 			        }
  258: 			      }
  259:                               if ($output=~/\<body[^\>]*\>(.*)/si) {
  260:                                  $output=$1; 
  261:                               }
  262:                               $output=~s/\<\/body\>.*//si;
  263:                               if ($output=~/\<form/si) {
  264: 				  $nforms++;
  265:                                   $output=~s/\<form[^\>]*\>//gsi;
  266:                                   $output=~s/\<\/form[^\>]*\>//gsi;
  267:                                   $output=~
  268: 				      s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([\w\.\:]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
  269:                               }
  270:                               $thisdir=~s/\/[^\/]*$//;
  271: 			      map {
  272: 				  unless (($_=~/^http:\/\//i) ||
  273:                                           ($_=~/^\//)) {
  274: 				      my $newlocation=
  275: 				    &Apache::lonnet::hreflocation($thisdir,$_);
  276:                      $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
  277: 				  }
  278: 			      } @rlinks;
  279: # -------------------------------------------------- Deal with Applet codebases
  280:   $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
  281: 			      $ssibody{$_}=$output;
  282: # ---------------------------------------------------------------- End SSI cell
  283:                           }
  284:                       } @colcont;
  285:                      } 
  286:                   }
  287:                   unless ($contents) {
  288:                       $r->content_type('text/html');
  289:                       $r->send_http_header;
  290:                       $r->print('<html><body>Empty page.</body></html>');
  291:                   } else {
  292: # ------------------------------------------------------------------ Build page
  293: 
  294: # ---------------------------------------------------------------- Send headers
  295:                       if ($isxml) {
  296: 			  $r->content_type('text/xml');
  297:                           $r->send_http_header;
  298:                           $r->print($xmlheader);
  299: 		      } else {
  300:                           $r->content_type('text/html');
  301:                           $r->send_http_header;
  302:                           $r->print('<html>');
  303: 		      }
  304: # ------------------------------------------------------------------------ Head
  305:                       $r->print("\n<head>\n".$allmeta);
  306:                       $allscript=~
  307:        s/\/\/ BEGIN LON\-CAPA Internal.+\/\/ END LON\-CAPA Internal\s//gs;
  308:                       if ($allscript) {
  309: 			  $r->print("\n<script language='JavaScript'>\n".
  310:                                    $allscript."\n</script>\n");
  311:                       }
  312:                       $r->print(&Apache::lonxml::registerurl(1));
  313:                       $r->print("\n</head>\n");
  314: # ------------------------------------------------------------------ Start body
  315:                       if ($isxml) {
  316:                           $r->print($xmlbody);
  317:                       } else {
  318: 			  $r->print(
  319:  '<body bgcolor="#FFFFFF" onLoad="'.&Apache::lonxml::loadevents.
  320:                      '" onUnload="'.&Apache::lonxml::unloadevents.'">');
  321:                       }
  322: # ------------------------------------------------------------------ Start form
  323:                       if ($nforms) {
  324: 			  $r->print('<form method="post" action="'.
  325: 				    $requrl.'">');
  326:                       }
  327: # ----------------------------------------------------------------- Start table
  328:                       $r->print('<table cols="'.$lcm.'" border="0">');
  329:                       for ($i=0;$i<=$#rows;$i++) {
  330: 			if ($rows[$i]) {
  331:                           $r->print("\n<tr>");
  332:                           my @colcont=split(/\&/,$rows[$i]);
  333:                           my $avespan=$lcm/($#colcont+1);
  334:                           for ($j=0;$j<=$#colcont;$j++) {
  335:                               my $rid=$colcont[$j];
  336:                               my $metainfo='<a href="'.
  337:                                     $metalink{$rid}.'" target="LONcatInfo">'.
  338:                           '<img src="/adm/lonMisc/cat_button.gif" border=0>'.
  339: 			  '</img></a>';
  340:                               if (
  341:  ($hash{'src_'.$rid}=~/\.(problem|exam|quiz|assess|survey|form)$/) &&
  342:  (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'}))) {
  343: 				  my ($mapid,$resid)=split(/\./,$rid);
  344:                                  my $symb=
  345:                 &Apache::lonnet::declutter($hash{'map_id_'.$mapid}).
  346:                 '___'.$resid.'___'.
  347: 		&Apache::lonnet::declutter($hash{'src_'.$rid});
  348:                                  $metainfo.=
  349:                   '<a href="/adm/grades?symb='.$symb.
  350:                   '&command=submission" target="LONcatInfo">'.
  351:                           '<img src="/adm/lonMisc/subm_button.gif" border=0>'.
  352: 			  '</img></a>'.
  353:                   '<a href="/adm/grades?symb='.$symb.
  354:                   '&command=viewgrades" target="LONcatInfo">'.
  355:                           '<img src="/adm/lonMisc/pgrd_button.gif" border=0>'.
  356: 			  '</img></a>'.
  357:                   '<a href="/adm/parmset?symb='.$symb.'" target="LONcatInfo">'.
  358:                           '<img src="/adm/lonMisc/pprm_button.gif" border=0>'.
  359: 			      '</img></a>';
  360:                               }
  361:                               $metainfo.='<br></br>';
  362:                               $r->print('<td colspan="'.$avespan.'"');
  363:                               if ($cellemb{$rid} eq 'ssi') {
  364: 				  if ($ssibgcolor{$rid}) {
  365:                                      $r->print(' bgcolor="'.
  366:                                                $ssibgcolor{$rid}.'"');
  367:                                   }
  368:                                   $r->print('>'.$metainfo.'<font');
  369:                                   if ($ssitext{$rid}) {
  370: 				     $r->print(' text="'.$ssitext{$rid}.'"');
  371:                                   }
  372:                                   if ($ssilink{$rid}) {
  373: 				     $r->print(' link="'.$ssilink{$rid}.'"');
  374:                                   }
  375:                                   if ($ssitext{$rid}) {
  376: 				     $r->print(' vlink="'.$ssivlink{$rid}.'"');
  377:                                   }
  378:                                   if ($ssialink{$rid}) {
  379: 				     $r->print(' alink="'.$ssialink{$rid}.'"');
  380:                                   }
  381:                             
  382:                                   $r->print('>'.$ssibody{$rid}.'</font>');
  383:                               } elsif ($cellemb{$rid} eq 'img') {
  384:                                   $r->print('>'.$metainfo.'<img src="'.
  385:                                     $hash{'src_'.$rid}.'"></img>');
  386: 			      } elsif ($cellemb{$rid} eq 'emb') {
  387:                                   $r->print('>'.$metainfo.'<embed src="'.
  388:                                     $hash{'src_'.$rid}.'"></embed>');
  389:                               }
  390:                               $r->print('</td>');
  391:                           }
  392:                           $r->print('</tr>');
  393: 		        }
  394:                       }
  395:                       $r->print("\n</table>");
  396: # ---------------------------------------------------------------- Submit, etc.
  397:                       if ($nforms) {
  398:                           $r->print(
  399: 	                  '<input name="all_submit" value="Submit All" type="'.
  400: 			  (($nforms>1)?'submit':'hidden').'"></input></form>');
  401:                       }
  402:                       $r->print('</body>'.&Apache::lonxml::xmlend());
  403: # -------------------------------------------------------------------- End page
  404:                   }                  
  405: # ------------------------------------------------------------- End render page
  406:               } else {
  407:                   $r->content_type('text/html');
  408:                   $r->send_http_header;
  409: 		  $r->print('<html><body>Page undefined.</body></html>');
  410:               }
  411: # ------------------------------------------------------------------ Untie hash
  412:               unless (untie(%hash)) {
  413:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  414:                        "Could not untie coursemap $fn (browse).</font>"); 
  415:               }
  416: # -------------------------------------------------------------------- All done
  417: 	      return OK;
  418: # ----------------------------------------------- Errors, hash could no be tied
  419:           }
  420:       } 
  421:   }
  422:   $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  423:   return HTTP_NOT_ACCEPTABLE; 
  424: }
  425: 
  426: 1;
  427: __END__
  428: 
  429: 
  430: 
  431: 
  432: 
  433: 
  434: 

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