Annotation of rat/lonpageflip.pm, revision 1.35
1.1 www 1: # The LearningOnline Network with CAPA
2: #
3: # Page flip handler
4: #
1.35 ! www 5: # $Id: lonpageflip.pm,v 1.34 2003/01/14 18:47:50 www 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;
1.34 www 52:
53: sub cleanup {
54: if (tied(%hash)){
55: &Apache::lonnet::logthis('Cleanup pageflip: hash');
56: unless (untie(%hash)) {
57: &Apache::lonnet::logthis('Failed cleanup pageflip: hash');
58: }
59: }
60: }
1.1 www 61:
1.3 www 62: sub addrid {
1.4 www 63: my ($current,$new,$condid)=@_;
64: unless ($condid) { $condid=0; }
1.27 www 65:
1.3 www 66: if ($current) {
1.5 www 67: $current.=','.$new;
1.3 www 68: } else {
1.5 www 69: $current=''.$new;
1.3 www 70: }
1.27 www 71:
1.3 www 72: return $current;
1.25 www 73: }
74:
75: sub fullmove {
76: my ($rid,$mapurl,$direction)=@_;
77: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.28 albertel 78: &GDBM_READER(),0640)) {
1.25 www 79: ($rid,$mapurl)=&move($rid,$mapurl,$direction);
80: untie(%hash);
81: }
82: return($rid,$mapurl);
1.1 www 83: }
84:
1.15 www 85: sub move {
86: my ($rid,$mapurl,$direction)=@_;
1.23 www 87: my $startoutrid=$rid;
1.15 www 88:
89: my $next='';
90:
91: my $mincond=1;
92: my $posnext='';
93: if ($direction eq 'forward') {
94: # --------------------------------------------------------------------- Forward
1.33 www 95: while ($hash{'type_'.$rid} eq 'finish') {
96: $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
1.15 www 97: }
98: map {
99: my $thiscond=
100: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
101: if ($thiscond>=$mincond) {
102: if ($posnext) {
103: $posnext.=','.$_.':'.$thiscond;
104: } else {
105: $posnext=$_.':'.$thiscond;
106: }
107: if ($thiscond>$mincond) { $mincond=$thiscond; }
108: }
109: } split(/\,/,$hash{'to_'.$rid});
110: map {
111: my ($linkid,$condval)=split(/\:/,$_);
112: if ($condval>=$mincond) {
113: $next=&addrid($next,$hash{'goesto_'.$linkid},
114: $hash{'condid_'.$hash{'undercond_'.$linkid}});
115: }
116: } split(/\,/,$posnext);
117: if ($hash{'is_map_'.$next}) {
1.23 www 118: # This jumps to the beginning of a new map (going down level)
1.15 www 119: if (
120: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
121: $mapurl=$hash{'src_'.$next};
122: $next=$hash{'map_start_'.$hash{'src_'.$next}};
123: }
1.23 www 124: } elsif
125: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
126: # This comes up from a map (coming up one level);
127: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
1.15 www 128: }
129: } elsif ($direction eq 'back') {
130: # ------------------------------------------------------------------- Backwards
1.33 www 131: while ($hash{'type_'.$rid} eq 'start') {
132: $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
133: }
1.15 www 134: map {
135: my $thiscond=
136: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
137: if ($thiscond>=$mincond) {
138: if ($posnext) {
139: $posnext.=','.$_.':'.$thiscond;
140: } else {
141: $posnext=$_.':'.$thiscond;
142: }
143: if ($thiscond>$mincond) { $mincond=$thiscond; }
144: }
145: } split(/\,/,$hash{'from_'.$rid});
146: map {
147: my ($linkid,$condval)=split(/\:/,$_);
148: if ($condval>=$mincond) {
149: $next=&addrid($next,$hash{'comesfrom_'.$linkid},
150: $hash{'condid_'.$hash{'undercond_'.$linkid}});
151: }
152: } split(/\,/,$posnext);
153: if ($hash{'is_map_'.$next}) {
1.24 www 154: # This jumps to the end of a new map (going down one level)
1.15 www 155: if (
156: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
157: $mapurl=$hash{'src_'.$next};
158: $next=$hash{'map_finish_'.$hash{'src_'.$next}};
159: }
1.24 www 160: } elsif
161: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
162: # This comes back up from a map (going up one level);
163: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
1.15 www 164: }
165: }
166: return ($next,$mapurl);
167: }
168:
1.1 www 169: # ================================================================ Main Handler
170:
171: sub handler {
1.2 www 172: my $r=shift;
1.1 www 173:
174: # ------------------------------------------- Set document type for header only
175:
1.2 www 176: if ($r->header_only) {
1.1 www 177: $r->content_type('text/html');
178: $r->send_http_header;
1.2 www 179: return OK;
180: }
181:
1.5 www 182: my %cachehash=();
183: my $multichoice=0;
184: my %multichoicehash=();
1.4 www 185: my $redirecturl='';
186: my $next='';
187: my @possibilities=();
1.2 www 188: if (($ENV{'form.postdata'})&&($ENV{'request.course.fn'})) {
189: $ENV{'form.postdata'}=~/(\w+)\:(.*)/;
190: my $direction=$1;
191: my $currenturl=$2;
1.10 www 192: if ($direction eq 'return') {
193: # -------------------------------------------------------- Return to last known
194: my $last;
195: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.28 albertel 196: &GDBM_READER(),0640)) {
1.10 www 197: $last=$hash{'last_known'};
198: untie(%hash);
199: }
200: my $newloc;
201: if ($last) {
1.29 www 202: $newloc=&Apache::lonnet::clutter((split(/\_\_\_/,$last))[1]);
1.10 www 203: } else {
204: $newloc='/adm/noidea.html';
205: }
206: $r->content_type('text/html');
207: $r->header_out(Location =>
208: 'http://'.$ENV{'HTTP_HOST'}.$newloc);
209:
210: return REDIRECT;
211: }
1.2 www 212: $currenturl=~s/^http\:\/\///;
213: $currenturl=~s/^[^\/]+//;
1.35 ! www 214: #
! 215: # Is the current URL on the map? If not, start with last known URL
! 216: #
! 217: unless (&Apache::lonnet::is_on_map($currenturl)) {
1.7 www 218: my $last;
219: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.28 albertel 220: &GDBM_READER(),0640)) {
1.7 www 221: $last=$hash{'last_known'};
222: untie(%hash);
223: }
224: if ($last) {
1.29 www 225: $currenturl=&Apache::lonnet::clutter((split(/\_\_\_/,$last))[1]);
1.7 www 226: } else {
227: $r->content_type('text/html');
228: $r->header_out(Location =>
229: 'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
1.9 www 230: return REDIRECT;
1.7 www 231: }
232: }
1.3 www 233: # ------------------------------------------- Do we have any idea where we are?
234: my $position;
235: if ($position=Apache::lonnet::symbread($currenturl)) {
236: # ------------------------------------------------------------------------- Yes
1.22 www 237: my ($startoutmap,$mapnum,$thisurl)=split(/\_\_\_/,$position);
238: $cachehash{$startoutmap}{$thisurl}=$mapnum;
1.23 www 239: $cachehash{$startoutmap}{'last_known'}=
240: &Apache::lonnet::declutter($currenturl);
1.20 albertel 241:
1.5 www 242: # ============================================================ Tie the big hash
1.3 www 243: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.28 albertel 244: &GDBM_READER(),0640)) {
1.29 www 245: my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
246: '.'.$mapnum;
1.14 www 247:
1.15 www 248: # ------------------------------------------------- Move forward, backward, etc
1.22 www 249: my $endupmap;
250: ($next,$endupmap)=&move($rid,$startoutmap,$direction);
1.15 www 251: # -------------------------------------- Do we have one and only one empty URL?
252: my $safecount=0;
1.26 www 253: while (($next) && ($next!~/\,/) &&
254: ((!$hash{'src_'.$next}) || ($hash{'randomout_'.$next}))
255: && ($safecount<10000)) {
1.22 www 256: ($next,$endupmap)=&move($next,$endupmap,$direction);
1.15 www 257: $safecount++;
258: }
1.22 www 259: # We are now at at least one non-empty URL
1.4 www 260: # ----------------------------------------------------- Check out possibilities
261: if ($next) {
262: @possibilities=split(/\,/,$next);
263: if ($#possibilities==0) {
1.5 www 264: # ---------------------------------------------- Only one possibility, redirect
265: $redirecturl=$hash{'src_'.$next};
1.22 www 266: $cachehash{$endupmap}
1.19 www 267: {&Apache::lonnet::declutter($redirecturl)}
1.5 www 268: =(split(/\./,$next))[1];
1.4 www 269: } else {
1.5 www 270: # ------------------------ There are multiple possibilities for a next resource
271: $multichoice=1;
272: map {
273: $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
274: $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
1.6 www 275: $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
1.32 bowersj2 276: (my $first, my $second) = $_ =~ /(\d+).(\d+)/;
277: my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$_});
278: $multichoicehash{'symb_'.$_} =
279: Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
280: $second.'___'.$symbSrc);
281:
1.22 www 282: my ($choicemap,$choiceres)=split(/\./,$_);
283: $cachehash
284: {&Apache::lonnet::declutter($hash{'src_'.$choicemap})}
285: {&Apache::lonnet::declutter(
1.5 www 286: $multichoicehash
287: {'src_'.$_}
288: )}
1.22 www 289: =$choiceres;
1.5 www 290: } @possibilities;
1.4 www 291: }
1.5 www 292: } else {
293: # -------------------------------------------------------------- No place to go
294: $multichoice=-1;
1.4 www 295: }
1.5 www 296: # ----------------- The program must come past this point to untie the big hash
1.3 www 297: untie(%hash);
1.5 www 298: # --------------------------------------------------------- Store position info
1.22 www 299: $cachehash{$startoutmap}{'last_direction'}=$direction;
1.19 www 300: foreach my $thismap (keys %cachehash) {
1.20 albertel 301: &Apache::lonnet::symblist($thismap,%{$cachehash{$thismap}});
1.19 www 302: }
1.5 www 303: # ============================================== Do not return before this line
1.4 www 304: if ($redirecturl) {
1.5 www 305: # ----------------------------------------------------- There is a URL to go to
1.31 www 306: # ------------------------------------------------- Check for critical messages
307: if ((time-$ENV{'user.criticalcheck.time'})>300) {
308: my @what=&Apache::lonnet::dump
309: ('critical',$ENV{'user.domain'},
310: $ENV{'user.name'});
311: if ($what[0]) {
312: if (($what[0] ne 'con_lost') &&
313: ($what[0]!~/^error\:/)) {
314: $redirecturl='/adm/email?critical=display';
315: }
316: }
317: &Apache::lonnet::appenv('user.criticalcheck.time'=>time);
318: }
319:
1.4 www 320: $r->content_type('text/html');
321: $r->header_out(Location =>
322: 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
323: return REDIRECT;
1.5 www 324: } else {
325: # --------------------------------------------------------- There was a problem
1.8 www 326: $r->content_type('text/html');
327: $r->send_http_header;
328: if ($#possibilities>0) {
329: $r->print(<<ENDSTART);
330: <head><title>Choose Next Location</title></head>
331: <body bgcolor="#FFFFFF">
332: <h1>LON-CAPA</h1>
333: There are several possibilities of where to go next.
334: <p>
335: Please click on the the resource you intend to access:
336: <p>
337: <table border=2>
338: <tr><th>Title</th><th>Type</th></tr>
339: ENDSTART
1.26 www 340: foreach (@possibilities) {
1.8 www 341: $r->print(
342: '<tr><td><a href="'.
1.32 bowersj2 343: $multichoicehash{'src_'.$_}.'?symb=' .
344: Apache::lonnet::escape($multichoicehash{'symb_'.$_}).'">'.
1.8 www 345: $multichoicehash{'title_'.$_}.
346: '</a></td><td>'.$multichoicehash{'type_'.$_}.
347: '</td></tr>');
1.26 www 348: }
1.8 www 349: $r->print('</table></body></html>');
350: return OK;
351: } else {
352: $r->print(<<ENDNONE);
353: <head><title>Choose Next Location</title></head>
354: <body bgcolor="#FFFFFF">
1.17 www 355: <img src="/adm/lonIcons/lonlogos.gif" align=right>
1.8 www 356: <h1>Sorry!</h1>
357: <h2>Next resource could not be identified.</h2>
1.17 www 358: <h3>You probably are at the beginning or the end of the course.</h3>
1.8 www 359: </body>
360: </html>
361: ENDNONE
362: return OK;
363: }
364: }
1.5 www 365: } else {
366: # ------------------------------------------------- Problem, could not tie hash
367: $ENV{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
368: return HTTP_NOT_ACCEPTABLE;
1.3 www 369: }
1.5 www 370: } else {
371: # ---------------------------------------- No, could not determine where we are
1.6 www 372: $r->internal_redirect('/adm/ambiguous');
1.2 www 373: }
1.5 www 374: } else {
1.2 www 375: # -------------------------- Class was not initialized or page fliped strangely
376: $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
377: return HTTP_NOT_ACCEPTABLE;
378: }
1.1 www 379: }
380:
381: 1;
382: __END__
383:
384:
385:
386:
387:
388:
389:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>