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