--- loncom/imspackages/imsimportdocs.pm	2004/03/21 20:44:58	1.5
+++ loncom/imspackages/imsimportdocs.pm	2005/10/27 18:19:36	1.14
@@ -1,3 +1,26 @@
+# Copyright Michigan State University Board of Trustees
+#
+# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
+#
+# LON-CAPA is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# LON-CAPA is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with LON-CAPA; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+# /home/httpd/html/adm/gpl.txt
+#
+# http://www.lon-capa.org/
+#
+
 package Apache::imsimportdocs;
 
 use Apache::Constants qw(:common :http :methods);
@@ -120,22 +143,21 @@ 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 $uname=$env{'user.name'};
+    my $udom=$env{'user.domain'};
     my $plainname=&Apache::lonnet::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(&Apache::loncommon::bodytag('Import IMS package',undef,'',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.').'</body></html>');
         return OK;
     }
@@ -143,11 +165,11 @@ sub handler {
     &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);
     }
 
@@ -164,13 +186,13 @@ $javascript
 </head>
 ENDHEAD
 # -------------------------------------------------------------------- Body tag
-    $r->print(&Apache::loncommon::bodytag('Import IMS package'));
-    if ($ENV{'form.phase'} eq 'one') {
+    $r->print(&Apache::loncommon::bodytag('Import IMS package',undef,'',1));
+    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>");
     return OK;
@@ -210,7 +232,9 @@ Please choose the CMS used to create you
         <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='webct4'>WebCT 4
         </select>
         </font>
        </td>
@@ -234,7 +258,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>
@@ -275,7 +299,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);
@@ -285,6 +309,8 @@ sub display_two {
         $unzip_result = &Apache::imsprocessor::expand_zip($tempdir,$fname);
     }
     my %resources = ();
+    my %includedres = ();
+    my %includeditems = ();
     my %items = ();
     my %hrefs = ();
     my %resinfo = ();
@@ -307,10 +333,10 @@ 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') {
+                if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webct4') {
                     foreach my $area (keys %{$cmsmap{$cms}}) {
                         if ($resources{$res}{type} eq $cmsmap{$cms}{$area}) {
                             $count{$area} ++;
@@ -440,7 +466,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,15 +501,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 = ();
@@ -505,7 +533,6 @@ sub display_three {
     my $timenow = time;
 
     my $destdir = $Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.$cdom.'/'.$crs.'/'.$timenow;
-    my $dirname = $cdom.'/'.$crs.'/'.$timenow;
     my $seqstem = "/uploaded/$cdom/$crs/$timenow";
     my $db_handling = '';
     my $user_handling = '';
@@ -513,32 +540,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 (defined($env{"form.$area"}) && ($env{'form.'.$area} ne '')) {
             if ($cms eq 'angel' && $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) {
@@ -553,37 +596,40 @@ sub display_three {
                 mkdir("$fullpath/$name",0770);
             }
         }
-        &Apache::imsprocessor::target_resources(\%resources,\%imports,\@targets);
-        my $copy_result = &Apache::imsprocessor::copy_resources('DOCS',$cms,\%hrefs,$tempdir,\@targets,\%urls,$crs,$cdom,$chome,$destdir,$timenow);
+        &Apache::imsprocessor::target_resources(\%resources,\%importareas,\@targets);
 
         my @boards = ();
         my @announcements = ();
         my @quizzes = ();
         my @surveys = ();
+        my @pools = ();
         my @groups = ();
         my %messages = ();
         my @timestamp = ();
         my %boardnum = ();
         my @topurls = ();
         my @topnames = ();
+        my @packages = ();
 
-        &Apache::imsprocessor::process_resinfo($cms,$tempdir,$destdir,\%items,\%resources,\@boards,\@announcements,\@quizzes,\@surveys,\@groups,\%messages,\@timestamp,\%boardnum,\%resinfo,$udom,$uname,$cdom,$crs,$db_handling,$user_handling,\%total,$dirname,$seqstem,\@resrcfiles);
+        &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,$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);
- 
         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 = ();
@@ -595,7 +641,7 @@ sub display_three {
             }
         } elsif ($toplevel eq 'newfolder') {
             my $url = &Apache::lonnet::unescape("/uploaded/$cdom/$crs/$timenow/sequences/Top.sequence");
-            my $name = &Apache::lonnet::unescape("$ENV{'form.foldername'}");
+            my $name = &Apache::lonnet::unescape("$env{'form.foldername'}");
             push @imports, $name, $url;
         }
         my $errtext='';
@@ -605,7 +651,7 @@ sub display_three {
             $Apache::lonratedt::order[0]=1;
             $Apache::lonratedt::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";
         }
@@ -641,11 +687,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 .= ', '.&mt('or the next time you log in.');
     $initblock .= qq|</font></h3></form>|;  
     $r->print($initblock);
     $r->print(<<ENDBLOCKTWO);