Annotation of rat/lonambiguous.pm, revision 1.23

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

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