Annotation of rat/lonambiguous.pm, revision 1.22

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

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