--- loncom/interface/londocs.pm 2009/01/28 13:24:54 1.328 +++ loncom/interface/londocs.pm 2025/01/07 21:01:37 1.722 @@ -1,3707 +1,10892 @@ -# The LearningOnline Network -# Documents -# -# $Id: londocs.pm,v 1.328 2009/01/28 13:24:54 ehlerst Exp $ -# -# Copyright Michigan State University Board of Trustees -# -# This file is part of the LearningOnline Network with CAPA (LON-CAPA). -# -# LON-CAPA is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# LON-CAPA is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with LON-CAPA; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# /home/httpd/html/adm/gpl.txt -# -# http://www.lon-capa.org/ -# - - - -package Apache::londocs; - -use strict; -use Apache::Constants qw(:common :http); -use Apache::imsexport; -use Apache::lonnet; -use Apache::loncommon; -use LONCAPA::map(); -use Apache::lonratedt(); -use Apache::lonxml; -use Apache::lonclonecourse; -use Apache::lonnavmaps; -use HTML::Entities; -use GDBM_File; -use Apache::lonlocal; -use Cwd; -use LONCAPA qw(:DEFAULT :match); - -my $iconpath; - -my %hash; - -my $hashtied; -my %alreadyseen=(); - -my $hadchanges; - - -my %help=(); - - -sub mapread { - my ($coursenum,$coursedom,$map)=@_; - return - &LONCAPA::map::mapread('/uploaded/'.$coursedom.'/'.$coursenum.'/'. - $map); -} - -sub storemap { - my ($coursenum,$coursedom,$map)=@_; - my ($outtext,$errtext)= - &LONCAPA::map::storemap('/uploaded/'.$coursedom.'/'.$coursenum.'/'. - $map,1); - if ($errtext) { return ($errtext,2); } - - $hadchanges=1; - return ($errtext,0); -} - - - -sub authorhosts { - my %outhash=(); - my $home=0; - my $other=0; - foreach my $key (keys(%env)) { - if ($key=~/^user\.role\.(au|ca)\.(.+)$/) { - my $role=$1; - my $realm=$2; - my ($start,$end)=split(/\./,$env{$key}); - if (($start) && ($start>time)) { next; } - if (($end) && (time>$end)) { next; } - my ($ca,$cd); - if ($1 eq 'au') { - $ca=$env{'user.name'}; - $cd=$env{'user.domain'}; - } else { - ($cd,$ca)=($realm=~/^\/($match_domain)\/($match_username)$/); - } - my $allowed=0; - my $myhome=&Apache::lonnet::homeserver($ca,$cd); - my @ids=&Apache::lonnet::current_machine_ids(); - foreach my $id (@ids) { if ($id eq $myhome) { $allowed=1; } } - if ($allowed) { - $home++; - $outhash{'home_'.$ca.'@'.$cd}=1; - } else { - $outhash{'otherhome_'.$ca.'@'.$cd}=$myhome; - $other++; - } - } - } - return ($home,$other,%outhash); -} - - -sub dumpbutton { - my ($home,$other,%outhash)=&authorhosts(); - my $type = &Apache::loncommon::course_type(); - if ($home+$other==0) { return ''; } - if ($home) { - return ''. - &Apache::loncommon::help_open_topic('Docs_Dump_Course_Docs'); - } else { - return '
-ENDPASTE - $r->print(' '); - - my $type; - if ($env{'docs.markedcopy_url'} =~ m{^(?:/adm/wrapper/ext|(?:http|https)(?::|:))//} ) { - $type = &mt('External Resource'); - $r->print($type.': '. - &LONCAPA::map::qtescape($env{'docs.markedcopy_title'}).' ('. - &LONCAPA::map::qtescape($env{'docs.markedcopy_url'}).')'); - } else { - my $extension = (split(/\./,$env{'docs.markedcopy_url'}))[-1]; - my $icon = &Apache::loncommon::icon($extension); - if ($extension eq 'sequence' && - $env{'docs.markedcopy_url'} =~ m{/default_\d+\.sequence$ }x) { - $icon = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL')); - $icon .= '/folder_closed.gif'; - } - $icon = ''; - $r->print($icon.$type.': '. &parse_supplemental_title(&LONCAPA::map::qtescape($env{'docs.markedcopy_title'}))); - } - if ($container eq 'page') { - $r->print(' - - -'); - } else { - $r->print(' - -'); - } - $r->print('
'); -} - -sub do_paste_from_buffer { - my ($coursenum,$coursedom,$folder) = @_; - - if (!$env{'form.pastemarked'}) { - return; - } - -# paste resource to end of list - my $url=&LONCAPA::map::qtescape($env{'docs.markedcopy_url'}); - my $title=&LONCAPA::map::qtescape($env{'docs.markedcopy_title'}); -# Maps need to be copied first - if (($url=~/\.(page|sequence)$/) && ($url=~/^\/uploaded\//)) { - $title=&mt('Copy of').' '.$title; - my $newid=$$.int(rand(100)).time; - my ($oldid,$ext) = ($url=~/^(.+)\.(\w+)$/); - if ($oldid =~ m{^(/uploaded/\Q$coursedom\E/\Q$coursenum\E/)(\D+)(\d+)$}) { - my $path = $1; - my $prefix = $2; - my $ancestor = $3; - if (length($ancestor) > 10) { - $ancestor = substr($ancestor,-10,10); - } - $oldid = $path.$prefix.$ancestor; - } - my $counter = 0; - my $newurl=$oldid.$newid.'.'.$ext; - my $is_unique = &uniqueness_check($newurl); - while (!$is_unique && $counter < 100) { - $counter ++; - $newid ++; - $newurl = $oldid.$newid; - $is_unique = &uniqueness_check($newurl); - } - if (!$is_unique) { - if ($url=~/\.page$/) { - return &mt('Paste failed: an error occurred creating a unique URL for the composite page'); - } else { - return &mt('Paste failed: an error occurred creating a unique URL for the folder'); - } - } - my $storefn=$newurl; - $storefn=~s{^/\w+/$match_domain/$match_username/}{}; - my $paste_map_result = - &Apache::lonclonecourse::writefile($env{'request.course.id'},$storefn, - &Apache::lonnet::getfile($url)); - if ($paste_map_result eq '/adm/notfound.html') { - if ($url=~/\.page$/) { - return &mt('Paste failed: an error occurred saving the composite page'); - } else { - return &mt('Paste failed: an error occurred saving the folder'); - } - } - $url = $newurl; - } -# published maps can only exists once, so remove it from paste buffer when done - if (($url=~/\.(page|sequence)$/) && ($url=~m {^/res/})) { - &Apache::lonnet::delenv('docs\\.markedcopy'); - } - if ($url=~ m{/smppg$}) { - my $db_name = &Apache::lonsimplepage::get_db_name($url); - if ($db_name =~ /^smppage_/) { - #simple pages, need to copy the db contents to a new one. - my %contents=&Apache::lonnet::dump($db_name,$coursedom,$coursenum); - my $now = time(); - $db_name =~ s{_\d*$ }{_$now}x; - my $result=&Apache::lonnet::put($db_name,\%contents, - $coursedom,$coursenum); - $url =~ s{/(\d*)/smppg$ }{/$now/smppg}x; - $title=&mt('Copy of').' '.$title; - } - } - $title = &LONCAPA::map::qtunescape($title); - my $ext='false'; - if ($url=~m{^http(|s)://}) { $ext='true'; } - $url = &LONCAPA::map::qtunescape($url); -# Now insert the URL at the bottom - my $newidx = &LONCAPA::map::getresidx($url); - if ($env{'docs.markedcopy_supplemental'}) { - if ($folder =~ /^supplemental/) { - $title = $env{'docs.markedcopy_supplemental'}; - } else { - (undef,undef,$title) = - &parse_supplemental_title($env{'docs.markedcopy_supplemental'}); - } - } else { - if ($folder=~/^supplemental/) { - $title=time.'___&&&___'.$env{'user.name'}.'___&&&___'. - $env{'user.domain'}.'___&&&___'.$title; - } - } - - $LONCAPA::map::resources[$newidx]= $title.':'.$url.':'.$ext.':normal:res'; - push(@LONCAPA::map::order, $newidx); - return 'ok'; -# Store the result -} - -sub uniqueness_check { - my ($newurl) = @_; - my $unique = 1; - foreach my $res (@LONCAPA::map::order) { - my ($name,$url)=split(/\:/,$LONCAPA::map::resources[$res]); - $url=&LONCAPA::map::qtescape($url); - if ($newurl eq $url) { - $unique = 0; - last; - } - } - return $unique; -} - -my %parameter_type = ( 'randompick' => 'int_pos', - 'hiddenresource' => 'string_yesno', - 'encrypturl' => 'string_yesno', - 'randomorder' => 'string_yesno',); -my $valid_parameters_re = join('|',keys(%parameter_type)); -# set parameters -sub update_parameter { - - return 0 if ($env{'form.changeparms'} !~ /^($valid_parameters_re)$/); - - my $which = $env{'form.changeparms'}; - my $idx = $env{'form.setparms'}; - if ($env{'form.'.$which.'_'.$idx}) { - my $value = ($which eq 'randompick') ? $env{'form.'.$which.'_'.$idx} - : 'yes'; - &LONCAPA::map::storeparameter($idx, 'parameter_'.$which, $value, - $parameter_type{$which}); - &remember_parms($idx,$which,'set',$value); - } else { - &LONCAPA::map::delparameter($idx,'parameter_'.$which); - - &remember_parms($idx,$which,'del'); - } - return 1; -} - - -sub handle_edit_cmd { - my ($coursenum,$coursedom) =@_; - - my ($cmd,$idx)=split('_',$env{'form.cmd'}); - - my $ratstr = $LONCAPA::map::resources[$LONCAPA::map::order[$idx]]; - my ($title, $url, @rrest) = split(':', $ratstr); - - if ($cmd eq 'del') { - if (($url=~m|/+uploaded/\Q$coursedom\E/\Q$coursenum\E/|) && - ($url!~/\.(page|sequence|problem|exam|quiz|assess|survey|form|library|task)$/)) { - &Apache::lonnet::removeuploadedurl($url); - } else { - &LONCAPA::map::makezombie($LONCAPA::map::order[$idx]); - } - splice(@LONCAPA::map::order, $idx, 1); - - } elsif ($cmd eq 'cut') { - &LONCAPA::map::makezombie($LONCAPA::map::order[$idx]); - splice(@LONCAPA::map::order, $idx, 1); - - } elsif ($cmd eq 'up' - && ($idx) && (defined($LONCAPA::map::order[$idx-1]))) { - @LONCAPA::map::order[$idx-1,$idx] = @LONCAPA::map::order[$idx,$idx-1]; - - } elsif ($cmd eq 'down' - && defined($LONCAPA::map::order[$idx+1])) { - @LONCAPA::map::order[$idx+1,$idx] = @LONCAPA::map::order[$idx,$idx+1]; - - } elsif ($cmd eq 'rename') { - - my $comment = &LONCAPA::map::qtunescape($env{'form.title'}); - if ($comment=~/\S/) { - $LONCAPA::map::resources[$LONCAPA::map::order[$idx]]= - $comment.':'.join(':', $url, @rrest); - } -# Devalidate title cache - my $renamed_url=&LONCAPA::map::qtescape($url); - &Apache::lonnet::devalidate_title_cache($renamed_url); - } else { - return 0; - } - return 1; -} - -sub editor { - my ($r,$coursenum,$coursedom,$folder,$allowed,$upload_output,$type)=@_; - - my $container= ($env{'form.pagepath'}) ? 'page' - : 'sequence'; - - my ($errtext,$fatal) = &mapread($coursenum,$coursedom, - $folder.'.'.$container); - return $errtext if ($fatal); - - if ($#LONCAPA::map::order<1) { - my $idx=&LONCAPA::map::getresidx(); - if ($idx<=0) { $idx=1; } - $LONCAPA::map::order[0]=$idx; - $LONCAPA::map::resources[$idx]=''; - } - - my ($breadcrumbtrail,$randompick,$ishidden,$isencrypted,$plain,$is_random_order)= - &breadcrumbs($folder,$allowed,$type); - $r->print($breadcrumbtrail); - -# ------------------------------------------------------------ Process commands - -# ---------------- if they are for this folder and user allowed to make changes - if (($allowed) && ($env{'form.folder'} eq $folder)) { -# set parameters and change order - &snapshotbefore(); - - if (&update_parameter()) { - ($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container); - return $errtext if ($fatal); - } - - if ($env{'form.newpos'} && $env{'form.currentpos'}) { -# change order - my $res = splice(@LONCAPA::map::order,$env{'form.currentpos'}-1,1); - splice(@LONCAPA::map::order,$env{'form.newpos'}-1,0,$res); - - ($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container); - return $errtext if ($fatal); - } - - if ($env{'form.pastemarked'}) { - my $paste_res = - &do_paste_from_buffer($coursenum,$coursedom,$folder); - if ($paste_res eq 'ok') { - ($errtext,$fatal) = &storemap($coursenum,$coursedom,$folder.'.'.$container); - return $errtext if ($fatal); - } elsif ($paste_res ne '') { - $r->print(''.$paste_res.'
'); - } - } - - $r->print($upload_output); - - if (&handle_edit_cmd()) { - ($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container); - return $errtext if ($fatal); - } -# Group import/search - if ($env{'form.importdetail'}) { - my @imports; - foreach my $item (split(/\&/,$env{'form.importdetail'})) { - if (defined($item)) { - my ($name,$url,$residx)= - map {&unescape($_)} split(/\=/,$item); - push(@imports, [$name, $url, $residx]); - } - } - ($errtext,$fatal)=&group_import($coursenum, $coursedom, $folder, - $container,'londocs',@imports); - return $errtext if ($fatal); - } -# Loading a complete map - if ($env{'form.loadmap'}) { - if ($env{'form.importmap'}=~/\w/) { - foreach my $res (&Apache::lonsequence::attemptread(&Apache::lonnet::filelocation('',$env{'form.importmap'}))) { - my ($title,$url,$ext,$type)=split(/\:/,$res); - my $idx=&LONCAPA::map::getresidx($url); - $LONCAPA::map::resources[$idx]=$res; - $LONCAPA::map::order[$#LONCAPA::map::order+1]=$idx; - } - ($errtext,$fatal)=&storemap($coursenum,$coursedom, - $folder.'.'.$container); - return $errtext if ($fatal); - } else { - $r->print(''.&mt('No map selected.').'
'); - - } - } - &log_differences($plain); - } -# ---------------------------------------------------------------- End commands -# ---------------------------------------------------------------- Print screen - my $idx=0; - my $shown=0; - if (($ishidden) || ($isencrypted) || ($randompick>=0) || ($is_random_order)) { - $r->print(''.&mt('Parameters').':
'.&mt('Caution: this folder is set to randomly pick a subset of resources. Adding or removing resources from this folder will change the set of resources that the students see, resulting in spurious or missing credit for completed problems, not limited to ones you modify. Do not modify the contents of this folder if it is in active student use.').'
'); - } - if ($is_random_order) { - $r->print(''.&mt('Caution: this folder is set to randomly order its contents. Adding or removing resources from this folder will change the order of resources shown.').'
'); - } - $r->print(''.&mt('Currently no documents.').' |
'.$errtext.'
'; - return 'failed'; - } else { - if ($parseaction eq 'parse') { - my $total_embedded = keys(%{$allfiles}); - if ($total_embedded > 0) { - my $num = 0; - my $state = ' - - - - - '; - $phase_status = 'phasetwo'; - - $$upload_output .= - 'This file contains embedded multimedia objects, which need to be uploaded to LON-CAPA.- - | -
- - | -
\n"); - foreach my $res ($navmap->retrieveResources()) { - $r->print($res->compTitle()."\t".$res->symb()."\n"); - } - $r->print("\n\n"); - } - $r->print(''.&mt('Return to DOCS').''); -} - - -sub verifycontent { - my ($r) = @_; - my $type = &Apache::loncommon::course_type(); - my $loaderror=&Apache::lonnet::overloaderror($r); - if ($loaderror) { return $loaderror; } - $r->print(&Apache::loncommon::start_page('Verify '.$type.' Documents')); - $r->print(&Apache::lonhtmlcommon::breadcrumbs('Verify '.$type.' Documents')); - $hashtied=0; - undef %alreadyseen; - %alreadyseen=(); - &tiehash(); - foreach my $key (keys(%hash)) { - if ($hash{$key}=~/\.(page|sequence)$/) { - if (($key=~/^src_/) && ($alreadyseen{&unescape($hash{$key})})) { - $r->print('
'. - &Apache::lonnet::gettitle($linkurl). - ' | ||||
'. - ' | '. - ''.$linkurl. - ' | |||
'. - ' | '. - &Apache::lonlocal::locallocaltime( - &Apache::lonnet::metadata($root.'.'.$extension, - 'lastrevisiondate') - ). - ' | '. - 'Most Recent: '. - ''.$currentversion.''. - ' | '. - 'In '.$type.': '. - ''); -# Used in course - my $usedversion=$hash{'version_'.$linkurl}; - if (($usedversion) && ($usedversion ne 'mostrecent')) { - $r->print($usedversion); - } else { - $r->print($currentversion); - } - $r->print(' | '. - 'Use: '); -# Set version - $r->print(&Apache::loncommon::select_form($setversions{$linkurl}, - 'set_version_'.$linkurl, - ('select_form_order' => - ['',1..$currentversion,'mostrecent'], - '' => '', - 'mostrecent' => 'most recent', - map {$_,$_} (1..$currentversion)))); - $r->print(' |
'); - my $lastold=1; - for (my $prevvers=1;$prevvers<$currentversion;$prevvers++) { - my $url=$root.'.'.$prevvers.'.'.$extension; - if (&Apache::lonnet::metadata($url,'lastrevisiondate')< - $starttime) { - $lastold=$prevvers; - } - } - # - # Code to figure out how many version entries should go in - # each of the four columns - my $entries_per_col = 0; - my $num_entries = ($currentversion-$lastold); - if ($num_entries % 4 == 0) { - $entries_per_col = $num_entries/4; - } else { - $entries_per_col = $num_entries/4 + 1; - } - my $entries_count = 0; - $r->print(' | ');
- my $cols_output = 1;
- for (my $prevvers=$lastold;$prevvers<$currentversion;$prevvers++) {
- my $url=$root.'.'.$prevvers.'.'.$extension;
- $r->print(''.&mt('Version').' '.$prevvers.' ('.
- &Apache::lonlocal::locallocaltime(
- &Apache::lonnet::metadata($url,
- 'lastrevisiondate')
- ).
- ')');
- if (&Apache::loncommon::fileembstyle($extension) eq 'ssi') {
- $r->print(' '.&mt('Diffs').'');
- }
- $r->print(' '); - if (++$entries_count % $entries_per_col == 0) { - $r->print(' | ');
- if ($cols_output != 4) {
- $r->print(''); - $cols_output++; - } - } - } - while($cols_output++ < 4) { - $r->print(' | ') - } - $r->print(' |
- $lt{'copm'}
-
-
- $help{'Load_Map'}
-
');
-# ''.&mt('Main Course Documents'). -# ($allowed?' '.$help{'Main_Course_Documents'}:'').''); - my $folder=$env{'form.folder'}; - if ($folder eq '' || $folder eq 'supplemental') { - $folder='default'; - $env{'form.folderpath'}='default&'.&escape(&mt('Main '.$type.' Documents')); - $uploadtag = ''; - } - my $postexec=''; - if ($folder eq 'default') { - $r->print(''); - } else { - #$postexec='self.close();'; - } - $hadchanges=0; - my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed, - $upload_output,$type); - if ($error) { - $r->print(''.$error.' '); - } - if ($hadchanges) { - &mark_hash_old(); - } - &changewarning($r,$postexec); - my $folderseq='/uploaded/'.$coursedom.'/'.$coursenum.'/default_'.time. - '.sequence'; - my $pageseq = '/uploaded/'.$coursedom.'/'.$coursenum.'/default_'.time. - '.page'; - my $container='sequence'; - if ($env{'form.pagepath'}) { - $container='page'; - } - my $readfile='/uploaded/'.$coursedom.'/'.$coursenum.'/'.$folder.'.'.$container; - - - - my $recoverform=(<
| |||
');
-# ''.&mt('Supplemental Course Documents'). -# ($allowed?' '.$help{'Supplemental'}:'').''); - my $folder=$env{'form.folder'}; - unless ($folder=~/^supplemental/) { - $folder='supplemental'; - } - if ($folder =~ /^supplemental$/ && - (($env{'form.folderpath'} =~ /^default\&/) || ($env{'form.folderpath'} eq ''))) { - $env{'form.folderpath'} = 'supplemental&'. - &escape(&mt('Supplemental '.$type.' Documents')); - } - my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$type); - if ($error) { - $r->print(''.$error.' '); - } - if ($allowed) { - my $folderseq= - '/uploaded/'.$coursedom.'/'.$coursenum.'/supplemental_'.time. - '.sequence'; - - my $path = &HTML::Entities::encode($env{'form.folderpath'},'<>&"'); - - my $supupdocform=(<- - - $checkbox - - - $lt{'comment'}: - - - - - - - $help{'Uploading_From_Harddrive'} - - -SUPDOCFORM - - my $supnewfolderform=(<
|
'. -&mt('It is recommended that you use an up-to-date virus scanner before handling this file.')."
'.&mt('No author or co-author roles on this server.').'
'); + $r->print(&endContentScreen()); + return ''; + } + my $origcrsid=$env{'request.course.id'}; + my %origcrsdata=&Apache::lonnet::coursedescription($origcrsid); + if (($env{'form.authorspace'}) && ($env{'form.authorfolder'}=~/\w/)) { +# Do the dumping + unless ($outhash{'home_'.$env{'form.authorspace'}}) { + $r->print(''.&mt('Selected Authoring Space is not on this server.').'
'. + &endContentScreen()); + return ''; + } + my ($ca,$cd)=split(/\:/,$env{'form.authorspace'}); + $r->print(''.&mt('No author or co-author roles on this server.').'
'); + $r->print(&endContentScreen()); + return ''; + } + my $docroot = $r->dir_config('lonDocRoot'); + my $is_course_home; + my @ids=&Apache::lonnet::current_machine_ids(); + if (($coursehome ne '') && (grep(/^\Q$coursehome\E$/,@ids))) { + $is_course_home = 1; + } + my $exclude = &Apache::lonnet::priv_exclude(); + my $srcurl = "/priv/$coursedom/$coursenum"; + my $srctop = $docroot.$srcurl; + my $resurl = "/res/$coursedom/$coursenum"; + my $res_exclude = &Apache::lonnet::res_exclude(); + if (($env{'form.authorspace'}) && ($env{'form.authorfolder'}=~/\w/)) { + $r->print(''. + &mt('You do not have permission to copy files and/or directories from Course Authoring Space.'). + '
'. + &endContentScreen()); + return ''; + } + unless ($outhash{'home_'.$env{'form.authorspace'}}) { + $r->print(''.&mt('Selected Authoring Space is not on this server.').'
'. + &endContentScreen()); + return ''; + } + my ($ca,$cd)=split(/\:/,$env{'form.authorspace'}); + my $desturl = "/priv/$cd/$ca"; + my $destresurl = "/res/$cd/$ca"; + my $desttop = $docroot.$desturl; + my $subdir = &clean($env{'form.authorfolder'}); + $subdir = &cleandir($subdir); + if ($subdir eq '') { + $r->print(''.&mt('After removal of disallowed characters target sub-directory name was blank.').'
'. + &endContentScreen()); + return ''; + } elsif ($subdir =~/^_+$/) { + $r->print(''.&mt('After replacement of non-alphanumeric characters with _ in target sub-directory name, nothing but underscores was left.').'
'. + &endContentScreen()); + return ''; + } + my (%tocopy,%dirs_to_make,%files_to_copy); + map { $tocopy{&unescape($_)} = 1; } &Apache::loncommon::get_env_multiple('form.copytouser'); + if (keys(%tocopy)) { + my (%subdirs,%files); + &Apache::lonnet::recursedirs($is_course_home,1,undef,$exclude,0,0,$srcurl,'',\%subdirs,\%files); + foreach my $possible (sort(keys(%tocopy))) { + if ($possible =~ m{/$}) { + my $possdir = $possible; + $possdir =~ s{^/+|/+$}{}g; + if (exists($subdirs{$possdir})) { + $dirs_to_make{$possdir} = 1; + } else { + delete($tocopy{$possible}); + } + } else { + my ($path,$fname) = ($possible =~ m{(.*/)([^/]+)$}); + my $found = 0; + if ($path eq '/') { + if (ref($files{$path}) eq 'HASH') { + if (exists($files{$path}{$fname})) { + $found = 1; + $files_to_copy{$fname} = 1; + } + } + } else { + $path =~ s{^/+|/+$}{}g; + if (ref($files{$path}) eq 'HASH') { + if (exists($files{$path}{$fname})) { + $dirs_to_make{$path} = 1; + $files_to_copy{"$path/$fname"} = 1; + $found = 1; + } + } + } + unless ($found) { + delete($tocopy{$possible}); + } + } + } + } else { + $r->print(''.&mt('No files or directories selected for copying').'
'); + $r->print(&endContentScreen()); + return ''; + } + if (keys(%tocopy)) { + my (%resdirs,%resfiles); + &Apache::lonnet::recursedirs($is_course_home,1,undef,$res_exclude,0,0,$resurl,'',\%resdirs,\%resfiles); + my ($notopdir,%newdir,%newfile,%checkdeps,%newresfile); + $r->print(''.&mt('Copy to: [_1]', + ''.$desturl.'/'.$subdir.''). + '
'."\n"); + if (keys(%dirs_to_make)) { + unless (-e $desttop.'/'.$subdir) { + mkdir($desttop.'/'.$subdir,0755); + } + if (-e $desttop.'/'.$subdir) { + foreach my $dir (sort(keys(%dirs_to_make))) { + my @dirs=split(/\//,$dir); + my $path="$desttop/$subdir"; + my $makepath=$path; + my $fail; + for (my $i=0;$i<@dirs;$i++) { + $makepath.='/'.$dirs[$i]; + unless (-e $makepath) { + unless (mkdir($makepath,0755)) { + $fail = 1; + last; + } + if (($i == scalar(@dirs)-1) && (!$fail)) { + $newdir{$dir} = 1; + } + } + } + if ($fail) { + $r->print(''.&mt('Target directory: [_1] does not exist, and could not be created.', + ''.$desturl.'/'.$subdir.'/'.$dir.''). + '
'."\n"); + } + } + } else { + $notopdir = 1; + } + } + if (keys(%files_to_copy)) { + unless (-e $desttop.'/'.$subdir) { + mkdir($desttop.'/'.$subdir,0755); + } + if (-e $desttop.'/'.$subdir) { + my $num = 0; + my ($copyright,$customdistfile); + if ($env{'form.copyright'} eq 'default' || $env{'form.copyright'} eq 'domain' || $env{'form.copyright'} eq 'public') { + $copyright = $env{'form.copyright'}; + } elsif ($env{'form.copyright'} eq 'custom') { + if ($env{'form.customrights'} =~ m{^/res/$match_domain/$match_username/.+\.rights$}) { + my ($rightsdom,$rightsuname) = ($1,$2); + my $rightshome = &Apache::lonnet::homeserver($rightsdom,$rightsuname); + if (($rightshome eq 'no_host') || ($rightshome eq '')) { + $copyright = 'default'; + } elsif (grep(/^\Q$rightshome\E$/,@ids)) { + if (-e $docroot.$env{'form.customrights'}) { + $copyright = 'custom'; + $customdistfile = $env{'form.customrights'}; + } else { + $copyright = 'default'; + } + } else { + my $rightsfile = &Apache::lonnet::filelocation('',$env{'form.customrights'}); + unless (&Apache::lonnet::getfile($rightsfile) eq '-1') { + $customdistfile = $env{'form.customrights'}; + } + } + } + } + my $sourceavail; + if ($env{'form.sourceavail'} =~ /^(open|closed)$/) { + $sourceavail = $env{'form.sourceavail'}; + } + my $respublish; + if ($env{'form.respublish'}) { + $respublish = 1; + } + my $nokeyref = &Apache::lonpublisher::getnokey($r->dir_config('lonIncludes')); + foreach my $file (keys(%files_to_copy)) { + my ($fail,$dup,$dir_is_file,$src,$dest,$path,$fname); + if ($file =~ m{/}) { + ($path,$fname) = ($file =~ m{^(.+)/([^/]+)$}); + if (-d "$desttop/$subdir/$path") { + if (-e "$desttop/$subdir/$path/$fname") { + $dup = 1; + } else { + $src = "$srctop/$path/$fname"; + $dest = "$desttop/$subdir/$path/$fname"; + } + } elsif (-f "$desttop/$subdir/$path") { + $dir_is_file = 1; + } else { + $fail = 1; + } + } elsif (-e "$desttop/$subdir/$file") { + $dup = 1; + } else { + $src = "$srctop/$file"; + $dest = "$desttop/$subdir/$file"; + $fname = $file; + } + if ($fail) { + $r->print(''.&mt('Target directory: [_1] does not exist, and could not be created.', + ''.$desturl.'/'.$subdir.'/'.$path.''). + '
'."\n"); + } elsif ($dup) { + $r->print(''.&mt('Target file: [_1] already exists -- not overwriting.', + ''.$desturl.'/'.$subdir.'/'.$file.''). + '
'."\n"); + } elsif ($dir_is_file) { + $r->print(''.&mt('Target directory: [_1] name is already in a use for a file -- not overwriting.', + ''.$desturl.'/'.$subdir.'/'.$file.''). + '
'."\n"); + } elsif (($src ne '') && ($dest ne '')) { + my $ressrc = $docroot.$resurl.'/'.$file; + my $ressrcmeta = $ressrc.'.meta'; + my ($ext) = ($file =~ /\.(\w+)$/); + my $embstyle=&Apache::loncommon::fileembstyle($ext); + my ($getres,$getresmeta); + if ($respublish) { + if ($path eq '') { + if ((ref($resfiles{'/'}) eq 'HASH') && + (exists($resfiles{'/'}{$fname}))) { + $getres = 1; + $getresmeta = 1; + } + } elsif ((ref($resfiles{$path}) eq 'HASH') && + (exists($resfiles{$path}{$fname}))) { + $getres = 1; + $getresmeta = 1; + } + } + if ($is_course_home) { + my ($needpriv,$needprivmeta); + if ($respublish) { + if ($getres) { + if (&Apache::londiff::are_different_files($src,$ressrc)) { + $needpriv = 1; + if (&File::Copy::copy($ressrc,$dest)) { + if ($embstyle eq 'ssi') { + &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd); + } + } + } else { + if (&File::Copy::copy($src,$dest)) { + $newfile{$file} = $desturl.'/'.$subdir.'/'.$file; + if ($embstyle eq 'ssi') { + &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd,$subdir); + } + } + } + } else { + $needpriv = 1; + } + if ($getresmeta) { + if ((-e $src.'.meta') && (!-e $dest.'.meta')) { + if (&Apache::londiff::are_different_files($src.'.meta',$ressrc.'.meta')) { + if (&File::Copy::copy($ressrc.'.meta',$dest.'.meta')) { + &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright, + $customdistfile,$sourceavail,\%checkdeps); + } + $needprivmeta = 1; + } else { + if (&File::Copy::copy($src.'.meta',$dest.'.meta')) { + &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright, + $customdistfile,$sourceavail,\%checkdeps); + } + } + } + } + if ($getres) { + my $destresfile = $docroot.$destresurl.'/'.$subdir.'/'.$file; + if (-e $dest) { + my $output = &Apache::lonpublisher::batchpublish($r,$dest,$destresfile,$nokeyref,1); + if (-e $destresfile) { + $newresfile{$file} = $destresurl.'/'.$subdir.'/'.$file; + } + } + } + } else { + $needpriv = 1; + if ((-e $src.'.meta') && (!-e $dest.'.meta')) { + $needprivmeta = 1; + } + } + if ($needpriv) { + if (&File::Copy::copy($src,$dest)) { + $newfile{$file} = $desturl.'/'.$subdir.'/'.$file; + if ($embstyle eq 'ssi') { + &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd,$subdir); + } + } + } + if ($needprivmeta) { + if (&File::Copy::copy($src.'.meta',$dest.'.meta')) { + &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright, + $customdistfile,$sourceavail,\%checkdeps); + } + } + } else { + my ($needpriv,$needprivmeta); + if ($respublish) { + if ($getres) { + &Apache::lonnet::repcopy($docroot.$resurl.'/'.$file); + } + if ($getresmeta) { + &Apache::lonnet::repcopy($docroot.$resurl.'/'.$file.'.meta'); + } + if (-e $docroot.$resurl.'/'.$file) { + if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file,$dest) eq 'ok') { + if (&Apache::londiff::are_different_files($docroot.$resurl.'/'.$file,$dest)) { + $needpriv = 1; + if (&File::Copy::copy($docroot.$resurl.'/'.$file,$dest)) { + if ($embstyle eq 'ssi') { + &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd); + } + } + } else { + if ($embstyle eq 'ssi') { + &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd,$subdir); + } + $newfile{$file} = $desturl.'/'.$subdir.'/'.$file; + } + } + } else { + $needpriv = 1; + } + if (-e $docroot.$resurl.'/'.$file.'.meta') { + if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file.'.meta',$dest.'.meta') eq 'ok') { + if (&Apache::londiff::are_different_files($docroot.$resurl.'/'.$file.'.meta',$dest.'.meta')) { + $needprivmeta = 1; + if (&File::Copy::copy($docroot.$resurl.'/'.$file.'.meta',$dest.'.meta')) { + &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright, + $customdistfile,$sourceavail,\%checkdeps); + } + } else { + &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright, + $customdistfile,$sourceavail,\%checkdeps); + } + } + } else { + if (!-e $dest.'.meta') { + $needprivmeta = 1; + } + } + if ($getres) { + my $destresfile = $docroot.$destresurl.'/'.$subdir.'/'.$file; + if (-e $dest) { + my $output = &Apache::lonpublisher::batchpublish($r,$dest,$destresfile,$nokeyref,1); + if (-e $destresfile) { + $newresfile{$file} = $destresurl.'/'.$subdir.'/'.$file; + } + } + } + } else { + $needpriv = 1; + if (!-e $dest.'.meta') { + $needprivmeta = 1; + } + } + if ($needpriv) { + if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file,$dest) eq 'ok') { + if ($embstyle eq 'ssi') { + &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd,$subdir); + } + $newfile{$file} = $desturl.'/'.$subdir.'/'.$file; + } + } + if ($needprivmeta) { + if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file.'.meta',$dest.'.meta') eq 'ok') { + &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright, + $customdistfile,$sourceavail,\%checkdeps); + } + } + } + } + } + } else { + $notopdir = 1; + } + } + if ($notopdir) { + $r->print(''.&mt('No files or sub-directories copied').'
'."\n".
+ ''.&mt('Target directory: [_1] does not exist, and could not be created.',
+ ''.$desturl.'/'.$subdir.'').
+ '
'.&mt('Created the following directories in [_1]:',''.$desturl.'/'.$subdir.''). + '
'."\n". + ''.&mt('Copied the following files to [_1]:',''.$desturl.'/'.$subdir.''). + '
'."\n". + ''.&mt('You may also need to copy the following missing dependencies for files copied to [_1]:', + ''.$desturl.'/'.$subdir.''). + '
'."\n". + ''.&mt('No currently existing files or directories in Course Authoring Space selected for copying').'
'); + $r->print(&endContentScreen()); + return ''; + } + } else { + my $chkname = 'copytouser'; + my $context = 'crsauthored'; + my (%subdirs,%files,@dirs_by_depth,@files_by_depth,%parent,%children,%hierarchy,@checked_maps); + &Apache::lonnet::recursedirs($is_course_home,1,undef,$exclude,0,0,$srcurl,'',\%subdirs,\%files,1); + foreach my $key (keys(%subdirs)) { + next if (($key eq '/') || ($key eq '')); + my @items = split(/\//,$key); + my $dir = pop(@items); + my $depth = scalar(@items); + my $path; + if (!$depth) { + $path = '/'; + } else { + $path = join('/',@items); + } + $dirs_by_depth[$depth]{$path}{$dir} = 1; + } + foreach my $path (keys(%files)) { + next if ($path eq ''); + my $depth; + if ($path eq '/') { + $depth = 0; + } else { + $depth = scalar(split(/\//,$path)); + } + if (ref($files{$path}) eq 'HASH') { + foreach my $file (keys(%{$files{$path}})) { + $files_by_depth[$depth]{$path}{$file} = $files{$path}{$file}; + } + } + } + my ($info,$display,$onsubmit,$togglebuttons,$disabled); + my (%resdirs,%resfiles); + &Apache::lonnet::recursedirs($is_course_home,1,undef,$res_exclude,0,0,$resurl,'',\%resdirs,\%resfiles); + my $numpub = 0; + if (keys(%resfiles)) { + foreach my $dir (keys(%resfiles)) { + if (ref($resfiles{$dir}) eq 'HASH') { + foreach my $file (keys(%{$resfiles{$dir}})) { + if (exists($files{$dir}{$file})) { + $numpub ++; + } + } + } + } + } + if ($readonly) { + $disabled = ' disabled="disabled"'; + } + if ($disabled) { + $togglebuttons = '