Annotation of rat/lonambiguous.pm, revision 1.3

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to resolve ambiguous file locations
                      3: #
                      4: # (TeX Content Handler
                      5: #
                      6: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
                      7: #
1.3     ! www         8: # 10/11,10/12,10/16 Gerd Kortemeyer
1.1       www         9: 
                     10: package Apache::lonambiguous;
                     11: 
                     12: use strict;
                     13: use Apache::lonnet;
                     14: use Apache::Constants qw(:common REDIRECT);
                     15: use GDBM_File;
                     16: 
                     17: 
                     18: # ----------------------------------------------------------- Could not resolve
                     19: 
                     20: sub getlost {
1.2       www        21:     my ($r,$errmsg)=@_;
1.1       www        22:     $r->content_type('text/html');
                     23:     $r->send_http_header;
1.2       www        24:     $r->print(
                     25:  '<head><title>Unknown Error</title></head><body bgcolor="#FFFFFF"><h1>'.
                     26:  'LON-CAPA</h1>Could not handle ambiguous resource reference.<p>'.$errmsg.
                     27:  '</body></html>');
1.1       www        28: }
                     29: 
                     30: # ================================================================ Main Handler
                     31: 
                     32: sub handler {
                     33:    my $r=shift;
                     34: 
                     35:    if ($r->header_only) {
                     36:       $r->content_type('text/html');
                     37:       $r->send_http_header;
                     38:       return OK;
                     39:    }
                     40: 
1.2       www        41: # ---------------------------------------------------------- Is this selecting?
                     42:    
                     43:    my %bighash;
                     44: 
                     45:    if ($ENV{'form.selecturl'}) {
                     46:        my $envkey;
                     47:        if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
                     48:                     &GDBM_READER,0640)) {
                     49:           foreach $envkey (keys %ENV) {
                     50:              if ($envkey=~/^form\.(\d+)\.(\d+)$/) {
1.3     ! www        51: # ---------------------------------------------------- Update symb and redirect
1.2       www        52: 	         my $mapid=$1;
                     53:                  my $resid=$2;
                     54:                  my $resurl=$bighash{'src_'.$mapid.'.'.$resid};
                     55:                  &Apache::lonnet::symblist($bighash{'map_id_'.$mapid},
                     56: 				           $resurl => $resid);
1.3     ! www        57:                  untie(%bighash);
1.2       www        58:                  $r->header_out(Location => 
                     59:                                 'http://'.$ENV{'HTTP_HOST'}.$resurl);
                     60:                  return REDIRECT;
                     61:              }
                     62: 	  }
                     63:           untie(%bighash);
                     64:        } else {
                     65:           &getlost($r,'Could not access course structure.');
                     66:           return OK;
                     67:        }
                     68:    }
                     69: 
1.1       www        70: # ---------------------------------------------------------- Do we have a case?
                     71: 
                     72:    my $thisfn;
                     73:    unless (($thisfn=$ENV{'request.ambiguous'})&&($ENV{'request.course.fn'})) {
1.2       www        74:        &getlost($r,'Could not find information on resource.');
1.1       www        75:        return OK;
                     76:    }
                     77:       
                     78: # ---------------------------------- Should this file have been part of a page?
                     79: 
                     80:     $thisfn=&Apache::lonnet::declutter($thisfn);
                     81:     my %hash;
                     82:     my $syval='';
                     83:     
                     84:     if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
                     85:                   &GDBM_READER,0640)) {
                     86:        $syval=$hash{$thisfn};
                     87:        untie(%hash);
                     88:     }
1.2       www        89: 
1.1       www        90: # ---------------------------------------------------------- There was an entry
1.2       www        91: 
1.1       www        92:     if ($syval) {
1.2       www        93: 
1.1       www        94:        if ($syval=~/\_$/) {
                     95: # ----------------------------------- Okay, this should have appeared on a page
                     96: 	   $syval=~s/\_\_\_$//;
                     97:  	   $r->content_type('text/html');
                     98:            $r->header_out(Location => 
                     99:                 'http://'.$ENV{'HTTP_HOST'}.'/res/'.$syval);
                    100:            return REDIRECT;
                    101:        } else {
                    102: #  There is not really a problem (???), but cannot go back without endless loop
1.2       www       103:            &getlost($r,'The nature of the problem is unclear');
1.1       www       104:            return OK;
                    105:        }
                    106:     }
                    107: 
                    108: # ------------------------------------------------ Would be standalone resource
                    109: 
                    110:    if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
                    111:                     &GDBM_READER,0640)) {
                    112: # ---------------------------------------------- Get ID(s) for current resource
                    113:       my $ids=$bighash{'ids_/res/'.$thisfn};
                    114:       if ($ids) {
                    115: # ------------------------------------------------------------------- Has ID(s)
                    116:          my @possibilities=split(/\,/,$ids);
                    117:          my $couldbe='';
                    118:          map {
1.2       www       119:              if (&Apache::lonnet::allowed('bre',$bighash{'src_'.$_})) {
1.1       www       120: 	         if ($couldbe) {
                    121: 		     $couldbe.=','.$_;
                    122:                  } else {
                    123:                      $couldbe=$_;
                    124:                  }
                    125:              }
                    126: 	 } @possibilities;
                    127:          if ($couldbe) {
                    128:             @possibilities=split(/\,/,$couldbe);
                    129:             if ($#possibilities>0) {
                    130: # ----------------------------------------------- Okay, really multiple choices
                    131:                $r->content_type('text/html');
                    132:                $r->send_http_header;
1.2       www       133:                $r->print(<<ENDSTART);
                    134: <head><title>Choose Location</title></head>
                    135: <body bgcolor="#FFFFFF">
                    136: <h1>LON-CAPA</h1>
                    137: The resource you had been accessing appears more than once in this course,
                    138: and LON-CAPA has insufficient session information to determine which instance
                    139: of the resource you meant.
                    140: <p>
                    141: Please click on the instance of the resource you intended to access:
                    142: <p>
                    143: <form action="/adm/ambiguous" method=post>
                    144: <input type=hidden name=orgurl value="$thisfn">
                    145: <input type=hidden name=selecturl value=1>
                    146: <table border=2>
                    147: <tr><th>&nbsp;</th><th>Title</th><th>Type</th><th>Part of ...</th></tr>
                    148: ENDSTART
                    149:                map {
                    150:                    my $mapurl=$bighash{'map_id_'.(split(/\./,$_))[0]};
                    151:                    $r->print('<tr><td><input type=submit value=Select name="'.
                    152:                               $_.'"></td><td>'.$bighash{'title_'.$_}.
                    153:                               '</td><td>'.$bighash{'type_'.$_}.
                    154:                               '</td><td><a href="'.$mapurl.'">'.$mapurl.
                    155: 			      '</a></td></tr>');
                    156:                } @possibilities;
                    157:                $r->print('</table></form></body></html>');
1.1       www       158: 	       untie(%bighash);
                    159:                return OK;
                    160:             }
                    161:          }
                    162:       }
                    163:       untie(%bighash);
                    164:   }
                    165: 
                    166: # ------------------------------------ This handler should not have been called
1.2       www       167:    &getlost($r,'Invalid call of handler');
1.1       www       168:    return OK;
                    169: }
                    170: 
                    171: 1;
                    172: __END__
                    173: 
                    174: 
                    175: 
                    176: 
                    177: 
                    178: 
                    179: 

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