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

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

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