File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.14: download - view: text, annotated - select for diffs
Mon Feb 14 22:46:12 2005 UTC (19 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
User's choice of content types to import (e.g., only discussion boards, and quizzes) is now incorporated when building the LON-CAPA structure from the description of course organization in the IMS manifest file.  In addition, a .sequence file is now created for access to all questions contained in question pools.  Previously these questions were only accessible in the case of imports into CSTR.

    1: # Copyright Michigan State University Board of Trustees
    2: #
    3: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    4: #
    5: # LON-CAPA is free software; you can redistribute it and/or modify
    6: # it under the terms of the GNU General Public License as published by
    7: # the Free Software Foundation; either version 2 of the License, or
    8: # (at your option) any later version.
    9: #
   10: # LON-CAPA is distributed in the hope that it will be useful,
   11: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13: # GNU General Public License for more details.
   14: #
   15: # You should have received a copy of the GNU General Public License
   16: # along with LON-CAPA; if not, write to the Free Software
   17: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18: #
   19: # /home/httpd/html/adm/gpl.txt
   20: #
   21: # http://www.lon-capa.org/
   22: #
   23: 
   24: package Apache::imsprocessor;
   25: 
   26: use Apache::lonnet;
   27: use LONCAPA::Configuration;
   28: use strict;
   29: 
   30: sub ims_config {
   31:     my ($areas,$cmsmap,$areaname) = @_;
   32:     @{$areas} = ("doc","extlink","announce","staff","board","quiz","survey","pool","users");
   33:     %{$$cmsmap{bb5}} = (
   34:                 announce => 'resource/x-bb-announcement',
   35:                 board => 'resource/x-bb-discussionboard',
   36:                 doc => 'resource/x-bb-document',
   37:                 extlink => 'resource/x-bb-externallink',
   38:                 pool => 'assessment/x-bb-pool',
   39:                 quiz => 'assessment/x-bb-quiz',
   40:                 staff => 'resource/x-bb-staffinfo',
   41:                 survey => 'assessment/x-bb-survey',
   42:                 users => 'course/x-bb-user',
   43:                 );
   44:     %{$$cmsmap{bb6}} =  %{$$cmsmap{bb5}};
   45:     $$cmsmap{bb6}{conference} = 'resource/x-bb-conference';
   46:     %{$$cmsmap{angel}} =  (
   47:                 board => 'BOARD',
   48:                 extlink => 'LINK',
   49:                 msg => 'MESSAGE',
   50:                 quiz => 'QUIZ',
   51:                 survey => 'FORM',
   52:                 );
   53:     @{$$cmsmap{angel}{doc}} = ('FILE','PAGE');
   54:     %{$areaname} = (
   55:                 announce => 'Announcements',
   56:                 board => 'Discussion Boards',
   57:                 doc => 'Documents, pages, and folders',
   58:                 extlink => 'Links to external sites',
   59:                 pool => 'Question pools',
   60:                 quiz => 'Quizzes',
   61:                 staff => 'Staff information',
   62:                 survey => 'Surveys',
   63:                 users => 'Enrollment',
   64:                 );
   65: }
   66:  
   67: sub create_tempdir {
   68:     my ($context,$pathinfo,$timenow) = @_;   
   69:     my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
   70:     my $tempdir;
   71:     if ($context eq 'DOCS') {
   72:         $tempdir =  $$configvars{'lonDaemons'}.'/tmp/'.$pathinfo;
   73:         if (!-e "$tempdir") {
   74:             mkdir("$tempdir",0770);
   75:         } 
   76:         $tempdir .= '/'.$timenow;
   77:         if (!-e "$tempdir") {
   78:             mkdir("$tempdir",0770);
   79:         } 
   80:     } elsif ($context eq "CSTR") {
   81:         if (!-e "$pathinfo/temp") {
   82:             mkdir("$pathinfo/temp",0770);
   83:         }
   84:         $tempdir =  $pathinfo.'/temp';
   85:     }
   86:     return $tempdir;
   87: }
   88: 
   89: sub uploadzip {
   90:     my ($context,$tempdir,$source) = @_;
   91:     my $fname;
   92:     if ($context eq 'DOCS') {
   93:         $fname=$ENV{'form.uploadname.filename'};
   94: # Replace Windows backslashes by forward slashes
   95:         $fname=~s/\\/\//g;
   96: # Get rid of everything but the actual filename
   97:         $fname=~s/^.*\/([^\/]+)$/$1/;
   98: # Replace spaces by underscores
   99:         $fname=~s/\s+/\_/g;
  100: # Replace all other weird characters by nothing
  101:         $fname=~s/[^\w\.\-]//g;
  102: # See if there is anything left
  103:         unless ($fname) { return 'error: no uploaded file'; }
  104: # Save the file
  105:         chomp($ENV{'form.uploadname'});
  106:         open(my $fh,'>'.$tempdir.'/'.$fname);
  107:         print $fh $ENV{'form.uploadname'};
  108:         close($fh);
  109:     } elsif ($context eq 'CSTR') {
  110:         if ($source =~ m/\/([^\/]+)$/) {
  111:             $fname = $1;
  112:             my $destination = $tempdir.'/'.$fname;
  113:             rename($source,$destination);
  114:         }
  115:     }
  116:     return $fname;   
  117: }
  118: 
  119: sub expand_zip {
  120:     my ($tempdir,$filename) = @_;
  121:     my $zipfile = "$tempdir/$filename";
  122:     if (!-e "$zipfile") {
  123:         return 'no zip';
  124:     }
  125:     if ($filename =~ m|\.zip$|i) {
  126:     # unzip can cause an sh launch which can pass along all of %ENV
  127:     # which can be too large for /bin/sh to handle
  128:         my %oldENV=%ENV;
  129:         undef(%ENV);
  130:         open(OUTPUT, "unzip -o $zipfile -d $tempdir  2> /dev/null |");
  131:         close(OUTPUT);
  132:         %ENV=%oldENV;
  133:         undef(%oldENV);
  134:     } else {
  135:         return 'nozip';
  136:     }
  137:     if ($filename =~ m|\.zip$|i) {
  138:         unlink($zipfile);
  139:     }
  140:     return 'ok';
  141: }
  142: 
  143: sub process_manifest {
  144:     my ($cms,$tempdir,$resources,$items,$hrefs,$resinfo,$phase,$includedres,$includeditems) = @_;
  145:     my %toc = (
  146:               bb6 => 'organization',
  147:               bb5 => 'tableofcontents',
  148:               angel => 'organization',
  149:               );
  150:     my %contents = ();
  151:     my @state = ();
  152:     my $itm = '';
  153:     my $identifier = '';
  154:     my @seq = "Top";
  155:     my $lastitem;
  156:     %{$$items{'Top'}} = (
  157:                       contentscount => 0,
  158:                       resnum => 'toplevel',
  159:                       );
  160:     %{$$resources{'toplevel'}} = (
  161:                                   revitm => 'Top'
  162:                                  );
  163:  
  164:     if ($cms eq 'angel') {
  165:         $$resources{'toplevel'}{type} = "FOLDER";
  166:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  167:         $$resources{'toplevel'}{type} = 'resource/x-bb-document';
  168:     } else {
  169:         $$resources{'toplevel'}{type} = 'webcontent';
  170:     }
  171: 
  172:     unless (-e "$tempdir/imsmanifest.xml") {
  173:         return 'nomanifest';
  174:     } 
  175: 
  176:     my $xmlfile = $tempdir.'/imsmanifest.xml';
  177:     my $p = HTML::Parser->new
  178:     (
  179:        xml_mode => 1,
  180:        start_h =>
  181:            [sub {
  182:                 my ($tagname, $attr) = @_;
  183:                 push @state, $tagname;
  184:                 my $num = @state - 3;
  185:                 my $start = $num;
  186:                 my $statestr = '';
  187:                 foreach (@state) {
  188:                     $statestr .= "$_ ";
  189:                 }
  190:                 if ( ($state[0] eq "manifest") && ($state[1] eq "organizations") && ($state[2] eq $toc{$cms}) ) {
  191:                     my $searchstr = "manifest organizations $toc{$cms}";
  192:                     while ($num > 0) {
  193:                         $searchstr .= " item";
  194:                         $num --; 
  195:                     }
  196:                     if (("@state" eq $searchstr) && (@state > 3)) {
  197:                         $itm = $attr->{identifier};
  198:                         if ($$includeditems{$itm} || $phase ne 'build') {              
  199:                             %{$$items{$itm}} = ();
  200:                             $$items{$itm}{contentscount} = 0;
  201:                             if ($cms eq 'bb5' || $cms eq 'bb6') {
  202:                                 $$items{$itm}{resnum} = $attr->{identifierref};
  203:                                 if ($cms eq 'bb5') {
  204:                                     $$items{$itm}{title} = $attr->{title};
  205:                                 }
  206:                             } elsif ($cms eq 'angel') {
  207:                                 if ($attr->{identifierref} =~ m/^res(.+)$/) {
  208:                                     $$items{$itm}{resnum} = $1;
  209:                                 }
  210:                             }
  211:                             unless (defined(%{$$resources{$$items{$itm}{resnum}}}) ) {
  212:                                 %{$$resources{$$items{$itm}{resnum}}} = ();
  213:                             }
  214:                             $$resources{$$items{$itm}{resnum}}{revitm} = $itm;
  215:                             if ($start > @seq) {
  216:                                 unless ($lastitem eq '') {
  217:                                     push @seq, $lastitem;
  218:                                     unless ( defined($contents{$seq[-1]}) ) {
  219:                                         @{$contents{$seq[-1]}} = ();
  220:                                     }
  221:                                     push @{$contents{$seq[-1]}},$itm;
  222:                                     $$items{$itm}{parentseq} = $seq[-1];
  223:                                 }
  224:                             } elsif ($start < @seq) {
  225:                                 my $diff = @seq - $start;
  226:                                 while ($diff > 0) {
  227:                                     pop @seq;
  228:                                     $diff --;
  229:                                 }
  230:                                 if (@seq) {
  231:                                     push @{$contents{$seq[-1]}}, $itm;
  232:                                 }
  233:                             } else {
  234:                                 push @{$contents{$seq[-1]}}, $itm;
  235:                             }
  236:                             my $path;
  237:                             if (@seq > 1) {
  238:                                 $path = join(',',@seq);
  239:                             } elsif (@seq > 0) {
  240:                                 $path = $seq[0];
  241:                             }
  242:                             $$items{$itm}{filepath} = $path;
  243:                             if ($cms eq 'bb5' || $cms eq 'bb6') {
  244:                                 if ($$items{$itm}{filepath} eq 'Top') {
  245:                                     $$items{$itm}{resnum} = $itm;
  246:                                     $$resources{$$items{$itm}{resnum}}{type} = 'resource/x-bb-document';
  247:                                     $$resources{$$items{$itm}{resnum}}{revitm} = $itm;
  248:                                     $$resinfo{$$items{$itm}{resnum}}{'isfolder'} = 'true';
  249:                                 }
  250:                             }
  251:                             $$items{$seq[-1]}{contentscount} ++;
  252:                             $lastitem = $itm;
  253:                         }
  254:                     }
  255:                 } elsif ("@state" eq "manifest resources resource" ) {
  256:                     $identifier = $attr->{identifier};
  257:                     if ($$includedres{$identifier} || $phase ne 'build') { 
  258:                         if ($cms eq 'bb5' || $cms eq 'bb6') {
  259:                             $$resources{$identifier}{file} = $attr->{file};
  260:                             $$resources{$identifier}{type} = $attr->{type};
  261:                         } elsif ($cms eq 'angel') {
  262:                             $identifier = substr($identifier,3);
  263:                             if ($attr->{href} =~ m-^_assoc/$identifier/(.+)$-) {
  264:                                 $$resources{$identifier}{file} = $1;
  265:                             }
  266:                         }
  267:                         @{$$hrefs{$identifier}} = ();
  268:                     }
  269:                 } elsif ("@state" eq "manifest resources resource file") {
  270:                     if ($$includedres{$identifier} || $phase ne 'build') {
  271:                         if ($cms eq 'bb5' || $cms eq 'bb6') {
  272:                             push @{$$hrefs{$identifier}},$attr->{href};
  273:                         } elsif ($cms eq 'angel') {
  274:                             if ($attr->{href} =~ m/^_assoc\\$identifier\\(.+)$/) {
  275:                                 push @{$$hrefs{$identifier}},$1;
  276:                             } elsif ($attr->{href} =~ m/^Icons\\icon(\w+)\.gif/) {
  277:                                 $$resources{$identifier}{type} = $1;
  278:                             }
  279:                         }
  280:                     }
  281:                 }
  282:            }, "tagname, attr"],
  283:         text_h =>
  284:             [sub {
  285:                 my ($text) = @_;
  286:                 if ($state[0] eq "manifest" && $state[1] eq "organizations" && $state[2] eq $toc{$cms} && $state[-1] eq "title") {
  287:                     if ($$includeditems{$itm} || $phase ne 'build') {
  288:                         if ($cms eq 'angel' || $cms eq 'bb6') {
  289:                             $$items{$itm}{title} = $text;
  290:                         }
  291:                     }
  292:                 }
  293:               }, "dtext"],
  294:         end_h =>
  295:               [sub {
  296:                   my ($tagname) = @_;
  297:                   pop @state;
  298:                }, "tagname"],
  299:     );
  300:     $p->parse_file($xmlfile);
  301:     $p->eof;
  302: 
  303:     foreach my $itm (keys %contents) {
  304:         @{$$items{$itm}{contents}} = @{$contents{$itm}};
  305:     }
  306:     return 'ok' ;
  307: }
  308: 
  309: sub get_imports {
  310:     my ($includeditems,$items,$resources,$importareas,$itm) = @_;
  311:     if (exists($$items{$itm}{resnum})) {
  312:         if ($$importareas{$$resources{$$items{$itm}{resnum}}{type}}) {
  313:             unless (exists($$includeditems{$itm})) {
  314:                 $$includeditems{$itm} = 1;
  315:             }
  316:         }
  317:     }
  318:     if ($$items{$itm}{contentscount} > 0) {
  319:         foreach my $child (@{$$items{$itm}{contents}}) {
  320:             &get_imports($includeditems,$items,$resources,$importareas,$child);
  321:         }
  322:     }
  323: }
  324: 
  325: sub get_parents {
  326:     my ($includeditems,$items,$itm) = @_;
  327:     my @pathitems = ();
  328:     if ($$items{$itm}{filepath} =~ m/,/) {
  329:        @pathitems = split/,/,$$items{$itm}{filepath};
  330:     } else {
  331:        $pathitems[0] = $$items{$itm}{filepath};
  332:     }
  333:     foreach (@pathitems) {
  334:         $$includeditems{$_} = 1;
  335:     }
  336: }
  337: 
  338: sub target_resources {
  339:     my ($resources,$oktypes,$targets) = @_;
  340:     foreach my $key (keys %{$resources}) {
  341:         if ( defined($$oktypes{$$resources{$key}{type}}) ) {
  342:             push @{$targets}, $key;
  343:         }
  344:     }
  345:     return;
  346: }
  347: 
  348: sub copy_resources {
  349:     my ($context,$cms,$hrefs,$tempdir,$targets,$url,$crs,$cdom,$chome,$destdir,$timenow) = @_;
  350:     if ($context eq 'DOCS') {
  351:         foreach my $key (sort keys %{$hrefs}) {
  352:             if (grep/^$key$/,@{$targets}) {
  353:                 %{$$url{$key}} = ();
  354:                 foreach my $file (@{$$hrefs{$key}}) {
  355:                     my $source = $tempdir.'/'.$key.'/'.$file;
  356:                     my $filename = '';
  357:                     my $fpath = $timenow.'/resfiles/'.$key.'/';
  358:                     if ($cms eq 'angel') {
  359:                         if ($file eq 'pg'.$key.'.htm') {
  360:                             next;
  361:                         }
  362:                     }
  363:                     $file =~ s-\\-/-g;
  364:                     $file = $fpath.$file;
  365:                     my $fileresult = &Apache::lonnet::process_coursefile('copy',$crs,$cdom,$chome,$file,$source);
  366:                 }
  367:             }
  368:         }
  369:     } elsif ($context eq 'CSTR') {
  370:         if (!-e "$destdir/resfiles") {
  371:             mkdir("$destdir/resfiles",0770);
  372:         }
  373:         foreach my $key (sort keys %{$hrefs}) {
  374:             if (grep/^$key$/,@{$targets}) {
  375:                 foreach my $file (@{$$hrefs{$key}}) {
  376:                     $file =~ s-\\-/-g;
  377:                     if ( ($cms eq 'angel' && $file ne 'pg'.$key.'.htm') || ($cms eq 'bb5') || ($cms eq 'bb6') ) {
  378:                         if (!-e "$destdir/resfiles/$key") {
  379:                             mkdir("$destdir/resfiles/$key",0770);
  380:                         }
  381: 
  382:                         my $filepath = $file;
  383:                         my $front = '';
  384:                         while ($filepath =~ m-(\w+)/(.+)-) {
  385:                             $front .= $1.'/';
  386:                             $filepath = $2;
  387:                             my $fulldir = "$destdir/resfiles/$key/$front";
  388:                             chop($fulldir);
  389:                             if (!-e "$fulldir") {
  390:                                 mkdir("$fulldir",0770);
  391:                             }
  392:                         }
  393:                         if ($cms eq 'angel') {
  394:                             rename("$tempdir/_assoc/$key/$file","$destdir/resfiles/$key/$file");
  395:                         } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  396:                             rename("$tempdir/$key/$file","$destdir/resfiles/$key/$file");
  397:                         }
  398:                     }
  399:                 }
  400:             }
  401:         }
  402:     }
  403: }
  404: 
  405: sub process_resinfo {
  406:     my ($cms,$context,$docroot,$destdir,$items,$resources,$targets,$boards,$announcements,$quizzes,$surveys,$pools,$groups,$messages,$timestamp,$boardnum,$resinfo,$udom,$uname,$cdom,$crs,$db_handling,$user_handling,$total,$dirname,$seqstem,$resrcfiles,$packages,$hrefs,$pagesfiles,$sequencesfiles) = @_;
  407:     my $board_id = time;
  408:     my $board_count = 0;
  409:     my $announce_handling = 'include';
  410:     my $longcrs = '';
  411:     if ($crs =~ m/^(\d)(\d)(\d)/) {
  412:         $longcrs = $1.'/'.$2.'/'.$3.'/'.$crs;
  413:     }
  414:     if ($context eq 'CSTR') {
  415:         if (!-e "$destdir/resfiles") {
  416:             mkdir("$destdir/resfiles",0770);
  417:         }
  418:     }
  419:     if ($cms eq 'angel') {
  420:         my $currboard = '';
  421:         foreach my $key (sort keys %{$resources}) {
  422:           if (grep/^$key$/,@{$targets}) {
  423:             if ($$resources{$key}{type} eq "BOARD") {
  424:                 push @{$boards}, $key;
  425:                 $$boardnum{$$resources{$key}{revitm}} = $board_count;
  426:                 $currboard = $key;
  427:                 @{$$messages{$key}} = ();
  428:                 $$timestamp[$board_count] = $board_id;
  429:                 $board_id ++;
  430:                 $board_count ++;
  431:             } elsif ($$resources{$key}{type} eq "MESSAGE") {
  432:                 push @{$$messages{$currboard}}, $key;
  433:             } elsif ($$resources{$key}{type} eq "PAGE" || $$resources{$key}{type} eq "LINK") {
  434:                 %{$$resinfo{$key}} = ();
  435:                 &angel_content($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$$resources{$key}{type},$$items{$$resources{$key}{revitm}}{title},$resrcfiles);
  436:             } elsif ($$resources{$key}{type} eq "QUIZ") {
  437:                 %{$$resinfo{$key}} = ();
  438:                 push @{$quizzes}, $key;
  439: #               &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  440:             } elsif ($$resources{$key}{type} eq "FORM") {
  441:                 %{$$resinfo{$key}} = ();
  442:                 push @{$surveys}, $key;
  443: #                &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  444:             } elsif ($$resources{$key}{type} eq "DROPBOX") {
  445:                 %{$$resinfo{$key}} = ();
  446:             }
  447:           }
  448:         }
  449:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  450:         foreach my $key (sort keys %{$resources}) {
  451:           if (grep/^$key$/,@{$targets}) {
  452:             if ($$resources{$key}{type} eq "resource/x-bb-document") {
  453:                 unless ($$items{$$resources{$key}{revitm}}{filepath} eq 'Top') {
  454:                     %{$$resinfo{$key}} = ();
  455:                     &process_content($cms,$key,$context,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$resrcfiles,$packages,$hrefs);
  456:                 }
  457:             } elsif ($$resources{$key}{type} eq "resource/x-bb-staffinfo") {
  458:                 %{$$resinfo{$key}} = ();
  459:                 &process_staff($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  460:             } elsif ($$resources{$key}{type} eq "resource/x-bb-externallink") {
  461:                 %{$$resinfo{$key}} = ();
  462:                 &process_link($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  463:             } elsif ($$resources{$key}{type} eq "resource/x-bb-discussionboard") {
  464:                 %{$$resinfo{$key}} = ();
  465:                 unless ($db_handling eq 'ignore') {
  466:                     push @{$boards}, $key;
  467:                     $$timestamp[$board_count] = $board_id;
  468:                     &process_db($key,$docroot,$destdir,$board_id,$crs,$cdom,$db_handling,$uname,\%{$$resinfo{$key}},$longcrs);
  469:                     $board_id ++;
  470:                     $board_count ++;
  471:                 }
  472:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-pool") {
  473:                 %{$$resinfo{$key}} = ();
  474:                 &process_assessment($context,$key,$docroot,'pool',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles);
  475:                 push @{$pools}, $key;
  476:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-quiz") {
  477:                 %{$$resinfo{$key}} = ();
  478:                 &process_assessment($context,$key,$docroot,'quiz',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles);
  479:                 push @{$quizzes}, $key;
  480:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-survey") {
  481:                 %{$$resinfo{$key}} = ();
  482:                 &process_assessment($context,$key,$docroot,'survey',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles);
  483:                 push @{$surveys}, $key;
  484:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-group") {
  485:                 %{$$resinfo{$key}} = ();
  486:                 push @{$groups}, $key;
  487:                 &process_group($key,$docroot,$destdir,\%{$$resinfo{$key}});
  488:             } elsif ($$resources{$key}{type} eq "resource/x-bb-user") {   
  489:                 %{$$resinfo{$key}} = ();
  490:                 unless ($user_handling eq 'ignore') {
  491:                     &process_user($key,$docroot,$destdir,\%{$$resinfo{$key}},$crs,$cdom,$user_handling);
  492:                 }
  493:             } elsif ($$resources{$key}{type} eq "resource/x-bb-announcement") {
  494:                 unless ($announce_handling eq 'ignore') {
  495:                     push @{$announcements}, $key;
  496:                     %{$$resinfo{$key}} = ();
  497:                     &process_announce($key,$docroot,$destdir,\%{$$resinfo{$key}},$resinfo,$seqstem,$resrcfiles);
  498:                 }
  499:             }
  500:           }
  501:         }
  502:         if (@{$announcements}) {
  503:             $$items{'Top'}{'contentscount'} ++;
  504:         }
  505:         if (@{$boards}) {
  506:             $$items{'Top'}{'contentscount'} ++;
  507:         }
  508:         if (@{$quizzes}) {
  509:             $$items{'Top'}{'contentscount'} ++;
  510:         }
  511:         if (@{$surveys}) {
  512:             $$items{'Top'}{'contentscount'} ++;
  513:         }
  514:         if (@{$pools}) {
  515:             $$items{'Top'}{'contentscount'} ++;
  516:         }
  517:     }
  518: 
  519:     $$total{'board'} = $board_count;
  520:     $$total{'quiz'} = @{$quizzes};
  521:     $$total{'surv'} = @{$surveys};
  522:     $$total{'pool'} = @{$pools};
  523: }
  524: 
  525: sub build_structure {
  526:     my ($cms,$context,$destdir,$items,$resinfo,$resources,$targets,$hrefs,$udom,$uname,$newdir,$timenow,$cdom,$crs,$timestamp,$total,$boards,$announcements,$quizzes,$surveys,$pools,$boardnum,$pagesfiles,$seqfiles,$topurls,$topnames,$packages,$includeditems) = @_;
  527:     my %flag = ();
  528:     my %count = ();
  529:     my %pagecontents = ();
  530:     my %seqtext = ();
  531:     my $topnum = 0;
  532:     my $topspecials = @$announcements + @$boards + @$quizzes + @$surveys + @$pools;
  533: 
  534:     if (!-e "$destdir") {
  535:         mkdir("$destdir",0755);
  536:     }
  537:     if (!-e "$destdir/sequences") {
  538:         mkdir("$destdir/sequences",0770);
  539:     }
  540:     if (!-e "$destdir/resfiles") {
  541:         mkdir("$destdir/resfiles",0770);
  542:     }
  543:     if (!-e "$destdir/pages") {
  544:         mkdir("$destdir/pages",0770);
  545:     }
  546:     if (!-e "$destdir/problems") {
  547:         mkdir("$destdir/problems",0770);
  548:     }
  549: 
  550:     $seqtext{'Top'} = qq|<map>\n|;       
  551:     %{$$resinfo{$$items{'Top'}{resnum}}} = (
  552:                                          isfolder => 'true',
  553:                                         );
  554: 
  555:     my $srcstem = "";
  556:  
  557:     if ($context eq 'DOCS') {
  558:         $srcstem = "/uploaded/$cdom/$crs/$timenow";
  559:     } elsif ($context eq 'CSTR') {
  560:         $srcstem = "/res/$udom/$uname/$newdir";
  561:     }
  562: 
  563:     foreach my $key (sort keys %{$items}) {
  564:       if ($$includeditems{$key}) {
  565:         %{$flag{$key}} = (
  566:                           page => 0,
  567:                           seq => 0,
  568:                           board => 0,
  569:                           file => 0,
  570:                          );
  571: 
  572:         %{$count{$key}} = (
  573:                            page => -1,
  574:                            seq => 0,
  575:                            board => 0,
  576:                            file => 0,
  577:                           );
  578: 
  579:         my $src = "";
  580: 
  581:         my $next_id = 2;
  582:         my $curr_id = 1;
  583:         my $resnum = $$items{$key}{resnum};
  584:         my $type = $$resources{$resnum}{type};
  585:         if (($cms eq 'angel' && $type eq "FOLDER") || (($cms eq 'bb5' || $cms eq 'bb6') && $$resinfo{$resnum}{'isfolder'} eq "true") && (($type eq "resource/x-bb-document") || ($type eq "resource/x-bb-staffinfo") || ($type eq "resource/x-bb-externallink")) ) {
  586:             unless (($cms eq 'bb5') && $key eq 'Top') {
  587:                 $seqtext{$key} = "<map>\n";
  588:             }
  589:             if ($$items{$key}{contentscount} == 0) {
  590: 	        if ($key eq 'Top') {
  591:                     unless ($topspecials) {
  592:                         $seqtext{$key} .= qq|<resource id="$curr_id" src="" type="start"></resource>
  593: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  594: <resource id="$next_id" src="" type="finish"></resource>\n|;
  595:                     }
  596:                 } else {
  597:                     $seqtext{$key} .= qq|<resource id="$curr_id" src="" type="start"></resource>
  598: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  599: <resource id="$next_id" src="" type="finish"></resource>\n|;
  600:                 }
  601:             } else {
  602:                 my $contcount = @{$$items{$key}{contents}};
  603:                 my $contitem = $$items{$key}{contents}[0];
  604:                 my $res = $$items{$contitem}{resnum};
  605:                 my $type = $$resources{$res}{type};
  606:                 my $title = $$items{$contitem}{title};
  607:                 my $packageflag = 0;
  608:                 if (grep/^$res$/,@{$packages}) {
  609:                     $packageflag = 1;
  610:                 }
  611:                 $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag);
  612:                 unless ($flag{$key}{page} == 1) {
  613:                     $seqtext{$key} .= qq|<resource id="$curr_id" src="$src" title="$title" type="start"|;
  614:                     unless ($flag{$key}{seq} || $flag{$key}{board} || $flag{$key}{file}) {
  615:                         $flag{$key}{page} = 1;
  616:                     }
  617:                     if ($key eq 'Top') {
  618:                         push @{$topurls}, $src;
  619:                         push @{$topnames}, $title;
  620:                     }
  621:                 }
  622:                 if ($contcount == 1) {
  623:                     $seqtext{$key} .= qq|></resource>
  624: <link from="$curr_id" to="$next_id" index="$curr_id"></link>|;
  625:                     if ($key eq 'Top') {
  626:                         unless ($topspecials) {
  627:                             $seqtext{$key} .= qq|
  628: <resource id="$next_id" src="" type="finish"></resource>\n|;
  629:                         }
  630:                     } else {
  631:                         $seqtext{$key} .= qq|
  632: <resource id="$next_id" src="" type="finish"></resource>\n|;
  633:                     }
  634:                 } else {
  635:                     if ($contcount > 2 ) {
  636:                         for (my $i=1; $i<$contcount-1; $i++) {
  637:                             my $contitem = $$items{$key}{contents}[$i];
  638:                             my $res = $$items{$contitem}{resnum};
  639:                             my $type = $$resources{$res}{type};
  640:                             my $title = $$items{$contitem}{title};
  641:                             my $packageflag = 0;
  642:                             if (grep/^$res$/,@{$packages}) {
  643:                                 $packageflag = 1;
  644:                             }
  645:                             $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag);
  646:                             unless ($flag{$key}{page} == 1) {
  647:                                 $seqtext{$key} .= qq|></resource>
  648: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  649: <resource id="$next_id" src="$src" title="$title"|;
  650:                                 $curr_id ++;
  651:                                 $next_id ++;
  652:                                 unless ($flag{$key}{seq} || $flag{$key}{board} || $flag{$key}{file}) {
  653:                                     $flag{$key}{page} = 1;
  654:                                 }
  655:                                 if ($key eq 'Top') {
  656:                                     push @{$topurls}, $src;
  657:                                     push @{$topnames}, $title;
  658:                                 }
  659:                             }
  660:                         }
  661:                     }
  662:                     my $contitem = $$items{$key}{contents}[-1];
  663:                     my $res = $$items{$contitem}{resnum};
  664:                     my $type = $$resources{$res}{type};
  665:                     my $title = $$items{$contitem}{title};
  666:                     my $packageflag = 0;
  667:                     if (grep/^$res$/,@{$packages}) {
  668:                         $packageflag = 1;
  669:                     }
  670:                     $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag);
  671:                     if ($flag{$key}{page}) {
  672:                         if ($count{$key}{seq} + $count{$key}{page} + $count{$key}{board} + $count{$key}{file} +1 == 1) {
  673:                             $seqtext{$key} .= qq|></resource>
  674: <link from="$curr_id" index="$curr_id" to="$next_id">
  675: <resource id ="$next_id" src="" |;
  676:                         }
  677:                     } else {
  678:                         $seqtext{$key} .= qq|></resource>
  679: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  680: <resource id="$next_id" src="$src" title="$title" |;
  681:                         if ($key eq 'Top') {
  682:                             push @{$topurls}, $src;
  683:                             push @{$topnames}, $title;
  684:                         }
  685:                     }
  686:                     if ($contcount == $$items{$key}{contentscount}) {
  687:                         $seqtext{$key} .= qq|type="finish"></resource>\n|;
  688:                     } else {
  689:                         $curr_id ++;
  690:                         $next_id ++;
  691:                         $seqtext{$key} .= qq|></resource>
  692: <link from="$curr_id" to="$next_id" index="$curr_id"></link>\n|;
  693:                     } 
  694:                 }
  695:             }
  696:             unless (($cms eq 'bb5') && $key eq 'Top') {
  697:                 $seqtext{$key} .= "</map>\n";
  698:                 open(LOCFILE,">$destdir/sequences/$key.sequence");
  699:                 print LOCFILE $seqtext{$key};
  700:                 close(LOCFILE);
  701:                 push @{$seqfiles}, "$key.sequence";
  702:             }
  703:             $count{$key}{page} ++;
  704:             $$total{page} += $count{$key}{page};
  705:         }
  706:         $$total{seq} += $count{$key}{seq};
  707:       }
  708:     }
  709:     $topnum += ($count{'Top'}{page} + $count{'Top'}{seq});
  710: 
  711:     if ($cms eq 'bb5' || $cms eq 'bb6') {
  712:         if (@{$announcements} > 0) {
  713:             &process_specials($context,'announcements',$announcements,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  714:         }
  715:         if (@{$boards} > 0) {
  716:             &process_specials($context,'boards',$boards,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  717:         }
  718:         if (@{$quizzes} > 0) {
  719:             &process_specials($context,'quizzes',$quizzes,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  720:         }
  721:         if (@{$surveys} > 0)  {
  722:             &process_specials($context,'surveys',$surveys,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  723:         }
  724:         if (@{$pools} > 0)  {
  725:             &process_specials($context,'pools',$pools,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  726:         }
  727:         $seqtext{'Top'} .= "</map>\n";
  728:         open(TOPFILE,">$destdir/sequences/Top.sequence");
  729:         print TOPFILE $seqtext{'Top'};
  730:         close(TOPFILE);
  731:         push @{$seqfiles}, 'Top.sequence';
  732:     }
  733: 
  734:     my $filestem;
  735:     if ($context eq 'DOCS') {
  736:         $filestem = "/uploaded/$cdom/$crs/$timenow";
  737:     } elsif ($context eq 'CSTR') {
  738:         $filestem = "/res/$udom/$uname/$newdir";
  739:     }
  740: 
  741:     foreach my $key (sort keys %pagecontents) {
  742:         for (my $i=0; $i<@{$pagecontents{$key}}; $i++) {
  743:             my $filename = $destdir.'/pages/'.$key.'_'.$i.'.page';
  744:             my $resource = "$filestem/resfiles/$$items{$pagecontents{$key}[$i][0]}{resnum}.html";
  745:             my $res = $$items{$pagecontents{$key}[$i][0]}{resnum};
  746:             my $resource = $filestem.'/resfiles/'.$res.'.html';
  747:             if (grep/^$res$/,@{$packages}) {
  748:                 $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # should be entry_point
  749:             }
  750:             open(PAGEFILE,">$filename");
  751:             print PAGEFILE qq|<map>
  752: <resource src="$resource" id="1" type="start" title="$$items{$pagecontents{$key}[$i][0]}{title}"></resource>
  753: <link to="2" index="1" from="1">\n|;
  754:             if (@{$pagecontents{$key}[$i]} == 1) {
  755:                 print PAGEFILE qq|<resource src="" id="2" type="finish"></resource>\n|;
  756:             } elsif (@{$pagecontents{$key}[$i]} == 2)  {
  757:                 my $res = $$items{$pagecontents{$key}[$i][1]}{resnum};
  758:                 my $resource = $filestem.'/resfiles/'.$res.'.html';
  759:                 if (grep/^$res$/,@{$packages}) {
  760:                     $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # should be entry_point
  761:                 }
  762:                 print PAGEFILE qq|<resource src="$resource" id="2" type="finish" title="$$items{$pagecontents{$key}[$i][1]}{title}"></resource>\n|;
  763:             } else {
  764:                 for (my $j=1; $j<@{$pagecontents{$key}[$i]}-1; $j++) {
  765:                     my $curr_id = $j+1;
  766:                     my $next_id = $j+2;
  767:                     my $res = $$items{$pagecontents{$key}[$i][$j]}{resnum};
  768:                     my $resource = $filestem.'/resfiles/'.$res.'.html';
  769:                     if (grep/^$res$/,@{$packages}) {
  770:                         $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # entry_point
  771:                     }
  772:                     print PAGEFILE qq|<resource src="$resource" id="$curr_id" title="$$items{$pagecontents{$key}[$i][$j]}{title}"></resource>
  773: <link to="$next_id" index="$curr_id" from="$curr_id">\n|;
  774:                 }
  775:                 my $final_id = @{$pagecontents{$key}[$i]};
  776:                 my $res = $$items{$pagecontents{$key}[$i][-1]}{resnum};
  777:                 my $resource = $filestem.'/resfiles/'.$res.'.html';
  778:                 if (grep/^$res$/,@{$packages}) {
  779:                     $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # entry_point
  780:                 }
  781:                 print PAGEFILE qq|<resource src="$resource" id="$final_id" type="finish" title="$$items{$pagecontents{$key}[$i][-1]}{title}"></resource>\n|;
  782:             }
  783:             print PAGEFILE "</map>";
  784:             close(PAGEFILE);
  785:             push @{$pagesfiles}, $key.'_'.$i.'.page'; 
  786:         }
  787:     }
  788: }
  789: 
  790: sub make_structure {
  791:     my ($cms,$key,$srcstem,$flag,$count,$timestamp,$boardnum,$hrefs,$pagecontents,$res,$type,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag) = @_;
  792:     my $src ='';
  793:     if (($cms eq 'angel' && $type eq 'FOLDER') || (($cms eq 'bb5' || $cms eq 'bb6') && ($$resinfo{$res}{'isfolder'} eq 'true')  || ($key eq 'Top')) ) {
  794:         $src = $srcstem.'/sequences/'.$contitem.'.sequence';
  795:         $$flag{$key}{page} = 0;
  796:         $$flag{$key}{seq} = 1;
  797:         $$count{$key}{seq} ++;
  798:     } elsif ($cms eq 'angel' && $type eq 'BOARD') {
  799:         $src = '/adm/'.$cdom.'/'.$uname.'/'.$$timestamp[$$boardnum{$res}].'/bulletinboard'; 
  800:         $$flag{$key}{page} = 0;
  801:         $$flag{$key}{board} = 1;
  802:         $$count{$key}{board} ++;
  803:     } elsif ($cms eq 'angel' && $type eq "FILE") {
  804:         foreach my $file (@{$$hrefs{$res}}) {
  805:             unless ($file eq 'pg'.$res.'.htm') {
  806:                 $src = $srcstem.'/resfiles/'.$res.'/'.$file;
  807:             }
  808:         }
  809:         $$flag{$key}{page} = 0;
  810:         $$flag{$key}{file} = 1;
  811:     } elsif ($cms eq 'angel' && (($type eq "PAGE") || ($type eq "LINK")) )  {
  812:         if ($$flag{$key}{page}) {
  813:             if ($$count{$key}{page} == -1) {
  814:                 print STDERR "Array index is -1, we shouldnt be here, key is $key, type is $type\n";
  815:             } else { 
  816:                 push @{$$pagecontents{$key}[$$count{$key}{page}]},$contitem;
  817:             }
  818:         } else {
  819:             $$count{$key}{page} ++;
  820:             $src = $srcstem.'/pages/'.$key.'_'.$$count{$key}{page}.'.page';
  821:             @{$$pagecontents{$key}[$$count{$key}{page}]} = ("$contitem");
  822:             $$flag{$key}{seq} = 0;
  823:         }
  824:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  825:         if ($$flag{$key}{page}) {
  826:             push @{$$pagecontents{$key}[$$count{$key}{page}]},$contitem;
  827:         } else {
  828:             if ($contcount == 1) {
  829:                 if ($packageflag) {
  830:                     $src = $srcstem.'/resfiles/'.$res.'/index.html'; # Needs to be entry point
  831:                 } else {
  832:                     $src = $srcstem.'/resfiles/'.$res.'.html';
  833:                 }
  834:             } else {
  835:                 $$count{$key}{page} ++;
  836:                 $src = $srcstem.'/pages/'.$key.'_'.$$count{$key}{page}.'.page';
  837:                 @{$$pagecontents{$key}[$$count{$key}{page}]} = ("$contitem");
  838:             }
  839:             $$flag{$key}{seq} = 0;
  840:         }
  841:     }
  842:     return $src;
  843: }
  844: 
  845: 
  846: # ---------------------------------------------------------------- Process Blackboard specials - announcements, bulletin boards, quizzes and surveys
  847: sub process_specials {
  848:     my ($context,$type,$specials,$topnum,$contentscount,$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,$seqtext,$pagesfiles,$seqfiles,$topurls,$topnames) = @_;
  849:     my $src = '';
  850:     my $specialsrc = '';
  851:     my $nextnum = 0;
  852:     my $seqstem = '';
  853:     if ($context eq 'CSTR') {
  854:         $seqstem = "/res/$udom/$uname/$newdir";
  855:     } elsif ($context eq 'DOCS') {
  856:         $seqstem = '/uploaded/'.$cdom.'/'.$crs.'/'.$timenow;
  857:     }
  858:     my %seqnames = (
  859:                   boards => 'bulletinboards',
  860:                   quizzes => 'quizzes',
  861:                   surveys => 'surveys',
  862:                   announcements => 'announcements',
  863:                   pools => 'pools'
  864:                   );
  865:     my %seqtitles = (
  866:                   boards => 'Course Bulletin Boards',
  867:                   quizzes => 'Course Quizzes',
  868:                   surveys => 'Course Surveys',
  869:                   announcements => 'Course Announcements',
  870:                   pools => 'Course Question Pools'
  871:                    );
  872:     $$topnum ++;
  873: 
  874:     if ($type eq 'announcements') {
  875:         $src = "$seqstem/pages/$seqnames{$type}.page";
  876:     } else {
  877:         $src = "$seqstem/sequences/$seqnames{$type}.sequence";
  878:     }
  879: 
  880:     push @{$topurls}, $src;
  881:     push @{$topnames}, $seqtitles{$type};
  882: 
  883:     $$seqtext .= qq|<resource id="$$topnum" src="$src" title="$seqtitles{$type}"|;
  884:     $nextnum = $$topnum +1;
  885:     if ($$topnum == 1) {
  886:         $$seqtext .= qq| type="start"></resource>
  887: <link from="$$topnum" to="$nextnum" index="$$topnum"></link>\n|;
  888:         if ($$topnum == $contentscount) {
  889:             $$seqtext .= qq|<resource id="$nextnum" src="" type="finish"></resource>\n|;
  890:         }
  891:     } else {
  892:         if ($$topnum == $contentscount) {
  893:             $$seqtext .= qq| type="finish"></resource>\n|;
  894:         } else {
  895:             $$seqtext .= qq|></resource>
  896: <link from="$$topnum" to="$nextnum" index="$$topnum"></link>\n|;
  897:         }
  898:     }
  899: 
  900:     if ($type eq "announcements") {
  901:         push @{$pagesfiles}, "$seqnames{$type}.page";
  902:         open(ITEM,">$destdir/pages/$seqnames{$type}.page");
  903:     } else {
  904:         push @{$seqfiles}, "$seqnames{$type}.sequence";
  905:         open(ITEM,">$destdir/sequences/$seqnames{$type}.sequence");
  906:     }
  907: 
  908:     if ($type eq 'boards') {
  909:         $specialsrc = "/adm/$udom/$uname/$$timestamp[0]/bulletinboard";
  910:     } elsif ($type eq 'announcements') {
  911:         $specialsrc = "$seqstem/resfiles/$$specials[0].html";
  912:     } elsif ($type eq 'pools') {
  913:         $specialsrc = "$seqstem/sequences/$$specials[0].sequence";
  914:     } else {
  915:         $specialsrc = "$seqstem/pages/$$specials[0].page";
  916:     }
  917:     print ITEM qq|<map>
  918: <resource id="1" src="$specialsrc" title="$$resinfo{$$specials[0]}{title}" type="start"></resource>
  919: <link from="1" to="2" index="1"></link>|;
  920:     if (@{$specials} == 1) {
  921:         print ITEM qq|
  922: <resource id="2" src="" type="finish"></resource>\n|;
  923:     } else {
  924:         for (my $i=1; $i<@{$specials}; $i++) {
  925:             my $curr = $i+1;
  926:             my $next = $i+2;
  927:             if ($type eq 'boards') {
  928:                 $specialsrc = "/adm/$udom/$uname/$$timestamp[$i]/bulletinboard";
  929:             } elsif ($type eq 'announcements') {
  930:                 $specialsrc = "$seqstem/resfiles/$$specials[$i].html";
  931:             } else {
  932:                 $specialsrc = "$seqstem/pages/$$specials[$i].page";
  933:             }
  934:             print ITEM qq|<resource id="$curr" src="$specialsrc" title="$$resinfo{$$specials[$i]}{title}"|;
  935:             if (@{$specials} == $i+1) {
  936:                 print ITEM qq| type="finish"></resource>\n|;
  937:             } else {
  938:                 print ITEM qq|></resource>
  939: <link from="$curr" to="$next" index="$next">\n|;
  940:             }
  941:         }
  942:     }
  943:     print ITEM qq|</map>|;
  944:     close(ITEM);
  945: }
  946: 
  947: # ---------------------------------------------------------------- Process Blackboard users
  948: sub process_user {
  949:   my ($res,$docroot,$destdir,$settings,$user_crs,$user_cdom,$user_handling) = @_;
  950:   my $xmlfile = $docroot.'/'.$res.".dat";
  951:   my $filecount = 0;
  952:   my @state;
  953:   my $userid = '';
  954:   my $linknum = 0;
  955: 
  956:   my $p = HTML::Parser->new
  957:     (
  958:      xml_mode => 1,
  959:      start_h =>
  960:      [sub {
  961:         my ($tagname, $attr) = @_;
  962:         push @state, $tagname;
  963:         if ("@state" eq "USERS USER") {
  964:             $userid = $attr->{value};
  965:             %{$$settings{$userid}} = ();
  966:             @{$$settings{$userid}{links}} = ();
  967:         } elsif ("@state" eq "USERS USER LOGINID") {  
  968:             $$settings{$userid}{loginid} = $attr->{value};
  969:         } elsif ("@state" eq "USERS USER PASSPHRASE") {  
  970:             $$settings{$userid}{passphrase} = $attr->{value};
  971:         } elsif ("@state" eq "USERS USER STUDENTID" ) {
  972:             $$settings{$userid}{studentid} = $attr->{value};
  973:         } elsif ("@state" eq "USERS USER NAMES FAMILY" ) {
  974:             $$settings{$userid}{family} = $attr->{value};
  975:         } elsif ("@state" eq "USERS USER NAMES GIVEN" ) {
  976:             $$settings{$userid}{given} = $attr->{value};
  977:         } elsif ("@state" eq "USERS USER ADDRESSES BUSINESS DATA EMAIL") {
  978:             $$settings{$userid}{email} = $attr->{value};
  979:         } elsif ("@state" eq "USERS USER USER_ROLE") {
  980:             $$settings{$userid}{user_role} = $attr->{value};
  981:         } elsif ("@state" eq "USERS USER FLAGS ISAVAILABLE") {
  982:             $$settings{$userid}{isavailable} = $attr->{value};
  983:         } elsif ("@state" eq "USERS USER PERSONALPAGE FILELIST IMAGE") {
  984:             $$settings{$userid}{image} = $attr->{value};
  985:         } elsif ( ($state[-2] eq "LINKLIST") && ($state[-1] eq "LINK") ) {
  986:             %{$$settings{$userid}{links}[$linknum]} = ();
  987:             $$settings{$userid}{links}[$linknum]{url} = $attr->{value};
  988:             $linknum ++;
  989:         }
  990:      }, "tagname, attr"],
  991:      text_h =>
  992:      [sub {
  993:         my ($text) = @_;
  994:         if ("@state" eq "USERS USER PERSONALPAGE TITLE") {
  995:             $$settings{$userid}{title} = $text;
  996:         } elsif ("@state" eq "USERS USER PERSONALPAGE DESCRIPTION") {
  997:             $$settings{$userid}{description} = $text;
  998:         } elsif (($state[-2] eq "LINK") && ($state[-1] eq "TITLE")) {
  999:             $$settings{$userid}{links}[$linknum]{title} = $text;
 1000:         } elsif (($state[-3] eq "LINK") && ($state[-2] eq  "DESCRIPTION") && ($state[-1] eq "TEXT")) {
 1001:             $$settings{$userid}{links}[$linknum]{text} = $text;
 1002:         }
 1003:       }, "dtext"],
 1004:      end_h =>
 1005:      [sub {
 1006:         my ($tagname) = @_;
 1007:         if ("@state" eq "USERS USER") {
 1008:             $linknum = 0;
 1009:         }
 1010:         pop @state;
 1011:      }, "tagname"],
 1012:     );
 1013:   $p->unbroken_text(1);
 1014:   $p->parse_file($xmlfile);
 1015:   $p->eof;
 1016:   
 1017:   my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
 1018:   my $xmlstem =  $$configvars{'lonDaemons'}."/tmp/".$user_cdom."_".$user_crs."_";
 1019: 
 1020:   foreach my $user_id (keys %{$settings}) {
 1021:       if ($$settings{$user_id}{user_role} eq "s") {
 1022:            
 1023:       } elsif ($user_handling eq 'enrollall') {
 1024: 
 1025:       }
 1026:   }
 1027: }
 1028: 
 1029: # ---------------------------------------------------------------- Process Blackboard groups
 1030: sub process_group {  
 1031:   my ($res,$docroot,$destdir,$settings) = @_;
 1032:   my $xmlfile = $docroot.'/'.$res.".dat";
 1033:   my $filecount = 0;
 1034:   my @state;
 1035:   my $grp;
 1036: 
 1037:   my $p = HTML::Parser->new
 1038:     (
 1039:      xml_mode => 1,
 1040:      start_h =>
 1041:      [sub {
 1042:         my ($tagname, $attr) = @_;
 1043:         push @state, $tagname;
 1044:         if ("@state" eq "GROUPS GROUP") {
 1045:             $grp = $attr->{id};
 1046:         }        
 1047:         if ("@state" eq "GROUPS GROUP TITLE") {
 1048:             $$settings{$grp}{title} = $attr->{value};
 1049:         } elsif ("@state" eq "GROUPS GROUP FLAGS ISAVAILABLE") {  
 1050:             $$settings{$grp}{isavailable} = $attr->{value};
 1051:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASCHATROOM") {  
 1052:             $$settings{$grp}{chat} = $attr->{value};
 1053:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASDISCUSSIONBOARD") {
 1054:             $$settings{$grp}{discussion} = $attr->{value};
 1055:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASTRANSFERAREA") {
 1056:             $$settings{$grp}{transfer} = $attr->{value};
 1057:         } elsif ("@state" eq "GROUPS GROUP FLAGS ISPUBLIC") {
 1058:             $$settings{$grp}{public} = $attr->{value};
 1059:         }
 1060:      }, "tagname, attr"],
 1061:      text_h =>
 1062:      [sub {
 1063:         my ($text) = @_;
 1064:         if ("@state" eq "GROUPS DESCRIPTION") {
 1065:           $$settings{$grp}{description} = $text;
 1066: #          print "Staff text is $text\n";
 1067:         }
 1068:       }, "dtext"],
 1069:      end_h =>
 1070:      [sub {
 1071:         my ($tagname) = @_;
 1072:         pop @state;
 1073:      }, "tagname"],
 1074:     );
 1075:   $p->unbroken_text(1);
 1076:   $p->parse_file($xmlfile);
 1077:   $p->eof;
 1078: }
 1079: 
 1080: # ---------------------------------------------------------------- Process Blackboard Staff
 1081: sub process_staff {
 1082:   my ($res,$docroot,$dirname,$destdir,$settings,$resrcfiles) = @_;
 1083:   my $xmlfile = $docroot.'/'.$res.".dat";
 1084:   my $filecount = 0;
 1085:   my @state;
 1086:   %{$$settings{name}} = ();
 1087:   %{$$settings{office}} = ();  
 1088: 
 1089:   my $p = HTML::Parser->new
 1090:     (
 1091:      xml_mode => 1,
 1092:      start_h =>
 1093:      [sub {
 1094:         my ($tagname, $attr) = @_;
 1095:         push @state, $tagname;
 1096:         if ("@state" eq "STAFFINFO TITLE") {
 1097:             $$settings{title} = $attr->{value};
 1098:         } elsif ("@state" eq "STAFFINFO BIOGRAPHY TEXTCOLOR") {
 1099:             $$settings{textcolor} = $attr->{value};
 1100:         } elsif ("@state" eq "STAFFINFO BIOGRAPHY FLAGS ISHTML") {
 1101:             $$settings{ishtml} = $attr->{value};
 1102:         } elsif ("@state" eq "STAFFINFO FLAGS ISAVAILABLE" ) {
 1103:             $$settings{isavailable} = $attr->{value};
 1104:         } elsif ("@state" eq "STAFFINFO FLAGS ISFOLDER" ) {
 1105:             $$settings{isfolder} = $attr->{value};
 1106:         } elsif ("@state" eq "STAFFINFO POSITION" ) {
 1107:             $$settings{position} = $attr->{value};
 1108:         } elsif ("@state" eq "STAFFINFO HOMEPAGE" ) {
 1109:             $$settings{homepage} = $attr->{value};
 1110:         } elsif ("@state" eq "STAFFINFO IMAGE") {
 1111:             $$settings{image} = $attr->{value};
 1112:         }
 1113:      }, "tagname, attr"],
 1114:      text_h =>
 1115:      [sub {
 1116:         my ($text) = @_;
 1117:         if ("@state" eq "STAFFINFO BIOGRAPHY TEXT") {
 1118:           $$settings{text} = $text;
 1119: #          print "Staff text is $text\n";
 1120:         } elsif ("@state" eq "STAFFINFO CONTACT PHONE") {
 1121:           $$settings{phone} = $text;
 1122:         } elsif ("@state" eq "STAFFINFO CONTACT EMAIL") {
 1123:           $$settings{email} = $text;
 1124:         } elsif ("@state" eq "STAFFINFO CONTACT NAME FORMALTITLE") {
 1125:           $$settings{name}{formaltitle} = $text;
 1126:         } elsif ("@state" eq "STAFFINFO CONTACT NAME FAMILY") {
 1127:           $$settings{name}{family} = $text;
 1128:         } elsif ("@state" eq "STAFFINFO CONTACT NAME GIVEN") {
 1129:           $$settings{name}{given} = $text;
 1130:         } elsif ("@state" eq "STAFFINFO CONTACT OFFICE HOURS") {
 1131:           $$settings{office}{hours} = $text;
 1132:         }  elsif ("@state" eq "STAFFINFO CONTACT OFFICE ADDRESS") {
 1133:           $$settings{office}{address} = $text;
 1134:         }        
 1135:       }, "dtext"],
 1136:      end_h =>
 1137:      [sub {
 1138:         my ($tagname) = @_;
 1139:         pop @state;
 1140:      }, "tagname"],
 1141:     );
 1142:   $p->unbroken_text(1);
 1143:   $p->parse_file($xmlfile);
 1144:   $p->eof;
 1145: 
 1146:     my $fontcol = '';
 1147:     if (defined($$settings{textcolor})) {
 1148:         $fontcol =  qq|color="$$settings{textcolor}"|;
 1149:     }
 1150:     if (defined($$settings{text})) {
 1151:         if ($$settings{ishtml} eq "true") {
 1152:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1153:         }
 1154:     }
 1155:     my $staffentry = qq|
 1156: <table border="0" cellpadding="0" cellspacing="0" width="100%">
 1157:   <tr>
 1158:     <td colspan="2"><hr /><font face="arial,helv" size="3"><b>$$settings{name}{formaltitle} $$settings{name}{given} $$settings{name}{family}</b></font>
 1159:     </td>
 1160:   </tr>
 1161:   <tr>
 1162:     <td valign="top">
 1163:       <table width="100% border="0" cols="2" cellpadding="0" cellspacing="0">|;
 1164:     if ( defined($$settings{email}) && $$settings{email} ne '') {
 1165:         $staffentry .= qq|
 1166:         <tr>
 1167:           <td width="100" valign="top">
 1168:            <font face="arial" size="2"><b>Email:</b></font>
 1169:           </td>
 1170:           <td>
 1171:            <font face="arial" size="2"><a href="mailto:$$settings{email}">$$settings{email}</a></font>
 1172:           </td>
 1173:         </tr>
 1174:         |;
 1175:     }
 1176:     if (defined($$settings{phone}) && $$settings{phone} ne '') {
 1177:         $staffentry .= qq|
 1178:         <tr>
 1179:           <td width="100" valign="top">
 1180:             <font face="arial" size="2"><b>Phone:</b></font>
 1181:           </td>
 1182:           <td>
 1183:             <font face="arial" size="2">$$settings{phone}</font>
 1184:           </td>
 1185:         </tr>
 1186:         |;
 1187:     }
 1188:     if (defined($$settings{office}{address}) && $$settings{office}{address} ne '') {
 1189:         $staffentry .= qq|
 1190:         <tr>
 1191:          <td width="100" valign="top">
 1192:            <font face="arial" size="2"><b>Address:</b></font>
 1193:          </td>
 1194:          <td>
 1195:            <font face="arial" size="2">$$settings{office}{address}</font>
 1196:          </td>
 1197:         </tr>
 1198:         |;
 1199:     }
 1200:     if (defined($$settings{office}{hours}) && $$settings{office}{hours} ne '') {
 1201:         $staffentry .= qq|
 1202:         <tr>
 1203:           <td width="100" valign="top">
 1204:             <font face="arial" size="2"><b>Office Hours:</b></font>
 1205:           </td>
 1206:           <td>
 1207:             <font face=arial size=2>$$settings{office}{hours}</font>
 1208:           </td>
 1209:         </tr>
 1210:         |;
 1211:     }
 1212:     if ( defined($$settings{homepage}) && $$settings{homepage} ne '') {
 1213:         $staffentry .= qq|
 1214:         <tr>
 1215:           <td width="100" valign="top">
 1216:             <font face="arial" size="2"><b>Personal Link:</b></font>
 1217:           </td>
 1218:           <td>
 1219:             <font face="arial" size="2"><a href="$$settings{homepage}">$$settings{homepage}</a></font>
 1220:           </td>
 1221:         </tr>
 1222:         |;
 1223:     }
 1224:     if (defined($$settings{text}) && $$settings{text} ne '') {
 1225:         $staffentry .= qq|
 1226:         <tr>
 1227:           <td colspan="2">
 1228:             <font face="arial" size="2" $fontcol><b>Other Information:</b><br/>$$settings{text}</font>
 1229:           </td>
 1230:         </tr>
 1231:         |;
 1232:      }
 1233:      $staffentry .= qq|
 1234:       </table>
 1235:     </td>
 1236:     <td align="right" valign="top">
 1237:      |;
 1238:      if ( defined($$settings{image}) ) {
 1239:          $staffentry .= qq|
 1240:       <img src="$dirname/resfiles/$res/$$settings{image}">
 1241:          |;
 1242:      }
 1243:      $staffentry .= qq|
 1244:     </td>
 1245:   </tr>
 1246: </table>
 1247:     |;
 1248:     open(FILE,">$destdir/resfiles/$res.html");
 1249:     push @{$resrcfiles}, "$res.html";
 1250:     print FILE qq|<html>
 1251: <head>
 1252: <title>$$settings{title}</title>
 1253: </head>
 1254: <body bgcolor='#ffffff'>
 1255: $staffentry
 1256: </body>
 1257: </html>|;
 1258:     close(FILE);
 1259: }
 1260: 
 1261: # ---------------------------------------------------------------- Process Blackboard Links
 1262: sub process_link {
 1263:     my ($res,$docroot,$dirname,$destdir,$settings,$resrcfiles) = @_;
 1264:     my $xmlfile = $docroot.'/'.$res.".dat";
 1265:     my @state = ();
 1266:     my $p = HTML::Parser->new
 1267:     (
 1268:         xml_mode => 1,
 1269:         start_h =>
 1270:         [sub {
 1271:             my ($tagname, $attr) = @_;
 1272:             push @state, $tagname;
 1273:             if ("@state" eq "EXTERNALLINK TITLE") {
 1274:                 $$settings{title} = $attr->{value};
 1275:             } elsif ("@state" eq "EXTERNALLINK TEXTCOLOR") {  
 1276:                 $$settings{textcolor} = $attr->{value};
 1277:             } elsif ("@state" eq "EXTERNALLINK DESCRIPTION FLAGS ISHTML") {  
 1278:                 $$settings{ishtml} = $attr->{value};                               
 1279:             } elsif ("@state" eq "EXTERNALLINK FLAGS ISAVAILABLE" ) {
 1280:                 $$settings{isavailable} = $attr->{value};
 1281:             } elsif ("@state" eq "EXTERNALLINK FLAGS LAUNCHINNEWWINDOW" ) {
 1282:                 $$settings{newwindow} = $attr->{value};
 1283:             } elsif ("@state" eq "EXTERNALLINK FLAGS ISFOLDER" ) {
 1284:                 $$settings{isfolder} = $attr->{value};
 1285:             } elsif ("@state" eq "EXTERNALLINK POSITION" ) {
 1286:                 $$settings{position} = $attr->{value};
 1287:             } elsif ("@state" eq "EXTERNALLINK URL" ) {
 1288:                 $$settings{url} = $attr->{value};
 1289:             }
 1290:         }, "tagname, attr"],
 1291:         text_h =>
 1292:         [sub {
 1293:             my ($text) = @_;
 1294:             if ("@state" eq "EXTERNALLINK DESCRIPTION TEXT") {
 1295:                $$settings{text} = $text;
 1296:             }
 1297:         }, "dtext"],
 1298:         end_h =>
 1299:         [sub {
 1300:             my ($tagname) = @_;
 1301:             pop @state;
 1302:         }, "tagname"],
 1303:     );
 1304:     $p->unbroken_text(1);
 1305:     $p->parse_file($xmlfile);
 1306:     $p->eof;
 1307: 
 1308:     my $linktag = '';
 1309:     my $fontcol = '';
 1310:     if (defined($$settings{textcolor})) {
 1311:         $fontcol =  qq|<font color="$$settings{textcolor}">|;
 1312:     }
 1313:     if (defined($$settings{text})) {
 1314:         if ($$settings{ishtml} eq "true") {
 1315:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1316:         }
 1317:     }
 1318: 
 1319:     if (defined($$settings{url}) ) {
 1320:         $linktag = qq|<a href="$$settings{url}"|;
 1321:         if ($$settings{newwindow} eq "true") {
 1322:             $linktag .= qq| target="launch"|;
 1323:         }
 1324:         $linktag .= qq|>$$settings{title}</a>|;
 1325:     }
 1326: 
 1327:     open(FILE,">$destdir/resfiles/$res.html");
 1328:     push @{$resrcfiles}, "$res.html";
 1329:     print FILE qq|<html>
 1330: <head>
 1331: <title>$$settings{title}</title>
 1332: </head>
 1333: <body bgcolor='#ffffff'>
 1334: $fontcol
 1335: $linktag
 1336: $$settings{text}
 1337: |;
 1338:     if (defined($$settings{textcolor})) {
 1339:         print FILE qq|</font>|;
 1340:     }
 1341:     print FILE qq|
 1342:   </body>
 1343:  </html>|;
 1344:     close(FILE);
 1345: }
 1346: 
 1347: # ---------------------------------------------------------------- Process Blackboard Discussion Boards
 1348: sub process_db {
 1349:     my ($res,$docroot,$destdir,$timestamp,$crs,$cdom,$handling,$uname,$settings,$longcrs) = @_;
 1350:     my $xmlfile = $docroot.'/'.$res.".dat";
 1351:     my @state = ();
 1352:     my @allmsgs = ();
 1353:     my %msgidx = ();
 1354:     my %threads; # all threads, keyed by message ID
 1355:     my $msg_id; # the current message ID
 1356:     my %message; # the current message being accumulated for $msg_id
 1357: 
 1358:     my $p = HTML::Parser->new
 1359:     (
 1360:        xml_mode => 1,
 1361:        start_h =>
 1362:        [sub {
 1363:            my ($tagname, $attr) = @_;
 1364:            push @state, $tagname;
 1365:            my $depth = 0;
 1366:            my @seq = ();
 1367:            if ("@state" eq "FORUM TITLE") {
 1368:                $$settings{title} = $attr->{value};
 1369:            } elsif ("@state" eq "FORUM DESCRIPTION TEXTCOLOR") {  
 1370:                $$settings{textcolor} = $attr->{value};
 1371:            } elsif ("@state" eq "FORUM DESCRIPTION FLAGS ISHTML") {  
 1372:                $$settings{ishtml} = $attr->{value};
 1373:            } elsif ("@state" eq "FORUM DESCRIPTION FLAGS ISNEWLINELITERAL") {  
 1374:                $$settings{newline} = $attr->{value};
 1375:            } elsif ("@state" eq "FORUM POSITION" ) {
 1376:                $$settings{position} = $attr->{value};
 1377:            } elsif ("@state" eq "FORUM FLAGS ISREADONLY") {
 1378:                $$settings{isreadonly} = $attr->{value};
 1379:            } elsif ("@state" eq "FORUM FLAGS ISAVAILABLE" ) {
 1380:                $$settings{isavailable} = $attr->{value};
 1381:            } elsif ("@state" eq "FORUM FLAGS ALLOWANONYMOUSPOSTINGS" ) {
 1382:                $$settings{allowanon} = $attr->{value};
 1383:            } elsif ( ($state[0] eq "FORUM") && ($state[1] eq "MESSAGETHREADS") && ($state[2] eq "MSG") ) {
 1384:                if ($state[-1] eq "MSG") {
 1385:                    unless ($msg_id eq '') {
 1386:                        push @{$threads{$msg_id}}, { %message };
 1387:                        $depth = @state - 3;
 1388:                        if ($depth > @seq) {
 1389:                            push @seq, $msg_id; 
 1390:                        }
 1391:                    }
 1392:                    if ($depth < @seq) {
 1393:                        pop @seq;
 1394:                    }                
 1395:                    $msg_id = $attr->{id};
 1396:                    push @allmsgs, $msg_id;
 1397:                    $msgidx{$msg_id} = @allmsgs;
 1398:                    %message = ();
 1399:                    $message{depth} = $depth;
 1400:                    if ($depth > 0) {
 1401:                        $message{parent} = $seq[-1];
 1402:                    } else {
 1403:                        $message{parent} = "None";
 1404:                    }
 1405:                } elsif ($state[-1] eq "TITLE") {
 1406:                    $message{title} = $attr->{value};
 1407:                } elsif ( ( $state[-3] eq "MESSAGETEXT" ) && ( $state[-2] eq "FLAGS" ) && ( $state[-1] eq "ISHTML" ) ) {
 1408:                    $message{ishtml} = $attr->{value};
 1409:                } elsif ( ( $state[-3] eq "MESSAGETEXT" ) && ( $state[-2] eq "FLAGS" ) && ( $state[-1] eq "ISNEWLINELITERAL" ) ) {
 1410:                    $message{newline} = $attr->{value};
 1411:                } elsif ( ( $state[-2] eq "DATES" ) && ( $state[-1] eq "CREATED" ) ) {
 1412:                    $message{created} = $attr->{value};
 1413:                } elsif ( $state[@state-2] eq "FLAGS") {
 1414:                    if ($state[@state-1] eq "ISANONYMOUS") {
 1415:                        $message{isanonymous} =  $attr->{value};
 1416:                    }
 1417:                } elsif ( $state[-2] eq "USER" ) {
 1418:                    if ($state[-1] eq "USERID") {
 1419:                        $message{userid} =  $attr->{value};
 1420:                    } elsif ($state[@state-1] eq "USERNAME") {
 1421:                        $message{username} =  $attr->{value};
 1422:                    } elsif ($state[@state-1] eq "EMAIL") {
 1423:                        $message{email} =  $attr->{value};
 1424:                    }          
 1425:                } elsif ( ($state[-2] eq "FILELIST") && ($state[-1] eq "IMAGE") ) {
 1426:                    $message{attachment} = $attr->{value};
 1427:                }
 1428:            }
 1429:        }, "tagname, attr"],
 1430:        text_h =>
 1431:        [sub {
 1432:            my ($text) = @_;
 1433:            if ("@state" eq "FORUM DESCRIPTION TEXT") {
 1434:                $$settings{text} = $text;
 1435:            } elsif ( ($state[0] eq "FORUM") && ($state[1] eq "MESSAGETHREADS") && ($state[2] eq "MSG") ) {
 1436:                if ( ($state[-2] eq "MESSAGETEXT") && ($state[-1] eq "TEXT") ){
 1437:                    $message{text} = $text;
 1438:                }
 1439:            }
 1440:        }, "dtext"],
 1441:        end_h =>
 1442:        [sub {
 1443:            my ($tagname) = @_;
 1444:            if ( $state[-1] eq "MESSAGETHREADS" ) {
 1445:                push @{$threads{$msg_id}}, { %message };
 1446:            }
 1447:            pop @state;
 1448:        }, "tagname"],
 1449:     );
 1450:     $p->unbroken_text(1);
 1451:     $p->parse_file($xmlfile);
 1452:     $p->eof;
 1453: 
 1454:     if (defined($$settings{text})) {
 1455:         if ($$settings{ishtml} eq "false") {
 1456:             if ($$settings{isnewline} eq "true") {
 1457:                 $$settings{text} =~ s#\n#<br/>#g;
 1458:             }
 1459:         } else {
 1460:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1461:         }
 1462:         if (defined($$settings{fontcolor}) ) {
 1463:             $$settings{text} = "<font color=\"".$$settings{textcolor}."\">".$$settings{text}."</font>";
 1464:         }
 1465:     }
 1466:     my $boardname = 'bulletinpage_'.$timestamp;
 1467:     my %boardinfo = (
 1468:                   'aaa_title' => $$settings{title},
 1469:                   'bbb_content' => $$settings{text},
 1470:                   'ccc_webreferences' => '',
 1471:                   'uploaded.lastmodified' => time,
 1472:                   );
 1473:   
 1474:     my $putresult = &Apache::lonnet::put($boardname,\%boardinfo,$cdom,$crs);
 1475:     if ($handling eq 'importall') {
 1476:         foreach my $msg_id (@allmsgs) {
 1477:             foreach my $message ( @{$threads{$msg_id}} ) {
 1478:                 my %contrib = (
 1479:                             'sendername' => $$message{userid},
 1480:                             'senderdomain' => $cdom,
 1481:                             'screenname' => '',
 1482:                             'plainname' => $$message{username},
 1483:                             );
 1484:                 unless ($$message{parent} eq 'None') {
 1485:                     $contrib{replyto} = $msgidx{$$message{parent}};
 1486:                 }
 1487:                 if (defined($$message{isanonymous}) ) {
 1488:                     if ($$message{isanonymous} eq 'true') {
 1489:                         $contrib{'anonymous'} = 'true';
 1490:                     }
 1491:                 }
 1492:                 if ( defined($$message{attachment}) )  {
 1493:                     my $url = $$message{attachment};
 1494:                     my $oldurl = $url;
 1495:                     my $newurl = $url;
 1496:                     unless ($url eq '') {
 1497:                         $newurl =~ s/\//_/g;
 1498:                         unless ($longcrs eq '') {
 1499:                             if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles") {
 1500:                                 mkdir("/home/httpd/lonUsers/$cdom/$longcrs/userfiles",0755);
 1501:                             }
 1502:                             if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl") {
 1503:                                 system("cp $destdir/resfiles/$res/$$message{attachment} /home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl");
 1504:                             }
 1505:                             $contrib{attachmenturl} = '/uploaded/'.$cdom.'/'.$crs.'/'.$newurl;
 1506:                         }
 1507:                     }
 1508:                 }
 1509:                 if (defined($$message{title}) ) {
 1510:                     $contrib{'message'} = $$message{title};
 1511:                 }
 1512:                 if (defined($$message{text})) {
 1513:                     if ($$message{ishtml} eq "false") {
 1514:                         if ($$message{isnewline} eq "true") {
 1515:                             $$message{text} =~ s#\n#<br/>#g;
 1516:                         }
 1517:                     } else {
 1518:                         $$message{text} = &HTML::Entities::decode($$message{text});
 1519:                     }
 1520:                     $contrib{'message'} .= '<br /><br />'.$$message{text};
 1521:                     my $symb = 'bulletin___'.$timestamp.'___adm/wrapper/adm/'.$cdom.'/'.$uname.'/'.$timestamp.'/bulletinboard';
 1522:                     my $postresult = &addposting($symb,\%contrib,$cdom,$crs);
 1523:                 }
 1524:             }
 1525:         }
 1526:     }
 1527: }
 1528: 
 1529: # ---------------------------------------------------------------- Add Posting to Bulletin Board
 1530: sub addposting {
 1531:     my ($symb,$contrib,$cdom,$crs)=@_;
 1532:     my $status='';
 1533:     if (($symb) && ($$contrib{message})) {
 1534:          my $crsdom = $cdom.'_'.$crs;
 1535:          &Apache::lonnet::store($contrib,$symb,$crsdom,$cdom,$crs);
 1536:          my %storenewentry=($symb => time);
 1537:          &Apache::lonnet::put('discussiontimes',\%storenewentry,$cdom,$crs);
 1538:     }
 1539:     my %record=&Apache::lonnet::restore('_discussion');
 1540:     my ($temp)=keys %record;
 1541:     unless ($temp=~/^error\:/) {
 1542:         my %newrecord=();
 1543:         $newrecord{'resource'}=$symb;
 1544:         $newrecord{'subnumber'}=$record{'subnumber'}+1;
 1545:         &Apache::lonnet::cstore(\%newrecord,'_discussion');
 1546:         $status = 'ok';
 1547:     } else {
 1548:         $status.='Failed.';
 1549:     }
 1550:     return $status;
 1551: }
 1552: # ---------------------------------------------------------------- Process Blackboard Assessments - pools, quizzes, surveys
 1553: sub process_assessment {
 1554:     my ($context,$res,$docroot,$container,$dirname,$destdir,$settings,$total,$udom,$uname,$pagesfiles,$sequencesfiles) = @_;
 1555:     my $xmlfile = $docroot.'/'.$res.".dat";
 1556: #  print "XML file is $xmlfile\n";
 1557:     my @state = ();
 1558:     my @allids = ();
 1559:     my %allanswers = ();
 1560:     my %allchoices = ();
 1561:     my $id; # the current question ID
 1562:     my $answer_id; # the current answer ID
 1563:     my %toptag = ( pool => 'POOL',
 1564:                  quiz => 'ASSESSMENT',
 1565:                  survey => 'ASSESSMENT'
 1566:                );
 1567: 
 1568:     my $p = HTML::Parser->new
 1569:     (
 1570:      xml_mode => 1,
 1571:      start_h =>
 1572:      [sub {
 1573:         my ($tagname, $attr) = @_;
 1574:         push @state, $tagname;
 1575:         my $depth = 0;
 1576:         my @seq = ();
 1577:         my $class;
 1578:         my $state_str = join(" ",@state);
 1579:         if ($container eq "pool") {
 1580:             if ("@state" eq "POOL TITLE") {
 1581:                 $$settings{title} = $attr->{value};
 1582:             }
 1583:         } else {
 1584:             if ("@state" eq "ASSESSMENT TITLE") {  
 1585:                 $$settings{title} = $attr->{value};          
 1586:             } elsif ("@state" eq "ASSESSMENT FLAG" ) {
 1587:                 $$settings{isnewline} = $attr->{value};
 1588:             } elsif ("@state" eq "ASSESSMENT FLAGS ISAVAILABLE") {
 1589:                 $$settings{isavailable} = $attr->{value};
 1590:             } elsif ("@state" eq "ASSESSMENT FLAGS ISANONYMOUS" ) {
 1591:                 $$settings{isanonymous} = $attr->{id};
 1592:             } elsif ("@state" eq "ASSESSMENT FLAGS GIVE FEEDBACK" ) {
 1593:                 $$settings{feedback} = $attr->{id};        
 1594:             } elsif ("@state" eq "ASSESSMENT FLAGS SHOWCORRECT" ) {
 1595:                 $$settings{showcorrect} = $attr->{id};        
 1596:             } elsif ("@state" eq "ASSESSMENT FLAGS SHOWRESULTS" ) {
 1597:                 $$settings{showresults} = $attr->{id};        
 1598:             } elsif ("@state" eq "ASSESSMENT FLAGS ALLOWMULTIPLE" ) {
 1599:                 $$settings{allowmultiple} = $attr->{id};        
 1600:             } elsif ("@state" eq "ASSESSMENT ASSESSMENTTYPE" ) {
 1601:                 $$settings{type} = $attr->{id};        
 1602:             }
 1603:         }    
 1604:         if ("@state" eq "$toptag{$container} QUESTIONLIST QUESTION") {  
 1605:             $id = $attr->{id};
 1606:             push @allids, $id;
 1607:             %{$$settings{$id}} = ();
 1608:             @{$allanswers{$id}} = ();
 1609:             $$settings{$id}{class} = $attr->{class};
 1610:             unless ($container eq "pool") {
 1611:                 $$settings{$id}{points} = $attr->{points};
 1612:             }
 1613:             @{$$settings{$id}{correctanswer}} = ();                              
 1614:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[-1] =~ m/^QUESTION_(\w+)$/) ) {
 1615:             $id = $attr->{id};
 1616:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "BODY") && ($state[3] eq "FLAGS") ) {
 1617:             if ($state[4] eq "ISHTML") {
 1618:                 $$settings{$id}{html} = $attr->{value};
 1619:             } elsif ($state[4] eq "ISNEWLINELITERAL") {
 1620:                 $$settings{$id}{newline} = $attr->{value};
 1621:             }
 1622:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "IMAGE") ) {
 1623:             $$settings{$id}{image} = $attr->{value};
 1624:             $$settings{$id}{style} = $attr->{style};
 1625:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "URL") ) {
 1626:             $$settings{$id}{url} = $attr->{value};
 1627:             $$settings{$id}{name} = $attr->{name};
 1628:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[-1] eq "ANSWER") ) {
 1629:             $answer_id = $attr->{id};
 1630:             push @{$allanswers{$id}},$answer_id;
 1631:             %{$$settings{$id}{$answer_id}} = ();
 1632:             $$settings{$id}{$answer_id}{position} = $attr->{position};
 1633:             if ($$settings{$id}{class} eq 'QUESTION_MATCH') {
 1634:                 $$settings{$id}{$answer_id}{placement} = $attr->{placement};
 1635:                 $$settings{$id}{$answer_id}{type} = 'answer';
 1636:             }
 1637:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[-1] eq "CHOICE") ) {
 1638:             $answer_id = $attr->{id};
 1639:             push @{$allchoices{$id}},$answer_id; 
 1640:             %{$$settings{$id}{$answer_id}} = ();
 1641:             $$settings{$id}{$answer_id}{position} = $attr->{position};
 1642:             $$settings{$id}{$answer_id}{placement} = $attr->{placement};
 1643:             $$settings{$id}{$answer_id}{type} = 'choice';
 1644:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "ANSWER") ) {
 1645:             if ($state[3] eq "IMAGE") {
 1646:                 $$settings{$id}{$answer_id}{image} = $attr->{value};
 1647:                 $$settings{$id}{$answer_id}{style} = $attr->{style};
 1648:             } elsif ($state[3] eq "URL") {
 1649:                 $$settings{$id}{$answer_id}{url} = $attr->{value};
 1650:             }
 1651:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "CHOICE") ) {
 1652:             if ($state[3] eq "IMAGE") {
 1653:                 $$settings{$id}{$answer_id}{image} = $attr->{value};
 1654:                 $$settings{$id}{$answer_id}{style} = $attr->{style};
 1655:             } elsif ($state[3] eq "URL") {
 1656:                 $$settings{$id}{$answer_id}{url} = $attr->{value};
 1657:             }
 1658:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[3] eq "CORRECTANSWER") ) {
 1659:             my $corr_answer = $attr->{answer_id};
 1660:             push @{$$settings{$id}{correctanswer}}, $corr_answer;
 1661:             my $type = $1;
 1662:             if ($type eq 'TRUEFALSE') {
 1663:                 $$settings{$id}{$corr_answer}{answer_position} = $attr->{position};
 1664:             } elsif ($type eq 'ORDER') {
 1665:                 $$settings{$id}{$corr_answer}{order} = $attr->{order};
 1666:             } elsif ($type eq 'MATCH') {
 1667:                 $$settings{$id}{$corr_answer}{choice_id} = $attr->{choice_id};
 1668:             }
 1669:         }
 1670:      }, "tagname, attr"],
 1671:      text_h =>
 1672:      [sub {
 1673:         my ($text) = @_;
 1674:         unless ($container eq "pool") {        
 1675:             if ("@state" eq "ASSESSMENT DESCRIPTION TEXT") {
 1676:                 $$settings{description} = $text;
 1677:             } elsif ("@state" eq "ASSESSMENT INSTRUCTIONS ") {
 1678:                 $$settings{instructions}{text} = $text;
 1679:             }
 1680:         }
 1681:         if ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "BODY") && ($state[3] eq "TEXT") ) {
 1682:             $$settings{$id}{text} = $text;
 1683:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "ANSWER") && ($state[3] eq "TEXT") ) {
 1684:             $$settings{$id}{$answer_id}{text} = $text;
 1685:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "CHOICE") && ($state[3] eq "TEXT") ) {
 1686:             $$settings{$id}{$answer_id}{text} = $text;            
 1687:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[3] eq "FEEDBACK_WHEN_CORRECT") ) {
 1688:             $$settings{$id}{feedback_corr} = $text;
 1689:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[3] eq "FEEDBACK_WHEN_INCORRECT") ) {
 1690:             $$settings{$id}{feedback_incorr} = $text;       
 1691:         }
 1692:       }, "dtext"],
 1693:      end_h =>
 1694:      [sub {
 1695:         my ($tagname) = @_;
 1696:         pop @state;
 1697:      }, "tagname"],
 1698:     );
 1699:     $p->unbroken_text(1);
 1700:     $p->parse_file($xmlfile);
 1701:     $p->eof;
 1702: 
 1703:     my $dirtitle = $$settings{'title'};
 1704:     $dirtitle =~ s/\W//g;
 1705:     $dirtitle .= '_'.$res;
 1706:     if (!-e "$destdir/problems") {
 1707:         mkdir("$destdir/problems",0755);
 1708:     }
 1709:     if (!-e "$destdir/problems/$dirtitle") {
 1710:         mkdir("$destdir/problems/$dirtitle",0755);
 1711:     }
 1712:     my $newdir = "$destdir/problems/$dirtitle";
 1713:     my $seqdir = "$destdir/sequences";
 1714:     my $pagedir = "$destdir/pages";
 1715:     my $curr_id = 0;
 1716:     my $next_id = 1;
 1717:     my $fh;
 1718:     my $containerdir;
 1719:     if ($container eq 'pool') {
 1720:         $containerdir = $seqdir.'/'.$res.'.sequence';
 1721:         if (!-e "$seqdir") {
 1722:             mkdir("$seqdir",0770);
 1723:         }
 1724:         open($fh,">$containerdir");
 1725:         $$total{seq} ++;
 1726:         push @{$sequencesfiles},$res.'.sequence';
 1727:     } else {
 1728:         $containerdir = $pagedir.'/'.$res.'.page';
 1729:         if (!-e "$destdir/pages") {
 1730:             mkdir("$destdir/pages",0770);
 1731:         }
 1732:         open($fh,">$containerdir");
 1733:         $$total{page} ++;
 1734:         push @{$pagesfiles},$res.'.page';
 1735:     }
 1736:     print $fh qq|<map>
 1737: |;
 1738:     my $probsrc = "/res/lib/templates/simpleproblem.problem";
 1739:     my ($cid,$cdom,$cnum);
 1740:     if ($context eq 'DOCS') {
 1741:         $cid = $ENV{'request.course.id'};
 1742:         ($cdom,$cnum) = split/_/,$cid;
 1743:     }
 1744:     if ($context eq 'CSTR') {
 1745:         $probsrc="/res/$udom/$uname/$dirname/problems/$dirtitle/$allids[0].problem";
 1746:     }
 1747:     print $fh qq|<resource id="1" src="$probsrc" type="start" title="question_0001"></resource>|;
 1748:     if (@allids == 1) {
 1749:         print $fh qq|
 1750: <link from="1" to="2" index="1"></link>
 1751: <resource id="2" src="" type="finish">\n|;
 1752:     } else {
 1753:         for (my $j=1; $j<@allids; $j++) {
 1754:             my $qntitle = $j;
 1755:             while (length($qntitle) <4) {
 1756:                 $qntitle = '0'.$qntitle;
 1757:             }
 1758:             $curr_id = $j;
 1759:             $next_id = $curr_id + 1;
 1760:             if ($context eq 'CSTR') {
 1761:                 $probsrc = "/res/$udom/$uname/$dirname/problems/$dirtitle/$allids[$j].problem";
 1762:             }
 1763:             print $fh qq|
 1764: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
 1765: <resource id="$next_id" src="$probsrc" title="question_$qntitle"|;
 1766:             if ($next_id == @allids) {
 1767:                 print $fh qq| type="finish"></resource>\n|;
 1768:             } else {
 1769:                 print $fh qq|></resource>|;
 1770:             }
 1771:         }
 1772:     }
 1773:     print $fh qq|</map>|;
 1774:     close($fh);
 1775:     my $qnum = 0;
 1776:     foreach my $id (@allids) {
 1777:         $qnum ++;
 1778:         my $output;
 1779:         my $permcontainer = $containerdir;
 1780:         $permcontainer =~ s#/home/httpd/html/userfiles#uploaded#;
 1781:         my $symb = $cid.'.'.$permcontainer.'___'.$qnum.'___lib/templates/simpleproblem.problem.0.';
 1782:         my %resourcedata = ();
 1783:         for (my $i=0; $i<10; $i++) {
 1784:             my $iter = $i+1;
 1785:             $resourcedata{$symb.'text'.$iter} = "";
 1786:             $resourcedata{$symb.'value'.$iter} = "unused";
 1787:             $resourcedata{$symb.'position'.$iter} = "random";
 1788:         }  
 1789:         $resourcedata{$symb.'randomize'} = 'yes';
 1790:         $resourcedata{$symb.'maxfoils'} = 10;
 1791:         if ($context eq 'CSTR') {
 1792:             $output = qq|<problem>
 1793: |;
 1794:         }
 1795:         $$total{prob} ++;
 1796:         if ($$settings{$id}{class} eq "QUESTION_ESSAY") {
 1797:             if ($context eq 'CSTR') {
 1798:                 $output .= qq|<startouttext />$$settings{$id}{text}<endouttext />
 1799:  <essayresponse>
 1800:  <textfield></textfield>
 1801:  </essayresponse>
 1802:  <postanswerdate>
 1803:  $$settings{$id}{feedbackcorr}
 1804:  </postanswerdate>
 1805: |;
 1806:              } else {
 1807: 		 $resourcedata{$symb.'questiontext'} = $$settings{$id}{text};
 1808:                  $resourcedata{$symb.'hiddenparts'} = '!essay';
 1809:                  $resourcedata{$symb.'questiontype'} = 'essay';
 1810:              }
 1811:         } else {
 1812:             if ($context eq 'CSTR') {
 1813:                 $output .= qq|<startouttext />$$settings{$id}{text}\n|;
 1814:             } else {
 1815:                 $resourcedata{$symb.'questiontext'} = $$settings{$id}{text};
 1816:             }
 1817:             my ($image,$imglink,$url);
 1818:             if ( defined($$settings{$id}{image}) ) {
 1819:                 if ( $$settings{$id}{style} eq 'embed' ) {
 1820:                     $image = qq|<br /><img src="$dirname/resfiles/$res/$$settings{$id}{image}" /><br />|;
 1821:                 } else {
 1822:                     $imglink = qq|<br /><a href="$dirname/resfiles/$res/$$settings{$id}{image}">Link to file</a><br />|;
 1823:                 }
 1824:             }
 1825:             if ( defined($$settings{$id}{url}) ) {
 1826:                 $url = qq|<br /><a href="$$settings{$id}{url}">$$settings{$id}{name}</a><br />|;
 1827:             }
 1828:             if ($context eq 'CSTR') {
 1829:                 $output .= $image.$imglink.$url.'
 1830: <endouttext />';
 1831:             } else {
 1832:                 $resourcedata{$symb.'questiontext'} .= $image.$imglink.$url;
 1833:             }
 1834:             if ($$settings{$id}{class} eq 'QUESTION_MULTIPLECHOICE') {
 1835:                 my $numfoils = @{$allanswers{$id}};
 1836:                 if ($context eq 'CSTR') {
 1837:                     $output .= qq|
 1838:  <radiobuttonresponse max="$numfoils" randomize="yes">
 1839:   <foilgroup>
 1840: |;
 1841:                 } else {
 1842:                     $resourcedata{$symb.'hiddenparts'} = '!radio';
 1843:                     $resourcedata{$symb.'questiontype'} = 'radio';
 1844:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 1845:                 }
 1846:                 for (my $k=0; $k<@{$allanswers{$id}}; $k++) {
 1847:                     my $iter = $k+1;
 1848:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 1849:                     if (grep/^$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 1850:                         $output .= "true\" location=\"";
 1851:                         $resourcedata{$symb.'value'.$iter} = "true";
 1852:                     } else {
 1853:                         $output .= "false\" location=\"";
 1854:                         $resourcedata{$symb.'value'.$iter} = "false";
 1855:                     }
 1856:                     if (lc ($allanswers{$id}[$k]) =~ m/^\s?([Aa]ll)|([Nn]one)\sof\sthe\sabove\.?/) {
 1857:                         $output .= "bottom\"";
 1858:                         $resourcedata{$symb.'position'.$iter} = "bottom";
 1859:                     } else {
 1860:                         $output .= "random\"";
 1861:                     }
 1862:                     $output .= "\><startouttext />".$$settings{$id}{$allanswers{$id}[$k]}{text};
 1863:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$allanswers{$id}[$k]}{text};
 1864:                     my ($ans_image,$ans_link);
 1865:                     if ( defined($$settings{$id}{$allanswers{$id}[$k]}{image}) ) {
 1866:                         if ( $$settings{$id}{$allanswers{$id}[$k]}{style} eq 'embed' ) {
 1867:                             $ans_image .= qq|<br /><img src="$dirname/resfiles/$res/$$settings{$id}{$allanswers{$id}[$k]}{image}" /><br />|;
 1868:                         } else {
 1869:                             $ans_link .= qq|<br /><a href="$dirname/resfiles/$res/$$settings{$id}{$allanswers{$id}[$k]}{image}" />Link to file</a><br/>|;
 1870:                         }
 1871:                     }
 1872:                     $output .= $ans_image.$ans_link.'<endouttext /></foil>'."\n";
 1873:                     $resourcedata{$symb.'text'.$iter} .= $ans_image.$ans_link;
 1874:                 }
 1875:                 if ($context eq 'CSTR') {
 1876:                     chomp($output);
 1877:                     $output .= qq|
 1878:   </foilgroup>
 1879:  </radiobuttonresponse>
 1880: |;
 1881:                 }
 1882:             } elsif ($$settings{$id}{class} eq 'QUESTION_TRUEFALSE') {
 1883:                 my $numfoils = @{$allanswers{$id}};
 1884:                 if ($context eq 'CSTR') {
 1885:                     $output .= qq|
 1886:    <radiobuttonresponse max="$numfoils" randomize="yes">
 1887:     <foilgroup>
 1888: |;
 1889:                 } else {
 1890:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 1891:                     $resourcedata{$symb.'hiddenparts'} = '!radio';
 1892:                     $resourcedata{$symb.'questiontype'} = 'radio';
 1893:                 }
 1894:                 for (my $k=0; $k<@{$allanswers{$id}}; $k++) {
 1895:                     my $iter = $k+1;
 1896:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 1897:                     if (grep/^$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 1898:                         $output .= "true\" location=\"random\"";
 1899:                         $resourcedata{$symb.'value'.$iter} = "true";
 1900:                     } else {
 1901:                         $output .= "false\" location=\"random\"";
 1902:                         $resourcedata{$symb.'value'.$iter} = "false";
 1903:                     }
 1904:                     $output .= "\><startouttext />".$$settings{$id}{$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 1905:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$allanswers{$id}[$k]}{text};
 1906:                 }
 1907:                 if ($context eq 'CSTR') {
 1908:                     chomp($output);
 1909:                     $output .= qq|
 1910:     </foilgroup>
 1911:    </radiobuttonresponse>
 1912: |;
 1913:                 }
 1914:             } elsif ($$settings{$id}{class} eq 'QUESTION_MULTIPLEANSWER') {
 1915:                 my $numfoils = @{$allanswers{$id}};
 1916:                 if ($context eq 'CSTR') {
 1917:                     $output .= qq|
 1918:    <optionresponse max="$numfoils" randomize="yes">
 1919:     <foilgroup options="('True','False')">
 1920: |;
 1921:                 } else {
 1922:                     $resourcedata{$symb.'newopt'} = '';
 1923:                     $resourcedata{$symb.'delopt'} = '';
 1924:                     $resourcedata{$symb.'options'} = "('True','False')";
 1925:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 1926:                     $resourcedata{$symb.'questiontype'} = 'option';
 1927:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 1928:                 }
 1929:                 for (my $k=0; $k<@{$allanswers{$id}}; $k++) {
 1930:                     my $iter = $k+1;
 1931:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 1932:                     if (grep/^$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 1933:                         $output .= "True\"";
 1934:                         $resourcedata{$symb.'value'.$iter} = "True";
 1935:                     } else {
 1936:                         $output .= "False\"";
 1937:                         $resourcedata{$symb.'value'.$iter} = "False";
 1938:                     }
 1939:                     $output .= "\><startouttext />".$$settings{$id}{$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 1940:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$allanswers{$id}[$k]}{text};
 1941:                 }
 1942:                 if ($context eq 'CSTR') {  
 1943:                     chomp($output);
 1944:                     $output .= qq|
 1945:     </foilgroup>
 1946:    </optionresponse>
 1947: |;
 1948:                 }
 1949:             } elsif ($$settings{$id}{class} eq 'QUESTION_ORDER') {
 1950:                 my $numfoils = @{$allanswers{$id}};
 1951:                 my @allorder = ();
 1952:                 if ($context eq 'CSTR') {
 1953:                     $output .= qq|
 1954:    <rankresponse max="$numfoils" randomize="yes">
 1955:     <foilgroup>
 1956: |;
 1957:                 } else {
 1958:                     $resourcedata{$symb.'newopt'} = '';
 1959:                     $resourcedata{$symb.'delopt'} = '';
 1960:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 1961:                     $resourcedata{$symb.'questiontype'} = 'option';
 1962:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 1963:                 }
 1964:                 for (my $k=0; $k<@{$allanswers{$id}}; $k++) {
 1965:                     if ($context eq 'CSTR') {
 1966:                         $output .= "   <foil location=\"random\" name=\"foil".$k."\" value=\"".$$settings{$id}{$allanswers{$id}[$k]}{order}."\"><startouttext />".$$settings{$id}{$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 1967:                     } else {
 1968:                         my $iter = $k+1;
 1969:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$allanswers{$id}[$k]}{text};
 1970:                         if (!grep/^$$settings{$id}{$allanswers{$id}[$k]}{order}$/,@allorder) {
 1971:                             push @allorder, $$settings{$id}{$allanswers{$id}[$k]}{order};
 1972:                         }
 1973:                     }
 1974:                 }
 1975:                 if ($context eq 'CSTR') {
 1976:                     chomp($output);
 1977:                     $output .= qq|
 1978:     </foilgroup>
 1979:    </rankresponse>
 1980: |;
 1981:                 } else {
 1982:                     @allorder = sort {$a <=> $b} @allorder;
 1983:                     $resourcedata{$symb.'options'} = "('".join("','",@allorder)."')";
 1984:                 }
 1985:             } elsif ($$settings{$id}{class} eq 'QUESTION_FILLINBLANK') {
 1986:                 my $numerical = 1;
 1987:                 if ($context eq 'DOCS') {
 1988:                     $numerical = 0;
 1989:                 } else {
 1990:                     for (my $k=0; $k<@{$allanswers{$id}}; $k++) {
 1991:                         if ($$settings{$id}{$allanswers{$id}[$k]}{text} =~ m/([^\d\.]|\.\.)/) {
 1992:                             $numerical = 0;
 1993:                         }
 1994:                     }
 1995:                 }
 1996:                 if ($numerical) {
 1997:                     my $numans;
 1998:                     my $tol;
 1999:                     if (@{$allanswers{$id}} == 1) {
 2000:                         $tol = 5;
 2001:                         $numans = $$settings{$id}{$allanswers{$id}[0]}{text};
 2002:                     } else {
 2003:                         my $min = $$settings{$id}{$allanswers{$id}[0]}{text};
 2004:                         my $max = $$settings{$id}{$allanswers{$id}[0]}{text};
 2005:                         for (my $k=1; $k<@{$allanswers{$id}}; $k++) {
 2006:                             if ($$settings{$id}{$allanswers{$id}[$k]}{text} <= $min) {
 2007:                                 $min = $$settings{$id}{$allanswers{$id}[$k]}{text};
 2008:                             }
 2009:                             if ($$settings{$id}{$allanswers{$id}[$k]}{text} >= $max) {
 2010:                                 $max = $$settings{$id}{$allanswers{$id}[$k]}{text};
 2011:                             }
 2012:                         }
 2013:                         $numans = ($max + $min)/2;
 2014:                         $tol = 100*($max - $min)/($numans*2);
 2015:                     }
 2016:                     if ($context eq 'CSTR') {
 2017:                         $output .= qq|
 2018: <numericalresponse answer="$numans">
 2019:         <responseparam type="tolerance" default="$tol%" name="tol" description="Numerical Tolerance" />
 2020:         <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures"
 2021: />
 2022:         <textline />
 2023: </numericalresponse>
 2024: |;
 2025:                     }
 2026:                 } else {
 2027:                     if ($context eq 'DOCS') {
 2028:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 2029:                         $resourcedata{$symb.'questiontype'} = 'string';
 2030:                         $resourcedata{$symb.'maxfoils'} = @{$allanswers{$id}};
 2031:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 2032:                         $resourcedata{$symb.'stringtype'} = 'ci';
 2033:                         $resourcedata{$symb.'stringanswer'} = $$settings{$id}{$allanswers{$id}[0]}{text};
 2034:                     } else {
 2035:                         if (@{$allanswers{$id}} == 1) {
 2036:                             $output .= qq|
 2037: <stringresponse answer="$$settings{$id}{$allanswers{$id}[0]}{text}" type="ci">
 2038: <textline>
 2039: </textline>
 2040: </stringresponse>
 2041: |;
 2042:                         } else {
 2043:                             my @answertext = ();
 2044:                             for (my $k=0; $k<@{$allanswers{$id}}; $k++) {
 2045:                                 $$settings{$id}{$allanswers{$id}[$k]}{text} =~ s/\|/\|/g;
 2046:                                 push @answertext, $$settings{$id}{$allanswers{$id}[$k]}{text};
 2047:                             }
 2048:                             my $regexpans = join('|',@answertext);
 2049:                             $regexpans = '/^('.$regexpans.')\b/';
 2050:                             $output .= qq|
 2051: <stringresponse answer="$regexpans" type="re">
 2052: <textline>
 2053: </textline>
 2054: </stringresponse>
 2055: |;
 2056:                         }
 2057:                     } 
 2058:                 }
 2059:             } elsif ($$settings{$id}{class} eq "QUESTION_MATCH") {
 2060:                 my @allmatchers = ();
 2061:                 my %matchtext = ();
 2062:                 if ($context eq 'CSTR') {
 2063:                     $output .= qq|
 2064: <matchresponse max="10" randomize="yes">
 2065:     <foilgroup>
 2066:         <itemgroup>
 2067: |;
 2068:                 } else {
 2069:                     $resourcedata{$symb.'newopt'} = '';
 2070:                     $resourcedata{$symb.'delopt'} = '';
 2071:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 2072:                     $resourcedata{$symb.'questiontype'} = 'option';
 2073:                     $resourcedata{$symb.'maxfoils'} =  @{$allanswers{$id}};
 2074:                 }
 2075:                 for (my $k=0; $k<@{$allchoices{$id}}; $k++) {
 2076:                     if ($context eq 'CSTR') {
 2077:                         $output .= qq|
 2078: <item name="$allchoices{$id}[$k]">
 2079: <startouttext />$$settings{$id}{$allchoices{$id}[$k]}{text}<endouttext />
 2080: </item>
 2081:                     |;
 2082:                     } else {
 2083:                         if (!grep/^$$settings{$id}{$allchoices{$id}[$k]}{text}$/,@allmatchers) {
 2084:                             push @allmatchers, $$settings{$id}{$allchoices{$id}[$k]}{text};
 2085:                             $matchtext{$allchoices{$id}[$k]} = $$settings{$id}{$allchoices{$id}[$k]}{text};
 2086:                         }
 2087:                     }
 2088:                 }
 2089:                 if ($context eq 'CSTR') {
 2090:                     $output .= qq|
 2091:         </itemgroup>
 2092: |;
 2093:                 }
 2094:                 for (my $k=0; $k<@{$allanswers{$id}}; $k++) {
 2095:                     if ($context eq 'CSTR') {
 2096:                         $output .= qq|
 2097:         <foil location="random" value="$$settings{$id}{$allanswers{$id}[$k]}{choice_id}" name="$allanswers{$id}[$k]">
 2098:          <startouttext />$$settings{$id}{$allanswers{$id}[$k]}{text}<endouttext />
 2099:         </foil>
 2100: |;
 2101:                     } else {
 2102:                         my $iter = $k+1;
 2103:                         $resourcedata{$symb.'value'.$iter} = $matchtext{$$settings{$id}{$allanswers{$id}[$k]}{choice_id}};
 2104:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$allanswers{$id}[$k]}{text};
 2105:                     }
 2106:                 }
 2107:                 if ($context eq 'CSTR') {
 2108:                     $output .= qq|
 2109:     </foilgroup>
 2110: </matchresponse>
 2111: |;
 2112:                 } else {
 2113:                     $resourcedata{$symb.'options'} = "('".join("','",@allmatchers)."')";
 2114:                 }
 2115:             }
 2116:         }
 2117:         if ($context eq 'CSTR') {
 2118:             $output .= qq|</problem>
 2119: |;
 2120:             open(PROB,">$newdir/$id.problem");
 2121:             print PROB $output;
 2122:             close PROB;
 2123:         } else {
 2124: # put %resourcedata;
 2125:             my $reply=&Apache::lonnet::cput
 2126:                 ('resourcedata',\%resourcedata,$cdom,$cnum);
 2127:         }
 2128:     }
 2129: }
 2130: 
 2131: # ---------------------------------------------------------------- Process Blackboard Announcements
 2132: sub process_announce {
 2133:     my ($res,$docroot,$destdir,$settings,$globalresref,$seqstem,$resrcfiles) = @_;
 2134:     my $xmlfile = $docroot.'/'.$res.".dat";
 2135:     my @state = ();
 2136:     my @assess = ();
 2137:     my $id;
 2138:     my $p = HTML::Parser->new
 2139:     (
 2140:      xml_mode => 1,
 2141:      start_h =>
 2142:      [sub {
 2143:         my ($tagname, $attr) = @_;
 2144:         push @state, $tagname;
 2145:         if ("@state" eq "ANNOUNCEMENT TITLE") {
 2146:             $$settings{title} = $attr->{value};
 2147:             $$settings{startassessment} = ();
 2148:         } elsif ("@state" eq "ANNOUNCEMENT DESCRIPTION FLAGS ISHTML") {  
 2149:             $$settings{ishtml} = $attr->{value};          
 2150:         } elsif ("@state" eq "ANNOUNCEMENT DESCRIPTION FLAGS ISNEWLINELITERAL" ) {
 2151:             $$settings{isnewline} = $attr->{value};
 2152:         } elsif ("@state" eq "ANNOUNCEMENT ISPERMANENT" ) {
 2153:             $$settings{ispermanent} = $attr->{value};
 2154:         } elsif ("@state" eq "ANNOUNCEMENT DATES UPDATED") {
 2155:             $$settings{dates} = $attr->{value}; 
 2156:         } elsif ("@state" eq "ANNOUNCEMENT FILES STARTASSESSMENT" ) {
 2157:             $id = $attr->{id};
 2158:             %{$$settings{startassessment}{$id}} = ();
 2159:             push @assess,$id;
 2160:         } elsif ("@state" eq "ANNOUNCEMENT FILES STARTASSESSMENT ATTRIB" ) {
 2161:             my $key = $attr->{key};
 2162:             $$settings{startassessment}{$id}{$key} = $attr->{value};
 2163:         }
 2164:      }, "tagname, attr"],
 2165:      text_h =>
 2166:      [sub {
 2167:         my ($text) = @_;
 2168:         if ("@state" eq "ANNOUNCEMENT DESCRIPTION TEXT") {
 2169:             $$settings{text} = $text;
 2170:         }
 2171:       }, "dtext"],
 2172:      end_h =>
 2173:      [sub {
 2174:         my ($tagname) = @_;
 2175:         pop @state;
 2176:      }, "tagname"],
 2177:     );
 2178:     $p->unbroken_text(1);
 2179:     $p->parse_file($xmlfile);
 2180:     $p->eof;
 2181: 
 2182:     if (defined($$settings{text})) {
 2183:         if ($$settings{ishtml} eq "false") {
 2184:             if ($$settings{isnewline} eq "true") {
 2185:                 $$settings{text} =~ s#\n#<br/>#g;
 2186:             }
 2187:         } else {
 2188:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 2189:         }
 2190:     }
 2191:   
 2192:     if (@assess > 0) {
 2193:         foreach my $id (@assess) {
 2194:             $$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.";
 2195:         }
 2196:     }
 2197: 
 2198:     open(FILE,">$destdir/resfiles/$res.html");
 2199:     push @{$resrcfiles}, "$res.html";
 2200:     print FILE qq|<html>
 2201: <head>
 2202: <title>$$settings{title}</title>
 2203: </head>
 2204: <body bgcolor='#ffffff'>
 2205: <table>
 2206:  <tr>
 2207:   <td bgcolor='#CCCCFF'>$$settings{title} - announcement date: $$settings{dates}</td>
 2208:  </tr>
 2209: </table>
 2210: <br/>
 2211: $$settings{text}
 2212: |;
 2213:     print FILE qq|
 2214:   </body>
 2215:  </html>|;
 2216:     close(FILE);
 2217: }
 2218: 
 2219: # ---------------------------------------------------------------- Process Blackboard Content
 2220: sub process_content {
 2221:     my ($cms,$res,$context,$docroot,$destdir,$settings,$dom,$user,$resrcfiles,$packages,$hrefs) = @_;
 2222:     my $xmlfile = $docroot.'/'.$res.".dat";
 2223:     my $destresdir = $destdir;
 2224:     if ($context eq 'CSTR') {
 2225: #        $destresdir =~ s|/home/$user/public_html/|/res/$dom/$user/|;
 2226:         $destresdir =~ s|/home/$user/public_html/|/priv/$user/|;
 2227:     } elsif ($context eq 'DOCS') {
 2228:         $destresdir =~ s|^/home/httpd/html/userfiles|/uploaded|;
 2229:     }
 2230:     my $filetag = '';
 2231:     if ($cms eq 'bb5') {
 2232:         $filetag = 'FILEREF';
 2233:     } elsif ($cms eq 'bb6') {
 2234:         $filetag = 'FILE';
 2235:     }
 2236:     my $filecount = 0;
 2237:     my @allrelfiles = ();
 2238:     my @state;
 2239:     @{$$settings{files}} = (); 
 2240:     my $p = HTML::Parser->new
 2241:     (
 2242:       xml_mode => 1,
 2243:       start_h =>
 2244:       [sub {
 2245:         my ($tagname, $attr) = @_;
 2246:         push @state, $tagname;
 2247:         if ("@state" eq "CONTENT ") {
 2248:             %{$$settings{maindata}} = ();
 2249:         } elsif ("@state" eq "CONTENT TITLECOLOR") {
 2250:             $$settings{titlecolor} =  $attr->{value};
 2251:         } elsif ("@state" eq "CONTENT MAINDATA TEXTCOLOR") {
 2252:             $$settings{maindata}{color} = $attr->{value};
 2253:         } elsif ("@state" eq "CONTENT MAINDATA FLAGS ISHTML") {  
 2254:             $$settings{maindata}{ishtml} = $attr->{value}; 
 2255:         } elsif ("@state" eq "CONTENT MAINDATA FLAGS ISNEWLINELITERAL") {  
 2256:             $$settings{maindata}{isnewline} = $attr->{value};
 2257:         } elsif ("@state" eq "CONTENT BODY TYPE") {
 2258:             $$settings{maindata}{bodytype} =  $attr->{value};
 2259:         } elsif ("@state" eq "CONTENT FLAGS ISAVAILABLE" ) {
 2260:             $$settings{isavailable} = $attr->{value};
 2261:         } elsif ("@state" eq "CONTENT FLAGS ISFOLDER" ) {
 2262:             $$settings{isfolder} = $attr->{value};
 2263:         } elsif ("@state" eq "CONTENT FLAGS LAUNCHINNEWWINDOW" ) {
 2264:             $$settings{newwindow} = $attr->{value};
 2265:         } elsif ("@state" eq "CONTENT FILES $filetag") {
 2266:             %{$$settings{files}[$filecount]} = ();
 2267:             %{$$settings{files}[$filecount]{registry}} = (); 
 2268:         } elsif ("@state" eq "CONTENT FILES FILEREF RELFILE" ) {
 2269:             $$settings{files}[$filecount]{'relfile'} = $attr->{value};
 2270:             push @allrelfiles, $attr->{value};
 2271:         } elsif ("@state" eq "CONTENT FILES $filetag MIMETYPE") {
 2272:             $$settings{files}[$filecount]{mimetype} = $attr->{value};
 2273:         } elsif ("@state" eq "CONTENT FILES $filetag CONTENTTYPE") {
 2274:             $$settings{files}[$filecount]{contenttype} = $attr->{value};
 2275:         } elsif ("@state" eq "CONTENT FILES $filetag FILEACTION") {
 2276:             $$settings{files}[$filecount]{fileaction} = $attr->{value};
 2277:         } elsif ("@state" eq "CONTENT FILES $filetag PACKAGEPARENT") {
 2278:             $$settings{files}[$filecount]{packageparent} = $attr->{value};
 2279:         } elsif ("@state" eq "CONTENT FILES $filetag LINKNAME") {
 2280:             $$settings{files}[$filecount]{linkname} = $attr->{value};
 2281:         } elsif ("@state" eq "CONTENT FILES $filetag REGISTRY REGISTRYENTRY") {
 2282:             my $key = $attr->{key};
 2283:             $$settings{files}[$filecount]{registry}{$key} = $attr->{value};
 2284:         }
 2285:       }, "tagname, attr"],
 2286:       text_h =>
 2287:       [sub {
 2288:         my ($text) = @_;
 2289:         if ("@state" eq "CONTENT TITLE") {
 2290:             $$settings{title} = $text;
 2291:         } elsif ( ("@state" eq "CONTENT MAINDATA TEXT") || ("@state" eq "CONTENT BODY TEXT") ) {
 2292:             $$settings{maindata}{text} = $text;
 2293:         }  elsif ("@state" eq "CONTENT FILES $filetag REFTEXT") {
 2294:             $$settings{files}[$filecount]{reftext} = $text;
 2295:         } elsif ("@state" eq "CONTENT FILES FILE NAME" ) {
 2296:             $$settings{files}[$filecount]{'relfile'} = $text;
 2297:             push @allrelfiles, $text;
 2298:         }
 2299:        }, "dtext"],
 2300:       end_h =>
 2301:       [sub {
 2302:         my ($tagname) = @_;
 2303:         if ("@state" eq "CONTENT FILES $filetag") {
 2304:             $filecount ++;
 2305:         }
 2306:         pop @state;
 2307:       }, "tagname"],
 2308:      );
 2309:     $p->unbroken_text(1);
 2310:     $p->parse_file($xmlfile);
 2311:     $p->eof;
 2312:     my $linktag = '';
 2313:     my $fontcol = '';
 2314:     if (@{$$settings{files}} > 0) {
 2315:         for (my $filecount=0;  $filecount<@{$$settings{files}}; $filecount++) {
 2316:             if ($$settings{files}[$filecount]{'fileaction'} eq 'embed') {
 2317:                 if ( $$settings{files}[$filecount]{reftext} =~ m#<\!\-\-\s_(\d+)\\_\s\-\-\>#) { 
 2318:                     my $newtag = qq|<img src="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"/>|;
 2319:                     $$settings{maindata}{text} =~ s#<\!\-\-\s_/($1)\\_\s\-\-\>#$newtag#;
 2320:                 } elsif ( $$settings{files}[$filecount]{reftext} =~m#^_/(\d+)\\_$# ) {
 2321:                     my $reftag = $1;
 2322:                     my $newtag;
 2323:                     if ($$settings{files}[$filecount]{mimetype} =~ m/^image/) {
 2324:                         $newtag = qq|<img src="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"|;
 2325:                         if ( defined($$settings{files}[$filecount]{registry}{alttext}) ) {
 2326:                             $newtag .= qq| alt="$$settings{files}[$filecount]{registry}{alttext}"|;
 2327:                         }
 2328:                         if ( defined($$settings{files}[$filecount]{registry}{alignment}) )
 2329: {
 2330:                             $newtag .= qq| align="$$settings{files}[$filecount]{registry}{alignment}"|; 
 2331:                         }
 2332:                         if ( defined($$settings{files}[$filecount]{registry}{border}) ) {
 2333:                             $newtag .= qq| border="$$settings{files}[$filecount]{registry}{border}"|;
 2334:                         }
 2335:                         $newtag .= " />";
 2336:                         my $reftext =  $$settings{files}[$filecount]{reftext};
 2337:                         my $fname = $$settings{files}[$filecount]{'relfile'};
 2338:                         $$settings{maindata}{text} =~ s/<!\-\-\sCOMMENT\sBLOCK\sFOR\sEMBEDDED\sFILE:\s$fname[\s\n]+DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n]+//;
 2339: #                      $$settings{maindata}{text} =~ s/DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n]+//;
 2340:                         $$settings{maindata}{text} =~ s/Move\swhole\scomment\sto\schange\sfile\splacement\swithin\spage\.[\s\n]+//;
 2341:                         $$settings{maindata}{text} =~ s/_\/$reftag\\_/$newtag/;
 2342:                         $$settings{maindata}{text} =~ s/END\sOF\sBLOCK\sON\sNEXT\sLINE[\s\n]+//;
 2343:                         $$settings{maindata}{text} =~ s/\-\->//;
 2344: #                      $$settings{maindata}{text} =~ s/<!\-\-\sCOMMENT\sBLOCK\sFOR\sEMBEDDED\sFILE:\s$fname[\s\n]+DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n\]+_\/$reftag\\_[\s\n]+END\sOF\sBLOCK\sON\sNEXT\sLINE[\s\n\]+\-\->/$newtag/;
 2345: #                      print STDERR $$settings{maindata}{text};
 2346:                     }
 2347:                 } else {
 2348:                     my $filename=$$settings{files}[$filecount]{'relfile'};
 2349:                     my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 2350:                     $$settings{maindata}{text} =~ s#(src|SRC|value)=("|&quot;)$filename("|&quot;)#$1="$newfilename"#g;
 2351:                 }
 2352:             } elsif ($$settings{files}[$filecount]{fileaction} eq 'link') {
 2353:                 unless (($$settings{files}[$filecount]{packageparent} ne '') && (grep/^$$settings{files}[$filecount]{packageparent}$/,@{$$settings{files}}) ) {
 2354:                     $linktag .= qq|<a href="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"|;
 2355:                     if ($$settings{newwindow} eq "true") {
 2356:                         $linktag .= qq| target="$res$filecount"|;
 2357:                     }
 2358:                     foreach my $entry (keys %{$$settings{files}[$filecount]{registry}}) {
 2359:                         $linktag .= qq| $entry="$$settings{files}[$filecount]{registry}{$entry}"|;
 2360:                     }
 2361:                       $linktag .= qq|>$$settings{files}[$filecount]{linkname}</a><br/>\n|;
 2362:                 }
 2363:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'PACKAGE') || ($$settings{files}[$filecount]{fileaction} eq 'package') ) {
 2364:                my $open_package = '';
 2365:                if ($$settings{files}[$filecount]{'relfile'} =~ m|\.zip$|i) {
 2366:                    $open_package = &expand_zip("$docroot/$res",$$settings{files}[$filecount]{'relfile'});
 2367:                }
 2368:                if ($open_package eq 'ok') {
 2369:                    opendir(DIR,"$docroot/$res");
 2370:                    my @dircontents = grep(!/^\./,readdir(DIR));
 2371:                    closedir(DIR);
 2372:                    push @{$resrcfiles}, @dircontents;
 2373:                    @{$$hrefs{$res}} = @dircontents;
 2374:                    push @{$packages}, $res;
 2375:                }
 2376:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'BROKEN_IMAGE') && ($cms eq 'bb6') ) {
 2377:                 my $filename=$$settings{files}[$filecount]{'relfile'};
 2378:                 my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 2379:                 $$settings{maindata}{text} =~ s#(src|SRC|value)=("|&quot;)$filename("|&quot;)#$1="$newfilename"#g;
 2380:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'LINK') && ($cms eq 'bb6') ) {
 2381:                 my $filename=$$settings{files}[$filecount]{'relfile'};
 2382:                 my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 2383:                 my $filetitle = $$settings{files}[$filecount]{'linkname'};
 2384:                 $$settings{maindata}{text} = '<a href="'.$newfilename.'">'.$filetitle.'</a><br /><br />'. $$settings{maindata}{text};
 2385:             }
 2386:         }
 2387:     }
 2388:     if (defined($$settings{maindata}{textcolor})) {
 2389:         $fontcol =  qq|<font color="$$settings{maindata}{textcolor}">|;
 2390:     }
 2391:     if (defined($$settings{maindata}{text})) {
 2392:         if ($$settings{maindata}{bodytype} eq "S") {
 2393:             $$settings{maindata}{text} =~ s#\n#<br/>#g;
 2394:         }
 2395:         if ($$settings{maindata}{ishtml} eq "false") {
 2396:             if ($$settings{maindata}{isnewline} eq "true") {
 2397:                 $$settings{maindata}{text} =~ s#\n#<br/>#g;
 2398:             }
 2399:         } else {
 2400: #            $$settings{maindata}{text} = &HTML::Entities::decode($$settings{maindata}{text});
 2401:         }
 2402:     }
 2403: 
 2404:     if (!open(FILE,">$destdir/resfiles/$res.html")) {
 2405:         &Apache::lonnet::logthis("IMS import error: Cannot open file - $destdir/resfiles/$res.html - $!");
 2406:     } else {
 2407:         push @{$resrcfiles}, "$res.html";
 2408:         my $htmldoc = 0;
 2409: #        if ($$settings{maindata}{text} =~ m-&lt;(html|HTML)>.+&lt;\\(html|HTML)-) {
 2410:         if ($$settings{maindata}{text} =~ m-<(html|HTML)>-) {
 2411:             $htmldoc = 1;
 2412:         }
 2413:         unless ($htmldoc) {
 2414:             print FILE qq|<html>
 2415: <head>
 2416: <title>$$settings{title}</title>
 2417: </head>
 2418: <body bgcolor='#ffffff'>
 2419: $fontcol
 2420: |;
 2421:         }
 2422:         unless ($$settings{title} eq '') { 
 2423:             print FILE qq|$$settings{title}<br/><br/>\n|;
 2424:         }
 2425:         print FILE qq|
 2426: $$settings{maindata}{text}
 2427: $linktag|;
 2428:         unless ($htmldoc) {
 2429:             if (defined($$settings{maindata}{textcolor})) {
 2430:                 print FILE qq|</font>|;
 2431:             }
 2432:             print FILE qq|
 2433:   </body>
 2434:  </html>|;
 2435:         }
 2436:         close(FILE);
 2437:     }
 2438: }
 2439: 
 2440: 
 2441: sub process_angelboards {
 2442:     my ($context,$destdir,$boards,$timestamp,$crs,$cdom,$uname,$db_handling,$messages,$items,$resources,$hrefs,$tempdir,$longcrs) = @_;
 2443:     for (my $i=0; $i<@{$boards}; $i++) {
 2444:         my %msgidx = ();
 2445:         my $forumtext = '';
 2446:         my $boardname = 'bulletinpage_'.$$timestamp[$i];
 2447:         my $forumfile = $tempdir.'/_assoc/'.$$boards[$i].'/pg'.$$boards[$i].'.htm';
 2448:         my @state = ();
 2449:         my $p = HTML::Parser->new
 2450:         (
 2451:            xml_mode => 1,
 2452:            start_h =>
 2453:            [sub {
 2454:                 my ($tagname, $attr) = @_;
 2455:                 push @state, $tagname;
 2456:                 },  "tagname, attr"],
 2457:            text_h =>
 2458:            [sub {
 2459:                 my ($text) = @_;
 2460:                 if ("@state" eq "html body div div") {
 2461:                     $forumtext = $text;
 2462:                 }
 2463:               }, "dtext"],
 2464:             end_h =>
 2465:             [sub {
 2466:                   my ($tagname) = @_;
 2467:                   pop @state;
 2468:                }, "tagname"],
 2469:         );
 2470:         $p->parse_file($forumfile);
 2471:         $p->eof;
 2472: 
 2473:         my %boardinfo = (
 2474:                   'aaa_title' => $$items{$$resources{$$boards[$i]}{revitm}}{title},
 2475:                   'bbb_content' => $forumtext,
 2476:                   'ccc_webreferences' => '',
 2477:                   'uploaded.lastmodified' => time,
 2478:                   );
 2479:         my $msgcount = 0; 
 2480:                                                                                                      
 2481:         my $putresult = &Apache::lonnet::put($boardname,\%boardinfo,$cdom,$crs);
 2482:         if ($db_handling eq 'importall') {
 2483:             foreach my $msg_id (@{$$messages{$$boards[$i]}}) {
 2484:                 $msgcount ++;
 2485:                 $msgidx{$msg_id} = $msgcount;
 2486:                 my %contrib = (
 2487:                             'sendername' => 'NoName',
 2488:                             'senderdomain' => $cdom,
 2489:                             'screenname' => '',
 2490:                             'message' => $$items{$$resources{$msg_id}{revitm}}{title}
 2491:                             );
 2492:                 unless ( $$items{$$resources{$msg_id}{revitm}}{parentseq} eq $$resources{$$boards[$i]}{revitm} ) {
 2493:                     unless ( $msgidx{$$items{$$items{$$resources{$msg_id}{revitm}}{parentseq}}{resnum}} eq ''){
 2494:                         $contrib{replyto} = $msgidx{$$items{$$items{$$resources{$msg_id}{revitm}}{parentseq}}{resnum}};
 2495:                     }
 2496:                 }
 2497:                 if ( @{$$hrefs{$msg_id}} > 1 )  {
 2498:                     my $newurl = '';
 2499:                     foreach my $file (@{$$hrefs{$msg_id}}) {
 2500:                         unless ($file eq 'pg'.$msg_id.'.htm') {
 2501:                             $newurl = $msg_id.$file;
 2502:                              unless ($longcrs eq '') {
 2503:                                 if ($context eq 'CSTR') {
 2504:                                     if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles") {
 2505:                                         mkdir("/home/httpd/lonUsers/$cdom/$longcrs/userfiles",0755);
 2506:                                     }
 2507:                                     if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl") {
 2508:                                         rename("$destdir/resfiles/$msg_id/$file","/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl");
 2509:                                     }
 2510:                                 }
 2511:                                 $contrib{attachmenturl} = '/uploaded/'.$cdom.'/'.$crs.'/'.$file;
 2512:                             }
 2513:                         }
 2514:                     }
 2515:                 }
 2516:                 my $xmlfile = $tempdir.'/_assoc/'.$msg_id.'/'.$$resources{$msg_id}{file};
 2517:                 &angel_message($msg_id,\%contrib,$xmlfile);
 2518:                 unless ($$resources{$msg_id}{file} eq '') {
 2519:                     unlink($xmlfile);
 2520:                 }
 2521:                 my $symb = 'bulletin___'.$$timestamp[$i].'___adm/wrapper/adm/'.$cdom.'/'.$uname.'/'.$$timestamp[$i].'/bulletinboard';
 2522:                 my $postresult = &addposting($symb,\%contrib,$cdom,$crs);
 2523:             }
 2524:         }
 2525:     }
 2526: }
 2527: 
 2528: # ---------------------------------------------------------------- Process ANGEL message board messages
 2529: sub angel_message {
 2530:     my ($msg_id,$contrib,$xmlfile) = @_;
 2531:     my @state = ();
 2532:     my $p = HTML::Parser->new
 2533:     (
 2534:        xml_mode => 1,
 2535:        start_h =>
 2536:        [sub {
 2537:              my ($tagname, $attr) = @_;
 2538:              push @state, $tagname;
 2539:              },  "tagname, attr"],
 2540:         text_h =>
 2541:         [sub {
 2542:              my ($text) = @_;
 2543:              if ("@state" eq "html body table tr td div small span") {
 2544:                   $$contrib{'plainname'} = $text;
 2545:              } elsif ("@state" eq "html body div div") {
 2546:                   $$contrib{'message'} .= '<br /><br />'.$text;
 2547:              }
 2548:            }, "dtext"],
 2549:          end_h =>
 2550:          [sub {
 2551:                my ($tagname) = @_;
 2552:                pop @state;
 2553:             }, "tagname"],
 2554:     );
 2555:     $p->parse_file($xmlfile);
 2556:     $p->eof;
 2557: }
 2558: 
 2559: # ---------------------------------------------------------------- ANGEL content
 2560: sub angel_content {
 2561:     my ($res,$docroot,$destdir,$settings,$dom,$user,$type,$title,$resrcfiles) = @_;
 2562:     my $xmlfile = $docroot.'/_assoc/'.$res.'/pg'.$res.'.htm';
 2563:     my $filecount = 0;
 2564:     my $firstline;
 2565:     my $lastline;
 2566:     my @buffer = ();
 2567:     my @state;
 2568:     @{$$settings{links}} = ();
 2569:     my $p = HTML::Parser->new
 2570:     (
 2571:        xml_mode => 1,
 2572:        start_h =>
 2573:        [sub {
 2574:              my ($tagname, $attr) = @_;
 2575:              push @state, $tagname;
 2576:             },  "tagname, attr"],
 2577:        text_h =>
 2578:        [sub {
 2579:              my ($text) = @_;
 2580:              if ("@state" eq "html body table tr td div small span") {
 2581:                  $$settings{'subtitle'} = $text;
 2582:              } elsif ("@state" eq "html body div div") {
 2583:                  $$settings{'text'} = $text;
 2584:              } elsif ("@state" eq "html body div div a") {
 2585:                 push @{$$settings{'links'}}, $text;
 2586:              }
 2587:             }, "dtext"],
 2588:        end_h =>
 2589:        [sub {
 2590:              my ($tagname) = @_;
 2591:              pop @state;
 2592:             }, "tagname"],
 2593:     );
 2594:     $p->parse_file($xmlfile);
 2595:     $p->eof;
 2596:     if ($type eq "PAGE") {
 2597:         open(FILE,"<$xmlfile");
 2598:         @buffer = <FILE>;
 2599:         close(FILE);
 2600:         chomp(@buffer);
 2601:         $firstline = -1;
 2602:         $lastline = 0;
 2603:         for (my $i=0; $i<@buffer; $i++) {
 2604:             if (($firstline == -1) && ($buffer[$i] =~ m/<div\sclass="normalDiv"><div\sclass="normalSpan">/)) {
 2605:                 $firstline = $i;
 2606:                 $buffer[$i] = substr($buffer[$i],index($buffer[$i],'"normalSpan"')+13);
 2607:             }
 2608:             if (($firstline > -1) && ($buffer[$i] =~ m-<p></p></div></div>-)) {
 2609:                 $buffer[$i] = substr($buffer[$i],0,index($buffer[$i],'<p></p></div></div>'));
 2610:                 $lastline = $i;
 2611:             }
 2612:         }
 2613:     }
 2614:     open(FILE,">$destdir/resfiles/$res.html");
 2615:     push @{$resrcfiles}, "$res.html";
 2616:     print FILE qq|<html>
 2617: <head>
 2618: <title>$title</title>
 2619: </head>
 2620: <body bgcolor='#ffffff'>
 2621:     |;
 2622:     unless ($title eq '') {
 2623:         print FILE qq|<b>$title</b><br/>\n|;
 2624:     }
 2625:     unless ($$settings{subtitle} eq '') {
 2626:         print FILE qq|$$settings{subtitle}<br/>\n|;
 2627:     }
 2628:     print FILE "<br/>\n";
 2629:     if ($type eq "LINK") {
 2630:         foreach my $link (@{$$settings{links}}) {
 2631:             print FILE qq|<a href="$link">$link</a><br/>\n|; 
 2632:         }
 2633:     } elsif ($type eq "PAGE") {
 2634:         if ($firstline > -1) {
 2635:             for (my $i=$firstline; $i<=$lastline; $i++) {
 2636:                 print FILE "$buffer[$i]\n";
 2637:             }
 2638:         }
 2639:     }
 2640:     print FILE qq|
 2641:   </body>
 2642:  </html>|;
 2643:     close(FILE);
 2644: }
 2645: 
 2646: 1;
 2647: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>