File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.6: download - view: text, annotated - select for diffs
Thu Jan 17 12:17:50 2002 UTC (22 years, 5 months ago) by harris41
Branches: MAIN
CVS tags: stable_2002_spring, HEAD
fixing bug (maybe want to use a different mechanism to track state information
in the future)

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

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