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

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

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