File:
[LON-CAPA] /
rat /
lonpageflip.pm
Revision
1.46:
download - view:
text,
annotated -
select for diffs
Wed Oct 29 22:09:17 2003 UTC (20 years, 10 months ago) by
www
Branches:
MAIN
CVS tags:
version_1_1_X,
version_1_1_3,
version_1_1_2,
version_1_1_1,
version_1_1_0,
version_1_0_99_3,
version_1_0_99_2,
version_1_0_99_1,
version_1_0_99,
HEAD
Version number is not part of anything in bighash except src.
1: # The LearningOnline Network with CAPA
2: #
3: # Page flip handler
4: #
5: # $Id: lonpageflip.pm,v 1.46 2003/10/29 22:09:17 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 cleanup {
54: if (tied(%hash)){
55: &Apache::lonnet::logthis('Cleanup pageflip: hash');
56: unless (untie(%hash)) {
57: &Apache::lonnet::logthis('Failed cleanup pageflip: hash');
58: }
59: }
60: }
61:
62: sub addrid {
63: my ($current,$new,$condid)=@_;
64: unless ($condid) { $condid=0; }
65:
66: if ($current) {
67: $current.=','.$new;
68: } else {
69: $current=''.$new;
70: }
71:
72: return $current;
73: }
74:
75: sub fullmove {
76: my ($rid,$mapurl,$direction)=@_;
77: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
78: &GDBM_READER(),0640)) {
79: ($rid,$mapurl)=&move($rid,$mapurl,$direction);
80: untie(%hash);
81: }
82: return($rid,$mapurl);
83: }
84:
85: sub move {
86: my ($rid,$mapurl,$direction)=@_;
87: my $startoutrid=$rid;
88:
89: my $next='';
90:
91: my $mincond=1;
92: my $posnext='';
93: if ($direction eq 'forward') {
94: # --------------------------------------------------------------------- Forward
95: while ($hash{'type_'.$rid} eq 'finish') {
96: $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
97: }
98: map {
99: my $thiscond=
100: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
101: if ($thiscond>=$mincond) {
102: if ($posnext) {
103: $posnext.=','.$_.':'.$thiscond;
104: } else {
105: $posnext=$_.':'.$thiscond;
106: }
107: if ($thiscond>$mincond) { $mincond=$thiscond; }
108: }
109: } split(/\,/,$hash{'to_'.$rid});
110: map {
111: my ($linkid,$condval)=split(/\:/,$_);
112: if ($condval>=$mincond) {
113: $next=&addrid($next,$hash{'goesto_'.$linkid},
114: $hash{'condid_'.$hash{'undercond_'.$linkid}});
115: }
116: } split(/\,/,$posnext);
117: if ($hash{'is_map_'.$next}) {
118: # This jumps to the beginning of a new map (going down level)
119: if (
120: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
121: $mapurl=$hash{'src_'.$next};
122: $next=$hash{'map_start_'.$hash{'src_'.$next}};
123: }
124: } elsif
125: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
126: # This comes up from a map (coming up one level);
127: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
128: }
129: } elsif ($direction eq 'back') {
130: # ------------------------------------------------------------------- Backwards
131: while ($hash{'type_'.$rid} eq 'start') {
132: $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
133: }
134: map {
135: my $thiscond=
136: &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$_}});
137: if ($thiscond>=$mincond) {
138: if ($posnext) {
139: $posnext.=','.$_.':'.$thiscond;
140: } else {
141: $posnext=$_.':'.$thiscond;
142: }
143: if ($thiscond>$mincond) { $mincond=$thiscond; }
144: }
145: } split(/\,/,$hash{'from_'.$rid});
146: map {
147: my ($linkid,$condval)=split(/\:/,$_);
148: if ($condval>=$mincond) {
149: $next=&addrid($next,$hash{'comesfrom_'.$linkid},
150: $hash{'condid_'.$hash{'undercond_'.$linkid}});
151: }
152: } split(/\,/,$posnext);
153: if ($hash{'is_map_'.$next}) {
154: # This jumps to the end of a new map (going down one level)
155: if (
156: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
157: $mapurl=$hash{'src_'.$next};
158: $next=$hash{'map_finish_'.$hash{'src_'.$next}};
159: }
160: } elsif
161: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
162: # This comes back up from a map (going up one level);
163: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
164: }
165: }
166: return ($next,$mapurl);
167: }
168:
169: # ================================================================ Main Handler
170:
171: sub handler {
172: my $r=shift;
173:
174: # ------------------------------------------- Set document type for header only
175:
176: if ($r->header_only) {
177: $r->content_type('text/html');
178: $r->send_http_header;
179: return OK;
180: }
181:
182: my %cachehash=();
183: my $multichoice=0;
184: my %multichoicehash=();
185: my $redirecturl='';
186: my $next='';
187: my @possibilities=();
188: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
189: if (($ENV{'form.postdata'})&&($ENV{'request.course.fn'})) {
190: $ENV{'form.postdata'}=~/(\w+)\:(.*)/;
191: my $direction=$1;
192: my $currenturl=$2;
193: $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
194: if ($direction eq 'return') {
195: # -------------------------------------------------------- Return to last known
196: my $last;
197: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
198: &GDBM_READER(),0640)) {
199: $last=$hash{'last_known'};
200: untie(%hash);
201: }
202: my $newloc;
203: if (($last) && (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
204: &GDBM_READER(),0640))) {
205: my ($murl,$fn)=&Apache::lonnet::decode_symb($last);
206: my $id;
207: ($murl,$id,$fn)=&Apache::lonnet::decode_symb(&Apache::lonnet::symbread($fn));
208: $newloc=$hash{'src_'.
209: $hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id};
210: unless ($newloc) { $newloc='/adm/noidea.html'; }
211: untie %hash;
212: } else {
213: $newloc='/adm/noidea.html';
214: }
215: $r->content_type('text/html');
216: $r->header_out(Location =>
217: 'http://'.$ENV{'HTTP_HOST'}.$newloc);
218:
219: return REDIRECT;
220: }
221: $currenturl=~s/^http\:\/\///;
222: $currenturl=~s/^[^\/]+//;
223: #
224: # Is the current URL on the map? If not, start with last known URL
225: #
226: unless (&Apache::lonnet::is_on_map($currenturl)) {
227: my $last;
228: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
229: &GDBM_READER(),0640)) {
230: $last=$hash{'last_known'};
231: untie(%hash);
232: }
233: if ($last) {
234: $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[1]);
235: } else {
236: $r->content_type('text/html');
237: $r->header_out(Location =>
238: 'http://'.$ENV{'HTTP_HOST'}.'/adm/noidea.html');
239: return REDIRECT;
240: }
241: }
242: # ------------------------------------------- Do we have any idea where we are?
243: my $position;
244: if ($position=Apache::lonnet::symbread($currenturl)) {
245: # ------------------------------------------------------------------------- Yes
246: my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
247: $cachehash{$startoutmap}{$thisurl}=$mapnum;
248: $cachehash{$startoutmap}{'last_known'}=
249: &Apache::lonnet::declutter($currenturl);
250:
251: # ============================================================ Tie the big hash
252: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
253: &GDBM_READER(),0640)) {
254: my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
255: '.'.$mapnum;
256:
257: # ------------------------------------------------- Move forward, backward, etc
258: my $endupmap;
259: ($next,$endupmap)=&move($rid,$startoutmap,$direction);
260: # -------------------------------------- Do we have one and only one empty URL?
261: my $safecount=0;
262: while (($next) && ($next!~/\,/) &&
263: ((!$hash{'src_'.$next}) || ($hash{'randomout_'.$next}))
264: && ($safecount<10000)) {
265: ($next,$endupmap)=&move($next,$endupmap,$direction);
266: $safecount++;
267: }
268: # We are now at at least one non-empty URL
269: # ----------------------------------------------------- Check out possibilities
270: if ($next) {
271: @possibilities=split(/\,/,$next);
272: if ($#possibilities==0) {
273: # ---------------------------------------------- Only one possibility, redirect
274: $redirecturl=$hash{'src_'.$next};
275: $cachehash{$endupmap}
276: {&Apache::lonnet::declutter($redirecturl)}
277: =(split(/\./,$next))[1];
278: } else {
279: # ------------------------ There are multiple possibilities for a next resource
280: $multichoice=1;
281: map {
282: $multichoicehash{'src_'.$_}=$hash{'src_'.$_};
283: $multichoicehash{'title_'.$_}=$hash{'title_'.$_};
284: $multichoicehash{'type_'.$_}=$hash{'type_'.$_};
285: (my $first, my $second) = $_ =~ /(\d+).(\d+)/;
286: my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$_});
287: $multichoicehash{'symb_'.$_} =
288: Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
289: $second.'___'.$symbSrc);
290:
291: my ($choicemap,$choiceres)=split(/\./,$_);
292: $cachehash
293: {&Apache::lonnet::declutter($hash{'src_'.$choicemap})}
294: {&Apache::lonnet::declutter(
295: $multichoicehash
296: {'src_'.$_}
297: )}
298: =$choiceres;
299: } @possibilities;
300: }
301: } else {
302: # -------------------------------------------------------------- No place to go
303: $multichoice=-1;
304: }
305: # ----------------- The program must come past this point to untie the big hash
306: untie(%hash);
307: # --------------------------------------------------------- Store position info
308: $cachehash{$startoutmap}{'last_direction'}=$direction;
309: foreach my $thismap (keys %cachehash) {
310: &Apache::lonnet::symblist($thismap,%{$cachehash{$thismap}});
311: }
312: # ============================================== Do not return before this line
313: if ($redirecturl) {
314: # ----------------------------------------------------- There is a URL to go to
315: if ($direction eq 'forward') {
316: &Apache::lonnet::linklog($currenturl,$redirecturl);
317: }
318: if ($direction eq 'back') {
319: &Apache::lonnet::linklog($redirecturl,$currenturl);
320: }
321: # ------------------------------------------------- Check for critical messages
322: if ((time-$ENV{'user.criticalcheck.time'})>300) {
323: my @what=&Apache::lonnet::dump
324: ('critical',$ENV{'user.domain'},
325: $ENV{'user.name'});
326: if ($what[0]) {
327: if (($what[0] ne 'con_lost') &&
328: ($what[0]!~/^error\:/)) {
329: $redirecturl='/adm/email?critical=display';
330: }
331: }
332: &Apache::lonnet::appenv('user.criticalcheck.time'=>time);
333: }
334:
335: $r->content_type('text/html');
336: $r->header_out(Location =>
337: 'http://'.$ENV{'HTTP_HOST'}.$redirecturl);
338: return REDIRECT;
339: } else {
340: # --------------------------------------------------------- There was a problem
341: $r->content_type('text/html');
342: $r->send_http_header;
343: if ($#possibilities>0) {
344: my $bodytag=
345: &Apache::loncommon::bodytag('Multiple Resources');
346: $r->print(<<ENDSTART);
347: <head><title>Choose Next Location</title></head>
348: $bodytag
349: <h3>There are several possibilities of where to go next</h3>
350: <p>
351: Please click on the the resource you intend to access:
352: <p>
353: <table border=2>
354: <tr><th>Title</th><th>Type</th></tr>
355: ENDSTART
356: foreach (@possibilities) {
357: $r->print(
358: '<tr><td><a href="'.
359: $multichoicehash{'src_'.$_}.'?symb=' .
360: Apache::lonnet::escape($multichoicehash{'symb_'.$_}).'">'.
361: $multichoicehash{'title_'.$_}.
362: '</a></td><td>'.$multichoicehash{'type_'.$_}.
363: '</td></tr>');
364: }
365: $r->print('</table></body></html>');
366: return OK;
367: } else {
368: my $bodytag=&Apache::loncommon::bodytag('No Resource');
369: $r->print(<<ENDNONE);
370: <head><title>No Resource</title></head>
371: $bodytag
372: <h3>Next resource could not be identified.</h3>
373: <p>You probably are at the <b>beginning</b> or the <b>end</b> of the
374: course.</p>
375: <ul>
376: <li><a href="/adm/flip?postdata=return:">Go Back</a></li>
377: <li><a href="/adm/navmaps">Navigate Course Content</a></li>
378: </ul>
379: </body>
380: </html>
381: ENDNONE
382: return OK;
383: }
384: }
385: } else {
386: # ------------------------------------------------- Problem, could not tie hash
387: $ENV{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
388: return HTTP_NOT_ACCEPTABLE;
389: }
390: } else {
391: # ---------------------------------------- No, could not determine where we are
392: if ( &Apache::lonnet::mod_perl_version() == 2 ) {
393: &Apache::lonnet::cleanenv();
394: }
395: $r->internal_redirect('/adm/ambiguous');
396: }
397: } else {
398: # -------------------------- Class was not initialized or page fliped strangely
399: $ENV{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
400: return HTTP_NOT_ACCEPTABLE;
401: }
402: }
403:
404: 1;
405: __END__
406:
407:
408:
409:
410:
411:
412:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>