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