File:  [LON-CAPA] / rat / lonambiguous.pm
Revision 1.9: download - view: text, annotated - select for diffs
Sat Apr 12 15:43:58 2003 UTC (21 years ago) by www
Branches: MAIN
CVS tags: version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, version_0_99_3, version_0_99_2, version_0_99_1, version_0_99_0, conference_2003, HEAD
Bug 1355 - update to use new routines.

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

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