File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.142: download - view: text, annotated - select for diffs
Tue Oct 4 20:39:57 2022 UTC (18 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Provide more information to privileged users if map could not be loaded
  during course initialization because file was missing.

    1: # The LearningOnline Network with CAPA
    2: # Page Handler
    3: #
    4: # $Id: lonpage.pm,v 1.142 2022/10/04 20:39:57 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: 
   31: 
   32: 
   33: package Apache::lonpage;
   34: 
   35: use strict;
   36: use Apache::Constants qw(:common :http);
   37: use Apache::lonnet;
   38: use Apache::loncommon();
   39: use Apache::lonhtmlcommon;
   40: use Apache::lonxml();
   41: use Apache::lonlocal;
   42: use Apache::lonmenu;
   43: use Apache::lonhomework;
   44: use Apache::lonparmset;
   45: use Apache::lonenc();
   46: use HTML::TokeParser;
   47: use HTML::Entities();
   48: use GDBM_File;
   49: use Apache::lonsequence;
   50: use lib '/home/httpd/lib/perl/';
   51: use LONCAPA;
   52:  
   53: 
   54: # -------------------------------------------------------------- Module Globals
   55: my %hash;
   56: my @rows;
   57: 
   58: # ------------------------------------------------------------------ Euclid gcd
   59: 
   60: sub euclid {
   61:     my ($e,$f)=@_;
   62:     my $a; my $b; my $r;
   63:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
   64:     while ($r!=0) {
   65: 	$a=$b; $b=$r;
   66:         $r=$a%$b;
   67:     }
   68:     return $b;
   69: }
   70: 
   71: # ------------------------------------------------------------ Build page table
   72: 
   73: sub tracetable {
   74:     my ($sofar,$rid,$beenhere)=@_;
   75:     my $further=$sofar;
   76:     my $randomout=0;
   77:     unless ($env{'request.role.adv'}) {
   78:         $randomout = $hash{'randomout_'.$rid};
   79:     }
   80:     unless ($beenhere=~/\&$rid\&/) {
   81:         $beenhere.=$rid.'&';
   82:         unless ($randomout) {
   83:             if (defined($hash{'is_map_'.$rid})) {
   84:                 if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
   85:                     (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
   86:                     my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
   87: 	            $sofar=
   88:                        &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
   89:                        '&'.$frid.$beenhere);
   90:                     $sofar++;
   91:                     if ($hash{'src_'.$frid}) {
   92:                         my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
   93:                         if (($brepriv eq '2') || ($brepriv eq 'F')) {
   94:                             if (defined($rows[$sofar])) {
   95:                                 $rows[$sofar].='&'.$frid;
   96:                             } else {
   97:                                 $rows[$sofar]=$frid;
   98:                             }
   99: 	                }
  100: 	            }
  101: 	        }
  102:             } else {
  103:                 $sofar++;
  104:                 if ($hash{'src_'.$rid}) {
  105:                     my ($mapid,$resid)=split(/\./,$rid);
  106:                     my $symb = &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$hash{'src_'.$rid});
  107:                     my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid},$symb);
  108:                     if (($brepriv eq '2') || ($brepriv eq 'F')) {
  109:                         if (defined($rows[$sofar])) {
  110:                             $rows[$sofar].='&'.$rid;
  111:                         } else {
  112:                             $rows[$sofar]=$rid;
  113:                         }
  114: 	            }
  115:                 }
  116:             }
  117:         }
  118: 
  119:         if (defined($hash{'to_'.$rid})) {
  120: 	    my $mincond=1;
  121:             my $next='';
  122:             foreach (split(/\,/,$hash{'to_'.$rid})) {
  123:                 my $thiscond=
  124:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  125:                 if ($thiscond>=$mincond) {
  126: 		    if ($next) {
  127: 		        $next.=','.$_.':'.$thiscond;
  128:                     } else {
  129:                         $next=$_.':'.$thiscond;
  130: 		    }
  131:                     if ($thiscond>$mincond) { $mincond=$thiscond; }
  132: 	        }
  133:             }
  134:             foreach (split(/\,/,$next)) {
  135:                 my ($linkid,$condval)=split(/\:/,$_);
  136:                 if ($condval>=$mincond) {
  137:                     my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
  138:                     if ($now>$further) { $further=$now; }
  139: 	        }
  140:             }
  141:         }
  142:     }
  143:     return $further;
  144: }
  145: 
  146: # ================================================================ Main Handler
  147: 
  148: sub handler {
  149:   my $r=shift;
  150: 
  151: # ------------------------------------------- Set document type for header only
  152: 
  153:   if ($r->header_only) {
  154:        if ($env{'browser.mathml'}) {
  155:            &Apache::loncommon::content_type($r,'text/xml');
  156:        } else {
  157:            &Apache::loncommon::content_type($r,'text/html'); 
  158:        }
  159:        $r->send_http_header;
  160:        return OK;
  161:    }
  162:   
  163:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  164:                                           ['forceselect','launch']);
  165:   my $number_of_columns = 1;
  166:   my $requrl=$r->uri;  
  167:   my $target = $env{'form.grade_target'};
  168: 
  169: # Short term solution: define target as 'tex_answer' when retrieving answers
  170: # for resources in a .page when generating printouts.
  171: # A better long-term fix would be to modify the way problem rendering, and 
  172: # answer rendering are retrieved for individual resources when printing a .page,
  173: # so rendered problem and answer are sequential for individual resources in 
  174: # the .page
  175: #
  176:   if ($target eq 'answer') {
  177:       if ($env{'form.answer_output_mode'} eq 'tex') {
  178:           $target = 'tex_answer';
  179:       }
  180:   }
  181: #  &Apache::lonnet::logthis("Got a target of $target");
  182:   if ($target eq 'meta') {
  183:       &Apache::loncommon::content_type($r,'text/html');
  184:       $r->send_http_header;
  185:       return OK;
  186:   }
  187: # ----------------------------------------------------------------- Tie db file
  188:   if (($env{'request.course.fn'}) && (!$env{'form.forceselect'})) {
  189:       my $fn=$env{'request.course.fn'};
  190:       if (-e "$fn.db") {
  191:           my %buttonshide;
  192:           my $hostname = $r->hostname();
  193:           my $lonhost = $r->dir_config('lonHostID');
  194:           my $ip = &Apache::lonnet::get_host_ip($lonhost);
  195:           if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
  196: # ------------------------------------------------------------------- Hash tied
  197:               my $firstres=$hash{'map_start_'.$requrl};
  198:               my $lastres=$hash{'map_finish_'.$requrl};
  199:               if (($firstres) && ($lastres)) {
  200: # ------------------------------------------------------------- Countdown Timer
  201:                   my $now = time;
  202:                   my ($pagefirstaccess,%hastimeleft,%countdowndisp,%donebutton,
  203:                       %donebtnextra,%buttonbytime,$donetime,$symbtosetdone);
  204:                   my ($pagesymb,$courseid,$domain,$name)=&Apache::lonnet::whichuser();
  205:                   unless ($pagesymb) {
  206:                       $pagesymb=&Apache::lonnet::symbread($requrl);
  207:                   }
  208:                   if ($pagesymb && ($courseid ne '') && ($domain ne '') && ($name ne '')) {
  209:                       my %times=&Apache::lonnet::get('firstaccesstimes',
  210:                                                      [$courseid."\0".$pagesymb],
  211:                                                      $domain,$name);
  212:                       if ($times{$courseid."\0".$pagesymb} =~ /^\d+$/) {
  213:                           $pagefirstaccess = $times{$courseid."\0".$pagesymb};
  214:                           if ($pagefirstaccess && $env{'form.LC_interval_done'} eq 'true') {
  215:                               $donetime = $now - $pagefirstaccess;
  216:                           }
  217:                       }
  218:                   }
  219: 
  220: # ----------------------------------------------------------------- Render page
  221: 
  222:                   @rows=();
  223: 
  224:                   &tracetable(0,$firstres,'&');
  225: 
  226: # ------------------------------------------------------------ Add to symb list
  227: 
  228:                   my $i;
  229:                   my %symbhash=();
  230:                   for ($i=0;$i<=$#rows;$i++) {
  231: 		     if ($rows[$i]) {
  232:                         my @colcont=split(/\&/,$rows[$i]);
  233:                         foreach my $rid (@colcont) {
  234: 			    my ($mapid,$resid)=split(/\./,$rid);
  235: 			    $symbhash{$hash{'src_'.$rid}}=
  236: 				[$hash{'src_'.$rid},$resid];
  237:                             if (($donetime) && ($symbtosetdone eq '')) {
  238:                                 my $src = $hash{'src_'.$rid};
  239:                                 if ($hash{'encrypted_'.$rid}) {
  240:                                     $src=&Apache::lonenc::encrypted($src);
  241:                                 }
  242:                                 my ($mapid,$resid)=split(/\./,$rid);
  243:                                 my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$src);
  244:                                 if ($src =~ /$LONCAPA::assess_re/) {
  245:                                     my @interval=&Apache::lonnet::EXT("resource.0.interval",$symb);
  246:                                     if (@interval > 1) {
  247:                                         if (($interval[1] eq 'map') && ($pagefirstaccess)) {
  248:                                             my ($timelimit) = ($interval[0] =~ /^(\d+)/);
  249:                                             if ($timelimit) {
  250:                                                 if ($pagefirstaccess + $timelimit > $now) {
  251:                                                     $symbtosetdone = $symb;
  252:                                                 }
  253:                                             }
  254:                                         }
  255:                                     }
  256:                                 }
  257:                             }
  258: 		        }
  259: 		     }
  260: 		  }
  261:                   &Apache::lonnet::symblist($requrl,%symbhash);
  262: 
  263: # ------------------------------------------------------------------ Page parms
  264: 
  265:                   my $j;
  266:                   my $lcm=1;
  267:                   my $contents=0;
  268:                   my $nforms=0;
  269:                   my $nuploads=0;
  270:                   my $ntimers=0;
  271:                   my %turninpaths;
  272:                   my %multiresps;
  273:                   my $turninparent;
  274:                   
  275:                   my %ssibody=();
  276:                   my %ssibgcolor=();
  277:                   my %ssitext=();
  278:                   my %ssilink=();
  279:                   my %ssivlink=();
  280:                   my %ssialink=();
  281:                   my %cssrefs=();
  282:                   my %httpref=();
  283:      
  284:                   my %cellemb=();
  285:                   my %cellexternal=();
  286: 
  287:                   my $allscript='';
  288:                   my $allmeta='';
  289: 
  290:                   my $isxml=0;
  291:                   my $xmlheader='';
  292:                   my $xmlbody='';
  293: 
  294: # ---------------------------------------------------------- Handle Done button
  295: 
  296:                   # Set the event timer to zero if the "done button" was clicked.
  297:                   if ($donetime && $symbtosetdone) {
  298:                       &Apache::lonparmset::storeparm_by_symb_inner($symbtosetdone,'0_interval',
  299:                                                                    2,$donetime,'date_interval',
  300:                                                                    $name,$domain);
  301:                       undef($env{'form.LC_interval_done'});
  302:                   }
  303: 
  304: # --------------------------------------------- Get SSI output, post parameters
  305: 
  306:                   for ($i=0;$i<=$#rows;$i++) {
  307: 		     if ($rows[$i]) {
  308: 		      $contents++;
  309:                       my @colcont=split(/\&/,$rows[$i]);
  310:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  311:                       foreach (@colcont) {
  312:                           my $src=$hash{'src_'.$_};
  313:                           my $plainsrc = $src;
  314:                           my $anchor;
  315:                           if ($hash{'ext_'.$_} eq 'true:') {
  316:                               $cellexternal{$_}=($hash{'ext_'.$_} eq 'true:');
  317:                               $src =~ s{^/ext/}{http://};
  318:                               $src =~ s{http://https://}{https://};
  319:                               if ($src =~ /(\#[^#]+)$/) {
  320:                                   $anchor = $1;
  321:                                   $src =~ s/\#[^#]+$//;
  322:                               }
  323:                           }
  324:                           my $unencsrc = $src;
  325:                           my ($extension)=($src=~/\.(\w+)$/);
  326: 			  if ($hash{'encrypted_'.$_}) {
  327: 			      $src=&Apache::lonenc::encrypted($src);
  328: 			  }
  329:                           my ($mapid,$resid)=split(/\./,$_);
  330:                           my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$plainsrc);
  331:                           unless ($env{'request.role.adv'}) {
  332:                               $buttonshide{$symb} = &Apache::lonnet::EXT("resource.0.buttonshide",$symb);
  333:                           }
  334:                           $cellemb{$_}=
  335: 			      &Apache::loncommon::fileembstyle($extension);
  336:                           if ($cellexternal{$_}) {
  337:                               if (($target eq 'tex') || ($target eq 'tex_answer')) {
  338:                                   my $shown = $src.$anchor;
  339:                                   if (($hash{'encrypted_'.$_}) && (!$env{'request.role.adv'})) {
  340:                                       $shown = &mt('URL not shown (encrypted)');
  341:                                   }
  342:                                   my $title=&Apache::lonnet::gettitle($symb);
  343:                                   $title = &Apache::lonxml::latex_special_symbols($title);
  344:                                   $shown = &Apache::lonxml::latex_special_symbols($shown);
  345:                                   $ssibody{$_} = ' \strut \\\\ \textit{'.$title.'} \strut \\\\ '.$shown.'\\\\';
  346:                               } else {
  347:                                   my $showsrc = $src;
  348:                                   my ($is_pdf,$title,$linktext);
  349:                                   if ($unencsrc =~ /\.pdf$/i) {
  350:                                       $is_pdf = 1;
  351:                                   }
  352:                                   if (($hash{'encrypted_'.$_}) && ($symb)) {
  353:                                       $title=&Apache::lonnet::gettitle(&Apache::lonenc::encrypted($symb));
  354:                                   } else {
  355:                                       $title=&Apache::lonnet::gettitle($symb);
  356:                                   }
  357:                                   if ($env{'browser.mobile'}) {
  358:                                       if ($is_pdf) {
  359:                                           $linktext = &mt('Link to PDF (for mobile devices)');
  360:                                           $ssibody{$_} = &create_extlink($unencsrc,$anchor,$title,$linktext);
  361:                                       } else {
  362:                                           $linktext = &mt('Link to resource');
  363:                                           $ssibody{$_} = &create_extlink($unencsrc,$anchor,$title,$linktext);
  364:                                       }
  365:                                   } else {
  366:                                       my $absolute = $env{'request.use_absolute'};
  367:                                       my $uselink = &Apache::loncommon::is_nonframeable($unencsrc,$absolute,$hostname,$ip);
  368:                                       if (($uselink) || (($ENV{'SERVER_PORT'} == 443) && ($unencsrc =~ m{^http://}))) {
  369:                                           $linktext = &mt('Link to resource');
  370:                                           $ssibody{$_} =  &create_extlink($unencsrc,$anchor,$title,$linktext);
  371:                                       } else {
  372:                                           if (($hash{'encrypted_'.$_}) && ($symb) && (!$env{'request.role.adv'})) {
  373:                                               $showsrc .= '?symb='.&Apache::lonenc::encrypted($symb);
  374:                                           } elsif ($anchor) {
  375:                                               $showsrc .= $anchor;
  376:                                           }
  377:                                           $ssibody{$_} = <<ENDEXT;
  378: <iframe src="$showsrc" width="100%" height="300px">No iframe support!</iframe>
  379: ENDEXT
  380:                                       }
  381:                                   }
  382:                               }
  383:                           } elsif ($cellemb{$_} eq 'ssi') {
  384: # --------------------------------------------------------- This is an SSI cell
  385: 			      my $prefix='p_'.$_.'_';
  386:                               my $idprefix= 'p_'.join('_',($mapid,$resid,''));
  387:                               my %posthash=('request.prefix' => $prefix,
  388: 					    'LONCAPA_INTERNAL_no_discussion' => 'true',
  389: 					    'symb' => $symb);
  390: 			      if (($env{'form.grade_target'} eq 'tex') ||
  391:                                  ($env{'form.answer_output_mode'} eq 'tex')) {
  392: 				  $posthash{'grade_target'}=$env{'form.grade_target'};
  393: 				  $posthash{'textwidth'}=$env{'form.textwidth'};
  394: 				  $posthash{'problem_split'}=$env{'form.problem_split'};
  395: 				  $posthash{'latex_type'}=$env{'form.latex_type'};
  396: 				  $posthash{'rndseed'}=$env{'form.rndseed'};
  397:                                   $posthash{'answer_output_mode'} = $env{'form.answer_output_mode'};
  398: 			      }
  399: 			      my $submitted=$env{'form.all_submit_pressed'};
  400: 			      if (!$submitted) {
  401: 				  foreach my $key (keys(%env)) {
  402: 				      if ($key=~/^\Qform.$prefix\Esubmit_(.+)_pressed$/) {
  403:                                           if ($env{$key}) {
  404:                                               $submitted=1;
  405:                                               last;
  406:                                           }
  407: 				      }
  408:                                   }
  409: 			      }
  410:                               if ($submitted) {
  411: 				  foreach my $key (keys(%env)) {
  412: 				      if ($key=~/^\Qform.$prefix\E/) {
  413: 					  my $name=$key;
  414: 					  $name=~s/^\Qform.$prefix\E//;
  415: 					  $posthash{$name}=$env{$key};
  416:                                       }
  417: 				  }
  418: 				  if ($env{'form.all_submit_pressed'}) {
  419: 				      $posthash{'all_submit'}='yes';
  420: 				  }
  421: 			      } elsif ($env{'form.'.$prefix.'markaccess'} eq 'yes') {
  422: 		                  $posthash{'markaccess'} = $env{'form.'.$prefix.'markaccess'};
  423: 		              }
  424:                               my $output=Apache::lonnet::ssi($src,%posthash);
  425: 			      $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
  426:                               if (($target eq 'tex') || ($target eq 'tex_answer')) {
  427: 				  $output =~ s/^([^&]+)\\begin\{document}//;
  428: 				  $output =~ s/\\end\{document}//;
  429: #				  $output = '\parbox{\minipagewidth}{ '.$output.' }';
  430:                                   #some additional cleanup necessary for LateX (due to limitations of table environment 
  431: 				  $output =~ s/(\\vskip\s*\d+mm)\s*(\\\\)+/$1/g;
  432: 			      }
  433:                               my $matheditor;
  434:                               if ($output =~ /\Qjavascript:LC_mathedit_HWVAL_\E/) {
  435:                                   $matheditor = 'dragmath';
  436:                               } elsif ($output =~ /LCmathField/) {
  437:                                   $matheditor = 'lcmath';
  438:                               }
  439:                               my $parser=HTML::TokeParser->new(\$output);
  440:                               my $token;
  441:                               my $thisdir=$src;
  442:                               my $bodydef=0;
  443:                               my $thisxml=0;
  444:                               my @rlinks=();
  445:                               my @css_hrefs=();
  446:                               if ($output=~/\?xml/) {
  447:                                  $isxml=1;
  448:                                  $thisxml=1;
  449:                                  $output=~
  450:          /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
  451:                                  $xmlheader=$1;
  452: 			      }
  453:                               while ($token=$parser->get_token) {
  454: 				if ($token->[0] eq 'S') {
  455:                                   if ($token->[1] eq 'a') {
  456: 				      if ($token->[2]->{'href'}) {
  457:                                          $rlinks[$#rlinks+1]=
  458: 					     $token->[2]->{'href'};
  459: 				      }
  460: 				  } elsif ($token->[1] eq 'img') {
  461:                                          $rlinks[$#rlinks+1]=
  462: 					     $token->[2]->{'src'};
  463: 				  } elsif ($token->[1] eq 'embed') {
  464:                                          $rlinks[$#rlinks+1]=
  465: 					     $token->[2]->{'src'};
  466: 				  } elsif ($token->[1] eq 'base') {
  467: 				      $thisdir=$token->[2]->{'href'};
  468: 				  } elsif ($token->[1] eq 'body') {
  469: 				      $bodydef=1;
  470:                                       $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
  471:                                       $ssitext{$_}=$token->[2]->{'text'};
  472:                                       $ssilink{$_}=$token->[2]->{'link'};
  473:                                       $ssivlink{$_}=$token->[2]->{'vlink'};
  474:                                       $ssialink{$_}=$token->[2]->{'alink'};
  475:                                       if ($thisxml) {
  476: 					  $xmlbody=$token->[4];
  477:                                       }
  478:                                   } elsif ($token->[1] eq 'meta') {
  479: 				    if ($token->[4] !~ m:/>$:) {
  480: 				      $allmeta.="\n".$token->[4].'</meta>';
  481: 				    } else {
  482: 				      $allmeta.="\n".$token->[4];
  483: 				    }
  484:                                   } elsif (($token->[1] eq 'script') &&
  485:                                            ($bodydef==0)) {
  486: 				      $allscript.="\n\n"
  487:                                                 .$parser->get_text('/script');
  488:                                   } elsif (($token->[1] eq 'link') &&
  489:                                            ($bodydef==0)) {
  490:                                       if (($token->[2]->{'href'} !~ m{^/adm/}) &&
  491:                                           ($token->[2]->{'rel'} eq 'stylesheet')) {
  492:                                               $css_hrefs[$#css_hrefs+1]=
  493:                                                   $token->[2]->{'href'};
  494: 
  495:                                       }
  496:                                   }
  497: 			        }
  498: 			      }
  499:                               if ($output=~/\<body[^\>]*\>(.*)/si) {
  500:                                  $output=$1; 
  501:                               }
  502:                               $output=~s/\<\/body\>.*//si;
  503:                               if ($output=~/\<form/si) {
  504:                                   my $hastimer; 
  505: 				  $nforms++;
  506:                                   $output=~s/\<form[^\>]*\>//gsi;
  507:                                   $output=~s/\<\/form[^\>]*\>//gsi;
  508:                                   if ($output=~/\<input[^\>]+name\s*=\s*[\'\"]*HWFILE/) {
  509:                                       $nuploads++;
  510:                                   }
  511:                                   if ($output=~/\<input[^\>]+name\s*=\s*[\'\"]*accessbutton/) {
  512:                                       $ntimers++;
  513:                                       $hastimer = 1;
  514:                                   }
  515:                                   $output=~
  516: 				      s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([^\'\"]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
  517:                                   $output=~
  518:                                       s/\<((?:input|select|button|textarea)[^\>]+)id\s*\=\s*[\'\"]*([^\'\"]+)[\'\"]*([^\>]*)\>/\<$1 id="$idprefix$2" $3\>/gsi;
  519:                                   $output=~
  520:                                       s/(\Qthis.form.elements['\E)(HW(?:VAL|CHK)_[^']+\'\]\.(?:value=\'|checked))/$1$prefix$2/gsi;
  521:                                   if ($hastimer) {
  522:                                       $output=~
  523:                                           s/\<(input[^\>]+name=\Q"$prefix\Eaccessbutton"[^\>]+)(?:\Qdocument.markaccess.submit();\E)([^\>]*)\>/\<$1pageTimer(this.form,'$prefix')$2\>/gsi;
  524:                                       $output=~  s/\<(input[^\>]+name=\Q"$prefix\Emarkaccess"[^\>]+value=["'])(?:yes)(['"][^\>]*)\>/\<$1$2\>/gsi;
  525:                                   }
  526:                                   if ($matheditor eq 'dragmath') {
  527:                                       $output=~
  528:                                           s/(\Qjavascript:LC_mathedit_\E)(HWVAL_)([^'"]+?)(\(['"]*)(\QHWVAL_\E\3['"]\)\;void\(0\)\;)/$1$idprefix$2$3$4$idprefix$5/g;
  529:                                       $output=~
  530:                                           s/(function\s+LC_mathedit_)(HWVAL_)([^'"]+?)(\s+\(LCtextline\))/$1$idprefix$2$3$4/g;
  531:                                   } elsif ($matheditor eq 'lcmath') {
  532:                                       $output=~
  533:                                           s/(var\s+LCmathField\s+=\s+document\.getElementById\(['"])([^'"]+?)(['"]\)\;)/$1$idprefix$2$3/g;
  534:                                   }
  535:                                   $output=~
  536:                                       s/(\Q<div id="msg_\E)(\Qsubmit_\E)([^"]*)(\Q" style="display:none">\E)/<input type="hidden" name="$prefix$2$3_pressed" id="$idprefix$2$3_pressed" value="" \/>$1$idprefix$2$3$4/g;
  537:                                   $output=~
  538:                                       s/(\Q<td class="LC_status_\E)(\Qsubmit_\E)([^\"]*)(\s*[^\"]*"\>)/$1$idprefix$2$3$4/g;
  539:                                   if ($nuploads) {
  540:                                        ($turninpaths{$prefix},$multiresps{$prefix}) = 
  541:                                            &Apache::loncommon::get_turnedin_filepath($symb,$env{'user.name'},$env{'user.domain'});
  542:                                        if ($turninparent eq '') {
  543:                                            $turninparent = $turninpaths{$prefix};
  544:                                            $turninparent =~ s{(/[^/]+)$}{}; 
  545:                                        }
  546:                                   }
  547:                                   $output=~
  548:                                       s/\<((?:input|select)[^\>]+\Qjavascript:setSubmittedPart\E)\(\s*[\'\"]([^\'\"]+)[\'\"]*\s*\)/\<$1('$2','$prefix')/gsi;
  549:                                   $output=~
  550:                                       s/\<(input[^\>]+\Qonfocus=\"javascript:disableAutoComplete\E)\(\'([^\']+)\'\)(;\")/\<$1('$idprefix$2')$3/gsi;
  551:                                   unless ($hastimer) {
  552:                                       if ($plainsrc =~ /$LONCAPA::assess_re/) {
  553:                                           %Apache::lonhomework::history =
  554:                                               &Apache::lonnet::restore($symb,$courseid,$domain,$name);
  555:                                           my $type = 'problem';
  556:                                           if ($extension eq 'task') {
  557:                                               $type = 'Task';
  558:                                           }
  559:                                           my ($status,$accessmsg,$slot_name,$slot) =
  560:                                               &Apache::lonhomework::check_slot_access('0',$type,$symb);
  561:                                           undef(%Apache::lonhomework::history);
  562:                                           my $probstatus = &Apache::lonnet::EXT("resource.0.problemstatus",$symb);
  563:                                           if (($status eq 'CAN_ANSWER') || (($status eq 'CANNOT_ANSWER') && 
  564:                                               (($probstatus eq 'no') || ($probstatus eq 'no_feedback_ever'))) ||
  565:                                               (($status eq 'NOT_YET_VIEWED') && ($posthash{'markaccess'} eq 'yes'))) {
  566:                                               my ($slothastime,$timerhastime);
  567:                                               if ($slot_name ne '') {
  568:                                                   if (ref($slot) eq 'HASH') {
  569:                                                       if (($slot->{'starttime'} < $now) &&
  570:                                                           ($slot->{'endtime'} > $now)) {
  571:                                                           $slothastime = $now - $slot->{'endtime'};
  572:                                                       }
  573:                                                   }
  574:                                               }
  575:                                               my $duedate = &Apache::lonnet::EXT("resource.0.duedate",$symb);
  576:                                               my @interval=&Apache::lonnet::EXT("resource.0.interval",$symb);
  577:                                               if (@interval > 1) {
  578:                                                   my $first_access;
  579:                                                   if ($interval[1] eq 'map') {
  580:                                                       my $ignorecache;
  581:                                                       if ($env{'form.'.$prefix.'markaccess'} eq 'yes') {
  582:                                                           $ignorecache = 1;
  583:                                                       }
  584:                                                       $first_access=&Apache::lonnet::get_first_access($interval[1],undef,$pagesymb,$ignorecache);
  585:                                                       if (($first_access) && (!$pagefirstaccess)) {
  586:                                                           $pagefirstaccess = $first_access;
  587:                                                       }
  588:                                                   } else {
  589:                                                       $first_access=&Apache::lonnet::get_first_access($interval[1],$symb);
  590:                                                   }
  591:                                                   if ($first_access > 0) {
  592:                                                       my ($timelimit) = ($interval[0] =~ /^(\d+)/);
  593:                                                       if ($timelimit) {
  594:                                                           my $timeremains = $timelimit + $first_access - $now;
  595:                                                           if ($timeremains > 0) {
  596:                                                               $timerhastime = $timeremains;
  597:                                                           }
  598:                                                       }
  599:                                                   }
  600:                                               }
  601:                                               if (($duedate && $duedate > $now) ||
  602:                                                   (!$duedate && $timerhastime > 0) ||
  603:                                                   ($slot_name ne '' && $slothastime)) {
  604:                                                   if ((@interval > 1 && $timerhastime) ||
  605:                                                       ($type eq 'Task' && $slothastime)) {
  606:                                                       $countdowndisp{$symb} = 'inline';
  607:                                                       if ((@interval > 1) && ($timerhastime)) {
  608:                                                           $hastimeleft{$symb} = $timerhastime;
  609:                                                           if ($pagefirstaccess) {
  610:                                                               my ($timelimit,$usesdone,$donebuttontext,$proctor,$secret);
  611:                                                               ($timelimit,my $donesuffix) = split(/_/,$interval[0],2);
  612:                                                               if ($donesuffix =~ /^done\:([^\:]+)\:(.*)$/) {
  613:                                                                   $usesdone = 'done';
  614:                                                                   $donebuttontext = $1;
  615:                                                                   (undef,$proctor,$secret) = split(/_/,$2);
  616:                                                               } elsif ($donesuffix =~ /^done(|_.+)$/) {
  617:                                                                   $donebuttontext = &mt('Done');
  618:                                                                   ($usesdone,$proctor,$secret) = split(/_/,$donesuffix);
  619:                                                               }
  620:                                                               if ($usesdone eq 'done') {
  621:                                                                   $donebutton{$symb} = $timelimit;
  622:                                                                   push(@{$buttonbytime{$timelimit}},$symb);
  623:                                                                   $donebtnextra{$symb} = {
  624:                                                                                               text    => $donebuttontext,
  625:                                                                                               proctor => $proctor,
  626:                                                                                               secret  => $secret,
  627:                                                                                               type    => $interval[1],
  628:                                                                                          };
  629:                                                               }
  630:                                                           }
  631:                                                       } else {
  632:                                                           $hastimeleft{$symb} = $slothastime;
  633:                                                       }
  634:                                                   } else {
  635:                                                       $hastimeleft{$symb} = $duedate - $now;
  636:                                                       $countdowndisp{$symb} = 'none';
  637:                                                   }
  638:                                                   unless ($donebutton{$symb}) {
  639:                                                       $donebutton{$symb} = 0;
  640:                                                   }
  641:                                               }
  642:                                           }
  643:                                       }
  644:                                   }
  645:                               }
  646:                               $thisdir=~s/\/[^\/]*$//;
  647: 			      foreach (@rlinks) {
  648: 				  unless (($_=~/^https?\:\/\//i) ||
  649: 					  ($_=~/^\//) ||
  650: 					  ($_=~/^javascript:/i) ||
  651: 					  ($_=~/^mailto:/i) ||
  652: 					  ($_=~/^\#/)) {
  653: 				      my $newlocation=
  654: 				    &Apache::lonnet::hreflocation($thisdir,$_);
  655:                      $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
  656: 				  }
  657: 			      }
  658:                               foreach my $css_href (@css_hrefs) {
  659:                                   next if ($css_href eq '');
  660:                                   unless ($css_href =~ m{https?://}) {
  661:                                       my $proburl = &Apache::lonnet::clutter($plainsrc);
  662:                                       unless ($css_href =~ m{^/}) {
  663:                                           my $probdir = $proburl;
  664:                                           $probdir=~s/\/[^\/]*$//;
  665:                                           $css_href = &Apache::lonnet::hreflocation($probdir,$css_href);
  666:                                       }
  667:                                       if ($css_href =~ m{^/(res|uploaded)/}) {
  668:                                           unless (($env{'httpref.'.$css_href}) ||
  669:                                                   ($httpref{'httpref.'.$css_href}) ||
  670:                                                   (&Apache::lonnet::is_on_map($css_href))) {
  671:                                               if ($env{'httpref.'.$proburl}) {
  672:                                                   $proburl = $env{'httpref.'.$proburl};
  673:                                               }
  674:                                               $httpref{'httpref.'.$css_href} = $proburl;
  675:                                           }
  676:                                       }
  677:                                   }
  678:                                   $cssrefs{$css_href} = 1;
  679:                               }
  680: # -------------------------------------------------- Deal with Applet codebases
  681:   $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
  682: 			      $ssibody{$_}=$output;
  683: # ---------------------------------------------------------------- End SSI cell
  684:                           }
  685:                       }
  686:                      }
  687:                   }
  688:                   unless ($contents) {
  689:                       &Apache::loncommon::content_type($r,'text/html');
  690:                       $r->send_http_header;
  691:                       $r->print(&Apache::loncommon::start_page(undef,undef,
  692: 							       {'force_register' => 1,}));
  693:                       $r->print(&mt('This page is either empty or it only contains resources that are currently hidden').'. ');
  694:                       $r->print('<br /><br />'.&mt('Please use the LON-CAPA navigation arrows to move to another item in the course').
  695: 				&Apache::loncommon::end_page());
  696:                   } else {
  697: # ------------------------------------------------------------------ Build page
  698: 
  699: # ---------------------------------------------------------------- Send headers
  700: 		      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  701: 			  if ($isxml) {
  702: 			      &Apache::loncommon::content_type($r,'text/xml');
  703: 			  } else {
  704: 			      &Apache::loncommon::content_type($r,'text/html');
  705: 			  }
  706: 			  $r->send_http_header;
  707: # ------------------------------------------------------------------------ Head
  708: 			  if ($allscript) {
  709: 			      $allscript = 
  710: 				  "\n".'<script type="text/javascript">'."\n".
  711: 				  $allscript.
  712: 				  "\n</script>\n";
  713: 			  }
  714:                           if (($nforms) && ($nuploads)) {
  715:                               $allscript .= &Apache::lonhtmlcommon::file_submissionchk_js(\%turninpaths,\%multiresps).
  716:                                             '<script type="text/javascript" '.
  717:                                             'src="/res/adm/includes/file_upload.js"></script>';
  718:                           }
  719:                           if (($nforms) && (&Apache::lonhtmlcommon::htmlareabrowser())) {
  720:                               my %textarea_args = (
  721:                                   dragmath => 'math',
  722:                               );
  723:                               $allscript .= &Apache::lonhtmlcommon::htmlareaselectactive(\%textarea_args);
  724:                           }
  725:                           if ($ntimers) {
  726:                               $allscript .= '<script type="text/javascript">'."\n".
  727:                                             '// <![CDATA['."\n".
  728:                                             'function pageTimer(form,prefix) {'."\n".
  729:                                             "   form.elements[prefix+'markaccess'].value = 'yes';\n".
  730:                                             "   form.submit();\n".
  731:                                             '}'."\n".
  732:                                             '// ]]>'.
  733:                                             "\n</script>\n";
  734:                           }
  735:                           &Apache::lonhtmlcommon::clear_breadcrumb_tools();
  736:                           if (keys(%hastimeleft)) {
  737:                               my (%uniquetimes,%uniquedisplays);
  738:                               foreach my $item (values(%hastimeleft)) {
  739:                                   if (exists($uniquetimes{$item})) {
  740:                                       $uniquetimes{$item} ++; 
  741:                                   } else {
  742:                                       $uniquetimes{$item} = 1;
  743:                                   }
  744:                               }
  745:                               if (scalar(keys(%uniquetimes)) == 1) {
  746:                                   my (%uniquedisplays,%uniquedones,$currdisp,$donebuttontime,
  747:                                       $donebuttonextras);
  748:                                   if (keys(%countdowndisp)) {
  749:                                       foreach my $item (values(%countdowndisp)) {
  750:                                           if (exists($uniquedisplays{$item})) {
  751:                                               $uniquedisplays{$item} ++;
  752:                                           } else {
  753:                                               $uniquedisplays{$item} = 1;
  754:                                           }
  755:                                       }
  756:                                       my @countdowndisplay = keys(%uniquedisplays);
  757:                                       if (scalar(@countdowndisplay) == 1) {
  758:                                           $currdisp = $countdowndisplay[0];
  759:                                       }
  760:                                   }
  761:                                   if (keys(%donebutton)) {
  762:                                       foreach my $item (values(%donebutton)) {
  763:                                           if (exists($uniquedones{$item})) {
  764:                                               $uniquedones{$item} ++;
  765:                                           } else {
  766:                                               $uniquedones{$item} = 1;
  767:                                           }
  768:                                       }
  769:                                       my @donebuttons = sort { $ <=> $b } (keys(%uniquedones));
  770:                                       if (scalar(@donebuttons) == 1) {
  771:                                           if ($donebuttons[0]) {
  772:                                               $donebuttontime = $donebuttons[0];
  773:                                               if (ref($buttonbytime{$donebuttontime}) eq 'ARRAY') {
  774:                                                   $donebuttonextras = $donebtnextra{$buttonbytime{$donebuttontime}->[0]};
  775:                                               }
  776:                                           }
  777:                                       }
  778:                                   }
  779:                                   &add_countdown_timer($currdisp,$donebuttontime,$donebuttonextras);
  780:                               }
  781:                           }
  782:                           my $pagebuttonshide;
  783:                           if (keys(%buttonshide)) {
  784:                               my %uniquebuttonhide;
  785:                               foreach my $item (values(%buttonshide)) {
  786:                                   if (exists($uniquebuttonhide{$item})) {
  787:                                       $uniquebuttonhide{$item} ++;
  788:                                   } else {
  789:                                       $uniquebuttonhide{$item} = 1;
  790:                                   }
  791:                               }
  792:                               if (keys(%uniquebuttonhide) == 1) {
  793:                                   if (lc((keys(%uniquebuttonhide))[0]) eq 'yes') {
  794:                                       $pagebuttonshide = 'yes';
  795:                                   }
  796:                               }
  797:                           }
  798:                           if (keys(%cssrefs)) {
  799:                               my $links;
  800:                               if (keys(%cssrefs)) {
  801:                                   foreach my $css_href (keys(%cssrefs)) {
  802:                                       next unless ($css_href =~ m{^(/res/|/uploaded/|https?://)});
  803:                                       $links .= '<link rel="stylesheet" type="text/css" href="'.$css_href.'" />'."\n";
  804:                                   }
  805:                               }
  806:                               if ($links) {
  807:                                   if (keys(%httpref)) {
  808:                                       &Apache::lonnet::appenv(\%httpref);
  809:                                   }
  810:                                   $allscript .= "\n$links";
  811:                               }
  812:                           }
  813: # ------------------------------------------------------------------ Start body
  814: 			  $r->print(&Apache::loncommon::start_page(undef,$allscript,
  815: 								   {'force_register' => 1,
  816: 								    'bgcolor'        => '#ffffff',
  817: 								    'hide_buttons'   => $pagebuttonshide}));
  818: # ------------------------------------------------------------------ Start form
  819: 			  if ($nforms) {
  820: 			      my $fmtag = '<form name="lonhomework" method="post"  enctype="multipart/form-data"';
  821:                               if ($nuploads) {
  822:                                   my $multi;
  823:                                   if ($nuploads > 1) {
  824:                                       $multi = 1;
  825:                                   }
  826:                                   $fmtag .= 'onsubmit="return file_submission_check(this,'."'$turninparent','$multi'".');"';
  827:                               }
  828:                               $fmtag .= ' action="'.
  829: 					&Apache::lonenc::check_encrypt($requrl)
  830: 					.'" id="LC_page">';
  831:                               $r->print($fmtag);
  832: 			  }
  833: 		      } elsif (($target eq 'tex') || ($target eq 'tex_answer')) {
  834: 			  #  I think this is not needed as the header
  835: 			  # will be put in for each of the page parts
  836: 			  # by the londefdef.pm now that we are opening up
  837: 			  # the parts of a page.
  838: 			  #$r->print('\documentclass{article}
  839:                           #       \newcommand{\keephidden}[1]{}           
  840:                           #       \usepackage[dvips]{graphicx}
  841:                           #       \usepackage{epsfig}
  842:                           #       \usepackage{calc}
  843:                           #       \usepackage{longtable}
  844:                           #       \begin{document}');
  845: 		      }
  846: # ----------------------------------------------------------------- Start table
  847: 		      if (($target eq 'tex') || ($target eq 'tex_answer')) {
  848: #			 #  $r->print('\begin{longtable}INSERTTHEHEADOFLONGTABLE\endfirsthead\endhead ');
  849: 			  if ($number_of_columns le $lcm) {$number_of_columns=$lcm;};
  850: 		      } else {
  851: 			  $r->print('<table width="100%" cols="'.$lcm.'" border="0">');
  852: 		      }
  853: # generate rows
  854:                       for ($i=0;$i<=$#rows;$i++) {
  855: 			if ($rows[$i]) {
  856: 			    unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  857: 				$r->print("\n<tr>");
  858: 			    }
  859:                           my @colcont=split(/\&/,$rows[$i]);
  860:                           my $avespan=$lcm/($#colcont+1);
  861:                           for ($j=0;$j<=$#colcont;$j++) {
  862:                               my $rid=$colcont[$j];
  863: 			      my $metainfo =&get_buttons(\%hash,$rid,\%buttonshide,$hostname).'<br />';
  864: 			    unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  865: 				$r->print('<td colspan="'.$avespan.'"');
  866: 			    }
  867:                               if (($cellemb{$rid} eq 'ssi') || ($cellexternal{$rid})) {
  868: 				  unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  869: 				      if ($ssibgcolor{$rid}) {
  870: 					  $r->print(' bgcolor="'.
  871: 						    $ssibgcolor{$rid}.'"');
  872: 				      }
  873: 				      $r->print('>'.$metainfo.'<font');
  874: 		    
  875: 				      if ($ssitext{$rid}) {
  876: 					  $r->print(' text="'.$ssitext{$rid}.'"');
  877: 				      }
  878: 				      if ($ssilink{$rid}) {
  879: 					  $r->print(' link="'.$ssilink{$rid}.'"');
  880: 				      }
  881: 				      if ($ssitext{$rid}) {
  882: 					  $r->print(' vlink="'.$ssivlink{$rid}.'"');
  883: 				      }
  884: 				      if ($ssialink{$rid}) {
  885: 					  $r->print(' alink="'.$ssialink{$rid}.'"');
  886: 				      }             
  887: 				      $r->print('>');
  888: 				  }
  889:                                   $r->print($ssibody{$rid});
  890: 				  unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  891: 				      $r->print('</font>');
  892:                                   }
  893:                                   if ($env{'course.'.
  894:                                       $env{'request.course.id'}.
  895:                                       '.pageseparators'} eq 'yes') {
  896:                                       unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  897:                                           $r->print('<hr />');
  898:                                       } 
  899: 				  }
  900: 			      } elsif ($cellemb{$rid} eq 'img') {
  901:                                   $r->print('>'.$metainfo.'<img src="'.
  902:                                     $hash{'src_'.$rid}.'" />');
  903: 			      } elsif ($cellemb{$rid} eq 'emb') {
  904:                                   $r->print('>'.$metainfo.'<embed src="'.
  905:                                     $hash{'src_'.$rid}.'"></embed>');
  906:                               } elsif (&Apache::lonnet::declutter($hash{'src_'.$rid}) !~/\.(sequence|page)$/) {
  907:                                   $r->print($metainfo.'<b>'.$hash{'title_'.$rid}.'</b><br />');
  908:                                   unless ($cellemb{$rid} eq 'wrp') {
  909:                                       $r->print(&mt('It is recommended that you use an up-to-date virus scanner before handling this file.'));
  910:                                   }
  911:                                   $r->print('</p><p><table>'.
  912:                                             &Apache::londocs::entryline(0,
  913:                                                                         &mt("Click to download or use your browser's Save Link function"),
  914:                                                                         '/'.&Apache::lonnet::declutter($hash{'src_'.$rid})).
  915:                                                                         '</table></p><br />');
  916:                               }
  917: 			      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  918: 				  $r->print('</td>');
  919: 			      } else {
  920: #                                  for (my $incol=1;$incol<=$avespan;$incol++) {
  921: #				      $r->print(' & ');
  922: #				  }
  923: 			      }
  924:                           }
  925: 			      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  926: 				  $r->print('</tr>');
  927: 			      } else {
  928: #				  $r->print('REMOVETHEHEADOFLONGTABLE\\\\');
  929: 			      }
  930: 		        }
  931:                       }
  932: 		      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  933: 			  $r->print("\n</table>");
  934: 		      } else {
  935: #			  $r->print('\end{longtable}\strut');
  936: 		      }
  937: # ---------------------------------------------------------------- Submit, etc.
  938:                       if ($nforms) {
  939:                           my $class;
  940:                           if ($nforms > 1) {
  941:                               $class = ' class="LC_hwk_submit"';
  942:                               if ($ntimers) {
  943:                                   $nforms = 1;
  944:                                   $class = '';
  945:                               }
  946:                           }
  947:                           $r->print(
  948: 	                  '<input name="all_submit" value="'.&mt('Submit All').'" type="'.
  949: 			  (($nforms>1)?'submit':'hidden').'"'.$class.' id="all_submit" />'.
  950:                           '<input type="hidden" name="all_submit_pressed" '.
  951:                           'id="all_submit_pressed" value="" />'.
  952:                           '<div id="msg_all_submit" style="display:none">'.
  953:                           &mt('Processing your submission ...').'</div></form>');
  954:                       }
  955: 		      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  956: 			  $r->print(&Apache::loncommon::end_page({'discussion'
  957: 								      => 1,}));
  958: 		      } else {
  959: 			  $r->print('\end{document}'.$number_of_columns);
  960: 		      }
  961: 		      &Apache::lonnet::symblist($requrl,%symbhash);
  962: 		      my ($map,$id,$url)=&Apache::lonnet::decode_symb(&Apache::lonnet::symbread());
  963: 		      &Apache::lonnet::symblist($map,'last_known'=>[$url,$id]);
  964: # -------------------------------------------------------------------- End page
  965:                   }                  
  966: # ------------------------------------------------------------- End render page
  967:               } else {
  968:                   if ($hash{'map_type_'.$hash{'map_pc_'.$requrl}} eq 'none') {
  969:                       &Apache::loncommon::content_type($r,'text/html');
  970:                       $r->send_http_header;
  971:                       $r->print(&Apache::loncommon::start_page(undef,undef,
  972:                                                                {'force_register' => 1,}));
  973:                       my $crstype = &Apache::loncommon::course_type();
  974:                       if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
  975:                           $r->print('<span class="LC_warning">'.&mt('Missing composite page file.').'</span><br />'.
  976:                                     &mt("You may want to use the $crstype Editor to remove this item."));
  977:                       } else {
  978:                           if ($crstype eq 'Placement') {
  979:                               $r->print('<span class="LC_warning">'.&mt('Missing page').'</span>');
  980:                           } else {
  981:                               $r->print('<span class="LC_info">'.
  982:                                         &mt('This resource was unavailable when your '.lc($crstype).' session was loaded').'<br />'.
  983:                                         &mt("Please use 'Contents' to list items available in the $crstype.").'</span>');
  984:                           }
  985:                       }
  986:                       $r->print(&Apache::loncommon::end_page());
  987:                   } else {
  988:                       &Apache::loncommon::content_type($r,'text/html');
  989:                       $r->send_http_header;
  990:                       &Apache::lonsequence::viewmap($r,$requrl);
  991:                   }
  992:               }
  993: # ------------------------------------------------------------------ Untie hash
  994:               unless (untie(%hash)) {
  995:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  996:                        "Could not untie coursemap $fn (browse).</font>"); 
  997:               }
  998: # -------------------------------------------------------------------- All done
  999: 	      return OK;
 1000: # ----------------------------------------------- Errors, hash could no be tied
 1001:           }
 1002:       } 
 1003:   }
 1004:   &Apache::loncommon::content_type($r,'text/html');
 1005:   $r->send_http_header;
 1006:   &Apache::lonsequence::viewmap($r,$requrl);
 1007:   return OK; 
 1008: }
 1009: 
 1010: sub get_buttons {
 1011:     my ($hash,$rid,$buttonshide,$hostname) = @_;
 1012: 
 1013:     my ($mapid,$resid)=split(/\./,$rid);
 1014:     my $symb=&Apache::lonnet::encode_symb($hash->{'map_id_'.$mapid},
 1015: 					  $resid,
 1016: 					  $hash->{'src_'.$rid});
 1017:     my ($aname,$shownsymb);
 1018:     if (($hash->{'encrypted_'.$rid}) && (!$env{'request.role.adv'})) {
 1019:         $aname = 'LC_'.$rid;
 1020:         $shownsymb = &Apache::lonenc::encrypted($symb);
 1021:     } else {
 1022:         $shownsymb = $symb;
 1023:         my $dispsymb = $symb;
 1024:         if ($symb =~ /\#([^\#]+)$/) {
 1025:             my $escan = &escape('#');
 1026:             $dispsymb =~ s/#([^\#]+)$/$escan$1/;
 1027:         }
 1028:         $aname = &escape($dispsymb);
 1029:     }
 1030:     my $metainfo = '<a name="'.$aname.'"></a>';
 1031:     unless ($env{'request.role.adv'}) {
 1032:         if ($buttonshide->{$symb} eq 'yes') {
 1033:             return $metainfo;
 1034:         }
 1035:     }
 1036:     my $crs_sec = $env{'request.course.id'} . (($env{'request.course.sec'} ne '')
 1037:                                                ? "/$env{'request.course.sec'}"
 1038:                                                : '');
 1039:     my $esrc=&Apache::lonnet::declutter($hash->{'src_'.$rid});
 1040:     if ($hash->{'encrypted_'.$rid}) {
 1041: 	$esrc=&Apache::lonenc::encrypted($esrc);
 1042:     }
 1043:     if ($hash->{'src_'.$rid} !~ m-^/uploaded/-
 1044:         && $hash->{'src_'.$rid} !~ m{^/ext/}
 1045: 	&& !$env{'request.enc'}
 1046: 	&& ($env{'request.role.adv'}
 1047: 	    || !$hash->{'encrypted_'.$rid})) { 
 1048: 	$metainfo .= '<a href="'.$hash->{'src_'.$rid}.'.meta'.'" '.
 1049: 	    'target="LONcatInfo">'.
 1050:             '<img src="/res/adm/pages/catalog.png" class="LC_icon"'.
 1051:             ' alt="'.&mt('Show Metadata').'"'.
 1052:             ' title="'.&mt('Show Metadata').'" />'.
 1053: 	    '</a>';
 1054:     }
 1055:     if (($hash->{'src_'.$rid} !~ m{^/uploaded/}) &&
 1056:         ($hash->{'src_'.$rid} !~ m{^/ext/})) {
 1057:         $metainfo .= '<a href="/adm/evaluate?postdata='.
 1058: 	    &escape($esrc).
 1059: 	    '" target="LONcatInfo">'.
 1060:             '<img src="/res/adm/pages/eval.png" class="LC_icon"'.
 1061:             ' alt="'.&mt('Provide my evaluation of this resource').'"'.
 1062:             ' title="'.&mt('Provide my evaluation of this resource').'" />'.
 1063: 	    '</a>';
 1064:     }
 1065:     if (($hash->{'src_'.$rid}=~/$LONCAPA::assess_re/) &&
 1066: 	($hash->{'src_'.$rid} !~ m-^/uploaded/-)) {
 1067: 
 1068: 	if ((&Apache::lonnet::allowed('mgr',$crs_sec)) ||
 1069:             (&Apache::lonnet::allowed('vgr',$crs_sec))) {
 1070: 	    $metainfo.=
 1071: 		'<a href="/adm/grades?symb='.&escape($symb).
 1072: #               '&command=submission" target="LONcatInfo">'.
 1073: 		'&command=submission">'.
 1074:                 '<img src="/adm/lonMisc/subm_button.png" class="LC_icon"'.
 1075:                 ' alt="'.&mt('View Submissions for a Student or a Group of Students').'"'.
 1076:                 ' title="'.&mt('View Submissions for a Student or a Group of Students').'" />'.
 1077: 		'</a>';
 1078:         }
 1079:         if (&Apache::lonnet::allowed('mgr',$crs_sec)) {
 1080:             $metainfo.=
 1081: 		'<a href="/adm/grades?symb='.&escape($symb).
 1082: #               '&command=gradingmenu" target="LONcatInfo">'.
 1083: 		'&command=gradingmenu">'.
 1084:                 '<img src="/res/adm/pages/pgrd.png" class="LC_icon"'.
 1085:                 ' alt="'.&mt('Content Grades').'"'.
 1086:                 ' title="'.&mt('Content Grades').'" />'.
 1087: 		'</a>';
 1088: 	}
 1089: 	if ((&Apache::lonnet::allowed('opa',$crs_sec)) ||
 1090:             (&Apache::lonnet::allowed('vpa',$crs_sec))) {
 1091: 	    $metainfo.=
 1092: 		'<a href="/adm/parmset?symb='.&escape($symb).
 1093: #               '" target="LONcatInfo">'.
 1094: 		'" >'.
 1095:                 '<img src="/adm/lonMisc/pprm_button.png" class="LC_icon"'.
 1096:                 ' alt="'.&mt('Content Settings').'"'.
 1097:                 ' title="'.&mt('Content Settings').'" />'.
 1098: 		'</a>';
 1099: 	}
 1100:     }
 1101:     if ($env{'request.course.id'}) {
 1102:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1103:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1104:         my $file=&Apache::lonnet::declutter($hash->{'src_'.$rid});
 1105:         my $editbutton = '';
 1106:         if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
 1107:             my ($cfile,$home,$switchserver,$forceedit,$forceview) =
 1108:                 &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,$hash->{'src_'.$rid},$symb);
 1109:             if ($cfile ne '') {
 1110:                 my $jscall = &Apache::lonhtmlcommon::jump_to_editres($cfile,$home,$switchserver,
 1111:                                                                      $forceedit,1,$symb,$shownsymb,
 1112:                                                                      undef,&escape($env{'form.title'}),
 1113:                                                                      $hostname);
 1114:                 if ($jscall) {
 1115:                     $editbutton = 1;
 1116:                     my $icon = 'pcstr.png';
 1117:                     my $label = &mt('Edit');
 1118:                     my $title = &mt('Edit this resource');
 1119:                     my $pic = '<img src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$icon).'"'.
 1120:                               ' class="LC_icon" alt="'.$label.'" title="'.$title.'" />';
 1121:                     $metainfo .= '&nbsp;<a href="javascript:'.$jscall.';">'.$pic.'</a>';
 1122:                 }
 1123:             }
 1124:         }
 1125:         if ((!$editbutton) && ($file=~/$LONCAPA::assess_re/)) {
 1126:             my $url = &Apache::lonnet::clutter($file);
 1127:             my $viewsrcbutton;
 1128:             if ((&Apache::lonnet::allowed('cre','/')) &&
 1129:                 (&Apache::lonnet::metadata($url,'sourceavail') eq 'open')) {
 1130:                 $viewsrcbutton = 1;
 1131:             } elsif (&Apache::lonnet::allowed('vxc',$crs_sec)) {
 1132:                 if ($url =~ m{^\Q/res/$cdom/\E($LONCAPA::match_username)/}) {
 1133:                     my $auname = $1;
 1134:                     if (($env{'request.course.adhocsrcaccess'} ne '') &&
 1135:                         (grep(/^\Q$auname\E$/,split(/,/,$env{'request.course.adhocsrcaccess'})))) {
 1136:                         $viewsrcbutton = 1;
 1137:                     } elsif ((&Apache::lonnet::metadata($url,'sourceavail') eq 'open') &&
 1138:                              (&Apache::lonnet::allowed('bre',$crs_sec))) {
 1139:                         $viewsrcbutton = 1;
 1140:                     }
 1141:                 }
 1142:             }
 1143:             if ($viewsrcbutton) {
 1144:                 my $icon = 'pcstr.png';
 1145:                 my $label = &mt('View Source');
 1146:                 my $title = &mt('View source code');
 1147:                 my $jsrid = $rid;
 1148:                 $jsrid =~ s/\./_/g;
 1149:                 my $showurl = &escape(&Apache::lonenc::check_encrypt($url));
 1150:                 my $pic = '<img src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$icon).'"'.
 1151:                           ' class="LC_icon" alt="'.$label.'" title="'.$title.'" />';
 1152:                 $metainfo .= '&nbsp;<a href="javascript:open_source_'.$jsrid.'();">'.$pic.'</a>'."\n".
 1153:                              '<script type="text/javascript">'."\n".
 1154:                              "function open_source_$jsrid() {\n".
 1155:                              "  sourcewin=window.open('/adm/source?inhibitmenu=yes&viewonly=1&filename=$showurl','LONsource',".
 1156:                              "'height=500,width=600,resizable=yes,location=no,menubar=no,toolbar=no,scrollbars=yes');\n".
 1157:                              "}\n".
 1158:                              "</script>\n";
 1159:             }
 1160:         }
 1161:     }
 1162:     return $metainfo;
 1163: }
 1164: 
 1165: sub add_countdown_timer {
 1166:     my ($currdisp,$donebuttontime,$donebuttonextras) = @_;
 1167:     my ($collapse,$expand,$alttxt,$title,$donebutton);
 1168:     if ($currdisp eq 'inline') {
 1169:         $collapse = '&#9658;&nbsp;';
 1170:     } else {
 1171:         $expand = '&#9668;&nbsp;';
 1172:     }
 1173:     if ($donebuttontime) {
 1174:         my ($type,$proctor,$donebuttontext);
 1175:         if (ref($donebuttonextras) eq 'HASH') {
 1176:             $proctor = $donebuttonextras->{'proctor'};
 1177:             $donebuttontext = $donebuttonextras->{'text'};
 1178:             $type = $donebuttonextras->{'type'};
 1179:         } else {
 1180:             $donebuttontext = &mt('Done');
 1181:             $type = 'map';
 1182:         }
 1183:         $donebutton = 
 1184:             &Apache::lonmenu::done_button_js($type,'','',$proctor,$donebuttontext);
 1185:     }
 1186:     unless ($env{'environment.icons'} eq 'iconsonly') {
 1187:         $alttxt = &mt('Timer');
 1188:         $title = $alttxt.'&nbsp;';
 1189:     }
 1190:     my $desc = &mt('Countdown to due date/time');
 1191:     my $output = <<END;
 1192: $donebutton
 1193: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
 1194: <span id="ddcountcollapse" class="LC_menubuttons_inline_text">
 1195: $collapse
 1196: </span></a>
 1197: <span id="duedatecountdown" class="LC_menubuttons_inline_text" style="display: $currdisp;"></span>
 1198: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
 1199: <span id="ddcountexpand" class="LC_menubuttons_inline_text" >$expand</span>
 1200: <img src="/res/adm/pages/timer.png" title="$desc" class="LC_icon" alt="$alttxt" /><span class="LC_menubuttons_inline_text">$title</span></a>
 1201: END
 1202:     &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$output);
 1203:     return;
 1204: }
 1205: 
 1206: sub create_extlink {
 1207:     my ($url,$anchor,$title,$linktext) = @_;
 1208:     my $shownlink;
 1209:     unless ($title eq '') {
 1210:         $shownlink = '<span style="font-weight:bold;">'.$title.'</span><br />';
 1211:     }
 1212:     my $dest = &HTML::Entities::encode($url.$anchor,'&<>"');
 1213:     $shownlink .= '<a href="'.$dest.'">'.$linktext.'</a>';
 1214:     return $shownlink;
 1215: }
 1216: 
 1217: 1;
 1218: __END__
 1219: 
 1220: 
 1221: =head1 NAME
 1222: 
 1223: Apache::lonpage - Page Handler
 1224: 
 1225: =head1 SYNOPSIS
 1226: 
 1227: Invoked by /etc/httpd/conf/srm.conf:
 1228: 
 1229:  <LocationMatch "^/res/.*\.page$>
 1230:  SetHandler perl-script
 1231:  PerlHandler Apache::lonpage
 1232:  </LocationMatch>
 1233: 
 1234: =head1 INTRODUCTION
 1235: 
 1236: This module renders a .page resource.
 1237: 
 1238: This is part of the LearningOnline Network with CAPA project
 1239: described at http://www.lon-capa.org.
 1240: 
 1241: =head1 HANDLER SUBROUTINE
 1242: 
 1243: This routine is called by Apache and mod_perl.
 1244: 
 1245: =over 4
 1246: 
 1247: =item *
 1248: 
 1249: set document type for header only
 1250: 
 1251: =item *
 1252: 
 1253: tie db file
 1254: 
 1255: =item *
 1256: 
 1257: render page
 1258: 
 1259: =item *
 1260: 
 1261: add to symb list
 1262: 
 1263: =item *
 1264: 
 1265: page parms
 1266: 
 1267: =item *
 1268: 
 1269: Get SSI output, post parameters
 1270: 
 1271: =item *
 1272: 
 1273: SSI cell rendering
 1274: 
 1275: =item *
 1276: 
 1277: Deal with Applet codebases
 1278: 
 1279: =item *
 1280: 
 1281: Build page
 1282: 
 1283: =item *
 1284: 
 1285: send headers
 1286: 
 1287: =item *
 1288: 
 1289: start body
 1290: 
 1291: =item *
 1292: 
 1293: start form
 1294: 
 1295: =item *
 1296: 
 1297: start table
 1298: 
 1299: =item *
 1300: 
 1301: submit element, etc, render page, untie hash
 1302: 
 1303: =back
 1304: 
 1305: =head1 OTHER SUBROUTINES
 1306: 
 1307: =over 4
 1308: 
 1309: =item *
 1310: 
 1311: euclid() : Euclid's method for determining the greatest common denominator.
 1312: 
 1313: =item *
 1314: 
 1315: tracetable() : Build page table.
 1316: 
 1317: =back
 1318: 
 1319: =cut
 1320: 
 1321: 

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