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

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

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