File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.53: download - view: text, annotated - select for diffs
Fri Jul 6 08:34:37 2007 UTC (16 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- gettitle only take one arg

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

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