Annotation of rat/lonpageflip.pm, revision 1.62
1.1 www 1: # The LearningOnline Network with CAPA
2: #
3: # Page flip handler
4: #
1.62 ! albertel 5: # $Id: lonpageflip.pm,v 1.61 2006/01/09 18:39:31 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:
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: }
1.61 albertel 99: foreach my $id (split(/\,/,$hash{'to_'.$rid})) {
100: my $thiscond=
101: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$id}});
1.15 www 102: if ($thiscond>=$mincond) {
103: if ($posnext) {
1.61 albertel 104: $posnext.=','.$id.':'.$thiscond;
1.15 www 105: } else {
1.61 albertel 106: $posnext=$id.':'.$thiscond;
1.15 www 107: }
108: if ($thiscond>$mincond) { $mincond=$thiscond; }
109: }
1.61 albertel 110: }
111: foreach my $id (split(/\,/,$posnext)) {
112: my ($linkid,$condval)=split(/\:/,$id);
1.15 www 113: if ($condval>=$mincond) {
114: $next=&addrid($next,$hash{'goesto_'.$linkid},
115: $hash{'condid_'.$hash{'undercond_'.$linkid}});
116: }
1.61 albertel 117: }
1.15 www 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.61 albertel 133: }
1.15 www 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.62 ! albertel 139: foreach my $id (split(/\,/,$hash{'from_'.$rid})) {
! 140: my $thiscond=
! 141: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$id}});
! 142: if ($thiscond>=$mincond) {
! 143: if ($posnext) {
! 144: $posnext.=','.$id.':'.$thiscond;
! 145: } else {
! 146: $posnext=$id.':'.$thiscond;
! 147: }
! 148: if ($thiscond>$mincond) { $mincond=$thiscond; }
! 149: }
! 150: }
! 151: foreach my $id (split(/\,/,$posnext)) {
! 152: my ($linkid,$condval)=split(/\:/,$id);
! 153: if ($condval>=$mincond) {
! 154: $next=&addrid($next,$hash{'comesfrom_'.$linkid},
! 155: $hash{'condid_'.$hash{'undercond_'.$linkid}});
! 156: }
! 157: }
1.15 www 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;
1.62 ! albertel 341: foreach my $id (@possibilities) {
! 342: $multichoicehash{'src_'.$id}=$hash{'src_'.$id};
! 343: $multichoicehash{'title_'.$id}=$hash{'title_'.$id};
! 344: $multichoicehash{'type_'.$id}=$hash{'type_'.$id};
! 345: (my $first, my $second) = $id =~ /(\d+).(\d+)/;
! 346: my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$id});
! 347: $multichoicehash{'symb_'.$id} =
1.32 bowersj2 348: Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
349: $second.'___'.$symbSrc);
350:
1.62 ! albertel 351: my ($choicemap,$choiceres)=split(/\./,$id);
1.52 albertel 352: my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
1.62 ! albertel 353: my $url=$multichoicehash{'src_'.$id};
1.52 albertel 354: $cachehash{$map}{$url}=[$url,$choiceres];
1.62 ! albertel 355: }
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;
1.59 www 404: my %lt=&Apache::lonlocal::texthash('title' => 'End of Sequence',
405: 'explain' =>
406: 'You have reached the end of the sequence of materials.',
407: 'back' => 'Go Back',
408: 'nav' => 'Navigate Course Content',
409: 'wherenext' =>
410: 'There are several possibilities of where to go next',
411: 'pick' =>
412: 'Please click on the the resource you intend to access',
413: 'titleheader' => 'Title',
414: 'type' => 'Type');
1.8 www 415: if ($#possibilities>0) {
1.37 www 416: my $bodytag=
417: &Apache::loncommon::bodytag('Multiple Resources');
1.8 www 418: $r->print(<<ENDSTART);
419: <head><title>Choose Next Location</title></head>
1.37 www 420: $bodytag
1.59 www 421: <h3>$lt{'wherenext'}</h3>
1.8 www 422: <p>
1.59 www 423: $lt{'pick'}:
1.8 www 424: <p>
425: <table border=2>
1.59 www 426: <tr><th>$lt{'titleheader'}</th><th>$lt{'type'}</th></tr>
1.8 www 427: ENDSTART
1.62 ! albertel 428: foreach my $id (@possibilities) {
1.8 www 429: $r->print(
430: '<tr><td><a href="'.
1.62 ! albertel 431: $multichoicehash{'src_'.$id}.'?symb=' .
! 432: Apache::lonnet::escape($multichoicehash{'symb_'.$id}).'">'.
! 433: $multichoicehash{'title_'.$id}.
! 434: '</a></td><td>'.$multichoicehash{'type_'.$id}.
1.8 www 435: '</td></tr>');
1.26 www 436: }
1.59 www 437: $r->print('</table>');
1.8 www 438: } else {
1.37 www 439: my $bodytag=&Apache::loncommon::bodytag('No Resource');
1.59 www 440: $r->print(<<ENDNONE);
1.37 www 441: <head><title>No Resource</title></head>
442: $bodytag
1.59 www 443: <h3>$lt{'title'}</h3>
444: <p>$lt{'explain'}</p>
445: ENDNONE
446: }
447: $r->print(<<ENDMENU);
1.37 www 448: <ul>
1.59 www 449: <li><a href="/adm/flip?postdata=return:">$lt{'back'}</a></li>
450: <li><a href="/adm/navmaps">$lt{'nav'}</a></li>
451: </ul></body></html>
452: ENDMENU
453: return OK;
454: }
1.5 www 455: } else {
456: # ------------------------------------------------- Problem, could not tie hash
1.53 albertel 457: $env{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
1.5 www 458: return HTTP_NOT_ACCEPTABLE;
1.3 www 459: }
1.5 www 460: } else {
461: # ---------------------------------------- No, could not determine where we are
1.42 albertel 462: $r->internal_redirect('/adm/ambiguous');
1.2 www 463: }
1.5 www 464: } else {
1.2 www 465: # -------------------------- Class was not initialized or page fliped strangely
1.53 albertel 466: $env{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
1.2 www 467: return HTTP_NOT_ACCEPTABLE;
468: }
1.1 www 469: }
470:
471: 1;
472: __END__
473:
474:
475:
476:
477:
478:
479:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>