File:  [LON-CAPA] / rat / lonambiguous.pm
Revision 1.22: download - view: text, annotated - select for diffs
Tue Nov 11 16:06:46 2008 UTC (15 years, 5 months ago) by jms
Branches: MAIN
CVS tags: HEAD
Added POD documentation

    1: # The LearningOnline Network with CAPA
    2: # Handler to resolve ambiguous file locations
    3: #
    4: # $Id: lonambiguous.pm,v 1.22 2008/11/11 16:06:46 jms 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: 
   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: 
   65: package Apache::lonambiguous;
   66: 
   67: use strict;
   68: use Apache::lonnet;
   69: use Apache::Constants qw(:common REDIRECT);
   70: use GDBM_File;
   71: use Apache::loncommon;
   72: use Apache::lonlocal;
   73: 
   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:     }
   83:     return OK;
   84: }
   85: 
   86: # ----------------------------------------------------------- Could not resolve
   87: 
   88: sub getlost {
   89:     my ($r,$errmsg)=@_;
   90:     $errmsg=&mt($errmsg);
   91:     &Apache::loncommon::content_type($r,'text/html');
   92:     $r->send_http_header;
   93:     $r->print(&Apache::loncommon::start_page('Could not handle ambiguous resource reference').
   94: 	      $errmsg.
   95: 	      &Apache::loncommon::end_page());
   96: }
   97: 
   98: # ================================================================ Main Handler
   99: 
  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: 
  109: sub handler {
  110:    my $r=shift;
  111: 
  112:    if ($r->header_only) {
  113:       &Apache::loncommon::content_type($r,'text/html');
  114:       $r->send_http_header;
  115:       return OK;
  116:    }
  117: 
  118: # ---------------------------------------------------------- Is this selecting?
  119:  
  120:    if ($env{'form.selecturl'}) {
  121:        my $envkey;
  122:        if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
  123:                     &GDBM_READER(),0640)) {
  124:           foreach $envkey (keys %env) {
  125:              if ($envkey=~/^form\.(\d+)\.(\d+)$/) {
  126: # ---------------------------------------------------- Update symb and redirect
  127: 	         my $mapid=$1;
  128:                  my $resid=$2;
  129:                  my $resurl=$bighash{'src_'.$mapid.'.'.$resid};
  130:                  &Apache::lonnet::symblist($bighash{'map_id_'.$mapid},
  131: 				           $resurl => [$resurl,$resid]);
  132:                  untie(%bighash);
  133:                  $r->header_out(Location => 
  134: 				&Apache::lonnet::absolute_url().$resurl);
  135:                  return REDIRECT;
  136:              }
  137: 	  }
  138:           untie(%bighash);
  139:        } else {
  140:           &getlost($r,'Could not access course structure.');
  141:           return OK;
  142:        }
  143:    }
  144: 
  145: # ---------------------------------------------------------- Do we have a case?
  146: 
  147:    my $thisfn;
  148:    unless (($thisfn=$env{'request.ambiguous'})&&($env{'request.course.fn'})) {
  149:        &getlost($r,'Could not find information on resource.');
  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:     
  159:     if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
  160:                   &GDBM_READER(),0640)) {
  161:        $syval=$hash{$thisfn};
  162:        untie(%hash);
  163:     }
  164: 
  165: # ---------------------------------------------------------- There was an entry
  166: 
  167:     if ($syval) {
  168: 	my ($page,undef,$res)=&Apache::lonnet::decode_symb($syval);
  169: 	if ($res eq 'page') {
  170: # ----------------------------------- Okay, this should have appeared on a page
  171: 	   &Apache::loncommon::content_type($r,'text/html');
  172:            $r->header_out(Location => 
  173: 			  &Apache::lonnet::absolute_url().
  174: 			  &Apache::lonnet::clutter($page));
  175:            return REDIRECT;
  176:        } else {
  177: #  There is not really a problem (???), but cannot go back without endless loop
  178:            &getlost($r,'The nature of the problem is unclear');
  179:            return OK;
  180:        }
  181:     }
  182: # ------------------------------------Encrypted requests go straight to navmaps
  183:    if ($env{'request.enc'}) {
  184:        &Apache::loncommon::content_type($r,'text/html');
  185:        $r->header_out(Location => 
  186: 		      &Apache::lonnet::absolute_url().'/adm/navmaps');
  187:        return REDIRECT;
  188:    }
  189: # ------------------------------------------------ Would be standalone resource
  190: 
  191:    if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
  192:                     &GDBM_READER(),0640)) {
  193: # ---------------------------------------------- Get ID(s) for current resource
  194:       my $ids=$bighash{'ids_'.&Apache::lonnet::clutter($thisfn)};
  195:       if ($ids) {
  196: # ------------------------------------------------------------------- Has ID(s)
  197:          my @possibilities=split(/\,/,$ids);
  198:          my $couldbe='';
  199:          foreach (@possibilities) {
  200:              if ($bighash{'encrypted_'.$_}) { next; }
  201: 	     my $symb=&make_symb($_);
  202:              if (&Apache::lonnet::allowed('bre',$bighash{'src_'.$_},$symb)) {
  203: 	         if ($couldbe) {
  204: 		     $couldbe.=','.$_;
  205:                  } else {
  206:                      $couldbe=$_;
  207:                  }
  208:              }
  209: 	 }
  210:          if ($couldbe) {
  211:             @possibilities=split(/\,/,$couldbe);
  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);
  217: 		&Apache::loncommon::content_type($r,'text/html');
  218: 		$r->header_out(Location => 
  219: 			       &Apache::lonnet::absolute_url().
  220: 			       $resurl.'?symb='.$symb);
  221: 		return REDIRECT;
  222: 	    }
  223:             if ($#possibilities>0) {
  224: # ----------------------------------------------- Okay, really multiple choices
  225: 	       &Apache::loncommon::content_type($r,'text/html');
  226:                $r->send_http_header;
  227:                my $start_page=
  228: 		   &Apache::loncommon::start_page('Pick Instance of Resource');
  229:                $r->print(<<ENDSTART);
  230: $start_page
  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:
  236: </p>
  237: <table border="2">
  238: <tr><th>Title</th><th>Part of ...</th></tr>
  239: ENDSTART
  240:                map {
  241: 		   my $resurl=$bighash{'src_'.$_};
  242:                    my $mapurl=$bighash{'map_id_'.(split(/\./,$_))[0]};
  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>');
  249:                } @possibilities;
  250:                $r->print('</table>'.&Apache::loncommon::end_page());
  251: 	       untie(%bighash);
  252:                return OK;
  253:             }
  254:          }
  255:       }
  256:       untie(%bighash);
  257:   }
  258: 
  259: # ------------------------------------ This handler should not have been called
  260:    &getlost($r,'Invalid call of handler');
  261:    return OK;
  262: }
  263: 
  264: 1;
  265: __END__
  266: 
  267: 
  268: 
  269: 
  270: 
  271: 
  272: 

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