File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.68.6.6: download - view: text, annotated - select for diffs
Thu Aug 8 12:08:01 2013 UTC (10 years, 8 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0
Diff to branchpoint 1.68: preferred, unified
- For 2.11
  - Backport 1.73

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

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