1: # The LearningOnline Network with CAPA
2: #
3: # Page flip handler
4: #
5: # $Id: lonpageflip.pm,v 1.24 2002/04/12 14:24:58 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: # (Page Handler
30: #
31: # (TeX Content Handler
32: #
33: # 05/29/00,05/30 Gerd Kortemeyer)
34: # 08/30,08/31,09/06,09/14,09/15,09/16,09/19,09/20,09/21,09/23,
35: # 10/02 Gerd Kortemeyer)
36: #
37: # 10/03,10/05,10/06,10/07,10/09,10/10,10/11,10/16,10/17,
38: # 11/14,11/16,
39: # 10/01/01,05/01,05/28,07/05 Gerd Kortemeyer
40:
41: package Apache::lonpageflip;
42:
43: use strict;
44: use Apache::Constants qw(:common :http REDIRECT);
45: use Apache::lonnet();
46: use HTML::TokeParser;
47: use GDBM_File;
48:
49: # ========================================================== Module Global Hash
50:
51: my %hash;
52:
53: sub addrid {
54: my ($current,$new,$condid)=@_;
55: unless ($condid) { $condid=0; }
56: if (&Apache::lonnet::allowed('bre',$hash{'src_'.$new})) {
57: if ($current) {
58: $current.=','.$new;
59: } else {
60: $current=''.$new;
61: }
62: }
63: return $current;
64: }
65:
66: sub move {
67: my ($rid,$mapurl,$direction)=@_;
68: my $startoutrid=$rid;
69:
70: my $next='';
71:
72: my $mincond=1;
73: my $posnext='';
74: if ($direction eq 'forward') {
75: # --------------------------------------------------------------------- Forward
76: if ($hash{'type_'.$rid} eq 'finish') {
77: $rid=$hash{'ids_/res/'.$mapurl};
78: }
79: map {
80: my $thiscond=
81: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
82: if ($thiscond>=$mincond) {
83: if ($posnext) {
84: $posnext.=','.$_.':'.$thiscond;
85: } else {
86: $posnext=$_.':'.$thiscond;
87: }
88: if ($thiscond>$mincond) { $mincond=$thiscond; }
89: }
90: } split(/\,/,$hash{'to_'.$rid});
91: map {
92: my ($linkid,$condval)=split(/\:/,$_);
93: if ($condval>=$mincond) {
94: $next=&addrid($next,$hash{'goesto_'.$linkid},
95: $hash{'condid_'.$hash{'undercond_'.$linkid}});
96: }
97: } split(/\,/,$posnext);
98: if ($hash{'is_map_'.$next}) {
99: # This jumps to the beginning of a new map (going down level)
100: if (
101: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
102: $mapurl=$hash{'src_'.$next};
103: $next=$hash{'map_start_'.$hash{'src_'.$next}};
104: }
105: } elsif
106: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
107: # This comes up from a map (coming up one level);
108: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
109: }
110: } elsif ($direction eq 'back') {
111: # ------------------------------------------------------------------- Backwards
112: if ($hash{'type_'.$rid} eq 'start') {
113: $rid=$hash{'ids_/res/'.$mapurl};
114: }
115: map {
116: my $thiscond=
117: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
118: if ($thiscond>=$mincond) {
119: if ($posnext) {
120: $posnext.=','.$_.':'.$thiscond;
121: } else {
122: $posnext=$_.':'.$thiscond;
123: }
124: if ($thiscond>$mincond) { $mincond=$thiscond; }
125: }
126: } split(/\,/,$hash{'from_'.$rid});
127: map {
128: my ($linkid,$condval)=split(/\:/,$_);
129: if ($condval>=$mincond) {
130: $next=&addrid($next,$hash{'comesfrom_'.$linkid},
131: $hash{'condid_'.$hash{'undercond_'.$linkid}});
132: }
133: } split(/\,/,$posnext);
134: if ($hash{'is_map_'.$next}) {
135: # This jumps to the end of a new map (going down one level)
136: if (
137: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
138: $mapurl=$hash{'src_'.$next};
139: $next=$hash{'map_finish_'.$hash{'src_'.$next}};
140: }
141: } elsif
142: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
143: # This comes back up from a map (going up one level);
144: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
145: }
146: }
147: return ($next,$mapurl);
148: }
149:
150: # ================================================================ Main Handler
151:
152: sub handler {
153: my $r=shift;
154:
155: # ------------------------------------------- Set document type for header only
156:
157: if ($r->header_only) {
158: $r->content_type('text/html');
159: $r->send_http_header;
160: return OK;
161: }
162:
163: my %cachehash=();
164: my $multichoice=0;
165: my %multichoicehash=();
166: my $redirecturl='';
167: my $next='';
168: my @possibilities=();
169: if (($ENV{'form.postdata'})&&($ENV{'request.course.fn'})) {
170: $ENV{'form.postdata'}=~/(\w+)\:(.*)/;
171: my $direction=$1;
172: my $currenturl=$2;
173: if ($direction eq 'return') {
174: # -------------------------------------------------------- Return to last known
175: my $last;
176: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
177: &GDBM_READER,0640)) {
178: $last=$hash{'last_known'};
179: untie(%hash);
180: }
181: my $newloc;
182: if ($last) {
183: $newloc='/res/'.(split(/\_\_\_/,$last))[1];
184: } else {
185: $newloc='/adm/noidea.html';
186: }
187: $r->content_type('text/html');
188: $r->header_out(Location =>
189: 'http://'.$ENV{'HTTP_HOST'}.$newloc);
190:
191: return REDIRECT;
192: }
193: $currenturl=~s/^http\:\/\///;
194: $currenturl=~s/^[^\/]+//;
195: unless (($currenturl=~/^\/res\//) ||
196: ($currenturl=~/^\/adm\/wrapper\//)) {
197: my $last;
198: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
199: &GDBM_READER,0640)) {
200: $last=$hash{'last_known'};
201: untie(%hash);
202: }
203: if ($last) {
204: $currenturl='/res/'.(split(/\_\_\_/,$last))[1];
205: } else {
206: $r->content_type('text/html');
207: $r->header_out(Location =>
208: 'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
209: return REDIRECT;
210: }
211: }
212: # ------------------------------------------- Do we have any idea where we are?
213: my $position;
214: if ($position=Apache::lonnet::symbread($currenturl)) {
215: # ------------------------------------------------------------------------- Yes
216: my ($startoutmap,$mapnum,$thisurl)=split(/\_\_\_/,$position);
217: $cachehash{$startoutmap}{$thisurl}=$mapnum;
218: $cachehash{$startoutmap}{'last_known'}=
219: &Apache::lonnet::declutter($currenturl);
220:
221: # ============================================================ Tie the big hash
222: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
223: &GDBM_READER,0640)) {
224: my $rid=$hash{'map_pc_/res/'.$startoutmap}.'.'.$mapnum;
225:
226: # ------------------------------------------------- Move forward, backward, etc
227: my $endupmap;
228: ($next,$endupmap)=&move($rid,$startoutmap,$direction);
229: # -------------------------------------- Do we have one and only one empty URL?
230: my $safecount=0;
231: while (($next) && ($next!~/\,/) && (!$hash{'src_'.$next})
232: && ($safecount<25)) {
233: ($next,$endupmap)=&move($next,$endupmap,$direction);
234: $safecount++;
235: }
236: # We are now at at least one non-empty URL
237: # ----------------------------------------------------- Check out possibilities
238: if ($next) {
239: @possibilities=split(/\,/,$next);
240: if ($#possibilities==0) {
241: # ---------------------------------------------- Only one possibility, redirect
242: $redirecturl=$hash{'src_'.$next};
243: $cachehash{$endupmap}
244: {&Apache::lonnet::declutter($redirecturl)}
245: =(split(/\./,$next))[1];
246: } else {
247: # ------------------------ There are multiple possibilities for a next resource
248: $multichoice=1;
249: map {
250: $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
251: $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
252: $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
253: my ($choicemap,$choiceres)=split(/\./,$_);
254: $cachehash
255: {&Apache::lonnet::declutter($hash{'src_'.$choicemap})}
256: {&Apache::lonnet::declutter(
257: $multichoicehash
258: {'src_'.$_}
259: )}
260: =$choiceres;
261: } @possibilities;
262: }
263: } else {
264: # -------------------------------------------------------------- No place to go
265: $multichoice=-1;
266: }
267: # ----------------- The program must come past this point to untie the big hash
268: untie(%hash);
269: # --------------------------------------------------------- Store position info
270: $cachehash{$startoutmap}{'last_direction'}=$direction;
271: foreach my $thismap (keys %cachehash) {
272: &Apache::lonnet::symblist($thismap,%{$cachehash{$thismap}});
273: }
274: # ============================================== Do not return before this line
275: if ($redirecturl) {
276: # ----------------------------------------------------- There is a URL to go to
277: $r->content_type('text/html');
278: $r->header_out(Location =>
279: 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
280: return REDIRECT;
281: } else {
282: # --------------------------------------------------------- There was a problem
283: $r->content_type('text/html');
284: $r->send_http_header;
285: if ($#possibilities>0) {
286: $r->print(<<ENDSTART);
287: <head><title>Choose Next Location</title></head>
288: <body bgcolor="#FFFFFF">
289: <h1>LON-CAPA</h1>
290: There are several possibilities of where to go next.
291: <p>
292: Please click on the the resource you intend to access:
293: <p>
294: <table border=2>
295: <tr><th>Title</th><th>Type</th></tr>
296: ENDSTART
297: map {
298: $r->print(
299: '<tr><td><a href="'.
300: $multichoicehash{'src_'.$_}.'">'.
301: $multichoicehash{'title_'.$_}.
302: '</a></td><td>'.$multichoicehash{'type_'.$_}.
303: '</td></tr>');
304: } @possibilities;
305: $r->print('</table></body></html>');
306: return OK;
307: } else {
308: $r->print(<<ENDNONE);
309: <head><title>Choose Next Location</title></head>
310: <body bgcolor="#FFFFFF">
311: <img src="/adm/lonIcons/lonlogos.gif" align=right>
312: <h1>Sorry!</h1>
313: <h2>Next resource could not be identified.</h2>
314: <h3>You probably are at the beginning or the end of the course.</h3>
315: </body>
316: </html>
317: ENDNONE
318: return OK;
319: }
320: }
321: } else {
322: # ------------------------------------------------- Problem, could not tie hash
323: $ENV{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
324: return HTTP_NOT_ACCEPTABLE;
325: }
326: } else {
327: # ---------------------------------------- No, could not determine where we are
328: $r->internal_redirect('/adm/ambiguous');
329: }
330: } else {
331: # -------------------------- Class was not initialized or page fliped strangely
332: $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
333: return HTTP_NOT_ACCEPTABLE;
334: }
335: }
336:
337: 1;
338: __END__
339:
340:
341:
342:
343:
344:
345:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>