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

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.32    ! www         5: # $Id: groupsort.pm,v 1.31 2005/04/07 06:56:22 albertel 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.6       harris41   30: # YEAR=2002
1.4       harris41   31: #
                     32: ###
1.1       harris41   33: 
                     34: package Apache::groupsort;
                     35: 
                     36: use strict;
                     37: 
                     38: use Apache::Constants qw(:common);
                     39: use GDBM_File;
1.15      www        40: use Apache::loncommon;
1.23      www        41: use Apache::lonlocal;
1.27      taceyjo1   42: use Apache::lonnet;
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.23      www        66:     &Apache::loncommon::content_type($r,'text/html');
1.1       harris41   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='';
1.31      albertel   72:     if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
1.8       www        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
1.30      albertel  108:     my $html=&Apache::lonxml::xmlbegin();
1.1       harris41  109:     $r->print(<<END);
1.30      albertel  110: $html
1.1       harris41  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.32    ! www       119:     opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
1.2       harris41  120: 		       opener.escape(url),'false','normal');
1.1       harris41  121:     opener.save();
                    122:     if (linkflag) {
                    123: 	opener.joinres(opener.linkmode,opener.mostrecent,0);
                    124:     }
                    125:     opener.linkmode=opener.mostrecent;
                    126: }
1.8       www       127: $finishimport
1.1       harris41  128: function selectchange(val) {
1.3       harris41  129:     var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
1.1       harris41  130:     orderchange(val,newval);
                    131: }
                    132: function move(val,newval) {
                    133:     orderchange(val,newval);
                    134: }
                    135: function orderchange(val,newval) {
                    136:     document.forms.groupsort.oldval.value=val;
                    137:     document.forms.groupsort.newval.value=newval;
                    138:     document.forms.groupsort.submit();
                    139: }
                    140: </script>
                    141: </head>
                    142: END
1.2       harris41  143:     # read pertinent machine configuration
1.1       harris41  144:     my $domain  = $r->dir_config('lonDefDomain');
1.2       harris41  145:     $iconpath = $r->dir_config('lonIconsURL') . "/";
                    146: 
                    147:     my %shash; # sort order (key is resource location, value is sort order)
                    148:     my %thash; # title (key is resource location, value is title)
1.9       www       149: 
1.4       harris41  150:     my $diropendb;
1.11      www       151: # ------------------------------ which file do we open? Easy if explictly given
1.31      albertel  152:     if ($env{'form.catalogmode'} eq 'groupsearch') {
1.4       harris41  153: 	$diropendb = 
1.31      albertel  154: 	    "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_searchcat.db";
1.4       harris41  155:     }
1.31      albertel  156:     elsif ($env{'form.catalogmode'} eq 'groupimport') {
1.4       harris41  157: 	$diropendb = 
1.31      albertel  158: 	    "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_indexer.db";
1.6       harris41  159:     }
1.31      albertel  160:     elsif ($env{'form.catalogmode'} eq 'groupsec') {
1.11      www       161: 	$diropendb = 
1.31      albertel  162: 	    "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_groupsec.db";
1.11      www       163:     }
                    164: # --------------------- not explicitly given, choose the one most recently used
1.6       harris41  165:     else { # choose last accessed
1.11      www       166:         my @dbfn;
                    167:         my @dbst;
                    168: 
                    169: 	$dbfn[0] =
1.31      albertel  170: 	    "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_searchcat.db";
1.11      www       171:         $dbst[0]=-1;
                    172: 	if (-e $dbfn[0]) {
                    173: 	    $dbst[0]=(stat($dbfn[0]))[9];
1.6       harris41  174: 	}
1.11      www       175: 	$dbfn[1] =
1.31      albertel  176:             "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_indexer.db";
1.11      www       177:         $dbst[1]=-1;
                    178: 	if (-e $dbfn[1]) {
                    179:             $dbst[1]=(stat($dbfn[1]))[9];
1.6       harris41  180:         }
1.11      www       181: 	$dbfn[2] =
1.31      albertel  182:             "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_groupsec.db";
1.11      www       183:         $dbst[2]=-1;
                    184: 	if (-e $dbfn[2]) {
                    185:             $dbst[2]=(stat($dbfn[2]))[9];
                    186:         }
                    187: # Expand here for more modes
                    188: # ....
                    189: 
                    190: # Okay, find most recent existing
                    191: 
                    192:         my $newest=0;
1.12      www       193:         $diropendb='';
1.11      www       194:         for (my $i=0; $i<=$#dbfn; $i++) {
                    195: 	    if ($dbst[$i]>$newest) {
                    196: 		$newest=$dbst[$i];
                    197:                 $diropendb=$dbfn[$i];
                    198:             }
1.6       harris41  199:         }
1.11      www       200: 
1.4       harris41  201:     }
1.11      www       202: # ----------------------------- diropendb is now the filename of the db to open
1.13      albertel  203:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.31      albertel  204: 	my $acts = $env{'form.acts'};
1.2       harris41  205: 	my @Acts = split(/b/,$acts);
1.1       harris41  206: 	my %ahash;
                    207: 	my %achash;
1.2       harris41  208: 	my $ac = 0;
1.5       harris41  209: 	foreach (@Acts) {
1.2       harris41  210: 	    my ($state,$ref) = split(/a/);
                    211: 	    $ahash{$ref} = $state;
                    212: 	    $achash{$ref} = $ac;
1.1       harris41  213: 	    $ac++;
1.5       harris41  214: 	}
                    215: 	foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
1.2       harris41  216: 	    my $key = $_;
1.1       harris41  217: 	    if ($ahash{$key} eq '1') {
1.3       harris41  218: #		my $keyz=join("<br />",keys %hash);
                    219: #		print "<br />$key<br />$keyz".$hash{'pre_'.$key.'_link'}."<br />\n";
1.2       harris41  220: 		$hash{'store_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41  221: 		    $hash{'pre_'.$key.'_title'};
1.2       harris41  222: 		$hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41  223: 		    $hash{'storectr'}+0;
                    224: 		$hash{'storectr'}++;
                    225: 	    }
                    226: 	    if ($ahash{$key} eq '0') {
                    227: 		if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
                    228: 		    delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
                    229: 		}
                    230: 	    }
1.5       harris41  231: 	}
                    232: 	foreach (keys %hash) {
1.1       harris41  233: 	    if ($_ =~ /^store_/) {
1.2       harris41  234: 		my $key = $_;
                    235: 		$key =~ s/^store_//;
                    236: 		$shash{$key} = $hash{'storectr_'.$key};
1.27      taceyjo1  237: 		if (&Apache::lonnet::gettitle($key) eq '') {
1.28      albertel  238: 		    $thash{$key} = $hash{'store_'.$key};
                    239: 		} else {
                    240: 		    $thash{$key} = &Apache::lonnet::gettitle($key);
                    241: 		}
1.1       harris41  242: 	    }
1.5       harris41  243: 	}
1.31      albertel  244: 	if ($env{'form.oldval'}) {
1.2       harris41  245: 	    my $newctr = 0;
1.1       harris41  246: 	    my %chash;
1.5       harris41  247: 	    foreach (sort {$shash{$a} <=> $shash{$b}} (keys %shash)) {
1.2       harris41  248: 		my $key = $_;
1.1       harris41  249: 		$newctr++;
1.2       harris41  250: 		$shash{$key} = $newctr;
                    251: 		$hash{'storectr_'.$key} = $newctr;
                    252: 		$chash{$newctr} = $key;
1.5       harris41  253: 	    }
1.31      albertel  254: 	    my $oldval = $env{'form.oldval'};
                    255: 	    my $newval = $env{'form.newval'};
1.2       harris41  256: 	    if ($oldval != $newval) {
1.3       harris41  257: 		# when newval==0, then push down and delete
                    258: 		if ($newval!=0) {
                    259: 		    $shash{$chash{$oldval}} = $newval;
                    260: 		    $hash{'storectr_'.$chash{$oldval}} = $newval;
                    261: 		}
                    262: 		else {
                    263: 		    $shash{$chash{$oldval}} = $newctr;
                    264: 		    $hash{'storectr_'.$chash{$oldval}} = $newctr;
                    265: 		}
                    266: 		if ($newval==0) { # push down
                    267: 		    my $newval2=$newctr;
                    268: 		    for my $idx ($oldval..($newval2-1)) {
                    269: 			$shash{$chash{$idx+1}} = $idx;
                    270: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
                    271: 		    }
                    272: 		    delete $shash{$chash{$oldval}};
                    273: 		    delete $hash{'storectr_'.$chash{$oldval}};
                    274: 		    delete $hash{'store_'.$chash{$oldval}};
                    275: 		}
                    276: 		elsif ($oldval < $newval) { # push down
1.1       harris41  277: 		    for my $idx ($oldval..($newval-1)) {
1.2       harris41  278: 			$shash{$chash{$idx+1}} = $idx;
                    279: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
1.1       harris41  280: 		    }
                    281: 		}
1.2       harris41  282: 		elsif ($oldval > $newval) { # push up
1.1       harris41  283: 		    for my $idx (reverse($newval..($oldval-1))) {
1.2       harris41  284: 			$shash{$chash{$idx}} = $idx+1;
                    285: 			$hash{'storectr_'.$chash{$idx}} = $idx+1;
1.1       harris41  286: 		    }
                    287: 		}
                    288: 	    }
                    289: 	}
                    290:     } else {
1.2       harris41  291: 	$r->print('Unable to tie hash to db file</body></html>');
1.1       harris41  292: 	return OK;
                    293:     }
                    294:     untie %hash;
1.2       harris41  295:     my $ctr = 0;
                    296:     my $clen = scalar(keys %shash);
1.22      albertel  297:     if ($clen > 1) {
1.23      www       298: 	my %lt=&Apache::lonlocal::texthash(
                    299: 		'fin'=> 'Finalize order of resources',
                    300: 		'gb' => 'Go Back',
                    301: 		'ns' => 'New Search',
                    302: 		'fi' => 'Finish Import',
                    303: 		'ca' => 'Cancel',
                    304: 		'co' => 'Change Order',
                    305: 		'ti' => 'Title',
                    306: 		'pa' => 'Path'
                    307: 		);
1.22      albertel  308: 	$r->print(&Apache::loncommon::bodytag('Sort Imported Resources'));
                    309: 	$r->print(<<END);
1.23      www       310: <b><font color="#888888">$lt{'fin'}</font></b>
1.14      matthew   311: <form method='post' action='/adm/groupsort' name='groupsort'
                    312:       enctype='application/x-www-form-urlencoded'>
1.1       harris41  313: <input type="hidden" name="fnum" value="$clen" />
                    314: <input type="hidden" name="oldval" value="" />
                    315: <input type="hidden" name="newval" value="" />
1.31      albertel  316: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.3       harris41  317: END
1.11      www       318: 
1.22      albertel  319:         # --- Expand here if "GO BACK" button desired
1.31      albertel  320:         if ($env{'form.catalogmode'} eq 'groupimport') {
1.29      matthew   321:             my $resurl = &Apache::loncommon::lastresurl();
1.22      albertel  322: 	    $r->print(<<END);
1.23      www       323: <input type="button" name="alter" value="$lt{'gb'}"
1.29      matthew   324:  onClick="window.location='$resurl?catalogmode=groupimport'" />&nbsp;
1.3       harris41  325: END
1.22      albertel  326:         }
1.31      albertel  327: 	if ($env{'form.catalogmode'} eq 'groupsearch') {
1.22      albertel  328: 	    $r->print(<<END);
1.23      www       329: <input type="button" name="alter" value="$lt{'ns'}"
1.16      matthew   330:  onClick="window.location='/adm/searchcat?catalogmode=groupsearch&cleargroupsort=1'" />&nbsp;
1.3       harris41  331: END
1.22      albertel  332:         }
                    333:         # ---
1.11      www       334: 
1.22      albertel  335: 	$r->print(<<END);
1.23      www       336: <input type="button" name="alter" value="$lt{'fi'}"
1.2       harris41  337:  onClick="finish_import()" />&nbsp;
1.23      www       338: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
1.1       harris41  339: END
1.22      albertel  340:         $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
                    341: 	$r->print("<table border=0><tr>\n");
1.23      www       342: 	$r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'co'}</b></td>\n");
                    343: 	$r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'ti'}</b></td>\n");
                    344: 	$r->print("<td bgcolor='$titleclr'><b>$lt{'pa'}</b></td></tr>\n");
1.22      albertel  345:     } else {
                    346: 	$r->print(<<END);
                    347: <body>
                    348: <form method='post' action='/adm/groupsort' name='groupsort'
                    349:       enctype='application/x-www-form-urlencoded'>
                    350: <input type="hidden" name="fnum" value="$clen" />
                    351: <input type="hidden" name="oldval" value="" />
                    352: <input type="hidden" name="newval" value="" />
1.31      albertel  353: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.22      albertel  354: END
                    355:     }
1.5       harris41  356:     foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
1.1       harris41  357: 	my $key=$_;
                    358: 	$ctr++;
1.25      albertel  359: 	my $iconname=&Apache::loncommon::icon($key);
1.22      albertel  360: 	if ($clen > 1) {
                    361: 	    $r->print("<tr><td bgcolor='$fileclr'>");
                    362: 	    $r->print(&movers($clen,$ctr));
                    363: 	}
1.1       harris41  364: 	$r->print(&hidden($ctr-1,$thash{$key},$key));
1.22      albertel  365: 	if ($clen > 1) {
                    366: 	    $r->print("</td><td bgcolor='$fileclr'>");
                    367: 	    $r->print(&select_box($clen,$ctr));
                    368: 	    $r->print("</td><td bgcolor='$fileclr'>");
1.26      albertel  369: 	    $r->print("<img src='$iconname' />");
1.22      albertel  370: 	    $r->print("</td><td bgcolor='$fileclr'>");
                    371: 	    $r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
                    372: 	    $r->print("$key</td></tr>\n");
                    373: 	} 
                    374:     }
                    375:     if ($clen > 1) {
                    376: 	$r->print("</table></td></tr></table></form>");
                    377:     } else {
                    378: 	$r->print(<<END);
                    379: <script type="text/javascript">
                    380:     finish_import();
                    381: </script>
                    382: END
                    383:     }
1.1       harris41  384:     $r->print(<<END);
                    385: </body>
                    386: </html>
                    387: END
1.22      albertel  388: 
1.1       harris41  389:     return OK;
                    390: }
                    391: 
1.2       harris41  392: # --------------------------------------- Hidden values (returns scalar string)
1.1       harris41  393: sub hidden {
1.2       harris41  394:     my ($sel,$title,$filelink) = @_;
                    395:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
                    396: 	'" />';
                    397:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
                    398: 	$filelink.'" />';
1.1       harris41  399:     return $string;
                    400: }
                    401: 
1.2       harris41  402: # --------------------------------------- Moving arrows (returns scalar string)
1.1       harris41  403: sub movers {
1.2       harris41  404:     my ($total,$sel) = @_;
                    405:     my $dsel = $sel-1;
                    406:     my $usel = $sel+1;
                    407:     $usel = 1 if $usel > $total;
                    408:     $dsel = $total if $dsel < 1;
1.1       harris41  409:     my $string;
1.2       harris41  410:     $string = (<<END);
1.1       harris41  411: <table border='0' cellspacing='0' cellpadding='0'>
1.2       harris41  412: <tr><td><a href='javascript:move($sel,$dsel)'>
                    413: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
                    414: <tr><td><a href='javascript:move($sel,$usel)'>
                    415: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1       harris41  416: </table>
                    417: END
                    418:     return $string;
                    419: }
1.2       harris41  420: 
                    421: # ------------------------------------------ Select box (returns scalar string)
1.1       harris41  422: sub select_box {
1.2       harris41  423:     my ($total,$sel) = @_;
1.1       harris41  424:     my $string;
1.2       harris41  425:     $string = '<select name="alt'.$sel.'"';
                    426:     $string .= " onChange='selectchange($sel)'>";
1.3       harris41  427:     $string .= "<option name='o0' value='0'>remove</option>";
1.1       harris41  428:     for my $cur (1..$total) {
1.2       harris41  429: 	$string .= "<option name='o$cur' value='$cur'";
                    430: 	if ($cur == $sel) {
                    431: 	    $string .= "selected";
1.1       harris41  432: 	}
1.2       harris41  433: 	$string .= ">$cur</option>";
1.1       harris41  434:     }
1.2       harris41  435:     $string .= "</select>\n";
1.1       harris41  436:     return $string;
                    437: }
                    438: 
                    439: 1;
                    440: 
                    441: __END__

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