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