File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.61: download - view: text, annotated - select for diffs
Fri Nov 2 21:08:45 2007 UTC (16 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_2_7_X, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_99_1, version_2_5_99_0, HEAD
- BUG#5499 - passing the utf string through a hiddent form parm apparently was getting converted from UTF-8 to ISO, pass it arround escpaed to prevent any changes

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

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