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