File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.1: download - view: text, annotated - select for diffs
Wed Aug 8 03:00:54 2001 UTC (22 years, 11 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
phase two of importing a group of resources.. allows for
reordering

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

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