File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.23: download - view: text, annotated - select for diffs
Tue Sep 23 00:26:10 2003 UTC (20 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
Internationalizing.

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

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