File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.2: download - view: text, annotated - select for diffs
Mon Aug 13 14:50:53 2001 UTC (22 years, 9 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
cleaning up code and fully implementing for the group import

    1: # The LearningOnline Network with CAPA
    2: #
    3: # The LON-CAPA group sort handler
    4: #
    5: # Allows for sorting prior to import into RAT.
    6: #
    7: # YEAR=2001
    8: # 8/7,8/8 Scott Harrison
    9: 
   10: package Apache::groupsort;
   11: 
   12: use strict;
   13: 
   14: use Apache::Constants qw(:common);
   15: use GDBM_File;
   16: 
   17: my %hash; # variable to tie to user specific database
   18: my $iconpath; # variable to be accessible to multiple subroutines
   19: 
   20: # ---------------------------------------------------------------- Main Handler
   21: sub handler {
   22:     my $r = shift;
   23: 
   24:     # color scheme
   25:     my $fileclr = '#ffffe6';
   26:     my $titleclr = '#ddffff';
   27: 
   28:     $r->content_type('text/html');
   29:     $r->send_http_header;
   30:     return OK if $r->header_only;
   31: 
   32:     # output start of web page
   33:     $r->print(<<END);
   34: <html>
   35: <head>
   36: <title>The LearningOnline Network With CAPA Group Sorter</title>
   37: <script language='javascript'>
   38: function insertRowInLastRow() {
   39:     opener.insertrow(opener.maxrow);
   40:     opener.addobj(opener.maxrow,'e&2');
   41: }
   42: function placeResourceInLastRow (title,url,linkflag) {
   43:     opener.newresource(opener.maxrow,2,opener.escape(title),
   44: 		       opener.escape(url),'false','normal');
   45:     opener.save();
   46:     opener.mostrecent=opener.obj.length-1;
   47:     if (linkflag) {
   48: 	opener.joinres(opener.linkmode,opener.mostrecent,0);
   49:     }
   50:     opener.linkmode=opener.mostrecent;
   51: }
   52: function finish_import() {
   53:     var linkflag=false;
   54:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
   55: 	insertRowInLastRow();
   56: 	placeResourceInLastRow(
   57: 	       eval("document.forms.groupsort.title"+num+".value"),
   58:  	       eval("document.forms.groupsort.filelink"+num+".value"),
   59: 	       linkflag
   60: 	);
   61:         linkflag=true;
   62:     }
   63:     opener.editmode=0;
   64:     opener.notclear=0;
   65:     opener.linkmode=0;
   66:     opener.infoclear();
   67:     opener.draw();
   68:     self.close();
   69: }
   70: function selectchange(val) {
   71:     var newval=1+eval("document.forms.groupsort.alt"+val+".selectedIndex");
   72:     orderchange(val,newval);
   73: }
   74: function move(val,newval) {
   75:     orderchange(val,newval);
   76: }
   77: function orderchange(val,newval) {
   78:     document.forms.groupsort.oldval.value=val;
   79:     document.forms.groupsort.newval.value=newval;
   80:     document.forms.groupsort.submit();
   81: }
   82: </script>
   83: </head>
   84: <body bgcolor="#FFFFFF">
   85: END
   86: 
   87:     # read pertinent machine configuration
   88:     my $domain  = $r->dir_config('lonDefDomain');
   89:     $iconpath = $r->dir_config('lonIconsURL') . "/";
   90:     my $diropendb = "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
   91: 
   92:     my %shash; # sort order (key is resource location, value is sort order)
   93:     my %thash; # title (key is resource location, value is title)
   94:     map {
   95:        my ($name, $value) = split(/=/,$_);
   96:        $value =~ tr/+/ /;
   97:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
   98:        if ($name eq 'acts') {
   99:            $ENV{'form.'.$name}=$value;
  100:        }
  101:     } (split(/&/,$ENV{'QUERY_STRING'}));
  102:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
  103: 	my $acts = $ENV{'form.acts'};
  104: 	my @Acts = split(/b/,$acts);
  105: 	my %ahash;
  106: 	my %achash;
  107: 	my $ac = 0;
  108: 	map {
  109: 	    my ($state,$ref) = split(/a/);
  110: 	    $ahash{$ref} = $state;
  111: 	    $achash{$ref} = $ac;
  112: 	    $ac++;
  113: 	} (@Acts);
  114: 	map {
  115: 	    my $key = $_;
  116: 	    if ($ahash{$key} eq '1') {
  117: 		$hash{'store_'.$hash{'pre_'.$key.'_link'}} =
  118: 		    $hash{'pre_'.$key.'_title'};
  119: 		$hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
  120: 		    $hash{'storectr'}+0;
  121: 		$hash{'storectr'}++;
  122: 	    }
  123: 	    if ($ahash{$key} eq '0') {
  124: 		if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
  125: 		    delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
  126: 		}
  127: 	    }
  128: 	} sort {$achash{$a} <=> $achash{$b}} (keys %ahash);
  129: 	map {
  130: 	    if ($_ =~ /^store_/) {
  131: 		my $key = $_;
  132: 		$key =~ s/^store_//;
  133: 		$shash{$key} = $hash{'storectr_'.$key};
  134: 		$thash{$key} = $hash{'store_'.$key};
  135: 	    }
  136: 	} keys %hash;	
  137: 	if ($ENV{'form.oldval'}) {
  138: 	    my $newctr = 0;
  139: 	    my %chash;
  140: 	    map {
  141: 		my $key = $_;
  142: 		$newctr++;
  143: 		$shash{$key} = $newctr;
  144: 		$hash{'storectr_'.$key} = $newctr;
  145: 		$chash{$newctr} = $key;
  146: 	    } sort {$shash{$a} <=> $shash{$b}} (keys %shash);
  147: 	    my $oldval = $ENV{'form.oldval'};
  148: 	    my $newval = $ENV{'form.newval'};
  149: 	    if ($oldval != $newval) {
  150: 		$shash{$chash{$oldval}} = $newval;
  151: 		$hash{'storectr_'.$chash{$oldval}} = $newval;
  152: 		if ($oldval < $newval) { # push down
  153: 		    for my $idx ($oldval..($newval-1)) {
  154: 			$shash{$chash{$idx+1}} = $idx;
  155: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
  156: 		    }
  157: 		}
  158: 		elsif ($oldval > $newval) { # push up
  159: 		    for my $idx (reverse($newval..($oldval-1))) {
  160: 			$shash{$chash{$idx}} = $idx+1;
  161: 			$hash{'storectr_'.$chash{$idx}} = $idx+1;
  162: 		    }
  163: 		}
  164: 	    }
  165: 	}
  166:     } else {
  167: 	$r->print('Unable to tie hash to db file</body></html>');
  168: 	return OK;
  169:     }
  170:     untie %hash;
  171:     my $ctr = 0;
  172:     my $clen = scalar(keys %shash);
  173:     $r->print('<h2><font color="#888888">The LearningOnline With CAPA '.
  174: 	      'Group Sorter</font></h2>'."\n");
  175:     $r->print('<b><font color="#888888">Finalize order of resources</font>'.
  176: 	      '</b>'."\n");
  177:     $r->print("<form method='post' action='/adm/groupsort' name='groupsort' ".
  178: 	      "enctype='application/x-www-form-urlencoded'>");
  179:     $r->print(<<END);
  180: <input type="hidden" name="fnum" value="$clen" />
  181: <input type="hidden" name="oldval" value="" />
  182: <input type="hidden" name="newval" value="" />
  183: <input type="button" name="alter" value="GO BACK"
  184:  onClick="window.location='/res/?catalogmode=groupimport'" />&nbsp;
  185: <input type="button" name="alter" value="FINISH IMPORT"
  186:  onClick="finish_import()" />&nbsp;
  187: <input type="button" name="alter" value="CANCEL" onClick="self.close()" />
  188: END
  189:     $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
  190:     $r->print("<table border=0><tr>\n");
  191:     $r->print("<td colspan='2' bgcolor='$titleclr'><b>Change order</b></td>".
  192: 	      "\n");
  193:     $r->print("<td colspan='2' bgcolor='$titleclr'><b>Title</b></td>\n");
  194:     $r->print("<td bgcolor='$titleclr'><b>Path</b></td></tr>\n");
  195:     map {
  196: 	my $key=$_;
  197: 	$ctr++;
  198: 	my @file_ext = split(/\./,$key);
  199: 	my $curfext = $file_ext[scalar(@file_ext)-1];
  200: 	$r->print("<tr><td bgcolor='$fileclr'>");
  201: 	$r->print(&movers($clen,$ctr));
  202: 	$r->print(&hidden($ctr-1,$thash{$key},$key));
  203: 	$r->print("</td><td bgcolor='$fileclr'>");
  204: 	$r->print(&select_box($clen,$ctr));
  205: 	$r->print("</td><td bgcolor='$fileclr'>");
  206: 	$r->print("<img src='$iconpath$curfext.gif'>");
  207: 	$r->print("</td><td bgcolor='$fileclr'>");
  208: 	$r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
  209: 	$r->print("$key</td></tr>\n");
  210:     } sort {$shash{$a}<=>$shash{$b}} (keys %shash);
  211:     $r->print("</table></td></tr></table></form>");
  212:     $r->print(<<END);
  213: </body>
  214: </html>
  215: END
  216:     return OK;
  217: }
  218: 
  219: # --------------------------------------- Hidden values (returns scalar string)
  220: sub hidden {
  221:     my ($sel,$title,$filelink) = @_;
  222:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
  223: 	'" />';
  224:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
  225: 	$filelink.'" />';
  226:     return $string;
  227: }
  228: 
  229: # --------------------------------------- Moving arrows (returns scalar string)
  230: sub movers {
  231:     my ($total,$sel) = @_;
  232:     my $dsel = $sel-1;
  233:     my $usel = $sel+1;
  234:     $usel = 1 if $usel > $total;
  235:     $dsel = $total if $dsel < 1;
  236:     my $string;
  237:     $string = (<<END);
  238: <table border='0' cellspacing='0' cellpadding='0'>
  239: <tr><td><a href='javascript:move($sel,$dsel)'>
  240: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
  241: <tr><td><a href='javascript:move($sel,$usel)'>
  242: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
  243: </table>
  244: END
  245:     return $string;
  246: }
  247: 
  248: # ------------------------------------------ Select box (returns scalar string)
  249: sub select_box {
  250:     my ($total,$sel) = @_;
  251:     my $string;
  252:     $string = '<select name="alt'.$sel.'"';
  253:     $string .= " onChange='selectchange($sel)'>";
  254:     for my $cur (1..$total) {
  255: 	$string .= "<option name='o$cur' value='$cur'";
  256: 	if ($cur == $sel) {
  257: 	    $string .= "selected";
  258: 	}
  259: 	$string .= ">$cur</option>";
  260:     }
  261:     $string .= "</select>\n";
  262:     return $string;
  263: }
  264: 
  265: 1;
  266: 
  267: __END__

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