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 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: $currenturl=~s/^http\:\/\///;
65: $currenturl=~s/^[^\/]+//;
66: # ------------------------------------------- Do we have any idea where we are?
67: my $position;
68: if ($position=Apache::lonnet::symbread($currenturl)) {
69: # ------------------------------------------------------------------------- Yes
70: my ($mapurl,$mapnum,$thisurl)=split(/\_\_\_/,$position);
71: $cachehash{$thisurl}=$mapnum;
72: # ============================================================ Tie the big hash
73: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
74: &GDBM_READER,0640)) {
75: my $rid=$hash{'map_pc_/res/'.$mapurl}.'.'.$mapnum;
76: my $next='';
77: my $mincond=1;
78: my $posnext='';
79: if ($direction eq 'forward') {
80: # --------------------------------------------------------------------- Forward
81: map {
82: my $thiscond=
83: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
84: if ($thiscond>=$mincond) {
85: if ($posnext) {
86: $posnext.=','.$_.':'.$thiscond;
87: } else {
88: $posnext=$_.':'.$thiscond;
89: }
90: if ($thiscond>$mincond) { $mincond=$thiscond; }
91: }
92: } split(/\,/,$hash{'to_'.$rid});
93: map {
94: my ($linkid,$condval)=split(/\:/,$_);
95: if ($condval>=$mincond) {
96: $next=&addrid($next,$hash{'goesto_'.$linkid},
97: $hash{'condid_'.$hash{'undercond_'.$linkid}});
98: }
99: } split(/\,/,$posnext);
100: } elsif ($direction eq 'back') {
101: # ------------------------------------------------------------------- Backwards
102: map {
103: my $thiscond=
104: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
105: if ($thiscond>=$mincond) {
106: if ($posnext) {
107: $posnext.=','.$_.':'.$thiscond;
108: } else {
109: $posnext=$_.':'.$thiscond;
110: }
111: if ($thiscond>$mincond) { $mincond=$thiscond; }
112: }
113: } split(/\,/,$hash{'from_'.$rid});
114: map {
115: my ($linkid,$condval)=split(/\:/,$_);
116: if ($condval>=$mincond) {
117: $next=&addrid($next,$hash{'comesfrom_'.$linkid},
118: $hash{'condid_'.$hash{'undercond_'.$linkid}});
119: }
120: } split(/\,/,$posnext);
121: } elsif ($direction eq 'up') {
122: # -------------------------------------------------------------------------- Up
123: } elsif ($direction eq 'down') {
124: # ------------------------------------------------------------------------ Down
125: }
126: # ----------------------------------------------------- Check out possibilities
127: if ($next) {
128: @possibilities=split(/\,/,$next);
129: if ($#possibilities==0) {
130: # ---------------------------------------------- Only one possibility, redirect
131: $redirecturl=$hash{'src_'.$next};
132: $cachehash{&Apache::lonnet::declutter($redirecturl)}
133: =(split(/\./,$next))[1];
134: } else {
135: # ------------------------ There are multiple possibilities for a next resource
136: $multichoice=1;
137: map {
138: $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
139: $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
140: $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
141: $cachehash
142: {&Apache::lonnet::declutter(
143: $multichoicehash
144: {'src_'.$_}
145: )}
146: =(split(/\./,$_))[1];
147: } @possibilities;
148: }
149: } else {
150: # -------------------------------------------------------------- No place to go
151: $multichoice=-1;
152: }
153: # ----------------- The program must come past this point to untie the big hash
154: untie(%hash);
155: # --------------------------------------------------------- Store position info
156: $cachehash{'last_direction'}=$direction;
157: &Apache::lonnet::symblist($mapurl,%cachehash);
158: # ============================================== Do not return before this line
159: if ($redirecturl) {
160: # ----------------------------------------------------- There is a URL to go to
161: $r->content_type('text/html');
162: $r->header_out(Location =>
163: 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
164: return REDIRECT;
165: } else {
166: # --------------------------------------------------------- There was a problem
167: }
168: } else {
169: # ------------------------------------------------- Problem, could not tie hash
170: $ENV{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
171: return HTTP_NOT_ACCEPTABLE;
172: }
173: } else {
174: # ---------------------------------------- No, could not determine where we are
175: $r->internal_redirect('/adm/ambiguous');
176: }
177: } else {
178: # -------------------------- Class was not initialized or page fliped strangely
179: $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
180: return HTTP_NOT_ACCEPTABLE;
181: }
182: }
183:
184: 1;
185: __END__
186:
187:
188:
189:
190:
191:
192:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>