1: # The LearningOnline Network with CAPA
2: # The LON-CAPA group sort handler
3: # Allows for sorting prior to import into RAT.
4: #
5: # $Id: groupsort.pm,v 1.22 2003/08/26 05:44:11 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: # YEAR=2001
30: # YEAR=2002
31: #
32: ###
33:
34: package Apache::groupsort;
35:
36: use strict;
37:
38: use Apache::Constants qw(:common);
39: use GDBM_File;
40: use Apache::loncommon;
41:
42: my %hash; # variable to tie to user specific database
43: my $iconpath; # variable to be accessible to multiple subroutines
44:
45: sub cleanup {
46: if (tied(%hash)){
47: &Apache::lonnet::logthis('Cleanup groupsort: hash');
48: unless (untie(%hash)) {
49: &Apache::lonnet::logthis('Failed cleanup groupsort: hash');
50: }
51: }
52: }
53:
54: # ---------------------------------------------------------------- Main Handler
55: sub handler {
56: my $r = shift;
57:
58: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
59: ['acts','catalogmode','mode']);
60: # color scheme
61: my $fileclr = '#ffffe6';
62: my $titleclr = '#ddffff';
63:
64: $r->content_type('text/html');
65: $r->send_http_header;
66: return OK if $r->header_only;
67:
68: # finish_import looks different for graphical or "simple" RAT
69: my $finishimport='';
70: if ($ENV{'form.mode'} eq 'simple') {
71: $finishimport=(<<ENDSMP);
72: function finish_import() {
73: opener.document.forms.simpleedit.importdetail.value='';
74: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
75: opener.document.forms.simpleedit.importdetail.value+='&'+
76: escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
77: escape(eval("document.forms.groupsort.filelink"+num+".value"));
78: }
79: opener.document.forms.simpleedit.submit();
80: self.close();
81: }
82: ENDSMP
83: } else {
84: $finishimport=(<<ENDADV);
85: function finish_import() {
86: var linkflag=false;
87: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
88: insertRowInLastRow();
89: placeResourceInLastRow(
90: eval("document.forms.groupsort.title"+num+".value"),
91: eval("document.forms.groupsort.filelink"+num+".value"),
92: linkflag
93: );
94: linkflag=true;
95: }
96: opener.editmode=0;
97: opener.notclear=0;
98: opener.linkmode=0;
99: opener.draw();
100: self.close();
101: }
102: ENDADV
103: }
104:
105: # output start of web page
106:
107: $r->print(<<END);
108: <html>
109: <head>
110: <title>The LearningOnline Network With CAPA Group Sorter</title>
111: <script language='javascript'>
112: function insertRowInLastRow() {
113: opener.insertrow(opener.maxrow);
114: opener.addobj(opener.maxrow,'e&2');
115: }
116: function placeResourceInLastRow (title,url,linkflag) {
117: opener.newresource(opener.maxrow,2,opener.escape(title),
118: opener.escape(url),'false','normal');
119: opener.save();
120: opener.mostrecent=opener.obj.length-1;
121: if (linkflag) {
122: opener.joinres(opener.linkmode,opener.mostrecent,0);
123: }
124: opener.linkmode=opener.mostrecent;
125: }
126: $finishimport
127: function selectchange(val) {
128: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
129: orderchange(val,newval);
130: }
131: function move(val,newval) {
132: orderchange(val,newval);
133: }
134: function orderchange(val,newval) {
135: document.forms.groupsort.oldval.value=val;
136: document.forms.groupsort.newval.value=newval;
137: document.forms.groupsort.submit();
138: }
139: </script>
140: </head>
141: END
142: # read pertinent machine configuration
143: my $domain = $r->dir_config('lonDefDomain');
144: $iconpath = $r->dir_config('lonIconsURL') . "/";
145:
146: my %shash; # sort order (key is resource location, value is sort order)
147: my %thash; # title (key is resource location, value is title)
148:
149: my $diropendb;
150: # ------------------------------ which file do we open? Easy if explictly given
151: if ($ENV{'form.catalogmode'} eq 'groupsearch') {
152: $diropendb =
153: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db";
154: }
155: elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
156: $diropendb =
157: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_indexer.db";
158: }
159: elsif ($ENV{'form.catalogmode'} eq 'groupsec') {
160: $diropendb =
161: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_groupsec.db";
162: }
163: # --------------------- not explicitly given, choose the one most recently used
164: else { # choose last accessed
165: my @dbfn;
166: my @dbst;
167:
168: $dbfn[0] =
169: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db";
170: $dbst[0]=-1;
171: if (-e $dbfn[0]) {
172: $dbst[0]=(stat($dbfn[0]))[9];
173: }
174: $dbfn[1] =
175: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_indexer.db";
176: $dbst[1]=-1;
177: if (-e $dbfn[1]) {
178: $dbst[1]=(stat($dbfn[1]))[9];
179: }
180: $dbfn[2] =
181: "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_groupsec.db";
182: $dbst[2]=-1;
183: if (-e $dbfn[2]) {
184: $dbst[2]=(stat($dbfn[2]))[9];
185: }
186: # Expand here for more modes
187: # ....
188:
189: # Okay, find most recent existing
190:
191: my $newest=0;
192: $diropendb='';
193: for (my $i=0; $i<=$#dbfn; $i++) {
194: if ($dbst[$i]>$newest) {
195: $newest=$dbst[$i];
196: $diropendb=$dbfn[$i];
197: }
198: }
199:
200: }
201: # ----------------------------- diropendb is now the filename of the db to open
202: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
203: my $acts = $ENV{'form.acts'};
204: my @Acts = split(/b/,$acts);
205: my %ahash;
206: my %achash;
207: my $ac = 0;
208: foreach (@Acts) {
209: my ($state,$ref) = split(/a/);
210: $ahash{$ref} = $state;
211: $achash{$ref} = $ac;
212: $ac++;
213: }
214: foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
215: my $key = $_;
216: if ($ahash{$key} eq '1') {
217: # my $keyz=join("<br />",keys %hash);
218: # print "<br />$key<br />$keyz".$hash{'pre_'.$key.'_link'}."<br />\n";
219: $hash{'store_'.$hash{'pre_'.$key.'_link'}} =
220: $hash{'pre_'.$key.'_title'};
221: $hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
222: $hash{'storectr'}+0;
223: $hash{'storectr'}++;
224: }
225: if ($ahash{$key} eq '0') {
226: if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
227: delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
228: }
229: }
230: }
231: foreach (keys %hash) {
232: if ($_ =~ /^store_/) {
233: my $key = $_;
234: $key =~ s/^store_//;
235: $shash{$key} = $hash{'storectr_'.$key};
236: $thash{$key} = $hash{'store_'.$key};
237: }
238: }
239: if ($ENV{'form.oldval'}) {
240: my $newctr = 0;
241: my %chash;
242: foreach (sort {$shash{$a} <=> $shash{$b}} (keys %shash)) {
243: my $key = $_;
244: $newctr++;
245: $shash{$key} = $newctr;
246: $hash{'storectr_'.$key} = $newctr;
247: $chash{$newctr} = $key;
248: }
249: my $oldval = $ENV{'form.oldval'};
250: my $newval = $ENV{'form.newval'};
251: if ($oldval != $newval) {
252: # when newval==0, then push down and delete
253: if ($newval!=0) {
254: $shash{$chash{$oldval}} = $newval;
255: $hash{'storectr_'.$chash{$oldval}} = $newval;
256: }
257: else {
258: $shash{$chash{$oldval}} = $newctr;
259: $hash{'storectr_'.$chash{$oldval}} = $newctr;
260: }
261: if ($newval==0) { # push down
262: my $newval2=$newctr;
263: for my $idx ($oldval..($newval2-1)) {
264: $shash{$chash{$idx+1}} = $idx;
265: $hash{'storectr_'.$chash{$idx+1}} = $idx;
266: }
267: delete $shash{$chash{$oldval}};
268: delete $hash{'storectr_'.$chash{$oldval}};
269: delete $hash{'store_'.$chash{$oldval}};
270: }
271: elsif ($oldval < $newval) { # push down
272: for my $idx ($oldval..($newval-1)) {
273: $shash{$chash{$idx+1}} = $idx;
274: $hash{'storectr_'.$chash{$idx+1}} = $idx;
275: }
276: }
277: elsif ($oldval > $newval) { # push up
278: for my $idx (reverse($newval..($oldval-1))) {
279: $shash{$chash{$idx}} = $idx+1;
280: $hash{'storectr_'.$chash{$idx}} = $idx+1;
281: }
282: }
283: }
284: }
285: } else {
286: $r->print('Unable to tie hash to db file</body></html>');
287: return OK;
288: }
289: untie %hash;
290: my $ctr = 0;
291: my $clen = scalar(keys %shash);
292: if ($clen > 1) {
293: $r->print(&Apache::loncommon::bodytag('Sort Imported Resources'));
294: $r->print(<<END);
295: <b><font color="#888888">Finalize order of resources</font></b>
296: <form method='post' action='/adm/groupsort' name='groupsort'
297: enctype='application/x-www-form-urlencoded'>
298: <input type="hidden" name="fnum" value="$clen" />
299: <input type="hidden" name="oldval" value="" />
300: <input type="hidden" name="newval" value="" />
301: <input type="hidden" name="mode" value="$ENV{'form.mode'}" />
302: END
303:
304: # --- Expand here if "GO BACK" button desired
305: if ($ENV{'form.catalogmode'} eq 'groupimport') {
306: $r->print(<<END);
307: <input type="button" name="alter" value="GO BACK"
308: onClick="window.location='/res/?catalogmode=groupimport'" />
309: END
310: }
311: if ($ENV{'form.catalogmode'} eq 'groupsearch') {
312: $r->print(<<END);
313: <input type="button" name="alter" value="New Search"
314: onClick="window.location='/adm/searchcat?catalogmode=groupsearch&cleargroupsort=1'" />
315: END
316: }
317: # ---
318:
319: $r->print(<<END);
320: <input type="button" name="alter" value="FINISH IMPORT"
321: onClick="finish_import()" />
322: <input type="button" name="alter" value="CANCEL" onClick="self.close()" />
323: END
324: $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
325: $r->print("<table border=0><tr>\n");
326: $r->print("<td colspan='2' bgcolor='$titleclr'><b>Change order</b></td>\n");
327: $r->print("<td colspan='2' bgcolor='$titleclr'><b>Title</b></td>\n");
328: $r->print("<td bgcolor='$titleclr'><b>Path</b></td></tr>\n");
329: } else {
330: $r->print(<<END);
331: <body>
332: <form method='post' action='/adm/groupsort' name='groupsort'
333: enctype='application/x-www-form-urlencoded'>
334: <input type="hidden" name="fnum" value="$clen" />
335: <input type="hidden" name="oldval" value="" />
336: <input type="hidden" name="newval" value="" />
337: <input type="hidden" name="mode" value="$ENV{'form.mode'}" />
338: END
339: }
340: foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
341: my $key=$_;
342: $ctr++;
343: my @file_ext = split(/\./,$key);
344: my $curfext = $file_ext[scalar(@file_ext)-1];
345: my $iconname="unknown.gif";
346: my $embstyle = &Apache::loncommon::fileembstyle($curfext);
347: # The unless conditional that follows is a bit of overkill
348: $iconname = $curfext.".gif" unless
349: (!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn');
350: if ($clen > 1) {
351: $r->print("<tr><td bgcolor='$fileclr'>");
352: $r->print(&movers($clen,$ctr));
353: }
354: $r->print(&hidden($ctr-1,$thash{$key},$key));
355: if ($clen > 1) {
356: $r->print("</td><td bgcolor='$fileclr'>");
357: $r->print(&select_box($clen,$ctr));
358: $r->print("</td><td bgcolor='$fileclr'>");
359: $r->print("<img src='$iconpath$iconname'>");
360: $r->print("</td><td bgcolor='$fileclr'>");
361: $r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
362: $r->print("$key</td></tr>\n");
363: }
364: }
365: if ($clen > 1) {
366: $r->print("</table></td></tr></table></form>");
367: } else {
368: $r->print(<<END);
369: <script type="text/javascript">
370: finish_import();
371: </script>
372: END
373: }
374: $r->print(<<END);
375: </body>
376: </html>
377: END
378:
379: return OK;
380: }
381:
382: # --------------------------------------- Hidden values (returns scalar string)
383: sub hidden {
384: my ($sel,$title,$filelink) = @_;
385: my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
386: '" />';
387: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
388: $filelink.'" />';
389: return $string;
390: }
391:
392: # --------------------------------------- Moving arrows (returns scalar string)
393: sub movers {
394: my ($total,$sel) = @_;
395: my $dsel = $sel-1;
396: my $usel = $sel+1;
397: $usel = 1 if $usel > $total;
398: $dsel = $total if $dsel < 1;
399: my $string;
400: $string = (<<END);
401: <table border='0' cellspacing='0' cellpadding='0'>
402: <tr><td><a href='javascript:move($sel,$dsel)'>
403: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
404: <tr><td><a href='javascript:move($sel,$usel)'>
405: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
406: </table>
407: END
408: return $string;
409: }
410:
411: # ------------------------------------------ Select box (returns scalar string)
412: sub select_box {
413: my ($total,$sel) = @_;
414: my $string;
415: $string = '<select name="alt'.$sel.'"';
416: $string .= " onChange='selectchange($sel)'>";
417: $string .= "<option name='o0' value='0'>remove</option>";
418: for my $cur (1..$total) {
419: $string .= "<option name='o$cur' value='$cur'";
420: if ($cur == $sel) {
421: $string .= "selected";
422: }
423: $string .= ">$cur</option>";
424: }
425: $string .= "</select>\n";
426: return $string;
427: }
428:
429: 1;
430:
431: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>