1: # The LearningOnline Network with CAPA
2: #
3: # Page flip handler
4: #
5: # $Id: lonpageflip.pm,v 1.81 2013/05/10 12:49:39 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 first_accessible_resource {
221: my $furl;
222: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
223: &GDBM_READER(),0640)) {
224: $furl=$hash{'first_url'};
225: my %args;
226: my ($url,$args) = split(/\?/,$furl);
227: foreach my $pair (split(/\&/,$args)) {
228: my ($name,$value) = split(/=/,$pair);
229: $args{&unescape($name)} = &unescape($value);
230: }
231: if (!&Apache::lonnet::allowed('bre',$url,$args{'symb'})) {
232: # Wow, we cannot see this ... move forward to the next one that we can see
233: my ($newrid,$newmap)=&move($hash{'first_rid'},$hash{'first_mapurl'},'forward');
234: # Build the new URL
235: my ($newmapid,$newresid)=split(/\./,$newrid);
236: my $symb=&Apache::lonnet::encode_symb($newmap,$newresid,$hash{'src_'.$newrid});
237: $furl=&add_get_param($hash{'src_'.$newrid},{ 'symb' => $symb });
238: if ($hash{'encrypted_'.$newrid}) {
239: $furl=&Apache::lonenc::encrypted($furl);
240: }
241: }
242: untie(%hash);
243: return $furl;
244: } else {
245: return '/adm/navmaps';
246: }
247: }
248:
249: # ================================================================ Main Handler
250:
251: sub handler {
252: my $r=shift;
253:
254: # ------------------------------------------- Set document type for header only
255:
256: if ($r->header_only) {
257: &Apache::loncommon::content_type($r,'text/html');
258: $r->send_http_header;
259: return OK;
260: }
261:
262: my %cachehash=();
263: my $multichoice=0;
264: my %multichoicehash=();
265: my ($redirecturl,$redirectsymb);
266: my $next='';
267: my @possibilities=();
268: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
269: if (($env{'form.postdata'})&&($env{'request.course.fn'})) {
270: $env{'form.postdata'}=~/(\w+)\:(.*)/;
271: my $direction=$1;
272: my $currenturl=$2;
273: if ($currenturl=~m|^/enc/|) {
274: $currenturl=&Apache::lonenc::unencrypted($currenturl);
275: }
276: $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
277: if ($direction eq 'firstres') {
278: my $furl=&first_accessible_resource();
279: &Apache::loncommon::content_type($r,'text/html');
280: $r->header_out(Location =>
281: &Apache::lonnet::absolute_url().$furl);
282:
283: return REDIRECT;
284: }
285: if ($direction eq 'return') {
286: # -------------------------------------------------------- Return to last known
287: my $last;
288: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
289: &GDBM_READER(),0640)) {
290: $last=$hash{'last_known'};
291: untie(%hash);
292: }
293: my $newloc;
294: if (($last) && (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
295: &GDBM_READER(),0640))) {
296: my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
297: $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
298: $newloc=$hash{'src_'.$id};
299: if ($newloc) {
300: if ($hash{'encrypted_'.$id}) { $newloc=&Apache::lonenc::encrypted($newloc); }
301:
302: } else {
303: $newloc='/adm/navmaps';
304: }
305: untie %hash;
306: } else {
307: $newloc='/adm/navmaps';
308: }
309: &Apache::loncommon::content_type($r,'text/html');
310: $r->header_out(Location =>
311: &Apache::lonnet::absolute_url().$newloc);
312:
313: return REDIRECT;
314: }
315: $currenturl=~s/^https?\:\/\///;
316: $currenturl=~s/^[^\/]+//;
317: #
318: # Is the current URL on the map? If not, start with last known URL
319: #
320: unless (&Apache::lonnet::is_on_map($currenturl)) {
321: my $last;
322: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
323: &GDBM_READER(),0640)) {
324: $last=$hash{'last_known'};
325: untie(%hash);
326: }
327: if ($last) {
328: $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[2]);
329: } else {
330: &Apache::loncommon::content_type($r,'text/html');
331: $r->header_out(Location =>
332: &Apache::lonnet::absolute_url().
333: '/adm/noidea.html');
334: return REDIRECT;
335: }
336: }
337: # ------------------------------------------- Do we have any idea where we are?
338: my $position;
339: if ($position=Apache::lonnet::symbread($currenturl)) {
340: # ------------------------------------------------------------------------- Yes
341: my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
342: $cachehash{$startoutmap}{$thisurl}=[$thisurl,$mapnum];
343: $cachehash{$startoutmap}{'last_known'}=
344: [&Apache::lonnet::declutter($currenturl),$mapnum];
345:
346: # ============================================================ Tie the big hash
347: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
348: &GDBM_READER(),0640)) {
349: my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
350: '.'.$mapnum;
351:
352: # ------------------------------------------------- Move forward, backward, etc
353: my $endupmap;
354: ($next,$endupmap)=&move($rid,$startoutmap,$direction);
355: # -------------------------------------- Do we have one and only one empty URL?
356: # We are now at at least one non-empty URL
357: # ----------------------------------------------------- Check out possibilities
358: if ($next) {
359: @possibilities=split(/\,/,$next);
360: if ($#possibilities==0) {
361: # ---------------------------------------------- Only one possibility, redirect
362: ($redirecturl,$redirectsymb)=&hash_src($next);
363: $cachehash{$endupmap}{$redirecturl}=
364: [$redirecturl,(split(/\./,$next))[1]];
365: } else {
366: # ------------------------ There are multiple possibilities for a next resource
367: $multichoice=1;
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} =
375: Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
376: $second.'___'.$symbSrc);
377:
378: my ($choicemap,$choiceres)=split(/\./,$id);
379: my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
380: my $url=$multichoicehash{'src_'.$id};
381: $cachehash{$map}{$url}=[$url,$choiceres];
382: }
383: }
384: } else {
385: # -------------------------------------------------------------- No place to go
386: $multichoice=-1;
387: }
388: # ----------------- The program must come past this point to untie the big hash
389: untie(%hash);
390: # --------------------------------------------------------- Store position info
391: $cachehash{$startoutmap}{'last_direction'}=[$direction,'notasymb'];
392: foreach my $thismap (keys %cachehash) {
393: my $mapnum=$cachehash{$thismap}->{'mapnum'};
394: delete($cachehash{$thismap}->{'mapnum'});
395: &Apache::lonnet::symblist($thismap,
396: %{$cachehash{$thismap}});
397: }
398: # ============================================== Do not return before this line
399: if ($redirecturl) {
400: # ----------------------------------------------------- There is a URL to go to
401: if ($direction eq 'forward') {
402: &Apache::lonnet::linklog($currenturl,$redirecturl);
403: }
404: if ($direction eq 'back') {
405: &Apache::lonnet::linklog($redirecturl,$currenturl);
406: }
407: # ------------------------------------------------- Check for critical messages
408: if ((time-$env{'user.criticalcheck.time'})>300) {
409: my @what=&Apache::lonnet::dump
410: ('critical',$env{'user.domain'},
411: $env{'user.name'});
412: if ($what[0]) {
413: if (($what[0] ne 'con_lost') &&
414: ($what[0]!~/^error\:/)) {
415: $redirecturl='/adm/email?critical=display';
416: $redirectsymb='';
417: }
418: }
419: &Apache::lonnet::appenv({'user.criticalcheck.time'=>time});
420: }
421:
422: &Apache::loncommon::content_type($r,'text/html');
423: my $url=&Apache::lonnet::absolute_url().$redirecturl;
424: $url = &add_get_param($url, { 'symb' => $redirectsymb});
425: $r->header_out(Location => $url);
426: return REDIRECT;
427: } else {
428: # --------------------------------------------------------- There was a problem
429: &Apache::loncommon::content_type($r,'text/html');
430: $r->send_http_header;
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');
442: if ($#possibilities>0) {
443: my $start_page=
444: &Apache::loncommon::start_page('Multiple Resources');
445: $r->print(<<ENDSTART);
446: $start_page
447: <h3>$lt{'wherenext'}</h3>
448: <p>
449: $lt{'pick'}:
450: <p>
451: <table border="2">
452: <tr><th>$lt{'titleheader'}</th><th>$lt{'type'}</th></tr>
453: ENDSTART
454: foreach my $id (@possibilities) {
455: $r->print(
456: '<tr><td><a href="'.
457: &add_get_param($multichoicehash{'src_'.$id},
458: {'symb' =>
459: $multichoicehash{'symb_'.$id},
460: }).'">'.
461: $multichoicehash{'title_'.$id}.
462: '</a></td><td>'.$multichoicehash{'type_'.$id}.
463: '</td></tr>');
464: }
465: $r->print('</table>');
466: } else {
467: my $start_page=
468: &Apache::loncommon::start_page('No Resource');
469: $r->print(<<ENDNONE);
470: $start_page
471: <h3>$lt{'title'}</h3>
472: <p>$lt{'explain'}</p>
473: ENDNONE
474: }
475: $r->print(<<ENDMENU);
476: <ul>
477: <li><a href="/adm/flip?postdata=return:">$lt{'back'}</a></li>
478: <li><a href="/adm/navmaps">$lt{'nav'}</a></li>
479: </ul>
480: ENDMENU
481: $r->print(&Apache::loncommon::end_page());
482: return OK;
483: }
484: } else {
485: # ------------------------------------------------- Problem, could not tie hash
486: $env{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
487: return HTTP_NOT_ACCEPTABLE;
488: }
489: } else {
490: # ---------------------------------------- No, could not determine where we are
491: $r->internal_redirect('/adm/ambiguous');
492: return OK;
493: }
494: } else {
495: # -------------------------- Class was not initialized or page fliped strangely
496: $env{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
497: return HTTP_NOT_ACCEPTABLE;
498: }
499: }
500:
501: 1;
502: __END__
503:
504: =pod
505:
506: =head1 NAME
507:
508: Apache::lonpageflip
509:
510: =head1 SYNOPSIS
511:
512: Deals with forward, backward, and other page flips.
513:
514: This is part of the LearningOnline Network with CAPA project
515: described at http://www.lon-capa.org.
516:
517: =head1 OVERVIEW
518:
519: (empty)
520:
521: =head1 SUBROUTINES
522:
523: =over cleanup()
524:
525: =item addrid()
526:
527: =item fullmove()
528:
529: =item hash_src()
530:
531: =item move()
532:
533: =item get_next_possible_move()
534:
535: =item first_accessible_resource()
536:
537: =item handler()
538:
539: =back
540:
541: =cut
542:
543:
544:
545:
546:
547:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>