[LON-CAPA-cvs] cvs: loncom /interface loncommon.pm portfolio.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Mon, 16 Jun 2008 23:34:12 -0000


This is a MIME encoded message

--raeburn1213659252
Content-Type: text/plain

raeburn		Mon Jun 16 19:34:12 2008 EDT

  Modified files:              
    /loncom/interface	portfolio.pm loncommon.pm 
  Log:
  - &upload_embedded() moved from portfolio.pm to loncommon.pm to be more widely available.
  - &check_for_upload() to prevent overwriting of existing files with uploaded embedded objects moved from portfolio.pm to loncommon.pm.
  - &suppress_embed_prompt() added to allow course-wide suppression of embedded object checking for uploded web pages for students in course context.
  - checkbox added to allow users to opt out of embedded object checking when uploading web pages to portfolio (default is to check). 
  
  
--raeburn1213659252
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20080616193412.txt"

Index: loncom/interface/portfolio.pm
diff -u loncom/interface/portfolio.pm:1.194 loncom/interface/portfolio.pm:1.195
--- loncom/interface/portfolio.pm:1.194	Mon Jun  9 18:34:55 2008
+++ loncom/interface/portfolio.pm	Mon Jun 16 19:34:12 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # portfolio browser
 #
-# $Id: portfolio.pm,v 1.194 2008/06/09 22:34:55 raeburn Exp $
+# $Id: portfolio.pm,v 1.195 2008/06/16 23:34:12 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -103,6 +103,17 @@
 	# FIXME: This line should be deleted once Portfolio uses breadcrumbs
 	$r->print(&Apache::loncommon::help_open_topic('Portfolio About', &mt('Help on the portfolio')));
 
+        my $parse_check;
+        if (!&suppress_embed_prompt()) {
+            $parse_check = <<"END";
+        <br />
+        <span class="LC_nobreak">
+         <label>$text{'parse'}
+         <input type="checkbox" name="parserflag" checked="checked" />
+         </label>
+        </span>
+END
+        }
         $r->print(<<"TABLE"); 
 <table id="LC_portfolio_actions">
   <tr id="LC_portfolio_upload">
@@ -117,13 +128,9 @@
 	<input type="hidden" name="action" value="$env{"form.action"}" />
 	<input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
 	<input type="hidden" name="mode" value="$env{"form.mode"}" />
-	<input type="submit" name="storeupl" value="$text{'upload'}" />$help_fileupload
-        <br />
-        <span class="LC_nobreak">
-         <label>$text{'parse'}
-         <input type="checkbox" name="parserflag" checked="checked" />
-         </label>
-        </span>
+	<input type="submit" name="storeupl" value="$text{'upload'}" />
+$help_fileupload
+$parse_check
       </form>
     </td>
   </tr>
@@ -1809,10 +1816,17 @@
 sub upload {
     my ($r,$url,$group)=@_;
     my $fname=&Apache::lonnet::clean_filename($env{'form.uploaddoc.filename'});
-
-    my ($state,$msg) = &check_for_upload($env{'form.currentpath'},
-					 $fname,$group,'uploaddoc');
-
+    my $disk_quota = &get_quota($group);
+    my $portfolio_root = &get_portfolio_root();
+    my $port_path = &get_port_path();
+    my ($uname,$udom) = &get_name_dom($group);
+    my $getpropath = 1;
+    my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
+    my ($state,$msg) = 
+        &Apache::loncommon::check_for_upload($env{'form.currentpath'},$fname,
+		                             $group,'uploaddoc',$portfolio_root,
+                                             $port_path,$disk_quota,
+                                             $current_disk_usage,$uname,$udom);
     if ($state eq 'will_exceed_quota'
 	|| $state eq 'file_locked'
 	|| $state eq 'file_exists' ) {
@@ -1820,7 +1834,6 @@
 	return;
     }
 
-    my $port_path = &get_port_path();
     my (%allfiles,%codebase,$mode);
     if ($env{'form.uploaddoc.filename'} =~ m/(\.htm|\.html|\.shtml)$/i) {
         if ($env{'form.parserflag'}) {
@@ -1837,73 +1850,27 @@
 	$r->print(&done('Back',$url));
     } else {
 	if (%allfiles) {
-	    my $state = <<STATE;
+            if (!&suppress_embed_prompt()) {
+	        my $state = <<STATE;
     <input type="hidden" name="action"      value="upload_embedded" />
     <input type="hidden" name="currentpath" value="$env{'form.currentpath'}" />
     <input type="hidden" name="fieldname"   value="$env{'form.fieldname'}" />
     <input type="hidden" name="mode"        value="$env{'form.mode'}" />
 STATE
-            $r->print("<h2>".&mt("Reference Warning")."</h2>");
-            $r->print("<p>".&mt("Completed upload of the file. This file contained references to other files. You must upload the referenced files or else the uploaded file may not work properly.")."</p>");
-            $r->print("<p>".&mt("Please select the locations from which the referenced files are to be uploaded.")."</p>");
-	    $r->print(&Apache::loncommon::ask_for_embedded_content('/adm/portfolio',$state,\%allfiles,\%codebase,
+                $r->print("<h2>".&mt("Reference Warning")."</h2>");
+                $r->print("<p>".&mt("Completed upload of the file. This file contained references to other files. You must upload the referenced files or else the uploaded file may not work properly.")."</p>");
+                $r->print("<p>".&mt("Please select the locations from which the referenced files are to be uploaded.")."</p>");
+	        $r->print(&Apache::loncommon::ask_for_embedded_content('/adm/portfolio',$state,\%allfiles,\%codebase,
 				      {'error_on_invalid_names'   => 1,
 				       'ignore_remote_references' => 1,}));
-	    $r->print('<p>Or '.&done('Return to directory',$url).'</p>');
+	        $r->print('<p>Or '.&done('Return to directory',$url).'</p>');
+            }
 	} else {
 	    $r->print(&done(undef,$url));
 	}
     }
 }
 
-sub upload_embedded {
-    my ($r,$url,$group)=@_;
-    for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
-	next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
-	my $orig_uploaded_filename = 
-	    $env{'form.embedded_item_'.$i.'.filename'};
-
-	$env{'form.embedded_orig_'.$i} = 
-	    &unescape($env{'form.embedded_orig_'.$i});
-	my ($path,$fname) = 
-	    ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
-	# no path, whole string is fname
-	if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
-
-	$path = $env{'form.currentpath'}.$path;
-	$fname = &Apache::lonnet::clean_filename($fname);
-
-	my ($state,$msg) = &check_for_upload($path,$fname,$group,
-					     'embedded_item_'.$i);
-
-	if ($state eq 'will_exceed_quota'
-	    || $state eq 'file_locked'
-	    || $state eq 'file_exists' ) {
-	    $r->print($msg);
-	    next;
-	}
-
-	my $port_path = &get_port_path();
-	my $src_path = $env{'form.embedded_orig_'.$i};
-	$env{'form.embedded_item_'.$i.'.filename'}=$fname;
-	
-	my $result=
-	    &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
-					    $port_path.$path);
-	if ($result !~ m|^/uploaded/|) {
-	    $r->print('<span class="LC_error">'
-                      .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
-                           ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
-                      .'</span><br />');
-	    next;
-	} else {
-	    $r->print("<p> Uploaded ".
-		      &display_file($port_path.$path,$fname).'</p>');
-	}
-    }
-    $r->print(&done(undef,$url));
-}
-
 sub lock_info {
     my ($r,$url,$group) = @_;
     my ($uname,$udom) = &get_name_dom($group);
@@ -2143,6 +2110,17 @@
     return $disk_quota;
 }
 
+sub suppress_embed_prompt {
+    my $suppress_prompt = 0;
+    if (($env{'request.role'} =~ /^st/) && ($env{'request.course.id'} ne '')) {
+        if ($env{'course.'.$env{'request.course.id'}.'.suppress_embed_prompt'} eq 'yes') {
+            $suppress_prompt = 1;
+        }
+    }
+    return $suppress_prompt;
+}
+
+
 sub handler {
     # this handles file management
     my $r = shift;
@@ -2267,7 +2245,14 @@
         }
     } elsif ($env{'form.action'} eq 'upload_embedded') {
 	if ($can_upload) {
-	    &upload_embedded($r,$url,$group);
+            my $disk_quota = &get_quota($group);
+            my $getpropath = 1;
+            my $current_disk_usage = 
+                &Apache::lonnet::diskusage($udom,$uname,$portfolio_root,$getpropath);
+	    $r->print(
+                &Apache::loncommon::upload_embedded('portfolio',$port_path,$uname,$udom,
+                    $group,$portfolio_root,$group,$disk_quota,$current_disk_usage));
+            $r->print(&done(undef,$url));
         } else {
             &missing_priv($r,$url,'upload');
         }
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.660 loncom/interface/loncommon.pm:1.661
--- loncom/interface/loncommon.pm:1.660	Mon Jun  9 18:34:55 2008
+++ loncom/interface/loncommon.pm	Mon Jun 16 19:34:12 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.660 2008/06/09 22:34:55 raeburn Exp $
+# $Id: loncommon.pm,v 1.661 2008/06/16 23:34:12 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -7041,7 +7041,7 @@
    <form name="upload_embedded" action="'.$actionurl.'"
                   method="post" enctype="multipart/form-data">';
     $upload_output .= $state;
-    $upload_output .= '<b>Upload embedded files</b>:<br />'.start_data_table();
+    $upload_output .= '<b>Upload embedded files</b>:<br />'.&start_data_table();
 
     my $num = 0;
     foreach my $embed_file (sort {lc($a) cmp lc($b)} keys(%{$allfiles})) {
@@ -7056,9 +7056,8 @@
             $upload_output.='<span class="LC_warning">'.&mt("Invalid characters").'</span>';
 
         } else {
-
             $upload_output .='
-           <input name="embedded_item_'.$num.'" type="file" value="bob" />
+           <input name="embedded_item_'.$num.'" type="file" value="" />
            <input name="embedded_orig_'.$num.'" type="hidden" value="'.&escape($embed_file).'" />';
             my $attrib = join(':',@{$$allfiles{$embed_file}});
             $upload_output .=
@@ -7083,6 +7082,186 @@
     return $upload_output;
 }
 
+sub upload_embedded {
+    my ($context,$dirpath,$uname,$udom,$dir_root,$url_root,$group,$disk_quota,
+        $current_disk_usage) = @_;
+    my $output;
+    for (my $i=0; $i<$env{'form.number_embedded_items'}; $i++) {
+        next if (!exists($env{'form.embedded_item_'.$i.'.filename'}));
+        my $orig_uploaded_filename =
+            $env{'form.embedded_item_'.$i.'.filename'};
+
+        $env{'form.embedded_orig_'.$i} =
+            &unescape($env{'form.embedded_orig_'.$i});
+        my ($path,$fname) =
+            ($env{'form.embedded_orig_'.$i} =~ m{(.*/)([^/]*)});
+        # no path, whole string is fname
+        if (!$fname) { $fname = $env{'form.embedded_orig_'.$i} };
+
+        $path = $env{'form.currentpath'}.$path;
+        $fname = &Apache::lonnet::clean_filename($fname);
+        # See if there is anything left
+        next if ($fname eq '');
+
+        # Check if file already exists as a file or directory.
+        my ($state,$msg);
+        if ($context eq 'portfolio') {
+            my $port_path = $dirpath;
+            if ($group ne '') {
+                $port_path = "groups/$group/$port_path";
+            }
+            ($state,$msg) = &check_for_upload($path,$fname,$group,'embedded_item_'.$i,
+                                              $dir_root,$port_path,$disk_quota,
+                                              $current_disk_usage,$uname,$udom);
+            if ($state eq 'will_exceed_quota'
+                || $state eq 'file_locked'
+                || $state eq 'file_exists' ) {
+                $output .= $msg;
+                next;
+            }
+        } elsif (($context eq 'author') || ($context eq 'testbank')) {
+            ($state,$msg) = &check_for_existing($path,$fname,'embedded_item_'.$i);
+            if ($state eq 'exists') {
+                $output .= $msg;
+                next;
+            }
+        }
+        # Check if extension is valid
+        if (($fname =~ /\.(\w+)$/) &&
+            (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
+            $output .= &mt('Invalid file extension ([_1]) - reserved for LONCAPA use - rename the file with a different extension and re-upload. ',$1);
+            next;
+        } elsif (($fname =~ /\.(\w+)$/) &&
+                 (!defined(&Apache::loncommon::fileembstyle($1)))) {
+            $output .= &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
+            next;
+        } elsif ($fname=~/\.(\d+)\.(\w+)$/) {
+            $output .= &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
+            next;
+        }
+
+        $env{'form.embedded_item_'.$i.'.filename'}=$fname;
+        if ($context eq 'portfolio') {
+            my $result=
+                &Apache::lonnet::userfileupload('embedded_item_'.$i,'',
+                                                $dirpath.$path);
+            if ($result !~ m|^/uploaded/|) {
+                $output .= '<span class="LC_error">'
+                      .&mt('An error occurred ([_1]) while trying to upload [_2] for embedded element [_3].'
+                           ,$result,$orig_uploaded_filename,$env{'form.embedded_orig_'.$i})
+                      .'</span><br />';
+                next;
+            } else {
+                $output .= '<p>'.&mt('Uploaded [_1]','<span class="LC_filename">'.
+                           $path.$fname.'</span>').'</p>';     
+            }
+        } else {
+# Save the file
+            my $target = $env{'form.embedded_item_'.$i};
+            my $fullpath = $dir_root.$dirpath.'/'.$path;
+            my $dest = $fullpath.$fname;
+            my $url = $url_root.$dirpath.'/'.$path.$fname;
+            my @parts=split(/\//,$fullpath);
+            my $count;
+            my $filepath = $dir_root;
+            for ($count=4;$count<=$#parts;$count++) {
+                $filepath .= "/$parts[$count]";
+                if ((-e $filepath)!=1) {
+                    mkdir($filepath,0770);
+                }
+            }
+            my $fh;
+            if (!open($fh,'>'.$dest)) {
+                &Apache::lonnet::logthis('Failed to create '.$dest);
+                $output .= '<span class="LC_error">'.
+                           &mt('An error occurred while trying to upload [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
+                           '</span><br />';
+            } else {
+                if (!print $fh $env{'form.embedded_item_'.$i}) {
+                    &Apache::lonnet::logthis('Failed to write to '.$dest);
+                    $output .= '<span class="LC_error">'.
+                              &mt('An error occurred while writing the file [_1] for embedded element [_2].',$orig_uploaded_filename,$env{'form.embedded_orig_'.$i}).
+                              '</span><br />';
+                } else {
+                    if ($context eq 'testbank') {
+                        $output .= &mt('Embedded file uploaded successfully:').
+                                   '&nbsp;<a href="'.$url.'">'.
+                                   $orig_uploaded_filename.'</a><br />';
+                    } else {
+                        $output .= '<font size="+2">'.
+                                   &mt('View embedded file: [_1]','<a href="'.$url.'">'.
+                                   $orig_uploaded_filename.'</a>').'</font><br />';
+                    }
+                }
+                close($fh);
+            }
+        }
+    }
+    return $output;
+}
+
+sub check_for_existing {
+    my ($path,$fname,$element) = @_;
+    my ($state,$msg);
+    if (-d $path.'/'.$fname) {
+        $state = 'exists';
+        $msg = &mt('Unable to upload [_1]. A directory by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
+    } elsif (-e $path.'/'.$fname) {
+        $state = 'exists';
+        $msg = &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$path);
+    }
+    if ($state eq 'exists') {
+        $msg = '<span class="LC_error">'.$msg.'</span><br />';
+    }
+    return ($state,$msg);
+}
+
+sub check_for_upload {
+    my ($path,$fname,$group,$element,$portfolio_root,$port_path,
+        $disk_quota,$current_disk_usage,$uname,$udom) = @_;
+    my $filesize = (length($env{'form.'.$element})) / 1000; #express in k (1024?)
+    my $getpropath = 1;
+    my @dir_list = &Apache::lonnet::dirlist($portfolio_root.$path,$udom,$uname,
+                                            $getpropath);
+    my $found_file = 0;
+    my $locked_file = 0;
+    foreach my $line (@dir_list) {
+        my ($file_name)=split(/\&/,$line,2);
+        if ($file_name eq $fname){
+            $file_name = $path.$file_name;
+            if ($group ne '') {
+                $file_name = $group.$file_name;
+            }
+            $found_file = 1;
+            if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
+                $locked_file = 1;
+            }
+        }
+    }
+    my $getpropath = 1;
+    if (($current_disk_usage + $filesize) > $disk_quota){
+        my $msg = '<span class="LC_error">'.
+                &mt('Unable to upload [_1]. (size = [_2] kilobytes). Disk quota will be exceeded.','<span class="LC_filename">'.$fname.'</span>',$filesize).'</span>'.
+                  '<br />'.&mt('Disk quota is [_1] kilobytes. Your current disk usage is [_2] kilobytes.',$disk_quota,$current_disk_usage);
+        return ('will_exceed_quota',$msg);
+    } elsif ($found_file) {
+        if ($locked_file) {
+            my $msg = '<span class="LC_error">';
+            $msg .= &mt('Unable to upload [_1]. A locked file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>','<span class="LC_filename">'.$port_path.$env{'form.currentpath'}.'</span>');
+            $msg .= '</span><br />';
+            $msg .= &mt('You will be able to rename or delete existing [_1] after a grade has been assigned.','<span class="LC_filename">'.$fname.'</span>');
+            return ('file_locked',$msg);
+        } else {
+            my $msg = '<span class="LC_error">';
+            $msg .= &mt('Unable to upload [_1]. A file by that name was found in [_2].','<span class="LC_filename">'.$fname.'</span>',$port_path.$env{'form.currentpath'});
+            $msg .= '</span>';
+            $msg .= '<br />';
+            $msg .= &mt('To upload, rename or delete existing [_1] in [_2].','<span class="LC_filename">'.$fname.'</span>', $port_path.$env{'form.currentpath'});
+            return ('file_exists',$msg);
+        }
+    }
+}
+
 
 =pod
 

--raeburn1213659252--