File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.69: download - view: text, annotated - select for diffs
Sat Jun 30 23:02:20 2012 UTC (11 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Facilitate code re-use.
- Move &update_content_constraints() from lonroles.pm to loncommon.pm
- Move &parse_supplemental_title() from londocs.pm to loncommon.pm

    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.69 2012/06/30 23:02:20 raeburn 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 qw(:DEFAULT :match);
   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 = LONCAPA::tempdir() .
   90:        "$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: 
  185: # ---------------------------------------------------------------- Main Handler
  186: sub handler {
  187:     my $r = shift;
  188:  
  189:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  190: 			     ['acts','mode','readfile','recover']);
  191: 
  192:     &Apache::loncommon::content_type($r,'text/html');
  193:     $r->send_http_header;
  194:     return OK if $r->header_only;
  195: 
  196: # finish_import looks different for graphical or "simple" RAT
  197:     my $finishimport='';
  198:     my $begincondition='';
  199:     my $endcondition='';
  200:     if (($env{'form.readfile'}))  {
  201:         $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
  202: 	$endcondition='}';
  203:     }
  204:     if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
  205:         $finishimport=(<<ENDSMP);
  206: function finish_import() {
  207:     opener.document.forms.simpleedit.importdetail.value='';
  208:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
  209: 	$begincondition
  210: 	opener.document.forms.simpleedit.importdetail.value+='&'+
  211:               eval("document.forms.groupsort.title"+num+".value")+'='+
  212: 	      eval("document.forms.groupsort.filelink"+num+".value")+'='+
  213: 	      eval("document.forms.groupsort.id"+num+".value");
  214: 	$endcondition
  215:     }
  216:     opener.document.forms.simpleedit.submit();
  217:     self.close();
  218: }
  219: ENDSMP
  220:     } else {
  221:         $finishimport=(<<ENDADV);
  222: function finish_import() {
  223:     var linkflag=false;
  224:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
  225: 	$begincondition
  226: 	insertRowInLastRow();
  227: 	placeResourceInLastRow(
  228: 	       eval("document.forms.groupsort.title"+num+".value"),
  229:  	       eval("document.forms.groupsort.filelink"+num+".value"),
  230:  	       eval("document.forms.groupsort.id"+num+".value"),
  231: 	       linkflag
  232: 	);
  233:         linkflag=true;
  234: 	$endcondition
  235:     }
  236:     opener.editmode=0;
  237:     opener.notclear=0;
  238:     opener.linkmode=0;
  239:     opener.draw();
  240:     self.close();
  241: }
  242: ENDADV
  243:     }
  244: 
  245: # output start of web page
  246:     my $js = <<END;
  247: <script type="text/javascript">
  248: function insertRowInLastRow() {
  249:     opener.insertrow(opener.maxrow);
  250:     opener.addobj(opener.maxrow,'e&2');
  251: }
  252: function placeResourceInLastRow (title,url,id,linkflag) {
  253:     opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
  254: 		       opener.escape(url),'false','normal',id);
  255:     opener.save();
  256:     if (linkflag) {
  257: 	opener.joinres(opener.linkmode,opener.mostrecent,0);
  258:     }
  259:     opener.linkmode=opener.mostrecent;
  260: }
  261: $finishimport
  262: function selectchange(val) {
  263:     var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
  264:     orderchange(val,newval);
  265: }
  266: function move(val,newval) {
  267:     orderchange(val,newval);
  268: }
  269: function orderchange(val,newval) {
  270:     document.forms.groupsort.oldval.value=val;
  271:     document.forms.groupsort.newval.value=newval;
  272:     document.forms.groupsort.submit();
  273: }
  274: </script>
  275: END
  276:     # read pertinent machine configuration
  277:     my $domain  = $r->dir_config('lonDefDomain');
  278:     $iconpath = $r->dir_config('lonIconsURL') . "/";
  279: 
  280:     my @resources;
  281: 
  282:     if ($env{'form.readfile'}) {
  283: 	&readfromfile($r,\@resources);
  284:     } else {
  285: 	&readfromdb($r,\@resources);
  286:     }
  287: 
  288:     my $ctr = 0;
  289:     my $clen = scalar(@resources);
  290:     my $title = '';
  291:     if ($env{'form.recover'}) {
  292:         $title = 'Recover Removed Resources';
  293:     } else {
  294:         $title = 'Sort Imported Resources';
  295:     }
  296:     if (($clen > 1) || ($env{'form.readfile'})) {
  297: 	my %lt=&Apache::lonlocal::texthash(
  298: 		'fin'=> 'Finalize order of resources',
  299: 		'ci' => 'Continue Import',
  300: 		'cs' => 'Continue Search',
  301: 		'fi' => 'Finish Import',
  302: 		're' => 'Recover Checked',
  303: 		'ip' => 'Import Checked',
  304: 		'ca' => 'Cancel',
  305: 		'co' => 'Change Order',
  306: 		'ti' => 'Title',
  307: 		'pa' => 'Path',
  308:                 'in' => 'Include'
  309: 		);
  310: 
  311: 	$r->print(&Apache::loncommon::start_page($title, $js));
  312: 	$r->print('<h1>'.&mt($title).'</h1>');
  313: 
  314: 	$r->print(<<END);
  315: <form method='post' action='/adm/groupsort' name='groupsort'
  316:       enctype='application/x-www-form-urlencoded'>
  317: <input type="hidden" name="fnum" value="$clen" />
  318: <input type="hidden" name="oldval" value="" />
  319: <input type="hidden" name="newval" value="" />
  320: <input type="hidden" name="mode" value="$env{'form.mode'}" />
  321: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
  322: <input type="hidden" name="recover" value="$env{'form.recover'}" />
  323: END
  324: 
  325:         $r->print(&Apache::loncommon::inhibit_menu_check('input'));
  326:         # ---
  327: 
  328:         my $buttontext = $lt{'re'};
  329:         if ($env{'form.recover'}) {
  330: 	    $r->print(<<END);
  331: <input type="button" name="alter" value="$buttontext"
  332:  onClick="finish_import()" />&nbsp;
  333: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
  334: END
  335: 	} else {
  336:         # --- Continue Buttons
  337: 	    my $resurl = 
  338: 		&Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
  339: 	    $r->print(<<END);
  340: <h2>$lt{'fin'}</h2>
  341: <div>
  342: <input type="button" name="alter" value="$lt{'ci'}"
  343:  onClick="window.location='$resurl?inhibitmenu=yes&amp;catalogmode=import'" />&nbsp;
  344: <input type="button" name="altersearch" value="$lt{'cs'}"
  345:  onClick="window.location='/adm/searchcat?inhibitmenu=yes&amp;catalogmode=import'" />&nbsp;
  346: <input type="button" name="alter" value="$lt{'fi'}"
  347:  onClick="finish_import()" />&nbsp;
  348: <input type="button" name="alter" value="$lt{'ca'}" onClick="self.close()" />
  349: </div>
  350: <br />
  351: END
  352:         }
  353: 
  354:         # Only display header if content exists
  355:         if ($clen > 0) {
  356:             $r->print(&Apache::loncommon::start_data_table()
  357:                      .&Apache::loncommon::start_data_table_header_row());
  358:             if (($env{'form.readfile'})) { 
  359:                 $r->print("<th>$lt{'in'}</th>\n");
  360:             } else { 
  361:                 $r->print('<th colspan="2">'.$lt{'co'}.'</th>'."\n"); 
  362:             }
  363:             $r->print('<th colspan="2">'.$lt{'ti'}.'</th>'."\n");
  364:             $r->print("<th>$lt{'pa'}</th>");
  365:             $r->print(&Apache::loncommon::end_data_table_header_row()."\n");
  366:         } else {
  367:             my $errtxt = '';
  368:             if ($env{'form.recover'}) {
  369:                 $errtxt = 'There are no resources to recover.';
  370:             } else {
  371:                 $errtxt = 'There are no resources to import.';
  372:             }
  373:             $r->print('<p class="LC_info">'.&mt($errtxt).'</p>');
  374:         }
  375:     } else {
  376: 	$r->print(&Apache::loncommon::start_page(undef,$js,
  377: 						 {'only_body' => 1}));
  378: #       $r->print('<h1>'.&mt($title).'</h1>');
  379: 	$r->print(<<END);
  380: <form method='post' action='/adm/groupsort' name='groupsort'
  381:       enctype='application/x-www-form-urlencoded'>
  382: <input type="hidden" name="fnum" value="$clen" />
  383: <input type="hidden" name="oldval" value="" />
  384: <input type="hidden" name="newval" value="" />
  385: <input type="hidden" name="mode" value="$env{'form.mode'}" />
  386: END
  387:         $r->print(&Apache::loncommon::inhibit_menu_check('input'));
  388: 
  389:     }
  390:     foreach my $resource (@resources) {
  391: 	$ctr++;
  392: 	my $iconname=&Apache::loncommon::icon($resource->{'url'});
  393: 	if (($clen > 1) || ($env{'form.readfile'})) {
  394: 	    $r->print(&Apache::loncommon::start_data_table_row()
  395:                      ."<td>");
  396:             if (($env{'form.readfile'})) {
  397: 		$r->print(&checkbox($ctr-1));
  398: 	    } else {
  399: 		$r->print(&movers($clen,$ctr));
  400: 	    }
  401: 	}
  402: 	$r->print(&hidden($ctr-1,$resource->{'title'},$resource->{'url'},
  403: 			  $resource->{'id'}));
  404: 	if (($clen > 1)  || ($env{'form.readfile'})) {
  405: 	    $r->print("</td>");
  406:             unless (($env{'form.readfile'})) {
  407: 		$r->print("<td>".
  408: 			  &select_box($clen,$ctr).
  409: 			  "</td>");
  410: 	    }
  411: 	    $r->print("<td>");
  412: 	    $r->print("<img src='$iconname' />");
  413: 	    $r->print("</td><td>");
  414:             if (($env{'form.recover'}) && 
  415:                 ($resource->{'url'} =~ m{/uploaded/$match_domain/$match_courseid/supplemental/})) {
  416: 	        my $title = &Apache::loncommon::parse_supplemental_title($resource->{'title'});
  417:                 $r->print($title);
  418:             } else {
  419:                  $r->print($resource->{'title'});
  420:             }
  421:             $r->print($resource->{'notes'}."</td><td>\n");
  422: 	    $r->print($resource->{'url'}."</td>"
  423:                      .&Apache::loncommon::end_data_table_row()
  424:                      ."\n");
  425: 	} 
  426:     }
  427:     if (($clen > 1) || ($env{'form.readfile'})) {
  428:         if ($clen > 0) {
  429:             $r->print(&Apache::loncommon::end_data_table());
  430:         }
  431:         $r->print('</form>');
  432:     } else {
  433: 	$r->print(<<END);
  434: <script type="text/javascript">
  435:     finish_import();
  436: </script>
  437: END
  438:     }
  439: 
  440:     $r->print(&Apache::loncommon::end_page());
  441: 
  442:     return OK;
  443: }
  444: 
  445: # --------------------------------------- Hidden values (returns scalar string)
  446: sub hidden {
  447:     my ($sel,$title,$filelink,$id) = @_;
  448:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.
  449: 	&escape($title).'" />';
  450:     $filelink=~s|^/ext/|http://|;
  451:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
  452: 	&escape($filelink).'" />';
  453:     $string .= '<input type="hidden" name="id'.$sel.'" value="'.&escape($id).'" />';
  454:     return $string;
  455: }
  456: 
  457: # --------------------------------------- Moving arrows (returns scalar string)
  458: sub movers {
  459:     my ($total,$sel) = @_;
  460:     my $dsel = $sel-1;
  461:     my $usel = $sel+1;
  462:     $usel = 1 if $usel > $total;
  463:     $dsel = $total if $dsel < 1;
  464:     my $string;
  465:     $string = (<<END);
  466: <table border='0' cellspacing='0' cellpadding='0'>
  467: <tr><td><a href='javascript:move($sel,$dsel)'>
  468: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
  469: <tr><td><a href='javascript:move($sel,$usel)'>
  470: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
  471: </table>
  472: END
  473:     return $string;
  474: }
  475: 
  476: # ------------------------------------------ Select box (returns scalar string)
  477: sub select_box {
  478:     my ($total,$sel) = @_;
  479:     my $string;
  480:     $string = '<select name="alt'.$sel.'"';
  481:     $string .= " onChange='selectchange($sel)'>";
  482:     $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
  483:     for my $cur (1..$total) {
  484: 	$string .= "<option name='o$cur' value='$cur'";
  485: 	if ($cur == $sel) {
  486: 	    $string .= "selected";
  487: 	}
  488: 	$string .= ">$cur</option>";
  489:     }
  490:     $string .= "</select>\n";
  491:     return $string;
  492: }
  493: 
  494: # ------------------------------------------------------------------- Checkbox
  495: 
  496: sub checkbox {
  497:     my $sel=shift;
  498:     return "<label><input type='checkbox' name='include$sel'".
  499:        ($env{"form.include$sel"}?' checked="checked"':'').
  500:        ' />'.&mt('Include').'</label>';
  501: }
  502: 
  503: 1;
  504: 
  505: __END__
  506: 
  507: =pod
  508: 
  509: =head1 NAME
  510: 
  511: Apache::groupsort.pm
  512: 
  513: =head1 SYNOPSIS
  514: 
  515: Implements a second phase of importing
  516: multiple resources into the RAT. Allows for
  517: reordering the sequence of resources
  518: 
  519: This is part of the LearningOnline Network with CAPA project
  520: described at http://www.lon-capa.org.
  521: 
  522: 
  523: =head1 NOTABLE SUBROUTINES
  524: 
  525: =over
  526: 
  527: =item 
  528: 
  529: =back
  530: 
  531: =cut
  532: 

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