--- loncom/publisher/testbankimport.pm 2008/09/22 01:53:33 1.17 +++ loncom/publisher/testbankimport.pm 2011/10/31 01:25:32 1.32 @@ -1,5 +1,5 @@ # Handler for parsing text upload problem descriptions into .problems -# $Id: testbankimport.pm,v 1.17 2008/09/22 01:53:33 raeburn Exp $ +# $Id: testbankimport.pm,v 1.32 2011/10/31 01:25:32 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -81,14 +81,15 @@ function createWin() { newWindow = window.open("","CreateDir","HEIGHT=400,WIDTH=750,scrollbars=yes") newWindow.document.open() newWindow.document.write('$start_page') - newWindow.document.write("[Author Header]\\n") + newWindow.document.write("[Author Header]\\n") newWindow.document.write("

$lt{'loca'}: $webpath

$lt{'newd'}

\\n") newWindow.document.write("
\\n") newWindow.document.write("$lt{'ente'}.

") - newWindow.document.write("") - newWindow.document.write("") - newWindow.document.write("") - newWindow.document.write("$webpath") + newWindow.document.write("") + newWindow.document.write("") + newWindow.document.write("") + newWindow.document.write("") + newWindow.document.write("$webpath") newWindow.document.write("
") newWindow.document.write('$end_page') newWindow.document.close() @@ -508,9 +509,9 @@ sub display_zero {
  • '.&mt('Review the identified questions, choose which to convert, and (optionally) override the default filename to be used for each problem file.').'
  • '.&mt('Complete the import of questions.').'
  • '. - &topic_bar(1,&mt('Optional: create a sub-directory in which the testbank questions will be saved')). + &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:'). - ' '. + ' '. &page_footer($env{'form.newdir'},$uname,$fn,$page,$webpath).'
    '); } @@ -522,7 +523,7 @@ sub display_one { my %topics; $topics{2} = &mt('Select the format of the question number - e.g., 1, 1., 1), (1 or (1) - ').'   '."\n". - '  '."\n". + '  '."\n". ' '."\n". + ''."\n". ''.$qtype[$i].''."\n". ''. ''.$showqn.'

    '."\n"); @@ -829,7 +830,7 @@ sub display_three { } $r->print(&Apache::loncommon::start_data_table_row(). ''.$qnum.''."\n". - ''."\n". + ''."\n". ''.$qtype[$i].''."\n". ''."\n". ''.$items->[$j].''."\n". @@ -1032,7 +1033,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 .= '/'; @@ -1187,7 +1188,8 @@ sub page_footer { - '; + + '; if ($page ne '') { $output .= ' @@ -1199,7 +1201,7 @@ sub page_footer { $output .= ' '; } $output .= ' @@ -1327,9 +1329,12 @@ sub create_mcq { my $qstn = ${$qstnref}[0]; my $numfoils = scalar(@{$qstnref}) - 1; my $datestamp = localtime; - my $timestamp = time; 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 '') { @@ -1610,7 +1615,7 @@ sub file_error { } sub parse_datafile { - my ($r,$uname,$filename,$pathname,$dirpath,$urlpath,$page_name,$subdir) = @_; + my ($r,$uname,$filename,$pathname,$dirpath,$urlpath,$page_name,$subdir,$timestamp) = @_; my ($badfile,$res,%allfiles,%codebase); my $mm = new File::MMagic; my ($text,$header,$css,$js); @@ -1623,16 +1628,18 @@ sub parse_datafile { close(TESTBANK); } elsif ($res eq 'application/rtf') { my $html = ''; -# my $image_uri = '..'.$pathname; - my $image_uri = $pathname; + my $image_uri = $timestamp; if ($page_name eq 'Target') { - $image_uri = $urlpath; + $image_uri = $urlpath.'/'.$timestamp; } - $image_uri =~ s/\/$//; my $image_dir; if ($page_name eq 'Blocks') { $image_dir = $dirpath; $image_dir =~ s/\/$//; + $image_dir .= '/'.$timestamp; + if (!-e $image_dir) { + mkdir($image_dir,0755); + } } else { $image_dir = $r->dir_config('lonDaemons').'/tmp/'. $env{'user.name'}.'_'.$env{'user.domain'}. @@ -1651,10 +1658,10 @@ sub parse_datafile { $parser->parse(); utf8::decode($html); ($text,$header,$css,$js) = - &parse_htmlcontent($res,$subdir,$html); + &parse_htmlcontent($res,$subdir,$html,undef,$page_name); } elsif ($res eq 'text/html') { ($text,$header,$css,$js) = - &parse_htmlcontent($res,$subdir,undef,$dirpath.$filename); + &parse_htmlcontent($res,$subdir,undef,$dirpath.$filename,$page_name); } else { $badfile = 1; } @@ -1664,7 +1671,7 @@ sub parse_datafile { } sub parse_htmlcontent { - my ($res,$subdir,$html,$fullpath) = @_; + my ($res,$subdir,$html,$fullpath,$page_name) = @_; my ($p,$fh); if ($res eq 'application/rtf') { $p = HTML::TokeParser->new( \$html ); @@ -1702,7 +1709,10 @@ sub parse_htmlcontent { my $contents = $token->[4]; if ($subdir ne '') { if (($token->[1] eq 'img') && ($token->[2]->{'src'} ne '')) { - $contents =~ s/(src=\s*["']?)/$1..\//i; + if (($res eq 'text/html') || + ($res eq 'application/rtf') && ($page_name ne 'Target')) { + $contents =~ s/(src=\s*["']?)/$1..\//i; + } } } if (($line eq '') && ($current_tag eq 'font')) { @@ -1764,27 +1774,43 @@ sub build_image_url { } 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(), + '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').': '.$uname. - &mt(' at ').$udom.'

    '; + $output .= '

    ' + .&mt('Co-Author [_1]',$uname.':'.$udom) + .'

    '; } return $output; } -sub topic_bar { - my ($imgnum,$title) = @_; - my $output = ' -
    - '.&mt('Step [_1]',$imgnum).
-              '  '.$title.' -
    -'; - return $output; -} - # ---------------------------------------------------------------- Main Handler sub handler { my $r=shift; @@ -1801,9 +1827,8 @@ sub handler { $env{'form.filename'}; } ($uname,$udom)= - &Apache::loncacc::constructaccess($env{'form.filename'}, - $r->dir_config('lonDefDomain')); - unless (($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; @@ -1812,7 +1837,7 @@ sub handler { my ($fn,$filename); if ($env{'form.filename'}) { $fn=$env{'form.filename'}; - $fn=~s/^http\:\/\/[^\/]+\///; + $fn=~s/^https?\:\/\/[^\/]+\///; $fn=~s/^\///; $fn=~s{(~|priv/)($LONCAPA::username_re)}{}; $fn=~s/\/+/\//g; @@ -1827,23 +1852,31 @@ sub handler { $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 $webpath = '/priv/'.$udom.'/'.$uname.$pathname; + 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; + } if ($env{'form.newdir'} ne '') { if ($env{'form.newdir'} =~ /^\Q$dirpath\E(.+)$/) { $subdir = $1; } } ($res,$badfile,$textref,$header,$css,$js) = - &parse_datafile($r,$uname,$filename,$pathname,$dirpath,$urlpath, - $page_name,$subdir); + &parse_datafile($r,$uname,$filename,$pathname,$dirpath,$webpath, + $page_name,$subdir,$env{'form.timestamp'}); if ($page_name eq 'Welcome') { &jscript_zero($webpath,\$javascript); } elsif ($page_name eq 'Blocks') { @@ -1874,19 +1907,37 @@ 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,$uname,$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('
    '. + &page_footer('',$uname,$fn).''); + } + } } if ($badfile) { &file_error($r,$uname,$fn,$current_page,$webpath,$res); - } else { + } 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'; + &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'; } } elsif ($env{'form.phase'} eq 'two') {
      - +