File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.18: download - view: text, annotated - select for diffs
Tue Jan 14 18:47:50 2003 UTC (21 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Attempt to better catch and log untied hashes by usage of cleanup handlers.

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

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