File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.58: download - view: text, annotated - select for diffs
Thu Jul 12 01:04:36 2007 UTC (16 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- when having multipled deleted resources with the same urls, restore
  the one with id you wanted to restore

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

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