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