Annotation of rat/lonambiguous.pm, revision 1.2

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

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