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