--- loncom/interface/loncommon.pm 2012/01/31 23:47:15 1.1055 +++ loncom/interface/loncommon.pm 2012/03/11 00:48:54 1.1058 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # a pile of common routines # -# $Id: loncommon.pm,v 1.1055 2012/01/31 23:47:15 raeburn Exp $ +# $Id: loncommon.pm,v 1.1058 2012/03/11 00:48:54 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -1748,6 +1748,7 @@ Inputs: $workbook Returns: $format, a hash reference. + =cut ############################################################### @@ -2856,6 +2857,7 @@ database which holds them. Uses global $thesaurus_db_file. + =cut ############################################################### @@ -4015,18 +4017,25 @@ sub findallcourses { if ($tstart) { next if ($tstart > $now); } - my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role,$realsec); + my ($cdom,$cnum,$sec,$cnumpart,$secpart,$role); (undef,$cdom,$cnumpart,$secpart) = split(/\//,$entry); + my $value = $trole.'/'.$cdom.'/'; if ($secpart eq '') { ($cnum,$role) = split(/_/,$cnumpart); $sec = 'none'; - $realsec = ''; + $value .= $cnum.'/'; } else { $cnum = $cnumpart; ($sec,$role) = split(/_/,$secpart); - $realsec = $sec; + $value .= $cnum.'/'.$sec; + } + if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') { + unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) { + push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value); + } + } else { + @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value); } - $courses{$cdom.'_'.$cnum}{$sec} = $trole.'/'.$cdom.'/'.$cnum.'/'.$realsec; } } else { foreach my $key (keys(%env)) { @@ -4044,11 +4053,19 @@ sub findallcourses { if ($now>$endtime) { $active=0; } } if ($active) { + my $value = $role.'/'.$cdom.'/'.$cnum.'/'; if ($sec eq '') { $sec = 'none'; + } else { + $value .= $sec; + } + if (ref($courses{$cdom.'_'.$cnum}{$sec}) eq 'ARRAY') { + unless (grep(/^\Q$value\E$/,@{$courses{$cdom.'_'.$cnum}{$sec}})) { + push(@{$courses{$cdom.'_'.$cnum}{$sec}},$value); + } + } else { + @{$courses{$cdom.'_'.$cnum}{$sec}} = ($value); } - $courses{$cdom.'_'.$cnum}{$sec} = - $role.'/'.$cdom.'/'.$cnum.'/'.$sec; } } } @@ -4141,34 +4158,38 @@ sub blockcheck { if ($otheruser) { # Resource belongs to user other than current user. # Assemble privs for that user, and check for 'evb' priv. - my ($trole,$tdom,$tnum,$tsec); - my $entry = $live_courses{$course}{$sec}; - if ($entry =~ /^cr/) { - ($trole,$tdom,$tnum,$tsec) = - ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|); - } else { - ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry); - } - my ($spec,$area,$trest,%allroles,%userroles); - $area = '/'.$tdom.'/'.$tnum; - $trest = $tnum; - if ($tsec ne '') { - $area .= '/'.$tsec; - $trest .= '/'.$tsec; - } - $spec = $trole.'.'.$area; - if ($trole =~ /^cr/) { - &Apache::lonnet::custom_roleprivs(\%allroles,$trole, - $tdom,$spec,$trest,$area); - } else { - &Apache::lonnet::standard_roleprivs(\%allroles,$trole, - $tdom,$spec,$trest,$area); - } - my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles); - if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) { - if ($1) { - $no_userblock = 1; - last; + my (%allroles,%userroles); + if (ref($live_courses{$course}{$sec}) eq 'ARRAY') { + foreach my $entry (@{$live_courses{$course}{$sec}}) { + my ($trole,$tdom,$tnum,$tsec); + if ($entry =~ /^cr/) { + ($trole,$tdom,$tnum,$tsec) = + ($entry =~ m|^(cr/$match_domain/$match_username/\w+)\./($match_domain)/($match_username)/?(\w*)$|); + } else { + ($trole,$tdom,$tnum,$tsec) = split(/\//,$entry); + } + my ($spec,$area,$trest); + $area = '/'.$tdom.'/'.$tnum; + $trest = $tnum; + if ($tsec ne '') { + $area .= '/'.$tsec; + $trest .= '/'.$tsec; + } + $spec = $trole.'.'.$area; + if ($trole =~ /^cr/) { + &Apache::lonnet::custom_roleprivs(\%allroles,$trole, + $tdom,$spec,$trest,$area); + } else { + &Apache::lonnet::standard_roleprivs(\%allroles,$trole, + $tdom,$spec,$trest,$area); + } + } + my ($author,$adv) = &Apache::lonnet::set_userprivs(\%userroles,\%allroles); + if ($userroles{'user.priv.'.$checkrole} =~ /evb\&([^\:]*)/) { + if ($1) { + $no_userblock = 1; + last; + } } } } else { @@ -8817,6 +8838,8 @@ sub get_future_slots { =pod +=back + =head1 HTTP Helpers =over 4 @@ -9735,9 +9758,6 @@ sub decompress_form { } $output .= '

'; $output .= <<"START"; -

-$lt{'this'} $lt{'youm'} -

@@ -9857,18 +9877,19 @@ sub process_decompression { } } if (@contents > 0) { - my (%children,%parent); + my (%children,%parent,%dirorder,%titles); my $wantform = 1; my ($count,$datatable) = &get_extracted($docudom,$docuname, $currdir,\%is_dir, \%children,\%parent, - \@contents,$wantform); + \@contents,\%dirorder, + \%titles,$wantform); if ($datatable ne '') { $output .= &archive_options_form('decompressed',$datatable, $count,$hiddenelem); - my $startcount = 3; + my $startcount = 4; $output .= &archive_javascript($startcount,$count, - %children); + \%titles,\%children); } } else { $warning = &mt('No new items extracted from archive file.'); @@ -9891,15 +9912,19 @@ sub process_decompression { } sub get_extracted { - my ($docudom,$docuname,$currdir,$is_dir,$children,$parent,$contents,$wantform) = @_; + my ($docudom,$docuname,$currdir,$is_dir,$children,$parent,$contents,$dirorder, + $titles,$wantform) = @_; my $count = 0; - my $lastcontainer = 0; my $depth = 0; my $datatable; + my @hierarchy; return unless ((ref($is_dir) eq 'HASH') && (ref($children) eq 'HASH') && - (ref($parent) eq 'HASH') && (ref($contents) eq 'ARRAY')); + (ref($parent) eq 'HASH') && (ref($contents) eq 'ARRAY') && + (ref($dirorder) eq 'HASH') && (ref($titles) eq 'HASH')); foreach my $item (@{$contents}) { $count ++; + @{$dirorder->{$count}} = @hierarchy; + $titles->{$count} = $item; &archive_hierarchy($depth,$count,$parent,$children); if ($wantform) { $datatable .= &archive_row($is_dir->{$item},$item, @@ -9907,25 +9932,26 @@ sub get_extracted { } if ($is_dir->{$item}) { $depth ++; - $lastcontainer = $count; - $parent->{$depth} = $lastcontainer; + push(@hierarchy,$count); + $parent->{$depth} = $count; $datatable .= &recurse_extracted_archive("$currdir/$item",$docudom,$docuname, - \$depth,\$count,\$lastcontainer, - $children,$parent,$wantform); + \$depth,\$count,\@hierarchy,$dirorder, + $children,$parent,$titles,$wantform); $depth --; - $lastcontainer = $parent->{$depth}; + pop(@hierarchy); } } return ($count,$datatable); } sub recurse_extracted_archive { - my ($currdir,$docudom,$docuname,$depth,$count,$lastcontainer, - $children,$parent,$wantform) = @_; + my ($currdir,$docudom,$docuname,$depth,$count,$hierarchy,$dirorder, + $children,$parent,$titles,$wantform) = @_; my $result=''; - unless ((ref($depth)) && (ref($count)) && (ref($lastcontainer)) && - (ref($children) eq 'HASH') && (ref($parent) eq 'HASH')) { + unless ((ref($depth)) && (ref($count)) && (ref($hierarchy) eq 'ARRAY') && + (ref($children) eq 'HASH') && (ref($parent) eq 'HASH') && + (ref($dirorder) eq 'HASH')) { return $result; } my $dirptr = 16384; @@ -9936,7 +9962,10 @@ sub recurse_extracted_archive { my ($item,undef,undef,$testdir)=split(/\&/,$dir_line,5); unless ($item =~ /^\.+$/) { $$count ++; + @{$dirorder->{$$count}} = @{$hierarchy}; + $titles->{$$count} = $item; &archive_hierarchy($$depth,$$count,$parent,$children); + my $is_dir; if ($dirptr&$testdir) { $is_dir = 1; @@ -9946,15 +9975,15 @@ sub recurse_extracted_archive { } if ($is_dir) { $$depth ++; - $$lastcontainer = $$count; - $parent->{$$depth} = $$lastcontainer; + push(@{$hierarchy},$$count); + $parent->{$$depth} = $$count; $result .= &recurse_extracted_archive("$currdir/$item",$docudom, $docuname,$depth,$count, - $lastcontainer,$children, - $parent,$wantform); + $hierarchy,$dirorder,$children, + $parent,$titles,$wantform); $$depth --; - $$lastcontainer = $parent->{$$depth}; + pop(@{$hierarchy}); } } } @@ -9983,8 +10012,10 @@ sub archive_row { if ($is_dir) { $choices{'display'} = &mt('Add as Folder'); } - my $output = &start_data_table_row()."\n"; + my $output = &start_data_table_row().''.$count.''."\n"; + my $offset = 0; foreach my $action ('display','dependency','discard') { + $offset ++; $output .= ''. ''; + if ($action eq 'dependency') { + $output .= ''; } - $output .= ' /> '.$choices{$action}.''; + $output .= ''; } $output .= '&').'" />'.(' ' x 2); @@ -10039,7 +10082,8 @@ sub archive_options_form { } sub archive_javascript { - my ($startcount,$numitems,%children) = @_; + my ($startcount,$numitems,$titles,$children) = @_; + return unless ((ref($titles) eq 'HASH') && (ref($children) eq 'HASH')); my $scripttag = < // 0) { - var startelement = $startcount + (count-1) * 5; - for (var j=1; j<4; j++) { - var item = startelement + j; - if (form.elements[item].type == 'radio') { - if (form.elements[item].checked) { - containerCheck(form,count,j); - break; + var startelement = $startcount + ((count-1) * 6); + for (var j=1; j<5; j++) { + if (j != 3) { + var item = startelement + j; + if (form.elements[item].type == 'radio') { + if (form.elements[item].checked) { + containerCheck(form,count,j); + break; + } } } } @@ -10074,35 +10124,114 @@ function propagateCheck(form,count) { } numitems = $numitems -var parents = new Array(numitems) +var titles = new Array(numitems); +var parents = new Array(numitems); for (var i=0; i $b } (keys(%children))) { - my @contents = split(/:/,$children{$container}); + foreach my $container (sort { $a <=> $b } (keys(%{$children}))) { + my @contents = split(/:/,$children->{$container}); for (my $i=0; $i<@contents; $i ++) { $scripttag .= 'parents['.$container.']['.$i.'] = '.$contents[$i]."\n"; } } + foreach my $key (sort { $a <=> $b } (keys(%{$titles}))) { + $scripttag .= "titles[$key] = '".$titles->{$key}."';\n"; + } + $scripttag .= < 0) { - var item = $startcount + ((count-1) * 5) + offset; + dependencyCheck(form,count,offset); + var item = (offset+$startcount)+6*(count-1); form.elements[item].checked = true; if(Object.prototype.toString.call(parents[count]) === '[object Array]') { if (parents[count].length > 0) { for (var j=0; j 0) { + var chosen = (offset+$startcount)+6*(count-1); + var depitem = $startcount + ((count-1) * 6) + 3; + var currtype = form.elements[depitem].type; + if (form.elements[chosen].value == 'dependency') { + document.getElementById('arc_depon_'+count).style.display='block'; + form.elements[depitem].options.length = 0; + form.elements[depitem].options[0] = new Option('Select','',true,true); + for (var i=1; i 0) { + var item = (1+offset+$startcount)+6*(count-1); + var picked = form.elements[item].options[form.elements[item].selectedIndex].value; + if (Object.prototype.toString.call(parents[count]) === '[object Array]') { + if (parents[count].length > 0) { + for (var j=0; j 0) { + var item = (offset+$startcount)+6*(count-1); + if (form.elements[item].type == 'radio') { + if (form.elements[item].value == 'dependency') { + if (form.elements[item+1].type == 'select-one') { + for (var i=0; i 0) { + for (var j=0; j END @@ -10151,19 +10280,13 @@ sub process_extracted_files { } } } - my ($output,%children,%parent); + my ($output,%children,%parent,%titles,%dirorder); if (keys(%toplevelitems) > 0) { my @contents = sort(keys(%toplevelitems)); - my ($count,undef) = &get_extracted($docudom,$docuname,$currdir,\%is_dir, - \%children,\%parent,\@contents); - } - my (@above,%hierarchy,%referrer,%orphaned,%todelete); - foreach my $depth (sort { $a <=> $b } keys(%parent)) { - push(@above,$parent{$depth}); - foreach my $item (split(/:/,$children{$parent{$depth}})) { - $hierarchy{$item} = \@above; - } + (my $count,undef) = &get_extracted($docudom,$docuname,$currdir,\%is_dir,\%children, + \%parent,\@contents,\%dirorder,\%titles); } + my (%referrer,%orphaned,%todelete,%newdest,%newseqid); if ($numitems) { for (my $i=1; $i<=$numitems; $i++) { my $path = $env{'form.archive_content_'.$i}; @@ -10178,9 +10301,9 @@ sub process_extracted_files { my ($title,$url,$outer); ($title) = ($path =~ m{/([^/]+)$}); $outer = 0; - if (ref($hierarchy{$i}) eq 'ARRAY') { - if (@{$hierarchy{$i}} > 0) { - foreach my $item (reverse(@{$hierarchy{$i}})) { + if (ref($dirorder{$i}) eq 'ARRAY') { + if (@{$dirorder{$i}} > 0) { + foreach my $item (reverse(@{$dirorder{$i}})) { if ($env{'form.archive_'.$item} eq 'display') { $outer = $item; last; @@ -10195,7 +10318,7 @@ sub process_extracted_files { next if ($fatal); if ((@archdirs > 0) && (grep(/^\Q$i\E$/,@archdirs))) { if ($context eq 'coursedocs') { - $mapinner{$i} = time; + $mapinner{$i} = time; $folders{$i} = 'default_'.$mapinner{$i}; $containers{$i} = 'sequence'; my $url = '/uploaded/'.$docudom.'/'.$docuname.'/'. @@ -10208,6 +10331,7 @@ sub process_extracted_files { &LONCAPA::map::storemap('/uploaded/'.$docudom.'/'. $docuname.'/'.$folders{$outer}. '.'.$containers{$outer},1); + $newseqid{$i} = $newidx; } } else { if ($context eq 'coursedocs') { @@ -10223,6 +10347,7 @@ sub process_extracted_files { } if (-e "$prefix$dir/$docstype/$mapinner{$outer}/$newidx") { system("mv $prefix$path $prefix$dir/$docstype/$mapinner{$outer}/$newidx/$title"); + $newdest{$i} = "$prefix$dir/$docstype/$mapinner{$outer}/$newidx"; } $LONCAPA::map::resources[$newidx]= $title.':'.$url.':false:normal:res'; @@ -10234,19 +10359,64 @@ sub process_extracted_files { } } } elsif ($env{'form.archive_'.$i} eq 'dependency') { - if (ref($hierarchy{$i}) eq 'ARRAY') { - foreach my $item (reverse(@{$hierarchy{$i}})) { - if ($env{'form.archive_'.$item} eq 'display') { - $referrer{$i} = $item; - last; - #FIXME identify as dependency in db file - #FIXME need to move item to referrer location - #FIXME need to setup httprefs so access allowed - } elsif ($env{'form.archive_'.$item} eq 'discard') { - $orphaned{$i} = $item; - last; + my ($title) = ($path =~ m{/([^/]+)$}); + $referrer{$i} = $env{'form.archive_dependent_on_'.$i}; + if ($env{'form.archive_'.$referrer{$i}} eq 'display') { + if (ref($dirorder{$i}) eq 'ARRAY') { + my ($itemidx,$fullpath); + for (my $j=0; $j<@{$dirorder{$i}}; $j++) { + if (ref($dirorder{$referrer{$i}}) eq 'ARRAY') { + my $container = $dirorder{$referrer{$i}}->[-1]; + for (my $j=0; $j<@{$dirorder{$i}}; $j++) { + if ($dirorder{$i}->[$j] eq $container) { + $itemidx = $j; + } + } + } + } + if ($itemidx ne '') { + if (grep(/^\Q$referrer{$i}\E$/,@archdirs)) { + if ($mapinner{$referrer{$i}}) { + $fullpath = "$prefix$dir/$docstype/$mapinner{$referrer{$i}}"; + for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) { + if (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) { + unless (defined($newseqid{$dirorder{$i}->[$j]})) { + $fullpath .= '/'.$titles{$dirorder{$i}->[$j]}; + if (!-e $fullpath) { + mkdir($fullpath,0755); + } + } + } else { + last; + } + } + } + } elsif ($newdest{$referrer{$i}}) { + $fullpath = $newdest{$referrer{$i}}; + for (my $j=$itemidx; $j<@{$dirorder{$i}}; $j++) { + if ($env{'form.archive_'.$dirorder{$i}->[$j]} eq 'discard') { + $orphaned{$i} = $env{'form.archive_'.$dirorder{$i}->[$j]}; + last; + } elsif (grep(/^\Q$dirorder{$i}->[$j]\E$/,@archdirs)) { + unless (defined($newseqid{$dirorder{$i}->[$j]})) { + $fullpath .= '/'.$titles{$dirorder{$i}->[$j]}; + if (!-e $fullpath) { + mkdir($fullpath,0755); + } + } + } else { + last; + } + } + } + if ($fullpath ne '') { + system("mv $prefix$path $fullpath/$title"); + } } } + } elsif ($env{'form.archive_'.$referrer{$i}} eq 'discard') { + $warning .= &mt('[_1] is a dependency of [_2], which was discarded.', + $path,$env{'form.archive_content_'.$referrer{$i}}).'
'; } } } else {