--- loncom/imspackages/imsprocessor.pm	2004/03/09 16:44:01	1.2
+++ loncom/imspackages/imsprocessor.pm	2004/03/21 20:44:58	1.6
@@ -3,12 +3,52 @@ package Apache::imsprocessor;
 use Apache::lonnet;
 use LONCAPA::Configuration;
 use strict;
+
+sub ims_config {
+    my ($areas,$cmsmap,$areaname) = @_;
+    @{$areas} = ("doc","extlink","announce","staff","board","quiz","survey","pool","users");
+    %{$$cmsmap{bb5}} = (
+                announce => 'resource/x-bb-announcement',
+                board => 'resource/x-bb-discussionboard',
+                doc => 'resource/x-bb-document',
+                extlink => 'resource/x-bb-externallink',
+                pool => 'assessment/x-bb-pool',
+                quiz => 'assessment/x-bb-quiz',
+                staff => 'resource/x-bb-staffinfo',
+                survey => 'assessment/x-bb-survey',
+                users => 'course/x-bb-user',
+                );
+ 
+    %{$$cmsmap{angel}} =  (
+                board => 'BOARD',
+                extlink => 'LINK',
+                msg => 'MESSAGE',
+                quiz => 'QUIZ',
+                survey => 'FORM',
+                );
+
+    @{$$cmsmap{angel}{doc}} = ('FILE','PAGE');
+
+
+    %{$areaname} = (
+                announce => 'Announcements',
+                board => 'Discussion Boards',
+                doc => 'Documents, pages, and folders',
+                extlink => 'Links to external sites',
+                pool => 'Question pools',
+                quiz => 'Quizzes',
+                staff => 'Staff information',
+                survey => 'Surveys',
+                users => 'Enrollment',
+                );
+         
+}
  
 sub create_tempdir {
-    my ($caller,$pathinfo,$timenow) = @_;   
+    my ($context,$pathinfo,$timenow) = @_;   
     my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
     my $tempdir;
-    if ($caller eq 'DOCS') {
+    if ($context eq 'DOCS') {
         $tempdir =  $$configvars{'lonDaemons'}.'/tmp/'.$pathinfo;
         if (!-e "$tempdir") {
             mkdir("$tempdir",0770);
@@ -17,7 +57,7 @@ sub create_tempdir {
         if (!-e "$tempdir") {
             mkdir("$tempdir",0770);
         } 
-    } elsif ($caller eq "CSTR") {
+    } elsif ($context eq "CSTR") {
         if (!-e "$pathinfo/temp") {
             mkdir("$pathinfo/temp",0770);
         }
@@ -26,6 +66,36 @@ sub create_tempdir {
     return $tempdir;
 }
 
+sub uploadzip {
+    my ($context,$tempdir,$source) = @_;
+    my $fname;
+    if ($context eq 'DOCS') {
+        $fname=$ENV{'form.uploadname.filename'};
+# Replace Windows backslashes by forward slashes
+        $fname=~s/\\/\//g;
+# Get rid of everything but the actual filename
+        $fname=~s/^.*\/([^\/]+)$/$1/;
+# Replace spaces by underscores
+        $fname=~s/\s+/\_/g;
+# Replace all other weird characters by nothing
+        $fname=~s/[^\w\.\-]//g;
+# See if there is anything left
+        unless ($fname) { return 'error: no uploaded file'; }
+# Save the file
+        chomp($ENV{'form.uploadname'});
+        open(my $fh,'>'.$tempdir.'/'.$fname);
+        print $fh $ENV{'form.uploadname'};
+        close($fh);
+    } elsif ($context eq 'CSTR') {
+        if ($source =~ m/\/([^\/]+)$/) {
+            $fname = $1;
+            my $destination = $tempdir.'/'.$fname;
+            rename($source,$destination);
+        }
+    }
+    return $fname;   
+}
+
 sub expand_zip {
     my ($tempdir,$filename) = @_;
     my $zipfile = "$tempdir/$filename";
@@ -60,7 +130,9 @@ sub process_manifest {
                       contentscount => 0,
                       resnum => 'toplevel',
                       );
-    %{$$resources{'toplevel'}} = ();
+    %{$$resources{'toplevel'}} = (
+                                  revitm => 'Top'
+                                 );
  
     if ($cms eq 'angel') {
         $$resources{'toplevel'}{type} = "FOLDER";
@@ -177,6 +249,11 @@ sub process_manifest {
         text_h =>
             [sub {
                 my ($text) = @_;
+                if ($state[0] eq "manifest" && $state[1] eq "organizations" && $state[2] eq "organization" && $state[-1] eq "title") {
+                    if ($cms eq 'angel') {
+                        $$items{$itm}{title} = $text;
+                    }
+                }
               }, "dtext"],
         end_h =>
               [sub {
@@ -212,31 +289,15 @@ sub copy_resources {
                 foreach my $file (@{$$hrefs{$key}}) {
                     my $source = $tempdir.'/'.$key.'/'.$file;
                     my $filename = '';
-                    my $fpath = $timenow.'/resfiles/'.$key.'/'; 
-                    if ($cms eq 'bb5') {
-                        if ($file =~ m-/-) {
-                            my @items = split/\//,$file;
-                            $filename = pop @items;
-                            $fpath .= join('/',@items);
-                            $fpath .= '/';
-                        } else {
-                            $filename = $file;
-                        }
-                        &Apache::lonnet::userfileupload(undef,'1',$filename,$fpath,$source);                            
-                    } elsif ($cms eq 'angel') {
-                        $file =~ s-\\-/-g;
-                        unless ($file eq 'pg'.$key.'.htm') {
-                            if ($file =~ m-/-) {
-                                my @items = split/\//,$file;
-                                $filename = pop @items;
-                                $fpath = join('/',@items);
-                                $fpath .= '/';
-                            } else {
-                                $filename = $file;
-                            }
-                            &Apache::lonnet::userfileupload(undef,'1',$filename,$fpath,$source);                            
+                    my $fpath = $timenow.'/resfiles/'.$key.'/';
+                    if ($cms eq 'angel') {
+                        if ($file eq 'pg'.$key.'.htm') {
+                            next;
                         }
                     }
+                    $file =~ s-\\-/-g;
+                    $file = $fpath.$file;
+                    my $fileresult = &Apache::lonnet::process_coursefile('copy',$crs,$cdom,$chome,$file,$source);
                 }
             }
         }
@@ -274,6 +335,36 @@ sub copy_resources {
     }
 }
 
+sub process_coursefile {
+    my ($crs,$cdom,$chome,$file,$source)=@_;
+    my $fetchresult = '';
+    my $fpath = '';
+    my $fname = $file;
+    ($fpath,$fname) = ($file =~ m/^(.*)\/([^\/])$/);
+    $fpath=$cdom.'/'.$crs.'/'.$fpath;
+    my $filepath=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles';
+    unless ($fpath eq '') {
+        my @parts=split(/\//,$fpath);
+        foreach my $part (@parts) {
+            $filepath.= '/'.$part;
+            if ((-e $filepath)!=1) {
+                mkdir($filepath,0777);
+            }
+        }
+    }
+    if ($source eq '') {
+        $fetchresult eq 'no source file provided';
+    } else {
+        my $destination = $filepath.'/'.$fname;
+        rename($source,$destination);
+        $fetchresult= &Apache::lonnet::reply('fetchuserfile:'.$cdom.'/'.$crs.'/'.$file,$chome);
+        unless ($fetchresult eq 'ok') {
+            &Apache::lonnet::logthis('Failed to transfer '.$cdom.'/'.$crs.'/'.$fname.' to host '.$chome.': '.$fetchresult);
+        }
+    }
+    return $fetchresult;
+}
+
 sub process_resinfo {
     my ($cms,$docroot,$destdir,$items,$resources,$boards,$announcements,$quizzes,$surveys,$groups,$messages,$timestamp,$boardnum,$resinfo,$udom,$uname,$cdom,$crs,$db_handling,$user_handling,$total,$dirname,$seqstem,$resrcfiles) = @_;
     my $board_id = time;
@@ -301,9 +392,11 @@ sub process_resinfo {
                 &angel_content($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$$resources{$key}{type},$$items{$$resources{$key}{revitm}}{title},$resrcfiles);
             } elsif ($$resources{$key}{type} eq "QUIZ") {
                 %{$$resinfo{$key}} = ();
+                push @{$quizzes}, $key;
 #               &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
             } elsif ($$resources{$key}{type} eq "FORM") {
                 %{$$resinfo{$key}} = ();
+                push @{$surveys}, $key;
 #                &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
             } elsif ($$resources{$key}{type} eq "DROPBOX") {
                 %{$$resinfo{$key}} = ();
@@ -312,8 +405,8 @@ sub process_resinfo {
     } elsif ($cms eq 'bb5') {
         foreach my $key (sort keys %{$resources}) {
             if ($$resources{$key}{type} eq "resource/x-bb-document") {
-                %{$$resinfo{$key}} = ();
                 unless ($$items{$$resources{$key}{revitm}}{filepath} eq 'Top') {
+                    %{$$resinfo{$key}} = ();
                     &process_content($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$resrcfiles);
                 }
             } elsif ($$resources{$key}{type} eq "resource/x-bb-staffinfo") {
@@ -355,32 +448,32 @@ sub process_resinfo {
                 unless ($announce_handling eq 'ignore') {
                     push @{$announcements}, $key;
                     %{$$resinfo{$key}} = ();
-                    &process_announce($key,$docroot,$destdir,\%{$$resinfo{$key}},$items,$resinfo,$seqstem,$resrcfiles);
+                    &process_announce($key,$docroot,$destdir,\%{$$resinfo{$key}},$resinfo,$seqstem,$resrcfiles);
                 }
             }
         }
+        if (@{$announcements}) {
+            $$items{'Top'}{'contentscount'} ++;
+        }
+        if (@{$boards}) {
+            $$items{'Top'}{'contentscount'} ++;
+        }
+        if (@{$quizzes}) {
+            $$items{'Top'}{'contentscount'} ++;
+        }
+        if (@{$surveys}) {
+            $$items{'Top'}{'contentscount'} ++;
+        
+        }
     }
-    $$total{'board'} = $board_count;
 
-    if (@{$announcements}) {
-        $$items{'Top'}{'contentscount'} ++;
-    }
-    if (@{$boards}) {
-        $$items{'Top'}{'contentscount'} ++;
-    }
-    if (@{$quizzes}) {
-        $$items{'Top'}{'contentscount'} ++;
-        $$total{'quiz'} = @{$quizzes};
-    }
-    if (@{$surveys}) {
-        $$items{'Top'}{'contentscount'} ++;
-        $$total{'surv'} = @{$surveys};
-    }
+    $$total{'board'} = $board_count;
+    $$total{'quiz'} = @{$quizzes};
+    $$total{'surv'} = @{$surveys};
 }
 
 sub build_structure {
-    my ($cms,$context,$destdir,$resinfo,$items,$resources,$hrefs,$udom,$uname,$newdir,$timenow,$cdom,$crs,$timestamp,$total,$boards,$announcements,$quizzes,$surveys,$boardnum,$pagesfiles,$seqfiles,$topurls,$topnames) = @_;
-
+    my ($cms,$context,$destdir,$items,$resinfo,$resources,$hrefs,$udom,$uname,$newdir,$timenow,$cdom,$crs,$timestamp,$total,$boards,$announcements,$quizzes,$surveys,$boardnum,$pagesfiles,$seqfiles,$topurls,$topnames) = @_;
     my %flag = ();
     my %count = ();
     my %pagecontents = ();
@@ -453,10 +546,7 @@ sub build_structure {
                 my $title = $$items{$contitem}{title};
                 $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$resinfo,$contitem,$uname,$cdom);
                 unless ($flag{$key}{page} == 1) {
-                    $seqtext{$key} .= qq|<resource id="$curr_id" src="$src" title="$title"|;
-                    unless ($cms eq 'bb5' && $key eq 'Top' && @{$announcements}) {
-                        $seqtext{$key} .= qq| type="start"|;
-                    }
+                    $seqtext{$key} .= qq|<resource id="$curr_id" src="$src" title="$title" type="start"|;
                     unless ($flag{$key}{seq} || $flag{$key}{board} || $flag{$key}{file}) {
                         $flag{$key}{page} = 1;
                     }
@@ -561,7 +651,7 @@ sub build_structure {
 
     my $filestem;
     if ($context eq 'DOCS') {
-        $filestem = "/uploaded/$cdom/$crs";
+        $filestem = "/uploaded/$cdom/$crs/$timenow";
     } elsif ($context eq 'CSTR') {
         $filestem = "/res/$udom/$uname/$newdir";
     }
@@ -571,12 +661,12 @@ sub build_structure {
             my $filename = $destdir.'/pages/'.$key.'_'.$i.'.page';
             open(PAGEFILE,">$filename");
             print PAGEFILE qq|<map>
-<resource src="$filestem/resfiles/$$items{$pagecontents{$key}[$i][0]}{resnum}/$$items{$pagecontents{$key}[$i][0]}{resnum}.html" id="1" type="start" title="$$items{$pagecontents{$key}[$i][0]}{title}"></resource>
+<resource src="$filestem/resfiles/$$items{$pagecontents{$key}[$i][0]}{resnum}.html" id="1" type="start" title="$$items{$pagecontents{$key}[$i][0]}{title}"></resource>
 <link to="2" index="1" from="1">\n|;
             if (@{$pagecontents{$key}[$i]} == 1) {
-                print PAGEFILE qq|<resource src="" id="2" type="finish"></resource>|;
+                print PAGEFILE qq|<resource src="" id="2" type="finish"></resource>\n|;
             } elsif (@{$pagecontents{$key}[$i]} == 2)  {
-                print PAGEFILE qq|<resource src="$filestem/resfiles/$$items{$pagecontents{$key}[$i][1]}{resnum}.html" id="2" type="finish" title="$$items{$pagecontents{$key}[$i][1]}{title}"></resource>|;
+                print PAGEFILE qq|<resource src="$filestem/resfiles/$$items{$pagecontents{$key}[$i][1]}{resnum}.html" id="2" type="finish" title="$$items{$pagecontents{$key}[$i][1]}{title}"></resource>\n|;
             } else {
                 for (my $j=1; $j<@{$pagecontents{$key}[$i]}-1; $j++) {
                     my $curr_id = $j+1;
@@ -586,7 +676,7 @@ sub build_structure {
 <link to="$next_id" index="$curr_id" from="$curr_id">\n|;
                 }
                 my $final_id = @{$pagecontents{$key}[$i]};
-                print PAGEFILE qq|<resource src="$filestem/resfiles/$$items{$pagecontents{$key}[$i][-1]}{resnum}/$$items{$pagecontents{$key}[$i][-1]}{resnum}.html" id="$final_id" type="finish" title="$$items{$pagecontents{$key}[$i][-1]}{title}"></resource>\n|;
+                print PAGEFILE qq|<resource src="$filestem/resfiles/$$items{$pagecontents{$key}[$i][-1]}{resnum}.html" id="$final_id" type="finish" title="$$items{$pagecontents{$key}[$i][-1]}{title}"></resource>\n|;
             }
             print PAGEFILE "</map>";
             close(PAGEFILE);
@@ -618,8 +708,8 @@ sub make_structure {
         $$flag{$key}{file} = 1;
     } elsif ($cms eq 'angel' && (($type eq "PAGE") || ($type eq "LINK")) )  {
         if ($$flag{$key}{page}) {
-            if ($$count{key}{page} == -1) {
-                print STDERR "Array index is -1, we shouldnt be here\n";
+            if ($$count{$key}{page} == -1) {
+                print STDERR "Array index is -1, we shouldnt be here, key is $key, type is $type\n";
             } else { 
                 push @{$$pagecontents{$key}[$$count{$key}{page}]},$contitem;
             }
@@ -651,7 +741,7 @@ sub process_specials {
     my $nextnum = 0;
     my $seqstem = '';
     if ($context eq 'CSTR') {
-        $seqstem = '/res/'.$udom.'/'.$uname.'/'.$newdir;
+        $seqstem = "/res/$udom/$uname/$newdir";
     } elsif ($context eq 'DOCS') {
         $seqstem = '/uploaded/'.$cdom.'/'.$crs.'/'.$timenow;
     }
@@ -1542,7 +1632,7 @@ sub process_assessment {
     foreach my $id (@allids) {
         my $output = qq|<problem>
 |;
-        $$total{problem} ++;
+        $$total{prob} ++;
         if ($$settings{$id}{class} eq "QUESTION_ESSAY") {
             $output .= qq|<startouttext />$$settings{$id}{text}<endouttext />
  <essayresponse>
@@ -1751,7 +1841,7 @@ sub process_assessment {
 
 # ---------------------------------------------------------------- Process Blackboard Announcements
 sub process_announce {
-    my ($res,$docroot,$destdir,$settings,$items,$globalresref,$seqstem,$resrcfiles) = @_;
+    my ($res,$docroot,$destdir,$settings,$globalresref,$seqstem,$resrcfiles) = @_;
     my $xmlfile = $docroot.'/'.$res.".dat";
     my @state = ();
     my @assess = ();
@@ -1812,7 +1902,7 @@ sub process_announce {
   
     if (@assess > 0) {
         foreach my $id (@assess) {
-            $$settings{text} = "A $$settings{startassessment}{$id}{assessment_type}, entitled $$globalresref{$$settings{startassessment}{$id}{assessment_id}}{title} is available. Click <a href='$seqstem/$$items{$$settings{startassessment}{$id}{assessment_id}}{revitm}.sequence'>here</a> to enter the folder the contains the problems in this assessment.";
+            $$settings{text} = "A $$settings{startassessment}{$id}{assessment_type}, entitled $$globalresref{$$settings{startassessment}{$id}{assessment_id}}{title} is available. Click <a href='$seqstem/pages/$$settings{startassessment}{$id}{assessment_id}.page' target='quizpage'>here</a> to enter the page that contains the problems in this assessment.";
         }
     }
 
@@ -1825,7 +1915,7 @@ sub process_announce {
 <body bgcolor='#ffffff'>
 <table>
  <tr>
-  <td bgcolor='#CCCCFF'>$$settings{title} - announcement date: $$settings{date}</td>
+  <td bgcolor='#CCCCFF'>$$settings{title} - announcement date: $$settings{dates}</td>
  </tr>
 </table>
 <br/>