Annotation of rat/lonambiguous.pm, revision 1.18

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to resolve ambiguous file locations
                      3: #
1.18    ! albertel    4: # $Id: lonambiguous.pm,v 1.17 2006/02/07 19:46:08 albertel Exp $
1.4       www         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: #
1.1       www        28: 
                     29: package Apache::lonambiguous;
                     30: 
                     31: use strict;
                     32: use Apache::lonnet;
                     33: use Apache::Constants qw(:common REDIRECT);
                     34: use GDBM_File;
1.9       www        35: use Apache::loncommon;
1.10      www        36: use Apache::lonlocal;
1.1       www        37: 
1.8       www        38: my %bighash;
                     39: 
                     40: sub cleanup {
                     41:     if (tied(%bighash)){
                     42: 	&Apache::lonnet::logthis('Cleanup ambiguous: bighash');
                     43:         unless (untie(%bighash)) {
                     44: 	    &Apache::lonnet::logthis('Failed cleanup ambiguous: bighash');
                     45:         }
                     46:     }
1.17      albertel   47:     return OK;
1.8       www        48: }
1.1       www        49: 
                     50: # ----------------------------------------------------------- Could not resolve
                     51: 
                     52: sub getlost {
1.2       www        53:     my ($r,$errmsg)=@_;
1.10      www        54:     $errmsg=&mt($errmsg);
1.14      albertel   55:     &Apache::loncommon::content_type($r,'text/html');
1.1       www        56:     $r->send_http_header;
1.18    ! albertel   57:     $r->print(&Apache::loncommon::start_page('Could not handle ambiguous resource reference').
        !            58: 	      $errmsg.
        !            59: 	      (&Apache::loncommon::end_page());
1.1       www        60: }
                     61: 
                     62: # ================================================================ Main Handler
                     63: 
1.11      albertel   64: sub make_symb {
                     65:     my ($id)=@_;
                     66:     my ($mapid,$resid)=split(/\./,$id);
                     67:     my $map=$bighash{'map_id_'.$mapid};
                     68:     my $res=$bighash{'src_'.$id};
                     69:     my $symb=&Apache::lonnet::encode_symb($map,$resid,$res);
                     70:     return $symb;
                     71: }
                     72: 
1.1       www        73: sub handler {
                     74:    my $r=shift;
                     75: 
                     76:    if ($r->header_only) {
1.10      www        77:       &Apache::loncommon::content_type($r,'text/html');
1.1       www        78:       $r->send_http_header;
                     79:       return OK;
                     80:    }
                     81: 
1.2       www        82: # ---------------------------------------------------------- Is this selecting?
1.8       www        83:  
1.16      albertel   84:    if ($env{'form.selecturl'}) {
1.2       www        85:        my $envkey;
1.16      albertel   86:        if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.5       albertel   87:                     &GDBM_READER(),0640)) {
1.16      albertel   88:           foreach $envkey (keys %env) {
1.2       www        89:              if ($envkey=~/^form\.(\d+)\.(\d+)$/) {
1.3       www        90: # ---------------------------------------------------- Update symb and redirect
1.2       www        91: 	         my $mapid=$1;
                     92:                  my $resid=$2;
                     93:                  my $resurl=$bighash{'src_'.$mapid.'.'.$resid};
                     94:                  &Apache::lonnet::symblist($bighash{'map_id_'.$mapid},
1.15      albertel   95: 				           $resurl => [$resurl,$resid]);
1.3       www        96:                  untie(%bighash);
1.2       www        97:                  $r->header_out(Location => 
                     98:                                 'http://'.$ENV{'HTTP_HOST'}.$resurl);
                     99:                  return REDIRECT;
                    100:              }
                    101: 	  }
                    102:           untie(%bighash);
                    103:        } else {
                    104:           &getlost($r,'Could not access course structure.');
                    105:           return OK;
                    106:        }
                    107:    }
                    108: 
1.1       www       109: # ---------------------------------------------------------- Do we have a case?
                    110: 
                    111:    my $thisfn;
1.16      albertel  112:    unless (($thisfn=$env{'request.ambiguous'})&&($env{'request.course.fn'})) {
1.2       www       113:        &getlost($r,'Could not find information on resource.');
1.1       www       114:        return OK;
                    115:    }
                    116:       
                    117: # ---------------------------------- Should this file have been part of a page?
                    118: 
                    119:     $thisfn=&Apache::lonnet::declutter($thisfn);
                    120:     my %hash;
                    121:     my $syval='';
                    122:     
1.16      albertel  123:     if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.5       albertel  124:                   &GDBM_READER(),0640)) {
1.1       www       125:        $syval=$hash{$thisfn};
                    126:        untie(%hash);
                    127:     }
1.2       www       128: 
1.1       www       129: # ---------------------------------------------------------- There was an entry
1.2       www       130: 
1.1       www       131:     if ($syval) {
1.15      albertel  132: 	my ($page,undef,$res)=&Apache::lonnet::decode_symb($syval);
                    133: 	if ($res eq 'page') {
1.1       www       134: # ----------------------------------- Okay, this should have appeared on a page
1.14      albertel  135: 	   &Apache::loncommon::content_type($r,'text/html');
1.1       www       136:            $r->header_out(Location => 
1.15      albertel  137: 			  'http://'.$ENV{'HTTP_HOST'}.
                    138: 			  &Apache::lonnet::clutter($page));
1.1       www       139:            return REDIRECT;
                    140:        } else {
                    141: #  There is not really a problem (???), but cannot go back without endless loop
1.2       www       142:            &getlost($r,'The nature of the problem is unclear');
1.1       www       143:            return OK;
                    144:        }
                    145:     }
1.13      albertel  146: # ------------------------------------Encrypted requests go straight to navmaps
1.16      albertel  147:    if ($env{'request.enc'}) {
1.14      albertel  148:        &Apache::loncommon::content_type($r,'text/html');
1.13      albertel  149:        $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.'/adm/navmaps');
                    150:        return REDIRECT;
                    151:    }
1.1       www       152: # ------------------------------------------------ Would be standalone resource
                    153: 
1.16      albertel  154:    if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.5       albertel  155:                     &GDBM_READER(),0640)) {
1.1       www       156: # ---------------------------------------------- Get ID(s) for current resource
1.7       albertel  157:       my $ids=$bighash{'ids_'.&Apache::lonnet::clutter($thisfn)};
1.1       www       158:       if ($ids) {
                    159: # ------------------------------------------------------------------- Has ID(s)
                    160:          my @possibilities=split(/\,/,$ids);
                    161:          my $couldbe='';
1.11      albertel  162:          foreach (@possibilities) {
                    163:              if ($bighash{'encrypted_'.$_}) { next; }
                    164: 	     my $symb=&make_symb($_);
                    165:              if (&Apache::lonnet::allowed('bre',$bighash{'src_'.$_},$symb)) {
1.1       www       166: 	         if ($couldbe) {
                    167: 		     $couldbe.=','.$_;
                    168:                  } else {
                    169:                      $couldbe=$_;
                    170:                  }
                    171:              }
1.11      albertel  172: 	 }
1.1       www       173:          if ($couldbe) {
                    174:             @possibilities=split(/\,/,$couldbe);
1.12      albertel  175: 	    if ($#possibilities==0) {
                    176: 		my $id=$possibilities[0];
                    177: 		my $resurl=$bighash{'src_'.$id};
                    178: 		my $mapurl=$bighash{'map_id_'.(split(/\./,$id))[0]};
                    179: 		my $symb=&make_symb($id);
1.14      albertel  180: 		&Apache::loncommon::content_type($r,'text/html');
1.12      albertel  181: 		&Apache::lonnet::logthis('http://'.$ENV{'HTTP_HOST'}.$resurl.'?symb='.$symb);
                    182: 		$r->header_out(Location => 
                    183: 			   'http://'.$ENV{'HTTP_HOST'}.$resurl.'?symb='.$symb);
                    184: 		return REDIRECT;
                    185: 	    }
1.1       www       186:             if ($#possibilities>0) {
                    187: # ----------------------------------------------- Okay, really multiple choices
1.14      albertel  188: 	       &Apache::loncommon::content_type($r,'text/html');
1.1       www       189:                $r->send_http_header;
1.18    ! albertel  190:                my $start_page=
        !           191: 		   &Apache::loncommon::start_page('Pick Instance of Resource');
1.2       www       192:                $r->print(<<ENDSTART);
1.18    ! albertel  193: $start_page
1.2       www       194: The resource you had been accessing appears more than once in this course,
                    195: and LON-CAPA has insufficient session information to determine which instance
                    196: of the resource you meant.
                    197: <p>
                    198: Please click on the instance of the resource you intended to access:
1.11      albertel  199: </p>
                    200: <table border="2">
                    201: <tr><th>Title</th><th>Part of ...</th></tr>
1.2       www       202: ENDSTART
                    203:                map {
1.11      albertel  204: 		   my $resurl=$bighash{'src_'.$_};
1.2       www       205:                    my $mapurl=$bighash{'map_id_'.(split(/\./,$_))[0]};
1.11      albertel  206: 		   my $symb=&make_symb($_);
                    207: 		   $r->print('<tr><td><a href="'.$resurl.'?symb='.$symb.'">'.
                    208: 			     &Apache::lonnet::gettitle($symb).
                    209: 			     '</a></td><td>'.
                    210: 			     &Apache::lonnet::gettitle($mapurl).'&nbsp;'.
                    211: 			     '</td></tr>');
1.2       www       212:                } @possibilities;
1.18    ! albertel  213:                $r->print('</table>'.&Apache::loncommon::end_page());
1.1       www       214: 	       untie(%bighash);
                    215:                return OK;
                    216:             }
                    217:          }
                    218:       }
                    219:       untie(%bighash);
                    220:   }
                    221: 
                    222: # ------------------------------------ This handler should not have been called
1.2       www       223:    &getlost($r,'Invalid call of handler');
1.1       www       224:    return OK;
                    225: }
                    226: 
                    227: 1;
                    228: __END__
                    229: 
                    230: 
                    231: 
                    232: 
                    233: 
                    234: 
                    235: 

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