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

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.26    ! albertel    5: # $Id: groupsort.pm,v 1.25 2003/12/30 20:47:23 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.1       harris41   42: 
1.2       harris41   43: my %hash; # variable to tie to user specific database
                     44: my $iconpath; # variable to be accessible to multiple subroutines
1.17      www        45: 
                     46: sub cleanup {
1.18      www        47:     if (tied(%hash)){
                     48: 	&Apache::lonnet::logthis('Cleanup groupsort: hash');
                     49:         unless (untie(%hash)) {
                     50: 	    &Apache::lonnet::logthis('Failed cleanup groupsort: hash');
                     51:         }
                     52:     }
1.17      www        53: }
1.2       harris41   54: 
                     55: # ---------------------------------------------------------------- Main Handler
1.1       harris41   56: sub handler {
                     57:     my $r = shift;
1.9       www        58:  
                     59:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                     60:                                            ['acts','catalogmode','mode']);
1.2       harris41   61:     # color scheme
                     62:     my $fileclr = '#ffffe6';
                     63:     my $titleclr = '#ddffff';
                     64: 
1.23      www        65:     &Apache::loncommon::content_type($r,'text/html');
1.1       harris41   66:     $r->send_http_header;
                     67:     return OK if $r->header_only;
1.2       harris41   68: 
1.8       www        69: # finish_import looks different for graphical or "simple" RAT
                     70:     my $finishimport='';
1.24      matthew    71:     if ($ENV{'form.mode'} eq 'simple' || $ENV{'form.mode'} eq '') {
1.8       www        72:         $finishimport=(<<ENDSMP);
                     73: function finish_import() {
1.10      www        74:     opener.document.forms.simpleedit.importdetail.value='';
                     75:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
                     76: 	opener.document.forms.simpleedit.importdetail.value+='&'+
                     77:               escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
                     78: 	      escape(eval("document.forms.groupsort.filelink"+num+".value"));
                     79:     }
                     80:     opener.document.forms.simpleedit.submit();
                     81:     self.close();
1.8       www        82: }
                     83: ENDSMP
                     84:     } else {
                     85:         $finishimport=(<<ENDADV);
                     86: function finish_import() {
                     87:     var linkflag=false;
                     88:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
                     89: 	insertRowInLastRow();
                     90: 	placeResourceInLastRow(
                     91: 	       eval("document.forms.groupsort.title"+num+".value"),
                     92:  	       eval("document.forms.groupsort.filelink"+num+".value"),
                     93: 	       linkflag
                     94: 	);
                     95:         linkflag=true;
                     96:     }
                     97:     opener.editmode=0;
                     98:     opener.notclear=0;
                     99:     opener.linkmode=0;
                    100:     opener.draw();
                    101:     self.close();
                    102: }
                    103: ENDADV
                    104:     }
                    105: 
                    106: # output start of web page
                    107: 
1.1       harris41  108:     $r->print(<<END);
                    109: <html>
                    110: <head>
                    111: <title>The LearningOnline Network With CAPA Group Sorter</title>
                    112: <script language='javascript'>
                    113: function insertRowInLastRow() {
                    114:     opener.insertrow(opener.maxrow);
                    115:     opener.addobj(opener.maxrow,'e&2');
                    116: }
                    117: function placeResourceInLastRow (title,url,linkflag) {
1.2       harris41  118:     opener.newresource(opener.maxrow,2,opener.escape(title),
                    119: 		       opener.escape(url),'false','normal');
1.1       harris41  120:     opener.save();
                    121:     opener.mostrecent=opener.obj.length-1;
                    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.4       harris41  152:     if ($ENV{'form.catalogmode'} eq 'groupsearch') {
                    153: 	$diropendb = 
1.21      albertel  154: 	    "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db";
1.4       harris41  155:     }
                    156:     elsif ($ENV{'form.catalogmode'} eq 'groupimport') {
                    157: 	$diropendb = 
1.21      albertel  158: 	    "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_indexer.db";
1.6       harris41  159:     }
1.11      www       160:     elsif ($ENV{'form.catalogmode'} eq 'groupsec') {
                    161: 	$diropendb = 
1.21      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.21      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.21      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.21      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.2       harris41  204: 	my $acts = $ENV{'form.acts'};
                    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};
                    237: 		$thash{$key} = $hash{'store_'.$key};
1.1       harris41  238: 	    }
1.5       harris41  239: 	}
1.1       harris41  240: 	if ($ENV{'form.oldval'}) {
1.2       harris41  241: 	    my $newctr = 0;
1.1       harris41  242: 	    my %chash;
1.5       harris41  243: 	    foreach (sort {$shash{$a} <=> $shash{$b}} (keys %shash)) {
1.2       harris41  244: 		my $key = $_;
1.1       harris41  245: 		$newctr++;
1.2       harris41  246: 		$shash{$key} = $newctr;
                    247: 		$hash{'storectr_'.$key} = $newctr;
                    248: 		$chash{$newctr} = $key;
1.5       harris41  249: 	    }
1.2       harris41  250: 	    my $oldval = $ENV{'form.oldval'};
                    251: 	    my $newval = $ENV{'form.newval'};
                    252: 	    if ($oldval != $newval) {
1.3       harris41  253: 		# when newval==0, then push down and delete
                    254: 		if ($newval!=0) {
                    255: 		    $shash{$chash{$oldval}} = $newval;
                    256: 		    $hash{'storectr_'.$chash{$oldval}} = $newval;
                    257: 		}
                    258: 		else {
                    259: 		    $shash{$chash{$oldval}} = $newctr;
                    260: 		    $hash{'storectr_'.$chash{$oldval}} = $newctr;
                    261: 		}
                    262: 		if ($newval==0) { # push down
                    263: 		    my $newval2=$newctr;
                    264: 		    for my $idx ($oldval..($newval2-1)) {
                    265: 			$shash{$chash{$idx+1}} = $idx;
                    266: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
                    267: 		    }
                    268: 		    delete $shash{$chash{$oldval}};
                    269: 		    delete $hash{'storectr_'.$chash{$oldval}};
                    270: 		    delete $hash{'store_'.$chash{$oldval}};
                    271: 		}
                    272: 		elsif ($oldval < $newval) { # push down
1.1       harris41  273: 		    for my $idx ($oldval..($newval-1)) {
1.2       harris41  274: 			$shash{$chash{$idx+1}} = $idx;
                    275: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
1.1       harris41  276: 		    }
                    277: 		}
1.2       harris41  278: 		elsif ($oldval > $newval) { # push up
1.1       harris41  279: 		    for my $idx (reverse($newval..($oldval-1))) {
1.2       harris41  280: 			$shash{$chash{$idx}} = $idx+1;
                    281: 			$hash{'storectr_'.$chash{$idx}} = $idx+1;
1.1       harris41  282: 		    }
                    283: 		}
                    284: 	    }
                    285: 	}
                    286:     } else {
1.2       harris41  287: 	$r->print('Unable to tie hash to db file</body></html>');
1.1       harris41  288: 	return OK;
                    289:     }
                    290:     untie %hash;
1.2       harris41  291:     my $ctr = 0;
                    292:     my $clen = scalar(keys %shash);
1.22      albertel  293:     if ($clen > 1) {
1.23      www       294: 	my %lt=&Apache::lonlocal::texthash(
                    295: 		'fin'=> 'Finalize order of resources',
                    296: 		'gb' => 'Go Back',
                    297: 		'ns' => 'New Search',
                    298: 		'fi' => 'Finish Import',
                    299: 		'ca' => 'Cancel',
                    300: 		'co' => 'Change Order',
                    301: 		'ti' => 'Title',
                    302: 		'pa' => 'Path'
                    303: 		);
1.22      albertel  304: 	$r->print(&Apache::loncommon::bodytag('Sort Imported Resources'));
                    305: 	$r->print(<<END);
1.23      www       306: <b><font color="#888888">$lt{'fin'}</font></b>
1.14      matthew   307: <form method='post' action='/adm/groupsort' name='groupsort'
                    308:       enctype='application/x-www-form-urlencoded'>
1.1       harris41  309: <input type="hidden" name="fnum" value="$clen" />
                    310: <input type="hidden" name="oldval" value="" />
                    311: <input type="hidden" name="newval" value="" />
1.14      matthew   312: <input type="hidden" name="mode" value="$ENV{'form.mode'}" />
1.3       harris41  313: END
1.11      www       314: 
1.22      albertel  315:         # --- Expand here if "GO BACK" button desired
                    316:         if ($ENV{'form.catalogmode'} eq 'groupimport') {
                    317: 	    $r->print(<<END);
1.23      www       318: <input type="button" name="alter" value="$lt{'gb'}"
1.2       harris41  319:  onClick="window.location='/res/?catalogmode=groupimport'" />&nbsp;
1.3       harris41  320: END
1.22      albertel  321:         }
                    322: 	if ($ENV{'form.catalogmode'} eq 'groupsearch') {
                    323: 	    $r->print(<<END);
1.23      www       324: <input type="button" name="alter" value="$lt{'ns'}"
1.16      matthew   325:  onClick="window.location='/adm/searchcat?catalogmode=groupsearch&cleargroupsort=1'" />&nbsp;
1.3       harris41  326: END
1.22      albertel  327:         }
                    328:         # ---
1.11      www       329: 
1.22      albertel  330: 	$r->print(<<END);
1.23      www       331: <input type="button" name="alter" value="$lt{'fi'}"
1.2       harris41  332:  onClick="finish_import()" />&nbsp;
1.23      www       333: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
1.1       harris41  334: END
1.22      albertel  335:         $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
                    336: 	$r->print("<table border=0><tr>\n");
1.23      www       337: 	$r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'co'}</b></td>\n");
                    338: 	$r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'ti'}</b></td>\n");
                    339: 	$r->print("<td bgcolor='$titleclr'><b>$lt{'pa'}</b></td></tr>\n");
1.22      albertel  340:     } else {
                    341: 	$r->print(<<END);
                    342: <body>
                    343: <form method='post' action='/adm/groupsort' name='groupsort'
                    344:       enctype='application/x-www-form-urlencoded'>
                    345: <input type="hidden" name="fnum" value="$clen" />
                    346: <input type="hidden" name="oldval" value="" />
                    347: <input type="hidden" name="newval" value="" />
                    348: <input type="hidden" name="mode" value="$ENV{'form.mode'}" />
                    349: END
                    350:     }
1.5       harris41  351:     foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
1.1       harris41  352: 	my $key=$_;
                    353: 	$ctr++;
1.25      albertel  354: 	my $iconname=&Apache::loncommon::icon($key);
1.22      albertel  355: 	if ($clen > 1) {
                    356: 	    $r->print("<tr><td bgcolor='$fileclr'>");
                    357: 	    $r->print(&movers($clen,$ctr));
                    358: 	}
1.1       harris41  359: 	$r->print(&hidden($ctr-1,$thash{$key},$key));
1.22      albertel  360: 	if ($clen > 1) {
                    361: 	    $r->print("</td><td bgcolor='$fileclr'>");
                    362: 	    $r->print(&select_box($clen,$ctr));
                    363: 	    $r->print("</td><td bgcolor='$fileclr'>");
1.26    ! albertel  364: 	    $r->print("<img src='$iconname' />");
1.22      albertel  365: 	    $r->print("</td><td bgcolor='$fileclr'>");
                    366: 	    $r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
                    367: 	    $r->print("$key</td></tr>\n");
                    368: 	} 
                    369:     }
                    370:     if ($clen > 1) {
                    371: 	$r->print("</table></td></tr></table></form>");
                    372:     } else {
                    373: 	$r->print(<<END);
                    374: <script type="text/javascript">
                    375:     finish_import();
                    376: </script>
                    377: END
                    378:     }
1.1       harris41  379:     $r->print(<<END);
                    380: </body>
                    381: </html>
                    382: END
1.22      albertel  383: 
1.1       harris41  384:     return OK;
                    385: }
                    386: 
1.2       harris41  387: # --------------------------------------- Hidden values (returns scalar string)
1.1       harris41  388: sub hidden {
1.2       harris41  389:     my ($sel,$title,$filelink) = @_;
                    390:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
                    391: 	'" />';
                    392:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
                    393: 	$filelink.'" />';
1.1       harris41  394:     return $string;
                    395: }
                    396: 
1.2       harris41  397: # --------------------------------------- Moving arrows (returns scalar string)
1.1       harris41  398: sub movers {
1.2       harris41  399:     my ($total,$sel) = @_;
                    400:     my $dsel = $sel-1;
                    401:     my $usel = $sel+1;
                    402:     $usel = 1 if $usel > $total;
                    403:     $dsel = $total if $dsel < 1;
1.1       harris41  404:     my $string;
1.2       harris41  405:     $string = (<<END);
1.1       harris41  406: <table border='0' cellspacing='0' cellpadding='0'>
1.2       harris41  407: <tr><td><a href='javascript:move($sel,$dsel)'>
                    408: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
                    409: <tr><td><a href='javascript:move($sel,$usel)'>
                    410: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1       harris41  411: </table>
                    412: END
                    413:     return $string;
                    414: }
1.2       harris41  415: 
                    416: # ------------------------------------------ Select box (returns scalar string)
1.1       harris41  417: sub select_box {
1.2       harris41  418:     my ($total,$sel) = @_;
1.1       harris41  419:     my $string;
1.2       harris41  420:     $string = '<select name="alt'.$sel.'"';
                    421:     $string .= " onChange='selectchange($sel)'>";
1.3       harris41  422:     $string .= "<option name='o0' value='0'>remove</option>";
1.1       harris41  423:     for my $cur (1..$total) {
1.2       harris41  424: 	$string .= "<option name='o$cur' value='$cur'";
                    425: 	if ($cur == $sel) {
                    426: 	    $string .= "selected";
1.1       harris41  427: 	}
1.2       harris41  428: 	$string .= ">$cur</option>";
1.1       harris41  429:     }
1.2       harris41  430:     $string .= "</select>\n";
1.1       harris41  431:     return $string;
                    432: }
                    433: 
                    434: 1;
                    435: 
                    436: __END__

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