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