File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.13: download - view: text, annotated - select for diffs
Mon Aug 12 18:21:42 2002 UTC (21 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- i think the last of bug#574

    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.13 2002/08/12 18:21:42 albertel 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: # ------------------------------ which file do we open? Easy if explictly given
  146:     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
  147: 	$diropendb = 
  148: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
  149:     }
  150:     elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
  151: 	$diropendb = 
  152: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
  153:     }
  154:     elsif ($ENV{'form.catalogmode'} eq 'groupsec') {
  155: 	$diropendb = 
  156: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_groupsec.db";
  157:     }
  158: # --------------------- not explicitly given, choose the one most recently used
  159:     else { # choose last accessed
  160:         my @dbfn;
  161:         my @dbst;
  162: 
  163: 	$dbfn[0] =
  164: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
  165:         $dbst[0]=-1;
  166: 	if (-e $dbfn[0]) {
  167: 	    $dbst[0]=(stat($dbfn[0]))[9];
  168: 	}
  169: 	$dbfn[1] =
  170:             "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
  171:         $dbst[1]=-1;
  172: 	if (-e $dbfn[1]) {
  173:             $dbst[1]=(stat($dbfn[1]))[9];
  174:         }
  175: 	$dbfn[2] =
  176:             "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_groupsec.db";
  177:         $dbst[2]=-1;
  178: 	if (-e $dbfn[2]) {
  179:             $dbst[2]=(stat($dbfn[2]))[9];
  180:         }
  181: # Expand here for more modes
  182: # ....
  183: 
  184: # Okay, find most recent existing
  185: 
  186:         my $newest=0;
  187:         $diropendb='';
  188:         for (my $i=0; $i<=$#dbfn; $i++) {
  189: 	    if ($dbst[$i]>$newest) {
  190: 		$newest=$dbst[$i];
  191:                 $diropendb=$dbfn[$i];
  192:             }
  193:         }
  194: 
  195:     }
  196: # ----------------------------- diropendb is now the filename of the db to open
  197:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
  198: 	my $acts = $ENV{'form.acts'};
  199: 	my @Acts = split(/b/,$acts);
  200: 	my %ahash;
  201: 	my %achash;
  202: 	my $ac = 0;
  203: 	foreach (@Acts) {
  204: 	    my ($state,$ref) = split(/a/);
  205: 	    $ahash{$ref} = $state;
  206: 	    $achash{$ref} = $ac;
  207: 	    $ac++;
  208: 	}
  209: 	foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
  210: 	    my $key = $_;
  211: 	    if ($ahash{$key} eq '1') {
  212: #		my $keyz=join("<br />",keys %hash);
  213: #		print "<br />$key<br />$keyz".$hash{'pre_'.$key.'_link'}."<br />\n";
  214: 		$hash{'store_'.$hash{'pre_'.$key.'_link'}} =
  215: 		    $hash{'pre_'.$key.'_title'};
  216: 		$hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
  217: 		    $hash{'storectr'}+0;
  218: 		$hash{'storectr'}++;
  219: 	    }
  220: 	    if ($ahash{$key} eq '0') {
  221: 		if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
  222: 		    delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
  223: 		}
  224: 	    }
  225: 	}
  226: 	foreach (keys %hash) {
  227: 	    if ($_ =~ /^store_/) {
  228: 		my $key = $_;
  229: 		$key =~ s/^store_//;
  230: 		$shash{$key} = $hash{'storectr_'.$key};
  231: 		$thash{$key} = $hash{'store_'.$key};
  232: 	    }
  233: 	}
  234: 	if ($ENV{'form.oldval'}) {
  235: 	    my $newctr = 0;
  236: 	    my %chash;
  237: 	    foreach (sort {$shash{$a} <=> $shash{$b}} (keys %shash)) {
  238: 		my $key = $_;
  239: 		$newctr++;
  240: 		$shash{$key} = $newctr;
  241: 		$hash{'storectr_'.$key} = $newctr;
  242: 		$chash{$newctr} = $key;
  243: 	    }
  244: 	    my $oldval = $ENV{'form.oldval'};
  245: 	    my $newval = $ENV{'form.newval'};
  246: 	    if ($oldval != $newval) {
  247: 		# when newval==0, then push down and delete
  248: 		if ($newval!=0) {
  249: 		    $shash{$chash{$oldval}} = $newval;
  250: 		    $hash{'storectr_'.$chash{$oldval}} = $newval;
  251: 		}
  252: 		else {
  253: 		    $shash{$chash{$oldval}} = $newctr;
  254: 		    $hash{'storectr_'.$chash{$oldval}} = $newctr;
  255: 		}
  256: 		if ($newval==0) { # push down
  257: 		    my $newval2=$newctr;
  258: 		    for my $idx ($oldval..($newval2-1)) {
  259: 			$shash{$chash{$idx+1}} = $idx;
  260: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
  261: 		    }
  262: 		    delete $shash{$chash{$oldval}};
  263: 		    delete $hash{'storectr_'.$chash{$oldval}};
  264: 		    delete $hash{'store_'.$chash{$oldval}};
  265: 		}
  266: 		elsif ($oldval < $newval) { # push down
  267: 		    for my $idx ($oldval..($newval-1)) {
  268: 			$shash{$chash{$idx+1}} = $idx;
  269: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
  270: 		    }
  271: 		}
  272: 		elsif ($oldval > $newval) { # push up
  273: 		    for my $idx (reverse($newval..($oldval-1))) {
  274: 			$shash{$chash{$idx}} = $idx+1;
  275: 			$hash{'storectr_'.$chash{$idx}} = $idx+1;
  276: 		    }
  277: 		}
  278: 	    }
  279: 	}
  280:     } else {
  281: 	$r->print('Unable to tie hash to db file</body></html>');
  282: 	return OK;
  283:     }
  284:     untie %hash;
  285:     my $ctr = 0;
  286:     my $clen = scalar(keys %shash);
  287:    $r->print('<h2><font color="#888888">The LearningOnline Network With CAPA '.
  288: 	      'Group Sorter</font></h2>'."\n");
  289:     $r->print('<b><font color="#888888">Finalize order of resources</font>'.
  290: 	      '</b>'."\n");
  291:     $r->print("<form method='post' action='/adm/groupsort' name='groupsort' ".
  292: 	      "enctype='application/x-www-form-urlencoded'>");
  293:     $r->print(<<END);
  294: <input type="hidden" name="fnum" value="$clen" />
  295: <input type="hidden" name="oldval" value="" />
  296: <input type="hidden" name="newval" value="" />
  297: END
  298: 
  299: # --- Expand here if "GO BACK" button desired
  300:     if ($ENV{'form.catalogmode'} eq 'groupimport') {
  301: 	$r->print(<<END);
  302: <input type="button" name="alter" value="GO BACK"
  303:  onClick="window.location='/res/?catalogmode=groupimport'" />&nbsp;
  304: END
  305:     }
  306:     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
  307: 	$r->print(<<END);
  308: <input type="button" name="alter" value="GO BACK"
  309:  onClick="window.location='/adm/searchcat?catalogmode=groupsearch'" />&nbsp;
  310: END
  311: }
  312: # ---
  313: 
  314:     $r->print(<<END);
  315: <input type="button" name="alter" value="FINISH IMPORT"
  316:  onClick="finish_import()" />&nbsp;
  317: <input type="button" name="alter" value="CANCEL" onClick="self.close()" />
  318: END
  319:     $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
  320:     $r->print("<table border=0><tr>\n");
  321:     $r->print("<td colspan='2' bgcolor='$titleclr'><b>Change order</b></td>".
  322: 	      "\n");
  323:     $r->print("<td colspan='2' bgcolor='$titleclr'><b>Title</b></td>\n");
  324:     $r->print("<td bgcolor='$titleclr'><b>Path</b></td></tr>\n");
  325:     foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
  326: 	my $key=$_;
  327: 	$ctr++;
  328: 	my @file_ext = split(/\./,$key);
  329: 	my $curfext = $file_ext[scalar(@file_ext)-1];
  330: 	$r->print("<tr><td bgcolor='$fileclr'>");
  331: 	$r->print(&movers($clen,$ctr));
  332: 	$r->print(&hidden($ctr-1,$thash{$key},$key));
  333: 	$r->print("</td><td bgcolor='$fileclr'>");
  334: 	$r->print(&select_box($clen,$ctr));
  335: 	$r->print("</td><td bgcolor='$fileclr'>");
  336: 	$r->print("<img src='$iconpath$curfext.gif'>");
  337: 	$r->print("</td><td bgcolor='$fileclr'>");
  338: 	$r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
  339: 	$r->print("$key</td></tr>\n");
  340:     } 
  341:     $r->print("</table></td></tr></table></form>");
  342:     $r->print(<<END);
  343: </body>
  344: </html>
  345: END
  346:     return OK;
  347: }
  348: 
  349: # --------------------------------------- Hidden values (returns scalar string)
  350: sub hidden {
  351:     my ($sel,$title,$filelink) = @_;
  352:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
  353: 	'" />';
  354:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
  355: 	$filelink.'" />';
  356:     return $string;
  357: }
  358: 
  359: # --------------------------------------- Moving arrows (returns scalar string)
  360: sub movers {
  361:     my ($total,$sel) = @_;
  362:     my $dsel = $sel-1;
  363:     my $usel = $sel+1;
  364:     $usel = 1 if $usel > $total;
  365:     $dsel = $total if $dsel < 1;
  366:     my $string;
  367:     $string = (<<END);
  368: <table border='0' cellspacing='0' cellpadding='0'>
  369: <tr><td><a href='javascript:move($sel,$dsel)'>
  370: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
  371: <tr><td><a href='javascript:move($sel,$usel)'>
  372: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
  373: </table>
  374: END
  375:     return $string;
  376: }
  377: 
  378: # ------------------------------------------ Select box (returns scalar string)
  379: sub select_box {
  380:     my ($total,$sel) = @_;
  381:     my $string;
  382:     $string = '<select name="alt'.$sel.'"';
  383:     $string .= " onChange='selectchange($sel)'>";
  384:     $string .= "<option name='o0' value='0'>remove</option>";
  385:     for my $cur (1..$total) {
  386: 	$string .= "<option name='o$cur' value='$cur'";
  387: 	if ($cur == $sel) {
  388: 	    $string .= "selected";
  389: 	}
  390: 	$string .= ">$cur</option>";
  391:     }
  392:     $string .= "</select>\n";
  393:     return $string;
  394: }
  395: 
  396: 1;
  397: 
  398: __END__

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