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