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