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