File:  [LON-CAPA] / loncom / interface / groupsort.pm
Revision 1.68.6.4: download - view: text, annotated - select for diffs
Tue Jan 8 03:51:06 2013 UTC (11 years, 4 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.68: preferred, unified
- For 2.11
  - Backport 1.70.

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

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