File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.42: download - view: text, annotated - select for diffs
Thu Jun 8 07:20:41 2006 UTC (18 years ago) by www
Branches: MAIN
CVS tags: HEAD
Clean up: no need for three database files.
Next step: being able to flip back and forth between import and search

    1: # The LearningOnline Network with CAPA
    2: # The LON-CAPA group sort handler
    3: # Allows for sorting prior to import into RAT.
    4: #
    5: # $Id: groupsort.pm,v 1.42 2006/06/08 07:20:41 www Exp $
    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.
   20: #
   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
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: ###
   30: 
   31: package Apache::groupsort;
   32: 
   33: use strict;
   34: 
   35: use Apache::Constants qw(:common);
   36: use GDBM_File;
   37: use Apache::loncommon;
   38: use Apache::lonlocal;
   39: use Apache::lonnet;
   40: 
   41: my $iconpath; # variable to be accessible to multiple subroutines
   42: my %hash; # variable to tie to user specific database
   43: 
   44: 
   45: sub readfromdb {
   46:     my ($r,$shash,$thash)=@_;
   47: 
   48:     my $diropendb = 
   49:        "/home/httpd/perl/tmp/$env{'user.domain'}_$env{'user.name'}_sel_res.db";
   50: 
   51: # ----------------------------- diropendb is now the filename of the db to open
   52:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
   53: 	my $acts = $env{'form.acts'};
   54: 	my @Acts = split(/b/,$acts);
   55: 	my %ahash;
   56: 	my %achash;
   57: 	my $ac = 0;
   58: 	foreach (@Acts) {
   59: 	    my ($state,$ref) = split(/a/);
   60: 	    $ahash{$ref} = $state;
   61: 	    $achash{$ref} = $ac;
   62: 	    $ac++;
   63: 	}
   64: 	foreach (sort {$achash{$a} <=> $achash{$b}} (keys %ahash)) {
   65: 	    my $key = $_;
   66: 	    if ($ahash{$key} eq '1') {
   67: 		$hash{'store_'.$hash{'pre_'.$key.'_link'}} =
   68: 		    $hash{'pre_'.$key.'_title'};
   69: 		$hash{'storectr_'.$hash{'pre_'.$key.'_link'}} =
   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: 	    }
   78: 	}
   79: 	foreach (keys %hash) {
   80: 	    if ($_ =~ /^store_/) {
   81: 		my $key = $_;
   82: 		$key =~ s/^store_//;
   83: 		$$shash{$key} = $hash{'storectr_'.$key};
   84: 		if (&Apache::lonnet::gettitle($key) eq '') {
   85: 		    $$thash{$key} = $hash{'store_'.$key};
   86: 		} else {
   87: 		    $$thash{$key} = &Apache::lonnet::gettitle($key);
   88: 		}
   89: 	    }
   90: 	}
   91: 	if ($env{'form.oldval'}) {
   92: 	    my $newctr = 0;
   93: 	    my %chash;
   94: 	    foreach (sort {$$shash{$a} <=> $$shash{$b}} (keys %{$shash})) {
   95: 		my $key = $_;
   96: 		$newctr++;
   97: 		$$shash{$key} = $newctr;
   98: 		$hash{'storectr_'.$key} = $newctr;
   99: 		$chash{$newctr} = $key;
  100: 	    }
  101: 	    my $oldval = $env{'form.oldval'};
  102: 	    my $newval = $env{'form.newval'};
  103: 	    if ($oldval != $newval) {
  104: 		# when newval==0, then push down and delete
  105: 		if ($newval!=0) {
  106: 		    $$shash{$chash{$oldval}} = $newval;
  107: 		    $hash{'storectr_'.$chash{$oldval}} = $newval;
  108: 		} else {
  109: 		    $$shash{$chash{$oldval}} = $newctr;
  110: 		    $hash{'storectr_'.$chash{$oldval}} = $newctr;
  111: 		}
  112: 		if ($newval==0) { # push down
  113: 		    my $newval2=$newctr;
  114: 		    for my $idx ($oldval..($newval2-1)) {
  115: 			$$shash{$chash{$idx+1}} = $idx;
  116: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
  117: 		    }
  118: 		    delete $$shash{$chash{$oldval}};
  119: 		    delete $hash{'storectr_'.$chash{$oldval}};
  120: 		    delete $hash{'store_'.$chash{$oldval}};
  121: 		} elsif ($oldval < $newval) { # push down
  122: 		    for my $idx ($oldval..($newval-1)) {
  123: 			$$shash{$chash{$idx+1}} = $idx;
  124: 			$hash{'storectr_'.$chash{$idx+1}} = $idx;
  125: 		    }
  126: 		} elsif ($oldval > $newval) { # push up
  127: 		    for my $idx (reverse($newval..($oldval-1))) {
  128: 			$$shash{$chash{$idx}} = $idx+1;
  129: 			$hash{'storectr_'.$chash{$idx}} = $idx+1;
  130: 		    }
  131: 		}
  132: 	    }
  133: 	}
  134:     } else {
  135: 	$r->print('Unable to tie hash to db file');
  136:     }
  137:     untie %hash;
  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:     }
  150:     return OK;
  151: }
  152: 
  153: # -------------------------------------------------------------- Read from file
  154: 
  155: sub readfromfile {
  156:     my ($r,$shash,$thash,$nhash)=@_;
  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;
  165: 	my $n=1;
  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: 		    }
  174: 
  175: 		    my $url=$token->[2]->{'src'};
  176:                     my $name=$token->[2]->{'title'};
  177: 		    $name=~s/ \[\((\d+)\,(\w+)\,(\w+)\)\]$//;
  178: 		    if ($1) {
  179: 			$$nhash{$url}='<br />'.&mt('Removed by ').
  180: 			    &Apache::loncommon::plainname($2,$3).', '.
  181: 			    &Apache::lonlocal::locallocaltime($1);
  182: 		    }
  183: 		    $name=~s/\&colon\;/\:/g;
  184: 		    $$thash{$url}=$name;
  185:                     $$shash{$url}=$n;
  186:                     $n++;
  187: 		}
  188: 	    }
  189: 	}
  190:     }
  191:     return ($shash,$thash);
  192: }
  193: 
  194: # ---------------------------------------------------------------- Main Handler
  195: sub handler {
  196:     my $r = shift;
  197:  
  198:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  199: 				     ['acts','mode','readfile','recover']);
  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='';
  210:     my $begincondition='';
  211:     my $endcondition='';
  212:     if ($env{'form.readfile'}) {
  213:         $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
  214: 	$endcondition='}';
  215:     }
  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++) {
  221: 	$begincondition
  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"));
  225: 	$endcondition
  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++) {
  236: 	$begincondition
  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;
  244: 	$endcondition
  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
  256:     my $js = <<END;
  257: <script type="text/javascript">
  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)
  292:     my %nhash; # notes (key is resource location);
  293: 
  294:     if ($env{'form.readfile'}) {
  295: 	&readfromfile($r,\%shash,\%thash,\%nhash);
  296:     } else {
  297: 	&readfromdb($r,\%shash,\%thash);
  298:     }
  299: 
  300:     my $ctr = 0;
  301:     my $clen = scalar(keys %shash);
  302:     if (($clen > 1) || ($env{'form.readfile'})) {
  303: 	my %lt=&Apache::lonlocal::texthash(
  304: 		'fin'=> 'Finalize order of resources',
  305: 		'ci' => 'Continue Import',
  306: 		'cs' => 'Continue Search',
  307: 		'fi' => 'Finish Import',
  308: 		'ca' => 'Cancel',
  309: 		'co' => 'Change Order',
  310: 		'ti' => 'Title',
  311: 		'pa' => 'Path',
  312:                 'in' => 'Include'
  313: 		);
  314: 	$r->print(&Apache::loncommon::start_page('Sort Imported Resources',
  315: 						 $js));
  316: 	$r->print(<<END);
  317: <b><font color="#888888">$lt{'fin'}</font></b>
  318: <form method='post' action='/adm/groupsort' name='groupsort'
  319:       enctype='application/x-www-form-urlencoded'>
  320: <input type="hidden" name="fnum" value="$clen" />
  321: <input type="hidden" name="oldval" value="" />
  322: <input type="hidden" name="newval" value="" />
  323: <input type="hidden" name="mode" value="$env{'form.mode'}" />
  324: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
  325: <input type="hidden" name="recover" value="$env{'form.recover'}" />
  326: END
  327: 
  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;
  335: END
  336:         # ---
  337: 
  338: 	$r->print(<<END);
  339: <input type="button" name="alter" value="$lt{'fi'}"
  340:  onClick="finish_import()" />&nbsp;
  341: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
  342: END
  343:         $r->print("<table border='0'><tr><td bgcolor='#eeeeee'>");
  344: 	$r->print("<table border=0><tr>\n");
  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: 	}
  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");
  352:     } else {
  353: 	$r->print(&Apache::loncommon::start_page(undef,$js,
  354: 						 {'only_body' => 1}));
  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="" />
  361: <input type="hidden" name="mode" value="$env{'form.mode'}" />
  362: END
  363:     }
  364:     foreach (sort {$shash{$a}<=>$shash{$b}} (keys %shash)) {
  365: 	my $key=$_;
  366: 	$ctr++;
  367: 	my $iconname=&Apache::loncommon::icon($key);
  368: 	if (($clen > 1) || ($env{'form.readfile'})) {
  369: 	    $r->print("<tr><td bgcolor='$fileclr'>");
  370:             if ($env{'form.readfile'}) {
  371: 		$r->print(&checkbox($ctr-1));
  372: 	    } else {
  373: 		$r->print(&movers($clen,$ctr));
  374: 	    }
  375: 	}
  376: 	$r->print(&hidden($ctr-1,$thash{$key},$key));
  377: 	if (($clen > 1)  || ($env{'form.readfile'})) {
  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'>");
  385: 	    $r->print("<img src='$iconname' />");
  386: 	    $r->print("</td><td bgcolor='$fileclr'>");
  387: 	    $r->print("$thash{$key}$nhash{$key}</td><td bgcolor='$fileclr'>\n");
  388: 	    $r->print("$key</td></tr>\n");
  389: 	} 
  390:     }
  391:     if (($clen > 1) || ($env{'form.readfile'})) {
  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:     }
  400: 
  401:     $r->print(&Apache::loncommon::end_page());
  402: 
  403:     return OK;
  404: }
  405: 
  406: # --------------------------------------- Hidden values (returns scalar string)
  407: sub hidden {
  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.'" />';
  413:     return $string;
  414: }
  415: 
  416: # --------------------------------------- Moving arrows (returns scalar string)
  417: sub movers {
  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;
  423:     my $string;
  424:     $string = (<<END);
  425: <table border='0' cellspacing='0' cellpadding='0'>
  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>
  430: </table>
  431: END
  432:     return $string;
  433: }
  434: 
  435: # ------------------------------------------ Select box (returns scalar string)
  436: sub select_box {
  437:     my ($total,$sel) = @_;
  438:     my $string;
  439:     $string = '<select name="alt'.$sel.'"';
  440:     $string .= " onChange='selectchange($sel)'>";
  441:     $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
  442:     for my $cur (1..$total) {
  443: 	$string .= "<option name='o$cur' value='$cur'";
  444: 	if ($cur == $sel) {
  445: 	    $string .= "selected";
  446: 	}
  447: 	$string .= ">$cur</option>";
  448:     }
  449:     $string .= "</select>\n";
  450:     return $string;
  451: }
  452: 
  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: 
  462: 1;
  463: 
  464: __END__

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