File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.7: download - view: text, annotated - select for diffs
Thu Mar 21 14:33:49 2002 UTC (22 years, 2 months ago) by www
Branches: MAIN
CVS tags: stable_2002_april, HEAD
Hopefully fixed crash problem on Macs

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

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