File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.17: download - view: text, annotated - select for diffs
Thu Jan 9 22:04:28 2003 UTC (21 years, 4 months ago) by www
Branches: MAIN
CVS tags: version_0_6_2, version_0_6_1, version_0_6, HEAD
Together with the cleanup hander on lonsearchcat.pm, we will hopefully never
see "cannot tie hash" again during group search and group import

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

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