File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.51: download - view: text, annotated - select for diffs
Fri Jun 15 19:18:45 2007 UTC (16 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- improve verbiage on the import screen when in recover mode

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

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