File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.10: download - view: text, annotated - select for diffs
Mon May 13 16:00:53 2002 UTC (22 years ago) by www
Branches: MAIN
CVS tags: HEAD
Now does import for Simple RAT

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

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