File:  [LON-CAPA] / loncom / interface / lonnavmaps.pm
Revision 1.6: download - view: text, annotated - select for diffs
Thu Jan 11 16:25:40 2001 UTC (23 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Lists headings

    1: # The LearningOnline Network with CAPA
    2: # Navigate Maps Handler
    3: #
    4: # (Page Handler
    5: #
    6: # (TeX Content Handler
    7: #
    8: # 05/29/00,05/30 Gerd Kortemeyer)
    9: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
   10: # 10/02,10/10,10/14,10/16,10/18,10/19,10/31,11/6,11/14,11/16 Gerd Kortemeyer)
   11: #
   12: # 3/1/1,6/1 Gerd Kortemeyer
   13: 
   14: package Apache::lonnavmaps;
   15: 
   16: use strict;
   17: use Apache::Constants qw(:common :http);
   18: use Apache::lonnet();
   19: use HTML::TokeParser;
   20: use GDBM_File;
   21: 
   22: # -------------------------------------------------------------- Module Globals
   23: my %hash;
   24: my @rows;
   25: 
   26: # ------------------------------------------------------------------ Euclid gcd
   27: 
   28: sub euclid {
   29:     my ($e,$f)=@_;
   30:     my $a; my $b; my $r;
   31:     if ($e>$f) { $b=$e; $r=$f; } else { $r=$e; $b=$f; }
   32:     while ($r!=0) {
   33: 	$a=$b; $b=$r;
   34:         $r=$a%$b;
   35:     }
   36:     return $b;
   37: }
   38: 
   39: # ------------------------------------------------------------ Build page table
   40: 
   41: sub tracetable {
   42:     my ($sofar,$rid,$beenhere)=@_;
   43:     my $further=$sofar;
   44:     unless ($beenhere=~/\&$rid\&/) {
   45:        $beenhere.=$rid.'&';  
   46: 
   47:        if (defined($hash{'is_map_'.$rid})) {
   48:            $sofar++;      
   49:            if (defined($rows[$sofar])) {
   50:               $rows[$sofar].='&h'.$rid;
   51:            } else {
   52:               $rows[$sofar]='h'.$rid;
   53:            }
   54:            if ((defined($hash{'map_start_'.$hash{'src_'.$rid}})) &&
   55:                (defined($hash{'map_finish_'.$hash{'src_'.$rid}}))) {
   56:               my $frid=$hash{'map_finish_'.$hash{'src_'.$rid}};
   57: 	      $sofar=
   58:                 &tracetable($sofar,$hash{'map_start_'.$hash{'src_'.$rid}},
   59:                 '&'.$frid.'&');
   60:               $sofar++;
   61:               if ($hash{'src_'.$frid}) {
   62:                my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$frid});
   63:                if (($brepriv eq '2') || ($brepriv eq 'F')) {
   64:                  if (defined($rows[$sofar])) {
   65:                    $rows[$sofar].='&'.$frid;
   66:                  } else {
   67:                    $rows[$sofar]=$frid;
   68:                  }
   69: 	       }
   70: 	      }
   71: 	   }
   72:        } else {
   73:           $sofar++;
   74:           if ($hash{'src_'.$rid}) {
   75:            my $brepriv=&Apache::lonnet::allowed('bre',$hash{'src_'.$rid});
   76:            if (($brepriv eq '2') || ($brepriv eq 'F')) {
   77:              if (defined($rows[$sofar])) {
   78:                $rows[$sofar].='&'.$rid;
   79:              } else {
   80:                $rows[$sofar]=$rid;
   81:              }
   82: 	   }
   83:           }
   84:        }
   85: 
   86:        if (defined($hash{'to_'.$rid})) {
   87: 	  my $mincond=1;
   88:           my $next='';
   89:           map {
   90:               my $thiscond=
   91:       &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
   92:               if ($thiscond>=$mincond) {
   93: 		  if ($next) {
   94: 		      $next.=','.$_.':'.$thiscond;
   95:                   } else {
   96:                       $next=$_.':'.$thiscond;
   97: 		  }
   98:                   if ($thiscond>$mincond) { $mincond=$thiscond; }
   99: 	      }
  100:           } split(/\,/,$hash{'to_'.$rid});
  101:           map {
  102:               my ($linkid,$condval)=split(/\:/,$_);
  103:               if ($condval>=$mincond) {
  104:                 my $now=&tracetable($sofar,$hash{'goesto_'.$linkid},$beenhere);
  105:                 if ($now>$further) { $further=$now; }
  106: 	      }
  107:           } split(/\,/,$next);
  108: 
  109:        }
  110:     }
  111:     return $further;
  112: }
  113: 
  114: # ================================================================ Main Handler
  115: 
  116: sub handler {
  117:   my $r=shift;
  118: 
  119: 
  120: # ------------------------------------------- Set document type for header only
  121: 
  122:   if ($r->header_only) {
  123:        if ($ENV{'browser.mathml'}) {
  124:            $r->content_type('text/xml');
  125:        } else {
  126:            $r->content_type('text/html');
  127:        }
  128:        $r->send_http_header;
  129:        return OK;
  130:    }
  131: 
  132:   my $requrl=$r->uri;
  133: # ----------------------------------------------------------------- Tie db file
  134:   if ($ENV{'request.course.fn'}) {
  135:       my $fn=$ENV{'request.course.fn'};
  136:       if (-e "$fn.db") {
  137:           if (tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER,0640)) {
  138: # ------------------------------------------------------------------- Hash tied
  139:               my $firstres=$hash{'map_start_/res/'.$ENV{'request.course.uri'}};
  140:               my $lastres=$hash{'map_finish_/res/'.$ENV{'request.course.uri'}};
  141:               if (($firstres) && ($lastres)) {
  142: # ----------------------------------------------------------------- Render page
  143: 
  144:                   @rows=();
  145: 
  146:                   &tracetable(0,$firstres,'&'.$lastres.'&');
  147:                   if ($hash{'src_'.$lastres}) {
  148:                      my $brepriv=
  149:                         &Apache::lonnet::allowed('bre',$hash{'src_'.$lastres});
  150:                      if (($brepriv eq '2') || ($brepriv eq 'F')) {
  151:                         $rows[$#rows+1]=''.$lastres;
  152: 		     }
  153: 		  }
  154: 
  155: # ------------------------------------------------------------------ Page parms
  156: 
  157:                   my $j;
  158:                   my $i;
  159:                   my $lcm=1;
  160:                   my $contents=0;
  161: 
  162: # ---------------------------------------------- Go through table to get layout
  163: 
  164:                   for ($i=0;$i<=$#rows;$i++) {
  165: 		     if ($rows[$i]) {
  166: 		      $contents++;
  167:                       my @colcont=split(/\&/,$rows[$i]);
  168:                       $lcm*=($#colcont+1)/euclid($lcm,($#colcont+1));
  169:                      } 
  170:                   }
  171: 
  172: 
  173:                   unless ($contents) {
  174:                       $r->content_type('text/html');
  175:                       $r->send_http_header;
  176:                       $r->print('<html><body>Empty Map.</body></html>');
  177:                   } else {
  178: # ------------------------------------------------------------------ Build page
  179: 
  180: # ---------------------------------------------------------------- Send headers
  181: 
  182:                           $r->content_type('text/html');
  183:                           $r->send_http_header;
  184:                           $r->print(
  185:                    '<html><head><title>Navigate LON-CAPA Maps</title></head>');
  186: 
  187: 			  $r->print('<body bgcolor="#FFFFFF">'.
  188:                            '<img align=right src=/adm/lonIcons/lonlogos.gif>'.
  189:                                     '<h1>Navigate Course Map</h1>');
  190:                           $r->rflush();
  191: # ----------------------------------------------------------------- Start table
  192:                       $r->print('<table cols="'.$lcm.'" border="0">');
  193:                       for ($i=0;$i<=$#rows;$i++) {
  194: 			if ($rows[$i]) {
  195:                           $r->print("\n<tr>");
  196:                           my @colcont=split(/\&/,$rows[$i]);
  197:                           my $avespan=$lcm/($#colcont+1);
  198:                           for ($j=0;$j<=$#colcont;$j++) {
  199:                               my $rid=$colcont[$j];
  200:                               my $add='<td>&nbsp;&nbsp;';
  201:                               if ($rid=~/^h(.+)/) {
  202: 				  $rid=$1;
  203:                                   $add='<th bgcolor="#AAFF55">';
  204:                               }
  205:                               $r->print($add.'<a href="'.
  206:                                 $hash{'src_'.$rid}.'">'.
  207:                                 $hash{'title_'.$rid}.'</a>');
  208:                               $r->print('</td>');
  209:                           }
  210:                           $r->print('</tr>');
  211: 		        }
  212:                       }
  213:                       $r->print("\n</table>");
  214: 
  215:                       $r->print('</body></html>');
  216: # -------------------------------------------------------------------- End page
  217:                   }                  
  218: # ------------------------------------------------------------- End render page
  219:               } else {
  220:                   $r->content_type('text/html');
  221:                   $r->send_http_header;
  222: 		  $r->print('<html><body>Coursemap undefined.</body></html>');
  223:               }
  224: # ------------------------------------------------------------------ Untie hash
  225:               unless (untie(%hash)) {
  226:                    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  227:                        "Could not untie coursemap $fn (browse).</font>"); 
  228:               }
  229: # -------------------------------------------------------------------- All done
  230: 	      return OK;
  231: # ----------------------------------------------- Errors, hash could no be tied
  232:           }
  233:       } 
  234:   }
  235: 
  236:   $ENV{'user.error.msg'}="$requrl:bre:0:0:Course not initialized";
  237:   return HTTP_NOT_ACCEPTABLE; 
  238: }
  239: 
  240: 1;
  241: __END__
  242: 
  243: 
  244: 
  245: 
  246: 
  247: 
  248: 

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