Annotation of loncom/interface/lonnavmaps.pm, revision 1.4

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

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