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