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