1: # The LearningOnline Network with CAPA
2: #
3: # Page flip handler
4: #
5: # $Id: lonpageflip.pm,v 1.68 2006/05/08 22:14:15 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 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: # ================================================================ Main Handler
204:
205: sub handler {
206: my $r=shift;
207:
208: # ------------------------------------------- Set document type for header only
209:
210: if ($r->header_only) {
211: &Apache::loncommon::content_type($r,'text/html');
212: $r->send_http_header;
213: return OK;
214: }
215:
216: my %cachehash=();
217: my $multichoice=0;
218: my %multichoicehash=();
219: my ($redirecturl,$redirectsymb);
220: my $next='';
221: my @possibilities=();
222: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
223: if (($env{'form.postdata'})&&($env{'request.course.fn'})) {
224: $env{'form.postdata'}=~/(\w+)\:(.*)/;
225: my $direction=$1;
226: my $currenturl=$2;
227: if ($currenturl=~m|^/enc/|) {
228: $currenturl=&Apache::lonenc::unencrypted($currenturl);
229: }
230: $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
231: if ($direction eq 'firstres') {
232: my $furl;
233: if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
234: &GDBM_READER(),0640)) {
235: $furl=$bighash{'first_url'};
236: untie(%bighash);
237: }
238: &Apache::loncommon::content_type($r,'text/html');
239: $r->header_out(Location =>
240: 'http://'.$ENV{'HTTP_HOST'}.$furl);
241:
242: return REDIRECT;
243: }
244: if ($direction eq 'return' || $direction eq 'navlaunch') {
245: # -------------------------------------------------------- Return to last known
246: my $last;
247: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
248: &GDBM_READER(),0640)) {
249: $last=$hash{'last_known'};
250: untie(%hash);
251: }
252: my $newloc;
253: if (($last) && (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
254: &GDBM_READER(),0640))) {
255: my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
256: $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
257: $newloc=$hash{'src_'.$id};
258: if ($newloc) {
259: if ($hash{'encrypted_'.$id}) { $newloc=&Apache::lonenc::encrypted($newloc); }
260:
261: } else {
262: $newloc='/adm/navmaps';
263: }
264: untie %hash;
265: } else {
266: $newloc='/adm/navmaps';
267: }
268: if ($newloc eq '/adm/navmaps' && $direction eq 'navlaunch') {
269: &navlaunch($r);
270: return OK;
271: } else {
272: &Apache::loncommon::content_type($r,'text/html');
273: $r->header_out(Location =>
274: 'http://'.$ENV{'HTTP_HOST'}.$newloc);
275:
276: return REDIRECT;
277: }
278: }
279: $currenturl=~s/^http\:\/\///;
280: $currenturl=~s/^[^\/]+//;
281: #
282: # Is the current URL on the map? If not, start with last known URL
283: #
284: unless (&Apache::lonnet::is_on_map($currenturl)) {
285: my $last;
286: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
287: &GDBM_READER(),0640)) {
288: $last=$hash{'last_known'};
289: untie(%hash);
290: }
291: if ($last) {
292: $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[2]);
293: } else {
294: if ($direction eq 'return') {
295: &Apache::loncommon::content_type($r,'text/html');
296: $r->header_out(Location =>
297: 'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
298: return REDIRECT;
299: } else {
300: &navlaunch($r);
301: return OK;
302: }
303: }
304: }
305: # ------------------------------------------- Do we have any idea where we are?
306: my $position;
307: if ($position=Apache::lonnet::symbread($currenturl)) {
308: # ------------------------------------------------------------------------- Yes
309: my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
310: $cachehash{$startoutmap}{$thisurl}=[$thisurl,$mapnum];
311: $cachehash{$startoutmap}{'last_known'}=
312: [&Apache::lonnet::declutter($currenturl),$mapnum];
313:
314: # ============================================================ Tie the big hash
315: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
316: &GDBM_READER(),0640)) {
317: my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
318: '.'.$mapnum;
319:
320: # ------------------------------------------------- Move forward, backward, etc
321: my $endupmap;
322: ($next,$endupmap)=&move($rid,$startoutmap,$direction);
323: # -------------------------------------- Do we have one and only one empty URL?
324: my $safecount=0;
325: while (($next) && ($next!~/\,/) &&
326: ((!$hash{'src_'.$next}) ||
327: ((!$env{'request.role.adv'}) && $hash{'randomout_'.$next}))
328: && ($safecount<10000)) {
329: ($next,$endupmap)=&move($next,$endupmap,$direction);
330: $safecount++;
331: }
332: # We are now at at least one non-empty URL
333: # ----------------------------------------------------- Check out possibilities
334: if ($next) {
335: @possibilities=split(/\,/,$next);
336: if ($#possibilities==0) {
337: # ---------------------------------------------- Only one possibility, redirect
338: ($redirecturl,$redirectsymb)=&hash_src($next);
339: $cachehash{$endupmap}{$redirecturl}=
340: [$redirecturl,(split(/\./,$next))[1]];
341: } else {
342: # ------------------------ There are multiple possibilities for a next resource
343: $multichoice=1;
344: foreach my $id (@possibilities) {
345: $multichoicehash{'src_'.$id}=$hash{'src_'.$id};
346: $multichoicehash{'title_'.$id}=$hash{'title_'.$id};
347: $multichoicehash{'type_'.$id}=$hash{'type_'.$id};
348: (my $first, my $second) = $id =~ /(\d+).(\d+)/;
349: my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$id});
350: $multichoicehash{'symb_'.$id} =
351: Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
352: $second.'___'.$symbSrc);
353:
354: my ($choicemap,$choiceres)=split(/\./,$id);
355: my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
356: my $url=$multichoicehash{'src_'.$id};
357: $cachehash{$map}{$url}=[$url,$choiceres];
358: }
359: }
360: } else {
361: # -------------------------------------------------------------- No place to go
362: $multichoice=-1;
363: }
364: # ----------------- The program must come past this point to untie the big hash
365: untie(%hash);
366: # --------------------------------------------------------- Store position info
367: $cachehash{$startoutmap}{'last_direction'}=[$direction,'notasymb'];
368: foreach my $thismap (keys %cachehash) {
369: my $mapnum=$cachehash{$thismap}->{'mapnum'};
370: delete($cachehash{$thismap}->{'mapnum'});
371: &Apache::lonnet::symblist($thismap,
372: %{$cachehash{$thismap}});
373: }
374: # ============================================== Do not return before this line
375: if ($redirecturl) {
376: # ----------------------------------------------------- There is a URL to go to
377: if ($direction eq 'forward') {
378: &Apache::lonnet::linklog($currenturl,$redirecturl);
379: }
380: if ($direction eq 'back') {
381: &Apache::lonnet::linklog($redirecturl,$currenturl);
382: }
383: # ------------------------------------------------- Check for critical messages
384: if ((time-$env{'user.criticalcheck.time'})>300) {
385: my @what=&Apache::lonnet::dump
386: ('critical',$env{'user.domain'},
387: $env{'user.name'});
388: if ($what[0]) {
389: if (($what[0] ne 'con_lost') &&
390: ($what[0]!~/^error\:/)) {
391: $redirecturl='/adm/email?critical=display';
392: $redirectsymb='';
393: }
394: }
395: &Apache::lonnet::appenv('user.criticalcheck.time'=>time);
396: }
397:
398: &Apache::loncommon::content_type($r,'text/html');
399: my $url='http://'.$ENV{'HTTP_HOST'}.$redirecturl;
400: $url = &add_get_param($url, { 'symb' => $redirectsymb});
401: $r->header_out(Location => $url);
402: return REDIRECT;
403: } else {
404: # --------------------------------------------------------- There was a problem
405: &Apache::loncommon::content_type($r,'text/html');
406: $r->send_http_header;
407: my %lt=&Apache::lonlocal::texthash('title' => 'End of Sequence',
408: 'explain' =>
409: 'You have reached the end of the sequence of materials.',
410: 'back' => 'Go Back',
411: 'nav' => 'Navigate Course Content',
412: 'wherenext' =>
413: 'There are several possibilities of where to go next',
414: 'pick' =>
415: 'Please click on the the resource you intend to access',
416: 'titleheader' => 'Title',
417: 'type' => 'Type');
418: if ($#possibilities>0) {
419: my $start_page=
420: &Apache::loncommon::start_page('Multiple Resources');
421: $r->print(<<ENDSTART);
422: $start_page
423: <h3>$lt{'wherenext'}</h3>
424: <p>
425: $lt{'pick'}:
426: <p>
427: <table border=2>
428: <tr><th>$lt{'titleheader'}</th><th>$lt{'type'}</th></tr>
429: ENDSTART
430: foreach my $id (@possibilities) {
431: $r->print(
432: '<tr><td><a href="'.
433: &add_get_param($multichoicehash{'src_'.$id},
434: {'symb' =>
435: $multichoicehash{'symb_'.$id},
436: }).'">'.
437: $multichoicehash{'title_'.$id}.
438: '</a></td><td>'.$multichoicehash{'type_'.$id}.
439: '</td></tr>');
440: }
441: $r->print('</table>');
442: } else {
443: my $start_page=
444: &Apache::loncommon::start_page('No Resource');
445: $r->print(<<ENDNONE);
446: $start_page
447: <h3>$lt{'title'}</h3>
448: <p>$lt{'explain'}</p>
449: ENDNONE
450: }
451: $r->print(<<ENDMENU);
452: <ul>
453: <li><a href="/adm/flip?postdata=return:">$lt{'back'}</a></li>
454: <li><a href="/adm/navmaps">$lt{'nav'}</a></li>
455: </ul>
456: ENDMENU
457: $r->print(&Apache::loncommon::end_page());
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>