File:
[LON-CAPA] /
rat /
lonpageflip.pm
Revision
1.74:
download - view:
text,
annotated -
select for diffs
Fri Oct 20 22:04:16 2006 UTC (17 years, 11 months ago) by
albertel
Branches:
MAIN
CVS tags:
version_2_6_X,
version_2_6_3,
version_2_6_2,
version_2_6_1,
version_2_6_0,
version_2_5_X,
version_2_5_99_1,
version_2_5_99_0,
version_2_5_2,
version_2_5_1,
version_2_5_0,
version_2_4_X,
version_2_4_99_0,
version_2_4_2,
version_2_4_1,
version_2_4_0,
version_2_3_X,
version_2_3_99_0,
version_2_3_2,
version_2_3_1,
version_2_3_0,
version_2_2_99_1,
version_2_2_99_0,
HEAD
- furl has the symb arg appeneded to it
1: # The LearningOnline Network with CAPA
2: #
3: # Page flip handler
4: #
5: # $Id: lonpageflip.pm,v 1.74 2006/10/20 22:04:16 albertel 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:
30: package Apache::lonpageflip;
31:
32: use strict;
33: use LONCAPA;
34: use Apache::Constants qw(:common :http REDIRECT);
35: use Apache::lonnet;
36: use Apache::loncommon();
37: use HTML::TokeParser;
38: use GDBM_File;
39:
40: # ========================================================== Module Global Hash
41:
42: my %hash;
43:
44: sub cleanup {
45: if (tied(%hash)){
46: &Apache::lonnet::logthis('Cleanup pageflip: hash');
47: unless (untie(%hash)) {
48: &Apache::lonnet::logthis('Failed cleanup pageflip: hash');
49: }
50: }
51: return OK;
52: }
53:
54: sub addrid {
55: my ($current,$new,$condid)=@_;
56: unless ($condid) { $condid=0; }
57:
58: if ($current) {
59: $current.=','.$new;
60: } else {
61: $current=''.$new;
62: }
63:
64: return $current;
65: }
66:
67: sub fullmove {
68: my ($rid,$mapurl,$direction)=@_;
69: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
70: &GDBM_READER(),0640)) {
71: ($rid,$mapurl)=&move($rid,$mapurl,$direction);
72: untie(%hash);
73: }
74: return($rid,$mapurl);
75: }
76:
77: sub hash_src {
78: my ($id)=@_;
79: my ($mapid,$resid)=split(/\./,$id);
80: my $symb=&Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
81: $resid,$hash{'src_'.$id});
82: if ($hash{'encrypted_'.$id}) {
83: return (&Apache::lonenc::encrypted($hash{'src_'.$id}),
84: &Apache::lonenc::encrypted($symb));
85: }
86: return ($hash{'src_'.$id},$symb);
87: }
88:
89: sub move {
90: my ($next,$endupmap,$direction) = @_;
91: my $safecount=0;
92: my $allowed=0;
93: do {
94: ($next,$endupmap)=&get_next_possible_move($next,$endupmap,$direction);
95:
96: my $url = $hash{'src_'.$next};
97: my ($mapid,$resid)=split(/\./,$next);
98: my $symb = &Apache::lonnet::encode_symb($hash{'map_id_'.$mapid},
99: $resid,$url);
100: if ($url eq '' || $symb eq '') {
101: $allowed = 0;
102: } else {
103: my $priv = &Apache::lonnet::allowed('bre',$url,$symb);
104: $allowed = (($priv eq 'F') || ($priv eq '2'));
105: }
106: $safecount++;
107: } while ( ($next)
108: && ($next!~/\,/)
109: && (
110: (!$hash{'src_'.$next})
111: || (
112: (!$env{'request.role.adv'})
113: && $hash{'randomout_'.$next}
114: )
115: || (!$allowed)
116: )
117: && ($safecount<10000));
118:
119: return ($next,$endupmap);
120: }
121:
122: sub get_next_possible_move {
123: my ($rid,$mapurl,$direction)=@_;
124: my $startoutrid=$rid;
125:
126: my $next='';
127:
128: my $mincond=1;
129: my $posnext='';
130: if ($direction eq 'forward') {
131: # --------------------------------------------------------------------- Forward
132: while ($hash{'type_'.$rid} eq 'finish') {
133: $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
134: }
135: foreach my $id (split(/\,/,$hash{'to_'.$rid})) {
136: my $condition= $hash{'conditions_'.$hash{'goesto_'.$id}};
137: my $rescond = &Apache::lonnet::docondval($condition);
138: my $linkcond = &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$id}});
139: my $thiscond = ($rescond<$linkcond)?$rescond:$linkcond;
140: if ($thiscond>=$mincond) {
141: if ($posnext) {
142: $posnext.=','.$id.':'.$thiscond;
143: } else {
144: $posnext=$id.':'.$thiscond;
145: }
146: if ($thiscond>$mincond) { $mincond=$thiscond; }
147: }
148: }
149: foreach my $id (split(/\,/,$posnext)) {
150: my ($linkid,$condval)=split(/\:/,$id);
151: if ($condval>=$mincond) {
152: $next=&addrid($next,$hash{'goesto_'.$linkid},
153: $hash{'condid_'.$hash{'undercond_'.$linkid}});
154: }
155: }
156: if ($hash{'is_map_'.$next}) {
157: # This jumps to the beginning of a new map (going down level)
158: if (
159: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
160: $mapurl=$hash{'src_'.$next};
161: $next=$hash{'map_start_'.$hash{'src_'.$next}};
162: } elsif (
163: # This jumps back up from an empty sequence, to a page up one level
164: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'page') {
165: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
166: }
167: } elsif
168: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
169: # This comes up from a map (coming up one level);
170: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
171: }
172: } elsif ($direction eq 'back') {
173: # ------------------------------------------------------------------- Backwards
174: while ($hash{'type_'.$rid} eq 'start') {
175: $rid=$hash{'ids_'.$hash{'map_id_'.(split(/\./,$rid))[0]}};
176: }
177: foreach my $id (split(/\,/,$hash{'from_'.$rid})) {
178: my $condition= $hash{'conditions_'.$hash{'comesfrom_'.$id}};
179: my $rescond = &Apache::lonnet::docondval($condition);
180: my $linkcond = &Apache::lonnet::directcondval($hash{'condid_'.$hash{'undercond_'.$id}});
181: my $thiscond = ($rescond<$linkcond)?$rescond:$linkcond;
182: if ($thiscond>=$mincond) {
183: if ($posnext) {
184: $posnext.=','.$id.':'.$thiscond;
185: } else {
186: $posnext=$id.':'.$thiscond;
187: }
188: if ($thiscond>$mincond) { $mincond=$thiscond; }
189: }
190: }
191: foreach my $id (split(/\,/,$posnext)) {
192: my ($linkid,$condval)=split(/\:/,$id);
193: if ($condval>=$mincond) {
194: $next=&addrid($next,$hash{'comesfrom_'.$linkid},
195: $hash{'condid_'.$hash{'undercond_'.$linkid}});
196: }
197: }
198: if ($hash{'is_map_'.$next}) {
199: # This jumps to the end of a new map (going down one level)
200: if (
201: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'sequence') {
202: $mapurl=$hash{'src_'.$next};
203: $next=$hash{'map_finish_'.$hash{'src_'.$next}};
204: } elsif (
205: $hash{'map_type_'.$hash{'map_pc_'.$hash{'src_'.$next}}} eq 'page') {
206: # This jumps back up from an empty sequence, to a page up one level
207: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
208: }
209: } elsif
210: ((split(/\./,$startoutrid))[0]!=(split(/\./,$next))[0]) {
211: # This comes back up from a map (going up one level);
212: $mapurl=$hash{'map_id_'.(split(/\./,$next))[0]};
213: }
214: }
215: return ($next,$mapurl);
216: }
217:
218: sub navlaunch {
219: my ($r)=@_;
220: &Apache::loncommon::content_type($r,'text/html');
221: &Apache::loncommon::no_cache($r);
222: $r->send_http_header;
223: $r->print(&Apache::loncommon::start_page('Launched'));
224: $r->print(<<ENDNAV);
225: <p><a href="/adm/flip?postdata=firstres%3a">Goto first resource</a></p>
226: <script type="text/javascript">
227: function collapse() {
228: menu=window.open("/adm/navmaps?collapseExternal","loncapanav",
229: "height=600,width=400,scrollbars=1");
230: this.document.location='/adm/navmaps?turningOffExternal';
231: }
232: </script>
233: <p><a href="javascript:collapse();">Collapse external navigation window</a></p>
234: ENDNAV
235: $r->print(&Apache::loncommon::end_page());
236: }
237:
238: sub first_accessible_resource {
239: my $furl;
240: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
241: &GDBM_READER(),0640)) {
242: $furl=$hash{'first_url'};
243: my %args;
244: my ($url,$args) = split(/\?/,$furl);
245: foreach my $pair (split(/\&/,$args)) {
246: my ($name,$value) = split(/=/,$pair);
247: $args{&unescape($name)} = &unescape($value);
248: }
249: if (!&Apache::lonnet::allowed('bre',$url,$args{'symb'})) {
250: # Wow, we cannot see this ... move forward to the next one that we can see
251: my ($newrid,$newmap)=&move($hash{'first_rid'},$hash{'first_mapurl'},'forward');
252: # Build the new URL
253: my ($newmapid,$newresid)=split(/\./,$newrid);
254: my $symb=&Apache::lonnet::encode_symb($newmap,$newresid,$hash{'src_'.$newrid});
255: $furl=&add_get_param($hash{'src_'.$newrid},{ 'symb' => $symb });
256: if ($hash{'encrypted_'.$newrid}) {
257: $furl=&Apache::lonenc::encrypted($furl);
258: }
259: }
260: untie(%hash);
261: return $furl;
262: } else {
263: return '/adm/navmaps';
264: }
265: }
266:
267: # ================================================================ Main Handler
268:
269: sub handler {
270: my $r=shift;
271:
272: # ------------------------------------------- Set document type for header only
273:
274: if ($r->header_only) {
275: &Apache::loncommon::content_type($r,'text/html');
276: $r->send_http_header;
277: return OK;
278: }
279:
280: my %cachehash=();
281: my $multichoice=0;
282: my %multichoicehash=();
283: my ($redirecturl,$redirectsymb);
284: my $next='';
285: my @possibilities=();
286: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['postdata']);
287: if (($env{'form.postdata'})&&($env{'request.course.fn'})) {
288: $env{'form.postdata'}=~/(\w+)\:(.*)/;
289: my $direction=$1;
290: my $currenturl=$2;
291: if ($currenturl=~m|^/enc/|) {
292: $currenturl=&Apache::lonenc::unencrypted($currenturl);
293: }
294: $currenturl=~s/\.\d+\.(\w+)$/\.$1/;
295: if ($direction eq 'firstres') {
296: my $furl=&first_accessible_resource();
297: &Apache::loncommon::content_type($r,'text/html');
298: $r->header_out(Location =>
299: &Apache::lonnet::absolute_url().$furl);
300:
301: return REDIRECT;
302: }
303: if ($direction eq 'return' || $direction eq 'navlaunch') {
304: # -------------------------------------------------------- Return to last known
305: my $last;
306: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
307: &GDBM_READER(),0640)) {
308: $last=$hash{'last_known'};
309: untie(%hash);
310: }
311: my $newloc;
312: if (($last) && (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
313: &GDBM_READER(),0640))) {
314: my ($murl,$id,$fn)=&Apache::lonnet::decode_symb($last);
315: $id=$hash{'map_pc_'.&Apache::lonnet::clutter($murl)}.'.'.$id;
316: $newloc=$hash{'src_'.$id};
317: if ($newloc) {
318: if ($hash{'encrypted_'.$id}) { $newloc=&Apache::lonenc::encrypted($newloc); }
319:
320: } else {
321: $newloc='/adm/navmaps';
322: }
323: untie %hash;
324: } else {
325: $newloc='/adm/navmaps';
326: }
327: if ($newloc eq '/adm/navmaps' && $direction eq 'navlaunch') {
328: &navlaunch($r);
329: return OK;
330: } else {
331: &Apache::loncommon::content_type($r,'text/html');
332: $r->header_out(Location =>
333: &Apache::lonnet::absolute_url().$newloc);
334:
335: return REDIRECT;
336: }
337: }
338: $currenturl=~s/^http\:\/\///;
339: $currenturl=~s/^[^\/]+//;
340: #
341: # Is the current URL on the map? If not, start with last known URL
342: #
343: unless (&Apache::lonnet::is_on_map($currenturl)) {
344: my $last;
345: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
346: &GDBM_READER(),0640)) {
347: $last=$hash{'last_known'};
348: untie(%hash);
349: }
350: if ($last) {
351: $currenturl=&Apache::lonnet::clutter((&Apache::lonnet::decode_symb($last))[2]);
352: } else {
353: if ($direction eq 'return') {
354: &Apache::loncommon::content_type($r,'text/html');
355: $r->header_out(Location =>
356: &Apache::lonnet::absolute_url().
357: '/adm/noidea.html');
358: return REDIRECT;
359: } else {
360: &navlaunch($r);
361: return OK;
362: }
363: }
364: }
365: # ------------------------------------------- Do we have any idea where we are?
366: my $position;
367: if ($position=Apache::lonnet::symbread($currenturl)) {
368: # ------------------------------------------------------------------------- Yes
369: my ($startoutmap,$mapnum,$thisurl)=&Apache::lonnet::decode_symb($position);
370: $cachehash{$startoutmap}{$thisurl}=[$thisurl,$mapnum];
371: $cachehash{$startoutmap}{'last_known'}=
372: [&Apache::lonnet::declutter($currenturl),$mapnum];
373:
374: # ============================================================ Tie the big hash
375: if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'.db',
376: &GDBM_READER(),0640)) {
377: my $rid=$hash{'map_pc_'.&Apache::lonnet::clutter($startoutmap)}.
378: '.'.$mapnum;
379:
380: # ------------------------------------------------- Move forward, backward, etc
381: my $endupmap;
382: ($next,$endupmap)=&move($rid,$startoutmap,$direction);
383: # -------------------------------------- Do we have one and only one empty URL?
384: # We are now at at least one non-empty URL
385: # ----------------------------------------------------- Check out possibilities
386: if ($next) {
387: @possibilities=split(/\,/,$next);
388: if ($#possibilities==0) {
389: # ---------------------------------------------- Only one possibility, redirect
390: ($redirecturl,$redirectsymb)=&hash_src($next);
391: $cachehash{$endupmap}{$redirecturl}=
392: [$redirecturl,(split(/\./,$next))[1]];
393: } else {
394: # ------------------------ There are multiple possibilities for a next resource
395: $multichoice=1;
396: foreach my $id (@possibilities) {
397: $multichoicehash{'src_'.$id}=$hash{'src_'.$id};
398: $multichoicehash{'title_'.$id}=$hash{'title_'.$id};
399: $multichoicehash{'type_'.$id}=$hash{'type_'.$id};
400: (my $first, my $second) = $id =~ /(\d+).(\d+)/;
401: my $symbSrc = Apache::lonnet::declutter($hash{'src_'.$id});
402: $multichoicehash{'symb_'.$id} =
403: Apache::lonnet::declutter($hash{'map_id_'.$first}.'___'.
404: $second.'___'.$symbSrc);
405:
406: my ($choicemap,$choiceres)=split(/\./,$id);
407: my $map=&Apache::lonnet::declutter($hash{'src_'.$choicemap});
408: my $url=$multichoicehash{'src_'.$id};
409: $cachehash{$map}{$url}=[$url,$choiceres];
410: }
411: }
412: } else {
413: # -------------------------------------------------------------- No place to go
414: $multichoice=-1;
415: }
416: # ----------------- The program must come past this point to untie the big hash
417: untie(%hash);
418: # --------------------------------------------------------- Store position info
419: $cachehash{$startoutmap}{'last_direction'}=[$direction,'notasymb'];
420: foreach my $thismap (keys %cachehash) {
421: my $mapnum=$cachehash{$thismap}->{'mapnum'};
422: delete($cachehash{$thismap}->{'mapnum'});
423: &Apache::lonnet::symblist($thismap,
424: %{$cachehash{$thismap}});
425: }
426: # ============================================== Do not return before this line
427: if ($redirecturl) {
428: # ----------------------------------------------------- There is a URL to go to
429: if ($direction eq 'forward') {
430: &Apache::lonnet::linklog($currenturl,$redirecturl);
431: }
432: if ($direction eq 'back') {
433: &Apache::lonnet::linklog($redirecturl,$currenturl);
434: }
435: # ------------------------------------------------- Check for critical messages
436: if ((time-$env{'user.criticalcheck.time'})>300) {
437: my @what=&Apache::lonnet::dump
438: ('critical',$env{'user.domain'},
439: $env{'user.name'});
440: if ($what[0]) {
441: if (($what[0] ne 'con_lost') &&
442: ($what[0]!~/^error\:/)) {
443: $redirecturl='/adm/email?critical=display';
444: $redirectsymb='';
445: }
446: }
447: &Apache::lonnet::appenv('user.criticalcheck.time'=>time);
448: }
449:
450: &Apache::loncommon::content_type($r,'text/html');
451: my $url=&Apache::lonnet::absolute_url().$redirecturl;
452: $url = &add_get_param($url, { 'symb' => $redirectsymb});
453: $r->header_out(Location => $url);
454: return REDIRECT;
455: } else {
456: # --------------------------------------------------------- There was a problem
457: &Apache::loncommon::content_type($r,'text/html');
458: $r->send_http_header;
459: my %lt=&Apache::lonlocal::texthash('title' => 'End of Sequence',
460: 'explain' =>
461: 'You have reached the end of the sequence of materials.',
462: 'back' => 'Go Back',
463: 'nav' => 'Navigate Course Content',
464: 'wherenext' =>
465: 'There are several possibilities of where to go next',
466: 'pick' =>
467: 'Please click on the the resource you intend to access',
468: 'titleheader' => 'Title',
469: 'type' => 'Type');
470: if ($#possibilities>0) {
471: my $start_page=
472: &Apache::loncommon::start_page('Multiple Resources');
473: $r->print(<<ENDSTART);
474: $start_page
475: <h3>$lt{'wherenext'}</h3>
476: <p>
477: $lt{'pick'}:
478: <p>
479: <table border=2>
480: <tr><th>$lt{'titleheader'}</th><th>$lt{'type'}</th></tr>
481: ENDSTART
482: foreach my $id (@possibilities) {
483: $r->print(
484: '<tr><td><a href="'.
485: &add_get_param($multichoicehash{'src_'.$id},
486: {'symb' =>
487: $multichoicehash{'symb_'.$id},
488: }).'">'.
489: $multichoicehash{'title_'.$id}.
490: '</a></td><td>'.$multichoicehash{'type_'.$id}.
491: '</td></tr>');
492: }
493: $r->print('</table>');
494: } else {
495: my $start_page=
496: &Apache::loncommon::start_page('No Resource');
497: $r->print(<<ENDNONE);
498: $start_page
499: <h3>$lt{'title'}</h3>
500: <p>$lt{'explain'}</p>
501: ENDNONE
502: }
503: $r->print(<<ENDMENU);
504: <ul>
505: <li><a href="/adm/flip?postdata=return:">$lt{'back'}</a></li>
506: <li><a href="/adm/navmaps">$lt{'nav'}</a></li>
507: </ul>
508: ENDMENU
509: $r->print(&Apache::loncommon::end_page());
510: return OK;
511: }
512: } else {
513: # ------------------------------------------------- Problem, could not tie hash
514: $env{'user.error.msg'}="/adm/flip:bre:0:1:Course Data Missing";
515: return HTTP_NOT_ACCEPTABLE;
516: }
517: } else {
518: # ---------------------------------------- No, could not determine where we are
519: $r->internal_redirect('/adm/ambiguous');
520: }
521: } else {
522: # -------------------------- Class was not initialized or page fliped strangely
523: $env{'user.error.msg'}="/adm/flip:bre:0:0:Choose Course";
524: return HTTP_NOT_ACCEPTABLE;
525: }
526: }
527:
528: 1;
529: __END__
530:
531:
532:
533:
534:
535:
536:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>