File:  [LON-CAPA] / rat / lonambiguous.pm
Revision 1.17: download - view: text, annotated - select for diffs
Tue Feb 7 19:46:08 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, HEAD
- Apache 2 doesn't like it when PerlCleanupHandlers don't return an Apache::Constant (like OK or DECLINED ...)

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

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