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

1.1       harris41    1: # The LearningOnline Network with CAPA
                      2: #
                      3: # The LON-CAPA group sort handler
                      4: #
                      5: # Allows for sorting prior to import into RAT.
                      6: #
                      7: # YEAR=2001
1.2     ! harris41    8: # 8/7,8/8 Scott Harrison
1.1       harris41    9: 
                     10: package Apache::groupsort;
                     11: 
                     12: use strict;
                     13: 
                     14: use Apache::Constants qw(:common);
                     15: use GDBM_File;
                     16: 
1.2     ! harris41   17: my %hash; # variable to tie to user specific database
        !            18: my $iconpath; # variable to be accessible to multiple subroutines
        !            19: 
        !            20: # ---------------------------------------------------------------- Main Handler
1.1       harris41   21: sub handler {
                     22:     my $r = shift;
1.2     ! harris41   23: 
        !            24:     # color scheme
        !            25:     my $fileclr = '#ffffe6';
        !            26:     my $titleclr = '#ddffff';
        !            27: 
1.1       harris41   28:     $r->content_type('text/html');
                     29:     $r->send_http_header;
                     30:     return OK if $r->header_only;
1.2     ! harris41   31: 
        !            32:     # output start of web page
1.1       harris41   33:     $r->print(<<END);
                     34: <html>
                     35: <head>
                     36: <title>The LearningOnline Network With CAPA Group Sorter</title>
                     37: <script language='javascript'>
                     38: function insertRowInLastRow() {
                     39:     opener.insertrow(opener.maxrow);
                     40:     opener.addobj(opener.maxrow,'e&2');
                     41: }
                     42: function placeResourceInLastRow (title,url,linkflag) {
1.2     ! harris41   43:     opener.newresource(opener.maxrow,2,opener.escape(title),
        !            44: 		       opener.escape(url),'false','normal');
1.1       harris41   45:     opener.save();
                     46:     opener.mostrecent=opener.obj.length-1;
                     47:     if (linkflag) {
                     48: 	opener.joinres(opener.linkmode,opener.mostrecent,0);
                     49:     }
                     50:     opener.linkmode=opener.mostrecent;
                     51: }
                     52: function finish_import() {
                     53:     var linkflag=false;
                     54:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.2     ! harris41   55: 	insertRowInLastRow();
        !            56: 	placeResourceInLastRow(
        !            57: 	       eval("document.forms.groupsort.title"+num+".value"),
        !            58:  	       eval("document.forms.groupsort.filelink"+num+".value"),
        !            59: 	       linkflag
        !            60: 	);
        !            61:         linkflag=true;
1.1       harris41   62:     }
                     63:     opener.editmode=0;
                     64:     opener.notclear=0;
                     65:     opener.linkmode=0;
                     66:     opener.infoclear();
                     67:     opener.draw();
1.2     ! harris41   68:     self.close();
1.1       harris41   69: }
                     70: function selectchange(val) {
                     71:     var newval=1+eval("document.forms.groupsort.alt"+val+".selectedIndex");
                     72:     orderchange(val,newval);
                     73: }
                     74: function move(val,newval) {
                     75:     orderchange(val,newval);
                     76: }
                     77: function orderchange(val,newval) {
                     78:     document.forms.groupsort.oldval.value=val;
                     79:     document.forms.groupsort.newval.value=newval;
                     80:     document.forms.groupsort.submit();
                     81: }
                     82: </script>
                     83: </head>
                     84: <body bgcolor="#FFFFFF">
                     85: END
1.2     ! harris41   86: 
        !            87:     # read pertinent machine configuration
1.1       harris41   88:     my $domain  = $r->dir_config('lonDefDomain');
1.2     ! harris41   89:     $iconpath = $r->dir_config('lonIconsURL') . "/";
1.1       harris41   90:     my $diropendb = "/home/httpd/perl/tmp/$domain\_$ENV{'user.name'}_indexer.db";
1.2     ! harris41   91: 
        !            92:     my %shash; # sort order (key is resource location, value is sort order)
        !            93:     my %thash; # title (key is resource location, value is title)
1.1       harris41   94:     map {
                     95:        my ($name, $value) = split(/=/,$_);
                     96:        $value =~ tr/+/ /;
                     97:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                     98:        if ($name eq 'acts') {
                     99:            $ENV{'form.'.$name}=$value;
                    100:        }
                    101:     } (split(/&/,$ENV{'QUERY_STRING'}));
                    102:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
1.2     ! harris41  103: 	my $acts = $ENV{'form.acts'};
        !           104: 	my @Acts = split(/b/,$acts);
1.1       harris41  105: 	my %ahash;
                    106: 	my %achash;
1.2     ! harris41  107: 	my $ac = 0;
1.1       harris41  108: 	map {
1.2     ! harris41  109: 	    my ($state,$ref) = split(/a/);
        !           110: 	    $ahash{$ref} = $state;
        !           111: 	    $achash{$ref} = $ac;
1.1       harris41  112: 	    $ac++;
                    113: 	} (@Acts);
                    114: 	map {
1.2     ! harris41  115: 	    my $key = $_;
1.1       harris41  116: 	    if ($ahash{$key} eq '1') {
1.2     ! harris41  117: 		$hash{'store_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41  118: 		    $hash{'pre_'.$key.'_title'};
1.2     ! harris41  119: 		$hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41  120: 		    $hash{'storectr'}+0;
                    121: 		$hash{'storectr'}++;
                    122: 	    }
                    123: 	    if ($ahash{$key} eq '0') {
                    124: 		if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
                    125: 		    delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
                    126: 		}
                    127: 	    }
1.2     ! harris41  128: 	} sort {$achash{$a} <=> $achash{$b}} (keys %ahash);
1.1       harris41  129: 	map {
                    130: 	    if ($_ =~ /^store_/) {
1.2     ! harris41  131: 		my $key = $_;
        !           132: 		$key =~ s/^store_//;
        !           133: 		$shash{$key} = $hash{'storectr_'.$key};
        !           134: 		$thash{$key} = $hash{'store_'.$key};
1.1       harris41  135: 	    }
                    136: 	} keys %hash;	
                    137: 	if ($ENV{'form.oldval'}) {
1.2     ! harris41  138: 	    my $newctr = 0;
1.1       harris41  139: 	    my %chash;
                    140: 	    map {
1.2     ! harris41  141: 		my $key = $_;
1.1       harris41  142: 		$newctr++;
1.2     ! harris41  143: 		$shash{$key} = $newctr;
        !           144: 		$hash{'storectr_'.$key} = $newctr;
        !           145: 		$chash{$newctr} = $key;
        !           146: 	    } sort {$shash{$a} <=> $shash{$b}} (keys %shash);
        !           147: 	    my $oldval = $ENV{'form.oldval'};
        !           148: 	    my $newval = $ENV{'form.newval'};
        !           149: 	    if ($oldval != $newval) {
        !           150: 		$shash{$chash{$oldval}} = $newval;
        !           151: 		$hash{'storectr_'.$chash{$oldval}} = $newval;
        !           152: 		if ($oldval < $newval) { # push down
1.1       harris41  153: 		    for my $idx ($oldval..($newval-1)) {
1.2     ! harris41  154: 			$shash{$chash{$idx+1}} = $idx;
        !           155: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
1.1       harris41  156: 		    }
                    157: 		}
1.2     ! harris41  158: 		elsif ($oldval > $newval) { # push up
1.1       harris41  159: 		    for my $idx (reverse($newval..($oldval-1))) {
1.2     ! harris41  160: 			$shash{$chash{$idx}} = $idx+1;
        !           161: 			$hash{'storectr_'.$chash{$idx}} = $idx+1;
1.1       harris41  162: 		    }
                    163: 		}
                    164: 	    }
                    165: 	}
                    166:     } else {
1.2     ! harris41  167: 	$r->print('Unable to tie hash to db file</body></html>');
1.1       harris41  168: 	return OK;
                    169:     }
                    170:     untie %hash;
1.2     ! harris41  171:     my $ctr = 0;
        !           172:     my $clen = scalar(keys %shash);
        !           173:     $r->print('<h2><font color="#888888">The LearningOnline With CAPA '.
        !           174: 	      'Group Sorter</font></h2>'."\n");
        !           175:     $r->print('<b><font color="#888888">Finalize order of resources</font>'.
        !           176: 	      '</b>'."\n");
        !           177:     $r->print("<form method='post' action='/adm/groupsort' name='groupsort' ".
        !           178: 	      "enctype='application/x-www-form-urlencoded'>");
1.1       harris41  179:     $r->print(<<END);
                    180: <input type="hidden" name="fnum" value="$clen" />
                    181: <input type="hidden" name="oldval" value="" />
                    182: <input type="hidden" name="newval" value="" />
1.2     ! harris41  183: <input type="button" name="alter" value="GO BACK"
        !           184:  onClick="window.location='/res/?catalogmode=groupimport'" />&nbsp;
        !           185: <input type="button" name="alter" value="FINISH IMPORT"
        !           186:  onClick="finish_import()" />&nbsp;
1.1       harris41  187: <input type="button" name="alter" value="CANCEL" onClick="self.close()" />
                    188: END
                    189:     $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
                    190:     $r->print("<table border=0><tr>\n");
1.2     ! harris41  191:     $r->print("<td colspan='2' bgcolor='$titleclr'><b>Change order</b></td>".
        !           192: 	      "\n");
1.1       harris41  193:     $r->print("<td colspan='2' bgcolor='$titleclr'><b>Title</b></td>\n");
                    194:     $r->print("<td bgcolor='$titleclr'><b>Path</b></td></tr>\n");
                    195:     map {
                    196: 	my $key=$_;
                    197: 	$ctr++;
                    198: 	my @file_ext = split(/\./,$key);
                    199: 	my $curfext = $file_ext[scalar(@file_ext)-1];
                    200: 	$r->print("<tr><td bgcolor='$fileclr'>");
                    201: 	$r->print(&movers($clen,$ctr));
                    202: 	$r->print(&hidden($ctr-1,$thash{$key},$key));
                    203: 	$r->print("</td><td bgcolor='$fileclr'>");
                    204: 	$r->print(&select_box($clen,$ctr));
                    205: 	$r->print("</td><td bgcolor='$fileclr'>");
                    206: 	$r->print("<img src='$iconpath$curfext.gif'>");
                    207: 	$r->print("</td><td bgcolor='$fileclr'>");
                    208: 	$r->print("$thash{$key}</td><td bgcolor='$fileclr'>\n");
                    209: 	$r->print("$key</td></tr>\n");
                    210:     } sort {$shash{$a}<=>$shash{$b}} (keys %shash);
                    211:     $r->print("</table></td></tr></table></form>");
                    212:     $r->print(<<END);
                    213: </body>
                    214: </html>
                    215: END
                    216:     return OK;
                    217: }
                    218: 
1.2     ! harris41  219: # --------------------------------------- Hidden values (returns scalar string)
1.1       harris41  220: sub hidden {
1.2     ! harris41  221:     my ($sel,$title,$filelink) = @_;
        !           222:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
        !           223: 	'" />';
        !           224:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
        !           225: 	$filelink.'" />';
1.1       harris41  226:     return $string;
                    227: }
                    228: 
1.2     ! harris41  229: # --------------------------------------- Moving arrows (returns scalar string)
1.1       harris41  230: sub movers {
1.2     ! harris41  231:     my ($total,$sel) = @_;
        !           232:     my $dsel = $sel-1;
        !           233:     my $usel = $sel+1;
        !           234:     $usel = 1 if $usel > $total;
        !           235:     $dsel = $total if $dsel < 1;
1.1       harris41  236:     my $string;
1.2     ! harris41  237:     $string = (<<END);
1.1       harris41  238: <table border='0' cellspacing='0' cellpadding='0'>
1.2     ! harris41  239: <tr><td><a href='javascript:move($sel,$dsel)'>
        !           240: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
        !           241: <tr><td><a href='javascript:move($sel,$usel)'>
        !           242: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1       harris41  243: </table>
                    244: END
                    245:     return $string;
                    246: }
1.2     ! harris41  247: 
        !           248: # ------------------------------------------ Select box (returns scalar string)
1.1       harris41  249: sub select_box {
1.2     ! harris41  250:     my ($total,$sel) = @_;
1.1       harris41  251:     my $string;
1.2     ! harris41  252:     $string = '<select name="alt'.$sel.'"';
        !           253:     $string .= " onChange='selectchange($sel)'>";
1.1       harris41  254:     for my $cur (1..$total) {
1.2     ! harris41  255: 	$string .= "<option name='o$cur' value='$cur'";
        !           256: 	if ($cur == $sel) {
        !           257: 	    $string .= "selected";
1.1       harris41  258: 	}
1.2     ! harris41  259: 	$string .= ">$cur</option>";
1.1       harris41  260:     }
1.2     ! harris41  261:     $string .= "</select>\n";
1.1       harris41  262:     return $string;
                    263: }
                    264: 
                    265: 1;
                    266: 
                    267: __END__

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