Annotation of loncom/interface/groupsort.pm, revision 1.7
1.1 harris41 1: # The LearningOnline Network with CAPA
1.4 harris41 2: # The LON-CAPA group sort handler
3: # Allows for sorting prior to import into RAT.
4: #
1.7 ! www 5: # $Id: groupsort.pm,v 1.6 2002/01/17 12:17:50 harris41 Exp $
1.4 harris41 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.
1.1 harris41 20: #
1.4 harris41 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
1.1 harris41 26: #
1.4 harris41 27: # http://www.lon-capa.org/
1.1 harris41 28: #
29: # YEAR=2001
1.4 harris41 30: # 8/7,8/8,10/14,10/15,12/10 Scott Harrison
1.6 harris41 31: # YEAR=2002
32: # 1/17 Scott Harrison
1.4 harris41 33: #
34: ###
1.1 harris41 35:
36: package Apache::groupsort;
37:
38: use strict;
39:
40: use Apache::Constants qw(:common);
41: use GDBM_File;
42:
1.2 harris41 43: my %hash; # variable to tie to user specific database
44: my $iconpath; # variable to be accessible to multiple subroutines
45:
46: # ---------------------------------------------------------------- Main Handler
1.1 harris41 47: sub handler {
48: my $r = shift;
1.2 harris41 49:
50: # color scheme
51: my $fileclr = '#ffffe6';
52: my $titleclr = '#ddffff';
53:
1.1 harris41 54: $r->content_type('text/html');
55: $r->send_http_header;
56: return OK if $r->header_only;
1.2 harris41 57:
58: # output start of web page
1.1 harris41 59: $r->print(<<END);
60: <html>
61: <head>
62: <title>The LearningOnline Network With CAPA Group Sorter</title>
63: <script language='javascript'>
64: function insertRowInLastRow() {
65: opener.insertrow(opener.maxrow);
66: opener.addobj(opener.maxrow,'e&2');
67: }
68: function placeResourceInLastRow (title,url,linkflag) {
1.2 harris41 69: opener.newresource(opener.maxrow,2,opener.escape(title),
70: opener.escape(url),'false','normal');
1.1 harris41 71: opener.save();
72: opener.mostrecent=opener.obj.length-1;
73: if (linkflag) {
74: opener.joinres(opener.linkmode,opener.mostrecent,0);
75: }
76: opener.linkmode=opener.mostrecent;
77: }
78: function finish_import() {
79: var linkflag=false;
80: for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.2 harris41 81: insertRowInLastRow();
82: placeResourceInLastRow(
83: eval("document.forms.groupsort.title"+num+".value"),
84: eval("document.forms.groupsort.filelink"+num+".value"),
85: linkflag
86: );
87: linkflag=true;
1.1 harris41 88: }
89: opener.editmode=0;
90: opener.notclear=0;
91: opener.linkmode=0;
92: opener.draw();
1.2 harris41 93: self.close();
1.1 harris41 94: }
95: function selectchange(val) {
1.3 harris41 96: var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
1.1 harris41 97: orderchange(val,newval);
98: }
99: function move(val,newval) {
100: orderchange(val,newval);
101: }
102: function orderchange(val,newval) {
103: document.forms.groupsort.oldval.value=val;
104: document.forms.groupsort.newval.value=newval;
105: document.forms.groupsort.submit();
106: }
107: </script>
108: </head>
109: <body bgcolor="#FFFFFF">
110: END
1.2 harris41 111:
112: # read pertinent machine configuration
1.1 harris41 113: my $domain = $r->dir_config('lonDefDomain');
1.2 harris41 114: $iconpath = $r->dir_config('lonIconsURL') . "/";
115:
116: my %shash; # sort order (key is resource location, value is sort order)
117: my %thash; # title (key is resource location, value is title)
1.5 harris41 118: foreach (split(/&/,$ENV{'QUERY_STRING'})) {
1.1 harris41 119: my ($name, $value) = split(/=/,$_);
120: $value =~ tr/+/ /;
121: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
122: if ($name eq 'acts') {
123: $ENV{'form.'.$name}=$value;
124: }
1.3 harris41 125: if ($name eq 'catalogmode') {
126: $ENV{'form.'.$name}=$value;
127: }
1.5 harris41 128: }
1.4 harris41 129: my $diropendb;
130: if ($ENV{'form.catalogmode'} eq 'groupsearch') {
131: $diropendb =
132: "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
133: }
134: elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
135: $diropendb =
136: "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
1.6 harris41 137: }
138: else { # choose last accessed
139: my $dsearch; my $dindex;
140: my $dsearcht; my $dindext;
141: $dsearch =
142: "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
143: if (-e $dsearch) {
144: $dsearcht=(stat($dsearch))[9];
145: }
146: $dindex =
147: "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
148: if (-e $dindex) {
149: $dindext=(stat($dindex))[9];
150: }
151: if (!$dsearcht and !$dindext) {
152: $diropendb='';
153: }
154: elsif ($dsearcht>$dindext) {
155: $diropendb=$dsearch;
156: }
157: else {
158: $diropendb=$dindex;
159: }
1.4 harris41 160: }
1.1 harris41 161: if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
1.2 harris41 162: my $acts = $ENV{'form.acts'};
163: my @Acts = split(/b/,$acts);
1.1 harris41 164: my %ahash;
165: my %achash;
1.2 harris41 166: my $ac = 0;
1.5 harris41 167: foreach (@Acts) {
1.2 harris41 168: my ($state,$ref) = split(/a/);
169: $ahash{$ref} = $state;
170: $achash{$ref} = $ac;
1.1 harris41 171: $ac++;
1.5 harris41 172: }
173: foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
1.2 harris41 174: my $key = $_;
1.1 harris41 175: if ($ahash{$key} eq '1') {
1.3 harris41 176: # my $keyz=join("<br />",keys %hash);
177: # print "<br />$key<br />$keyz".$hash{'pre_'.$key.'_link'}."<br />\n";
1.2 harris41 178: $hash{'store_'.$hash{'pre_'.$key.'_link'}} =
1.1 harris41 179: $hash{'pre_'.$key.'_title'};
1.2 harris41 180: $hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
1.1 harris41 181: $hash{'storectr'}+0;
182: $hash{'storectr'}++;
183: }
184: if ($ahash{$key} eq '0') {
185: if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
186: delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
187: }
188: }
1.5 harris41 189: }
190: foreach (keys %hash) {
1.1 harris41 191: if ($_ =~ /^store_/) {
1.2 harris41 192: my $key = $_;
193: $key =~ s/^store_//;
194: $shash{$key} = $hash{'storectr_'.$key};
195: $thash{$key} = $hash{'store_'.$key};
1.1 harris41 196: }
1.5 harris41 197: }
1.1 harris41 198: if ($ENV{'form.oldval'}) {
1.2 harris41 199: my $newctr = 0;
1.1 harris41 200: my %chash;
1.5 harris41 201: foreach (sort {$shash{$a} <=> $shash{$b}} (keys %shash)) {
1.2 harris41 202: my $key = $_;
1.1 harris41 203: $newctr++;
1.2 harris41 204: $shash{$key} = $newctr;
205: $hash{'storectr_'.$key} = $newctr;
206: $chash{$newctr} = $key;
1.5 harris41 207: }
1.2 harris41 208: my $oldval = $ENV{'form.oldval'};
209: my $newval = $ENV{'form.newval'};
210: if ($oldval != $newval) {
1.3 harris41 211: # when newval==0, then push down and delete
212: if ($newval!=0) {
213: $shash{$chash{$oldval}} = $newval;
214: $hash{'storectr_'.$chash{$oldval}} = $newval;
215: }
216: else {
217: $shash{$chash{$oldval}} = $newctr;
218: $hash{'storectr_'.$chash{$oldval}} = $newctr;
219: }
220: if ($newval==0) { # push down
221: my $newval2=$newctr;
222: for my $idx ($oldval..($newval2-1)) {
223: $shash{$chash{$idx+1}} = $idx;
224: $hash{'storectr_'.$chash{$idx+1}} = $idx;
225: }
226: delete $shash{$chash{$oldval}};
227: delete $hash{'storectr_'.$chash{$oldval}};
228: delete $hash{'store_'.$chash{$oldval}};
229: }
230: elsif ($oldval < $newval) { # push down
1.1 harris41 231: for my $idx ($oldval..($newval-1)) {
1.2 harris41 232: $shash{$chash{$idx+1}} = $idx;
233: $hash{'storectr_'.$chash{$idx+1}} = $idx;
1.1 harris41 234: }
235: }
1.2 harris41 236: elsif ($oldval > $newval) { # push up
1.1 harris41 237: for my $idx (reverse($newval..($oldval-1))) {
1.2 harris41 238: $shash{$chash{$idx}} = $idx+1;
239: $hash{'storectr_'.$chash{$idx}} = $idx+1;
1.1 harris41 240: }
241: }
242: }
243: }
244: } else {
1.2 harris41 245: $r->print('Unable to tie hash to db file</body></html>');
1.1 harris41 246: return OK;
247: }
248: untie %hash;
1.2 harris41 249: my $ctr = 0;
250: my $clen = scalar(keys %shash);
251: $r->print('<h2><font color="#888888">The LearningOnline With CAPA '.
252: 'Group Sorter</font></h2>'."\n");
253: $r->print('<b><font color="#888888">Finalize order of resources</font>'.
254: '</b>'."\n");
255: $r->print("<form method='post' action='/adm/groupsort' name='groupsort' ".
256: "enctype='application/x-www-form-urlencoded'>");
1.1 harris41 257: $r->print(<<END);
258: <input type="hidden" name="fnum" value="$clen" />
259: <input type="hidden" name="oldval" value="" />
260: <input type="hidden" name="newval" value="" />
1.3 harris41 261: END
262: if ($ENV{'form.catalogmode'} eq 'groupimport') {
263: $r->print(<<END);
1.2 harris41 264: <input type="button" name="alter" value="GO BACK"
265: onClick="window.location='/res/?catalogmode=groupimport'" />
1.3 harris41 266: END
267: }
268: if ($ENV{'form.catalogmode'} eq 'groupsearch') {
269: $r->print(<<END);
270: <input type="button" name="alter" value="GO BACK"
271: onClick="window.location='/adm/searchcat?catalogmode=groupsearch'" />
272: END
273: }
274: $r->print(<<END);
1.2 harris41 275: <input type="button" name="alter" value="FINISH IMPORT"
276: onClick="finish_import()" />
1.1 harris41 277: <input type="button" name="alter" value="CANCEL" onClick="self.close()" />
278: END
279: $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
280: $r->print("<table border=0><tr>\n");
1.2 harris41 281: $r->print("<td colspan='2' bgcolor='$titleclr'><b>Change order</b></td>".
282: "\n");
1.1 harris41 283: $r->print("<td colspan='2' bgcolor='$titleclr'><b>Title</b></td>\n");
284: $r->print("<td bgcolor='$titleclr'><b>Path</b></td></tr>\n");
1.5 harris41 285: foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
1.1 harris41 286: my $key=$_;
287: $ctr++;
288: my @file_ext = split(/\./,$key);
289: my $curfext = $file_ext[scalar(@file_ext)-1];
290: $r->print("<tr><td bgcolor='$fileclr'>");
291: $r->print(&movers($clen,$ctr));
292: $r->print(&hidden($ctr-1,$thash{$key},$key));
293: $r->print("</td><td bgcolor='$fileclr'>");
294: $r->print(&select_box($clen,$ctr));
295: $r->print("</td><td bgcolor='$fileclr'>");
296: $r->print("<img src='$iconpath$curfext.gif'>");
297: $r->print("</td><td bgcolor='$fileclr'>");
298: $r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
299: $r->print("$key</td></tr>\n");
1.5 harris41 300: }
1.1 harris41 301: $r->print("</table></td></tr></table></form>");
302: $r->print(<<END);
303: </body>
304: </html>
305: END
306: return OK;
307: }
308:
1.2 harris41 309: # --------------------------------------- Hidden values (returns scalar string)
1.1 harris41 310: sub hidden {
1.2 harris41 311: my ($sel,$title,$filelink) = @_;
312: my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
313: '" />';
314: $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
315: $filelink.'" />';
1.1 harris41 316: return $string;
317: }
318:
1.2 harris41 319: # --------------------------------------- Moving arrows (returns scalar string)
1.1 harris41 320: sub movers {
1.2 harris41 321: my ($total,$sel) = @_;
322: my $dsel = $sel-1;
323: my $usel = $sel+1;
324: $usel = 1 if $usel > $total;
325: $dsel = $total if $dsel < 1;
1.1 harris41 326: my $string;
1.2 harris41 327: $string = (<<END);
1.1 harris41 328: <table border='0' cellspacing='0' cellpadding='0'>
1.2 harris41 329: <tr><td><a href='javascript:move($sel,$dsel)'>
330: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
331: <tr><td><a href='javascript:move($sel,$usel)'>
332: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1 harris41 333: </table>
334: END
335: return $string;
336: }
1.2 harris41 337:
338: # ------------------------------------------ Select box (returns scalar string)
1.1 harris41 339: sub select_box {
1.2 harris41 340: my ($total,$sel) = @_;
1.1 harris41 341: my $string;
1.2 harris41 342: $string = '<select name="alt'.$sel.'"';
343: $string .= " onChange='selectchange($sel)'>";
1.3 harris41 344: $string .= "<option name='o0' value='0'>remove</option>";
1.1 harris41 345: for my $cur (1..$total) {
1.2 harris41 346: $string .= "<option name='o$cur' value='$cur'";
347: if ($cur == $sel) {
348: $string .= "selected";
1.1 harris41 349: }
1.2 harris41 350: $string .= ">$cur</option>";
1.1 harris41 351: }
1.2 harris41 352: $string .= "</select>\n";
1.1 harris41 353: return $string;
354: }
355:
356: 1;
357:
358: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>