File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.56: download - view: text, annotated - select for diffs
Wed Jul 11 23:53:59 2007 UTC (16 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixup headings when doing a recovery

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

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