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