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