--- loncom/interface/londocs.pm 2005/06/09 18:01:29 1.183 +++ loncom/interface/londocs.pm 2005/06/13 20:23:53 1.189 @@ -1,7 +1,7 @@ # The LearningOnline Network # Documents # -# $Id: londocs.pm,v 1.183 2005/06/09 18:01:29 www Exp $ +# $Id: londocs.pm,v 1.189 2005/06/13 20:23:53 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -679,7 +679,7 @@ sub process_content { if ($contents) { $$content_file = &store_template($contents,$tempexport,$count,$content_type); } - } elsif ($symb =~ m-lib/templates/examupload\.problem-m) { + } elsif ($symb =~ m-lib/templates/examupload\.problem$-) { $content_type = 'examupload'; } elsif ($symb =~ m-adm/(\w+)/(\w+)/(\d+)/bulletinboard$-) { $content_type = 'bulletinboard'; @@ -840,8 +840,7 @@ sub group_import { ''."\n". ''; $env{'form.output'}=$newmapstr; - my $home=&Apache::lonnet::homeserver($coursenum,$coursedom); - my $result=&Apache::lonnet::finishuserfileupload($coursenum,$coursedom,$home, + my $result=&Apache::lonnet::finishuserfileupload($coursenum,$coursedom, 'output',$1.$2); if ($result != m|^/uploaded/|) { $errtext.='Map not saved: A network error occured when trying to save the new map. '; @@ -904,7 +903,7 @@ sub breadcrumbs { } sub editor { - my ($r,$coursenum,$coursedom,$folder,$allowed)=@_; + my ($r,$coursenum,$coursedom,$folder,$allowed,$upload_output)=@_; my $errtext=''; my $fatal=0; my $container='sequence'; @@ -1032,56 +1031,17 @@ sub editor { } } -# upload a file, if present - if (($env{'form.uploaddoc.filename'}) && - ($env{'form.cmd'}=~/^upload_(\w+)/)) { - if ( ($folder=~/^$1/) || ($1 eq 'default') ) { - my $destination = 'docs/'; - if ($folder eq 'default') { - $destination .= 'default/'; - } elsif ($folder =~ /^default_(\d+)$/) { - $destination .= $1.'/'; - } -# this is for a course, not a user, so set coursedoc flag -# probably the only place in the system where this should be "1" - - my $newidx=&Apache::lonratedt::getresidx(); - $destination .= $newidx; - my $url=&Apache::lonnet::userfileupload('uploaddoc',1,$destination); - - my $ext='false'; - if ($url=~/^http\:\/\//) { $ext='true'; } - $url=~s/\:/\:/g; - my $comment=$env{'form.comment'}; - $comment=~s/\/\>\;/g; - $comment=~s/\:/\:/g; - if ($folder=~/^supplemental/) { - $comment=time.'___&&&___'.$env{'user.name'}.'___&&&___'. - $env{'user.domain'}.'___&&&___'.$comment; - } - $Apache::lonratedt::resources[$newidx]= - $comment.':'.$url.':'.$ext.':normal:res'; - $Apache::lonratedt::order[$#Apache::lonratedt::order+1]= - $newidx; - - ($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container); - if ($fatal) { - $r->print('

'.$errtext.'

'); - return; - } - } - } + $r->print($upload_output); if ($env{'form.cmd'}) { my ($cmd,$idx)=split(/\_/,$env{'form.cmd'}); if ($cmd eq 'del') { my (undef,$url)=split(':',$Apache::lonratedt::resources[$Apache::lonratedt::order[$idx]]); - if ($url=~m|/+uploaded/\Q$coursedom\E/\Q$coursenum\E/|) { - unless ($url=~/\.(page|sequence)$/) { - &Apache::lonnet::removeuploadedurl($url); - } + if (($url=~m|/+uploaded/\Q$coursedom\E/\Q$coursenum\E/|) && + ($url!~/\.(page|sequence|problem|exam|quiz|assess|survey|form|library)$/)) { + &Apache::lonnet::removeuploadedurl($url); + } else { + &Apache::lonratedt::makezombie($Apache::lonratedt::order[$idx]); } - &Apache::lonratedt::makezombie($Apache::lonratedt::order[$idx]); for (my $i=$idx;$i<$#Apache::lonratedt::order;$i++) { $Apache::lonratedt::order[$i]= $Apache::lonratedt::order[$i+1]; @@ -1214,6 +1174,127 @@ FOLDERINFO } } +sub process_file_upload { + my ($upload_output,$coursenum,$coursedom,$allfiles,$codebase) = @_; +# upload a file, if present + my $parseaction; + if ($env{'form.parserflag'}) { + $parseaction = 'parse'; + } + my $phase_status; + my $folder=$env{'form.folder'}; + if ($folder eq '' || $folder eq 'supplemental') { + $folder='default'; + } + if ( ($folder=~/^$1/) || ($1 eq 'default') ) { + my $errtext=''; + my $fatal=0; + my $container='sequence'; + if ($env{'form.pagepath'}) { + $container='page'; + } + ($errtext,$fatal)= + &mapread($coursenum,$coursedom,$folder.'.'.$container); + if ($#Apache::lonratedt::order<1) { + $Apache::lonratedt::order[0]=1; + $Apache::lonratedt::resources[1]=''; + } + if ($fatal) { + return 'failed'; + } + my $destination = 'docs/'; + if ($folder eq 'default') { + $destination .= 'default/'; + } elsif ($folder =~ /^default_(\d+)$/) { + $destination .= $1.'/'; + } +# this is for a course, not a user, so set coursedoc flag +# probably the only place in the system where this should be "1" + my $newidx=&Apache::lonratedt::getresidx(); + $destination .= $newidx; + my $url=&Apache::lonnet::userfileupload('uploaddoc',1,$destination,$parseaction,$allfiles,$codebase); + my $ext='false'; + if ($url=~/^http\:\/\//) { $ext='true'; } + $url=~s/\:/\:/g; + my $comment=$env{'form.comment'}; + $comment=~s/\/\>\;/g; + $comment=~s/\:/\:/g; + if ($folder=~/^supplemental/) { + $comment=time.'___&&&___'.$env{'user.name'}.'___&&&___'. + $env{'user.domain'}.'___&&&___'.$comment; + } + + $Apache::lonratedt::resources[$newidx]= + $comment.':'.$url.':'.$ext.':normal:res'; + $Apache::lonratedt::order[$#Apache::lonratedt::order+1]= + $newidx; + ($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container); + if ($fatal) { + $$upload_output .= '

'.$errtext.'

'; + return 'failed'; + } else { + if ($parseaction eq 'parse') { + my $total_embedded = keys %{$allfiles}; + if ($total_embedded > 0) { + my $num = 0; + $$upload_output .= 'This file contains embedded multimedia objects, which need to be uploaded to LON-CAPA.
+
+ + + + '; + $$upload_output .= 'Upload embedded files:
+ '; + foreach my $embed_file (keys %{$allfiles}) { + $$upload_output .= ''; + $num ++; + } + $phase_status = 'phasetwo'; + $$upload_output .= '
'.$embed_file. + ' + '; + my $attrib; + if (@{$$allfiles{$embed_file}} > 1) { + $attrib = join(':',@{$$allfiles{$embed_file}}); + } else { + $attrib = $$allfiles{$embed_file}[0]; + } + $$upload_output .= + ''; + if (exists($$codebase{$embed_file})) { + $$upload_output .= + ''; + } + $$upload_output .= '

+ +
'; + } else { + $$upload_output .= 'No embedded items identified
'; + } + } + } + } + return $phase_status; +} + +sub process_secondary_uploads { + my ($upload_output,$coursedom,$coursenum,$formname,$num,$newidx) = @_; + my $folder=$env{'form.folder'}; + my $destination = 'docs/'; + if ($folder eq 'default') { + $destination .= 'default/'; + } elsif ($folder =~ /^default_(\d+)$/) { + $destination .= $1.'/'; + } + $destination .= $newidx; + my ($url,$filename); + $url=&Apache::lonnet::userfileupload($formname.$num,1,$destination); + ($filename) = ($url =~ m-^/uploaded/$coursedom/$coursenum/$destination/(.+)$-); + return $filename; +} + # --------------------------------------------------------------- An entry line sub entryline { @@ -1867,15 +1948,15 @@ sub changewarning { if (!defined($message)) { $message='Changes will become active for your current session after [_1], or the next time you log in.'; } - $r->print( -''. -'
'. + $r->print("\n\n". +''."\n". +''. '

'. &mt($message,' '). -$help{'Caching'}.'

'); +$help{'Caching'}.''."\n\n"); } # ================================================================ Main Handler @@ -2120,7 +2201,7 @@ function removeres(folderpath,index,oldt } function cutres(folderpath,index,oldtitle,container,pagesymb) { - if (confirm('WARNING: Cutting a resource makes associated grades and scores inaccessible,\\neven if it is pasted in again elsewhere!\\nCut "'+oldtitle+'"?')) { + if (confirm('WARNING: Cutting a resource makes associated grades and scores inaccessible!\\nGrades remain inaccessible if resource is pasted into another folder.\\nCut "'+oldtitle+'"?')) { this.document.forms.renameform.cmd.value='cut_'+index; this.document.forms.renameform.markcopy.value=index; if (container == 'sequence') { @@ -2155,7 +2236,88 @@ ENDNEWSCRIPT &Apache::loncommon::bodytag('Course Documents','',$events, '','',$showdoc). &Apache::loncommon::help_open_menu('','','','',273,'RAT')); - unless ($showdoc) { + my %allfiles = (); + my %codebase = (); + my ($upload_result,$upload_output); + if ($allowed) { + if (($env{'form.uploaddoc.filename'}) && ($env{'form.cmd'}=~/^upload_(\w+)/)) { +# Process file upload - phase one - upload and parse primary file. + $upload_result = &process_file_upload(\$upload_output,$coursenum,$coursedom,\%allfiles,\%codebase); + if ($upload_result eq 'phasetwo') { + $r->print($upload_output); + } + } elsif ($env{'form.phasetwo'}) { + my %newname = (); + my %origname = (); + my %attribs = (); + my $updateflag = 0; + my $residx = $env{'form.newidx'}; + my $primary_url = &Apache::lonnet::unescape($env{'form.primaryurl'}); +# Process file upload - phase two - gather secondary files. + for (my $i=0; $i<$env{'form.phasetwo'}; $i++) { + if ($env{'form.embedded_item_'.$i.'.filename'}) { + my $javacodebase; + $newname{$i} = &process_secondary_uploads(\$upload_output,$coursedom,$coursenum,'embedded_item_',$i,$residx); + $origname{$i} = &Apache::lonnet::unescape($env{'form.embedded_orig_'.$i}); + if (exists($env{'form.embedded_codebase_'.$i})) { + $javacodebase = &Apache::lonnet::unescape($env{'form.embedded_codebase_'.$i}); + $origname{$i} =~ s#^\Q$javacodebase\E/##; + } + my @attributes = (); + if ($env{'form.embedded_attrib_'.$i} =~ /:/) { + @attributes = split/:/,$env{'form.embedded_attrib_'.$i}; + } else { + @attributes = ($env{'form.embedded_attrib_'.$i}); + } + foreach (@attributes) { + push(@{$attribs{$i}},&Apache::lonnet::unescape($_)); + } + if ($javacodebase) { + $codebase{$i} = $javacodebase; + $codebase{$i} =~ s#/$##; + $updateflag = 1; + } + } + unless ($newname{$i} eq $origname{$i}) { + $updateflag = 1; + } + } +# Process file upload - phase three - modify primary file + if ($updateflag) { + my ($content,$rtncode); + my $updateflag = 0; + my $getstatus = &Apache::lonnet::getuploaded('GET',$primary_url,$coursedom,$coursenum,\$content,\$rtncode); + if ($getstatus eq 'ok') { + foreach my $item (keys %newname) { + if ($newname{$item} ne $origname{$item}) { + my $attrib_regexp = ''; + if (@{$attribs{$item}} > 1) { + $attrib_regexp = join('|',@{$attribs{$item}}); + } else { + $attrib_regexp = $attribs{$item}[0]; + } + if ($content =~ m#($attrib_regexp\s*=\s*['"]?)\Q$origname{$item}\E(['"]?)#) { + } + $content =~ s#($attrib_regexp\s*=\s*['"]?)\Q$origname{$item}\E(['"]?)#$1$newname{$item}$2#gi; + } + if (exists($codebase{$item})) { + $content =~ s/(codebase\s*=\s*["']?)\Q$codebase{$item}\E(["']?)/$1.$2/i; + } + } +# Save edited file. + my $saveresult; + my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'}; + my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'}; + my $docuhome=$env{'course.'.$env{'request.course.id'}.'.home'}; + my $url = &Apache::lonnet::store_edited_file($primary_url,$content,$docudom,$docuname,$docuhome,\$saveresult); + } else { + &Apache::lonnet::logthis('retrieval of uploaded file - '.$primary_url.' - for editing, failed: '.$getstatus); + } + } + } + } + + unless ($showdoc || $upload_result eq 'phasetwo') { # ----------------------------------------------------------------------------- my %lt=&Apache::lonlocal::texthash( 'uplm' => 'Upload a new main course document', @@ -2169,6 +2331,7 @@ ENDNEWSCRIPT 'impo' => 'Import', 'selm' => 'Select Map', 'load' => 'Load Map', + 'reco' => 'Recover Deleted Resources', 'newf' => 'New Folder', 'newp' => 'New Composite Page', 'extr' => 'External Resource', @@ -2183,7 +2346,8 @@ ENDNEWSCRIPT 'imsf' => 'Import IMS package', 'file' => 'File', 'title' => 'Title', - 'comment' => 'Comment' + 'comment' => 'Comment', + 'parse' => 'If HTML file, upload embedded images/multimedia files' ); # ----------------------------------------------------------------------------- if ($allowed) { @@ -2250,7 +2414,7 @@ ENDCOURSEVERIFY #$postexec='self.close();'; } $hadchanges=0; - &editor($r,$coursenum,$coursedom,$folder,$allowed); + &editor($r,$coursenum,$coursedom,$folder,$allowed,$upload_output); if ($hadchanges) { &mark_hash_old() } @@ -2259,7 +2423,11 @@ ENDCOURSEVERIFY '.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; $r->print(< $lt{'uplm'} @@ -2275,6 +2443,13 @@ $lt{'title'}:
$uploadtag +
+ +$lt{'parse'}? + + +
+
$help{'Uploading_From_Harddrive'} @@ -2300,6 +2475,10 @@ value="$lt{'selm'}"> + + ENDFORM unless ($env{'form.pagepath'}) { $r->print(<print(''); } else { + unless ($upload_result eq 'phasetwo') { # -------------------------------------------------------- This is showdoc mode - $r->print("

".&mt('Uploaded Document').' - '. + $r->print("

".&mt('Uploaded Document').' - '. &Apache::lonnet::gettitle($r->uri).'

'. &mt('It is recommended that you use an up-to-date virus scanner before handling this file.')."

". - &entryline(0,&mt("Click to download or use your browser's Save Link function"),$showdoc).'

'); + &entryline(0,&mt("Click to download or use your browser's Save Link function"),$showdoc).'

'); + } } } $r->print('');