Annotation of loncom/interface/groupsort.pm, revision 1.77

1.1       harris41    1: # The LearningOnline Network with CAPA
1.4       harris41    2: # The LON-CAPA group sort handler
                      3: # Allows for sorting prior to import into RAT.
                      4: #
1.77    ! raeburn     5: # $Id: groupsort.pm,v 1.76 2017/05/30 15:10:57 raeburn Exp $
1.4       harris41    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.
1.1       harris41   20: #
1.4       harris41   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
1.1       harris41   26: #
1.4       harris41   27: # http://www.lon-capa.org/
1.1       harris41   28: #
1.4       harris41   29: ###
1.1       harris41   30: 
                     31: package Apache::groupsort;
                     32: 
                     33: use strict;
                     34: 
1.75      raeburn    35: use Apache::Constants qw(:common :http);
1.1       harris41   36: use GDBM_File;
1.15      www        37: use Apache::loncommon;
1.23      www        38: use Apache::lonlocal;
1.27      taceyjo1   39: use Apache::lonnet;
1.69      raeburn    40: use LONCAPA qw(:DEFAULT :match);
1.1       harris41   41: 
1.33      www        42: my $iconpath; # variable to be accessible to multiple subroutines
1.2       harris41   43: my %hash; # variable to tie to user specific database
1.8       www        44: 
1.2       harris41   45: 
1.55      albertel   46: sub update_actions_hash {
                     47:     my ($hash) = @_;
1.60      banghart   48:     # be careful in here, there is also a global %hash
1.55      albertel   49:     my $acts=$env{'form.acts'};
                     50:     my @Acts=split(/b/,$acts);
                     51:     my %ahash;
                     52:     my %achash;
1.60      banghart   53:     # some initial hashes for working with data
1.55      albertel   54:     my $ac=0;
                     55:     foreach (@Acts) {
1.60      banghart   56:  	my ($state,$ref)=split(/a/);
1.55      albertel   57: 	$ahash{$ref}=$state;
                     58: 	$achash{$ref}=$ac;
                     59: 	$ac++;
                     60:     }
                     61:     # sorting through the actions and changing the global database hash
1.74      raeburn    62:     foreach my $key (sort {$achash{$a}<=>$achash{$b}} (keys(%ahash))) {
1.55      albertel   63: 	if ($ahash{$key} eq '1') {
1.60      banghart   64: 	    $hash->{'store_'.$hash->{'pre_'.$key.'_link'}}=
1.55      albertel   65: 		$hash->{'pre_'.$key.'_title'};
1.60      banghart   66: 	    $hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}}=
1.55      albertel   67: 		$hash->{'storectr'}+0;
                     68: 	    $hash->{'storectr'}++;
                     69: 	}
                     70: 	if ($ahash{$key} eq '0') {
1.60      banghart   71: 	    if ($hash->{'store_'.$hash->{'pre_'.$key.'_link'}}) {
                     72: 		delete($hash->{'store_'.$hash->{'pre_'.$key.'_link'}});
                     73: 		delete($hash->{'storectr_'.$hash->{'pre_'.$key.'_link'}});
1.55      albertel   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: 
1.33      www        86: sub readfromdb {
1.57      albertel   87:     my ($r,$resources)=@_;
1.9       www        88: 
1.68      foxr       89:     my $diropendb = LONCAPA::tempdir() .
                     90:        "$env{'user.domain'}_$env{'user.name'}_sel_res.db";
1.11      www        91: 
                     92: # ----------------------------- diropendb is now the filename of the db to open
1.13      albertel   93:     if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) {
1.55      albertel   94: 	&update_actions_hash(\%hash);
                     95: 
1.57      albertel   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});
1.5       harris41  108: 	}
1.57      albertel  109: 
1.31      albertel  110: 	if ($env{'form.oldval'}) {
1.57      albertel  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);
1.1       harris41  119: 	    }
1.57      albertel  120: 	}
                    121: 	# store out new order
                    122: 	foreach my $which (0..$#$resources) {
                    123: 	    my $url =  $resources->[$which]{'url'};
                    124: 	    $hash{'storectr_'.$url} = $which;
1.1       harris41  125: 	}
                    126:     } else {
1.34      www       127: 	$r->print('Unable to tie hash to db file');
1.1       harris41  128:     }
1.57      albertel  129:     untie(%hash);
1.33      www       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:     }
1.39      albertel  141:     return OK;
1.33      www       142: }
                    143: 
1.34      www       144: # -------------------------------------------------------------- Read from file
                    145: 
                    146: sub readfromfile {
1.57      albertel  147:     my ($r,$resources)=@_;
1.34      www       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);
1.71      raeburn   155:         my ($token,$donechk,$allmaps);
                    156:         $allmaps = {};
1.34      www       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; }
1.70      raeburn   162:                         if ($token->[2]->{'src'} =~ /\.(page|sequence)$/) {
1.71      raeburn   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:                                 }
1.70      raeburn   169:                             }
1.71      raeburn   170:                             if ($allmaps->{$token->[2]->{'src'}}) { next; }
1.70      raeburn   171:                         }
1.34      www       172: 		    } else {
                    173: 			if ($token->[2]->{'type'} eq 'zombie') { next; }
                    174: 		    }
1.35      www       175: 
1.34      www       176:                     my $name=$token->[2]->{'title'};
1.50      albertel  177: 		    $name=~s/ \[\((\d+)\,($LONCAPA::username_re)\,($LONCAPA::domain_re)\)\]$//;
1.57      albertel  178: 		    my $note;
1.34      www       179: 		    if ($1) {
1.57      albertel  180: 			$note = '<br />'.&mt('Removed by ').
1.34      www       181: 			    &Apache::loncommon::plainname($2,$3).', '.
                    182: 			    &Apache::lonlocal::locallocaltime($1);
                    183: 		    }
1.37      www       184: 		    $name=~s/\&colon\;/\:/g;
1.58      albertel  185: 		    push(@{$resources}, {'url'   => $token->[2]->{'src'},
1.57      albertel  186: 					 'title' => $name,
1.58      albertel  187: 					 'note'  => $note,
                    188: 				         'id'    => $token->[2]->{'id'},});
1.34      www       189: 		}
                    190: 	    }
                    191: 	}
                    192:     }
                    193: }
                    194: 
1.33      www       195: # ---------------------------------------------------------------- Main Handler
                    196: sub handler {
                    197:     my $r = shift;
                    198:  
                    199:    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.67      wenzelju  200: 			     ['acts','mode','readfile','recover']);
1.33      www       201: 
                    202:     &Apache::loncommon::content_type($r,'text/html');
                    203:     $r->send_http_header;
                    204:     return OK if $r->header_only;
                    205: 
1.75      raeburn   206: # permissions checking
                    207:     my ($allowed,$canedit,$context,$cid);
1.76      raeburn   208:     if ($env{'form.readfile'} eq '') {
                    209:         $allowed = 1;
                    210:     } elsif ($env{'form.readfile'} =~ m{^/uploaded/($match_domain)/($match_courseid)/}) {
1.75      raeburn   211:         my ($cdom,$cnum) = ($1,$2);
                    212:         $cid = $cdom.'_'.$cnum;
                    213:         $context = 'course';
                    214:         if ((&Apache::lonnet::allowed('mdc',$cid)) ||
                    215:             (&Apache::lonnet::allowed('cev',$cid))) {
                    216:             $allowed = 1;
                    217:         }
                    218:     } elsif ($env{'form.readfile'} =~ m{^/res/}) {
                    219:         $context = 'res';
                    220:         if ((&Apache::lonnet::allowed('bre',$env{'form.readfile'})) ||
                    221:             (&Apache::lonnet::allowed('bro',$env{'form.readfile'}))) {
                    222:             $allowed = 1;
                    223:         }
                    224:     }
                    225:     if ($allowed) {
                    226:         if ($env{'form.mode'} eq 'rat') {
                    227:             if (&Apache::lonnet::allowed('are',$env{'request.role.domain'})) {
                    228:                 $canedit = 1;
                    229:             }
                    230:         } elsif (($env{'form.mode'} eq 'simple') || ($env{'form.mode'} eq '')) {
                    231:             if ($context eq 'course') {
                    232:                 if (&Apache::lonnet::allowed('mdc',$cid)) {
                    233:                     $canedit = 1;
                    234:                 }
                    235:             } elsif (($env{'request.course.id'}) &&
                    236:                      (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
                    237:                 $canedit = 1;
                    238:             } elsif (&Apache::lonnet::allowed('are',$env{'request.role.domain'})) {
                    239:                 $canedit = 1;
                    240:             }
                    241:         }
                    242:     }
                    243: 
                    244:     unless ($allowed) {
                    245:         if ($context eq 'course') {
1.77    ! raeburn   246:             if ($env{'request.course.id'} eq $cid) {
1.75      raeburn   247:                 $env{'user.error.msg'}=
                    248:                     "/adm/groupsort::0:1:Course environment gone, reinitialize the course";
                    249:             } else {
                    250:                 $env{'user.error.msg'}=
                    251:                     "/adm/groupsort:bre:0:0:Cannot view folder contents";
                    252:             }
                    253:         } else {
                    254:             $env{'user.error.msg'}=
                    255:                 "/adm/groupsort:bre:0:0:Cannot view map contents";
                    256:         }
                    257:         return HTTP_NOT_ACCEPTABLE;
                    258:     }
                    259: 
1.33      www       260: # finish_import looks different for graphical or "simple" RAT
                    261:     my $finishimport='';
1.37      www       262:     my $begincondition='';
                    263:     my $endcondition='';
1.75      raeburn   264:     my $noedit;
                    265:     unless ($canedit) {
                    266:         if ($context eq 'course') {
                    267:             $noedit = &js_escape(&mt('You do not have rights to edit the course.'));
                    268:         } else {
                    269:             $noedit = &js_escape(&mt('You do not have rights to edit map contents.'));
                    270:         }
1.77    ! raeburn   271:     }
1.67      wenzelju  272:     if (($env{'form.readfile'}))  {
1.37      www       273:         $begincondition='if (eval("document.forms.groupsort.include"+num+".checked")) {';
                    274: 	$endcondition='}';
                    275:     }
1.33      www       276:     if ($env{'form.mode'} eq 'simple' || $env{'form.mode'} eq '') {
1.75      raeburn   277:         if ($canedit) {
                    278:             $finishimport=(<<ENDSMP);
1.33      www       279: function finish_import() {
                    280:     opener.document.forms.simpleedit.importdetail.value='';
                    281:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37      www       282: 	$begincondition
1.33      www       283: 	opener.document.forms.simpleedit.importdetail.value+='&'+
1.61      albertel  284:               eval("document.forms.groupsort.title"+num+".value")+'='+
                    285: 	      eval("document.forms.groupsort.filelink"+num+".value")+'='+
                    286: 	      eval("document.forms.groupsort.id"+num+".value");
1.37      www       287: 	$endcondition
1.33      www       288:     }
                    289:     opener.document.forms.simpleedit.submit();
                    290:     self.close();
                    291: }
                    292: ENDSMP
1.75      raeburn   293:         } else {
                    294:             $finishimport=(<<ENDNO);
                    295: function finish_import() {
                    296:     alert('$noedit');
1.77    ! raeburn   297: }
1.75      raeburn   298: ENDNO
                    299:         }
1.33      www       300:     } else {
1.75      raeburn   301:         if ($canedit) {
                    302:             $finishimport=(<<ENDADV);
1.33      www       303: function finish_import() {
                    304:     var linkflag=false;
                    305:     for (var num=0; num<document.forms.groupsort.fnum.value; num++) {
1.37      www       306: 	$begincondition
1.33      www       307: 	insertRowInLastRow();
                    308: 	placeResourceInLastRow(
                    309: 	       eval("document.forms.groupsort.title"+num+".value"),
                    310:  	       eval("document.forms.groupsort.filelink"+num+".value"),
1.59      albertel  311:  	       eval("document.forms.groupsort.id"+num+".value"),
1.33      www       312: 	       linkflag
                    313: 	);
                    314:         linkflag=true;
1.37      www       315: 	$endcondition
1.33      www       316:     }
                    317:     opener.editmode=0;
                    318:     opener.notclear=0;
                    319:     opener.linkmode=0;
                    320:     opener.draw();
                    321:     self.close();
                    322: }
                    323: ENDADV
1.75      raeburn   324:         } else {
                    325:             $finishimport=(<<ENDNONE);
                    326: function finish_import() {
                    327:     alert('$noedit');
                    328: }
                    329: ENDNONE
                    330:         }
1.33      www       331:     }
                    332: 
                    333: # output start of web page
1.40      albertel  334:     my $js = <<END;
                    335: <script type="text/javascript">
1.33      www       336: function insertRowInLastRow() {
                    337:     opener.insertrow(opener.maxrow);
                    338:     opener.addobj(opener.maxrow,'e&2');
                    339: }
1.59      albertel  340: function placeResourceInLastRow (title,url,id,linkflag) {
1.33      www       341:     opener.mostrecent=opener.newresource(opener.maxrow,2,opener.escape(title),
1.59      albertel  342: 		       opener.escape(url),'false','normal',id);
1.33      www       343:     opener.save();
                    344:     if (linkflag) {
                    345: 	opener.joinres(opener.linkmode,opener.mostrecent,0);
                    346:     }
                    347:     opener.linkmode=opener.mostrecent;
                    348: }
                    349: $finishimport
                    350: function selectchange(val) {
                    351:     var newval=0+eval("document.forms.groupsort.alt"+val+".selectedIndex");
                    352:     orderchange(val,newval);
                    353: }
                    354: function move(val,newval) {
                    355:     orderchange(val,newval);
                    356: }
                    357: function orderchange(val,newval) {
                    358:     document.forms.groupsort.oldval.value=val;
                    359:     document.forms.groupsort.newval.value=newval;
                    360:     document.forms.groupsort.submit();
                    361: }
                    362: </script>
                    363: END
                    364:     # read pertinent machine configuration
                    365:     my $domain  = $r->dir_config('lonDefDomain');
                    366:     $iconpath = $r->dir_config('lonIconsURL') . "/";
                    367: 
1.57      albertel  368:     my @resources;
1.33      www       369: 
1.34      www       370:     if ($env{'form.readfile'}) {
1.57      albertel  371: 	&readfromfile($r,\@resources);
1.34      www       372:     } else {
1.57      albertel  373: 	&readfromdb($r,\@resources);
1.34      www       374:     }
1.33      www       375: 
1.2       harris41  376:     my $ctr = 0;
1.57      albertel  377:     my $clen = scalar(@resources);
1.66      bisitz    378:     my $title = '';
                    379:     if ($env{'form.recover'}) {
                    380:         $title = 'Recover Removed Resources';
                    381:     } else {
                    382:         $title = 'Sort Imported Resources';
                    383:     }
1.75      raeburn   384:     my $disabled;
                    385:     unless ($canedit) {
                    386:         $disabled = ' disabled="disabled"';
                    387:     }
1.67      wenzelju  388:     if (($clen > 1) || ($env{'form.readfile'})) {
1.23      www       389: 	my %lt=&Apache::lonlocal::texthash(
                    390: 		'fin'=> 'Finalize order of resources',
1.42      www       391: 		'ci' => 'Continue Import',
                    392: 		'cs' => 'Continue Search',
1.23      www       393: 		'fi' => 'Finish Import',
1.51      albertel  394: 		're' => 'Recover Checked',
1.64      bisitz    395: 		'ip' => 'Import Checked',
1.23      www       396: 		'ca' => 'Cancel',
                    397: 		'co' => 'Change Order',
                    398: 		'ti' => 'Title',
1.37      www       399: 		'pa' => 'Path',
                    400:                 'in' => 'Include'
1.23      www       401: 		);
1.64      bisitz    402: 
1.56      albertel  403: 	$r->print(&Apache::loncommon::start_page($title, $js));
1.66      bisitz    404: 	$r->print('<h1>'.&mt($title).'</h1>');
1.56      albertel  405: 
1.22      albertel  406: 	$r->print(<<END);
1.14      matthew   407: <form method='post' action='/adm/groupsort' name='groupsort'
                    408:       enctype='application/x-www-form-urlencoded'>
1.1       harris41  409: <input type="hidden" name="fnum" value="$clen" />
                    410: <input type="hidden" name="oldval" value="" />
                    411: <input type="hidden" name="newval" value="" />
1.31      albertel  412: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.36      www       413: <input type="hidden" name="readfile" value="$env{'form.readfile'}" />
                    414: <input type="hidden" name="recover" value="$env{'form.recover'}" />
1.3       harris41  415: END
1.11      www       416: 
1.54      albertel  417:         $r->print(&Apache::loncommon::inhibit_menu_check('input'));
1.51      albertel  418:         # ---
1.65      bisitz    419: 
                    420:         my $buttontext = $lt{'re'};
1.67      wenzelju  421:         if ($env{'form.recover'}) {
1.51      albertel  422: 	    $r->print(<<END);
1.65      bisitz    423: <input type="button" name="alter" value="$buttontext"
1.75      raeburn   424:  onclick="finish_import()"$disabled />&nbsp;
1.73      bisitz    425: <input type="button" name="alter" value="$lt{'ca'}" onclick="self.close()" />
1.51      albertel  426: END
                    427: 	} else {
1.42      www       428:         # --- Continue Buttons
1.51      albertel  429: 	    my $resurl = 
                    430: 		&Apache::loncommon::escape_single(&Apache::loncommon::lastresurl());
                    431: 	    $r->print(<<END);
1.62      bisitz    432: <h2>$lt{'fin'}</h2>
                    433: <div>
1.42      www       434: <input type="button" name="alter" value="$lt{'ci'}"
1.73      bisitz    435:  onclick="window.location='$resurl?inhibitmenu=yes&amp;catalogmode=import'" />&nbsp;
1.42      www       436: <input type="button" name="altersearch" value="$lt{'cs'}"
1.73      bisitz    437:  onclick="window.location='/adm/searchcat?inhibitmenu=yes&amp;catalogmode=import'" />&nbsp;
1.23      www       438: <input type="button" name="alter" value="$lt{'fi'}"
1.75      raeburn   439:  onclick="finish_import()"$disabled />&nbsp;
1.73      bisitz    440: <input type="button" name="alter" value="$lt{'ca'}" onclick="self.close()" />
1.62      bisitz    441: </div>
                    442: <br />
1.1       harris41  443: END
1.51      albertel  444:         }
1.65      bisitz    445: 
                    446:         # Only display header if content exists
                    447:         if ($clen > 0) {
                    448:             $r->print(&Apache::loncommon::start_data_table()
                    449:                      .&Apache::loncommon::start_data_table_header_row());
1.67      wenzelju  450:             if (($env{'form.readfile'})) { 
1.65      bisitz    451:                 $r->print("<th>$lt{'in'}</th>\n");
                    452:             } else { 
                    453:                 $r->print('<th colspan="2">'.$lt{'co'}.'</th>'."\n"); 
                    454:             }
                    455:             $r->print('<th colspan="2">'.$lt{'ti'}.'</th>'."\n");
                    456:             $r->print("<th>$lt{'pa'}</th>");
                    457:             $r->print(&Apache::loncommon::end_data_table_header_row()."\n");
                    458:         } else {
1.66      bisitz    459:             my $errtxt = '';
                    460:             if ($env{'form.recover'}) {
                    461:                 $errtxt = 'There are no resources to recover.';
                    462:             } else {
                    463:                 $errtxt = 'There are no resources to import.';
                    464:             }
                    465:             $r->print('<p class="LC_info">'.&mt($errtxt).'</p>');
1.65      bisitz    466:         }
1.22      albertel  467:     } else {
1.40      albertel  468: 	$r->print(&Apache::loncommon::start_page(undef,$js,
1.41      banghart  469: 						 {'only_body' => 1}));
1.66      bisitz    470: #       $r->print('<h1>'.&mt($title).'</h1>');
1.22      albertel  471: 	$r->print(<<END);
                    472: <form method='post' action='/adm/groupsort' name='groupsort'
                    473:       enctype='application/x-www-form-urlencoded'>
                    474: <input type="hidden" name="fnum" value="$clen" />
                    475: <input type="hidden" name="oldval" value="" />
                    476: <input type="hidden" name="newval" value="" />
1.31      albertel  477: <input type="hidden" name="mode" value="$env{'form.mode'}" />
1.22      albertel  478: END
1.54      albertel  479:         $r->print(&Apache::loncommon::inhibit_menu_check('input'));
                    480: 
1.22      albertel  481:     }
1.57      albertel  482:     foreach my $resource (@resources) {
1.1       harris41  483: 	$ctr++;
1.57      albertel  484: 	my $iconname=&Apache::loncommon::icon($resource->{'url'});
1.67      wenzelju  485: 	if (($clen > 1) || ($env{'form.readfile'})) {
1.62      bisitz    486: 	    $r->print(&Apache::loncommon::start_data_table_row()
                    487:                      ."<td>");
1.67      wenzelju  488:             if (($env{'form.readfile'})) {
1.75      raeburn   489: 		$r->print(&checkbox($ctr-1,$disabled));
1.37      www       490: 	    } else {
                    491: 		$r->print(&movers($clen,$ctr));
                    492: 	    }
1.22      albertel  493: 	}
1.58      albertel  494: 	$r->print(&hidden($ctr-1,$resource->{'title'},$resource->{'url'},
                    495: 			  $resource->{'id'}));
1.67      wenzelju  496: 	if (($clen > 1)  || ($env{'form.readfile'})) {
1.37      www       497: 	    $r->print("</td>");
1.67      wenzelju  498:             unless (($env{'form.readfile'})) {
1.62      bisitz    499: 		$r->print("<td>".
1.75      raeburn   500: 			  &select_box($clen,$ctr,$disabled).
1.37      www       501: 			  "</td>");
                    502: 	    }
1.62      bisitz    503: 	    $r->print("<td>");
1.26      albertel  504: 	    $r->print("<img src='$iconname' />");
1.62      bisitz    505: 	    $r->print("</td><td>");
1.72      raeburn   506:             if (($env{'form.recover'}) &&
1.69      raeburn   507:                 ($resource->{'url'} =~ m{/uploaded/$match_domain/$match_courseid/supplemental/})) {
1.72      raeburn   508:                 my $title = &Apache::loncommon::parse_supplemental_title($resource->{'title'});
1.69      raeburn   509:                 $r->print($title);
                    510:             } else {
1.72      raeburn   511:                 $r->print($resource->{'title'});
1.69      raeburn   512:             }
                    513:             $r->print($resource->{'notes'}."</td><td>\n");
1.62      bisitz    514: 	    $r->print($resource->{'url'}."</td>"
                    515:                      .&Apache::loncommon::end_data_table_row()
                    516:                      ."\n");
1.22      albertel  517: 	} 
                    518:     }
1.67      wenzelju  519:     if (($clen > 1) || ($env{'form.readfile'})) {
1.65      bisitz    520:         if ($clen > 0) {
                    521:             $r->print(&Apache::loncommon::end_data_table());
                    522:         }
                    523:         $r->print('</form>');
1.22      albertel  524:     } else {
                    525: 	$r->print(<<END);
                    526: <script type="text/javascript">
                    527:     finish_import();
                    528: </script>
                    529: END
                    530:     }
1.40      albertel  531: 
                    532:     $r->print(&Apache::loncommon::end_page());
1.22      albertel  533: 
1.1       harris41  534:     return OK;
                    535: }
                    536: 
1.2       harris41  537: # --------------------------------------- Hidden values (returns scalar string)
1.1       harris41  538: sub hidden {
1.58      albertel  539:     my ($sel,$title,$filelink,$id) = @_;
1.61      albertel  540:     my $string = '<input type="hidden" name="title'.$sel.'" value="'.
                    541: 	&escape($title).'" />';
1.48      albertel  542:     $filelink=~s|^/ext/|http://|;
1.2       harris41  543:     $string .= '<input type="hidden" name="filelink'.$sel.'" value="'.
1.61      albertel  544: 	&escape($filelink).'" />';
                    545:     $string .= '<input type="hidden" name="id'.$sel.'" value="'.&escape($id).'" />';
1.1       harris41  546:     return $string;
                    547: }
                    548: 
1.2       harris41  549: # --------------------------------------- Moving arrows (returns scalar string)
1.1       harris41  550: sub movers {
1.2       harris41  551:     my ($total,$sel) = @_;
                    552:     my $dsel = $sel-1;
                    553:     my $usel = $sel+1;
                    554:     $usel = 1 if $usel > $total;
                    555:     $dsel = $total if $dsel < 1;
1.1       harris41  556:     my $string;
1.2       harris41  557:     $string = (<<END);
1.1       harris41  558: <table border='0' cellspacing='0' cellpadding='0'>
1.2       harris41  559: <tr><td><a href='javascript:move($sel,$dsel)'>
                    560: <img src="${iconpath}move_up.gif" alt='UP' border='0' /></a></td></tr>
                    561: <tr><td><a href='javascript:move($sel,$usel)'>
                    562: <img src="${iconpath}move_down.gif" alt='DOWN' border='0' /></a></td></tr>
1.1       harris41  563: </table>
                    564: END
                    565:     return $string;
                    566: }
1.2       harris41  567: 
                    568: # ------------------------------------------ Select box (returns scalar string)
1.1       harris41  569: sub select_box {
1.75      raeburn   570:     my ($total,$sel,$disabled) = @_;
1.1       harris41  571:     my $string;
1.2       harris41  572:     $string = '<select name="alt'.$sel.'"';
1.75      raeburn   573:     $string .= " onchange='selectchange($sel)'.$disabled.'>";
1.36      www       574:     $string .= "<option name='o0' value='0'>".&mt('discard')."</option>";
1.1       harris41  575:     for my $cur (1..$total) {
1.2       harris41  576: 	$string .= "<option name='o$cur' value='$cur'";
                    577: 	if ($cur == $sel) {
                    578: 	    $string .= "selected";
1.1       harris41  579: 	}
1.2       harris41  580: 	$string .= ">$cur</option>";
1.1       harris41  581:     }
1.2       harris41  582:     $string .= "</select>\n";
1.1       harris41  583:     return $string;
                    584: }
                    585: 
1.37      www       586: # ------------------------------------------------------------------- Checkbox
                    587: 
                    588: sub checkbox {
1.75      raeburn   589:     my ($sel,$disabled) = @_;
1.37      www       590:     return "<label><input type='checkbox' name='include$sel'".
                    591:        ($env{"form.include$sel"}?' checked="checked"':'').
1.75      raeburn   592:        $disabled.' />'.&mt('Include').'</label>';
1.37      www       593: }
                    594: 
1.1       harris41  595: 1;
                    596: 
                    597: __END__
1.63      jms       598: 
                    599: =pod
                    600: 
                    601: =head1 NAME
                    602: 
                    603: Apache::groupsort.pm
                    604: 
                    605: =head1 SYNOPSIS
                    606: 
                    607: Implements a second phase of importing
                    608: multiple resources into the RAT. Allows for
                    609: reordering the sequence of resources
                    610: 
                    611: This is part of the LearningOnline Network with CAPA project
                    612: described at http://www.lon-capa.org.
                    613: 
                    614: 
                    615: =head1 NOTABLE SUBROUTINES
                    616: 
                    617: =over
                    618: 
                    619: =item 
                    620: 
                    621: =back
                    622: 
                    623: =cut
                    624: 

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