Annotation of rat/lonpageflip.pm, revision 1.29
1.1 www 1: # The LearningOnline Network with CAPA
2: #
3: # Page flip handler
4: #
1.29 ! www 5: # $Id: lonpageflip.pm,v 1.28 2002/08/12 18:21:42 albertel Exp $
1.18 www 6: #
7: # Copyright Michigan State University Board of Trustees
8: #
9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
10: #
11: # LON-CAPA is free software; you can redistribute it and/or modify
12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
16: # LON-CAPA is distributed in the hope that it will be useful,
17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
22: # along with LON-CAPA; if not, write to the Free Software
23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
27: # http://www.lon-capa.org/
28: #
1.1 www 29: # (Page Handler
30: #
31: # (TeX Content Handler
32: #
33: # 05/29/00,05/30 Gerd Kortemeyer)
34: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
35: # 10/02 Gerd Kortemeyer)
36: #
1.13 www 37: # 10/03,10/05,10/06,10/07,10/09,10/10,10/11,10/16,10/17,
1.14 www 38: # 11/14,11/16,
1.17 www 39: # 10/01/01,05/01,05/28,07/05 Gerd Kortemeyer
1.1 www 40:
41: package Apache::lonpageflip;
42:
43: use strict;
1.4 www 44: use Apache::Constants qw(:common :http REDIRECT);
1.1 www 45: use Apache::lonnet();
46: use HTML::TokeParser;
47: use GDBM_File;
48:
1.3 www 49: # ========================================================== Module Global Hash
50:
1.1 www 51: my %hash;
52:
1.3 www 53: sub addrid {
1.4 www 54: my ($current,$new,$condid)=@_;
55: unless ($condid) { $condid=0; }
1.27 www 56:
1.3 www 57: if ($current) {
1.5 www 58: $current.=','.$new;
1.3 www 59: } else {
1.5 www 60: $current=''.$new;
1.3 www 61: }
1.27 www 62:
1.3 www 63: return $current;
1.25 www 64: }
65:
66: sub fullmove {
67: my ($rid,$mapurl,$direction)=@_;
68: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.28 albertel 69: &GDBM_READER(),0640)) {
1.25 www 70: ($rid,$mapurl)=&move($rid,$mapurl,$direction);
71: untie(%hash);
72: }
73: return($rid,$mapurl);
1.1 www 74: }
75:
1.15 www 76: sub move {
77: my ($rid,$mapurl,$direction)=@_;
1.23 www 78: my $startoutrid=$rid;
1.15 www 79:
80: my $next='';
81:
82: my $mincond=1;
83: my $posnext='';
84: if ($direction eq 'forward') {
85: # --------------------------------------------------------------------- Forward
86: if ($hash{'type_'.$rid} eq 'finish') {
1.29 ! www 87: $rid=$hash{'ids_'.&Apache::lonnet::clutter($mapurl)};
1.15 www 88: }
89: map {
90: my $thiscond=
91: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
92: if ($thiscond>=$mincond) {
93: if ($posnext) {
94: $posnext.=','.$_.':'.$thiscond;
95: } else {
96: $posnext=$_.':'.$thiscond;
97: }
98: if ($thiscond>$mincond) { $mincond=$thiscond; }
99: }
100: } split(/\,/,$hash{'to_'.$rid});
101: map {
102: my ($linkid,$condval)=split(/\:/,$_);
103: if ($condval>=$mincond) {
104: $next=&addrid($next,$hash{'goesto_'.$linkid},
105: $hash{'condid_'.$hash{'undercond_'.$linkid}});
106: }
107: } split(/\,/,$posnext);
108: if ($hash{'is_map_'.$next}) {
1.23 www 109: # This jumps to the beginning of a new map (going down level)
1.15 www 110: if (
111: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
112: $mapurl=$hash{'src_'.$next};
113: $next=$hash{'map_start_'.$hash{'src_'.$next}};
114: }
1.23 www 115: } elsif
116: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
117: # This comes up from a map (coming up one level);
118: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
1.15 www 119: }
120: } elsif ($direction eq 'back') {
121: # ------------------------------------------------------------------- Backwards
122: if ($hash{'type_'.$rid} eq 'start') {
1.29 ! www 123: $rid=$hash{'ids_'.&Apache::lonnet::clutter($mapurl)};
1.15 www 124: }
125: map {
126: my $thiscond=
127: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
128: if ($thiscond>=$mincond) {
129: if ($posnext) {
130: $posnext.=','.$_.':'.$thiscond;
131: } else {
132: $posnext=$_.':'.$thiscond;
133: }
134: if ($thiscond>$mincond) { $mincond=$thiscond; }
135: }
136: } split(/\,/,$hash{'from_'.$rid});
137: map {
138: my ($linkid,$condval)=split(/\:/,$_);
139: if ($condval>=$mincond) {
140: $next=&addrid($next,$hash{'comesfrom_'.$linkid},
141: $hash{'condid_'.$hash{'undercond_'.$linkid}});
142: }
143: } split(/\,/,$posnext);
144: if ($hash{'is_map_'.$next}) {
1.24 www 145: # This jumps to the end of a new map (going down one level)
1.15 www 146: if (
147: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
148: $mapurl=$hash{'src_'.$next};
149: $next=$hash{'map_finish_'.$hash{'src_'.$next}};
150: }
1.24 www 151: } elsif
152: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
153: # This comes back up from a map (going up one level);
154: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
1.15 www 155: }
156: }
157: return ($next,$mapurl);
158: }
159:
1.1 www 160: # ================================================================ Main Handler
161:
162: sub handler {
1.2 www 163: my $r=shift;
1.1 www 164:
165: # ------------------------------------------- Set document type for header only
166:
1.2 www 167: if ($r->header_only) {
1.1 www 168: $r->content_type('text/html');
169: $r->send_http_header;
1.2 www 170: return OK;
171: }
172:
1.5 www 173: my %cachehash=();
174: my $multichoice=0;
175: my %multichoicehash=();
1.4 www 176: my $redirecturl='';
177: my $next='';
178: my @possibilities=();
1.2 www 179: if (($ENV{'form.postdata'})&&($ENV{'request.course.fn'})) {
180: $ENV{'form.postdata'}=~/(\w+)\:(.*)/;
181: my $direction=$1;
182: my $currenturl=$2;
1.10 www 183: if ($direction eq 'return') {
184: # -------------------------------------------------------- Return to last known
185: my $last;
186: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.28 albertel 187: &GDBM_READER(),0640)) {
1.10 www 188: $last=$hash{'last_known'};
189: untie(%hash);
190: }
191: my $newloc;
192: if ($last) {
1.29 ! www 193: $newloc=&Apache::lonnet::clutter((split(/\_\_\_/,$last))[1]);
1.10 www 194: } else {
195: $newloc='/adm/noidea.html';
196: }
197: $r->content_type('text/html');
198: $r->header_out(Location =>
199: 'http://'.$ENV{'HTTP_HOST'}.$newloc);
200:
201: return REDIRECT;
202: }
1.2 www 203: $currenturl=~s/^http\:\/\///;
204: $currenturl=~s/^[^\/]+//;
1.17 www 205: unless (($currenturl=~/^\/res\//) ||
206: ($currenturl=~/^\/adm\/wrapper\//)) {
1.7 www 207: my $last;
208: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.28 albertel 209: &GDBM_READER(),0640)) {
1.7 www 210: $last=$hash{'last_known'};
211: untie(%hash);
212: }
213: if ($last) {
1.29 ! www 214: $currenturl=&Apache::lonnet::clutter((split(/\_\_\_/,$last))[1]);
1.7 www 215: } else {
216: $r->content_type('text/html');
217: $r->header_out(Location =>
218: 'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
1.9 www 219: return REDIRECT;
1.7 www 220: }
221: }
1.3 www 222: # ------------------------------------------- Do we have any idea where we are?
223: my $position;
224: if ($position=Apache::lonnet::symbread($currenturl)) {
225: # ------------------------------------------------------------------------- Yes
1.22 www 226: my ($startoutmap,$mapnum,$thisurl)=split(/\_\_\_/,$position);
227: $cachehash{$startoutmap}{$thisurl}=$mapnum;
1.23 www 228: $cachehash{$startoutmap}{'last_known'}=
229: &Apache::lonnet::declutter($currenturl);
1.20 albertel 230:
1.5 www 231: # ============================================================ Tie the big hash
1.3 www 232: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.28 albertel 233: &GDBM_READER(),0640)) {
1.29 ! www 234: my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
! 235: '.'.$mapnum;
1.14 www 236:
1.15 www 237: # ------------------------------------------------- Move forward, backward, etc
1.22 www 238: my $endupmap;
239: ($next,$endupmap)=&move($rid,$startoutmap,$direction);
1.15 www 240: # -------------------------------------- Do we have one and only one empty URL?
241: my $safecount=0;
1.26 www 242: while (($next) && ($next!~/\,/) &&
243: ((!$hash{'src_'.$next}) || ($hash{'randomout_'.$next}))
244: && ($safecount<10000)) {
1.22 www 245: ($next,$endupmap)=&move($next,$endupmap,$direction);
1.15 www 246: $safecount++;
247: }
1.22 www 248: # We are now at at least one non-empty URL
1.4 www 249: # ----------------------------------------------------- Check out possibilities
250: if ($next) {
251: @possibilities=split(/\,/,$next);
252: if ($#possibilities==0) {
1.5 www 253: # ---------------------------------------------- Only one possibility, redirect
254: $redirecturl=$hash{'src_'.$next};
1.22 www 255: $cachehash{$endupmap}
1.19 www 256: {&Apache::lonnet::declutter($redirecturl)}
1.5 www 257: =(split(/\./,$next))[1];
1.4 www 258: } else {
1.5 www 259: # ------------------------ There are multiple possibilities for a next resource
260: $multichoice=1;
261: map {
262: $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
263: $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
1.6 www 264: $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
1.22 www 265: my ($choicemap,$choiceres)=split(/\./,$_);
266: $cachehash
267: {&Apache::lonnet::declutter($hash{'src_'.$choicemap})}
268: {&Apache::lonnet::declutter(
1.5 www 269: $multichoicehash
270: {'src_'.$_}
271: )}
1.22 www 272: =$choiceres;
1.5 www 273: } @possibilities;
1.4 www 274: }
1.5 www 275: } else {
276: # -------------------------------------------------------------- No place to go
277: $multichoice=-1;
1.4 www 278: }
1.5 www 279: # ----------------- The program must come past this point to untie the big hash
1.3 www 280: untie(%hash);
1.5 www 281: # --------------------------------------------------------- Store position info
1.22 www 282: $cachehash{$startoutmap}{'last_direction'}=$direction;
1.19 www 283: foreach my $thismap (keys %cachehash) {
1.20 albertel 284: &Apache::lonnet::symblist($thismap,%{$cachehash{$thismap}});
1.19 www 285: }
1.5 www 286: # ============================================== Do not return before this line
1.4 www 287: if ($redirecturl) {
1.5 www 288: # ----------------------------------------------------- There is a URL to go to
1.4 www 289: $r->content_type('text/html');
290: $r->header_out(Location =>
291: 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
292: return REDIRECT;
1.5 www 293: } else {
294: # --------------------------------------------------------- There was a problem
1.8 www 295: $r->content_type('text/html');
296: $r->send_http_header;
297: if ($#possibilities>0) {
298: $r->print(<<ENDSTART);
299: <head><title>Choose Next Location</title></head>
300: <body bgcolor="#FFFFFF">
301: <h1>LON-CAPA</h1>
302: There are several possibilities of where to go next.
303: <p>
304: Please click on the the resource you intend to access:
305: <p>
306: <table border=2>
307: <tr><th>Title</th><th>Type</th></tr>
308: ENDSTART
1.26 www 309: foreach (@possibilities) {
1.8 www 310: $r->print(
311: '<tr><td><a href="'.
312: $multichoicehash{'src_'.$_}.'">'.
313: $multichoicehash{'title_'.$_}.
314: '</a></td><td>'.$multichoicehash{'type_'.$_}.
315: '</td></tr>');
1.26 www 316: }
1.8 www 317: $r->print('</table></body></html>');
318: return OK;
319: } else {
320: $r->print(<<ENDNONE);
321: <head><title>Choose Next Location</title></head>
322: <body bgcolor="#FFFFFF">
1.17 www 323: <img src="/adm/lonIcons/lonlogos.gif" align=right>
1.8 www 324: <h1>Sorry!</h1>
325: <h2>Next resource could not be identified.</h2>
1.17 www 326: <h3>You probably are at the beginning or the end of the course.</h3>
1.8 www 327: </body>
328: </html>
329: ENDNONE
330: return OK;
331: }
332: }
1.5 www 333: } else {
334: # ------------------------------------------------- Problem, could not tie hash
335: $ENV{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
336: return HTTP_NOT_ACCEPTABLE;
1.3 www 337: }
1.5 www 338: } else {
339: # ---------------------------------------- No, could not determine where we are
1.6 www 340: $r->internal_redirect('/adm/ambiguous');
1.2 www 341: }
1.5 www 342: } else {
1.2 www 343: # -------------------------- Class was not initialized or page fliped strangely
344: $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
345: return HTTP_NOT_ACCEPTABLE;
346: }
1.1 www 347: }
348:
349: 1;
350: __END__
351:
352:
353:
354:
355:
356:
357:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>