File:  [LON-CAPA] / rat / lonpage.pm
Revision 1.111: download - view: text, annotated - select for diffs
Fri Jul 17 19:11:03 2015 UTC (8 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_X, HEAD
- Include countdown timer in composite page (i.e., .page), if all items
  with time left have the same amount of time remaining.

    1: # The LearningOnline Network with CAPA
    2: # Page Handler
    3: #
    4: # $Id: lonpage.pm,v 1.111 2015/07/17 19:11:03 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 HTML::TokeParser;
   45: use GDBM_File;
   46: use Apache::lonsequence;
   47: use lib '/home/httpd/lib/perl/';
   48: use LONCAPA;
   49:  
   50: 
   51: # -------------------------------------------------------------- Module Globals
   52: my %hash;
   53: my @rows;
   54: 
   55: # ------------------------------------------------------------------ Euclid gcd
   56: 
   57: sub euclid {
   58:     my ($e,$f)=@_;
   59:     my $a; my $b; my $r;
   60:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
   61:     while ($r!=0) {
   62: 	$a=$b; $b=$r;
   63:         $r=$a%$b;
   64:     }
   65:     return $b;
   66: }
   67: 
   68: # ------------------------------------------------------------ Build page table
   69: 
   70: sub tracetable {
   71:     my ($sofar,$rid,$beenhere)=@_;
   72:     my $further=$sofar;
   73:     my $randomout=0;
   74:     unless ($env{'request.role.adv'}) {
   75:         $randomout = $hash{'randomout_'.$rid};
   76:     }
   77:     unless ($beenhere=~/\&$rid\&/) {
   78:         $beenhere.=$rid.'&';
   79:         unless ($randomout) {
   80:             if (defined($hash{'is_map_'.$rid})) {
   81:                 if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
   82:                     (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
   83:                     my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
   84: 	            $sofar=
   85:                        &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
   86:                        '&'.$frid.$beenhere);
   87:                     $sofar++;
   88:                     if ($hash{'src_'.$frid}) {
   89:                         my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
   90:                         if (($brepriv eq '2') || ($brepriv eq 'F')) {
   91:                             if (defined($rows[$sofar])) {
   92:                                 $rows[$sofar].='&'.$frid;
   93:                             } else {
   94:                                 $rows[$sofar]=$frid;
   95:                             }
   96: 	                }
   97: 	            }
   98: 	        }
   99:             } else {
  100:                 $sofar++;
  101:                 if ($hash{'src_'.$rid}) {
  102:                     my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
  103:                     if (($brepriv eq '2') || ($brepriv eq 'F')) {
  104:                         if (defined($rows[$sofar])) {
  105:                             $rows[$sofar].='&'.$rid;
  106:                         } else {
  107:                             $rows[$sofar]=$rid;
  108:                         }
  109: 	            }
  110:                 }
  111:             }
  112:         }
  113: 
  114:         if (defined($hash{'to_'.$rid})) {
  115: 	    my $mincond=1;
  116:             my $next='';
  117:             foreach (split(/\,/,$hash{'to_'.$rid})) {
  118:                 my $thiscond=
  119:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
  120:                 if ($thiscond>=$mincond) {
  121: 		    if ($next) {
  122: 		        $next.=','.$_.':'.$thiscond;
  123:                     } else {
  124:                         $next=$_.':'.$thiscond;
  125: 		    }
  126:                     if ($thiscond>$mincond) { $mincond=$thiscond; }
  127: 	        }
  128:             }
  129:             foreach (split(/\,/,$next)) {
  130:                 my ($linkid,$condval)=split(/\:/,$_);
  131:                 if ($condval>=$mincond) {
  132:                     my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
  133:                     if ($now>$further) { $further=$now; }
  134: 	        }
  135:             }
  136:         }
  137:     }
  138:     return $further;
  139: }
  140: 
  141: # ================================================================ Main Handler
  142: 
  143: sub handler {
  144:   my $r=shift;
  145: 
  146: # ------------------------------------------- Set document type for header only
  147: 
  148:   if ($r->header_only) {
  149:        if ($env{'browser.mathml'}) {
  150:            &Apache::loncommon::content_type($r,'text/xml');
  151:        } else {
  152:            &Apache::loncommon::content_type($r,'text/html'); 
  153:        }
  154:        $r->send_http_header;
  155:        return OK;
  156:    }
  157:   
  158:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  159:                                           ['forceselect','launch']);
  160:   my $number_of_columns = 1;
  161:   my $requrl=$r->uri;  
  162:   my $target = $env{'form.grade_target'};
  163: 
  164: # Short term solution: define target as 'tex_answer' when retrieving answers
  165: # for resources in a .page when generating printouts.
  166: # A better long-term fix would be to modify the way problem rendering, and 
  167: # answer rendering are retrieved for individual resources when printing a .page,
  168: # so rendered problem and answer are sequential for individual resources in 
  169: # the .page
  170: #
  171:   if ($target eq 'answer') {
  172:       if ($env{'form.answer_output_mode'} eq 'tex') {
  173:           $target = 'tex_answer';
  174:       }
  175:   }
  176: #  &Apache::lonnet::logthis("Got a target of $target");
  177:   if ($target eq 'meta') {
  178:       &Apache::loncommon::content_type($r,'text/html');
  179:       $r->send_http_header;
  180:       return OK;
  181:   }
  182: # ----------------------------------------------------------------- Tie db file
  183:   if (($env{'request.course.fn'}) && (!$env{'form.forceselect'})) {
  184:       my $fn=$env{'request.course.fn'};
  185:       if (-e "$fn.db") {
  186:           if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
  187: # ------------------------------------------------------------------- Hash tied
  188:               my $firstres=$hash{'map_start_'.$requrl};
  189:               my $lastres=$hash{'map_finish_'.$requrl};
  190:               if (($firstres) && ($lastres)) {
  191: # ------------------------------------------------------------- Countdown Timer
  192:                   my $now = time;
  193:                   my ($pagefirstaccess,%hastimeleft,%countdowndisp);
  194:                   my ($pagesymb,$courseid,$domain,$name)=&Apache::lonnet::whichuser();
  195:                   if (($pagesymb && ($courseid ne '') && ($domain ne '') && ($name ne '')) {
  196:                       my %times=&Apache::lonnet::get('firstaccesstimes',
  197:                                                      [$courseid."\0".$pagesymb],
  198:                                                      $domain,$name);
  199:                       if ($times{$courseid."\0".$pagesymb} =~ /^\d+$/) {
  200:                           $pagefirstaccess = $times{$courseid."\0".$pagesymb};
  201:                       }
  202:                   }
  203: # ----------------------------------------------------------------- Render page
  204: 
  205:                   @rows=();
  206: 
  207:                   &tracetable(0,$firstres,'&');
  208: 
  209: # ------------------------------------------------------------ Add to symb list
  210: 
  211:                   my $i;
  212:                   my %symbhash=();
  213:                   for ($i=0;$i<=$#rows;$i++) {
  214: 		     if ($rows[$i]) {
  215:                         my @colcont=split(/\&/,$rows[$i]);
  216:                         foreach my $rid (@colcont) {
  217: 			    my ($mapid,$resid)=split(/\./,$rid);
  218: 			    $symbhash{$hash{'src_'.$rid}}=
  219: 				[$hash{'src_'.$rid},$resid];
  220: 		        }
  221: 		     }
  222: 		  }
  223:                   &Apache::lonnet::symblist($requrl,%symbhash);
  224: 
  225: # ------------------------------------------------------------------ Page parms
  226: 
  227:                   my $j;
  228:                   my $lcm=1;
  229:                   my $contents=0;
  230:                   my $nforms=0;
  231:                   my $nuploads=0;
  232:                   my $ntimers=0;
  233:                   my %turninpaths;
  234:                   my %multiresps;
  235:                   my $turninparent;
  236:                   
  237:                   my %ssibody=();
  238:                   my %ssibgcolor=();
  239:                   my %ssitext=();
  240:                   my %ssilink=();
  241:                   my %ssivlink=();
  242:                   my %ssialink=();
  243:      
  244:                   my %cellemb=();
  245:                   my %cellexternal=();
  246: 
  247:                   my $allscript='';
  248:                   my $allmeta='';
  249: 
  250:                   my $isxml=0;
  251:                   my $xmlheader='';
  252:                   my $xmlbody='';
  253: 
  254: # --------------------------------------------- Get SSI output, post parameters
  255: 
  256:                   for ($i=0;$i<=$#rows;$i++) {
  257: 		     if ($rows[$i]) {
  258: 		      $contents++;
  259:                       my @colcont=split(/\&/,$rows[$i]);
  260:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  261:                       foreach (@colcont) {
  262:                           my $src=$hash{'src_'.$_};
  263:                           my ($extension)=($src=~/\.(\w+)$/);
  264: 			  $cellexternal{$_}=($hash{'ext_'.$_} eq 'true:');
  265: 			  if ($hash{'encrypted_'.$_}) {
  266: 			      $src=&Apache::lonenc::encrypted($src);
  267: 			  }
  268:                           $cellemb{$_}=
  269: 			      &Apache::loncommon::fileembstyle($extension);
  270:                           if ($cellexternal{$_}) {
  271:                               unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  272:                                   $ssibody{$_} = <<ENDEXT;
  273: <iframe src="$src" width="100%">No iframe support!</iframe>
  274: ENDEXT
  275:                               }
  276:                           } elsif ($cellemb{$_} eq 'ssi') {
  277: # --------------------------------------------------------- This is an SSI cell
  278: 			      my ($mapid,$resid)=split(/\./,$_);
  279: 			      my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},$resid,$src);
  280: 			      
  281: 			      my $prefix=$_.'_';
  282:                               my $idprefix= join('_',($mapid,$resid,''));
  283:                               my %posthash=('request.prefix' => $prefix,
  284: 					    'LONCAPA_INTERNAL_no_discussion' => 'true',
  285: 					    'symb' => $symb);
  286: 			      if (($env{'form.grade_target'} eq 'tex') ||
  287:                                  ($env{'form.answer_output_mode'} eq 'tex')) {
  288: 				  $posthash{'grade_target'}=$env{'form.grade_target'};
  289: 				  $posthash{'textwidth'}=$env{'form.textwidth'};
  290: 				  $posthash{'problem_split'}=$env{'form.problem_split'};
  291: 				  $posthash{'latex_type'}=$env{'form.latex_type'};
  292: 				  $posthash{'rndseed'}=$env{'form.rndseed'};
  293:                                   $posthash{'answer_output_mode'} = $env{'form.answer_output_mode'};
  294: 			      }
  295: 			      my $submitted=exists($env{'form.all_submit'});
  296: 			      if (!$submitted) {
  297: 				  foreach my $key (keys(%env)) {
  298: 				      if ($key=~/^form.\Q$prefix\Esubmit_/) {
  299: 					  $submitted=1;last;
  300: 				      }
  301: 				  }
  302: 			      }
  303:                               if ($submitted) {
  304: 				  foreach my $key (keys(%env)) {
  305: 				      if ($key=~/^form.\Q$prefix\E/) {
  306: 					  my $name=$key;
  307: 					  $name=~s/^form.\Q$prefix\E//;
  308: 					  $posthash{$name}=$env{$key};
  309: 				      }
  310: 				  }
  311: 				  if (exists($env{'form.all_submit'})) {
  312: 				      $posthash{'all_submit'}='yes';
  313: 				  }
  314: 			      }
  315:                               my $output=Apache::lonnet::ssi($src,%posthash);
  316: 			      $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
  317:                               if (($target eq 'tex') || ($target eq 'tex_answer')) {
  318: 				  $output =~ s/^([^&]+)\\begin{document}//;
  319: 				  $output =~ s/\\end{document}//;
  320: #				  $output = '\parbox{\minipagewidth}{ '.$output.' }';
  321:                                   #some additional cleanup necessary for LateX (due to limitations of table environment 
  322: 				  $output =~ s/(\\vskip\s*\d+mm)\s*(\\\\)+/$1/g;
  323: 			      }
  324:                               my $matheditor;
  325:                               if ($output =~ /\Qjavascript:LC_mathedit_HWVAL_\E/) {
  326:                                   $matheditor = 'dragmath';
  327:                               } elsif ($output =~ /LCmathField/) {
  328:                                   $matheditor = 'lcmath';
  329:                               }
  330:                               my $parser=HTML::TokeParser->new(\$output);
  331:                               my $token;
  332:                               my $thisdir=$src;
  333:                               my $bodydef=0;
  334:                               my $thisxml=0;
  335:                               my @rlinks=();
  336:                               if ($output=~/\?xml/) {
  337:                                  $isxml=1;
  338:                                  $thisxml=1;
  339:                                  $output=~
  340:          /((?:\<(?:\?xml|\!DOC|html)[^\>]*(?:\>|\>\]\>)\s*)+)\<body[^\>]*\>/si;
  341:                                  $xmlheader=$1;
  342: 			      }
  343:                               while ($token=$parser->get_token) {
  344: 				if ($token->[0] eq 'S') {
  345:                                   if ($token->[1] eq 'a') {
  346: 				      if ($token->[2]->{'href'}) {
  347:                                          $rlinks[$#rlinks+1]=
  348: 					     $token->[2]->{'href'};
  349: 				      }
  350: 				  } elsif ($token->[1] eq 'img') {
  351:                                          $rlinks[$#rlinks+1]=
  352: 					     $token->[2]->{'src'};
  353: 				  } elsif ($token->[1] eq 'embed') {
  354:                                          $rlinks[$#rlinks+1]=
  355: 					     $token->[2]->{'src'};
  356: 				  } elsif ($token->[1] eq 'base') {
  357: 				      $thisdir=$token->[2]->{'href'};
  358: 				  } elsif ($token->[1] eq 'body') {
  359: 				      $bodydef=1;
  360:                                       $ssibgcolor{$_}=$token->[2]->{'bgcolor'};
  361:                                       $ssitext{$_}=$token->[2]->{'text'};
  362:                                       $ssilink{$_}=$token->[2]->{'link'};
  363:                                       $ssivlink{$_}=$token->[2]->{'vlink'};
  364:                                       $ssialink{$_}=$token->[2]->{'alink'};
  365:                                       if ($thisxml) {
  366: 					  $xmlbody=$token->[4];
  367:                                       }
  368:                                   } elsif ($token->[1] eq 'meta') {
  369: 				    if ($token->[4] !~ m:/>$:) {
  370: 				      $allmeta.="\n".$token->[4].'</meta>';
  371: 				    } else {
  372: 				      $allmeta.="\n".$token->[4];
  373: 				    }
  374:                                   } elsif (($token->[1] eq 'script') &&
  375:                                            ($bodydef==0)) {
  376: 				      $allscript.="\n\n"
  377:                                                 .$parser->get_text('/script');
  378:                                   }
  379: 			        }
  380: 			      }
  381:                               if ($output=~/\<body[^\>]*\>(.*)/si) {
  382:                                  $output=$1; 
  383:                               }
  384:                               $output=~s/\<\/body\>.*//si;
  385:                               if ($output=~/\<form/si) {
  386:                                   my $hastimer; 
  387: 				  $nforms++;
  388:                                   $output=~s/\<form[^\>]*\>//gsi;
  389:                                   $output=~s/\<\/form[^\>]*\>//gsi;
  390:                                   if ($output=~/\<input[^\>]+name\s*=\s*[\'\"]*HWFILE/) {
  391:                                       $nuploads++;
  392:                                   }
  393:                                   if ($output=~/\<input[^\>]+name\s*=\s*[\'\"]*accessbutton/) {
  394:                                       $ntimers++;
  395:                                       $hastimer = 1;
  396:                                   }
  397:                                   $output=~
  398: 				      s/\<((?:input|select|button|textarea)[^\>]+)name\s*\=\s*[\'\"]*([^\'\"]+)[\'\"]*([^\>]*)\>/\<$1 name="$prefix$2" $3\>/gsi;
  399:                                   $output=~
  400:                                       s/\<((?:input|select|button|textarea)[^\>]+)id\s*\=\s*[\'\"]*([^\'\"]+)[\'\"]*([^\>]*)\>/\<$1 id="$idprefix$2" $3\>/gsi;
  401:                                   if ($hastimer) {
  402:                                       $output=~
  403:                                           s/\<(input[^\>]+name=\Q"$prefix\Eaccessbutton"[^\>]+)(?:\Qdocument.markaccess.submit();\E)([^\>]*)\>/\<$1pageTimer(this.form,'$prefix')$2\>/gsi;
  404:                                       $output=~  s/\<(input[^\>]+name=\Q"$prefix\Emarkaccess"[^\>]+value=["'])(?:yes)(['"][^\>]*)\>/\<$1$2\>/gsi;
  405:                                   }
  406:                                   if ($matheditor eq 'dragmath') {
  407:                                       $output=~
  408:                                           s/(\Qjavascript:LC_mathedit_\E)(HWVAL_)([^'"]+?)(\(['"]*)(\QHWVAL_\E\3['"]\)\;void\(0\)\;)/$1$idprefix$2$3$4$idprefix$5/g;
  409:                                       $output=~
  410:                                           s/(function\s+LC_mathedit_)(HWVAL_)([^'"]+?)(\s+\(LCtextline\))/$1$idprefix$2$3$4/g;
  411:                                   } elsif ($matheditor eq 'lcmath') {
  412:                                       $output=~
  413:                                           s/(var\s+LCmathField\s+=\s+document\.getElementById\(['"])([^'"]+?)(['"]\)\;)/$1$idprefix$2$3/g;
  414:                                   }
  415:                                   $output=~
  416:                                       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;
  417:                                   $output=~
  418:                                       s/(\Q<td class="LC_status_\E)(\Qsubmit_\E)([^\"]*)(\s*[^\"]*"\>)/$1$idprefix$2$3$4/g;
  419:                                   if ($nuploads) {
  420:                                       $output=~
  421:                                           s/\<(input[^\>]+name=\"\Q$prefix\EHWFILE[^\>]+)\s*id\s*\=\s*[\'\"]*([^\'\"]+)[\'\"]*([^\)]*)\>/\<$1 id="$prefix$2" $3\>/gsi;
  422:                                        ($turninpaths{$prefix},$multiresps{$prefix}) = 
  423:                                            &Apache::loncommon::get_turnedin_filepath($symb,$env{'user.name'},$env{'user.domain'});
  424:                                        if ($turninparent eq '') {
  425:                                            $turninparent = $turninpaths{$prefix};
  426:                                            $turninparent =~ s{(/[^/]+)$}{}; 
  427:                                        }
  428:                                   }
  429:                                   $output=~
  430:                                       s/\<((?:input|select)[^\>]+\Qjavascript:setSubmittedPart\E)\(\s*[\'\"]([^\'\"]+)[\'\"]*\s*\)/\<$1('$2','$prefix')/gsi;
  431:                                   $output=~
  432:                                       s/\<(input[^\>]+\Qonfocus=\"javascript:disableAutoComplete\E)\(\'([^\']+)\'\)(;\")/\<$1('$idprefix$2')$3/gsi;
  433:                                   unless ($hastimer) {
  434:                                       if ($src =~ /$LONCAPA::assess_re/) {
  435:                                           %Apache::lonhomework::history =
  436:                                               &Apache::lonnet::restore($symb,$courseid,$domain,$name);
  437:                                           my $type = 'problem';
  438:                                           if ($src =~ /\.task$/) {
  439:                                               $type = 'Task';
  440:                                           }
  441:                                           my ($status,$accessmsg,$slot_name,$slot) =
  442:                                               &Apache::lonhomework::check_slot_access('0',$type,$symb);
  443:                                           undef(%Apache::lonhomework::history);
  444:                                           my $probstatus = &Apache::lonnet::EXT("resource.0.problemstatus",$symb);
  445:                                           if (($status eq 'CAN_ANSWER') || (($status eq 'CANNOT_ANSWER') && 
  446:                                               (($probstatus eq 'no') || ($probstatus eq 'no_feedback_ever')))) {
  447:                                               my ($slothastime,$timerhastime);
  448:                                               if ($slot_name ne '') {
  449:                                                   if (ref($slot) eq 'HASH') {
  450:                                                       if (($slot->{'starttime'} < $now) &&
  451:                                                           ($slot->{'endtime'} > $now)) {
  452:                                                           $slothastime = $now - $slot->{'endtime'};
  453:                                                       }
  454:                                                   }
  455:                                               }
  456:                                               my $duedate = &Apache::lonnet::EXT("resource.0.duedate",$symb);
  457:                                               my @interval=&Apache::lonnet::EXT("resource.0.interval",$symb);
  458:                                               if (@interval > 1) {
  459:                                                   my $first_access=&Apache::lonnet::get_first_access($interval[1],$symb);
  460:                                                   if ($first_access > 0) {
  461:                                                       my $timeremains = $first_access+$interval[0] - $now;
  462:                                                       if ($timeremains > 0) {
  463:                                                           $timerhastime = $timeremains;
  464:                                                       }
  465:                                                   }
  466:                                               }
  467:                                               if (($duedate && $duedate > $now) ||
  468:                                                   (!$duedate && $timerhastime > 0) ||
  469:                                                   ($slot_name ne '' && $slothastime)) {
  470:                                                   if ((@interval > 1 && $timerhastime) ||
  471:                                                       ($type eq 'Task' && $slothastime)) {
  472:                                                       $countdowndisp{$symb} = 'inline';
  473:                                                       if ((@interval > 1) && ($timerhastime)) {
  474:                                                           $hastimeleft{$symb} = $timerhastime;
  475:                                                       } else {
  476:                                                           $hastimeleft{$symb} = $slothastime;
  477:                                                       }
  478:                                                   } else {
  479:                                                       $hastimeleft{$symb} = $duedate - $now;
  480:                                                       $countdowndisp{$symb} = 'none';
  481:                                                   }
  482:                                               }
  483:                                           }
  484:                                       }
  485:                                   }
  486:                               }
  487:                               $thisdir=~s/\/[^\/]*$//;
  488: 			      foreach (@rlinks) {
  489: 				  unless (($_=~/^https?\:\/\//i) ||
  490: 					  ($_=~/^\//) ||
  491: 					  ($_=~/^javascript:/i) ||
  492: 					  ($_=~/^mailto:/i) ||
  493: 					  ($_=~/^\#/)) {
  494: 				      my $newlocation=
  495: 				    &Apache::lonnet::hreflocation($thisdir,$_);
  496:                      $output=~s/(\"|\'|\=\s*)$_(\"|\'|\s|\>)/$1$newlocation$2/;
  497: 				  }
  498: 			      }
  499: # -------------------------------------------------- Deal with Applet codebases
  500:   $output=~s/(\<applet[^\>]+)(codebase\=[^\S\>]+)*([^\>]*)\>/$1.($2?$2:' codebase="'.$thisdir.'"').$3.'>'/gei;
  501: 			      $ssibody{$_}=$output;
  502: # ---------------------------------------------------------------- End SSI cell
  503:                           }
  504:                       }
  505:                      } 
  506:                   }
  507:                   unless ($contents) {
  508:                       &Apache::loncommon::content_type($r,'text/html');
  509:                       $r->send_http_header;
  510:                       $r->print(&Apache::loncommon::start_page(undef,undef,
  511: 							       {'force_register' => 1,}));
  512:                       $r->print(&mt('This page is either empty or it only contains resources that are currently hidden').'. ');
  513:                       $r->print('<br /><br />'.&mt('Please use the LON-CAPA navigation arrows to move to another item in the course').
  514: 				&Apache::loncommon::end_page());
  515:                   } else {
  516: # ------------------------------------------------------------------ Build page
  517: 
  518: # ---------------------------------------------------------------- Send headers
  519: 		      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  520: 			  if ($isxml) {
  521: 			      &Apache::loncommon::content_type($r,'text/xml');
  522: 			  } else {
  523: 			      &Apache::loncommon::content_type($r,'text/html');
  524: 			  }
  525: 			  $r->send_http_header;
  526: # ------------------------------------------------------------------------ Head
  527: 			  if ($allscript) {
  528: 			      $allscript = 
  529: 				  "\n".'<script type="text/javascript">'."\n".
  530: 				  $allscript.
  531: 				  "\n</script>\n";
  532: 			  }
  533:                           if (($nforms) && ($nuploads)) {
  534:                               $allscript .= &Apache::lonhtmlcommon::file_submissionchk_js(\%turninpaths,\%multiresps);
  535:                           }
  536:                           if (($nforms) && (&Apache::lonhtmlcommon::htmlareabrowser())) {
  537:                               my %textarea_args = (
  538:                                   dragmath => 'math',
  539:                               );
  540:                               $allscript .= &Apache::lonhtmlcommon::htmlareaselectactive(\%textarea_args);
  541:                           }
  542:                           if ($ntimers) {
  543:                               $allscript .= '<script type="text/javascript">'."\n".
  544:                                             '// <![CDATA['."\n".
  545:                                             'function pageTimer(form,prefix) {'."\n".
  546:                                             "   form.elements[prefix+'markaccess'].value = 'yes';\n".
  547:                                             "   form.submit();\n".
  548:                                             '}'."\n".
  549:                                             '// ]]>'.
  550:                                             "\n</script>\n";
  551:                           }
  552:                           if (keys(%hastimeleft)) {
  553:                               my (%uniquetimes,%uniquedisplays);
  554:                               foreach my $item (values(%hastimeleft)) {
  555:                                   if (exists($uniquetimes{$item})) {
  556:                                       $uniquetimes{$item} ++; 
  557:                                   } else {
  558:                                       $uniquetimes{$item} = 1;
  559:                                   }
  560:                               }
  561:                               if (keys(%uniquetimes) == 1) {
  562:                                   my (%uniquedisplays,%uniquedones,$currdisp);
  563:                                   if (keys(%countdowndisp)) {
  564:                                       foreach my $item (values(%countdowndisp)) {
  565:                                           if (exists($uniquedisplays{$item})) {
  566:                                               $uniquedisplays{$item} ++;
  567:                                           } else {
  568:                                               $uniquedisplays{$item} = 1;
  569:                                           }
  570:                                       }
  571:                                       my @countdowndisplay = keys(%uniquedisplays);
  572:                                       if (scalar(@countdowndisplay) == 1) {
  573:                                           $currdisp = $countdowndisplay[0];
  574:                                       }
  575:                                   }
  576:                                   &add_countdown_timer($currdisp);
  577:                               }
  578:                           }
  579: # ------------------------------------------------------------------ Start body
  580: 			  $r->print(&Apache::loncommon::start_page(undef,$allscript,
  581: 								   {'force_register' => 1,
  582: 								    'bgcolor'        => '#ffffff',}));
  583: # ------------------------------------------------------------------ Start form
  584: 			  if ($nforms) {
  585: 			      my $fmtag = '<form name="lonhomework" method="post"  enctype="multipart/form-data"';
  586:                               if ($nuploads) {
  587:                                   my $multi;
  588:                                   if ($nuploads > 1) {
  589:                                       $multi = 1;
  590:                                   }
  591:                                   $fmtag .= 'onsubmit="return file_submission_check(this,'."'$turninparent','$multi'".');"';
  592:                               }
  593:                               $fmtag .= ' action="'.
  594: 					&Apache::lonenc::check_encrypt($requrl)
  595: 					.'" id="LC_page">';
  596:                               $r->print($fmtag);
  597: 			  }
  598: 		      } elsif (($target eq 'tex') || ($target eq 'tex_answer')) {
  599: 			  #  I think this is not needed as the header
  600: 			  # will be put in for each of the page parts
  601: 			  # by the londefdef.pm now that we are opening up
  602: 			  # the parts of a page.
  603: 			  #$r->print('\documentclass{article}
  604:                           #       \newcommand{\keephidden}[1]{}           
  605:                           #       \usepackage[dvips]{graphicx}
  606:                           #       \usepackage{epsfig}
  607:                           #       \usepackage{calc}
  608:                           #       \usepackage{longtable}
  609:                           #       \begin{document}');
  610: 		      }
  611: # ----------------------------------------------------------------- Start table
  612: 		      if (($target eq 'tex') || ($target eq 'tex_answer')) {
  613: #			 #  $r->print('\begin{longtable}INSERTTHEHEADOFLONGTABLE\endfirsthead\endhead ');
  614: 			  if ($number_of_columns le $lcm) {$number_of_columns=$lcm;};
  615: 		      } else {
  616: 			  $r->print('<table width="100%" cols="'.$lcm.'" border="0">');
  617: 		      }
  618: # generate rows
  619:                       for ($i=0;$i<=$#rows;$i++) {
  620: 			if ($rows[$i]) {
  621: 			    unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  622: 				$r->print("\n<tr>");
  623: 			    }
  624:                           my @colcont=split(/\&/,$rows[$i]);
  625:                           my $avespan=$lcm/($#colcont+1);
  626:                           for ($j=0;$j<=$#colcont;$j++) {
  627:                               my $rid=$colcont[$j];
  628: 			      my $metainfo =&get_buttons(\%hash,$rid).'<br />';
  629: 			    unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  630: 				$r->print('<td colspan="'.$avespan.'"');
  631: 			    }
  632:                               if (($cellemb{$rid} eq 'ssi') || ($cellexternal{$rid})) {
  633: 				  unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  634: 				      if ($ssibgcolor{$rid}) {
  635: 					  $r->print(' bgcolor="'.
  636: 						    $ssibgcolor{$rid}.'"');
  637: 				      }
  638: 				      $r->print('>'.$metainfo.'<font');
  639: 		    
  640: 				      if ($ssitext{$rid}) {
  641: 					  $r->print(' text="'.$ssitext{$rid}.'"');
  642: 				      }
  643: 				      if ($ssilink{$rid}) {
  644: 					  $r->print(' link="'.$ssilink{$rid}.'"');
  645: 				      }
  646: 				      if ($ssitext{$rid}) {
  647: 					  $r->print(' vlink="'.$ssivlink{$rid}.'"');
  648: 				      }
  649: 				      if ($ssialink{$rid}) {
  650: 					  $r->print(' alink="'.$ssialink{$rid}.'"');
  651: 				      }             
  652: 				      $r->print('>');
  653: 				  }
  654:                                   unless (($cellexternal{$rid}) && 
  655:                                           ($target eq 'tex') && ($target eq 'tex_answer')) {
  656:                                       $r->print($ssibody{$rid});
  657:                                   }
  658: 				  unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  659: 				      $r->print('</font>');
  660:                                   }
  661:                                   if ($env{'course.'.
  662:                                       $env{'request.course.id'}.
  663:                                       '.pageseparators'} eq 'yes') {
  664:                                       unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  665:                                           $r->print('<hr />');
  666:                                       } 
  667: 				  }
  668: 			      } elsif ($cellemb{$rid} eq 'img') {
  669:                                   $r->print('>'.$metainfo.'<img src="'.
  670:                                     $hash{'src_'.$rid}.'" />');
  671: 			      } elsif ($cellemb{$rid} eq 'emb') {
  672:                                   $r->print('>'.$metainfo.'<embed src="'.
  673:                                     $hash{'src_'.$rid}.'"></embed>');
  674:                               } elsif (&Apache::lonnet::declutter($hash{'src_'.$rid}) !~/\.(sequence|page)$/) {
  675:                                   $r->print($metainfo.'<b>'.$hash{'title_'.$rid}.'</b><br />');
  676:                                   unless ($cellemb{$rid} eq 'wrp') {
  677:                                       $r->print(&mt('It is recommended that you use an up-to-date virus scanner before handling this file.'));
  678:                                   }
  679:                                   $r->print('</p><p><table>'.
  680:                                             &Apache::londocs::entryline(0,
  681:                                                                         &mt("Click to download or use your browser's Save Link function"),
  682:                                                                         '/'.&Apache::lonnet::declutter($hash{'src_'.$rid})).
  683:                                                                         '</table></p><br />');
  684:                               }
  685: 			      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  686: 				  $r->print('</td>');
  687: 			      } else {
  688: #                                  for (my $incol=1;$incol<=$avespan;$incol++) {
  689: #				      $r->print(' & ');
  690: #				  }
  691: 			      }
  692:                           }
  693: 			      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  694: 				  $r->print('</tr>');
  695: 			      } else {
  696: #				  $r->print('REMOVETHEHEADOFLONGTABLE\\\\');
  697: 			      }
  698: 		        }
  699:                       }
  700: 		      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  701: 			  $r->print("\n</table>");
  702: 		      } else {
  703: #			  $r->print('\end{longtable}\strut');
  704: 		      }
  705: # ---------------------------------------------------------------- Submit, etc.
  706:                       if ($nforms) {
  707:                           my $class;
  708:                           if ($nforms > 1) {
  709:                               $class = ' class="LC_hwk_submit"';
  710:                               if ($ntimers) {
  711:                                   $nforms = 1;
  712:                                   $class = '';
  713:                               }
  714:                           }
  715:                           $r->print(
  716: 	                  '<input name="all_submit" value="'.&mt('Submit All').'" type="'.
  717: 			  (($nforms>1)?'submit':'hidden').'"'.$class.' id="all_submit" />'.
  718:                           '<div id="msg_all_submit" style="display:none">'.
  719:                           &mt('Processing your submission ...').'</div></form>');
  720:                       }
  721: 		      unless (($target eq 'tex') || ($target eq 'tex_answer')) {
  722: 			  $r->print(&Apache::loncommon::end_page({'discussion'
  723: 								      => 1,}));
  724: 		      } else {
  725: 			  $r->print('\end{document}'.$number_of_columns);
  726: 		      }
  727: 		      &Apache::lonnet::symblist($requrl,%symbhash);
  728: 		      my ($map,$id,$url)=&Apache::lonnet::decode_symb(&Apache::lonnet::symbread());
  729: 		      &Apache::lonnet::symblist($map,'last_known'=>[$url,$id]);
  730: # -------------------------------------------------------------------- End page
  731:                   }                  
  732: # ------------------------------------------------------------- End render page
  733:               } else {
  734:                   &Apache::loncommon::content_type($r,'text/html');
  735:                   $r->send_http_header;
  736:                   &Apache::lonsequence::viewmap($r,$requrl);
  737:               }
  738: # ------------------------------------------------------------------ Untie hash
  739:               unless (untie(%hash)) {
  740:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  741:                        "Could not untie coursemap $fn (browse).</font>"); 
  742:               }
  743: # -------------------------------------------------------------------- All done
  744: 	      return OK;
  745: # ----------------------------------------------- Errors, hash could no be tied
  746:           }
  747:       } 
  748:   }
  749:   &Apache::loncommon::content_type($r,'text/html');
  750:   $r->send_http_header;
  751:   &Apache::lonsequence::viewmap($r,$requrl);
  752:   return OK; 
  753: }
  754: 
  755: sub get_buttons {
  756:     my ($hash,$rid) = @_;
  757: 
  758:     my $metainfo = '';
  759:     my $esrc=&Apache::lonnet::declutter($hash->{'src_'.$rid});
  760:     my ($mapid,$resid)=split(/\./,$rid);
  761:     my $symb=&Apache::lonnet::encode_symb($hash->{'map_id_'.$mapid},
  762: 					  $resid,
  763: 					  $hash->{'src_'.$rid});
  764:     unless ($env{'request.role.adv'}) {
  765:         if (&Apache::lonnet::EXT('resource.0.buttonshide',$symb)) {
  766:             return;
  767:         }
  768:     }
  769:     if ($hash->{'encrypted_'.$rid}) {
  770: 	$symb=&Apache::lonenc::encrypted($symb);
  771: 	$esrc=&Apache::lonenc::encrypted($esrc);
  772:     }
  773:     if ($hash->{'src_'.$rid} !~ m-^/uploaded/-
  774:         && $hash->{'src_'.$rid} !~ m{^https?://}
  775: 	&& !$env{'request.enc'}
  776: 	&& ($env{'request.role.adv'}
  777: 	    || !$hash->{'encrypted_'.$rid})) { 
  778: 	$metainfo .='<a name="'.&escape($symb).'" />'.
  779: 	    '<a href="'.$hash->{'src_'.$rid}.'.meta'.'" target="LONcatInfo">'.
  780:             '<img src="/res/adm/pages/catalog.png" class="LC_icon"'.
  781:             ' alt="'.&mt('Show Metadata').'"'.
  782:             ' title="'.&mt('Show Metadata').'" />'.
  783: 	    '</a>';
  784:     }
  785:     if (($hash->{'src_'.$rid} !~ m{^/uploaded/}) &&
  786:         ($hash->{'src_'.$rid} !~ m{^https?://})) {
  787:         $metainfo .= '<a href="/adm/evaluate?postdata='.
  788: 	    &escape($esrc).
  789: 	    '" target="LONcatInfo">'.
  790:             '<img src="/res/adm/pages/eval.png" class="LC_icon"'.
  791:             ' alt="'.&mt('Provide my evaluation of this resource').'"'.
  792:             ' title="'.&mt('Provide my evaluation of this resource').'" />'.
  793: 	    '</a>';
  794:     }
  795:     if (($hash->{'src_'.$rid}=~/$LONCAPA::assess_re/) &&
  796: 	($hash->{'src_'.$rid} !~ m-^/uploaded/-)) {
  797: 
  798: 	if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'})) {
  799: 	    $metainfo.=
  800: 		'<a href="/adm/grades?symb='.&escape($symb).
  801: #               '&command=submission" target="LONcatInfo">'.
  802: 		'&command=submission">'.
  803:                 '<img src="/adm/lonMisc/subm_button.png" class="LC_icon"'.
  804:                 ' alt="'.&mt('View Submissions for a Student or a Group of Students').'"'.
  805:                 ' title="'.&mt('View Submissions for a Student or a Group of Students').'" />'.
  806: 		'</a>'.
  807: 		'<a href="/adm/grades?symb='.&escape($symb).
  808: #               '&command=gradingmenu" target="LONcatInfo">'.
  809: 		'&command=gradingmenu">'.
  810:                 '<img src="/res/adm/pages/pgrd.png" class="LC_icon"'.
  811:                 ' alt="'.&mt('Content Grades').'"'.
  812:                 ' title="'.&mt('Content Grades').'" />'.
  813: 		'</a>';
  814: 	}
  815: 	if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
  816: 	    $metainfo.=
  817: 		'<a href="/adm/parmset?symb='.&escape($symb).
  818: #               '" target="LONcatInfo">'.
  819: 		'" >'.
  820:                 '<img src="/adm/lonMisc/pprm_button.png" class="LC_icon"'.
  821:                 ' alt="'.&mt('Content Settings').'"'.
  822:                 ' title="'.&mt('Content Settings').'" />'.
  823: 		'</a>';
  824: 	}
  825:     }
  826:     if (($env{'request.course.id'}) && (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
  827:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  828:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  829:         my $file=&Apache::lonnet::declutter($hash->{'src_'.$rid});
  830:         my ($cfile,$home,$switchserver,$forceedit,$forceview) =
  831:             &Apache::lonnet::can_edit_resource($file,$cnum,$cdom,$hash->{'src_'.$rid},$symb);
  832:         if ($cfile ne '') {
  833:             my $jscall = &Apache::lonhtmlcommon::jump_to_editres($cfile,$home,$switchserver,
  834:                                                                  $forceedit,1,$symb,undef,
  835:                                                                  &escape($env{'form.title'}));
  836:             if ($jscall) {
  837:                 my $icon = 'pcstr.png';
  838:                 my $label = &mt('Edit');
  839:                 my $title = &mt('Edit this resource');
  840:                 my $pic = '<img src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/'.$icon).'"'.
  841:                           ' class="LC_icon" alt="'.$label.'" title="'.$title.'" />';
  842:                 $metainfo .= '&nbsp;<a href="javascript:'.$jscall.';">'.$pic.'</a>';
  843:             }
  844:         }
  845:     }
  846:     return $metainfo;
  847: }
  848: 
  849: sub add_countdown_timer {
  850:     my ($currdisp) = @_;
  851:     my ($collapse,$expand,$alttxt,$title);
  852:     if ($currdisp eq 'inline') {
  853:         $collapse = '&#9658;&nbsp;';
  854:     } else {
  855:         $expand = '&#9668;&nbsp;';
  856:     }
  857:     unless ($env{'environment.icons'} eq 'iconsonly') {
  858:         $alttxt = &mt('Timer');
  859:         $title = $alttxt.'&nbsp;';
  860:     }
  861:     my $desc = &mt('Countdown to due date/time');
  862:     my $output = <<END;
  863: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
  864: <span id="ddcountcollapse" class="LC_menubuttons_inline_text">
  865: $collapse
  866: </span></a>
  867: <span id="duedatecountdown" class="LC_menubuttons_inline_text" style="display: $currdisp;"></span>
  868: <a href="javascript:toggleCountdown();" class="LC_menubuttons_link">
  869: <span id="ddcountexpand" class="LC_menubuttons_inline_text" >$expand</span>
  870: <img src="/res/adm/pages/timer.png" title="$desc" class="LC_icon" alt="$alttxt" /><span class="LC_menubuttons_inline_text">$title</span></a>
  871: END
  872:     &Apache::lonhtmlcommon::clear_breadcrumb_tools();
  873:     &Apache::lonhtmlcommon::add_breadcrumb_tool('tools',$output);
  874:     return;
  875: }
  876: 
  877: 
  878: 1;
  879: __END__
  880: 
  881: 
  882: =head1 NAME
  883: 
  884: Apache::lonpage - Page Handler
  885: 
  886: =head1 SYNOPSIS
  887: 
  888: Invoked by /etc/httpd/conf/srm.conf:
  889: 
  890:  <LocationMatch "^/res/.*\.page$>
  891:  SetHandler perl-script
  892:  PerlHandler Apache::lonpage
  893:  </LocationMatch>
  894: 
  895: =head1 INTRODUCTION
  896: 
  897: This module renders a .page resource.
  898: 
  899: This is part of the LearningOnline Network with CAPA project
  900: described at http://www.lon-capa.org.
  901: 
  902: =head1 HANDLER SUBROUTINE
  903: 
  904: This routine is called by Apache and mod_perl.
  905: 
  906: =over 4
  907: 
  908: =item *
  909: 
  910: set document type for header only
  911: 
  912: =item *
  913: 
  914: tie db file
  915: 
  916: =item *
  917: 
  918: render page
  919: 
  920: =item *
  921: 
  922: add to symb list
  923: 
  924: =item *
  925: 
  926: page parms
  927: 
  928: =item *
  929: 
  930: Get SSI output, post parameters
  931: 
  932: =item *
  933: 
  934: SSI cell rendering
  935: 
  936: =item *
  937: 
  938: Deal with Applet codebases
  939: 
  940: =item *
  941: 
  942: Build page
  943: 
  944: =item *
  945: 
  946: send headers
  947: 
  948: =item *
  949: 
  950: start body
  951: 
  952: =item *
  953: 
  954: start form
  955: 
  956: =item *
  957: 
  958: start table
  959: 
  960: =item *
  961: 
  962: submit element, etc, render page, untie hash
  963: 
  964: =back
  965: 
  966: =head1 OTHER SUBROUTINES
  967: 
  968: =over 4
  969: 
  970: =item *
  971: 
  972: euclid() : Euclid's method for determining the greatest common denominator.
  973: 
  974: =item *
  975: 
  976: tracetable() : Build page table.
  977: 
  978: =back
  979: 
  980: =cut
  981: 
  982: 

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