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