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