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