Annotation of rat/lonambiguous.pm, revision 1.8

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

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