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