Annotation of loncom/interface/groupsort.pm, revision 1.17

1.1       harris41    1: # The LearningOnline Network with CAPA
1.4       harris41    2: # The LON-CAPA group sort handler
                      3: # Allows for sorting prior to import into RAT.
                      4: #
1.17    ! www         5: # $Id: groupsort.pm,v 1.16 2002/09/16 20:57:28 matthew Exp $
1.4       harris41    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.
1.1       harris41   20: #
1.4       harris41   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
1.1       harris41   26: #
1.4       harris41   27: # http://www.lon-capa.org/
1.1       harris41   28: #
                     29: # YEAR=2001
1.4       harris41   30: # 8/7,8/8,10/14,10/15,12/10 Scott Harrison
1.6       harris41   31: # YEAR=2002
                     32: # 1/17 Scott Harrison
1.4       harris41   33: #
                     34: ###
1.1       harris41   35: 
                     36: package Apache::groupsort;
                     37: 
                     38: use strict;
                     39: 
                     40: use Apache::Constants qw(:common);
                     41: use GDBM_File;
1.15      www        42: use Apache::loncommon;
1.1       harris41   43: 
1.2       harris41   44: my %hash; # variable to tie to user specific database
                     45: my $iconpath; # variable to be accessible to multiple subroutines
1.17    ! www        46: 
        !            47: sub cleanup {
        !            48:    untie(%hash) if (tied(%hash));
        !            49: }
1.2       harris41   50: 
                     51: # ---------------------------------------------------------------- Main Handler
1.1       harris41   52: sub handler {
                     53:     my $r = shift;
1.9       www        54:  
                     55:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                     56:                                            ['acts','catalogmode','mode']);
1.2       harris41   57:     # color scheme
                     58:     my $fileclr = '#ffffe6';
                     59:     my $titleclr = '#ddffff';
                     60: 
1.1       harris41   61:     $r->content_type('text/html');
                     62:     $r->send_http_header;
                     63:     return OK if $r->header_only;
1.2       harris41   64: 
1.8       www        65: # finish_import looks different for graphical or "simple" RAT
                     66:     my $finishimport='';
                     67:     if ($ENV{'form.mode'} eq 'simple') {
                     68:         $finishimport=(<<ENDSMP);
                     69: function finish_import() {
1.10      www        70:     opener.document.forms.simpleedit.importdetail.value='';
                     71:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
                     72: 	opener.document.forms.simpleedit.importdetail.value+='&'+
                     73:               escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
                     74: 	      escape(eval("document.forms.groupsort.filelink"+num+".value"));
                     75:     }
                     76:     opener.document.forms.simpleedit.submit();
                     77:     self.close();
1.8       www        78: }
                     79: ENDSMP
                     80:     } else {
                     81:         $finishimport=(<<ENDADV);
                     82: function finish_import() {
                     83:     var linkflag=false;
                     84:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
                     85: 	insertRowInLastRow();
                     86: 	placeResourceInLastRow(
                     87: 	       eval("document.forms.groupsort.title"+num+".value"),
                     88:  	       eval("document.forms.groupsort.filelink"+num+".value"),
                     89: 	       linkflag
                     90: 	);
                     91:         linkflag=true;
                     92:     }
                     93:     opener.editmode=0;
                     94:     opener.notclear=0;
                     95:     opener.linkmode=0;
                     96:     opener.draw();
                     97:     self.close();
                     98: }
                     99: ENDADV
                    100:     }
                    101: 
                    102: # output start of web page
                    103: 
1.1       harris41  104:     $r->print(<<END);
                    105: <html>
                    106: <head>
                    107: <title>The LearningOnline Network With CAPA Group Sorter</title>
                    108: <script language='javascript'>
                    109: function insertRowInLastRow() {
                    110:     opener.insertrow(opener.maxrow);
                    111:     opener.addobj(opener.maxrow,'e&2');
                    112: }
                    113: function placeResourceInLastRow (title,url,linkflag) {
1.2       harris41  114:     opener.newresource(opener.maxrow,2,opener.escape(title),
                    115: 		       opener.escape(url),'false','normal');
1.1       harris41  116:     opener.save();
                    117:     opener.mostrecent=opener.obj.length-1;
                    118:     if (linkflag) {
                    119: 	opener.joinres(opener.linkmode,opener.mostrecent,0);
                    120:     }
                    121:     opener.linkmode=opener.mostrecent;
                    122: }
1.8       www       123: $finishimport
1.1       harris41  124: function selectchange(val) {
1.3       harris41  125:     var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
1.1       harris41  126:     orderchange(val,newval);
                    127: }
                    128: function move(val,newval) {
                    129:     orderchange(val,newval);
                    130: }
                    131: function orderchange(val,newval) {
                    132:     document.forms.groupsort.oldval.value=val;
                    133:     document.forms.groupsort.newval.value=newval;
                    134:     document.forms.groupsort.submit();
                    135: }
                    136: </script>
                    137: </head>
                    138: END
1.15      www       139:     $r->print(&Apache::loncommon::bodytag('Sort Imported Resources'));
1.2       harris41  140:     # read pertinent machine configuration
1.1       harris41  141:     my $domain  = $r->dir_config('lonDefDomain');
1.2       harris41  142:     $iconpath = $r->dir_config('lonIconsURL') . "/";
                    143: 
                    144:     my %shash; # sort order (key is resource location, value is sort order)
                    145:     my %thash; # title (key is resource location, value is title)
1.9       www       146: 
1.4       harris41  147:     my $diropendb;
1.11      www       148: # ------------------------------ which file do we open? Easy if explictly given
1.4       harris41  149:     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
                    150: 	$diropendb = 
                    151: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
                    152:     }
                    153:     elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
                    154: 	$diropendb = 
                    155: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
1.6       harris41  156:     }
1.11      www       157:     elsif ($ENV{'form.catalogmode'} eq 'groupsec') {
                    158: 	$diropendb = 
                    159: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_groupsec.db";
                    160:     }
                    161: # --------------------- not explicitly given, choose the one most recently used
1.6       harris41  162:     else { # choose last accessed
1.11      www       163:         my @dbfn;
                    164:         my @dbst;
                    165: 
                    166: 	$dbfn[0] =
1.6       harris41  167: 	    "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_searchcat.db";
1.11      www       168:         $dbst[0]=-1;
                    169: 	if (-e $dbfn[0]) {
                    170: 	    $dbst[0]=(stat($dbfn[0]))[9];
1.6       harris41  171: 	}
1.11      www       172: 	$dbfn[1] =
1.6       harris41  173:             "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
1.11      www       174:         $dbst[1]=-1;
                    175: 	if (-e $dbfn[1]) {
                    176:             $dbst[1]=(stat($dbfn[1]))[9];
1.6       harris41  177:         }
1.11      www       178: 	$dbfn[2] =
                    179:             "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_groupsec.db";
                    180:         $dbst[2]=-1;
                    181: 	if (-e $dbfn[2]) {
                    182:             $dbst[2]=(stat($dbfn[2]))[9];
                    183:         }
                    184: # Expand here for more modes
                    185: # ....
                    186: 
                    187: # Okay, find most recent existing
                    188: 
                    189:         my $newest=0;
1.12      www       190:         $diropendb='';
1.11      www       191:         for (my $i=0; $i<=$#dbfn; $i++) {
                    192: 	    if ($dbst[$i]>$newest) {
                    193: 		$newest=$dbst[$i];
                    194:                 $diropendb=$dbfn[$i];
                    195:             }
1.6       harris41  196:         }
1.11      www       197: 
1.4       harris41  198:     }
1.11      www       199: # ----------------------------- diropendb is now the filename of the db to open
1.13      albertel  200:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.2       harris41  201: 	my $acts = $ENV{'form.acts'};
                    202: 	my @Acts = split(/b/,$acts);
1.1       harris41  203: 	my %ahash;
                    204: 	my %achash;
1.2       harris41  205: 	my $ac = 0;
1.5       harris41  206: 	foreach (@Acts) {
1.2       harris41  207: 	    my ($state,$ref) = split(/a/);
                    208: 	    $ahash{$ref} = $state;
                    209: 	    $achash{$ref} = $ac;
1.1       harris41  210: 	    $ac++;
1.5       harris41  211: 	}
                    212: 	foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
1.2       harris41  213: 	    my $key = $_;
1.1       harris41  214: 	    if ($ahash{$key} eq '1') {
1.3       harris41  215: #		my $keyz=join("<br />",keys %hash);
                    216: #		print "<br />$key<br />$keyz".$hash{'pre_'.$key.'_link'}."<br />\n";
1.2       harris41  217: 		$hash{'store_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41  218: 		    $hash{'pre_'.$key.'_title'};
1.2       harris41  219: 		$hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41  220: 		    $hash{'storectr'}+0;
                    221: 		$hash{'storectr'}++;
                    222: 	    }
                    223: 	    if ($ahash{$key} eq '0') {
                    224: 		if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
                    225: 		    delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
                    226: 		}
                    227: 	    }
1.5       harris41  228: 	}
                    229: 	foreach (keys %hash) {
1.1       harris41  230: 	    if ($_ =~ /^store_/) {
1.2       harris41  231: 		my $key = $_;
                    232: 		$key =~ s/^store_//;
                    233: 		$shash{$key} = $hash{'storectr_'.$key};
                    234: 		$thash{$key} = $hash{'store_'.$key};
1.1       harris41  235: 	    }
1.5       harris41  236: 	}
1.1       harris41  237: 	if ($ENV{'form.oldval'}) {
1.2       harris41  238: 	    my $newctr = 0;
1.1       harris41  239: 	    my %chash;
1.5       harris41  240: 	    foreach (sort {$shash{$a} <=> $shash{$b}} (keys %shash)) {
1.2       harris41  241: 		my $key = $_;
1.1       harris41  242: 		$newctr++;
1.2       harris41  243: 		$shash{$key} = $newctr;
                    244: 		$hash{'storectr_'.$key} = $newctr;
                    245: 		$chash{$newctr} = $key;
1.5       harris41  246: 	    }
1.2       harris41  247: 	    my $oldval = $ENV{'form.oldval'};
                    248: 	    my $newval = $ENV{'form.newval'};
                    249: 	    if ($oldval != $newval) {
1.3       harris41  250: 		# when newval==0, then push down and delete
                    251: 		if ($newval!=0) {
                    252: 		    $shash{$chash{$oldval}} = $newval;
                    253: 		    $hash{'storectr_'.$chash{$oldval}} = $newval;
                    254: 		}
                    255: 		else {
                    256: 		    $shash{$chash{$oldval}} = $newctr;
                    257: 		    $hash{'storectr_'.$chash{$oldval}} = $newctr;
                    258: 		}
                    259: 		if ($newval==0) { # push down
                    260: 		    my $newval2=$newctr;
                    261: 		    for my $idx ($oldval..($newval2-1)) {
                    262: 			$shash{$chash{$idx+1}} = $idx;
                    263: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
                    264: 		    }
                    265: 		    delete $shash{$chash{$oldval}};
                    266: 		    delete $hash{'storectr_'.$chash{$oldval}};
                    267: 		    delete $hash{'store_'.$chash{$oldval}};
                    268: 		}
                    269: 		elsif ($oldval < $newval) { # push down
1.1       harris41  270: 		    for my $idx ($oldval..($newval-1)) {
1.2       harris41  271: 			$shash{$chash{$idx+1}} = $idx;
                    272: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
1.1       harris41  273: 		    }
                    274: 		}
1.2       harris41  275: 		elsif ($oldval > $newval) { # push up
1.1       harris41  276: 		    for my $idx (reverse($newval..($oldval-1))) {
1.2       harris41  277: 			$shash{$chash{$idx}} = $idx+1;
                    278: 			$hash{'storectr_'.$chash{$idx}} = $idx+1;
1.1       harris41  279: 		    }
                    280: 		}
                    281: 	    }
                    282: 	}
                    283:     } else {
1.2       harris41  284: 	$r->print('Unable to tie hash to db file</body></html>');
1.1       harris41  285: 	return OK;
                    286:     }
                    287:     untie %hash;
1.2       harris41  288:     my $ctr = 0;
                    289:     my $clen = scalar(keys %shash);
1.14      matthew   290:    $r->print(<<END);
                    291: <b><font color="#888888">Finalize order of resources</font></b>
                    292: <form method='post' action='/adm/groupsort' name='groupsort'
                    293:       enctype='application/x-www-form-urlencoded'>
1.1       harris41  294: <input type="hidden" name="fnum" value="$clen" />
                    295: <input type="hidden" name="oldval" value="" />
                    296: <input type="hidden" name="newval" value="" />
1.14      matthew   297: <input type="hidden" name="mode" value="$ENV{'form.mode'}" />
1.3       harris41  298: END
1.11      www       299: 
                    300: # --- Expand here if "GO BACK" button desired
1.3       harris41  301:     if ($ENV{'form.catalogmode'} eq 'groupimport') {
                    302: 	$r->print(<<END);
1.2       harris41  303: <input type="button" name="alter" value="GO BACK"
                    304:  onClick="window.location='/res/?catalogmode=groupimport'" />&nbsp;
1.3       harris41  305: END
                    306:     }
                    307:     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
                    308: 	$r->print(<<END);
1.16      matthew   309: <input type="button" name="alter" value="New Search"
                    310:  onClick="window.location='/adm/searchcat?catalogmode=groupsearch&cleargroupsort=1'" />&nbsp;
1.3       harris41  311: END
1.16      matthew   312:     }
1.11      www       313: # ---
                    314: 
1.3       harris41  315:     $r->print(<<END);
1.2       harris41  316: <input type="button" name="alter" value="FINISH IMPORT"
                    317:  onClick="finish_import()" />&nbsp;
1.1       harris41  318: <input type="button" name="alter" value="CANCEL" onClick="self.close()" />
                    319: END
                    320:     $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
                    321:     $r->print("<table border=0><tr>\n");
1.2       harris41  322:     $r->print("<td colspan='2' bgcolor='$titleclr'><b>Change order</b></td>".
                    323: 	      "\n");
1.1       harris41  324:     $r->print("<td colspan='2' bgcolor='$titleclr'><b>Title</b></td>\n");
                    325:     $r->print("<td bgcolor='$titleclr'><b>Path</b></td></tr>\n");
1.5       harris41  326:     foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
1.1       harris41  327: 	my $key=$_;
                    328: 	$ctr++;
                    329: 	my @file_ext = split(/\./,$key);
                    330: 	my $curfext = $file_ext[scalar(@file_ext)-1];
                    331: 	$r->print("<tr><td bgcolor='$fileclr'>");
                    332: 	$r->print(&movers($clen,$ctr));
                    333: 	$r->print(&hidden($ctr-1,$thash{$key},$key));
                    334: 	$r->print("</td><td bgcolor='$fileclr'>");
                    335: 	$r->print(&select_box($clen,$ctr));
                    336: 	$r->print("</td><td bgcolor='$fileclr'>");
                    337: 	$r->print("<img src='$iconpath$curfext.gif'>");
                    338: 	$r->print("</td><td bgcolor='$fileclr'>");
                    339: 	$r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
                    340: 	$r->print("$key</td></tr>\n");
1.5       harris41  341:     } 
1.1       harris41  342:     $r->print("</table></td></tr></table></form>");
                    343:     $r->print(<<END);
                    344: </body>
                    345: </html>
                    346: END
                    347:     return OK;
                    348: }
                    349: 
1.2       harris41  350: # --------------------------------------- Hidden values (returns scalar string)
1.1       harris41  351: sub hidden {
1.2       harris41  352:     my ($sel,$title,$filelink) = @_;
                    353:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
                    354: 	'" />';
                    355:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
                    356: 	$filelink.'" />';
1.1       harris41  357:     return $string;
                    358: }
                    359: 
1.2       harris41  360: # --------------------------------------- Moving arrows (returns scalar string)
1.1       harris41  361: sub movers {
1.2       harris41  362:     my ($total,$sel) = @_;
                    363:     my $dsel = $sel-1;
                    364:     my $usel = $sel+1;
                    365:     $usel = 1 if $usel > $total;
                    366:     $dsel = $total if $dsel < 1;
1.1       harris41  367:     my $string;
1.2       harris41  368:     $string = (<<END);
1.1       harris41  369: <table border='0' cellspacing='0' cellpadding='0'>
1.2       harris41  370: <tr><td><a href='javascript:move($sel,$dsel)'>
                    371: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
                    372: <tr><td><a href='javascript:move($sel,$usel)'>
                    373: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1       harris41  374: </table>
                    375: END
                    376:     return $string;
                    377: }
1.2       harris41  378: 
                    379: # ------------------------------------------ Select box (returns scalar string)
1.1       harris41  380: sub select_box {
1.2       harris41  381:     my ($total,$sel) = @_;
1.1       harris41  382:     my $string;
1.2       harris41  383:     $string = '<select name="alt'.$sel.'"';
                    384:     $string .= " onChange='selectchange($sel)'>";
1.3       harris41  385:     $string .= "<option name='o0' value='0'>remove</option>";
1.1       harris41  386:     for my $cur (1..$total) {
1.2       harris41  387: 	$string .= "<option name='o$cur' value='$cur'";
                    388: 	if ($cur == $sel) {
                    389: 	    $string .= "selected";
1.1       harris41  390: 	}
1.2       harris41  391: 	$string .= ">$cur</option>";
1.1       harris41  392:     }
1.2       harris41  393:     $string .= "</select>\n";
1.1       harris41  394:     return $string;
                    395: }
                    396: 
                    397: 1;
                    398: 
                    399: __END__

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