--- loncom/publisher/testbankimport.pm 2009/03/26 16:50:12 1.20
+++ loncom/publisher/testbankimport.pm 2012/10/29 17:38:55 1.35
@@ -1,5 +1,5 @@
# Handler for parsing text upload problem descriptions into .problems
-# $Id: testbankimport.pm,v 1.20 2009/03/26 16:50:12 bisitz Exp $
+# $Id: testbankimport.pm,v 1.35 2012/10/29 17:38:55 raeburn Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -28,7 +28,6 @@ package Apache::testbankimport;
use strict;
use Apache::Constants qw(:common :http :methods);
-use Apache::loncacc;
use Apache::loncommon();
use Apache::lonnet;
use HTML::Entities();
@@ -81,14 +80,15 @@ function createWin() {
newWindow = window.open("","CreateDir","HEIGHT=400,WIDTH=750,scrollbars=yes")
newWindow.document.open()
newWindow.document.write('$start_page')
- newWindow.document.write(" \\n")
+ newWindow.document.write(" \\n")
newWindow.document.write("
- '.&mt('Select').'
+ '.&mt('Select').'
1
1.
(1)
@@ -533,7 +533,7 @@ sub display_one {
$r->print(''.&mt('Identification of blocks of questions').' '."\n".
'');
return;
}
@@ -573,7 +573,7 @@ sub display_one {
# ---------------------------------------------------------------- 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");
@@ -608,7 +608,7 @@ sub display_two {
&mt('You also indicated that the [quant,_1,question] can be divided into [quant,_2,block] of questions of a particular question type.',$qcount,$blocks).''.
&mt('Provide additional information below, about the types of questions you have uploaded, and, if applicable, the format of answers and "foils" for specific types of questions.').'
'.
&show_uploaded_data($textref,$header).
- &topic_bar(4,$steptitle).''.
+ &Apache::lonhtmlcommon::topic_bar(4,$steptitle).'
'.
&mt('For each of the [_1] question blocks, specify the question numbers of the first and last questions in the block (e.g., 1 and 10), and the question type of the questions in the block.',''.$blocks.' ').' '.
&mt('If required, provide additional information about foil formats and answer formats for the question types you select.').'
'.
&Apache::loncommon::start_data_table().
@@ -625,10 +625,10 @@ sub display_two {
$r->print(&Apache::loncommon::start_data_table_row().
'
'.$iter.' '."\n".
' '."\n".
- ' '."\n".
+ ' '."\n".
'
- '.&mt('Select').' '."\n");
+ '.&mt('Select').' '."\n");
foreach my $qtype (@types) {
$r->print(''.$typenames{$qtype}.' '."\n");
}
@@ -657,14 +657,14 @@ sub display_two {
&mt('For ranking questions you must use the Answer format column to choose the separator used between the (ranked) answers.').'
'.
- &page_footer($env{'form.newdir'},$uname,$fn,$page).'
+ &page_footer($env{'form.newdir'},$fn,$page).'
');
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'};
@@ -714,14 +714,14 @@ sub display_three {
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);
}
}
}
$r->print(''.&mt('Review and selection of problems to convert').' '."\n".
'');
}
# ---------------------------------------------------------------- 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 = '';
@@ -1032,7 +1032,7 @@ sub final_display {
for (my $i=0; $i<$numitems; $i++) {
$r->print(' '."\n");
}
- $r->print(&topic_bar(6,&mt('Result of conversion of testbank questions to LON-CAPA problems')));
+ $r->print(&Apache::lonhtmlcommon::topic_bar(6,&mt('Result of conversion of testbank questions to LON-CAPA problems')));
my $destdir = $dirpath;
if ($destdir ne '' && $subdir ne '') {
$subdir .= '/';
@@ -1041,7 +1041,7 @@ sub final_display {
if (@createprobs == 0) {
$state = 'unchecked';
$r->print(''.&mt('No questions were selected for conversion.').'
'.
- &page_footer($env{'form.newdir'},$uname,$fn,$page,$webpath,$subdir,$state).'');
+ &page_footer($env{'form.newdir'},$fn,$page,$webpath,$subdir,$state).'');
} elsif (($destdir ne '') && (-e $destdir)) {
my (@qn_file,@result,@numid);
my $qcount = 0;
@@ -1129,11 +1129,11 @@ sub final_display {
$r->print(''.&mt('The following files already existed, and were not overwritten so these problems generated from the testbank have not been saved:').' '.$existing.'
');
$state = 'existing';
}
- $r->print(&page_footer($env{'form.newdir'},$uname,$fn,$page,$webpath,$subdir,$state).'');
+ $r->print(&page_footer($env{'form.newdir'},$fn,$page,$webpath,$subdir,$state).'');
} else {
$state = 'nodir';
$r->print(''.&mt('No destination directory was available so import of questions could not proceed.').'
'.
- &page_footer($env{'form.newdir'},$uname,$fn,$page,$webpath,$subdir,$state).'');
+ &page_footer($env{'form.newdir'},$fn,$page,$webpath,$subdir,$state).'');
}
return;
}
@@ -1158,7 +1158,7 @@ sub show_uploaded_data {
}
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();';
@@ -1183,7 +1183,6 @@ sub page_footer {
}
my $output = '
-
@@ -1200,7 +1199,7 @@ sub page_footer {
$output .= '
-
+
';
}
$output .= '
@@ -1329,7 +1328,11 @@ sub create_mcq {
my $numfoils = scalar(@{$qstnref}) - 1;
my $datestamp = localtime;
my $numansrs = scalar(@{$answerref});
- my $output = '
+ my $output = '';
+ if ($qtype eq 'MC') {
+ $output .= "\n".' ';
+ }
+ $output .= '
';
if ($res eq 'application/rtf' || $res eq 'text/html') {
if ($header ne '') {
@@ -1599,18 +1602,18 @@ sub probfile_name {
}
sub file_error {
- my ($r,$uname,$fn,$current_page,$webpath,$res) = @_;
+ my ($r,$fn,$current_page,$webpath,$res) = @_;
$r->print('
');
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);
@@ -1625,7 +1628,7 @@ sub parse_datafile {
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') {
@@ -1763,84 +1766,96 @@ sub parse_htmlcontent {
}
sub build_image_url {
- my ($urlpath,$item) = @_;
- $item =~ s/( ]+src=["']?\s*)(\.?\.?\/?)/$1$urlpath/gsi;
- return $item;
+ my ($webpath,$item) = @_;
+ $item =~ s/( ]+src=["']?\s*)(\.?\.?\/?)/$1$webpath/gsi;
+ return $item;
}
sub print_header {
- my ($uname,$udom,$javascript,$loadentries,$title) = @_;
+ my ($uname,$udom,$javascript,$loadentries,$title,$current_page,$pagesref,
+ $namesref) = @_;
+ my $brcrum = [{'href' => &Apache::loncommon::authorspace("/priv/$udom/$uname/"),
+ 'text' => 'Construction Space'}];
+ if ($env{'form.phase'} eq 'three') {
+ if (ref($pagesref) eq 'ARRAY') {
+ for (my $i=0; $i<$current_page; $i++) {
+ my $goback = 1 + $i - $current_page;
+ if (ref($namesref) eq 'HASH') {
+ if ($namesref->{$pagesref->[$i]} ne '') {
+ if (ref($brcrum) eq 'ARRAY') {
+ my $text = $namesref->{$pagesref->[$i]};
+ my $href;
+ if ($goback == -1) {
+ $href = 'javascript:backPage();';
+ } else {
+ $href = 'javascript:history.go('.$goback.')';
+ }
+ push(@{$brcrum}, {'href' => $href,
+ 'text' => $text});
+ }
+ }
+ }
+ }
+ }
+ }
my $output = &Apache::loncommon::start_page($title,$javascript,
- {'add_entries' => $loadentries});
+ {'bread_crumbs' => $brcrum,
+ 'add_entries' => $loadentries});
if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
- $output .= ''
- .&mt('Co-Author [_1]:[_2]',$uname,$udom)
+ $output .= '
'
+ .&mt('Co-Author [_1]',$uname.':'.$udom)
.'
';
}
return $output;
}
-sub topic_bar {
- my ($imgnum,$title) = @_;
- my $output = '
-
-
'.$title.'
-
-';
- return $output;
-}
-
# ---------------------------------------------------------------- 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'},
- $r->dir_config('lonDefDomain'));
- unless (($uname) && ($udom)) {
- $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::lonnet::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/'.$uname.$pathname;
- my $urlpath = '/~'.$uname.$pathname;
- my $dirpath = '/home/'.$uname.'/public_html'.$pathname;
- my ($res,$subdir,$badfile,$textref,$header,$css,$js,%loadentries);
+ my ($filename,$webpath) = &File::Basename::fileparse($fn);
+ my $dirpath = $r->dir_config('lonDocRoot').$webpath;
+ my ($res,$subdir,$badfile,$textref,$header,$css,$js,%loadentries,@pages,%names);
if ($env{'form.phase'} eq 'three') {
$current_page = &display_control();
- my @pages = ('Welcome','Blocks','Format','Target','Confirmation');
+ @pages = ('Welcome','Blocks','Format','Target','Confirmation');
+ %names = (
+ Welcome => 'Testbank Format',
+ Blocks => 'Classification',
+ Format => 'Selection',
+ Target => 'Result'
+ );
$page_name = $pages[$current_page];
if ($env{'form.timestamp'} eq '') {
$env{'form.timestamp'} = time;
@@ -1851,8 +1866,8 @@ sub handler {
}
}
($res,$badfile,$textref,$header,$css,$js) =
- &parse_datafile($r,$uname,$filename,$pathname,$dirpath,$urlpath,
- $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') {
@@ -1883,23 +1898,41 @@ sub handler {
}
}
- $r->print(&print_header($uname,$udom,$javascript,\%loadentries,$title));
+ $r->print(&print_header($uname,$udom,$javascript,\%loadentries,$title,
+ $current_page,\@pages,\%names));
- if ($env{'form.phase'} eq 'three') {
- if ($env{'form.action'} eq 'upload_embedded') {
- $r->print(&Apache::lonupload::phasethree($r,$fn,$uname,$udom,'testbank'));
+ if (($env{'form.phase'} eq 'four') || ($env{'form.phase'} eq 'three')) {
+ if ($env{'form.phase'} eq 'four') {
+ $r->print(&Apache::lonupload::phasefour($r,$fn,$uname,$udom,'testbank'));
+ my $current_page = 0;
+ my $js;
+ &jscript_zero($webpath,\$js);
+ $js = '';
+ $r->print($js);
+ &display_zero($r,$fn,$current_page,$webpath);
+ } elsif ($env{'form.phase'} eq 'three') {
+ if ($env{'form.action'} eq 'upload_embedded') {
+ my ($result,$flag) =
+ &Apache::lonupload::phasethree($r,$fn,$uname,$udom,'testbank');
+ $r->print($result);
+ if ($flag eq 'modify_orightml') {
+ undef($page_name);
+ $r->print('');
+ }
+ }
}
if ($badfile) {
- &file_error($r,$uname,$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,$urlpath,$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';
+ &file_error($r,$fn,$current_page,$webpath,$res);
+ } else {
+ &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;
@@ -1907,13 +1940,13 @@ sub handler {
&jscript_zero($webpath,\$js);
$js = '';
$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.'');
+ &page_footer('',$fn).'');
}
} else {
- &Apache::lonupload::phaseone($r,$fn,$uname,$udom,'testbank');
+ &Apache::lonupload::phaseone($r,$fn,'testbank');
}
$r->print(&Apache::loncommon::end_page());
return OK;