[LON-CAPA-cvs] cvs: loncom /publisher testbankimport.pm

raeburn raeburn at source.lon-capa.org
Wed Nov 2 19:43:37 EDT 2011


raeburn		Wed Nov  2 23:43:37 2011 EDT

  Modified files:              
    /loncom/publisher	testbankimport.pm 
  Log:
  - Clean-up for bug 1320.
    - Restore support for import of testbanks to Construction Space.
    - Eliminate unused arg - $uname - in &display_zero(), &display_one()
        &display_two(), &display_three(), &final_display(), &parse_datafile,
        &file_error, &page_footer().
  - Coding style
    - URL of directory containing testbank file now consistently identified 
      as scalar: $webpath.
  
  
-------------- next part --------------
Index: loncom/publisher/testbankimport.pm
diff -u loncom/publisher/testbankimport.pm:1.32 loncom/publisher/testbankimport.pm:1.33
--- loncom/publisher/testbankimport.pm:1.32	Mon Oct 31 01:25:32 2011
+++ loncom/publisher/testbankimport.pm	Wed Nov  2 23:43:37 2011
@@ -1,5 +1,5 @@
 # Handler for parsing text upload problem descriptions into .problems
-# $Id: testbankimport.pm,v 1.32 2011/10/31 01:25:32 raeburn Exp $
+# $Id: testbankimport.pm,v 1.33 2011/11/02 23:43:37 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -485,7 +485,7 @@
 
 # ---------------------------------------------------------------- Display Zero
 sub display_zero {
-    my ($r,$uname,$fn,$page,$webpath) = @_;
+    my ($r,$fn,$page,$webpath) = @_;
     my $go_default = 'NextPage'; 
     if ($fn eq '') {
         $r->print('<b>'.&mt('Incomplete file upload').'</b> '.&mt('Return to the [_1]construction space menu[_2] to upload a file','<a href="'.$webpath.'">','</a>'));
@@ -512,14 +512,14 @@
         &Apache::lonhtmlcommon::topic_bar(1,&mt('Optional: create a sub-directory in which the testbank questions will be saved')).
         &mt('By default, LON-CAPA problems generated from the testbank file will be stored in the current directory.').' '.&mt('To store them in a new sub-directory:'). 
        ' <input type="button" name="createdir" value="'.&mt('Create sub-directory').'" onClick="javascript:createWin()" />'.
-       &page_footer($env{'form.newdir'},$uname,$fn,$page,$webpath).'
+       &page_footer($env{'form.newdir'},$fn,$page,$webpath).'
        </form>');
 }
 
 # ---------------------------------------------------------------- Display One
 
 sub display_one {
-    my ($r,$uname,$fn,$page,$textref,$header) = @_;
+    my ($r,$fn,$page,$textref,$header) = @_;
     my %topics;
     $topics{2} = &mt('Select the format of the question number - e.g., 1,  1., 1), (1 or (1) - ').' 
                <select name="qnumformat">
@@ -566,7 +566,7 @@
              &mt('4 multiple choice questions').'<br />'.
              &mt('3 essay questions').'</blockquote></p><p>'.
              &mt('You will indicate the question type and the question number range for each of the blocks on the next page.').'</p><br />'.
-             &page_footer($env{'form.newdir'},$uname,$fn,$page).'
+             &page_footer($env{'form.newdir'},$fn,$page).'
  </form>');
     return;
 }
@@ -574,7 +574,7 @@
 # ---------------------------------------------------------------- Display Two
 
 sub display_two {
-    my ($r,$uname,$fn,$page,$textref,$header,$qcount) = @_;
+    my ($r,$fn,$page,$textref,$header,$qcount) = @_;
     my $blocks = $env{'form.blocks'};
     my $qnumformat = $env{'form.qnumformat'};
     my @types = ("MC","MA","TF","Ess","FIB","Ord");
@@ -658,14 +658,14 @@
             &mt('For <i>ranking</i> questions you must use the <b>Answer format</b> column to choose the separator used between the (ranked) answers.').'</li></ul>
 <input type="hidden" name="blocks" value="'.$blocks.'" />
 <input type="hidden" name="qnumformat" value="'.$qnumformat.'" />'.
-           &page_footer($env{'form.newdir'},$uname,$fn,$page).'
+           &page_footer($env{'form.newdir'},$fn,$page).'
 </form>');
     return;
 }
 
 # ---------------------------------------------------------------- Display Three
 sub display_three {
-    my ($r,$uname,$fn,$page,$textref,$res,$header,$urlpath,$qcount) = @_;
+    my ($r,$fn,$page,$textref,$res,$header,$webpath,$qcount) = @_;
     my $qnumformat = $env{'form.qnumformat'};
     my $filename = $env{'form.filename'};
     my $source = $env{'form.go'};
@@ -715,7 +715,7 @@
         if ($header ne '') {
             $showheader = &HTML::Entities::decode($header);
             if ($res eq 'text/html') {
-                $showheader = &build_image_url($urlpath,$showheader);
+                $showheader = &build_image_url($webpath,$showheader);
             }
         }
     }
@@ -764,7 +764,7 @@
                                 if (($res eq 'application/rtf') || ($res eq 'text/html')) {
                                     $showqn = &HTML::Entities::decode($showqn);
                                     if ($res eq 'text/html') {
-                                        $showqn = &build_image_url($urlpath,$showqn);
+                                        $showqn = &build_image_url($webpath,$showqn);
                                     }
                                 }
                                 $r->print(&Apache::loncommon::start_data_table_row().
@@ -808,7 +808,7 @@
                                 if ($res eq 'application/rtf' || $res eq 'text/html') {
                                     $showfoil = &HTML::Entities::decode($showfoil);
                                     if ($res eq 'text/html') {
-                                        $showfoil = &build_image_url($urlpath,$showfoil);
+                                        $showfoil = &build_image_url($webpath,$showfoil);
                                     }
                                 } 
                                 $r->print("$foiltag $showfoil<br />\n");
@@ -859,13 +859,13 @@
           <input type="hidden" name="ansr_'.$i.'" value="'.$ansrtypes[$i].'" />');
         }
     }
-    $r->print('</p>'.&page_footer($env{'form.newdir'},$uname,$fn,$page).'
+    $r->print('</p>'.&page_footer($env{'form.newdir'},$fn,$page).'
               </form>');
 }
 
 # ---------------------------------------------------------------- Final Display
 sub final_display {
-    my ($r,$uname,$fn,$page,$textref,$res,$header,$css,$js,$webpath,$dirpath,$subdir) = @_;
+    my ($r,$fn,$page,$textref,$res,$header,$css,$js,$webpath,$dirpath,$subdir) = @_;
     my $qnumformat = $env{'form.qnumformat'};
     my $blocks = $env{'form.blocks'};
     my $question_id = '';
@@ -1042,7 +1042,7 @@
     if (@createprobs == 0) {
         $state = 'unchecked';
         $r->print('<p>'.&mt('No questions were selected for conversion.').'</p>'.
-                  &page_footer($env{'form.newdir'},$uname,$fn,$page,$webpath,$subdir,$state).'</form>');
+                  &page_footer($env{'form.newdir'},$fn,$page,$webpath,$subdir,$state).'</form>');
     } elsif (($destdir ne '') && (-e $destdir)) {
         my (@qn_file, at result, at numid);
         my $qcount = 0;
@@ -1130,11 +1130,11 @@
             $r->print('<p>'.&mt('The following files already existed, and were not overwritten so these problems generated from the testbank have not been saved:').'<br />'.$existing.'</p>');
             $state = 'existing';
         }
-        $r->print(&page_footer($env{'form.newdir'},$uname,$fn,$page,$webpath,$subdir,$state).'</form>');
+        $r->print(&page_footer($env{'form.newdir'},$fn,$page,$webpath,$subdir,$state).'</form>');
     } else {
         $state = 'nodir';
         $r->print('<p>'.&mt('No destination directory was available so import of questions could not proceed.').'</p>'.
-                  &page_footer($env{'form.newdir'},$uname,$fn,$page,$webpath,$subdir,$state).'</form>');
+                  &page_footer($env{'form.newdir'},$fn,$page,$webpath,$subdir,$state).'</form>');
     }
     return;
 }
@@ -1159,7 +1159,7 @@
 }
 
 sub page_footer {
-    my ($newdir,$uname,$fn,$page,$webpath,$subdir,$state) = @_;
+    my ($newdir,$fn,$page,$webpath,$subdir,$state) = @_;
     my $prevval = &mt('Previous Page');
     my $nextval = &mt('Next Page');
     my $prevclick = 'javascript:backPage();';
@@ -1184,7 +1184,6 @@
     }
     my $output = '
        <input type="hidden" name="newdir" value="'.&HTML::Entities::encode($newdir,'<>&"').'" />
-       <input type="hidden" name="uploaduname" value="'.$uname.'" />
        <input type="hidden" name="filename" value="'.$fn.'" />
        <input type="hidden" name="page" value="'.$page.'" />
        <input type="hidden" name="phase" value="three" />
@@ -1604,18 +1603,18 @@
 }
 
 sub file_error {
-    my ($r,$uname,$fn,$current_page,$webpath,$res) = @_;
+    my ($r,$fn,$current_page,$webpath,$res) = @_;
     $r->print('<p><form name="display" method="post" action="/adm/testbank">'.&mt('The file you uploaded does not appear to be in the correct format.').
               '</p><p>'.&mt('Extraction of questions is only possible for the following file types:').
               '<ul><li>'.&mt('plain text').'</li><li>RTF</li><li>HTML</li></ul>'.
               &mt('The file type identified for the file you uploaded is [_1].','<b>'.$res.'</b>').'</p>');
-    $r->print(&page_footer($env{'form.newdir'},$uname,$fn,$current_page,$webpath,undef,'badfile').
+    $r->print(&page_footer($env{'form.newdir'},$fn,$current_page,$webpath,undef,'badfile').
              '</form>');
     return;
 }
 
 sub parse_datafile {
-    my ($r,$uname,$filename,$pathname,$dirpath,$urlpath,$page_name,$subdir,$timestamp) = @_;
+    my ($r,$filename,$dirpath,$webpath,$page_name,$subdir,$timestamp) = @_;
     my ($badfile,$res,%allfiles,%codebase);
     my $mm = new File::MMagic;
     my ($text,$header,$css,$js);
@@ -1630,7 +1629,7 @@
                 my $html = '';
                 my $image_uri = $timestamp;
                 if ($page_name eq 'Target') {
-                    $image_uri = $urlpath.'/'.$timestamp;
+                    $image_uri = "$webpath/$timestamp";
                 }
                 my $image_dir;
                 if ($page_name eq 'Blocks') {
@@ -1768,9 +1767,9 @@
 }
 
 sub build_image_url {
-    my ($urlpath,$item) = @_;
-    $item =~ s/(<img[^>]+src=["']?\s*)(\.?\.?\/?)/$1$urlpath/gsi;
-    return $item; 
+    my ($webpath,$item) = @_;
+    $item =~ s/(<img[^>]+src=["']?\s*)(\.?\.?\/?)/$1$webpath/gsi;
+    return $item;
 }
 
 sub print_header {
@@ -1814,45 +1813,38 @@
 # ---------------------------------------------------------------- Main Handler
 sub handler {
     my $r=shift;
-    my $uname;
-    my $udom;
-    my $javascript = '';
-    my $page_name = '';
-    my $current_page = '';
-    my $qcount = '';
-    my $title = 'Upload testbank questions to Construction Space';
 
-    if ($env{'form.uploaduname'}) {
-        $env{'form.filename'}='/priv/'.$env{'form.uploaduname'}.'/'.
-            $env{'form.filename'};
-    }
-    ($uname,$udom)=
-        &Apache::loncacc::constructaccess($env{'form.filename'});
-    unless (($uname ne '') && ($udom ne '')) {
-        $r->log_reason($uname.':'.$udom.' trying to convert testbank file '.
-                       $env{'form.filename'}.' - not authorized',$r->filename);
-        return HTTP_NOT_ACCEPTABLE;
+    my $fn=$env{'form.filename'};
+
+    if ($env{'form.filename1'}) {
+       $fn=$env{'form.filename1'}.$env{'form.filename2'};
     }
+    $fn=~s{\+}{}g;
 
-    my ($fn,$filename);
-    if ($env{'form.filename'}) {
-        $fn=$env{'form.filename'};
-        $fn=~s/^https?\:\/\/[^\/]+\///;
-        $fn=~s/^\///;
-        $fn=~s{(~|priv/)($LONCAPA::username_re)}{};
-        $fn=~s/\/+/\//g;
-    } else {
+    unless ($fn) {
         $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
                        ' unspecified filename for upload', $r->filename);
         return HTTP_NOT_FOUND;
     }
 
+    my ($uname,$udom) = &Apache::loncacc::constructaccess($fn);
+    if (($uname eq '') || ($udom eq '')) {
+        $r->log_reason($uname.':'.$udom.' trying to convert testbank file '.
+                       $fn.' - not authorized',$r->filename);
+        return HTTP_NOT_ACCEPTABLE;
+    }
+
+    my $javascript = '';
+    my $page_name = '';
+    my $current_page = '';
+    my $qcount = '';
+    my $title = 'Upload testbank questions to Construction Space';
+
 # ----------------------------------------------------------- Start page output
     &Apache::loncommon::content_type($r,'text/html');
     $r->send_http_header;
 
-    my ($filename,$pathname) = &File::Basename::fileparse($fn);
-    my $webpath = '/priv/'.$udom.'/'.$uname.$pathname;
+    my ($filename,$webpath) = &File::Basename::fileparse($fn);
     my $dirpath = $r->dir_config('lonDocRoot').$webpath;
     my ($res,$subdir,$badfile,$textref,$header,$css,$js,%loadentries, at pages,%names);
 
@@ -1875,8 +1867,8 @@
             }
         }
         ($res,$badfile,$textref,$header,$css,$js) = 
-            &parse_datafile($r,$uname,$filename,$pathname,$dirpath,$webpath,
-                            $page_name,$subdir,$env{'form.timestamp'});
+            &parse_datafile($r,$filename,$dirpath,$webpath,$page_name,
+                            $subdir,$env{'form.timestamp'});
         if ($page_name eq 'Welcome') {
              &jscript_zero($webpath,\$javascript);
         } elsif ($page_name eq 'Blocks') {
@@ -1918,7 +1910,7 @@
             &jscript_zero($webpath,\$js);
             $js = '<script type="text/javascript">'."\n$js\n".'</script>';
             $r->print($js);
-            &display_zero($r,$uname,$fn,$current_page,$webpath);
+            &display_zero($r,$fn,$current_page,$webpath);
         } elsif ($env{'form.phase'} eq 'three') {
             if ($env{'form.action'} eq 'upload_embedded') {
                 my ($result,$flag) = 
@@ -1927,21 +1919,21 @@
                 if ($flag eq 'modify_orightml') {
                     undef($page_name); 
                     $r->print('<form name="testbankForm" method="post" action="/adm/testbank">'.
-                              &page_footer('',$uname,$fn).'</form>');
+                              &page_footer('',$fn).'</form>');
                 }
             }
         }
         if ($badfile) {
-            &file_error($r,$uname,$fn,$current_page,$webpath,$res);
+            &file_error($r,$fn,$current_page,$webpath,$res);
         } else {
-            &display_zero ($r,$uname,$fn,$current_page,$webpath) if $page_name eq 'Welcome';
-            &display_one ($r,$uname,$fn,$current_page,$textref,$header) if $page_name eq 'Blocks';
-            &display_two ($r,$uname,$fn,$current_page,$textref,$header,$qcount) if $page_name eq 'Format';
-            &display_three ($r,$uname,$fn,$current_page,$textref,$res,$header,$webpath,$qcount) if $page_name eq 'Target';
-            &final_display ($r,$uname,$fn,$current_page,$textref,$res,$header,$css,$js,$webpath,$dirpath,$subdir) if $page_name eq 'Confirmation';
+            &display_zero ($r,$fn,$current_page,$webpath) if $page_name eq 'Welcome';
+            &display_one ($r,$fn,$current_page,$textref,$header) if $page_name eq 'Blocks';
+            &display_two ($r,$fn,$current_page,$textref,$header,$qcount) if $page_name eq 'Format';
+            &display_three ($r,$fn,$current_page,$textref,$res,$header,$webpath,$qcount) if $page_name eq 'Target';
+            &final_display ($r,$fn,$current_page,$textref,$res,$header,$css,$js,$webpath,$dirpath,$subdir) if $page_name eq 'Confirmation';
         }
     } elsif ($env{'form.phase'} eq 'two') {
-        my ($result,$flag) = &Apache::lonupload::phasetwo($r,$fn,$uname,$udom,'testbank');
+        my ($result,$flag) = &Apache::lonupload::phasetwo($r,$fn,'testbank');
         $r->print($result);
         if ($flag eq 'ok') {
             my $current_page = 0;
@@ -1949,13 +1941,13 @@
             &jscript_zero($webpath,\$js);
             $js = '<script type="text/javascript">'."\n$js\n".'</script>';
             $r->print($js);
-            &display_zero($r,$uname,$fn,$current_page,$webpath);
+            &display_zero($r,$fn,$current_page,$webpath);
         } elsif ($flag eq 'embedded') {
             $r->print($js.'<form name="testbankForm" method="post" action="/adm/testbank">'.
-                      &page_footer('',$uname,$fn).'</form>');
+                      &page_footer('',$fn).'</form>');
         }
     } else {
-        &Apache::lonupload::phaseone($r,$fn,$uname,$udom,'testbank');
+        &Apache::lonupload::phaseone($r,$fn,'testbank');
     }
     $r->print(&Apache::loncommon::end_page());
     return OK;


More information about the LON-CAPA-cvs mailing list