--- loncom/imspackages/imsimportdocs.pm	2004/08/05 23:21:49	1.7
+++ loncom/imspackages/imsimportdocs.pm	2009/08/17 03:52:09	1.23
@@ -1,3 +1,7 @@
+# The LearningOnline Network with CAPA
+#
+# $Id: imsimportdocs.pm,v 1.23 2009/08/17 03:52:09 raeburn Exp $
+#
 # Copyright Michigan State University Board of Trustees
 #
 # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
@@ -30,6 +34,10 @@ use Apache::loncommon;
 use Apache::lonlocal;
 use Apache::imsprocessor;
 use LONCAPA::Configuration;
+use LONCAPA::map();
+use lib '/home/httpd/lib/perl/';
+use LONCAPA;
+ 
 use strict;
 
 sub jscript_one {
@@ -143,59 +151,54 @@ sub handler {
     &Apache::imsprocessor::ims_config(\@areas,\%cmsmap,\%areaname);
 
 # get course data
-    my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
-    my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
-    my $coursehome=$ENV{'course.'.$ENV{'request.course.id'}.'.home'};
+    my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
+    my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 
 # get personal data
  
-    my $uname=$ENV{'user.name'};
-    my $udom=$ENV{'user.domain'};
-    my $plainname=&Apache::lonnet::escape(
+    my $uname=$env{'user.name'};
+    my $udom=$env{'user.domain'};
+    my $plainname=&escape(
                      &Apache::loncommon::plainname($uname,$udom));
 
 # does this user have privileges to post, etc?
-    my $allowed=&Apache::lonnet::allowed('mdc',$ENV{'request.course.id'});
+    my $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
     unless ($allowed) {
-        $r->print("<html><head><title>The LearningOnline Network with CAPA</title></head>");
-        $r->print(&Apache::loncommon::bodytag('Import IMS package'));  
-        $r->print('<h3>'.&mt('Modification of Course Contents Disallowed').'</h3>'.&mt('Your current role does not grant you the right to modify course content in this course.').'</body></html>');
+        $r->print(&Apache::loncommon::start_page('Import IMS package',undef,
+						 {'only_body' => 1,}));  
+        $r->print('<h3>'.&mt('Modification of Course Contents Disallowed').'</h3>'.&mt('Your current role does not grant you the right to modify course content in this course.').
+		  &Apache::loncommon::end_page());
         return OK;
     }
 
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
        ['phase']);
 
-    if ($ENV{'form.phase'} eq 'one') {    
+    if ($env{'form.phase'} eq 'one') {    
         &jscript_one(\$javascript);
-    } elsif ($ENV{'form.phase'} eq 'two') {
+    } elsif ($env{'form.phase'} eq 'two') {
         &jscript_two(\$javascript);
-    } elsif ($ENV{'form.phase'} eq 'three') {
+    } elsif ($env{'form.phase'} eq 'three') {
         &jscript_three(\$javascript);
     }
 
+    $javascript = 
+	 "<script type=\"text/javascript\">\n".
+	 "//<!--\n$javascript\n// --></script>\n";
+    my $start_page = &Apache::loncommon::start_page('Import IMS package',
+						    $javascript,
+						    {'only_body' => 1,});
 # print screen
-    $r->print(<<ENDHEAD);
-<html>
-<head>
-<title>The LearningOnline Network with CAPA</title>
-<script type="text/javascript">
-<!--
-$javascript
--->
-</script>
-</head>
-ENDHEAD
-# -------------------------------------------------------------------- Body tag
-    $r->print(&Apache::loncommon::bodytag('Import IMS package'));
-    if ($ENV{'form.phase'} eq 'one') {
+    $r->print($start_page);
+
+    if ($env{'form.phase'} eq 'one') {
         &display_one($r);       
-    } elsif ($ENV{'form.phase'} eq 'two') {
+    } elsif ($env{'form.phase'} eq 'two') {
         &display_two($r,$coursenum,\@areas,\%areaname,%cmsmap);
-    } elsif ($ENV{'form.phase'} eq 'three') {
-        &display_three($r,$coursenum,$coursedom,$coursehome,$uname,$udom,\@areas,%cmsmap);
+    } elsif ($env{'form.phase'} eq 'three') {
+        &display_three($r,$coursenum,$coursedom,$uname,$udom,\@areas,%cmsmap);
     }   
-    $r->print("</body><html>");
+    $r->print(&Apache::loncommon::end_page());
     return OK;
 } 
 
@@ -215,7 +218,7 @@ sub display_one {
         </td>
        </tr>
        <tr bgcolor='#CCCCFF'>
-        <td valign='middle'><img src='/res/adm/pages/bl_step1.gif'>&nbsp;
+        <td valign="middle"><img src="/res/adm/pages/bl_step1.gif" alt="1" />&nbsp;
         </td>
         <td width='100%' align='left'>&nbsp;&nbsp;
          <font face='arial,helvetica,sans-serif'><b>Specify the Course Management system used to create the package.</b>&nbsp;&nbsp;
@@ -231,10 +234,11 @@ sub display_one {
         <font face='Arial,Helvetica,sans-serif'>
 Please choose the CMS used to create your IMS content package.&nbsp;&nbsp;
         <select name="source">
-         <option value='-1' selected="true">Please select
-         <option value='bb5'>Blackboard 5
-         <option value='bb6'>Blackboard 6
-         <option value='angel'>ANGEL
+         <option value='-1' selected="true">Please select</option>
+         <option value='bb5'>Blackboard 5</option>
+         <option value='bb6'>Blackboard 6</option>
+         <option value='angel5'>ANGEL 5.5</option>
+         <option value='webctce4'>WebCT 4 Campus Edition</option>
         </select>
         </font>
        </td>
@@ -246,7 +250,7 @@ Please choose the CMS used to create you
        <td colspan='2'>&nbsp;</td>
       </tr>
       <tr bgcolor='#CCCCFF'>
-        <td valign='middle'><img src='/res/adm/pages/bl_step2.gif'>&nbsp;
+        <td valign="middle"><img src="/res/adm/pages/bl_step2.gif" alt="2" />&nbsp;
         </td>
         <td width='100%' align='left'>&nbsp;&nbsp;
          <font face='arial,helvetica,sans-serif'><b>Locate the IMS content package you wish to upload.</b>&nbsp;&nbsp;
@@ -258,7 +262,7 @@ Please choose the CMS used to create you
       </tr>
       <tr>
        <td colspan='2'>&nbsp;
-        <input type="hidden" name="folder" value="$ENV{'form.folder'}" />
+        <input type="hidden" name="folder" value="$env{'form.folder'}" />
         <input type="hidden" name="phase" value="two" />
         <input type="file" name="uploadname" size="40" />
        </td>
@@ -278,10 +282,10 @@ Please choose the CMS used to create you
         <table border='0' cellspacing='0' cellpadding='0' width="100%">
          <tr>
           <td align='left'>
-           <input type="button" name="exitpage" value="Exit now" onClick="javascript:self.close()">
+           <input type="button" name="exitpage" value="Exit now" onClick="javascript:self.close()" />
           </td>
           <td align='right'>
-           <input type="button" name="nextpage" value="Upload IMS package" onClick="javascript:nextPage()">
+           <input type="button" name="nextpage" value="Upload IMS package" onClick="javascript:nextPage()" />
           </td>
          </tr>
         </table>
@@ -299,7 +303,7 @@ ENDBLOCK
 sub display_two {
     my ($r,$crs,$areasref,$areaname,%cmsmap) = @_;
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['folder','source']);
-    my $cms = $ENV{'form.source'};
+    my $cms = $env{'form.source'};
     my $timenow = time;
     my $tempdir = &Apache::imsprocessor::create_tempdir('DOCS',$crs,$timenow);
     my $fname = &Apache::imsprocessor::uploadzip('DOCS',$tempdir);
@@ -309,6 +313,8 @@ sub display_two {
         $unzip_result = &Apache::imsprocessor::expand_zip($tempdir,$fname);
     }
     my %resources = ();
+    my %includedres = ();
+    my %includeditems = ();
     my %items = ();
     my %hrefs = ();
     my %resinfo = ();
@@ -331,16 +337,16 @@ sub display_two {
                 );
 
     if ($unzip_result eq 'ok') {
-        $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo);
+        $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo,'choose',\%includedres,\%includeditems);
         if ($manifest_result eq 'ok') {
             foreach my $res (sort keys %resources) {
-                if ($cms eq 'bb5' || $cms eq 'bb6') {
+                if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webctce4') {
                     foreach my $area (keys %{$cmsmap{$cms}}) {
                         if ($resources{$res}{type} eq $cmsmap{$cms}{$area}) {
                             $count{$area} ++;
                         }
                     }
-                } elsif ($cms eq 'angel') {
+                } elsif ($cms eq 'angel5') {
                     foreach my $area (keys %{$cmsmap{$cms}}) {
                         if ($area eq 'doc') {
                             if (grep/^$resources{$res}{type}$/,@{$cmsmap{$cms}{doc}}) {
@@ -363,7 +369,7 @@ sub display_two {
        </td>
       </tr>
       <tr bgcolor='#CCCCFF'>
-       <td valign='middle'><img src='/res/adm/pages/bl_step3.gif'>
+       <td valign="middle"><img src="/res/adm/pages/bl_step3.gif" alt="3" />
        </td>
        <td width='100%' align='left'>&nbsp;&nbsp;
         <font face='arial,helvetica,sans-serif'><b>Choose which content types you wish to import</b></font>
@@ -401,13 +407,13 @@ ENDBLOCK
                     if ($area eq 'board') {
                         $r->print("            <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;
                  <select name='db_handling'>
-                  <option value='-2'>&lt;-- Check Import first
+                  <option value='-2'>&lt;-- Check Import first</option>
                  </select></font>
                 </td>");
                     } elsif ($area eq 'users') {
                         $r->print("            <td align='left'><font face='arial,helvetica,sans-serif'>&nbsp;&nbsp;
                  <select name='user_handling'>
-                  <option value='-2'>&lt;-- Check Import first
+                  <option value='-2'>&lt;-- Check Import first</option>
                  </select>
                  </font>        
                 </td>");
@@ -433,7 +439,7 @@ ENDBLOCK
        <td colspan='2'>&nbsp;<br /><br /></td>
       </tr>
       <tr bgcolor='#CCCCFF'>
-       <td valign='middle'><img src='/res/adm/pages/bl_step4.gif'>
+       <td valign="middle"><img src="/res/adm/pages/bl_step4.gif" alt="4" />
        </td>
        <td width='100%' align='left'>&nbsp;&nbsp;
         <font face='arial,helvetica,sans-serif'><b>Choose display options for listing of contents of top level of package.</b></font>
@@ -447,10 +453,10 @@ ENDBLOCK
        <td>
         <table border='0'>
          <tr>
-          <td><font face='arial,helvetica,sans-serif'><input type="radio" name="toplevel" value="newfolder" />Display listing of contents in a new folder, with folder name:&nbsp;&nbsp;&nbsp;<input type="text" name="foldername" size="15" value="Type Name Here" /></font></td>
+          <td><font face='arial,helvetica,sans-serif'><label><input type="radio" name="toplevel" value="newfolder" />Display listing of contents in a new folder, with folder name:</label>&nbsp;&nbsp;&nbsp;<input type="text" name="foldername" size="15" value="Type Name Here" /></font></td>
          </tr>
          <tr>
-          <td><font face='arial,helvetica,sans-serif'><input type="radio" name="toplevel" value="oldfolder" />Append listing of contents of top level of package to contents list for the current folder.</font></td>
+          <td><font face='arial,helvetica,sans-serif'><label><input type="radio" name="toplevel" value="oldfolder" />Append listing of contents of top level of package to contents list for the current folder.</label></font></td>
          </tr>
         </table>
        </td>
@@ -464,7 +470,7 @@ ENDBLOCK
       </tr>
       <tr>
        <td colspan='2'>&nbsp;
-          <input type="hidden" name="folder" value="$ENV{'form.folder'}" />
+          <input type="hidden" name="folder" value="$env{'form.folder'}" />
           <input type="hidden" name="source" value="$cms" />
           <input type="hidden" name="tempdir" value="$tempdir" />
           <input type="hidden" name="phase" value="three" />
@@ -475,10 +481,10 @@ ENDBLOCK
         <table border='0' cellspacing='0' cellpadding='0' width="100%">
          <tr>
           <td align='left'>
-           <input type='button' name='exitpage' value='Exit now' onClick="javascript:self.close()">
+           <input type='button' name='exitpage' value='Exit now' onClick="javascript:self.close()" />
           </td>
           <td align='right'>
-           <input type="button" name="nextpage" value="Complete Import" onClick="javascript:nextPage($counter)">
+           <input type="button" name="nextpage" value="Complete Import" onClick="javascript:nextPage($counter)" />
           </td>
          </tr>
         </table>
@@ -499,15 +505,17 @@ ENDDOCUMENT
 
 
 sub display_three {
-    my ($r,$crs,$cdom,$chome,$uname,$udom,$areas,%cmsmap) = @_;
-    my $folder = $ENV{'form.folder'};
-    my $cms = $ENV{'form.source'};
-    my $tempdir = $ENV{'form.tempdir'};
+    my ($r,$crs,$cdom,$uname,$udom,$areas,%cmsmap) = @_;
+    my $folder = $env{'form.folder'};
+    my $cms = $env{'form.source'};
+    my $tempdir = $env{'form.tempdir'};
     my $longcrs = '';
     if ($crs =~ m/^(\d)(\d)(\d)/) {
         $longcrs = $1.'/'.$2.'/'.$3.'/'.$crs;
     }
-    my %imports = ();
+    my %importareas = ();
+    my %includedres = ();
+    my %includeditems = ();
     my @targets = ();
     my %resources = ();
     my %items = ();
@@ -536,32 +544,48 @@ sub display_three {
     my $toplevel = '';
     my $foldername = '';
     my %topitems = ();
-    if (defined($ENV{'form.toplevel'}) ) {
-        $toplevel = $ENV{'form.toplevel'};     
+    if (defined($env{'form.toplevel'}) ) {
+        $toplevel = $env{'form.toplevel'};     
     }
-    if (defined($ENV{'form.foldername'}) ) {
-        $foldername = $ENV{'form.foldername'}; 
+    if (defined($env{'form.foldername'}) ) {
+        $foldername = $env{'form.foldername'}; 
     }
- 
+
     foreach my $area (@{$areas}) {
-        if (defined($ENV{"form.$area"}) ) {          
-            if ($cms eq 'angel' && $area eq 'doc') {
+        if (defined($env{"form.$area"}) && ($env{'form.'.$area} ne '')) {
+            if ($cms eq 'angel5' && $area eq 'doc') {
                 foreach (@{$cmsmap{$cms}{$area}}) {
-                    $imports{$_} = 1;
+                    $importareas{$_} = 1;
                 }
             } else {
-                $imports{$cmsmap{$cms}{$area}} = 1;
+                $importareas{$cmsmap{$cms}{$area}} = 1;
             }
             if ($area eq 'board') {
-                $db_handling = $ENV{'form.db_handling'};
+                $db_handling = $env{'form.db_handling'};
             } elsif ($area eq 'users') {
-                $user_handling = $ENV{'form.user_handling'};
+                $user_handling = $env{'form.user_handling'};
             }
         }
     }
-   
-    my $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo);
+
+    my $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo,'prepare',\%includedres,\%includeditems);
     if ($manifest_result eq 'ok') {
+        foreach my $res (sort keys %resources) {
+            if ($importareas{$resources{$res}{type}}) {
+                $includedres{$res} = 1;
+            }
+        }
+        foreach my $itm (sort keys %items) {
+            &Apache::imsprocessor::get_imports(\%includeditems,\%items,\%resources,\%importareas,$itm);
+        }
+    }
+    foreach my $itm (sort keys %includeditems) {
+        &Apache::imsprocessor::get_parents(\%includeditems,\%items,$itm);
+    }
+
+    $manifest_result = &Apache::imsprocessor::process_manifest($cms,$tempdir,\%resources,\%items,\%hrefs,\%resinfo,'build',\%includedres,\%includeditems);
+    if ($manifest_result eq 'ok') {
+
         my @path = ($cdom,$crs,$timenow);
         my $fullpath = $Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles';
         foreach my $item (@path) {
@@ -576,12 +600,13 @@ sub display_three {
                 mkdir("$fullpath/$name",0770);
             }
         }
-        &Apache::imsprocessor::target_resources(\%resources,\%imports,\@targets);
+        &Apache::imsprocessor::target_resources(\%resources,\%importareas,\@targets);
 
         my @boards = ();
         my @announcements = ();
         my @quizzes = ();
         my @surveys = ();
+        my @pools = ();
         my @groups = ();
         my %messages = ();
         my @timestamp = ();
@@ -590,49 +615,49 @@ sub display_three {
         my @topnames = ();
         my @packages = ();
 
-        &Apache::imsprocessor::process_resinfo($cms,'DOCS',$tempdir,$destdir,\%items,\%resources,\@boards,\@announcements,\@quizzes,\@surveys,\@groups,\%messages,\@timestamp,\%boardnum,\%resinfo,$udom,$uname,$cdom,$crs,$db_handling,$user_handling,\%total,$seqstem,$seqstem,\@resrcfiles,\@packages,\%hrefs);
+        &Apache::imsprocessor::process_resinfo($cms,'DOCS',$tempdir,$destdir,\%items,\%resources,\@targets,\@boards,\@announcements,\@quizzes,\@surveys,\@pools,\@groups,\%messages,\@timestamp,\%boardnum,\%resinfo,$udom,$uname,$cdom,$crs,$db_handling,$user_handling,\%total,$seqstem,$seqstem,\@resrcfiles,\@packages,\%hrefs,\@pages,\@sequences);
 
-        my $copy_result = &Apache::imsprocessor::copy_resources('DOCS',$cms,\%hrefs,$tempdir,\@targets,\%urls,$crs,$cdom,$chome,$destdir,$timenow);
+        my $copy_result = &Apache::imsprocessor::copy_resources('DOCS',$cms,\%hrefs,$tempdir,\@targets,\%urls,$crs,$cdom,$destdir,$timenow,\%importareas);
+
+        &Apache::imsprocessor::build_structure($cms,'DOCS',$destdir,\%items,\%resinfo,\%resources,\@targets,\%hrefs,$udom,$uname,'',$timenow,$cdom,$crs,\@timestamp,\%total,\@boards,\@announcements,\@quizzes,\@surveys,\@pools,\%boardnum,\@pages,\@sequences,\@topurls,\@topnames,\@packages,\%includeditems);
 
-        &Apache::imsprocessor::build_structure($cms,'DOCS',$destdir,\%items,\%resinfo,\%resources,\%hrefs,$udom,$uname,'',$timenow,$cdom,$crs,\@timestamp,\%total,\@boards,\@announcements,\@quizzes,\@surveys,\%boardnum,\@pages,\@sequences,\@topurls,\@topnames,\@packages);
- 
         foreach my $item (@pages) {
             my $filename = $timenow.'/pages/'.$item;
-            my $fetchresult= &Apache::lonnet::process_coursefile('propagate',$crs,$cdom,$chome,$filename,'');
+            my $fetchresult= &Apache::lonnet::process_coursefile('propagate',$crs,$cdom,$filename,'');
         }
         foreach my $item (@sequences) {
             unless ($item eq 'Top.sequence' && $toplevel eq 'oldfolder') {
                 my $filename = $timenow.'/sequences/'.$item;
-                my $fetchresult= &Apache::lonnet::process_coursefile('propagate',$crs,$cdom,$chome,$filename,'');
+                my $fetchresult= &Apache::lonnet::process_coursefile('propagate',$crs,$cdom,$filename,'');
             }
         }
         foreach my $item (@resrcfiles) {
             my $filename = $timenow.'/resfiles/'.$item;
-            my $fetchresult= &Apache::lonnet::process_coursefile('propagate',$crs,$cdom,$chome,$filename,'');
+            my $fetchresult= &Apache::lonnet::process_coursefile('propagate',$crs,$cdom,$filename,'');
         }
 
         my @imports = ();
         if ($toplevel eq 'oldfolder') {
             for (my $i=0; $i<@topurls; $i++) {
-                my $url = &Apache::lonnet::unescape($topurls[$i]);
-                my $name = &Apache::lonnet::unescape($topnames[$i]);
-                push @imports, $name, $url;
+                my $url = &unescape($topurls[$i]);
+                my $name = &unescape($topnames[$i]);
+                push(@imports, [$name, $url]);
             }
         } elsif ($toplevel eq 'newfolder') {
-            my $url = &Apache::lonnet::unescape("/uploaded/$cdom/$crs/$timenow/sequences/Top.sequence");
-            my $name = &Apache::lonnet::unescape("$ENV{'form.foldername'}");
-            push @imports, $name, $url;
+            my $url = &unescape("/uploaded/$cdom/$crs/$timenow/sequences/Top.sequence");
+            my $name = &unescape("$env{'form.foldername'}");
+            push(@imports, [$name, $url]);
         }
         my $errtext='';
         my $fatal=0;
         ($errtext,$fatal)=  &Apache::londocs::mapread($crs,$cdom,$folder.'.sequence');
-        if ($#Apache::lonratedt::order<1) {
-            $Apache::lonratedt::order[0]=1;
-            $Apache::lonratedt::resources[1]='';
+        if ($#LONCAPA::map::order<1) {
+            $LONCAPA::map::order[0]=1;
+            $LONCAPA::map::resources[1]='';
         }
-        my ($errtext,$fatal)=&Apache::londocs::group_import($crs, $cdom, $folder,@imports);
+        my ($errtext,$fatal)=&Apache::londocs::group_import($crs,$cdom,$folder,'sequence','imsimport',@imports);
         if ($fatal) {
-            print STDERR "Fatal error during group_import\n";
+            &Apache::lonnet::logthis("Fatal error during group_import.");
         }
     }
     if ($tempdir =~ m/^\/home\/httpd\/perl\/tmp\/$crs\/\d{10}/) {
@@ -648,7 +673,7 @@ sub display_three {
        </td>
       </tr>
       <tr bgcolor='#CCCCFF'>
-       <td valign='middle'><img src='/res/adm/pages/bl_step5.gif'>
+       <td valign="middle"><img src="/res/adm/pages/bl_step5.gif" alt="5" />
        </td>
        <td width='100%' align='left'>&nbsp;&nbsp;
         <font face='arial,helvetica,sans-serif'><b>Your import is complete</b></font>
@@ -666,11 +691,11 @@ ENDBLOCK
            <input type="hidden" name="orgurl" value="/adm/coursedocs" />
            <input type="hidden" name="selectrole" value="1" />
            <h3><font color="red">Changes will become active for your current session after
-           <input type="hidden" name="$ENV{'request.role'}" value="1" />
+           <input type="hidden" name="$env{'request.role'}" value="1" />
            <input type="button" value="|;
     $initblock .= &mt('re-initializing course');
-    $initblock .= qq|" onClick="javascript:init(this.form)"/>|;
-    $initblock .= &mt('or the next time you log in.');
+    $initblock .= qq|" onClick="javascript:init(this.form)" />|;
+    $initblock .= ', '.&mt('or the next time you log in.');
     $initblock .= qq|</font></h3></form>|;  
     $r->print($initblock);
     $r->print(<<ENDBLOCKTWO);