Annotation of loncom/interface/groupsort.pm, revision 1.20

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.20    ! albertel    5: # $Id: groupsort.pm,v 1.19 2003/02/03 18:03:52 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.6       harris41   30: # YEAR=2002
1.4       harris41   31: #
                     32: ###
1.1       harris41   33: 
                     34: package Apache::groupsort;
                     35: 
                     36: use strict;
                     37: 
                     38: use Apache::Constants qw(:common);
                     39: use GDBM_File;
1.15      www        40: use Apache::loncommon;
1.1       harris41   41: 
1.2       harris41   42: my %hash; # variable to tie to user specific database
                     43: my $iconpath; # variable to be accessible to multiple subroutines
1.17      www        44: 
                     45: sub cleanup {
1.18      www        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:     }
1.17      www        52: }
1.2       harris41   53: 
                     54: # ---------------------------------------------------------------- Main Handler
1.1       harris41   55: sub handler {
                     56:     my $r = shift;
1.9       www        57:  
                     58:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                     59:                                            ['acts','catalogmode','mode']);
1.2       harris41   60:     # color scheme
                     61:     my $fileclr = '#ffffe6';
                     62:     my $titleclr = '#ddffff';
                     63: 
1.1       harris41   64:     $r->content_type('text/html');
                     65:     $r->send_http_header;
                     66:     return OK if $r->header_only;
1.2       harris41   67: 
1.8       www        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() {
1.10      www        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();
1.8       www        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: 
1.1       harris41  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) {
1.2       harris41  117:     opener.newresource(opener.maxrow,2,opener.escape(title),
                    118: 		       opener.escape(url),'false','normal');
1.1       harris41  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: }
1.8       www       126: $finishimport
1.1       harris41  127: function selectchange(val) {
1.3       harris41  128:     var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
1.1       harris41  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
1.15      www       142:     $r->print(&Apache::loncommon::bodytag('Sort Imported Resources'));
1.2       harris41  143:     # read pertinent machine configuration
1.1       harris41  144:     my $domain  = $r->dir_config('lonDefDomain');
1.2       harris41  145:     $iconpath = $r->dir_config('lonIconsURL') . "/";
                    146: 
                    147:     my %shash; # sort order (key is resource location, value is sort order)
                    148:     my %thash; # title (key is resource location, value is title)
1.9       www       149: 
1.4       harris41  150:     my $diropendb;
1.11      www       151: # ------------------------------ which file do we open? Easy if explictly given
1.4       harris41  152:     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
                    153: 	$diropendb = 
                    154: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
                    155:     }
                    156:     elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
                    157: 	$diropendb = 
                    158: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
1.6       harris41  159:     }
1.11      www       160:     elsif ($ENV{'form.catalogmode'} eq 'groupsec') {
                    161: 	$diropendb = 
                    162: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_groupsec.db";
                    163:     }
                    164: # --------------------- not explicitly given, choose the one most recently used
1.6       harris41  165:     else { # choose last accessed
1.11      www       166:         my @dbfn;
                    167:         my @dbst;
                    168: 
                    169: 	$dbfn[0] =
1.6       harris41  170: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
1.11      www       171:         $dbst[0]=-1;
                    172: 	if (-e $dbfn[0]) {
                    173: 	    $dbst[0]=(stat($dbfn[0]))[9];
1.6       harris41  174: 	}
1.11      www       175: 	$dbfn[1] =
1.6       harris41  176:             "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
1.11      www       177:         $dbst[1]=-1;
                    178: 	if (-e $dbfn[1]) {
                    179:             $dbst[1]=(stat($dbfn[1]))[9];
1.6       harris41  180:         }
1.11      www       181: 	$dbfn[2] =
                    182:             "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_groupsec.db";
                    183:         $dbst[2]=-1;
                    184: 	if (-e $dbfn[2]) {
                    185:             $dbst[2]=(stat($dbfn[2]))[9];
                    186:         }
                    187: # Expand here for more modes
                    188: # ....
                    189: 
                    190: # Okay, find most recent existing
                    191: 
                    192:         my $newest=0;
1.12      www       193:         $diropendb='';
1.11      www       194:         for (my $i=0; $i<=$#dbfn; $i++) {
                    195: 	    if ($dbst[$i]>$newest) {
                    196: 		$newest=$dbst[$i];
                    197:                 $diropendb=$dbfn[$i];
                    198:             }
1.6       harris41  199:         }
1.11      www       200: 
1.4       harris41  201:     }
1.11      www       202: # ----------------------------- diropendb is now the filename of the db to open
1.13      albertel  203:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.2       harris41  204: 	my $acts = $ENV{'form.acts'};
                    205: 	my @Acts = split(/b/,$acts);
1.1       harris41  206: 	my %ahash;
                    207: 	my %achash;
1.2       harris41  208: 	my $ac = 0;
1.5       harris41  209: 	foreach (@Acts) {
1.2       harris41  210: 	    my ($state,$ref) = split(/a/);
                    211: 	    $ahash{$ref} = $state;
                    212: 	    $achash{$ref} = $ac;
1.1       harris41  213: 	    $ac++;
1.5       harris41  214: 	}
                    215: 	foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
1.2       harris41  216: 	    my $key = $_;
1.1       harris41  217: 	    if ($ahash{$key} eq '1') {
1.3       harris41  218: #		my $keyz=join("<br />",keys %hash);
                    219: #		print "<br />$key<br />$keyz".$hash{'pre_'.$key.'_link'}."<br />\n";
1.2       harris41  220: 		$hash{'store_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41  221: 		    $hash{'pre_'.$key.'_title'};
1.2       harris41  222: 		$hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41  223: 		    $hash{'storectr'}+0;
                    224: 		$hash{'storectr'}++;
                    225: 	    }
                    226: 	    if ($ahash{$key} eq '0') {
                    227: 		if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
                    228: 		    delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
                    229: 		}
                    230: 	    }
1.5       harris41  231: 	}
                    232: 	foreach (keys %hash) {
1.1       harris41  233: 	    if ($_ =~ /^store_/) {
1.2       harris41  234: 		my $key = $_;
                    235: 		$key =~ s/^store_//;
                    236: 		$shash{$key} = $hash{'storectr_'.$key};
                    237: 		$thash{$key} = $hash{'store_'.$key};
1.1       harris41  238: 	    }
1.5       harris41  239: 	}
1.1       harris41  240: 	if ($ENV{'form.oldval'}) {
1.2       harris41  241: 	    my $newctr = 0;
1.1       harris41  242: 	    my %chash;
1.5       harris41  243: 	    foreach (sort {$shash{$a} <=> $shash{$b}} (keys %shash)) {
1.2       harris41  244: 		my $key = $_;
1.1       harris41  245: 		$newctr++;
1.2       harris41  246: 		$shash{$key} = $newctr;
                    247: 		$hash{'storectr_'.$key} = $newctr;
                    248: 		$chash{$newctr} = $key;
1.5       harris41  249: 	    }
1.2       harris41  250: 	    my $oldval = $ENV{'form.oldval'};
                    251: 	    my $newval = $ENV{'form.newval'};
                    252: 	    if ($oldval != $newval) {
1.3       harris41  253: 		# when newval==0, then push down and delete
                    254: 		if ($newval!=0) {
                    255: 		    $shash{$chash{$oldval}} = $newval;
                    256: 		    $hash{'storectr_'.$chash{$oldval}} = $newval;
                    257: 		}
                    258: 		else {
                    259: 		    $shash{$chash{$oldval}} = $newctr;
                    260: 		    $hash{'storectr_'.$chash{$oldval}} = $newctr;
                    261: 		}
                    262: 		if ($newval==0) { # push down
                    263: 		    my $newval2=$newctr;
                    264: 		    for my $idx ($oldval..($newval2-1)) {
                    265: 			$shash{$chash{$idx+1}} = $idx;
                    266: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
                    267: 		    }
                    268: 		    delete $shash{$chash{$oldval}};
                    269: 		    delete $hash{'storectr_'.$chash{$oldval}};
                    270: 		    delete $hash{'store_'.$chash{$oldval}};
                    271: 		}
                    272: 		elsif ($oldval < $newval) { # push down
1.1       harris41  273: 		    for my $idx ($oldval..($newval-1)) {
1.2       harris41  274: 			$shash{$chash{$idx+1}} = $idx;
                    275: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
1.1       harris41  276: 		    }
                    277: 		}
1.2       harris41  278: 		elsif ($oldval > $newval) { # push up
1.1       harris41  279: 		    for my $idx (reverse($newval..($oldval-1))) {
1.2       harris41  280: 			$shash{$chash{$idx}} = $idx+1;
                    281: 			$hash{'storectr_'.$chash{$idx}} = $idx+1;
1.1       harris41  282: 		    }
                    283: 		}
                    284: 	    }
                    285: 	}
                    286:     } else {
1.2       harris41  287: 	$r->print('Unable to tie hash to db file</body></html>');
1.1       harris41  288: 	return OK;
                    289:     }
                    290:     untie %hash;
1.2       harris41  291:     my $ctr = 0;
                    292:     my $clen = scalar(keys %shash);
1.14      matthew   293:    $r->print(<<END);
                    294: <b><font color="#888888">Finalize order of resources</font></b>
                    295: <form method='post' action='/adm/groupsort' name='groupsort'
                    296:       enctype='application/x-www-form-urlencoded'>
1.1       harris41  297: <input type="hidden" name="fnum" value="$clen" />
                    298: <input type="hidden" name="oldval" value="" />
                    299: <input type="hidden" name="newval" value="" />
1.14      matthew   300: <input type="hidden" name="mode" value="$ENV{'form.mode'}" />
1.3       harris41  301: END
1.11      www       302: 
                    303: # --- Expand here if "GO BACK" button desired
1.3       harris41  304:     if ($ENV{'form.catalogmode'} eq 'groupimport') {
                    305: 	$r->print(<<END);
1.2       harris41  306: <input type="button" name="alter" value="GO BACK"
                    307:  onClick="window.location='/res/?catalogmode=groupimport'" />&nbsp;
1.3       harris41  308: END
                    309:     }
                    310:     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
                    311: 	$r->print(<<END);
1.16      matthew   312: <input type="button" name="alter" value="New Search"
                    313:  onClick="window.location='/adm/searchcat?catalogmode=groupsearch&cleargroupsort=1'" />&nbsp;
1.3       harris41  314: END
1.16      matthew   315:     }
1.11      www       316: # ---
                    317: 
1.3       harris41  318:     $r->print(<<END);
1.2       harris41  319: <input type="button" name="alter" value="FINISH IMPORT"
                    320:  onClick="finish_import()" />&nbsp;
1.1       harris41  321: <input type="button" name="alter" value="CANCEL" onClick="self.close()" />
                    322: END
                    323:     $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
                    324:     $r->print("<table border=0><tr>\n");
1.2       harris41  325:     $r->print("<td colspan='2' bgcolor='$titleclr'><b>Change order</b></td>".
                    326: 	      "\n");
1.1       harris41  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");
1.5       harris41  329:     foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
1.1       harris41  330: 	my $key=$_;
                    331: 	$ctr++;
                    332: 	my @file_ext = split(/\./,$key);
                    333: 	my $curfext = $file_ext[scalar(@file_ext)-1];
1.20    ! albertel  334: 	my $iconname="unknown.gif";
        !           335: 	my $embstyle = &Apache::loncommon::fileembstyle($curfext);
        !           336: 	# The unless conditional that follows is a bit of overkill
        !           337: 	$iconname = $curfext.".gif" unless
        !           338: 	    (!defined($embstyle) || $embstyle eq 'unk' || $embstyle eq 'hdn');
1.1       harris41  339: 	$r->print("<tr><td bgcolor='$fileclr'>");
                    340: 	$r->print(&movers($clen,$ctr));
                    341: 	$r->print(&hidden($ctr-1,$thash{$key},$key));
                    342: 	$r->print("</td><td bgcolor='$fileclr'>");
                    343: 	$r->print(&select_box($clen,$ctr));
                    344: 	$r->print("</td><td bgcolor='$fileclr'>");
1.20    ! albertel  345: 	$r->print("<img src='$iconpath$iconname'>");
1.1       harris41  346: 	$r->print("</td><td bgcolor='$fileclr'>");
                    347: 	$r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
                    348: 	$r->print("$key</td></tr>\n");
1.5       harris41  349:     } 
1.1       harris41  350:     $r->print("</table></td></tr></table></form>");
                    351:     $r->print(<<END);
                    352: </body>
                    353: </html>
                    354: END
                    355:     return OK;
                    356: }
                    357: 
1.2       harris41  358: # --------------------------------------- Hidden values (returns scalar string)
1.1       harris41  359: sub hidden {
1.2       harris41  360:     my ($sel,$title,$filelink) = @_;
                    361:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
                    362: 	'" />';
                    363:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
                    364: 	$filelink.'" />';
1.1       harris41  365:     return $string;
                    366: }
                    367: 
1.2       harris41  368: # --------------------------------------- Moving arrows (returns scalar string)
1.1       harris41  369: sub movers {
1.2       harris41  370:     my ($total,$sel) = @_;
                    371:     my $dsel = $sel-1;
                    372:     my $usel = $sel+1;
                    373:     $usel = 1 if $usel > $total;
                    374:     $dsel = $total if $dsel < 1;
1.1       harris41  375:     my $string;
1.2       harris41  376:     $string = (<<END);
1.1       harris41  377: <table border='0' cellspacing='0' cellpadding='0'>
1.2       harris41  378: <tr><td><a href='javascript:move($sel,$dsel)'>
                    379: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
                    380: <tr><td><a href='javascript:move($sel,$usel)'>
                    381: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1       harris41  382: </table>
                    383: END
                    384:     return $string;
                    385: }
1.2       harris41  386: 
                    387: # ------------------------------------------ Select box (returns scalar string)
1.1       harris41  388: sub select_box {
1.2       harris41  389:     my ($total,$sel) = @_;
1.1       harris41  390:     my $string;
1.2       harris41  391:     $string = '<select name="alt'.$sel.'"';
                    392:     $string .= " onChange='selectchange($sel)'>";
1.3       harris41  393:     $string .= "<option name='o0' value='0'>remove</option>";
1.1       harris41  394:     for my $cur (1..$total) {
1.2       harris41  395: 	$string .= "<option name='o$cur' value='$cur'";
                    396: 	if ($cur == $sel) {
                    397: 	    $string .= "selected";
1.1       harris41  398: 	}
1.2       harris41  399: 	$string .= ">$cur</option>";
1.1       harris41  400:     }
1.2       harris41  401:     $string .= "</select>\n";
1.1       harris41  402:     return $string;
                    403: }
                    404: 
                    405: 1;
                    406: 
                    407: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>