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