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