1: # The LearningOnline Network with CAPA
2: #
3: # Sequence Handler
4: #
5: # $Id: lonsequence.pm,v 1.19.2.1 2004/10/16 01:15:33 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: # (Handler to resolve ambiguous file locations
30: #
31: # (TeX Content Handler
32: #
33: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
34: #
35: # 10/11,10/12 Gerd Kortemeyer)
36: #
37: # 10/16 Gerd Kortemeyer
38:
39: package Apache::lonsequence;
40:
41: use strict;
42: use Apache::lonnet;
43: use Apache::Constants qw(:common :http REDIRECT);
44: use GDBM_File;
45: use Apache::lonratedt;
46: use Apache::lonratsrv;
47: use Apache::lonpageflip;
48: use Apache::loncommon;
49: use Apache::lonlocal;
50:
51: my %selhash;
52: my $successtied;
53:
54: # ----------------------------------------- Attempt to read from resource space
55:
56: sub attemptread {
57: my $fn=shift;
58: &Apache::lonnet::repcopy($fn);
59: if (-e $fn) {
60: return &Apache::lonratedt::attemptread($fn);
61: } else {
62: return ();
63: }
64: }
65:
66: sub mapread {
67: my $fn=shift;
68: &Apache::lonnet::repcopy($fn);
69: if (-e $fn) {
70: return &Apache::lonratedt::mapread($fn,'');
71: } else {
72: return ();
73: }
74: }
75:
76: # ---------------------------------------------------------------- View Handler
77:
78: sub viewmap {
79: my ($r,$url)=@_;
80: $r->print('<html>');
81: if ($ENV{'form.forceselect'}) { $r->print(<<ENDSCRIPT);
82: <script>
83:
84: function select_group() {
85: window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
86: }
87:
88: function queue(val) {
89: if (eval("document.forms."+val+".filelink.checked")) {
90: var l=val.length;
91: var v=val.substring(4,l);
92: document.forms.fileattr.acts.value+='1a'+v+'b';
93: }
94: else {
95: var l=val.length;
96: var v=val.substring(4,l);
97: document.forms.fileattr.acts.value+='0a'+v+'b';
98: }
99: }
100:
101: </script>
102: ENDSCRIPT
103: }
104: $r->print(&Apache::loncommon::bodytag('Map Contents').'<h1>'.$url.'</h1>');
105: # ------------------ This is trying to select. Provide buttons and tie %selhash
106: if ($ENV{'form.forceselect'}) { $r->print(<<ENDSELECT);
107: <form name=fileattr><input type=hidden name=acts value=''>
108: <input type="button" name="close" value='CLOSE' onClick="self.close()">
109: <input type="button" name="groupimport" value='GROUP IMPORT'
110: onClick="javascript:select_group()">
111: </form>
112: ENDSELECT
113: my $diropendb =
114: "/home/httpd/perl/tmp/$ENV{'user.domain'}\_$ENV{'user.name'}_groupsec.db";
115: if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
116: if ($ENV{'form.launch'} eq '1') {
117: &start_fresh_session();
118: }
119: $successtied=1;
120:
121: # - Evaluate actions from previous page (both cumulatively and chronologically)
122: if ($ENV{'form.catalogmode'} eq 'groupimport') {
123: my $acts=$ENV{'form.acts'};
124: my @Acts=split(/b/,$acts);
125: my %ahash;
126: my %achash;
127: my $ac=0;
128: # some initial hashes for working with data
129: foreach (@Acts) {
130: my ($state,$ref)=split(/a/);
131: $ahash{$ref}=$state;
132: $achash{$ref}=$ac;
133: $ac++;
134: }
135: # sorting through the actions and changing the tied database hash
136: foreach (sort {$achash{$a}<=>$achash{$b}} (keys %ahash)) {
137: my $key=$_;
138: if ($ahash{$key} eq '1') {
139: $selhash{'store_'.$selhash{'pre_'.$key.'_link'}}=
140: $selhash{'pre_'.$key.'_title'};
141: $selhash{'storectr_'.$selhash{'pre_'.$key.'_link'}}=
142: $selhash{'storectr'}+0;
143: $selhash{'storectr'}++;
144: }
145: if ($ahash{$key} eq '0') {
146: if ($selhash{'store_'.$selhash{'pre_'.$key.'_link'}}) {
147: delete $selhash{'store_'.$selhash{'pre_'.$key.'_link'}};
148: }
149: }
150: }
151: # deleting the previously cached listing
152: foreach (keys %selhash) {
153: if ($_ =~ /^pre_/ && $_ =~/link$/) {
154: my $key = $_;
155: $key =~ s/^pre_//;
156: $key =~ s/_[^_]*$//;
157: delete $selhash{'pre_'.$key.'_title'};
158: delete $selhash{'pre_'.$key.'_link'};
159: }
160: }
161: }
162: # -
163: }
164: }
165: # ----------------------------- successtied is now '1' if in working selectmode
166: my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
167: if ($fatal==1) {
168: $r->print('<p><b><font color="red">'.&mt('Map contents are not shown in order.').'</font></b></p><br />');
169: }
170: my $idx=0;
171: foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
172: if (defined($_)) {
173: $idx++;
174: if ($successtied) {
175: $r->print('<form name="form'.$idx.'">');
176: }
177: my ($title,$url)=split(/\:/,$_);
178: $title=~s/\&colon\;/\:/g;
179: $url=~s/\&colon\;/\:/g;
180: unless ($title) { $title=(split(/\//,$url))[-1] };
181: unless ($title) { $title='<i>'.&mt('Empty').'</i>'; }
182: if ($url) {
183: if ($successtied) {
184: my $checked='';
185: if ($selhash{'store_'.$url}) {
186: $checked=" checked";
187: }
188: $selhash{"pre_${idx}_link"}=$url;
189: $selhash{"pre_${idx}_title"}=$title;
190:
191: $r->print(<<ENDCHECKBOX);
192: <input type='checkbox' name='filelink'
193: value='$url' onClick='javascript:queue("form$idx")'$checked>
194: <input type='hidden' name='title' value='$title'>
195: ENDCHECKBOX
196: }
197: $r->print('<a href="'.&Apache::lonratsrv::qtescape($url).'">');
198: }
199: $r->print(&Apache::lonratsrv::qtescape($title));
200: if ($url) { $r->print('</a>'); }
201: if ($successtied) {
202: $r->print('</form>');
203: } else {
204: $r->print('<br>');
205: }
206: }
207: }
208: $r->print('</body></html>');
209: if ($successtied) {
210: untie %selhash;
211: }
212: }
213:
214: # ----------------------------------------------------------- Clean out selhash
215: sub start_fresh_session {
216: foreach (keys %selhash) {
217: if ($_ =~ /^pre_/) {
218: delete $selhash{$_};
219: }
220: if ($_ =~ /^store/) {
221: delete $selhash{$_};
222: }
223: }
224: }
225:
226:
227: # ================================================================ Main Handler
228:
229: sub handler {
230: my $r=shift;
231:
232: if ($r->header_only) {
233: &Apache::loncommon::content_type($r,'text/html');
234: $r->send_http_header;
235: return OK;
236: }
237:
238: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
239: ['forceselect','launch']);
240:
241: my %hash;
242: my %bighash;
243: my $requrl=$r->uri;
244:
245: $successtied=0;
246: # ------------------------------------------------------------ Tie symb db file
247: my $disurl='';
248: my $dismapid='';
249: my $exitdisid = '';
250: my $arrow_dir = '';
251:
252: if (($ENV{'request.course.fn'}) && (!$ENV{'form.forceselect'})) {
253: my $last;
254: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
255: &GDBM_READER(),0640)) {
256: $last=$hash{'last_direction'};
257: untie(%hash);
258: }
259: my $direction='';
260: my $prevmap='';
261: if ($last) {
262: ($prevmap,$direction)=(split(/\_\_\_/,$last));
263: }
264: # ------------------------------------------------------------- Tie big db file
265: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
266: &GDBM_READER(),0640)) {
267: my $disid='';
268: my $randomout ='';
269:
270: if ($direction eq 'back') {
271: $disid=$bighash{'map_finish_'.$requrl};
272: } else {
273: $disid=$bighash{'map_start_'.$requrl};
274: }
275: if ($disid) {
276: $disurl=$bighash{'src_'.$disid};
277: $dismapid=(split(/\./,$disid))[1];
278: $randomout = $bighash{'randomout_'.$disid};
279: } elsif (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
280: &GDBM_READER(),0640)) {
281: $last=$hash{'last_known'};
282: untie(%hash);
283: }
284:
285:
286: # ----------- If this is an empty one, or hidden, skip to next non-empty or non-hidden one
287: while ( ((!$disurl) && ($disid)) || ($randomout && $disid) ) {
288: $direction=($direction?$direction:'forward');
289: ($disid,$requrl)=
290: &Apache::lonpageflip::fullmove($disid,
291: &Apache::lonnet::declutter($requrl),$direction);
292: if ($disid) {
293: $disurl=$bighash{'src_'.$disid};
294: $dismapid=(split(/\./,$disid))[1];
295: $randomout = $bighash{'randomout_'.$disid};
296: }
297: }
298: $exitdisid = $disid;
299: $arrow_dir = $direction;
300:
301: # --------------------------------------- Untie hash, make sure to come by here
302: untie(%bighash);
303: }
304: }
305:
306: # now either disurl is set (going to first page), or we need another display
307: if ($disurl) {
308: # -------------------------------------------------- Has first or last resource
309: &Apache::lonnet::symblist($requrl,$disurl => $dismapid,
310: 'last_known' => &Apache::lonnet::declutter($disurl));
311: &Apache::loncommon::content_type($r,'text/html');
312: $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.$disurl);
313: return REDIRECT;
314: } else {
315: &Apache::loncommon::content_type($r,'text/html');
316: $r->send_http_header;
317: if ($exitdisid eq '' && $arrow_dir ne '') {
318: my %lt =&Apache::lonlocal::texthash(
319: 'back' => 'beginning',
320: 'forward' => 'end',
321: 'emfo' => 'Empty Folder/Sequence',
322: 'nere' => 'Next resource could not be displayed',
323: 'goba' => 'Go Back',
324: 'nacc' => 'Navigate Course Content',
325: );
326: my $warnmsg = 'As all folders and sequences ';
327: if ($arrow_dir eq 'forward') {
328: $warnmsg .= &mt('following the current resource were empty').',';
329: } elsif ($arrow_dir eq 'back') {
330: $warnmsg .= &mt('preceding the current resource were empty').',';
331: }
332: $warnmsg .= &mt('you have now reached the').' '.$lt{$arrow_dir}.' '.&mt('of the course.');
333: my $bodytag=&Apache::loncommon::bodytag(&mt('emfo'));
334: $r->print(<<ENDNONE);
335: <head><title>$lt{'emfo'}</title></head>
336: $bodytag
337: <h3>$lt{'nere'}</h3>
338: <p>$warnmsg</p>
339: <ul>
340: <li><a href="javascript:history.go(-1)">$lt{'goba'}</a></li>
341: <li><a href="/adm/navmaps">$lt{'nacc'}</a></li>
342: </ul>
343: </body>
344: </html>
345: ENDNONE
346: } else {
347: &viewmap($r,$requrl);
348: }
349: return OK;
350: }
351: }
352:
353: 1;
354: __END__
355:
356:
357:
358:
359:
360:
361:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>