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