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

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.42    ! www         5: # $Id: groupsort.pm,v 1.41 2006/03/26 19:44:45 banghart 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: #
1.4       harris41   29: ###
1.1       harris41   30: 
                     31: package Apache::groupsort;
                     32: 
                     33: use strict;
                     34: 
                     35: use Apache::Constants qw(:common);
                     36: use GDBM_File;
1.15      www        37: use Apache::loncommon;
1.23      www        38: use Apache::lonlocal;
1.27      taceyjo1   39: use Apache::lonnet;
1.1       harris41   40: 
1.33      www        41: my $iconpath; # variable to be accessible to multiple subroutines
1.2       harris41   42: my %hash; # variable to tie to user specific database
1.8       www        43: 
1.2       harris41   44: 
1.33      www        45: sub readfromdb {
                     46:     my ($r,$shash,$thash)=@_;
1.9       www        47: 
1.42    ! www        48:     my $diropendb = 
        !            49:        "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_sel_res.db";
1.11      www        50: 
                     51: # ----------------------------- diropendb is now the filename of the db to open
1.13      albertel   52:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.31      albertel   53: 	my $acts = $env{'form.acts'};
1.2       harris41   54: 	my @Acts = split(/b/,$acts);
1.1       harris41   55: 	my %ahash;
                     56: 	my %achash;
1.2       harris41   57: 	my $ac = 0;
1.5       harris41   58: 	foreach (@Acts) {
1.2       harris41   59: 	    my ($state,$ref) = split(/a/);
                     60: 	    $ahash{$ref} = $state;
                     61: 	    $achash{$ref} = $ac;
1.1       harris41   62: 	    $ac++;
1.5       harris41   63: 	}
                     64: 	foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
1.2       harris41   65: 	    my $key = $_;
1.1       harris41   66: 	    if ($ahash{$key} eq '1') {
1.2       harris41   67: 		$hash{'store_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41   68: 		    $hash{'pre_'.$key.'_title'};
1.2       harris41   69: 		$hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
1.1       harris41   70: 		    $hash{'storectr'}+0;
                     71: 		$hash{'storectr'}++;
                     72: 	    }
                     73: 	    if ($ahash{$key} eq '0') {
                     74: 		if ($hash{'store_'.$hash{'pre_'.$key.'_link'}}) {
                     75: 		    delete $hash{'store_'.$hash{'pre_'.$key.'_link'}};
                     76: 		}
                     77: 	    }
1.5       harris41   78: 	}
                     79: 	foreach (keys %hash) {
1.1       harris41   80: 	    if ($_ =~ /^store_/) {
1.2       harris41   81: 		my $key = $_;
                     82: 		$key =~ s/^store_//;
1.33      www        83: 		$$shash{$key} = $hash{'storectr_'.$key};
1.27      taceyjo1   84: 		if (&Apache::lonnet::gettitle($key) eq '') {
1.33      www        85: 		    $$thash{$key} = $hash{'store_'.$key};
1.28      albertel   86: 		} else {
1.33      www        87: 		    $$thash{$key} = &Apache::lonnet::gettitle($key);
1.28      albertel   88: 		}
1.1       harris41   89: 	    }
1.5       harris41   90: 	}
1.31      albertel   91: 	if ($env{'form.oldval'}) {
1.2       harris41   92: 	    my $newctr = 0;
1.1       harris41   93: 	    my %chash;
1.33      www        94: 	    foreach (sort {$$shash{$a} <=> $$shash{$b}} (keys %{$shash})) {
1.2       harris41   95: 		my $key = $_;
1.1       harris41   96: 		$newctr++;
1.33      www        97: 		$$shash{$key} = $newctr;
1.2       harris41   98: 		$hash{'storectr_'.$key} = $newctr;
                     99: 		$chash{$newctr} = $key;
1.5       harris41  100: 	    }
1.31      albertel  101: 	    my $oldval = $env{'form.oldval'};
                    102: 	    my $newval = $env{'form.newval'};
1.2       harris41  103: 	    if ($oldval != $newval) {
1.3       harris41  104: 		# when newval==0, then push down and delete
                    105: 		if ($newval!=0) {
1.33      www       106: 		    $$shash{$chash{$oldval}} = $newval;
1.3       harris41  107: 		    $hash{'storectr_'.$chash{$oldval}} = $newval;
1.33      www       108: 		} else {
                    109: 		    $$shash{$chash{$oldval}} = $newctr;
1.3       harris41  110: 		    $hash{'storectr_'.$chash{$oldval}} = $newctr;
                    111: 		}
                    112: 		if ($newval==0) { # push down
                    113: 		    my $newval2=$newctr;
                    114: 		    for my $idx ($oldval..($newval2-1)) {
1.33      www       115: 			$$shash{$chash{$idx+1}} = $idx;
1.3       harris41  116: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
                    117: 		    }
1.33      www       118: 		    delete $$shash{$chash{$oldval}};
1.3       harris41  119: 		    delete $hash{'storectr_'.$chash{$oldval}};
                    120: 		    delete $hash{'store_'.$chash{$oldval}};
1.33      www       121: 		} elsif ($oldval < $newval) { # push down
1.1       harris41  122: 		    for my $idx ($oldval..($newval-1)) {
1.33      www       123: 			$$shash{$chash{$idx+1}} = $idx;
1.2       harris41  124: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
1.1       harris41  125: 		    }
1.33      www       126: 		} elsif ($oldval > $newval) { # push up
1.1       harris41  127: 		    for my $idx (reverse($newval..($oldval-1))) {
1.33      www       128: 			$$shash{$chash{$idx}} = $idx+1;
1.2       harris41  129: 			$hash{'storectr_'.$chash{$idx}} = $idx+1;
1.1       harris41  130: 		    }
                    131: 		}
                    132: 	    }
                    133: 	}
                    134:     } else {
1.34      www       135: 	$r->print('Unable to tie hash to db file');
1.1       harris41  136:     }
                    137:     untie %hash;
1.33      www       138:     return ($shash,$thash);
                    139: }
                    140: 
                    141: 
                    142: 
                    143: sub cleanup {
                    144:     if (tied(%hash)){
                    145: 	&Apache::lonnet::logthis('Cleanup groupsort: hash');
                    146:         unless (untie(%hash)) {
                    147: 	    &Apache::lonnet::logthis('Failed cleanup groupsort: hash');
                    148:         }
                    149:     }
1.39      albertel  150:     return OK;
1.33      www       151: }
                    152: 
1.34      www       153: # -------------------------------------------------------------- Read from file
                    154: 
                    155: sub readfromfile {
1.35      www       156:     my ($r,$shash,$thash,$nhash)=@_;
1.34      www       157:     my $cont=&Apache::lonnet::getfile
                    158: 	(&Apache::lonnet::filelocation('',$env{'form.readfile'}));
                    159:     if ($cont==-1) {
                    160: 	$r->print('Unable to read file: '.
                    161: 		  &Apache::lonnet::filelocation('',$env{'form.readfile'}));
                    162:     } else {
                    163:         my $parser = HTML::TokeParser->new(\$cont);
                    164:         my $token;
1.35      www       165: 	my $n=1;
1.34      www       166:         while ($token = $parser->get_token) {
                    167: 	    if ($token->[0] eq 'S') {
                    168:                 if ($token->[1] eq 'resource') {
                    169: 		    if ($env{'form.recover'}) {
                    170: 			if ($token->[2]->{'type'} ne 'zombie') { next; }
                    171: 		    } else {
                    172: 			if ($token->[2]->{'type'} eq 'zombie') { next; }
                    173: 		    }
1.35      www       174: 
                    175: 		    my $url=$token->[2]->{'src'};
1.34      www       176:                     my $name=$token->[2]->{'title'};
                    177: 		    $name=~s/ \[\((\d+)\,(\w+)\,(\w+)\)\]$//;
                    178: 		    if ($1) {
1.35      www       179: 			$$nhash{$url}='<br />'.&mt('Removed by ').
1.34      www       180: 			    &Apache::loncommon::plainname($2,$3).', '.
                    181: 			    &Apache::lonlocal::locallocaltime($1);
                    182: 		    }
1.37      www       183: 		    $name=~s/\&colon\;/\:/g;
1.35      www       184: 		    $$thash{$url}=$name;
                    185:                     $$shash{$url}=$n;
                    186:                     $n++;
1.34      www       187: 		}
                    188: 	    }
                    189: 	}
                    190:     }
                    191:     return ($shash,$thash);
                    192: }
                    193: 
1.33      www       194: # ---------------------------------------------------------------- Main Handler
                    195: sub handler {
                    196:     my $r = shift;
                    197:  
                    198:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.42    ! www       199: 				     ['acts','mode','readfile','recover']);
1.33      www       200:     # color scheme
                    201:     my $fileclr = '#ffffe6';
                    202:     my $titleclr = '#ddffff';
                    203: 
                    204:     &Apache::loncommon::content_type($r,'text/html');
                    205:     $r->send_http_header;
                    206:     return OK if $r->header_only;
                    207: 
                    208: # finish_import looks different for graphical or "simple" RAT
                    209:     my $finishimport='';
1.37      www       210:     my $begincondition='';
                    211:     my $endcondition='';
                    212:     if ($env{'form.readfile'}) {
                    213:         $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
                    214: 	$endcondition='}';
                    215:     }
1.33      www       216:     if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
                    217:         $finishimport=(<<ENDSMP);
                    218: function finish_import() {
                    219:     opener.document.forms.simpleedit.importdetail.value='';
                    220:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37      www       221: 	$begincondition
1.33      www       222: 	opener.document.forms.simpleedit.importdetail.value+='&'+
                    223:               escape(eval("document.forms.groupsort.title"+num+".value"))+'='+
                    224: 	      escape(eval("document.forms.groupsort.filelink"+num+".value"));
1.37      www       225: 	$endcondition
1.33      www       226:     }
                    227:     opener.document.forms.simpleedit.submit();
                    228:     self.close();
                    229: }
                    230: ENDSMP
                    231:     } else {
                    232:         $finishimport=(<<ENDADV);
                    233: function finish_import() {
                    234:     var linkflag=false;
                    235:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37      www       236: 	$begincondition
1.33      www       237: 	insertRowInLastRow();
                    238: 	placeResourceInLastRow(
                    239: 	       eval("document.forms.groupsort.title"+num+".value"),
                    240:  	       eval("document.forms.groupsort.filelink"+num+".value"),
                    241: 	       linkflag
                    242: 	);
                    243:         linkflag=true;
1.37      www       244: 	$endcondition
1.33      www       245:     }
                    246:     opener.editmode=0;
                    247:     opener.notclear=0;
                    248:     opener.linkmode=0;
                    249:     opener.draw();
                    250:     self.close();
                    251: }
                    252: ENDADV
                    253:     }
                    254: 
                    255: # output start of web page
1.40      albertel  256:     my $js = <<END;
                    257: <script type="text/javascript">
1.33      www       258: function insertRowInLastRow() {
                    259:     opener.insertrow(opener.maxrow);
                    260:     opener.addobj(opener.maxrow,'e&2');
                    261: }
                    262: function placeResourceInLastRow (title,url,linkflag) {
                    263:     opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
                    264: 		       opener.escape(url),'false','normal');
                    265:     opener.save();
                    266:     if (linkflag) {
                    267: 	opener.joinres(opener.linkmode,opener.mostrecent,0);
                    268:     }
                    269:     opener.linkmode=opener.mostrecent;
                    270: }
                    271: $finishimport
                    272: function selectchange(val) {
                    273:     var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
                    274:     orderchange(val,newval);
                    275: }
                    276: function move(val,newval) {
                    277:     orderchange(val,newval);
                    278: }
                    279: function orderchange(val,newval) {
                    280:     document.forms.groupsort.oldval.value=val;
                    281:     document.forms.groupsort.newval.value=newval;
                    282:     document.forms.groupsort.submit();
                    283: }
                    284: </script>
                    285: END
                    286:     # read pertinent machine configuration
                    287:     my $domain  = $r->dir_config('lonDefDomain');
                    288:     $iconpath = $r->dir_config('lonIconsURL') . "/";
                    289: 
                    290:     my %shash; # sort order (key is resource location, value is sort order)
                    291:     my %thash; # title (key is resource location, value is title)
1.35      www       292:     my %nhash; # notes (key is resource location);
1.33      www       293: 
1.34      www       294:     if ($env{'form.readfile'}) {
1.35      www       295: 	&readfromfile($r,\%shash,\%thash,\%nhash);
1.34      www       296:     } else {
                    297: 	&readfromdb($r,\%shash,\%thash);
                    298:     }
1.33      www       299: 
1.2       harris41  300:     my $ctr = 0;
                    301:     my $clen = scalar(keys %shash);
1.38      www       302:     if (($clen > 1) || ($env{'form.readfile'})) {
1.23      www       303: 	my %lt=&Apache::lonlocal::texthash(
                    304: 		'fin'=> 'Finalize order of resources',
1.42    ! www       305: 		'ci' => 'Continue Import',
        !           306: 		'cs' => 'Continue Search',
1.23      www       307: 		'fi' => 'Finish Import',
                    308: 		'ca' => 'Cancel',
                    309: 		'co' => 'Change Order',
                    310: 		'ti' => 'Title',
1.37      www       311: 		'pa' => 'Path',
                    312:                 'in' => 'Include'
1.23      www       313: 		);
1.40      albertel  314: 	$r->print(&Apache::loncommon::start_page('Sort Imported Resources',
                    315: 						 $js));
1.22      albertel  316: 	$r->print(<<END);
1.23      www       317: <b><font color="#888888">$lt{'fin'}</font></b>
1.14      matthew   318: <form method='post' action='/adm/groupsort' name='groupsort'
                    319:       enctype='application/x-www-form-urlencoded'>
1.1       harris41  320: <input type="hidden" name="fnum" value="$clen" />
                    321: <input type="hidden" name="oldval" value="" />
                    322: <input type="hidden" name="newval" value="" />
1.31      albertel  323: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.36      www       324: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
                    325: <input type="hidden" name="recover" value="$env{'form.recover'}" />
1.3       harris41  326: END
1.11      www       327: 
1.42    ! www       328:         # --- Continue Buttons
        !           329:         my $resurl = &Apache::loncommon::lastresurl();
        !           330:         $r->print(<<END);
        !           331: <input type="button" name="alter" value="$lt{'ci'}"
        !           332:  onClick="window.location='$resurl'" />&nbsp;
        !           333: <input type="button" name="altersearch" value="$lt{'cs'}"
        !           334:  onClick="window.location='/adm/searchcat'" />&nbsp;
1.3       harris41  335: END
1.22      albertel  336:         # ---
1.11      www       337: 
1.22      albertel  338: 	$r->print(<<END);
1.23      www       339: <input type="button" name="alter" value="$lt{'fi'}"
1.2       harris41  340:  onClick="finish_import()" />&nbsp;
1.23      www       341: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
1.1       harris41  342: END
1.22      albertel  343:         $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
                    344: 	$r->print("<table border=0><tr>\n");
1.37      www       345: 	if ($env{'form.readfile'}) { 
                    346: 	    $r->print("<td bgcolor='$titleclr'><b>$lt{'in'}</b></td>\n");
                    347: 	} else { 
                    348: 	    $r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'co'}</b></td>\n"); 
                    349: 	}
1.23      www       350: 	$r->print("<td colspan='2' bgcolor='$titleclr'><b>$lt{'ti'}</b></td>\n");
                    351: 	$r->print("<td bgcolor='$titleclr'><b>$lt{'pa'}</b></td></tr>\n");
1.22      albertel  352:     } else {
1.40      albertel  353: 	$r->print(&Apache::loncommon::start_page(undef,$js,
1.41      banghart  354: 						 {'only_body' => 1}));
1.22      albertel  355: 	$r->print(<<END);
                    356: <form method='post' action='/adm/groupsort' name='groupsort'
                    357:       enctype='application/x-www-form-urlencoded'>
                    358: <input type="hidden" name="fnum" value="$clen" />
                    359: <input type="hidden" name="oldval" value="" />
                    360: <input type="hidden" name="newval" value="" />
1.31      albertel  361: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.22      albertel  362: END
                    363:     }
1.5       harris41  364:     foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
1.1       harris41  365: 	my $key=$_;
                    366: 	$ctr++;
1.25      albertel  367: 	my $iconname=&Apache::loncommon::icon($key);
1.38      www       368: 	if (($clen > 1) || ($env{'form.readfile'})) {
1.22      albertel  369: 	    $r->print("<tr><td bgcolor='$fileclr'>");
1.37      www       370:             if ($env{'form.readfile'}) {
                    371: 		$r->print(&checkbox($ctr-1));
                    372: 	    } else {
                    373: 		$r->print(&movers($clen,$ctr));
                    374: 	    }
1.22      albertel  375: 	}
1.1       harris41  376: 	$r->print(&hidden($ctr-1,$thash{$key},$key));
1.38      www       377: 	if (($clen > 1)  || ($env{'form.readfile'})) {
1.37      www       378: 	    $r->print("</td>");
                    379:             unless ($env{'form.readfile'}) {
                    380: 		$r->print("<td bgcolor='$fileclr'>".
                    381: 			  &select_box($clen,$ctr).
                    382: 			  "</td>");
                    383: 	    }
                    384: 	    $r->print("<td bgcolor='$fileclr'>");
1.26      albertel  385: 	    $r->print("<img src='$iconname' />");
1.22      albertel  386: 	    $r->print("</td><td bgcolor='$fileclr'>");
1.36      www       387: 	    $r->print("$thash{$key}$nhash{$key}</td><td bgcolor='$fileclr'>\n");
1.22      albertel  388: 	    $r->print("$key</td></tr>\n");
                    389: 	} 
                    390:     }
1.38      www       391:     if (($clen > 1) || ($env{'form.readfile'})) {
1.22      albertel  392: 	$r->print("</table></td></tr></table></form>");
                    393:     } else {
                    394: 	$r->print(<<END);
                    395: <script type="text/javascript">
                    396:     finish_import();
                    397: </script>
                    398: END
                    399:     }
1.40      albertel  400: 
                    401:     $r->print(&Apache::loncommon::end_page());
1.22      albertel  402: 
1.1       harris41  403:     return OK;
                    404: }
                    405: 
1.2       harris41  406: # --------------------------------------- Hidden values (returns scalar string)
1.1       harris41  407: sub hidden {
1.2       harris41  408:     my ($sel,$title,$filelink) = @_;
                    409:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.$title.
                    410: 	'" />';
                    411:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
                    412: 	$filelink.'" />';
1.1       harris41  413:     return $string;
                    414: }
                    415: 
1.2       harris41  416: # --------------------------------------- Moving arrows (returns scalar string)
1.1       harris41  417: sub movers {
1.2       harris41  418:     my ($total,$sel) = @_;
                    419:     my $dsel = $sel-1;
                    420:     my $usel = $sel+1;
                    421:     $usel = 1 if $usel > $total;
                    422:     $dsel = $total if $dsel < 1;
1.1       harris41  423:     my $string;
1.2       harris41  424:     $string = (<<END);
1.1       harris41  425: <table border='0' cellspacing='0' cellpadding='0'>
1.2       harris41  426: <tr><td><a href='javascript:move($sel,$dsel)'>
                    427: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
                    428: <tr><td><a href='javascript:move($sel,$usel)'>
                    429: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1       harris41  430: </table>
                    431: END
                    432:     return $string;
                    433: }
1.2       harris41  434: 
                    435: # ------------------------------------------ Select box (returns scalar string)
1.1       harris41  436: sub select_box {
1.2       harris41  437:     my ($total,$sel) = @_;
1.1       harris41  438:     my $string;
1.2       harris41  439:     $string = '<select name="alt'.$sel.'"';
                    440:     $string .= " onChange='selectchange($sel)'>";
1.36      www       441:     $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
1.1       harris41  442:     for my $cur (1..$total) {
1.2       harris41  443: 	$string .= "<option name='o$cur' value='$cur'";
                    444: 	if ($cur == $sel) {
                    445: 	    $string .= "selected";
1.1       harris41  446: 	}
1.2       harris41  447: 	$string .= ">$cur</option>";
1.1       harris41  448:     }
1.2       harris41  449:     $string .= "</select>\n";
1.1       harris41  450:     return $string;
                    451: }
                    452: 
1.37      www       453: # ------------------------------------------------------------------- Checkbox
                    454: 
                    455: sub checkbox {
                    456:     my $sel=shift;
                    457:     return "<label><input type='checkbox' name='include$sel'".
                    458:        ($env{"form.include$sel"}?' checked="checked"':'').
                    459:        ' />'.&mt('Include').'</label>';
                    460: }
                    461: 
1.1       harris41  462: 1;
                    463: 
                    464: __END__

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