Annotation of rat/lonsequence.pm, revision 1.19
1.1 www 1: # The LearningOnline Network with CAPA
2: #
3: # Sequence Handler
4: #
1.19 ! raeburn 5: # $Id: lonsequence.pm,v 1.18 2004/07/06 15:27:00 raeburn Exp $
1.5 www 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: #
1.1 www 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:
1.3 www 39: package Apache::lonsequence;
1.1 www 40:
41: use strict;
42: use Apache::lonnet;
1.3 www 43: use Apache::Constants qw(:common :http REDIRECT);
1.1 www 44: use GDBM_File;
1.8 www 45: use Apache::lonratedt;
46: use Apache::lonratsrv;
1.11 www 47: use Apache::lonpageflip;
1.14 www 48: use Apache::loncommon;
1.16 www 49: use Apache::lonlocal;
1.1 www 50:
1.12 www 51: my %selhash;
52: my $successtied;
1.8 www 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:
1.15 www 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:
1.8 www 76: # ---------------------------------------------------------------- View Handler
77:
78: sub viewmap {
1.9 www 79: my ($r,$url)=@_;
1.8 www 80: $r->print('<html>');
81: if ($ENV{'form.forceselect'}) { $r->print(<<ENDSCRIPT);
82: <script>
83:
84: function select_group() {
1.12 www 85: window.location="/adm/groupsort?catalogmode=groupsec&mode=rat&acts="+document.forms.fileattr.acts.value;
1.8 www 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: }
1.14 www 104: $r->print(&Apache::loncommon::bodytag('Map Contents').'<h1>'.$url.'</h1>');
1.12 www 105: # ------------------ This is trying to select. Provide buttons and tie %selhash
1.8 www 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
1.12 www 113: my $diropendb =
114: "/home/httpd/perl/tmp/$ENV{'user.domain'}\_$ENV{'user.name'}_groupsec.db";
1.13 albertel 115: if (tie(%selhash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.12 www 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: }
1.8 www 164: }
1.12 www 165: # ----------------------------- successtied is now '1' if in working selectmode
1.15 www 166: my ($errtext,$fatal)=&mapread(&Apache::lonnet::filelocation('',$url),'');
167: if ($fatal==1) {
1.16 www 168: $r->print('<p><b><font color="red">'.&mt('Map contents are not shown in order.').'</font></b></p><br />');
1.15 www 169: }
1.8 www 170: my $idx=0;
171: foreach (&attemptread(&Apache::lonnet::filelocation('',$url))) {
172: if (defined($_)) {
173: $idx++;
1.12 www 174: if ($successtied) {
1.8 www 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] };
1.16 www 181: unless ($title) { $title='<i>'.&mt('Empty').'</i>'; }
1.8 www 182: if ($url) {
1.12 www 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:
1.8 www 191: $r->print(<<ENDCHECKBOX);
192: <input type='checkbox' name='filelink'
1.12 www 193: value='$url' onClick='javascript:queue("form$idx")'$checked>
1.8 www 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>'); }
1.12 www 201: if ($successtied) {
1.8 www 202: $r->print('</form>');
203: } else {
204: $r->print('<br>');
205: }
206: }
207: }
208: $r->print('</body></html>');
1.12 www 209: if ($successtied) {
210: untie %selhash;
211: }
1.8 www 212: }
213:
1.12 www 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:
1.1 www 227: # ================================================================ Main Handler
228:
229: sub handler {
230: my $r=shift;
231:
232: if ($r->header_only) {
1.16 www 233: &Apache::loncommon::content_type($r,'text/html');
1.1 www 234: $r->send_http_header;
235: return OK;
236: }
1.8 www 237:
238: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.12 www 239: ['forceselect','launch']);
1.1 www 240:
1.2 www 241: my %hash;
1.1 www 242: my %bighash;
1.2 www 243: my $requrl=$r->uri;
244:
1.12 www 245: $successtied=0;
1.3 www 246: # ------------------------------------------------------------ Tie symb db file
1.9 www 247: my $disurl='';
248: my $dismapid='';
1.18 raeburn 249: my $exitdisid = '';
250: my $arrow_dir = '';
1.9 www 251:
252: if (($ENV{'request.course.fn'}) && (!$ENV{'form.forceselect'})) {
1.2 www 253: my $last;
254: if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
1.13 albertel 255: &GDBM_READER(),0640)) {
1.2 www 256: $last=$hash{'last_direction'};
257: untie(%hash);
258: }
1.3 www 259: my $direction='';
260: my $prevmap='';
261: if ($last) {
262: ($prevmap,$direction)=(split(/\_\_\_/,$last));
263: }
264: # ------------------------------------------------------------- Tie big db file
1.1 www 265: if (tie(%bighash,'GDBM_File',$ENV{'request.course.fn'}.'.db',
1.13 albertel 266: &GDBM_READER(),0640)) {
1.3 www 267: my $disid='';
1.17 raeburn 268: my $randomout ='';
1.10 www 269:
1.3 www 270: if ($direction eq 'back') {
271: $disid=$bighash{'map_finish_'.$requrl};
272: } else {
273: $disid=$bighash{'map_start_'.$requrl};
1.18 raeburn 274: }
1.3 www 275: if ($disid) {
276: $disurl=$bighash{'src_'.$disid};
1.4 www 277: $dismapid=(split(/\./,$disid))[1];
1.17 raeburn 278: $randomout = $bighash{'randomout_'.$disid};
1.18 raeburn 279: } elsif (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.'_symb.db',
280: &GDBM_READER(),0640)) {
281: $last=$hash{'last_known'};
282: untie(%hash);
1.3 www 283: }
1.18 raeburn 284:
285:
1.17 raeburn 286: # ----------- If this is an empty one, or hidden, skip to next non-empty or non-hidden one
1.18 raeburn 287: while ( ((!$disurl) && ($disid)) || ($randomout && $disid) ) {
1.11 www 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];
1.19 ! raeburn 295: $randomout = $bighash{'randomout_'.$disid};
1.11 www 296: }
297: }
1.18 raeburn 298: $exitdisid = $disid;
299: $arrow_dir = $direction;
1.11 www 300:
1.3 www 301: # --------------------------------------- Untie hash, make sure to come by here
302: untie(%bighash);
1.9 www 303: }
304: }
305:
306: # now either disurl is set (going to first page), or we need another display
307: if ($disurl) {
1.3 www 308: # -------------------------------------------------- Has first or last resource
1.9 www 309: &Apache::lonnet::symblist($requrl,$disurl => $dismapid,
1.7 www 310: 'last_known' => &Apache::lonnet::declutter($disurl));
1.16 www 311: &Apache::loncommon::content_type($r,'text/html');
1.9 www 312: $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.$disurl);
313: return REDIRECT;
1.1 www 314: } else {
1.16 www 315: &Apache::loncommon::content_type($r,'text/html');
1.9 www 316: $r->send_http_header;
1.18 raeburn 317: if ($exitdisid eq '') {
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: }
1.9 www 349: return OK;
1.1 www 350: }
351: }
352:
353: 1;
354: __END__
355:
356:
357:
358:
359:
360:
361:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>