File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.15: download - view: text, annotated - select for diffs
Mon Feb 21 23:47:46 2005 UTC (19 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Add option to import WebCT4 IMS package.  Work still required on import of questions and quizzes. Import of BB6 assessments also requires work (BB6 IMS packaging uses  QTI-type packaging for assessments unlike BB5).

    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:     %{$$cmsmap{webct4}} = (
   55:                 quiz => 'webctquiz',
   56:                 survey => 'webctsurvey',
   57:                 doc => 'webcontent'
   58:                 );
   59:     %{$areaname} = (
   60:                 announce => 'Announcements',
   61:                 board => 'Discussion Boards',
   62:                 doc => 'Documents, pages, and folders',
   63:                 extlink => 'Links to external sites',
   64:                 pool => 'Question pools',
   65:                 quiz => 'Quizzes',
   66:                 staff => 'Staff information',
   67:                 survey => 'Surveys',
   68:                 users => 'Enrollment',
   69:                 );
   70: }
   71:  
   72: sub create_tempdir {
   73:     my ($context,$pathinfo,$timenow) = @_;   
   74:     my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
   75:     my $tempdir;
   76:     if ($context eq 'DOCS') {
   77:         $tempdir =  $$configvars{'lonDaemons'}.'/tmp/'.$pathinfo;
   78:         if (!-e "$tempdir") {
   79:             mkdir("$tempdir",0770);
   80:         } 
   81:         $tempdir .= '/'.$timenow;
   82:         if (!-e "$tempdir") {
   83:             mkdir("$tempdir",0770);
   84:         } 
   85:     } elsif ($context eq "CSTR") {
   86:         if (!-e "$pathinfo/temp") {
   87:             mkdir("$pathinfo/temp",0770);
   88:         }
   89:         $tempdir =  $pathinfo.'/temp';
   90:     }
   91:     return $tempdir;
   92: }
   93: 
   94: sub uploadzip {
   95:     my ($context,$tempdir,$source) = @_;
   96:     my $fname;
   97:     if ($context eq 'DOCS') {
   98:         $fname=$ENV{'form.uploadname.filename'};
   99: # Replace Windows backslashes by forward slashes
  100:         $fname=~s/\\/\//g;
  101: # Get rid of everything but the actual filename
  102:         $fname=~s/^.*\/([^\/]+)$/$1/;
  103: # Replace spaces by underscores
  104:         $fname=~s/\s+/\_/g;
  105: # Replace all other weird characters by nothing
  106:         $fname=~s/[^\w\.\-]//g;
  107: # See if there is anything left
  108:         unless ($fname) { return 'error: no uploaded file'; }
  109: # Save the file
  110:         chomp($ENV{'form.uploadname'});
  111:         open(my $fh,'>'.$tempdir.'/'.$fname);
  112:         print $fh $ENV{'form.uploadname'};
  113:         close($fh);
  114:     } elsif ($context eq 'CSTR') {
  115:         if ($source =~ m/\/([^\/]+)$/) {
  116:             $fname = $1;
  117:             my $destination = $tempdir.'/'.$fname;
  118:             rename($source,$destination);
  119:         }
  120:     }
  121:     return $fname;   
  122: }
  123: 
  124: sub expand_zip {
  125:     my ($tempdir,$filename) = @_;
  126:     my $zipfile = "$tempdir/$filename";
  127:     if (!-e "$zipfile") {
  128:         return 'no zip';
  129:     }
  130:     if ($filename =~ m|\.zip$|i) {
  131:     # unzip can cause an sh launch which can pass along all of %ENV
  132:     # which can be too large for /bin/sh to handle
  133:         my %oldENV=%ENV;
  134:         undef(%ENV);
  135:         open(OUTPUT, "unzip -o $zipfile -d $tempdir  2> /dev/null |");
  136:         close(OUTPUT);
  137:         %ENV=%oldENV;
  138:         undef(%oldENV);
  139:     } else {
  140:         return 'nozip';
  141:     }
  142:     if ($filename =~ m|\.zip$|i) {
  143:         unlink($zipfile);
  144:     }
  145:     return 'ok';
  146: }
  147: 
  148: sub process_manifest {
  149:     my ($cms,$tempdir,$resources,$items,$hrefs,$resinfo,$phase,$includedres,$includeditems) = @_;
  150:     my %toc = (
  151:               bb6 => 'organization',
  152:               bb5 => 'tableofcontents',
  153:               angel => 'organization',
  154:               webct4 => 'organization',
  155:               );
  156:     my %contents = ();
  157:     my @state = ();
  158:     my $itm = '';
  159:     my $identifier = '';
  160:     my @seq = "Top";
  161:     my $lastitem;
  162:     %{$$items{'Top'}} = (
  163:                       contentscount => 0,
  164:                       resnum => 'toplevel',
  165:                       );
  166:     %{$$resources{'toplevel'}} = (
  167:                                   revitm => 'Top'
  168:                                  );
  169:  
  170:     if ($cms eq 'angel') {
  171:         $$resources{'toplevel'}{type} = "FOLDER";
  172:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  173:         $$resources{'toplevel'}{type} = 'resource/x-bb-document';
  174:     } else {
  175:         $$resources{'toplevel'}{type} = 'webcontent';
  176:     }
  177: 
  178:     unless (-e "$tempdir/imsmanifest.xml") {
  179:         return 'nomanifest';
  180:     } 
  181: 
  182:     my $xmlfile = $tempdir.'/imsmanifest.xml';
  183:     my $p = HTML::Parser->new
  184:     (
  185:        xml_mode => 1,
  186:        start_h =>
  187:            [sub {
  188:                 my ($tagname, $attr) = @_;
  189:                 push @state, $tagname;
  190:                 my $num = @state - 3;
  191:                 my $start = $num;
  192:                 my $statestr = '';
  193:                 foreach (@state) {
  194:                     $statestr .= "$_ ";
  195:                 }
  196:                 if ( ($state[0] eq "manifest") && ($state[1] eq "organizations") && ($state[2] eq $toc{$cms}) ) {
  197:                     my $searchstr = "manifest organizations $toc{$cms}";
  198:                     while ($num > 0) {
  199:                         $searchstr .= " item";
  200:                         $num --; 
  201:                     }
  202:                     if (("@state" eq $searchstr) && (@state > 3)) {
  203:                         $itm = $attr->{identifier};
  204:                         if ($$includeditems{$itm} || $phase ne 'build') {              
  205:                             %{$$items{$itm}} = ();
  206:                             $$items{$itm}{contentscount} = 0;
  207:                             if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webct4') {
  208:                                 $$items{$itm}{resnum} = $attr->{identifierref};
  209:                                 if ($cms eq 'bb5') {
  210:                                     $$items{$itm}{title} = $attr->{title};
  211:                                 }
  212:                             } elsif ($cms eq 'angel') {
  213:                                 if ($attr->{identifierref} =~ m/^res(.+)$/) {
  214:                                     $$items{$itm}{resnum} = $1;
  215:                                 }
  216:                             }
  217:                             unless (defined(%{$$resources{$$items{$itm}{resnum}}}) ) {
  218:                                 %{$$resources{$$items{$itm}{resnum}}} = ();
  219:                             }
  220:                             $$resources{$$items{$itm}{resnum}}{revitm} = $itm;
  221:                             if ($start > @seq) {
  222:                                 unless ($lastitem eq '') {
  223:                                     push @seq, $lastitem;
  224:                                     unless ( defined($contents{$seq[-1]}) ) {
  225:                                         @{$contents{$seq[-1]}} = ();
  226:                                     }
  227:                                     push @{$contents{$seq[-1]}},$itm;
  228:                                     $$items{$itm}{parentseq} = $seq[-1];
  229:                                 }
  230:                             } elsif ($start < @seq) {
  231:                                 my $diff = @seq - $start;
  232:                                 while ($diff > 0) {
  233:                                     pop @seq;
  234:                                     $diff --;
  235:                                 }
  236:                                 if (@seq) {
  237:                                     push @{$contents{$seq[-1]}}, $itm;
  238:                                 }
  239:                             } else {
  240:                                 push @{$contents{$seq[-1]}}, $itm;
  241:                             }
  242:                             my $path;
  243:                             if (@seq > 1) {
  244:                                 $path = join(',',@seq);
  245:                             } elsif (@seq > 0) {
  246:                                 $path = $seq[0];
  247:                             }
  248:                             $$items{$itm}{filepath} = $path;
  249:                             if ($cms eq 'bb5' || $cms eq 'bb6') {
  250:                                 if ($$items{$itm}{filepath} eq 'Top') {
  251:                                     $$items{$itm}{resnum} = $itm;
  252:                                     $$resources{$$items{$itm}{resnum}}{type} = 'resource/x-bb-document';
  253:                                     $$resources{$$items{$itm}{resnum}}{revitm} = $itm;
  254:                                     $$resinfo{$$items{$itm}{resnum}}{'isfolder'} = 'true';
  255:                                 }
  256:                             }
  257:                             $$items{$seq[-1]}{contentscount} ++;
  258:                             $lastitem = $itm;
  259:                         }
  260:                     }
  261:                 } elsif ("@state" eq "manifest resources resource" ) {
  262:                     $identifier = $attr->{identifier};
  263:                     if ($$includedres{$identifier} || $phase ne 'build') { 
  264:                         if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webct4') {
  265:                             $$resources{$identifier}{file} = $attr->{file};
  266:                             $$resources{$identifier}{type} = $attr->{type};
  267:                         } elsif ($cms eq 'angel') {
  268:                             $identifier = substr($identifier,3);
  269:                             if ($attr->{href} =~ m-^_assoc/$identifier/(.+)$-) {
  270:                                 $$resources{$identifier}{file} = $1;
  271:                             }
  272:                         }
  273:                         @{$$hrefs{$identifier}} = ();
  274:                     }
  275:                 } elsif ("@state" eq "manifest resources resource file") {
  276:                     if ($$includedres{$identifier} || $phase ne 'build') {
  277:                         if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webct4') {
  278:                             push @{$$hrefs{$identifier}},$attr->{href};
  279:                         } elsif ($cms eq 'angel') {
  280:                             if ($attr->{href} =~ m/^_assoc\\$identifier\\(.+)$/) {
  281:                                 push @{$$hrefs{$identifier}},$1;
  282:                             } elsif ($attr->{href} =~ m/^Icons\\icon(\w+)\.gif/) {
  283:                                 $$resources{$identifier}{type} = $1;
  284:                             }
  285:                         }
  286:                     }
  287:                 }
  288:            }, "tagname, attr"],
  289:         text_h =>
  290:             [sub {
  291:                 my ($text) = @_;
  292:                 if ("@state" eq "manifest metadata lom general title langstring") {
  293:                     $$items{'Top'}{title} = $text;
  294:                 }
  295:                 if ($state[0] eq "manifest" && $state[1] eq "organizations" && $state[2] eq $toc{$cms} && $state[-1] eq "title") {
  296:                     if ($$includeditems{$itm} || $phase ne 'build') {
  297:                         if ($cms eq 'angel' || $cms eq 'bb6') {
  298:                             $$items{$itm}{title} = $text;
  299:                         }
  300:                         if ($cms eq 'webct4') {
  301:                             $$items{$itm}{title} = $text;
  302:                             $$items{$itm}{title} =~ s/(<[^>]*>)//g;
  303:                         }
  304:                     }
  305:                 }
  306:               }, "dtext"],
  307:         end_h =>
  308:               [sub {
  309:                   my ($tagname) = @_;
  310:                   pop @state;
  311:                }, "tagname"],
  312:     );
  313:     $p->parse_file($xmlfile);
  314:     $p->eof;
  315: 
  316:     foreach my $itm (keys %contents) {
  317:         @{$$items{$itm}{contents}} = @{$contents{$itm}};
  318:     }
  319:     return 'ok' ;
  320: }
  321: 
  322: sub get_imports {
  323:     my ($includeditems,$items,$resources,$importareas,$itm) = @_;
  324:     if (exists($$items{$itm}{resnum})) {
  325:         if ($$importareas{$$resources{$$items{$itm}{resnum}}{type}}) {
  326:             unless (exists($$includeditems{$itm})) {
  327:                 $$includeditems{$itm} = 1;
  328:             }
  329:         }
  330:     }
  331:     if ($$items{$itm}{contentscount} > 0) {
  332:         foreach my $child (@{$$items{$itm}{contents}}) {
  333:             &get_imports($includeditems,$items,$resources,$importareas,$child);
  334:         }
  335:     }
  336: }
  337: 
  338: sub get_parents {
  339:     my ($includeditems,$items,$itm) = @_;
  340:     my @pathitems = ();
  341:     if ($$items{$itm}{filepath} =~ m/,/) {
  342:        @pathitems = split/,/,$$items{$itm}{filepath};
  343:     } else {
  344:        $pathitems[0] = $$items{$itm}{filepath};
  345:     }
  346:     foreach (@pathitems) {
  347:         $$includeditems{$_} = 1;
  348:     }
  349: }
  350: 
  351: sub target_resources {
  352:     my ($resources,$oktypes,$targets) = @_;
  353:     foreach my $key (keys %{$resources}) {
  354:         if ( defined($$oktypes{$$resources{$key}{type}}) ) {
  355:             push @{$targets}, $key;
  356:         }
  357:     }
  358:     return;
  359: }
  360: 
  361: sub copy_resources {
  362:     my ($context,$cms,$hrefs,$tempdir,$targets,$url,$crs,$cdom,$chome,$destdir,$timenow) = @_;
  363:     if ($context eq 'DOCS') {
  364:         foreach my $key (sort keys %{$hrefs}) {
  365:             if (grep/^$key$/,@{$targets}) {
  366:                 %{$$url{$key}} = ();
  367:                 foreach my $file (@{$$hrefs{$key}}) {
  368:                     my $source = $tempdir.'/'.$key.'/'.$file;
  369:                     if ($cms eq 'webct4') {
  370:                         $source = $tempdir.'/'.$file;
  371:                     }
  372:                     my $filename = '';
  373:                     my $fpath = $timenow.'/resfiles/'.$key.'/';
  374:                     if ($cms eq 'angel') {
  375:                         if ($file eq 'pg'.$key.'.htm') {
  376:                             next;
  377:                         }
  378:                     }
  379:                     $file =~ s-\\-/-g;
  380:                     my $copyfile = $file;
  381:                     if ($cms eq 'webct4') {
  382:                         if ($file =~ m-/([^/]+)$-) {
  383:                             $copyfile = $1;
  384:                         }
  385:                     }
  386:                     unless (($cms eq 'webct4') && ($copyfile eq 'questionDB.xml' || $copyfile =~ m/^quiz_QIZ_\d+\.xml$/)) {
  387:                         $copyfile = $fpath.$copyfile;
  388:                         my $fileresult;
  389:                         if (-e $source) {
  390:                             $fileresult = &Apache::lonnet::process_coursefile('copy',$crs,$cdom,$chome,$copyfile,$source);
  391:                         }
  392:                     }
  393:                 }
  394:             }
  395:         }
  396:     } elsif ($context eq 'CSTR') {
  397:         if (!-e "$destdir/resfiles") {
  398:             mkdir("$destdir/resfiles",0770);
  399:         }
  400:         foreach my $key (sort keys %{$hrefs}) {
  401:             if (grep/^$key$/,@{$targets}) {
  402:                 foreach my $file (@{$$hrefs{$key}}) {
  403:                     $file =~ s-\\-/-g;
  404:                     if ( ($cms eq 'angel' && $file ne 'pg'.$key.'.htm') || ($cms eq 'bb5') || ($cms eq 'bb6')) {
  405:                         if (!-e "$destdir/resfiles/$key") {
  406:                             mkdir("$destdir/resfiles/$key",0770);
  407:                         }
  408:                         my $filepath = $file;
  409:                         my $front = '';
  410:                         while ($filepath =~ m-(\w+)/(.+)-) {
  411:                             $front .= $1.'/';
  412:                             $filepath = $2;
  413:                             my $fulldir = "$destdir/resfiles/$key/$front";
  414:                             chop($fulldir);
  415:                             if (!-e "$fulldir") {
  416:                                 mkdir("$fulldir",0770);
  417:                             }
  418:                         }
  419:                         if ($cms eq 'angel') {
  420:                             rename("$tempdir/_assoc/$key/$file","$destdir/resfiles/$key/$file");
  421:                         } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  422:                             rename("$tempdir/$key/$file","$destdir/resfiles/$key/$file");
  423:                         }
  424:                     } elsif ($cms eq 'webct4') {
  425:                         if (!-e "$destdir/resfiles/$key") {
  426:                             mkdir("$destdir/resfiles/$key",0770);
  427:                         }
  428:                         if ($file =~ m-/([^/]+)$-) {
  429:                             my $copyfile = $1;
  430:                             unless ($copyfile eq 'questionDB.xml' || $copyfile =~ m/^quiz_QIZ_\d+\.xml$/) { 
  431:                                 rename("$tempdir/$file","$destdir/resfiles/$key/$1");
  432:                             }
  433:                         }
  434:                     }
  435:                 }
  436:             }
  437:         }
  438:     }
  439: }
  440: 
  441: sub process_resinfo {
  442:     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,$randompicks) = @_;
  443:     my $board_id = time;
  444:     my $board_count = 0;
  445:     my $dbparse = 0;
  446:     my $announce_handling = 'include';
  447:     my $longcrs = '';
  448:     if ($crs =~ m/^(\d)(\d)(\d)/) {
  449:         $longcrs = $1.'/'.$2.'/'.$3.'/'.$crs;
  450:     }
  451:     if ($context eq 'CSTR') {
  452:         if (!-e "$destdir/resfiles") {
  453:             mkdir("$destdir/resfiles",0770);
  454:         }
  455:     }
  456:     if ($cms eq 'angel') {
  457:         my $currboard = '';
  458:         foreach my $key (sort keys %{$resources}) {
  459:           if (grep/^$key$/,@{$targets}) {
  460:             if ($$resources{$key}{type} eq "BOARD") {
  461:                 push @{$boards}, $key;
  462:                 $$boardnum{$$resources{$key}{revitm}} = $board_count;
  463:                 $currboard = $key;
  464:                 @{$$messages{$key}} = ();
  465:                 $$timestamp[$board_count] = $board_id;
  466:                 $board_id ++;
  467:                 $board_count ++;
  468:             } elsif ($$resources{$key}{type} eq "MESSAGE") {
  469:                 push @{$$messages{$currboard}}, $key;
  470:             } elsif ($$resources{$key}{type} eq "PAGE" || $$resources{$key}{type} eq "LINK") {
  471:                 %{$$resinfo{$key}} = ();
  472:                 &angel_content($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$$resources{$key}{type},$$items{$$resources{$key}{revitm}}{title},$resrcfiles);
  473:             } elsif ($$resources{$key}{type} eq "QUIZ") {
  474:                 %{$$resinfo{$key}} = ();
  475:                 push @{$quizzes}, $key;
  476: #               &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  477:             } elsif ($$resources{$key}{type} eq "FORM") {
  478:                 %{$$resinfo{$key}} = ();
  479:                 push @{$surveys}, $key;
  480: #                &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  481:             } elsif ($$resources{$key}{type} eq "DROPBOX") {
  482:                 %{$$resinfo{$key}} = ();
  483:             }
  484:           }
  485:         }
  486:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  487:         foreach my $key (sort keys %{$resources}) {
  488:           if (grep/^$key$/,@{$targets}) {
  489:             if ($$resources{$key}{type} eq "resource/x-bb-document") {
  490:                 unless ($$items{$$resources{$key}{revitm}}{filepath} eq 'Top') {
  491:                     %{$$resinfo{$key}} = ();
  492:                     &process_content($cms,$key,$context,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$resrcfiles,$packages,$hrefs);
  493:                 }
  494:             } elsif ($$resources{$key}{type} eq "resource/x-bb-staffinfo") {
  495:                 %{$$resinfo{$key}} = ();
  496:                 &process_staff($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  497:             } elsif ($$resources{$key}{type} eq "resource/x-bb-externallink") {
  498:                 %{$$resinfo{$key}} = ();
  499:                 &process_link($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  500:             } elsif ($$resources{$key}{type} eq "resource/x-bb-discussionboard") {
  501:                 %{$$resinfo{$key}} = ();
  502:                 unless ($db_handling eq 'ignore') {
  503:                     push @{$boards}, $key;
  504:                     $$timestamp[$board_count] = $board_id;
  505:                     &process_db($key,$docroot,$destdir,$board_id,$crs,$cdom,$db_handling,$uname,\%{$$resinfo{$key}},$longcrs);
  506:                     $board_id ++;
  507:                     $board_count ++;
  508:                 }
  509:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-pool") {
  510:                 %{$$resinfo{$key}} = ();
  511:                 &process_assessment($cms,$context,$key,$docroot,'pool',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse);
  512:                 push @{$pools}, $key;
  513:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-quiz") {
  514:                 %{$$resinfo{$key}} = ();
  515:                 &process_assessment($cms,$context,$key,$docroot,'quiz',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse);
  516:                 push @{$quizzes}, $key;
  517:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-survey") {
  518:                 %{$$resinfo{$key}} = ();
  519:                 &process_assessment($cms,$context,$key,$docroot,'survey',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse);
  520:                 push @{$surveys}, $key;
  521:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-group") {
  522:                 %{$$resinfo{$key}} = ();
  523:                 push @{$groups}, $key;
  524:                 &process_group($key,$docroot,$destdir,\%{$$resinfo{$key}});
  525:             } elsif ($$resources{$key}{type} eq "resource/x-bb-user") {   
  526:                 %{$$resinfo{$key}} = ();
  527:                 unless ($user_handling eq 'ignore') {
  528:                     &process_user($key,$docroot,$destdir,\%{$$resinfo{$key}},$crs,$cdom,$user_handling);
  529:                 }
  530:             } elsif ($$resources{$key}{type} eq "resource/x-bb-announcement") {
  531:                 unless ($announce_handling eq 'ignore') {
  532:                     push @{$announcements}, $key;
  533:                     %{$$resinfo{$key}} = ();
  534:                     &process_announce($key,$docroot,$destdir,\%{$$resinfo{$key}},$resinfo,$seqstem,$resrcfiles);
  535:                 }
  536:             }
  537:           }
  538:         }
  539:         if (@{$announcements}) {
  540:             $$items{'Top'}{'contentscount'} ++;
  541:         }
  542:         if (@{$boards}) {
  543:             $$items{'Top'}{'contentscount'} ++;
  544:         }
  545:         if (@{$quizzes}) {
  546:             $$items{'Top'}{'contentscount'} ++;
  547:         }
  548:         if (@{$surveys}) {
  549:             $$items{'Top'}{'contentscount'} ++;
  550:         }
  551:         if (@{$pools}) {
  552:             $$items{'Top'}{'contentscount'} ++;
  553:         }
  554:     } elsif ($cms eq 'webct4') {
  555:         foreach my $key (sort keys %{$resources}) {
  556:             if (grep/^$key$/,@{$targets}) {
  557:                 if ($$resources{$key}{type} eq "webcontent") {
  558:                     %{$$resinfo{$key}} = ();
  559: #                    &webct4_content($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$$resources{$key}{type},$$items{$$resources{$key}{revitm}}{title},$resrcfiles);
  560:                 } elsif ($$resources{$key}{type} eq "webctquiz") {
  561: #                    &webct4_assessment($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$$resources{$key}{type},$$items{$$resources{$key}{revitm}}{title},$resrcfiles);
  562:                     &process_assessment($cms,$context,$key,$docroot,'quiz',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse);
  563: 
  564:                 }
  565:             }
  566:         }
  567:     }
  568: 
  569:     $$total{'board'} = $board_count;
  570:     $$total{'quiz'} = @{$quizzes};
  571:     $$total{'surv'} = @{$surveys};
  572:     $$total{'pool'} = @{$pools};
  573: }
  574: 
  575: sub build_structure {
  576:     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,$randompicks) = @_;
  577:     my %flag = ();
  578:     my %count = ();
  579:     my %pagecontents = ();
  580:     my %seqtext = ();
  581:     my $topnum = 0;
  582:     my $topspecials = @$announcements + @$boards + @$quizzes + @$surveys + @$pools;
  583: 
  584:     if (!-e "$destdir") {
  585:         mkdir("$destdir",0755);
  586:     }
  587:     if (!-e "$destdir/sequences") {
  588:         mkdir("$destdir/sequences",0770);
  589:     }
  590:     if (!-e "$destdir/resfiles") {
  591:         mkdir("$destdir/resfiles",0770);
  592:     }
  593:     if (!-e "$destdir/pages") {
  594:         mkdir("$destdir/pages",0770);
  595:     }
  596:     if (!-e "$destdir/problems") {
  597:         mkdir("$destdir/problems",0770);
  598:     }
  599: 
  600:     $seqtext{'Top'} = qq|<map>\n|;       
  601:     %{$$resinfo{$$items{'Top'}{resnum}}} = (
  602:                                          isfolder => 'true',
  603:                                         );
  604: 
  605:     my $srcstem = "";
  606:  
  607:     if ($context eq 'DOCS') {
  608:         $srcstem = "/uploaded/$cdom/$crs/$timenow";
  609:     } elsif ($context eq 'CSTR') {
  610:         $srcstem = "/res/$udom/$uname/$newdir";
  611:     }
  612: 
  613:     foreach my $key (sort keys %{$items}) {
  614:       if ($$includeditems{$key}) {
  615:         %{$flag{$key}} = (
  616:                           page => 0,
  617:                           seq => 0,
  618:                           board => 0,
  619:                           file => 0,
  620:                          );
  621: 
  622:         %{$count{$key}} = (
  623:                            page => -1,
  624:                            seq => 0,
  625:                            board => 0,
  626:                            file => 0,
  627:                           );
  628: 
  629:         my $src = "";
  630: 
  631:         my $next_id = 2;
  632:         my $curr_id = 1;
  633:         my $resnum = $$items{$key}{resnum};
  634:         my $type = $$resources{$resnum}{type};
  635:         my $contentscount = $$items{$key}{'contentscount'}; 
  636:         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")) || ($cms eq 'webct4' &&  $contentscount > 0)) {
  637:             print STDERR "$key is identified as a folder has $contentscount\n";
  638:             unless (($cms eq 'bb5') && $key eq 'Top') {
  639:                 $seqtext{$key} = "<map>\n";
  640:             }
  641:             if ($contentscount == 0) {
  642: 	        if ($key eq 'Top') {
  643:                     unless ($topspecials) {
  644:                         $seqtext{$key} .= qq|<resource id="$curr_id" src="" type="start"></resource>
  645: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  646: <resource id="$next_id" src="" type="finish"></resource>\n|;
  647:                     }
  648:                 } else {
  649:                     $seqtext{$key} .= qq|<resource id="$curr_id" src="" type="start"></resource>
  650: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  651: <resource id="$next_id" src="" type="finish"></resource>\n|;
  652:                 }
  653:             } else {
  654:                 my $contcount = @{$$items{$key}{contents}};
  655:                 my $contitem = $$items{$key}{contents}[0];
  656:                 my $contitemcount = $$items{$contitem}{contentscount}; 
  657:                 my ($res,$itm,$type);
  658:                 if (exists($$items{$contitem}{resnum})) {
  659:                     $res = $$items{$contitem}{resnum};
  660:                     $itm = $$resources{$res}{revitm};
  661:                     $type = $$resources{$res}{type};
  662:                 }
  663:                 my $title = $$items{$contitem}{title};
  664:                 my $packageflag = 0;
  665:                 if (grep/^$res$/,@{$packages}) {
  666:                     $packageflag = 1;
  667:                 }
  668:                 $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount);
  669:                 unless ($flag{$key}{page} == 1) {
  670:                     $seqtext{$key} .= qq|<resource id="$curr_id" src="$src" title="$title" type="start"|;
  671:                     unless ($flag{$key}{seq} || $flag{$key}{board} || $flag{$key}{file}) {
  672:                         $flag{$key}{page} = 1;
  673:                     }
  674:                     if ($key eq 'Top') {
  675:                         push @{$topurls}, $src;
  676:                         push @{$topnames}, $title;
  677:                     }
  678:                 }
  679:                 if ($contcount == 1) {
  680:                     $seqtext{$key} .= qq|></resource>
  681: <link from="$curr_id" to="$next_id" index="$curr_id"></link>|;
  682:                     if ($key eq 'Top') {
  683:                         unless ($topspecials) {
  684:                             $seqtext{$key} .= qq|
  685: <resource id="$next_id" src="" type="finish"></resource>\n|;
  686:                         }
  687:                     } else {
  688:                         $seqtext{$key} .= qq|
  689: <resource id="$next_id" src="" type="finish"></resource>\n|;
  690:                     }
  691:                 } else {
  692:                     if ($contcount > 2 ) {
  693:                         for (my $i=1; $i<$contcount-1; $i++) {
  694:                             my $contitem = $$items{$key}{contents}[$i];
  695:                             my $contitemcount = $$items{$contitem}{contentscount};
  696:                             my $res = $$items{$contitem}{resnum};
  697:                             my $type = $$resources{$res}{type};
  698:                             my $title = $$items{$contitem}{title};
  699:                             my $packageflag = 0;
  700:                             if (grep/^$res$/,@{$packages}) {
  701:                                 $packageflag = 1;
  702:                             }
  703:                             $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount);
  704: 
  705:                             unless ($flag{$key}{page} == 1) {
  706:                                 $seqtext{$key} .= qq|></resource>
  707: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  708: <resource id="$next_id" src="$src" title="$title"|;
  709:                                 $curr_id ++;
  710:                                 $next_id ++;
  711:                                 unless ($flag{$key}{seq} || $flag{$key}{board} || $flag{$key}{file}) {
  712:                                     $flag{$key}{page} = 1;
  713:                                 }
  714:                                 if ($key eq 'Top') {
  715:                                     push @{$topurls}, $src;
  716:                                     push @{$topnames}, $title;
  717:                                 }
  718:                             }
  719:                         }
  720:                     }
  721:                     my $contitem = $$items{$key}{contents}[-1];
  722:                     my $contitemcount = $$items{$contitem}{contentscount};
  723:                     my $res = $$items{$contitem}{resnum};
  724:                     my $type = $$resources{$res}{type};
  725:                     my $title = $$items{$contitem}{title};
  726:                     my $packageflag = 0;
  727:                     if (grep/^$res$/,@{$packages}) {
  728:                         $packageflag = 1;
  729:                     }
  730:                     $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount);
  731: 
  732:                     if ($flag{$key}{page}) {
  733:                         if ($count{$key}{seq} + $count{$key}{page} + $count{$key}{board} + $count{$key}{file} +1 == 1) {
  734:                             $seqtext{$key} .= qq|></resource>
  735: <link from="$curr_id" index="$curr_id" to="$next_id">
  736: <resource id ="$next_id" src="" |;
  737:                         }
  738:                     } else {
  739:                         $seqtext{$key} .= qq|></resource>
  740: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  741: <resource id="$next_id" src="$src" title="$title" |;
  742:                         if ($key eq 'Top') {
  743:                             push @{$topurls}, $src;
  744:                             push @{$topnames}, $title;
  745:                         }
  746:                     }
  747:                     if ($contcount == $$items{$key}{contentscount}) {
  748:                         $seqtext{$key} .= qq|type="finish"></resource>\n|;
  749:                     } else {
  750:                         $curr_id ++;
  751:                         $next_id ++;
  752:                         $seqtext{$key} .= qq|></resource>
  753: <link from="$curr_id" to="$next_id" index="$curr_id"></link>\n|;
  754:                     } 
  755:                 }
  756:             }
  757:             unless (($cms eq 'bb5') && $key eq 'Top') {
  758:                 $seqtext{$key} .= "</map>\n";
  759:                 open(LOCFILE,">$destdir/sequences/$key.sequence");
  760:                 print LOCFILE $seqtext{$key};
  761:                 close(LOCFILE);
  762:                 push @{$seqfiles}, "$key.sequence";
  763:             }
  764:             $count{$key}{page} ++;
  765:             $$total{page} += $count{$key}{page};
  766:         }
  767:         $$total{seq} += $count{$key}{seq};
  768:       }
  769:     }
  770:     $topnum += ($count{'Top'}{page} + $count{'Top'}{seq});
  771: 
  772:     if ($cms eq 'bb5' || $cms eq 'bb6') {
  773:         if (@{$announcements} > 0) {
  774:             &process_specials($context,'announcements',$announcements,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  775:         }
  776:         if (@{$boards} > 0) {
  777:             &process_specials($context,'boards',$boards,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  778:         }
  779:         if (@{$quizzes} > 0) {
  780:             &process_specials($context,'quizzes',$quizzes,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  781:         }
  782:         if (@{$surveys} > 0)  {
  783:             &process_specials($context,'surveys',$surveys,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  784:         }
  785:         if (@{$pools} > 0)  {
  786:             &process_specials($context,'pools',$pools,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  787:         }
  788:         $seqtext{'Top'} .= "</map>\n";
  789:         open(TOPFILE,">$destdir/sequences/Top.sequence");
  790:         print TOPFILE $seqtext{'Top'};
  791:         close(TOPFILE);
  792:         push @{$seqfiles}, 'Top.sequence';
  793:     }
  794: 
  795:     my $filestem;
  796:     if ($context eq 'DOCS') {
  797:         $filestem = "/uploaded/$cdom/$crs/$timenow";
  798:     } elsif ($context eq 'CSTR') {
  799:         $filestem = "/res/$udom/$uname/$newdir";
  800:     }
  801: 
  802:     foreach my $key (sort keys %pagecontents) {
  803:         for (my $i=0; $i<@{$pagecontents{$key}}; $i++) {
  804:             my $filename = $destdir.'/pages/'.$key.'_'.$i.'.page';
  805:             my $resource = "$filestem/resfiles/$$items{$pagecontents{$key}[$i][0]}{resnum}.html";
  806:             my $res = $$items{$pagecontents{$key}[$i][0]}{resnum};
  807:             my $resource = $filestem.'/resfiles/'.$res.'.html';
  808:             if (grep/^$res$/,@{$packages}) {
  809:                 $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # should be entry_point
  810:             }
  811:             open(PAGEFILE,">$filename");
  812:             print PAGEFILE qq|<map>
  813: <resource src="$resource" id="1" type="start" title="$$items{$pagecontents{$key}[$i][0]}{title}"></resource>
  814: <link to="2" index="1" from="1">\n|;
  815:             if (@{$pagecontents{$key}[$i]} == 1) {
  816:                 print PAGEFILE qq|<resource src="" id="2" type="finish"></resource>\n|;
  817:             } elsif (@{$pagecontents{$key}[$i]} == 2)  {
  818:                 my $res = $$items{$pagecontents{$key}[$i][1]}{resnum};
  819:                 my $resource = $filestem.'/resfiles/'.$res.'.html';
  820:                 if (grep/^$res$/,@{$packages}) {
  821:                     $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # should be entry_point
  822:                 }
  823:                 print PAGEFILE qq|<resource src="$resource" id="2" type="finish" title="$$items{$pagecontents{$key}[$i][1]}{title}"></resource>\n|;
  824:             } else {
  825:                 for (my $j=1; $j<@{$pagecontents{$key}[$i]}-1; $j++) {
  826:                     my $curr_id = $j+1;
  827:                     my $next_id = $j+2;
  828:                     my $res = $$items{$pagecontents{$key}[$i][$j]}{resnum};
  829:                     my $resource = $filestem.'/resfiles/'.$res.'.html';
  830:                     if (grep/^$res$/,@{$packages}) {
  831:                         $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # entry_point
  832:                     }
  833:                     print PAGEFILE qq|<resource src="$resource" id="$curr_id" title="$$items{$pagecontents{$key}[$i][$j]}{title}"></resource>
  834: <link to="$next_id" index="$curr_id" from="$curr_id">\n|;
  835:                 }
  836:                 my $final_id = @{$pagecontents{$key}[$i]};
  837:                 my $res = $$items{$pagecontents{$key}[$i][-1]}{resnum};
  838:                 my $resource = $filestem.'/resfiles/'.$res.'.html';
  839:                 if (grep/^$res$/,@{$packages}) {
  840:                     $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # entry_point
  841:                 }
  842:                 print PAGEFILE qq|<resource src="$resource" id="$final_id" type="finish" title="$$items{$pagecontents{$key}[$i][-1]}{title}"></resource>\n|;
  843:             }
  844:             print PAGEFILE "</map>";
  845:             close(PAGEFILE);
  846:             push @{$pagesfiles}, $key.'_'.$i.'.page'; 
  847:         }
  848:     }
  849: }
  850: 
  851: sub make_structure {
  852:     my ($cms,$key,$srcstem,$flag,$count,$timestamp,$boardnum,$hrefs,$pagecontents,$res,$type,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount) = @_;
  853:     my $src ='';
  854:     if (($cms eq 'angel' && $type eq 'FOLDER') || (($cms eq 'bb5' || $cms eq 'bb6') && (($$resinfo{$res}{'isfolder'} eq 'true') || $key eq 'Top')) || ($cms eq 'webct4' && $contitemcount > 0)) {
  855:         $src = $srcstem.'/sequences/'.$contitem.'.sequence';
  856:         $$flag{$key}{page} = 0;
  857:         $$flag{$key}{seq} = 1;
  858:         $$count{$key}{seq} ++;
  859:     } elsif ($cms eq 'angel' && $type eq 'BOARD') {
  860:         $src = '/adm/'.$cdom.'/'.$uname.'/'.$$timestamp[$$boardnum{$res}].'/bulletinboard'; 
  861:         $$flag{$key}{page} = 0;
  862:         $$flag{$key}{board} = 1;
  863:         $$count{$key}{board} ++;
  864:     } elsif ($cms eq 'angel' && $type eq "FILE") {
  865:         foreach my $file (@{$$hrefs{$res}}) {
  866:             unless ($file eq 'pg'.$res.'.htm') {
  867:                 $src = $srcstem.'/resfiles/'.$res.'/'.$file;
  868:             }
  869:         }
  870:         $$flag{$key}{page} = 0;
  871:         $$flag{$key}{file} = 1;
  872:     } elsif ($cms eq 'angel' && (($type eq "PAGE") || ($type eq "LINK")) )  {
  873:         if ($$flag{$key}{page}) {
  874:             if ($$count{$key}{page} == -1) {
  875:                 print STDERR "Array index is -1, we shouldnt be here, key is $key, type is $type\n";
  876:             } else { 
  877:                 push @{$$pagecontents{$key}[$$count{$key}{page}]},$contitem;
  878:             }
  879:         } else {
  880:             $$count{$key}{page} ++;
  881:             $src = $srcstem.'/pages/'.$key.'_'.$$count{$key}{page}.'.page';
  882:             @{$$pagecontents{$key}[$$count{$key}{page}]} = ("$contitem");
  883:             $$flag{$key}{seq} = 0;
  884:         }
  885:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  886:         if ($$flag{$key}{page}) {
  887:             push @{$$pagecontents{$key}[$$count{$key}{page}]},$contitem;
  888:         } else {
  889:             if ($contcount == 1) {
  890:                 if ($packageflag) {
  891:                     $src = $srcstem.'/resfiles/'.$res.'/index.html'; # Needs to be entry point
  892:                 } else {
  893:                     $src = $srcstem.'/resfiles/'.$res.'.html';
  894:                 }
  895:             } else {
  896:                 $$count{$key}{page} ++;
  897:                 $src = $srcstem.'/pages/'.$key.'_'.$$count{$key}{page}.'.page';
  898:                 @{$$pagecontents{$key}[$$count{$key}{page}]} = ("$contitem");
  899:             }
  900:             $$flag{$key}{seq} = 0;
  901:         }
  902:     } elsif ($cms eq 'webct4') {
  903:         unless ($type eq 'webctquiz') {
  904:             foreach my $file (@{$$hrefs{$res}}) {
  905:                 my $filename;
  906:                 if ($file =~ m-/([^/]+)$-) {
  907:                     $filename = $1;
  908:                 }
  909:                 $src = $srcstem.'/resfiles/'.$res.'/'.$filename;
  910:             }
  911:             $$flag{$key}{page} = 0;
  912:             $$flag{$key}{file} = 1;
  913:         }
  914:     }
  915:     return $src;
  916: }
  917: 
  918: 
  919: # ---------------------------------------------------------------- Process Blackboard specials - announcements, bulletin boards, quizzes and surveys
  920: sub process_specials {
  921:     my ($context,$type,$specials,$topnum,$contentscount,$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,$seqtext,$pagesfiles,$seqfiles,$topurls,$topnames) = @_;
  922:     my $src = '';
  923:     my $specialsrc = '';
  924:     my $nextnum = 0;
  925:     my $seqstem = '';
  926:     if ($context eq 'CSTR') {
  927:         $seqstem = "/res/$udom/$uname/$newdir";
  928:     } elsif ($context eq 'DOCS') {
  929:         $seqstem = '/uploaded/'.$cdom.'/'.$crs.'/'.$timenow;
  930:     }
  931:     my %seqnames = (
  932:                   boards => 'bulletinboards',
  933:                   quizzes => 'quizzes',
  934:                   surveys => 'surveys',
  935:                   announcements => 'announcements',
  936:                   pools => 'pools'
  937:                   );
  938:     my %seqtitles = (
  939:                   boards => 'Course Bulletin Boards',
  940:                   quizzes => 'Course Quizzes',
  941:                   surveys => 'Course Surveys',
  942:                   announcements => 'Course Announcements',
  943:                   pools => 'Course Question Pools'
  944:                    );
  945:     $$topnum ++;
  946: 
  947:     if ($type eq 'announcements') {
  948:         $src = "$seqstem/pages/$seqnames{$type}.page";
  949:     } else {
  950:         $src = "$seqstem/sequences/$seqnames{$type}.sequence";
  951:     }
  952: 
  953:     push @{$topurls}, $src;
  954:     push @{$topnames}, $seqtitles{$type};
  955: 
  956:     $$seqtext .= qq|<resource id="$$topnum" src="$src" title="$seqtitles{$type}"|;
  957:     $nextnum = $$topnum +1;
  958:     if ($$topnum == 1) {
  959:         $$seqtext .= qq| type="start"></resource>
  960: <link from="$$topnum" to="$nextnum" index="$$topnum"></link>\n|;
  961:         if ($$topnum == $contentscount) {
  962:             $$seqtext .= qq|<resource id="$nextnum" src="" type="finish"></resource>\n|;
  963:         }
  964:     } else {
  965:         if ($$topnum == $contentscount) {
  966:             $$seqtext .= qq| type="finish"></resource>\n|;
  967:         } else {
  968:             $$seqtext .= qq|></resource>
  969: <link from="$$topnum" to="$nextnum" index="$$topnum"></link>\n|;
  970:         }
  971:     }
  972: 
  973:     if ($type eq "announcements") {
  974:         push @{$pagesfiles}, "$seqnames{$type}.page";
  975:         open(ITEM,">$destdir/pages/$seqnames{$type}.page");
  976:     } else {
  977:         push @{$seqfiles}, "$seqnames{$type}.sequence";
  978:         open(ITEM,">$destdir/sequences/$seqnames{$type}.sequence");
  979:     }
  980: 
  981:     if ($type eq 'boards') {
  982:         $specialsrc = "/adm/$udom/$uname/$$timestamp[0]/bulletinboard";
  983:     } elsif ($type eq 'announcements') {
  984:         $specialsrc = "$seqstem/resfiles/$$specials[0].html";
  985:     } elsif ($type eq 'pools') {
  986:         $specialsrc = "$seqstem/sequences/$$specials[0].sequence";
  987:     } else {
  988:         $specialsrc = "$seqstem/pages/$$specials[0].page";
  989:     }
  990:     print ITEM qq|<map>
  991: <resource id="1" src="$specialsrc" title="$$resinfo{$$specials[0]}{title}" type="start"></resource>
  992: <link from="1" to="2" index="1"></link>|;
  993:     if (@{$specials} == 1) {
  994:         print ITEM qq|
  995: <resource id="2" src="" type="finish"></resource>\n|;
  996:     } else {
  997:         for (my $i=1; $i<@{$specials}; $i++) {
  998:             my $curr = $i+1;
  999:             my $next = $i+2;
 1000:             if ($type eq 'boards') {
 1001:                 $specialsrc = "/adm/$udom/$uname/$$timestamp[$i]/bulletinboard";
 1002:             } elsif ($type eq 'announcements') {
 1003:                 $specialsrc = "$seqstem/resfiles/$$specials[$i].html";
 1004:             } else {
 1005:                 $specialsrc = "$seqstem/pages/$$specials[$i].page";
 1006:             }
 1007:             print ITEM qq|<resource id="$curr" src="$specialsrc" title="$$resinfo{$$specials[$i]}{title}"|;
 1008:             if (@{$specials} == $i+1) {
 1009:                 print ITEM qq| type="finish"></resource>\n|;
 1010:             } else {
 1011:                 print ITEM qq|></resource>
 1012: <link from="$curr" to="$next" index="$next">\n|;
 1013:             }
 1014:         }
 1015:     }
 1016:     print ITEM qq|</map>|;
 1017:     close(ITEM);
 1018: }
 1019: 
 1020: # ---------------------------------------------------------------- Process Blackboard users
 1021: sub process_user {
 1022:   my ($res,$docroot,$destdir,$settings,$user_crs,$user_cdom,$user_handling) = @_;
 1023:   my $xmlfile = $docroot.'/'.$res.".dat";
 1024:   my $filecount = 0;
 1025:   my @state;
 1026:   my $userid = '';
 1027:   my $linknum = 0;
 1028: 
 1029:   my $p = HTML::Parser->new
 1030:     (
 1031:      xml_mode => 1,
 1032:      start_h =>
 1033:      [sub {
 1034:         my ($tagname, $attr) = @_;
 1035:         push @state, $tagname;
 1036:         if ("@state" eq "USERS USER") {
 1037:             $userid = $attr->{value};
 1038:             %{$$settings{$userid}} = ();
 1039:             @{$$settings{$userid}{links}} = ();
 1040:         } elsif ("@state" eq "USERS USER LOGINID") {  
 1041:             $$settings{$userid}{loginid} = $attr->{value};
 1042:         } elsif ("@state" eq "USERS USER PASSPHRASE") {  
 1043:             $$settings{$userid}{passphrase} = $attr->{value};
 1044:         } elsif ("@state" eq "USERS USER STUDENTID" ) {
 1045:             $$settings{$userid}{studentid} = $attr->{value};
 1046:         } elsif ("@state" eq "USERS USER NAMES FAMILY" ) {
 1047:             $$settings{$userid}{family} = $attr->{value};
 1048:         } elsif ("@state" eq "USERS USER NAMES GIVEN" ) {
 1049:             $$settings{$userid}{given} = $attr->{value};
 1050:         } elsif ("@state" eq "USERS USER ADDRESSES BUSINESS DATA EMAIL") {
 1051:             $$settings{$userid}{email} = $attr->{value};
 1052:         } elsif ("@state" eq "USERS USER USER_ROLE") {
 1053:             $$settings{$userid}{user_role} = $attr->{value};
 1054:         } elsif ("@state" eq "USERS USER FLAGS ISAVAILABLE") {
 1055:             $$settings{$userid}{isavailable} = $attr->{value};
 1056:         } elsif ("@state" eq "USERS USER PERSONALPAGE FILELIST IMAGE") {
 1057:             $$settings{$userid}{image} = $attr->{value};
 1058:         } elsif ( ($state[-2] eq "LINKLIST") && ($state[-1] eq "LINK") ) {
 1059:             %{$$settings{$userid}{links}[$linknum]} = ();
 1060:             $$settings{$userid}{links}[$linknum]{url} = $attr->{value};
 1061:             $linknum ++;
 1062:         }
 1063:      }, "tagname, attr"],
 1064:      text_h =>
 1065:      [sub {
 1066:         my ($text) = @_;
 1067:         if ("@state" eq "USERS USER PERSONALPAGE TITLE") {
 1068:             $$settings{$userid}{title} = $text;
 1069:         } elsif ("@state" eq "USERS USER PERSONALPAGE DESCRIPTION") {
 1070:             $$settings{$userid}{description} = $text;
 1071:         } elsif (($state[-2] eq "LINK") && ($state[-1] eq "TITLE")) {
 1072:             $$settings{$userid}{links}[$linknum]{title} = $text;
 1073:         } elsif (($state[-3] eq "LINK") && ($state[-2] eq  "DESCRIPTION") && ($state[-1] eq "TEXT")) {
 1074:             $$settings{$userid}{links}[$linknum]{text} = $text;
 1075:         }
 1076:       }, "dtext"],
 1077:      end_h =>
 1078:      [sub {
 1079:         my ($tagname) = @_;
 1080:         if ("@state" eq "USERS USER") {
 1081:             $linknum = 0;
 1082:         }
 1083:         pop @state;
 1084:      }, "tagname"],
 1085:     );
 1086:   $p->unbroken_text(1);
 1087:   $p->parse_file($xmlfile);
 1088:   $p->eof;
 1089:   
 1090:   my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
 1091:   my $xmlstem =  $$configvars{'lonDaemons'}."/tmp/".$user_cdom."_".$user_crs."_";
 1092: 
 1093:   foreach my $user_id (keys %{$settings}) {
 1094:       if ($$settings{$user_id}{user_role} eq "s") {
 1095:            
 1096:       } elsif ($user_handling eq 'enrollall') {
 1097: 
 1098:       }
 1099:   }
 1100: }
 1101: 
 1102: # ---------------------------------------------------------------- Process Blackboard groups
 1103: sub process_group {  
 1104:   my ($res,$docroot,$destdir,$settings) = @_;
 1105:   my $xmlfile = $docroot.'/'.$res.".dat";
 1106:   my $filecount = 0;
 1107:   my @state;
 1108:   my $grp;
 1109: 
 1110:   my $p = HTML::Parser->new
 1111:     (
 1112:      xml_mode => 1,
 1113:      start_h =>
 1114:      [sub {
 1115:         my ($tagname, $attr) = @_;
 1116:         push @state, $tagname;
 1117:         if ("@state" eq "GROUPS GROUP") {
 1118:             $grp = $attr->{id};
 1119:         }        
 1120:         if ("@state" eq "GROUPS GROUP TITLE") {
 1121:             $$settings{$grp}{title} = $attr->{value};
 1122:         } elsif ("@state" eq "GROUPS GROUP FLAGS ISAVAILABLE") {  
 1123:             $$settings{$grp}{isavailable} = $attr->{value};
 1124:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASCHATROOM") {  
 1125:             $$settings{$grp}{chat} = $attr->{value};
 1126:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASDISCUSSIONBOARD") {
 1127:             $$settings{$grp}{discussion} = $attr->{value};
 1128:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASTRANSFERAREA") {
 1129:             $$settings{$grp}{transfer} = $attr->{value};
 1130:         } elsif ("@state" eq "GROUPS GROUP FLAGS ISPUBLIC") {
 1131:             $$settings{$grp}{public} = $attr->{value};
 1132:         }
 1133:      }, "tagname, attr"],
 1134:      text_h =>
 1135:      [sub {
 1136:         my ($text) = @_;
 1137:         if ("@state" eq "GROUPS DESCRIPTION") {
 1138:           $$settings{$grp}{description} = $text;
 1139: #          print "Staff text is $text\n";
 1140:         }
 1141:       }, "dtext"],
 1142:      end_h =>
 1143:      [sub {
 1144:         my ($tagname) = @_;
 1145:         pop @state;
 1146:      }, "tagname"],
 1147:     );
 1148:   $p->unbroken_text(1);
 1149:   $p->parse_file($xmlfile);
 1150:   $p->eof;
 1151: }
 1152: 
 1153: # ---------------------------------------------------------------- Process Blackboard Staff
 1154: sub process_staff {
 1155:   my ($res,$docroot,$dirname,$destdir,$settings,$resrcfiles) = @_;
 1156:   my $xmlfile = $docroot.'/'.$res.".dat";
 1157:   my $filecount = 0;
 1158:   my @state;
 1159:   %{$$settings{name}} = ();
 1160:   %{$$settings{office}} = ();  
 1161: 
 1162:   my $p = HTML::Parser->new
 1163:     (
 1164:      xml_mode => 1,
 1165:      start_h =>
 1166:      [sub {
 1167:         my ($tagname, $attr) = @_;
 1168:         push @state, $tagname;
 1169:         if ("@state" eq "STAFFINFO TITLE") {
 1170:             $$settings{title} = $attr->{value};
 1171:         } elsif ("@state" eq "STAFFINFO BIOGRAPHY TEXTCOLOR") {
 1172:             $$settings{textcolor} = $attr->{value};
 1173:         } elsif ("@state" eq "STAFFINFO BIOGRAPHY FLAGS ISHTML") {
 1174:             $$settings{ishtml} = $attr->{value};
 1175:         } elsif ("@state" eq "STAFFINFO FLAGS ISAVAILABLE" ) {
 1176:             $$settings{isavailable} = $attr->{value};
 1177:         } elsif ("@state" eq "STAFFINFO FLAGS ISFOLDER" ) {
 1178:             $$settings{isfolder} = $attr->{value};
 1179:         } elsif ("@state" eq "STAFFINFO POSITION" ) {
 1180:             $$settings{position} = $attr->{value};
 1181:         } elsif ("@state" eq "STAFFINFO HOMEPAGE" ) {
 1182:             $$settings{homepage} = $attr->{value};
 1183:         } elsif ("@state" eq "STAFFINFO IMAGE") {
 1184:             $$settings{image} = $attr->{value};
 1185:         }
 1186:      }, "tagname, attr"],
 1187:      text_h =>
 1188:      [sub {
 1189:         my ($text) = @_;
 1190:         if ("@state" eq "STAFFINFO BIOGRAPHY TEXT") {
 1191:           $$settings{text} = $text;
 1192: #          print "Staff text is $text\n";
 1193:         } elsif ("@state" eq "STAFFINFO CONTACT PHONE") {
 1194:           $$settings{phone} = $text;
 1195:         } elsif ("@state" eq "STAFFINFO CONTACT EMAIL") {
 1196:           $$settings{email} = $text;
 1197:         } elsif ("@state" eq "STAFFINFO CONTACT NAME FORMALTITLE") {
 1198:           $$settings{name}{formaltitle} = $text;
 1199:         } elsif ("@state" eq "STAFFINFO CONTACT NAME FAMILY") {
 1200:           $$settings{name}{family} = $text;
 1201:         } elsif ("@state" eq "STAFFINFO CONTACT NAME GIVEN") {
 1202:           $$settings{name}{given} = $text;
 1203:         } elsif ("@state" eq "STAFFINFO CONTACT OFFICE HOURS") {
 1204:           $$settings{office}{hours} = $text;
 1205:         }  elsif ("@state" eq "STAFFINFO CONTACT OFFICE ADDRESS") {
 1206:           $$settings{office}{address} = $text;
 1207:         }        
 1208:       }, "dtext"],
 1209:      end_h =>
 1210:      [sub {
 1211:         my ($tagname) = @_;
 1212:         pop @state;
 1213:      }, "tagname"],
 1214:     );
 1215:   $p->unbroken_text(1);
 1216:   $p->parse_file($xmlfile);
 1217:   $p->eof;
 1218: 
 1219:     my $fontcol = '';
 1220:     if (defined($$settings{textcolor})) {
 1221:         $fontcol =  qq|color="$$settings{textcolor}"|;
 1222:     }
 1223:     if (defined($$settings{text})) {
 1224:         if ($$settings{ishtml} eq "true") {
 1225:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1226:         }
 1227:     }
 1228:     my $staffentry = qq|
 1229: <table border="0" cellpadding="0" cellspacing="0" width="100%">
 1230:   <tr>
 1231:     <td colspan="2"><hr /><font face="arial,helv" size="3"><b>$$settings{name}{formaltitle} $$settings{name}{given} $$settings{name}{family}</b></font>
 1232:     </td>
 1233:   </tr>
 1234:   <tr>
 1235:     <td valign="top">
 1236:       <table width="100% border="0" cols="2" cellpadding="0" cellspacing="0">|;
 1237:     if ( defined($$settings{email}) && $$settings{email} ne '') {
 1238:         $staffentry .= qq|
 1239:         <tr>
 1240:           <td width="100" valign="top">
 1241:            <font face="arial" size="2"><b>Email:</b></font>
 1242:           </td>
 1243:           <td>
 1244:            <font face="arial" size="2"><a href="mailto:$$settings{email}">$$settings{email}</a></font>
 1245:           </td>
 1246:         </tr>
 1247:         |;
 1248:     }
 1249:     if (defined($$settings{phone}) && $$settings{phone} ne '') {
 1250:         $staffentry .= qq|
 1251:         <tr>
 1252:           <td width="100" valign="top">
 1253:             <font face="arial" size="2"><b>Phone:</b></font>
 1254:           </td>
 1255:           <td>
 1256:             <font face="arial" size="2">$$settings{phone}</font>
 1257:           </td>
 1258:         </tr>
 1259:         |;
 1260:     }
 1261:     if (defined($$settings{office}{address}) && $$settings{office}{address} ne '') {
 1262:         $staffentry .= qq|
 1263:         <tr>
 1264:          <td width="100" valign="top">
 1265:            <font face="arial" size="2"><b>Address:</b></font>
 1266:          </td>
 1267:          <td>
 1268:            <font face="arial" size="2">$$settings{office}{address}</font>
 1269:          </td>
 1270:         </tr>
 1271:         |;
 1272:     }
 1273:     if (defined($$settings{office}{hours}) && $$settings{office}{hours} ne '') {
 1274:         $staffentry .= qq|
 1275:         <tr>
 1276:           <td width="100" valign="top">
 1277:             <font face="arial" size="2"><b>Office Hours:</b></font>
 1278:           </td>
 1279:           <td>
 1280:             <font face=arial size=2>$$settings{office}{hours}</font>
 1281:           </td>
 1282:         </tr>
 1283:         |;
 1284:     }
 1285:     if ( defined($$settings{homepage}) && $$settings{homepage} ne '') {
 1286:         $staffentry .= qq|
 1287:         <tr>
 1288:           <td width="100" valign="top">
 1289:             <font face="arial" size="2"><b>Personal Link:</b></font>
 1290:           </td>
 1291:           <td>
 1292:             <font face="arial" size="2"><a href="$$settings{homepage}">$$settings{homepage}</a></font>
 1293:           </td>
 1294:         </tr>
 1295:         |;
 1296:     }
 1297:     if (defined($$settings{text}) && $$settings{text} ne '') {
 1298:         $staffentry .= qq|
 1299:         <tr>
 1300:           <td colspan="2">
 1301:             <font face="arial" size="2" $fontcol><b>Other Information:</b><br/>$$settings{text}</font>
 1302:           </td>
 1303:         </tr>
 1304:         |;
 1305:      }
 1306:      $staffentry .= qq|
 1307:       </table>
 1308:     </td>
 1309:     <td align="right" valign="top">
 1310:      |;
 1311:      if ( defined($$settings{image}) ) {
 1312:          $staffentry .= qq|
 1313:       <img src="$dirname/resfiles/$res/$$settings{image}">
 1314:          |;
 1315:      }
 1316:      $staffentry .= qq|
 1317:     </td>
 1318:   </tr>
 1319: </table>
 1320:     |;
 1321:     open(FILE,">$destdir/resfiles/$res.html");
 1322:     push @{$resrcfiles}, "$res.html";
 1323:     print FILE qq|<html>
 1324: <head>
 1325: <title>$$settings{title}</title>
 1326: </head>
 1327: <body bgcolor='#ffffff'>
 1328: $staffentry
 1329: </body>
 1330: </html>|;
 1331:     close(FILE);
 1332: }
 1333: 
 1334: # ---------------------------------------------------------------- Process Blackboard Links
 1335: sub process_link {
 1336:     my ($res,$docroot,$dirname,$destdir,$settings,$resrcfiles) = @_;
 1337:     my $xmlfile = $docroot.'/'.$res.".dat";
 1338:     my @state = ();
 1339:     my $p = HTML::Parser->new
 1340:     (
 1341:         xml_mode => 1,
 1342:         start_h =>
 1343:         [sub {
 1344:             my ($tagname, $attr) = @_;
 1345:             push @state, $tagname;
 1346:             if ("@state" eq "EXTERNALLINK TITLE") {
 1347:                 $$settings{title} = $attr->{value};
 1348:             } elsif ("@state" eq "EXTERNALLINK TEXTCOLOR") {  
 1349:                 $$settings{textcolor} = $attr->{value};
 1350:             } elsif ("@state" eq "EXTERNALLINK DESCRIPTION FLAGS ISHTML") {  
 1351:                 $$settings{ishtml} = $attr->{value};
 1352:             } elsif ("@state" eq "EXTERNALLINK FLAGS ISAVAILABLE" ) {
 1353:                 $$settings{isavailable} = $attr->{value};
 1354:             } elsif ("@state" eq "EXTERNALLINK FLAGS LAUNCHINNEWWINDOW" ) {
 1355:                 $$settings{newwindow} = $attr->{value};
 1356:             } elsif ("@state" eq "EXTERNALLINK FLAGS ISFOLDER" ) {
 1357:                 $$settings{isfolder} = $attr->{value};
 1358:             } elsif ("@state" eq "EXTERNALLINK POSITION" ) {
 1359:                 $$settings{position} = $attr->{value};
 1360:             } elsif ("@state" eq "EXTERNALLINK URL" ) {
 1361:                 $$settings{url} = $attr->{value};
 1362:             }
 1363:         }, "tagname, attr"],
 1364:         text_h =>
 1365:         [sub {
 1366:             my ($text) = @_;
 1367:             if ("@state" eq "EXTERNALLINK DESCRIPTION TEXT") {
 1368:                $$settings{text} = $text;
 1369:             }
 1370:         }, "dtext"],
 1371:         end_h =>
 1372:         [sub {
 1373:             my ($tagname) = @_;
 1374:             pop @state;
 1375:         }, "tagname"],
 1376:     );
 1377:     $p->unbroken_text(1);
 1378:     $p->parse_file($xmlfile);
 1379:     $p->eof;
 1380: 
 1381:     my $linktag = '';
 1382:     my $fontcol = '';
 1383:     if (defined($$settings{textcolor})) {
 1384:         $fontcol =  qq|<font color="$$settings{textcolor}">|;
 1385:     }
 1386:     if (defined($$settings{text})) {
 1387:         if ($$settings{ishtml} eq "true") {
 1388:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1389:         }
 1390:     }
 1391: 
 1392:     if (defined($$settings{url}) ) {
 1393:         $linktag = qq|<a href="$$settings{url}"|;
 1394:         if ($$settings{newwindow} eq "true") {
 1395:             $linktag .= qq| target="launch"|;
 1396:         }
 1397:         $linktag .= qq|>$$settings{title}</a>|;
 1398:     }
 1399: 
 1400:     open(FILE,">$destdir/resfiles/$res.html");
 1401:     push @{$resrcfiles}, "$res.html";
 1402:     print FILE qq|<html>
 1403: <head>
 1404: <title>$$settings{title}</title>
 1405: </head>
 1406: <body bgcolor='#ffffff'>
 1407: $fontcol
 1408: $linktag
 1409: $$settings{text}
 1410: |;
 1411:     if (defined($$settings{textcolor})) {
 1412:         print FILE qq|</font>|;
 1413:     }
 1414:     print FILE qq|
 1415:   </body>
 1416:  </html>|;
 1417:     close(FILE);
 1418: }
 1419: 
 1420: # ---------------------------------------------------------------- Process Blackboard Discussion Boards
 1421: sub process_db {
 1422:     my ($res,$docroot,$destdir,$timestamp,$crs,$cdom,$handling,$uname,$settings,$longcrs) = @_;
 1423:     my $xmlfile = $docroot.'/'.$res.".dat";
 1424:     my @state = ();
 1425:     my @allmsgs = ();
 1426:     my %msgidx = ();
 1427:     my %threads; # all threads, keyed by message ID
 1428:     my $msg_id; # the current message ID
 1429:     my %message; # the current message being accumulated for $msg_id
 1430: 
 1431:     my $p = HTML::Parser->new
 1432:     (
 1433:        xml_mode => 1,
 1434:        start_h =>
 1435:        [sub {
 1436:            my ($tagname, $attr) = @_;
 1437:            push @state, $tagname;
 1438:            my $depth = 0;
 1439:            my @seq = ();
 1440:            if ("@state" eq "FORUM TITLE") {
 1441:                $$settings{title} = $attr->{value};
 1442:            } elsif ("@state" eq "FORUM DESCRIPTION TEXTCOLOR") {  
 1443:                $$settings{textcolor} = $attr->{value};
 1444:            } elsif ("@state" eq "FORUM DESCRIPTION FLAGS ISHTML") {  
 1445:                $$settings{ishtml} = $attr->{value};
 1446:            } elsif ("@state" eq "FORUM DESCRIPTION FLAGS ISNEWLINELITERAL") {  
 1447:                $$settings{newline} = $attr->{value};
 1448:            } elsif ("@state" eq "FORUM POSITION" ) {
 1449:                $$settings{position} = $attr->{value};
 1450:            } elsif ("@state" eq "FORUM FLAGS ISREADONLY") {
 1451:                $$settings{isreadonly} = $attr->{value};
 1452:            } elsif ("@state" eq "FORUM FLAGS ISAVAILABLE" ) {
 1453:                $$settings{isavailable} = $attr->{value};
 1454:            } elsif ("@state" eq "FORUM FLAGS ALLOWANONYMOUSPOSTINGS" ) {
 1455:                $$settings{allowanon} = $attr->{value};
 1456:            } elsif ( ($state[0] eq "FORUM") && ($state[1] eq "MESSAGETHREADS") && ($state[2] eq "MSG") ) {
 1457:                if ($state[-1] eq "MSG") {
 1458:                    unless ($msg_id eq '') {
 1459:                        push @{$threads{$msg_id}}, { %message };
 1460:                        $depth = @state - 3;
 1461:                        if ($depth > @seq) {
 1462:                            push @seq, $msg_id; 
 1463:                        }
 1464:                    }
 1465:                    if ($depth < @seq) {
 1466:                        pop @seq;
 1467:                    }                
 1468:                    $msg_id = $attr->{id};
 1469:                    push @allmsgs, $msg_id;
 1470:                    $msgidx{$msg_id} = @allmsgs;
 1471:                    %message = ();
 1472:                    $message{depth} = $depth;
 1473:                    if ($depth > 0) {
 1474:                        $message{parent} = $seq[-1];
 1475:                    } else {
 1476:                        $message{parent} = "None";
 1477:                    }
 1478:                } elsif ($state[-1] eq "TITLE") {
 1479:                    $message{title} = $attr->{value};
 1480:                } elsif ( ( $state[-3] eq "MESSAGETEXT" ) && ( $state[-2] eq "FLAGS" ) && ( $state[-1] eq "ISHTML" ) ) {
 1481:                    $message{ishtml} = $attr->{value};
 1482:                } elsif ( ( $state[-3] eq "MESSAGETEXT" ) && ( $state[-2] eq "FLAGS" ) && ( $state[-1] eq "ISNEWLINELITERAL" ) ) {
 1483:                    $message{newline} = $attr->{value};
 1484:                } elsif ( ( $state[-2] eq "DATES" ) && ( $state[-1] eq "CREATED" ) ) {
 1485:                    $message{created} = $attr->{value};
 1486:                } elsif ( $state[@state-2] eq "FLAGS") {
 1487:                    if ($state[@state-1] eq "ISANONYMOUS") {
 1488:                        $message{isanonymous} =  $attr->{value};
 1489:                    }
 1490:                } elsif ( $state[-2] eq "USER" ) {
 1491:                    if ($state[-1] eq "USERID") {
 1492:                        $message{userid} =  $attr->{value};
 1493:                    } elsif ($state[@state-1] eq "USERNAME") {
 1494:                        $message{username} =  $attr->{value};
 1495:                    } elsif ($state[@state-1] eq "EMAIL") {
 1496:                        $message{email} =  $attr->{value};
 1497:                    }          
 1498:                } elsif ( ($state[-2] eq "FILELIST") && ($state[-1] eq "IMAGE") ) {
 1499:                    $message{attachment} = $attr->{value};
 1500:                }
 1501:            }
 1502:        }, "tagname, attr"],
 1503:        text_h =>
 1504:        [sub {
 1505:            my ($text) = @_;
 1506:            if ("@state" eq "FORUM DESCRIPTION TEXT") {
 1507:                $$settings{text} = $text;
 1508:            } elsif ( ($state[0] eq "FORUM") && ($state[1] eq "MESSAGETHREADS") && ($state[2] eq "MSG") ) {
 1509:                if ( ($state[-2] eq "MESSAGETEXT") && ($state[-1] eq "TEXT") ){
 1510:                    $message{text} = $text;
 1511:                }
 1512:            }
 1513:        }, "dtext"],
 1514:        end_h =>
 1515:        [sub {
 1516:            my ($tagname) = @_;
 1517:            if ( $state[-1] eq "MESSAGETHREADS" ) {
 1518:                push @{$threads{$msg_id}}, { %message };
 1519:            }
 1520:            pop @state;
 1521:        }, "tagname"],
 1522:     );
 1523:     $p->unbroken_text(1);
 1524:     $p->parse_file($xmlfile);
 1525:     $p->eof;
 1526: 
 1527:     if (defined($$settings{text})) {
 1528:         if ($$settings{ishtml} eq "false") {
 1529:             if ($$settings{isnewline} eq "true") {
 1530:                 $$settings{text} =~ s#\n#<br/>#g;
 1531:             }
 1532:         } else {
 1533:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1534:         }
 1535:         if (defined($$settings{fontcolor}) ) {
 1536:             $$settings{text} = "<font color=\"".$$settings{textcolor}."\">".$$settings{text}."</font>";
 1537:         }
 1538:     }
 1539:     my $boardname = 'bulletinpage_'.$timestamp;
 1540:     my %boardinfo = (
 1541:                   'aaa_title' => $$settings{title},
 1542:                   'bbb_content' => $$settings{text},
 1543:                   'ccc_webreferences' => '',
 1544:                   'uploaded.lastmodified' => time,
 1545:                   );
 1546:   
 1547:     my $putresult = &Apache::lonnet::put($boardname,\%boardinfo,$cdom,$crs);
 1548:     if ($handling eq 'importall') {
 1549:         foreach my $msg_id (@allmsgs) {
 1550:             foreach my $message ( @{$threads{$msg_id}} ) {
 1551:                 my %contrib = (
 1552:                             'sendername' => $$message{userid},
 1553:                             'senderdomain' => $cdom,
 1554:                             'screenname' => '',
 1555:                             'plainname' => $$message{username},
 1556:                             );
 1557:                 unless ($$message{parent} eq 'None') {
 1558:                     $contrib{replyto} = $msgidx{$$message{parent}};
 1559:                 }
 1560:                 if (defined($$message{isanonymous}) ) {
 1561:                     if ($$message{isanonymous} eq 'true') {
 1562:                         $contrib{'anonymous'} = 'true';
 1563:                     }
 1564:                 }
 1565:                 if ( defined($$message{attachment}) )  {
 1566:                     my $url = $$message{attachment};
 1567:                     my $oldurl = $url;
 1568:                     my $newurl = $url;
 1569:                     unless ($url eq '') {
 1570:                         $newurl =~ s/\//_/g;
 1571:                         unless ($longcrs eq '') {
 1572:                             if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles") {
 1573:                                 mkdir("/home/httpd/lonUsers/$cdom/$longcrs/userfiles",0755);
 1574:                             }
 1575:                             if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl") {
 1576:                                 system("cp $destdir/resfiles/$res/$$message{attachment} /home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl");
 1577:                             }
 1578:                             $contrib{attachmenturl} = '/uploaded/'.$cdom.'/'.$crs.'/'.$newurl;
 1579:                         }
 1580:                     }
 1581:                 }
 1582:                 if (defined($$message{title}) ) {
 1583:                     $contrib{'message'} = $$message{title};
 1584:                 }
 1585:                 if (defined($$message{text})) {
 1586:                     if ($$message{ishtml} eq "false") {
 1587:                         if ($$message{isnewline} eq "true") {
 1588:                             $$message{text} =~ s#\n#<br/>#g;
 1589:                         }
 1590:                     } else {
 1591:                         $$message{text} = &HTML::Entities::decode($$message{text});
 1592:                     }
 1593:                     $contrib{'message'} .= '<br /><br />'.$$message{text};
 1594:                     my $symb = 'bulletin___'.$timestamp.'___adm/wrapper/adm/'.$cdom.'/'.$uname.'/'.$timestamp.'/bulletinboard';
 1595:                     my $postresult = &addposting($symb,\%contrib,$cdom,$crs);
 1596:                 }
 1597:             }
 1598:         }
 1599:     }
 1600: }
 1601: 
 1602: # ---------------------------------------------------------------- Add Posting to Bulletin Board
 1603: sub addposting {
 1604:     my ($symb,$contrib,$cdom,$crs)=@_;
 1605:     my $status='';
 1606:     if (($symb) && ($$contrib{message})) {
 1607:          my $crsdom = $cdom.'_'.$crs;
 1608:          &Apache::lonnet::store($contrib,$symb,$crsdom,$cdom,$crs);
 1609:          my %storenewentry=($symb => time);
 1610:          &Apache::lonnet::put('discussiontimes',\%storenewentry,$cdom,$crs);
 1611:     }
 1612:     my %record=&Apache::lonnet::restore('_discussion');
 1613:     my ($temp)=keys %record;
 1614:     unless ($temp=~/^error\:/) {
 1615:         my %newrecord=();
 1616:         $newrecord{'resource'}=$symb;
 1617:         $newrecord{'subnumber'}=$record{'subnumber'}+1;
 1618:         &Apache::lonnet::cstore(\%newrecord,'_discussion');
 1619:         $status = 'ok';
 1620:     } else {
 1621:         $status.='Failed.';
 1622:     }
 1623:     return $status;
 1624: }
 1625: 
 1626: sub parse_bb5_assessment {
 1627:     my ($res,$docroot,$container,$settings,$allanswers,$allchoices,$allids) = @_;
 1628:     my $xmlfile = $docroot.'/'.$res.".dat";
 1629: #  print "XML file is $xmlfile\n";
 1630:     my @state = ();
 1631:     my $id; # the current question ID
 1632:     my $answer_id; # the current answer ID
 1633:     my %toptag = ( pool => 'POOL',
 1634:                  quiz => 'ASSESSMENT',
 1635:                  survey => 'ASSESSMENT'
 1636:                );
 1637: 
 1638:     my $p = HTML::Parser->new
 1639:     (
 1640:      xml_mode => 1,
 1641:      start_h =>
 1642:      [sub {
 1643:         my ($tagname, $attr) = @_;
 1644:         push @state, $tagname;
 1645:         my $depth = 0;
 1646:         my @seq = ();
 1647:         my $class;
 1648:         my $state_str = join(" ",@state);
 1649:         if ($container eq "pool") {
 1650:             if ("@state" eq "POOL TITLE") {
 1651:                 $$settings{title} = $attr->{value};
 1652:             }
 1653:         } else {
 1654:             if ("@state" eq "ASSESSMENT TITLE") {  
 1655:                 $$settings{title} = $attr->{value};          
 1656:             } elsif ("@state" eq "ASSESSMENT FLAG" ) {
 1657:                 $$settings{isnewline} = $attr->{value};
 1658:             } elsif ("@state" eq "ASSESSMENT FLAGS ISAVAILABLE") {
 1659:                 $$settings{isavailable} = $attr->{value};
 1660:             } elsif ("@state" eq "ASSESSMENT FLAGS ISANONYMOUS" ) {
 1661:                 $$settings{isanonymous} = $attr->{id};
 1662:             } elsif ("@state" eq "ASSESSMENT FLAGS GIVE FEEDBACK" ) {
 1663:                 $$settings{feedback} = $attr->{id};        
 1664:             } elsif ("@state" eq "ASSESSMENT FLAGS SHOWCORRECT" ) {
 1665:                 $$settings{showcorrect} = $attr->{id};        
 1666:             } elsif ("@state" eq "ASSESSMENT FLAGS SHOWRESULTS" ) {
 1667:                 $$settings{showresults} = $attr->{id};        
 1668:             } elsif ("@state" eq "ASSESSMENT FLAGS ALLOWMULTIPLE" ) {
 1669:                 $$settings{allowmultiple} = $attr->{id};        
 1670:             } elsif ("@state" eq "ASSESSMENT ASSESSMENTTYPE" ) {
 1671:                 $$settings{type} = $attr->{id};        
 1672:             }
 1673:         }    
 1674:         if ("@state" eq "$toptag{$container} QUESTIONLIST QUESTION") {  
 1675:             $id = $attr->{id};
 1676:             push @{$allids}, $id;
 1677:             %{$$settings{$id}} = ();
 1678:             @{$$allanswers{$id}} = ();
 1679:             $$settings{$id}{class} = $attr->{class};
 1680:             unless ($container eq "pool") {
 1681:                 $$settings{$id}{points} = $attr->{points};
 1682:             }
 1683:             @{$$settings{$id}{correctanswer}} = ();                              
 1684:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[-1] =~ m/^QUESTION_(\w+)$/) ) {
 1685:             $id = $attr->{id};
 1686:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "BODY") && ($state[3] eq "FLAGS") ) {
 1687:             if ($state[4] eq "ISHTML") {
 1688:                 $$settings{$id}{html} = $attr->{value};
 1689:             } elsif ($state[4] eq "ISNEWLINELITERAL") {
 1690:                 $$settings{$id}{newline} = $attr->{value};
 1691:             }
 1692:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "IMAGE") ) {
 1693:             $$settings{$id}{image} = $attr->{value};
 1694:             $$settings{$id}{style} = $attr->{style};
 1695:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "URL") ) {
 1696:             $$settings{$id}{url} = $attr->{value};
 1697:             $$settings{$id}{name} = $attr->{name};
 1698:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[-1] eq "ANSWER") ) {
 1699:             $answer_id = $attr->{id};
 1700:             push @{$$allanswers{$id}},$answer_id;
 1701:             %{$$settings{$id}{$answer_id}} = ();
 1702:             $$settings{$id}{$answer_id}{position} = $attr->{position};
 1703:             if ($$settings{$id}{class} eq 'QUESTION_MATCH') {
 1704:                 $$settings{$id}{$answer_id}{placement} = $attr->{placement};
 1705:                 $$settings{$id}{$answer_id}{type} = 'answer';
 1706:             }
 1707:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[-1] eq "CHOICE") ) {
 1708:             $answer_id = $attr->{id};
 1709:             push @{$$allchoices{$id}},$answer_id; 
 1710:             %{$$settings{$id}{$answer_id}} = ();
 1711:             $$settings{$id}{$answer_id}{position} = $attr->{position};
 1712:             $$settings{$id}{$answer_id}{placement} = $attr->{placement};
 1713:             $$settings{$id}{$answer_id}{type} = 'choice';
 1714:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "ANSWER") ) {
 1715:             if ($state[3] eq "IMAGE") {
 1716:                 $$settings{$id}{$answer_id}{image} = $attr->{value};
 1717:                 $$settings{$id}{$answer_id}{style} = $attr->{style};
 1718:             } elsif ($state[3] eq "URL") {
 1719:                 $$settings{$id}{$answer_id}{url} = $attr->{value};
 1720:             }
 1721:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "CHOICE") ) {
 1722:             if ($state[3] eq "IMAGE") {
 1723:                 $$settings{$id}{$answer_id}{image} = $attr->{value};
 1724:                 $$settings{$id}{$answer_id}{style} = $attr->{style};
 1725:             } elsif ($state[3] eq "URL") {
 1726:                 $$settings{$id}{$answer_id}{url} = $attr->{value};
 1727:             }
 1728:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[3] eq "CORRECTANSWER") ) {
 1729:             my $corr_answer = $attr->{answer_id};
 1730:             push @{$$settings{$id}{correctanswer}}, $corr_answer;
 1731:             my $type = $1;
 1732:             if ($type eq 'TRUEFALSE') {
 1733:                 $$settings{$id}{$corr_answer}{answer_position} = $attr->{position};
 1734:             } elsif ($type eq 'ORDER') {
 1735:                 $$settings{$id}{$corr_answer}{order} = $attr->{order};
 1736:             } elsif ($type eq 'MATCH') {
 1737:                 $$settings{$id}{$corr_answer}{choice_id} = $attr->{choice_id};
 1738:             }
 1739:         }
 1740:      }, "tagname, attr"],
 1741:      text_h =>
 1742:      [sub {
 1743:         my ($text) = @_;
 1744:         unless ($container eq "pool") {        
 1745:             if ("@state" eq "ASSESSMENT DESCRIPTION TEXT") {
 1746:                 $$settings{description} = $text;
 1747:             } elsif ("@state" eq "ASSESSMENT INSTRUCTIONS ") {
 1748:                 $$settings{instructions}{text} = $text;
 1749:             }
 1750:         }
 1751:         if ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "BODY") && ($state[3] eq "TEXT") ) {
 1752:             $$settings{$id}{text} = $text;
 1753:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "ANSWER") && ($state[3] eq "TEXT") ) {
 1754:             $$settings{$id}{$answer_id}{text} = $text;
 1755:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "CHOICE") && ($state[3] eq "TEXT") ) {
 1756:             $$settings{$id}{$answer_id}{text} = $text;            
 1757:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[3] eq "FEEDBACK_WHEN_CORRECT") ) {
 1758:             $$settings{$id}{feedback_corr} = $text;
 1759:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[3] eq "FEEDBACK_WHEN_INCORRECT") ) {
 1760:             $$settings{$id}{feedback_incorr} = $text;       
 1761:         }
 1762:       }, "dtext"],
 1763:      end_h =>
 1764:      [sub {
 1765:         my ($tagname) = @_;
 1766:         pop @state;
 1767:      }, "tagname"],
 1768:     );
 1769:     $p->unbroken_text(1);
 1770:     $p->parse_file($xmlfile);
 1771:     $p->eof;
 1772: }
 1773: 
 1774: sub parse_bb6_assessment {
 1775:      my ($res,$docroot,$container,$settings,$allanswers,$allchoices,$allids) = @_;
 1776:      return;
 1777: }
 1778: 
 1779: sub parse_webct4_assessment {
 1780:     my ($res,$docroot,$container,$allids) = @_;
 1781:     my $xmlfile = $docroot.'/quiz'.$res.".xml"; #quiz file
 1782:     print STDERR "XML file is $xmlfile\n";
 1783:     my @state = ();
 1784:     my $id; # the current question ID
 1785:     my $p = HTML::Parser->new
 1786:     (
 1787:      xml_mode => 1,
 1788:      start_h =>
 1789:      [sub {
 1790:         my ($tagname, $attr) = @_;
 1791:         push @state, $tagname;
 1792:         my $depth = 0;
 1793:         my @seq = ();
 1794:         my $class;
 1795:         if ("@state" eq "questestinterop assessment section itemref") {
 1796:             $id = $attr->{linkrefid}; 
 1797:             push(@{$allids},$id);
 1798:         }
 1799:      }, "tagname, attr"],
 1800:      text_h =>
 1801:      [sub {
 1802:         my ($text) = @_;
 1803:       }, "dtext"],
 1804:      end_h =>
 1805:      [sub {
 1806:         my ($tagname) = @_;
 1807:         pop @state;
 1808:      }, "tagname"],
 1809:     );
 1810:     $p->unbroken_text(1);
 1811:     $p->parse_file($xmlfile);
 1812:     $p->eof;
 1813: }
 1814: 
 1815: sub parse_webct_quizprops {
 1816:     my ($res,$docroot,$container,$qzparams) = @_;
 1817:     my $xmlfile = $docroot.'/quiz'.$res.".xml"; #quiz file
 1818:     print STDERR "XML file is $xmlfile\n";
 1819:     my @state = ();
 1820:     %{$$qzparams{$res}} = ();
 1821:     my $p = HTML::Parser->new
 1822:     (
 1823:      xml_mode => 1,
 1824:      start_h =>
 1825:      [sub {
 1826:         my ($tagname, $attr) = @_;
 1827:         push @state, $tagname;
 1828:         my $depth = 0;
 1829:         my @seq = ();
 1830:         my $class;
 1831:         if ($state[0] eq 'properties' && $state[1] eq 'processing')  {
 1832:             if ($state[2] eq 'scores' && $state[3] eq 'score') {
 1833:                 $$qzparams{$res}{weight} = $attr->{linkrefid};
 1834:             } elsif ($state[2] eq 'selection' && $state[3] eq 'select') {
 1835:                 $$qzparams{$res}{numpick} = $attr->{linkrefid};      
 1836:             }
 1837:         }
 1838:      }, "tagname, attr"],
 1839:      text_h =>
 1840:      [sub {
 1841:         my ($text) = @_;
 1842:         if ($state[0] eq 'properties' && $state[1] eq 'delivery')  {
 1843:              if ($state[2] eq 'time_available') {
 1844:                 $$qzparams{$res}{opendate} = $text;
 1845:              } elsif ($state[2] eq 'time_due') {
 1846:                 $$qzparams{$res}{opendate} = $text;
 1847:              } elsif ($state[3] eq 'max_attempt') {
 1848:                 $$qzparams{$res}{tries} = $text;
 1849:              } elsif ($state[3] eq 'post_submission') {
 1850:                 $$qzparams{$res}{posts} = $text;
 1851:              }
 1852:         } elsif ($state[0] eq 'properties' && $state[1] eq 'result') {
 1853:              if ($state[2] eq 'display_answer') {
 1854:                 $$qzparams{$res}{answerdate} = $text;
 1855:              }
 1856:         } 
 1857:       }, "dtext"],
 1858:      end_h =>
 1859:      [sub {
 1860:         my ($tagname) = @_;
 1861:         pop @state;
 1862:      }, "tagname"],
 1863:     );
 1864:     $p->unbroken_text(1);
 1865:     $p->parse_file($xmlfile);
 1866:     $p->eof;
 1867: }
 1868: 
 1869: sub parse_webct4_questionDB {
 1870:     my ($docroot,$catinfo,$settings,$allanswers,$allchoices,$allids) = @_;
 1871:     my $xmlfile = $docroot.'/questionDB.xml'; #quizDB file
 1872:     print STDERR "XML file is $xmlfile\n";
 1873:     my @state = ();
 1874:     my $category; # the current category ID
 1875:     my $id; # the current question ID
 1876:     my $list; # the current list ID for multiple choice questions
 1877:     my $numid; # the current answer ID for numerical questions
 1878:     my $grp; # the current group ID for matching questions
 1879:     my $label; # the current reponse label for string questions 
 1880:     my $str_id; # the current string ID for string questions
 1881:     my $unitid; # the current unit ID for numerical questions
 1882:     my $answer_id; # the current answer ID
 1883:     my $fdbk; # the current feedback ID
 1884:     my $maxvar; # the current max variable for numerical problems
 1885:     my $minvar; # the current min variable for numerical problems
 1886:     my $stepvar; # the current step variable for numerical problems
 1887:     my $fibtype; # the current fill-in-blank type fro numerical or string
 1888:     my $prompt; 
 1889:     my %setvar = (
 1890:                    varname => '',
 1891:                    action => '',
 1892:                  );  
 1893:     my $p = HTML::Parser->new
 1894:     (
 1895:      xml_mode => 1,
 1896:      start_h =>
 1897:      [sub {
 1898:         my ($tagname, $attr) = @_;
 1899:         push @state, $tagname;
 1900:         if ("@state" eq "questestinterop section") {
 1901:             $category = $attr->{ident};
 1902:             %{$$catinfo{$category}} = ();
 1903:             $$catinfo{$category}{title} = $attr->{title};   
 1904:         }
 1905:         if ("@state" eq "questestinterop section item") {
 1906:             $id = $attr->{ident};
 1907:             push @{$allids}, $id;
 1908:             push(@{$$catinfo{$category}{contents}},$id);
 1909:             %{$$settings{$id}} = ();
 1910:             @{$$settings{$id}{grps}} = ();
 1911:             @{$$settings{$id}{lists}} = ();
 1912:             @{$$settings{$id}{feedback}} = ();
 1913:             %{$$settings{$id}{strings}} = ();
 1914:             %{$$allanswers{$id}} = ();
 1915:             @{$$allanswers{$id}{strings}} = ();
 1916:             $$settings{$id}{title} = $attr->{title};
 1917:         }
 1918: 
 1919: # Matching
 1920:         if ("@state" eq "questestinterop section item presentation material mattext") {
 1921:             $$settings{$id}{texttype} = $attr->{texttype};
 1922:         }
 1923:         if ("@state" eq "questestinterop section item presentation response_grp") {
 1924:             $$settings{$id}{class} = 'QUESTION_MATCH';        
 1925:             $grp = $attr->{ident};
 1926:             push(@{$$settings{$id}{grps}},$grp);
 1927:             %{$$settings{$id}{$grp}} = ();
 1928:             @{$$allanswers{$id}{$grp}} = ();
 1929:             @{$$settings{$id}{$grp}{correctanswer}} = ();
 1930:             $$settings{$id}{$grp}{rcardinality} = $attr->{rcardinality};
 1931:         }
 1932:         if ("@state" eq "questestinterop section item presentation response_grp material mattext") { 
 1933:             $$settings{$id}{$grp}{texttype} = $attr->{texttype};
 1934:         }
 1935:         if ("@state" eq "questestinterop section item presentation response_grp render_choice response_label") {
 1936:             $answer_id = $attr->{ident};
 1937:             push(@{$$allanswers{$id}{$grp}},$answer_id);
 1938:             %{$$settings{$id}{$grp}{$answer_id}} = ();
 1939:             $$settings{$id}{$grp}{$answer_id}{texttype} =  $attr->{texttype};
 1940:         }
 1941: 
 1942: # Multiple choice
 1943: 
 1944:         if ("@state" eq "questestinterop section item presentation flow material mattext") {
 1945:             $$settings{$id}{texttype} = $attr->{texttype};
 1946:         }
 1947:         if ("@state" eq "questestinterop section item presentation flow response_lid") {
 1948:             $$settings{$id}{class} = 'QUESTION_MULTIPLEANSWER';
 1949:             $list = $attr->{ident};
 1950:             push(@{$$settings{$id}{lists}},$list);
 1951:             %{$$settings{$id}{$list}} = ();
 1952:             @{$$allanswers{$id}{$list}} = ();
 1953:             @{$$settings{$id}{$list}{correctanswer}} = ();
 1954:             $$settings{$id}{$list}{rcardinality} = $attr->{rcardinality};
 1955:         }
 1956:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice") {
 1957:             $$settings{$id}{$list}{randomize} = $attr->{shuffle};
 1958:         }
 1959:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice flow_label response_label") {
 1960:             $answer_id = $attr->{ident};
 1961:             push(@{$$allanswers{$id}{$list}},$answer_id);
 1962:             %{$$settings{$id}{$list}{$answer_id}} = ();
 1963:         }
 1964:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice flow_label response_label material mattext") {
 1965:             $$settings{$id}{$list}{$answer_id}{texttype} = $attr->{texttype};
 1966:         }
 1967: 
 1968: # Numerical
 1969:         if ("@state" eq "questestinterop section item presentation material mat_extension webct:x_webct_v01_dynamicmattext") {
 1970:             $$settings{$id}{texttype} = $attr->{texttype};
 1971:         }
 1972:         if ("@state" eq "questestinterop section item presentation response_num") {
 1973:             $$settings{$id}{class} = 'QUESTION_NUMERICAL';
 1974:             $numid = $attr->{ident};
 1975:             %{$$settings{$id}{$numid}} = ();
 1976:             $$settings{$id}{$numid}{rcardinality} = $attr->{rcardinality};
 1977:         }
 1978:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_minvalue webct:x_webct_v01_variable") {            
 1979:             $minvar = $attr->{name};
 1980:             $$settings{$id}{$numid}{minvar} = $minvar;
 1981:         }
 1982:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_minvalue webct:x_webct_v01_variable") {
 1983:             $maxvar = $attr->{name};
 1984:             $$settings{$id}{$numid}{maxvar} = $maxvar;
 1985:         }
 1986:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_decimalnum webct:x_webct_v01_variable") {
 1987:             $stepvar = $attr->{name};
 1988:             $$settings{$id}{$numid}{stepvar} = $stepvar;
 1989:         }
 1990:         if ("@state" eq "questestinterop section item presentation response_num render_fib") {
 1991:             $fibtype = $attr->{fibtype};
 1992:             $prompt = $attr->{prompt};
 1993:         }
 1994:         if ("@state" eq "questestinterop section item presentation response_num render_fib response_label") {
 1995:             $$settings{$id}{$numid}{label} = $attr->{ident};
 1996:         }
 1997: 
 1998: # String or Numerical
 1999:         if ("@state" eq "questestinterop section item presentation response_str") {
 2000:             $str_id = $attr->{ident};
 2001:             push(@{$$settings{$id}{str}},$str_id);
 2002:             %{$$settings{$id}{$str_id}} = ();
 2003:             $$settings{$id}{$str_id}{rcardinality} = $attr->{rcardinality};
 2004:         }
 2005: 
 2006:         if ("@state" eq "questestinterop section item presentation response_str render_fib") {
 2007:             $fibtype = $attr->{fibtype};
 2008:         }    
 2009:         if ("@state" eq "questestinterop section item presentation response_str render_fib response_label") {
 2010:             $label = $attr->{ident};
 2011:             %{$$settings{$id}{$str_id}{$label}} = ();
 2012:             push(@{$$allanswers{$id}{strings}},$label);
 2013:             @{$$settings{$id}{strings}{$label}} = ();
 2014:             $$settings{$id}{$str_id}{$label}{fibtype} = $fibtype;
 2015:         }
 2016: 
 2017: # Numerical
 2018:         if ("@state" eq "questestinterop section item presentation response_str render_fib") {
 2019:             $fibtype = $attr->{fibtype};
 2020:             $prompt = $attr->{prompt};
 2021:         }
 2022:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_formula") {
 2023:             $$settings{$id}{$numid}{formula} = $attr->{ident};
 2024:         }
 2025:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_anspresentation") {
 2026:             $$settings{$id}{$numid}{digits} = $attr->{digits};
 2027:             $$settings{$id}{$numid}{format} = $attr->{format};
 2028:         }
 2029:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_anstolerance") {
 2030:             $$settings{$id}{$numid}{toltype} = $attr->{type};
 2031:         }
 2032:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_unit") {
 2033:             my $unitid = $attr->{ident};
 2034:             %{$$settings{$id}{$numid}{$unitid}} = ();
 2035:             $$settings{$id}{$numid}{$unitid}{value} = $attr->{value}; 
 2036:             $$settings{$id}{$numid}{$unitid}{space} = $attr->{space};
 2037:             $$settings{$id}{$numid}{$unitid}{case} = $attr->{case};
 2038:         }
 2039: 
 2040: # Matching 
 2041:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varequal") {
 2042:             $grp = $attr->{respident};
 2043:         }
 2044:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar setvar") {
 2045:             $setvar{varname} = $attr->{varname};
 2046:             if ($setvar{varname} eq 'WebCT_Correct') {
 2047:                 push(@{$$settings{$id}{$grp}{correctanswer}},$answer_id);
 2048:             }
 2049:         }
 2050: 
 2051: # String 
 2052:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varsubset") {
 2053:             $$settings{$id}{class} = 'QUESTION_FILLINBLANK';
 2054:             $answer_id = $attr->{respident};
 2055:             $$settings{$id}{class} = '';
 2056:         }
 2057:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar not") {
 2058:             $$settings{$id}{class} = 'QUESTION_ESSAY';
 2059:         }
 2060:  
 2061: 
 2062: # Feedback
 2063:  
 2064:         if ("@state" eq "questestinterop section item respcondition displayfeedback") {
 2065:             $fdbk = $attr->{linkrefid};
 2066:             push(@{$$settings{$id}{feedback}},$fdbk);
 2067:             $$settings{$id}{$fdbk} = ();
 2068:             $$settings{$id}{$fdbk}{feedbacktype} = $attr->{feedbacktype};
 2069:         }
 2070:         if ("@state" eq "questestinterop section item itemfeedback") {
 2071:             $fdbk = $attr->{ident};
 2072:             $$settings{$id}{$fdbk}{view} = $attr->{view};
 2073:         }
 2074:         if ("@state" eq "questestinterop section item itemfeedback material mattext") {
 2075:             $$settings{$id}{$fdbk}{texttype} = $attr->{texttype};
 2076:         }
 2077:      }, "tagname, attr"],
 2078:      text_h =>
 2079:      [sub {
 2080:         my ($text) = @_;
 2081:         if ("@state" eq "questestinterop section item itemmetadata qmd_itemtype") {
 2082:             $$settings{$id}{itemtype} = $text;
 2083:         }
 2084: 
 2085: #Matching
 2086: 
 2087:         if ("@state" eq "questestinterop section item presentation material mattext") {
 2088:             $$settings{$id}{text} = $text;
 2089:         }
 2090:         if ("@state" eq "questestinterop section item presentation response_grp material mattext") {
 2091:             $$settings{$id}{$grp}{text} = $text;
 2092:         }
 2093:         if ("@state" eq "questestinterop section item presentation response_grp render_choice response_label material mattext") {
 2094:             $$settings{$id}{$grp}{$answer_id}{text} = $text;
 2095:         }
 2096: 
 2097: # Multiple choice
 2098: 
 2099:         if ("@state" eq "questestinterop section item presentation flow material mattext") {
 2100:             $$settings{$id}{text} = $text;
 2101:         }
 2102: 
 2103:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice flow_label response_label material mattext") {
 2104:             $$settings{$id}{$list}{$answer_id}{text} = $text;
 2105:         }
 2106: 
 2107: # Numerical
 2108:         if ("@state" eq "questestinterop section item presentation material mat_extension webct:x_webct_v01_dynamicmattext") {
 2109:             $$settings{$id}{text} = $text;
 2110:         }
 2111:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_minvalue webct:x_webct_v01_variable") {
 2112:              $$settings{$id}{$minvar}{min} = $text;
 2113:         }
 2114:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_maxvalue webct:x_webct_v01_variable") {
 2115:              $$settings{$id}{$maxvar}{max} = $text;
 2116:         }
 2117:         if ("@state" eq "questestinterop section item presentation response_num material mat_extension webct:x_webct_v01_dynamicdata webct:x_webct_v01_datarange webct:x_webct_v01_decimalnum webct:x_webct_v01_variable") {
 2118:              $$settings{$id}{$stepvar}{step} = $text;
 2119:         }
 2120: 
 2121:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varequal") {
 2122:             $answer_id = $text;
 2123:         }
 2124:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varsubset") { # String
 2125:             push(@{$$settings{$id}{strings}{$answer_id}},$text);
 2126:         }
 2127:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar setvar") {
 2128:             if ($setvar{varname} eq "answerValue") { # Multiple Choice
 2129:                 if ($text =~ m/^\d+$/) {
 2130:                     if ($text > 0) {
 2131:                         push(@{$$settings{$id}{$list}{correctanswer}}),$answer_id;   
 2132:                     }
 2133:                 }
 2134:             }
 2135:         }
 2136: 
 2137:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_unit") {
 2138:             $$settings{$id}{$numid}{$unitid}{text} = $text;
 2139:         }
 2140: 
 2141:         if ("@state" eq "questestinterop section item itemfeedback material mattext") {
 2142:             $$settings{$id}{$fdbk}{text} = $text;
 2143:         }
 2144:       }, "dtext"],
 2145:      end_h =>
 2146:      [sub {
 2147:         my ($tagname) = @_;
 2148:         pop @state;
 2149:      }, "tagname"],
 2150:     );
 2151:     $p->unbroken_text(1);
 2152:     $p->parse_file($xmlfile);
 2153:     $p->eof;
 2154: }
 2155: 
 2156: sub process_assessment {
 2157:     my ($cms,$context,$res,$docroot,$container,$dirname,$destdir,$settings,$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,$dbparse) = @_;
 2158:     my @allids = ();
 2159:     my %allanswers = ();
 2160:     my %allchoices = ();
 2161:     my %qzparams = ();
 2162:     my @allquestids = ();
 2163:     my %catinfo = ();
 2164:     my %qzdbsettings = ();
 2165:     my %alldbanswers = ();
 2166:     my %alldbchoices = ();
 2167:     my @alldbquestids = ();
 2168:     my $containerdir;
 2169:     my $newdir;
 2170:     my $randompickflag = 0;
 2171:     my ($cid,$cdom,$cnum);
 2172:     if ($context eq 'DOCS') {
 2173:         $cid = $ENV{'request.course.id'};
 2174:         ($cdom,$cnum) = split/_/,$cid;
 2175:     }
 2176:     my $destresdir = $destdir;
 2177:     if ($context eq 'CSTR') {
 2178:         $destresdir =~ s|/home/$uname/public_html/|/res/$udom/$uname/|;
 2179:     } elsif ($context eq 'DOCS') {
 2180:         $destresdir =~ s|^/home/httpd/html/userfiles|/uploaded|;
 2181:     }
 2182:     if ($cms eq 'bb5') {
 2183:         &parse_bb5_assessment($res,$docroot,$container,$settings,\%allanswers,\%allchoices,\@allids);
 2184:     } elsif ($cms eq 'bb6') {
 2185:         &parse_bb6_assessment($res,$docroot,$container,$settings,\%allanswers,\%allchoices,\@allids);
 2186:     } elsif ($cms eq 'webct4') {
 2187:         unless($$dbparse) {
 2188:             &parse_webct4_questionDB($docroot,\%catinfo,\%qzdbsettings,\%alldbanswers,\%alldbchoices,\@alldbquestids);
 2189:             if (!-e "$destdir/sequences") {
 2190:                 mkdir("$destdir/sequences",0755);
 2191:             }
 2192:             my $numcats = keys %catinfo;
 2193:             my $curr_id = 0;
 2194:             my $next_id = 1;
 2195:             my $fh;
 2196:             open($fh,">$destdir/sequences/question_database.sequence");
 2197:             push @{$sequencesfiles},'question_database.sequence';
 2198:             foreach my $category (sort keys %catinfo) {
 2199:                 my $seqname = $catinfo{$category}{title}.'_'.$category;
 2200:                 $seqname =~ s/\s/_/g;
 2201:                 $seqname =~ s/\W//g;
 2202:                 push(@{$sequencesfiles},$seqname.'.sequence');
 2203:                 my $catsrc = "$destresdir/sequences/$seqname.sequence";
 2204:                 if ($curr_id == 0) {
 2205:                     print $fh qq|<resource id="1" src="$catsrc" type="start" title="$catinfo{$category}{title}"></resource>|;
 2206:                 }
 2207:                 if ($numcats == 1) {
 2208:                     print $fh qq|
 2209: <link from="1" to="2" index="1"></link>
 2210: <resource id="2" src="" type="finish">\n|;
 2211:                 } else {
 2212:                     $curr_id = $next_id;
 2213:                     $next_id = $curr_id + 1;
 2214:                     $catsrc = "$destresdir/sequences/$seqname.sequence";
 2215:                     print $fh qq|
 2216: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
 2217: <resource id="$next_id" src="$catsrc" title="$catinfo{$category}{title}"|;
 2218:                     if ($next_id == $numcats) {
 2219:                         print $fh qq| type="finish"></resource>\n|;
 2220:                     } else {
 2221:                         print $fh qq|></resource>\n|;
 2222:                     }
 2223:                 }
 2224:                 print $fh qq|</map>|;
 2225:                 if (!-e "$destdir/problems") {
 2226:                     mkdir("$destdir/problems",0755);
 2227:                 }
 2228:                 if (!-e "$destdir/problems/$seqname") {
 2229:                     mkdir("$destdir/problems/$seqname",0755);
 2230:                 }
 2231:                 my $newdir = "$destdir/problems/$seqname";
 2232:                 my $dbcontainerdir;
 2233:                 &build_problem_container($seqname,$newdir,$destdir,'database',$seqname,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,\@{$catinfo{$category}{contents}},$udom,$uname,$dirname,\$dbcontainerdir,\$newdir,$cid,$cdom,$cnum);
 2234:             }
 2235:             close($fh);
 2236:             &write_webct4_questions(\@alldbquestids,$context,$settings,$dirname,\%alldbanswers,\%alldbchoices,$total,$cid,$cdom,$cnum);
 2237:             $$dbparse = 1;
 2238:         }
 2239:         &parse_webct4_quizprops($res,$docroot,$container,\%qzparams);
 2240:         foreach (sort keys %qzparams) {
 2241:             if (exists($qzparams{$res}{numpick})) { 
 2242:                 if ($qzparams{$res}{numpick} < @allids) {
 2243:                     $$randompicks{$res} = $qzparams{$res}{numpick};
 2244:                     $randompickflag = 1;
 2245:                 }
 2246:             }
 2247:         }
 2248:         &parse_webct4_assessment($res,$docroot,$container,\@allids);
 2249:     }
 2250:     my $dirtitle = $$settings{'title'};
 2251:     $dirtitle =~ s/\W//g;
 2252:     $dirtitle .= '_'.$res;
 2253:     if (!-e "$destdir/problems") {
 2254:         mkdir("$destdir/problems",0755);
 2255:     }
 2256:     if (!-e "$destdir/problems/$dirtitle") {
 2257:         mkdir("$destdir/problems/$dirtitle",0755);
 2258:     }
 2259:     my $newdir = "$destdir/problems/$dirtitle";
 2260: 
 2261:     &build_problem_container($dirtitle,$newdir,$destdir,$container,$res,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,\@allids,$udom,$uname,$dirname,\$containerdir,\$newdir,$cid,$cdom,$cnum);
 2262:     if ($cms eq 'bb5') {
 2263:         &write_bb5_questions(\@allids,$containerdir,$context,$settings,$dirname,$res,\%allanswers,\%allchoices,$total,$newdir,$cid,$cdom,$cnum);
 2264:     } elsif ($cms eq 'bb6') {
 2265:         &write_bb6_questions(\@allids,$containerdir,$context,$settings,$dirname,$res,\%allanswers,\%allchoices,$total,$newdir,$cid,$cdom,$cnum);
 2266:     }
 2267: }
 2268: 
 2269: sub build_problem_container {
 2270:     my ($dirtitle,$newdir,$destdir,$container,$res,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,$allids,$udom,$uname,$dirname,$containerdir,$newdir,$cid,$cdom,$cnum) = @_;
 2271:     my $seqdir = "$destdir/sequences";
 2272:     my $pagedir = "$destdir/pages";
 2273:     my $curr_id = 0;
 2274:     my $next_id = 1;
 2275:     my $fh;
 2276:     if ($container eq 'pool' || $randompickflag || $container eq 'database') {
 2277:         $$containerdir = $seqdir.'/'.$res.'.sequence';
 2278:         if (!-e "$seqdir") {
 2279:             mkdir("$seqdir",0770);
 2280:         }
 2281:         open($fh,">$$containerdir");
 2282:         $$total{seq} ++;
 2283:         push @{$sequencesfiles},$res.'.sequence';
 2284:     } else {
 2285:         $$containerdir = $pagedir.'/'.$res.'.page';
 2286:         if (!-e "$destdir/pages") {
 2287:             mkdir("$destdir/pages",0770);
 2288:         }
 2289:         open($fh,">$$containerdir");
 2290:         $$total{page} ++;
 2291:         push @{$pagesfiles},$res.'.page';
 2292:     }
 2293:     print $fh qq|<map>
 2294: |;
 2295:     my $probsrc = "/res/lib/templates/simpleproblem.problem";
 2296:     if ($context eq 'CSTR') {
 2297:         $probsrc="/res/$udom/$uname/$dirname/problems/$dirtitle/$$allids[0].problem";
 2298:     }
 2299:     print $fh qq|<resource id="1" src="$probsrc" type="start" title="question_0001"></resource>|;
 2300:     if (@{$allids} == 1) {
 2301:         print $fh qq|
 2302: <link from="1" to="2" index="1"></link>
 2303: <resource id="2" src="" type="finish">\n|;
 2304:     } else {
 2305:         for (my $j=1; $j<@{$allids}; $j++) {
 2306:             my $qntitle = $j+1;
 2307:             while (length($qntitle) <4) {
 2308:                 $qntitle = '0'.$qntitle;
 2309:             }
 2310:             $curr_id = $j;
 2311:             $next_id = $curr_id + 1;
 2312:             if ($context eq 'CSTR') {
 2313:                 $probsrc = "/res/$udom/$uname/$dirname/problems/$dirtitle/$$allids[$j].problem";
 2314:             }
 2315:             print $fh qq|
 2316: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
 2317: <resource id="$next_id" src="$probsrc" title="question_$qntitle"|;
 2318:             if ($next_id == @{$allids}) {
 2319:                 print $fh qq| type="finish"></resource>\n|;
 2320:             } else {
 2321:                 print $fh qq|></resource>|;
 2322:             }
 2323:         }
 2324:     }
 2325:     print $fh qq|</map>|;
 2326:     close($fh);
 2327: }
 2328: 
 2329: sub write_bb5_questions {
 2330:     my ($allids,$containerdir,$context,$settings,$dirname,$res,$allanswers,$allchoices,$total,$newdir,$cid,$cdom,$cnum) = @_;
 2331:     my $qnum = 0;
 2332:     foreach my $id (@{$allids}) {
 2333:         $qnum ++;
 2334:         my $output;
 2335:         my $permcontainer = $containerdir;
 2336:         $permcontainer =~ s#/home/httpd/html/userfiles#uploaded#;
 2337:         my $symb = $cid.'.'.$permcontainer.'___'.$qnum.'___lib/templates/simpleproblem.problem.0.';
 2338:         my %resourcedata = ();
 2339:         for (my $i=0; $i<10; $i++) {
 2340:             my $iter = $i+1;
 2341:             $resourcedata{$symb.'text'.$iter} = "";
 2342:             $resourcedata{$symb.'value'.$iter} = "unused";
 2343:             $resourcedata{$symb.'position'.$iter} = "random";
 2344:         }
 2345:         $resourcedata{$symb.'randomize'} = 'yes';
 2346:         $resourcedata{$symb.'maxfoils'} = 10;
 2347:         if ($context eq 'CSTR') {
 2348:             $output = qq|<problem>
 2349: |;
 2350:         }
 2351:         $$total{prob} ++;
 2352:         if ($$settings{$id}{class} eq "QUESTION_ESSAY") {
 2353:             if ($context eq 'CSTR') {
 2354:                 $output .= qq|<startouttext />$$settings{$id}{text}<endouttext />
 2355:  <essayresponse>
 2356:  <textfield></textfield>
 2357:  </essayresponse>
 2358:  <postanswerdate>
 2359:   $$settings{$id}{feedbackcorr} 
 2360:  </postanswerdate>
 2361: |;
 2362:              } else {
 2363: 		 $resourcedata{$symb.'questiontext'} = $$settings{$id}{text};
 2364:                  $resourcedata{$symb.'hiddenparts'} = '!essay';
 2365:                  $resourcedata{$symb.'questiontype'} = 'essay';
 2366:              }
 2367:         } else {
 2368:             if ($context eq 'CSTR') {
 2369:                 $output .= qq|<startouttext />$$settings{$id}{text}\n|;
 2370:             } else {
 2371:                 $resourcedata{$symb.'questiontext'} = $$settings{$id}{text};
 2372:             }
 2373:             my ($image,$imglink,$url);
 2374:             if ( defined($$settings{$id}{image}) ) {
 2375:                 if ( $$settings{$id}{style} eq 'embed' ) {
 2376:                     $image = qq|<br /><img src="$dirname/resfiles/$res/$$settings{$id}{image}" /><br />|;
 2377:                 } else {
 2378:                     $imglink = qq|<br /><a href="$dirname/resfiles/$res/$$settings{$id}{image}">Link to file</a><br />|;
 2379:                 }
 2380:             }
 2381:             if ( defined($$settings{$id}{url}) ) {
 2382:                 $url = qq|<br /><a href="$$settings{$id}{url}">$$settings{$id}{name}</a><br />|;
 2383:             }
 2384:             if ($context eq 'CSTR') {
 2385:                 $output .= $image.$imglink.$url.'
 2386: <endouttext />';
 2387:             } else {
 2388:                 $resourcedata{$symb.'questiontext'} .= $image.$imglink.$url;
 2389:             }
 2390:             if ($$settings{$id}{class} eq 'QUESTION_MULTIPLECHOICE') {
 2391:                 my $numfoils = @{$$allanswers{$id}};
 2392:                 if ($context eq 'CSTR') {
 2393:                     $output .= qq|
 2394:  <radiobuttonresponse max="$numfoils" randomize="yes">
 2395:   <foilgroup>
 2396: |;
 2397:                 } else {
 2398:                     $resourcedata{$symb.'hiddenparts'} = '!radio';
 2399:                     $resourcedata{$symb.'questiontype'} = 'radio';
 2400:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 2401:                 }
 2402:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2403:                     my $iter = $k+1;
 2404:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 2405:                     if (grep/^$$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 2406:                         $output .= "true\" location=\"";
 2407:                         $resourcedata{$symb.'value'.$iter} = "true";
 2408:                     } else {
 2409:                         $output .= "false\" location=\"";
 2410:                         $resourcedata{$symb.'value'.$iter} = "false";
 2411:                     }
 2412:                     if (lc ($$allanswers{$id}[$k]) =~ m/^\s?([Aa]ll)|([Nn]one)\sof\sthe\sabove\.?/) {
 2413:                         $output .= "bottom\"";
 2414:                         $resourcedata{$symb.'position'.$iter} = "bottom";
 2415:                     } else {
 2416:                         $output .= "random\"";
 2417:                     }
 2418:                     $output .= "\><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text};
 2419:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2420:                     my ($ans_image,$ans_link);
 2421:                     if ( defined($$settings{$id}{$$allanswers{$id}[$k]}{image}) ) {
 2422:                         if ( $$settings{$id}{$$allanswers{$id}[$k]}{style} eq 'embed' ) {
 2423:                             $ans_image .= qq|<br /><img src="$dirname/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" /><br />|;
 2424:                         } else {
 2425:                             $ans_link .= qq|<br /><a href="$dirname/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" />Link to file</a><br/>|;
 2426:                         }
 2427:                     }
 2428:                     $output .= $ans_image.$ans_link.'<endouttext /></foil>'."\n";
 2429:                     $resourcedata{$symb.'text'.$iter} .= $ans_image.$ans_link;
 2430:                 }
 2431:                 if ($context eq 'CSTR') {
 2432:                     chomp($output);
 2433:                     $output .= qq|
 2434:   </foilgroup>
 2435:  </radiobuttonresponse>
 2436: |;
 2437:                 }
 2438:             } elsif ($$settings{$id}{class} eq 'QUESTION_TRUEFALSE') {
 2439:                 my $numfoils = @{$$allanswers{$id}};
 2440:                 if ($context eq 'CSTR') {
 2441:                     $output .= qq|
 2442:    <radiobuttonresponse max="$numfoils" randomize="yes">
 2443:     <foilgroup>
 2444: |;
 2445:                 } else {
 2446:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 2447:                     $resourcedata{$symb.'hiddenparts'} = '!radio';
 2448:                     $resourcedata{$symb.'questiontype'} = 'radio';
 2449:                 }
 2450:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2451:                     my $iter = $k+1;
 2452:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 2453:                     if (grep/^$$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 2454:                         $output .= "true\" location=\"random\"";
 2455:                         $resourcedata{$symb.'value'.$iter} = "true";
 2456:                     } else {
 2457:                         $output .= "false\" location=\"random\"";
 2458:                         $resourcedata{$symb.'value'.$iter} = "false";
 2459:                     }
 2460:                     $output .= "\><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 2461:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2462:                 }
 2463:                 if ($context eq 'CSTR') {
 2464:                     chomp($output);
 2465:                     $output .= qq|
 2466:     </foilgroup>
 2467:    </radiobuttonresponse>
 2468: |;
 2469:                 }
 2470:             } elsif ($$settings{$id}{class} eq 'QUESTION_MULTIPLEANSWER') {
 2471:                 my $numfoils = @{$$allanswers{$id}};
 2472:                 if ($context eq 'CSTR') {
 2473:                     $output .= qq|
 2474:    <optionresponse max="$numfoils" randomize="yes">
 2475:     <foilgroup options="('True','False')">
 2476: |;
 2477:                 } else {
 2478:                     $resourcedata{$symb.'newopt'} = '';
 2479:                     $resourcedata{$symb.'delopt'} = '';
 2480:                     $resourcedata{$symb.'options'} = "('True','False')";
 2481:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 2482:                     $resourcedata{$symb.'questiontype'} = 'option';
 2483:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 2484:                 }
 2485:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2486:                     my $iter = $k+1;
 2487:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 2488:                     if (grep/^$$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 2489:                         $output .= "True\"";
 2490:                         $resourcedata{$symb.'value'.$iter} = "True";
 2491:                     } else {
 2492:                         $output .= "False\"";
 2493:                         $resourcedata{$symb.'value'.$iter} = "False";
 2494:                     }
 2495:                     $output .= "\><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 2496:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2497:                 }
 2498:                 if ($context eq 'CSTR') {  
 2499:                     chomp($output);
 2500:                     $output .= qq|
 2501:     </foilgroup>
 2502:    </optionresponse>
 2503: |;
 2504:                 }
 2505:             } elsif ($$settings{$id}{class} eq 'QUESTION_ORDER') {
 2506:                 my $numfoils = @{$$allanswers{$id}};
 2507:                 my @allorder = ();
 2508:                 if ($context eq 'CSTR') {
 2509:                     $output .= qq|
 2510:    <rankresponse max="$numfoils" randomize="yes">
 2511:     <foilgroup>
 2512: |;
 2513:                 } else {
 2514:                     $resourcedata{$symb.'newopt'} = '';
 2515:                     $resourcedata{$symb.'delopt'} = '';
 2516:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 2517:                     $resourcedata{$symb.'questiontype'} = 'option';
 2518:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 2519:                 }
 2520:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2521:                     if ($context eq 'CSTR') {
 2522:                         $output .= "   <foil location=\"random\" name=\"foil".$k."\" value=\"".$$settings{$id}{$$allanswers{$id}[$k]}{order}."\"><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 2523:                     } else {
 2524:                         my $iter = $k+1;
 2525:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2526:                         if (!grep/^$$settings{$id}{$$allanswers{$id}[$k]}{order}$/,@allorder) {
 2527:                             push @allorder, $$settings{$id}{$$allanswers{$id}[$k]}{order};
 2528:                         }
 2529:                     }
 2530:                 }
 2531:                 if ($context eq 'CSTR') {
 2532:                     chomp($output);
 2533:                     $output .= qq|
 2534:     </foilgroup>
 2535:    </rankresponse>
 2536: |;
 2537:                 } else {
 2538:                     @allorder = sort {$a <=> $b} @allorder;
 2539:                     $resourcedata{$symb.'options'} = "('".join("','",@allorder)."')";
 2540:                 }
 2541:             } elsif ($$settings{$id}{class} eq 'QUESTION_FILLINBLANK') {
 2542:                 my $numerical = 1;
 2543:                 if ($context eq 'DOCS') {
 2544:                     $numerical = 0;
 2545:                 } else {
 2546:                     for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2547:                         if ($$settings{$id}{$$allanswers{$id}[$k]}{text} =~ m/([^\d\.]|\.\.)/) {
 2548:                             $numerical = 0;
 2549:                         }
 2550:                     }
 2551:                 }
 2552:                 if ($numerical) {
 2553:                     my $numans;
 2554:                     my $tol;
 2555:                     if (@{$$allanswers{$id}} == 1) {
 2556:                         $tol = 5;
 2557:                         $numans = $$settings{$id}{$$allanswers{$id}[0]}{text};
 2558:                     } else {
 2559:                         my $min = $$settings{$id}{$$allanswers{$id}[0]}{text};
 2560:                         my $max = $$settings{$id}{$$allanswers{$id}[0]}{text};
 2561:                         for (my $k=1; $k<@{$$allanswers{$id}}; $k++) {
 2562:                             if ($$settings{$id}{$$allanswers{$id}[$k]}{text} <= $min) {
 2563:                                 $min = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2564:                             }
 2565:                             if ($$settings{$id}{$$allanswers{$id}[$k]}{text} >= $max) {
 2566:                                 $max = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2567:                             }
 2568:                         }
 2569:                         $numans = ($max + $min)/2;
 2570:                         $tol = 100*($max - $min)/($numans*2);
 2571:                     }
 2572:                     if ($context eq 'CSTR') {
 2573:                         $output .= qq|
 2574: <numericalresponse answer="$numans">
 2575:         <responseparam type="tolerance" default="$tol%" name="tol" description="Numerical Tolerance" />
 2576:         <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures"
 2577: />
 2578:         <textline />
 2579: </numericalresponse>
 2580: |;
 2581:                     }
 2582:                 } else {
 2583:                     if ($context eq 'DOCS') {
 2584:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 2585:                         $resourcedata{$symb.'questiontype'} = 'string';
 2586:                         $resourcedata{$symb.'maxfoils'} = @{$$allanswers{$id}};
 2587:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 2588:                         $resourcedata{$symb.'stringtype'} = 'ci';
 2589:                         $resourcedata{$symb.'stringanswer'} = $$settings{$id}{$$allanswers{$id}[0]}{text};
 2590:                     } else {
 2591:                         if (@{$$allanswers{$id}} == 1) {
 2592:                             $output .= qq|
 2593: <stringresponse answer="$$settings{$id}{$$allanswers{$id}[0]}{text}" type="ci">
 2594: <textline>
 2595: </textline>
 2596: </stringresponse>
 2597: |;
 2598:                         } else {
 2599:                             my @answertext = ();
 2600:                             for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2601:                                 $$settings{$id}{$$allanswers{$id}[$k]}{text} =~ s/\|/\|/g;
 2602:                                 push @answertext, $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2603:                             }
 2604:                             my $regexpans = join('|',@answertext);
 2605:                             $regexpans = '/^('.$regexpans.')\b/';
 2606:                             $output .= qq|
 2607: <stringresponse answer="$regexpans" type="re">
 2608: <textline>
 2609: </textline>
 2610: </stringresponse>
 2611: |;
 2612:                         }
 2613:                     } 
 2614:                 }
 2615:             } elsif ($$settings{$id}{class} eq "QUESTION_MATCH") {
 2616:                 my @allmatchers = ();
 2617:                 my %matchtext = ();
 2618:                 if ($context eq 'CSTR') {
 2619:                     $output .= qq|
 2620: <matchresponse max="10" randomize="yes">
 2621:     <foilgroup>
 2622:         <itemgroup>
 2623: |;
 2624:                 } else {
 2625:                     $resourcedata{$symb.'newopt'} = '';
 2626:                     $resourcedata{$symb.'delopt'} = '';
 2627:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 2628:                     $resourcedata{$symb.'questiontype'} = 'option';
 2629:                     $resourcedata{$symb.'maxfoils'} =  @{$$allanswers{$id}};
 2630:                 }
 2631:                 for (my $k=0; $k<@{$$allchoices{$id}}; $k++) {
 2632:                     if ($context eq 'CSTR') {
 2633:                         $output .= qq|
 2634: <item name="$$allchoices{$id}[$k]">
 2635: <startouttext />$$settings{$id}{$$allchoices{$id}[$k]}{text}<endouttext />
 2636: </item>
 2637:                     |;
 2638:                     } else {
 2639:                         if (!grep/^$$settings{$id}{$$allchoices{$id}[$k]}{text}$/,@allmatchers) {
 2640:                             push @allmatchers, $$settings{$id}{$$allchoices{$id}[$k]}{text};
 2641:                             $matchtext{$$allchoices{$id}[$k]} = $$settings{$id}{$$allchoices{$id}[$k]}{text};
 2642:                         }
 2643:                     }
 2644:                 }
 2645:                 if ($context eq 'CSTR') {
 2646:                     $output .= qq|
 2647:         </itemgroup>
 2648: |;
 2649:                 }
 2650:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 2651:                     if ($context eq 'CSTR') {
 2652:                         $output .= qq|
 2653:         <foil location="random" value="$$settings{$id}{$$allanswers{$id}[$k]}{choice_id}" name="$$allanswers{$id}[$k]">
 2654:          <startouttext />$$settings{$id}{$$allanswers{$id}[$k]}{text}<endouttext />
 2655:         </foil>
 2656: |;
 2657:                     } else {
 2658:                         my $iter = $k+1;
 2659:                         $resourcedata{$symb.'value'.$iter} = $matchtext{$$settings{$id}{$$allanswers{$id}[$k]}{choice_id}};
 2660:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 2661:                     }
 2662:                 }
 2663:                 if ($context eq 'CSTR') {
 2664:                     $output .= qq|
 2665:     </foilgroup>
 2666: </matchresponse>
 2667: |;
 2668:                 } else {
 2669:                     $resourcedata{$symb.'options'} = "('".join("','",@allmatchers)."')";
 2670:                 }
 2671:             }
 2672:         }
 2673:         if ($context eq 'CSTR') {
 2674:             $output .= qq|</problem>
 2675: |;
 2676:             open(PROB,">$newdir/$id.problem");
 2677:             print PROB $output;
 2678:             close PROB;
 2679:         } else {
 2680: # put %resourcedata;
 2681:             my $reply=&Apache::lonnet::cput
 2682:                 ('resourcedata',\%resourcedata,$cdom,$cnum);
 2683:         }
 2684:     }
 2685: }
 2686: 
 2687: sub write_webct4_questions {
 2688:     my ($alldbquestids,$context,$settings,$dirname,$alldbanswers,$alldbchoices,$total,$cid,$cdom,$cnum) = @_;
 2689: }
 2690: 
 2691: sub write_bb6_questions {
 2692:     my ($allids,$containerdir,$context,$settings,$dirname,$res,$allanswers,$allchoices) = @_;
 2693: }
 2694: 
 2695: # ---------------------------------------------------------------- Process Blackboard Announcements
 2696: sub process_announce {
 2697:     my ($res,$docroot,$destdir,$settings,$globalresref,$seqstem,$resrcfiles) = @_;
 2698:     my $xmlfile = $docroot.'/'.$res.".dat";
 2699:     my @state = ();
 2700:     my @assess = ();
 2701:     my $id;
 2702:     my $p = HTML::Parser->new
 2703:     (
 2704:      xml_mode => 1,
 2705:      start_h =>
 2706:      [sub {
 2707:         my ($tagname, $attr) = @_;
 2708:         push @state, $tagname;
 2709:         if ("@state" eq "ANNOUNCEMENT TITLE") {
 2710:             $$settings{title} = $attr->{value};
 2711:             $$settings{startassessment} = ();
 2712:         } elsif ("@state" eq "ANNOUNCEMENT DESCRIPTION FLAGS ISHTML") {  
 2713:             $$settings{ishtml} = $attr->{value};          
 2714:         } elsif ("@state" eq "ANNOUNCEMENT DESCRIPTION FLAGS ISNEWLINELITERAL" ) {
 2715:             $$settings{isnewline} = $attr->{value};
 2716:         } elsif ("@state" eq "ANNOUNCEMENT ISPERMANENT" ) {
 2717:             $$settings{ispermanent} = $attr->{value};
 2718:         } elsif ("@state" eq "ANNOUNCEMENT DATES UPDATED") {
 2719:             $$settings{dates} = $attr->{value}; 
 2720:         } elsif ("@state" eq "ANNOUNCEMENT FILES STARTASSESSMENT" ) {
 2721:             $id = $attr->{id};
 2722:             %{$$settings{startassessment}{$id}} = ();
 2723:             push @assess,$id;
 2724:         } elsif ("@state" eq "ANNOUNCEMENT FILES STARTASSESSMENT ATTRIB" ) {
 2725:             my $key = $attr->{key};
 2726:             $$settings{startassessment}{$id}{$key} = $attr->{value};
 2727:         }
 2728:      }, "tagname, attr"],
 2729:      text_h =>
 2730:      [sub {
 2731:         my ($text) = @_;
 2732:         if ("@state" eq "ANNOUNCEMENT DESCRIPTION TEXT") {
 2733:             $$settings{text} = $text;
 2734:         }
 2735:       }, "dtext"],
 2736:      end_h =>
 2737:      [sub {
 2738:         my ($tagname) = @_;
 2739:         pop @state;
 2740:      }, "tagname"],
 2741:     );
 2742:     $p->unbroken_text(1);
 2743:     $p->parse_file($xmlfile);
 2744:     $p->eof;
 2745: 
 2746:     if (defined($$settings{text})) {
 2747:         if ($$settings{ishtml} eq "false") {
 2748:             if ($$settings{isnewline} eq "true") {
 2749:                 $$settings{text} =~ s#\n#<br/>#g;
 2750:             }
 2751:         } else {
 2752:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 2753:         }
 2754:     }
 2755:   
 2756:     if (@assess > 0) {
 2757:         foreach my $id (@assess) {
 2758:             $$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.";
 2759:         }
 2760:     }
 2761: 
 2762:     open(FILE,">$destdir/resfiles/$res.html");
 2763:     push @{$resrcfiles}, "$res.html";
 2764:     print FILE qq|<html>
 2765: <head>
 2766: <title>$$settings{title}</title>
 2767: </head>
 2768: <body bgcolor='#ffffff'>
 2769: <table>
 2770:  <tr>
 2771:   <td bgcolor='#CCCCFF'>$$settings{title} - announcement date: $$settings{dates}</td>
 2772:  </tr>
 2773: </table>
 2774: <br/>
 2775: $$settings{text}
 2776: |;
 2777:     print FILE qq|
 2778:   </body>
 2779:  </html>|;
 2780:     close(FILE);
 2781: }
 2782: 
 2783: # ---------------------------------------------------------------- Process Blackboard Content
 2784: sub process_content {
 2785:     my ($cms,$res,$context,$docroot,$destdir,$settings,$dom,$user,$resrcfiles,$packages,$hrefs) = @_;
 2786:     my $xmlfile = $docroot.'/'.$res.".dat";
 2787:     my $destresdir = $destdir;
 2788:     if ($context eq 'CSTR') {
 2789:         $destresdir =~ s|/home/$user/public_html/|/res/$dom/$user/|;
 2790:     } elsif ($context eq 'DOCS') {
 2791:         $destresdir =~ s|^/home/httpd/html/userfiles|/uploaded|;
 2792:     }
 2793:     my $filetag = '';
 2794:     if ($cms eq 'bb5') {
 2795:         $filetag = 'FILEREF';
 2796:     } elsif ($cms eq 'bb6') {
 2797:         $filetag = 'FILE';
 2798:     }
 2799:     my $filecount = 0;
 2800:     my @allrelfiles = ();
 2801:     my @state;
 2802:     @{$$settings{files}} = (); 
 2803:     my $p = HTML::Parser->new
 2804:     (
 2805:       xml_mode => 1,
 2806:       start_h =>
 2807:       [sub {
 2808:         my ($tagname, $attr) = @_;
 2809:         push @state, $tagname;
 2810:         if ("@state" eq "CONTENT ") {
 2811:             %{$$settings{maindata}} = ();
 2812:         } elsif ("@state" eq "CONTENT TITLECOLOR") {
 2813:             $$settings{titlecolor} =  $attr->{value};
 2814:         } elsif ("@state" eq "CONTENT MAINDATA TEXTCOLOR") {
 2815:             $$settings{maindata}{color} = $attr->{value};
 2816:         } elsif ("@state" eq "CONTENT MAINDATA FLAGS ISHTML") {  
 2817:             $$settings{maindata}{ishtml} = $attr->{value}; 
 2818:         } elsif ("@state" eq "CONTENT MAINDATA FLAGS ISNEWLINELITERAL") {  
 2819:             $$settings{maindata}{isnewline} = $attr->{value};
 2820:         } elsif ("@state" eq "CONTENT BODY TYPE") {
 2821:             $$settings{maindata}{bodytype} =  $attr->{value};
 2822:         } elsif ("@state" eq "CONTENT FLAGS ISAVAILABLE" ) {
 2823:             $$settings{isavailable} = $attr->{value};
 2824:         } elsif ("@state" eq "CONTENT FLAGS ISFOLDER" ) {
 2825:             $$settings{isfolder} = $attr->{value};
 2826:         } elsif ("@state" eq "CONTENT FLAGS LAUNCHINNEWWINDOW" ) {
 2827:             $$settings{newwindow} = $attr->{value};
 2828:         } elsif ("@state" eq "CONTENT FILES $filetag") {
 2829:             %{$$settings{files}[$filecount]} = ();
 2830:             %{$$settings{files}[$filecount]{registry}} = (); 
 2831:         } elsif ("@state" eq "CONTENT FILES FILEREF RELFILE" ) {
 2832:             $$settings{files}[$filecount]{'relfile'} = $attr->{value};
 2833:             push @allrelfiles, $attr->{value};
 2834:         } elsif ("@state" eq "CONTENT FILES $filetag MIMETYPE") {
 2835:             $$settings{files}[$filecount]{mimetype} = $attr->{value};
 2836:         } elsif ("@state" eq "CONTENT FILES $filetag CONTENTTYPE") {
 2837:             $$settings{files}[$filecount]{contenttype} = $attr->{value};
 2838:         } elsif ("@state" eq "CONTENT FILES $filetag FILEACTION") {
 2839:             $$settings{files}[$filecount]{fileaction} = $attr->{value};
 2840:         } elsif ("@state" eq "CONTENT FILES $filetag PACKAGEPARENT") {
 2841:             $$settings{files}[$filecount]{packageparent} = $attr->{value};
 2842:         } elsif ("@state" eq "CONTENT FILES $filetag LINKNAME") {
 2843:             $$settings{files}[$filecount]{linkname} = $attr->{value};
 2844:         } elsif ("@state" eq "CONTENT FILES $filetag REGISTRY REGISTRYENTRY") {
 2845:             my $key = $attr->{key};
 2846:             $$settings{files}[$filecount]{registry}{$key} = $attr->{value};
 2847:         }
 2848:       }, "tagname, attr"],
 2849:       text_h =>
 2850:       [sub {
 2851:         my ($text) = @_;
 2852:         if ("@state" eq "CONTENT TITLE") {
 2853:             $$settings{title} = $text;
 2854:         } elsif ( ("@state" eq "CONTENT MAINDATA TEXT") || ("@state" eq "CONTENT BODY TEXT") ) {
 2855:             $$settings{maindata}{text} = $text;
 2856:         }  elsif ("@state" eq "CONTENT FILES $filetag REFTEXT") {
 2857:             $$settings{files}[$filecount]{reftext} = $text;
 2858:         } elsif ("@state" eq "CONTENT FILES FILE NAME" ) {
 2859:             $$settings{files}[$filecount]{'relfile'} = $text;
 2860:             push @allrelfiles, $text;
 2861:         }
 2862:        }, "dtext"],
 2863:       end_h =>
 2864:       [sub {
 2865:         my ($tagname) = @_;
 2866:         if ("@state" eq "CONTENT FILES $filetag") {
 2867:             $filecount ++;
 2868:         }
 2869:         pop @state;
 2870:       }, "tagname"],
 2871:      );
 2872:     $p->unbroken_text(1);
 2873:     $p->parse_file($xmlfile);
 2874:     $p->eof;
 2875:     my $linktag = '';
 2876:     my $fontcol = '';
 2877:     if (@{$$settings{files}} > 0) {
 2878:         for (my $filecount=0;  $filecount<@{$$settings{files}}; $filecount++) {
 2879:             if ($$settings{files}[$filecount]{'fileaction'} eq 'embed') {
 2880:                 if ( $$settings{files}[$filecount]{reftext} =~ m#<\!\-\-\s_(\d+)\\_\s\-\-\>#) { 
 2881:                     my $newtag = qq|<img src="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"/>|;
 2882:                     $$settings{maindata}{text} =~ s#<\!\-\-\s_/($1)\\_\s\-\-\>#$newtag#;
 2883:                 } elsif ( $$settings{files}[$filecount]{reftext} =~m#^_/(\d+)\\_$# ) {
 2884:                     my $reftag = $1;
 2885:                     my $newtag;
 2886:                     if ($$settings{files}[$filecount]{mimetype} =~ m/^image/) {
 2887:                         $newtag = qq|<img src="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"|;
 2888:                         if ( defined($$settings{files}[$filecount]{registry}{alttext}) ) {
 2889:                             $newtag .= qq| alt="$$settings{files}[$filecount]{registry}{alttext}"|;
 2890:                         }
 2891:                         if ( defined($$settings{files}[$filecount]{registry}{alignment}) )
 2892: {
 2893:                             $newtag .= qq| align="$$settings{files}[$filecount]{registry}{alignment}"|; 
 2894:                         }
 2895:                         if ( defined($$settings{files}[$filecount]{registry}{border}) ) {
 2896:                             $newtag .= qq| border="$$settings{files}[$filecount]{registry}{border}"|;
 2897:                         }
 2898:                         $newtag .= " />";
 2899:                         my $reftext =  $$settings{files}[$filecount]{reftext};
 2900:                         my $fname = $$settings{files}[$filecount]{'relfile'};
 2901:                         $$settings{maindata}{text} =~ s/<!\-\-\sCOMMENT\sBLOCK\sFOR\sEMBEDDED\sFILE:\s$fname[\s\n]+DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n]+//;
 2902: #                      $$settings{maindata}{text} =~ s/DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n]+//;
 2903:                         $$settings{maindata}{text} =~ s/Move\swhole\scomment\sto\schange\sfile\splacement\swithin\spage\.[\s\n]+//;
 2904:                         $$settings{maindata}{text} =~ s/_\/$reftag\\_/$newtag/;
 2905:                         $$settings{maindata}{text} =~ s/END\sOF\sBLOCK\sON\sNEXT\sLINE[\s\n]+//;
 2906:                         $$settings{maindata}{text} =~ s/\-\->//;
 2907: #                      $$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/;
 2908: #                      print STDERR $$settings{maindata}{text};
 2909:                     }
 2910:                 } else {
 2911:                     my $filename=$$settings{files}[$filecount]{'relfile'};
 2912:                     my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 2913:                     $$settings{maindata}{text} =~ s#(src|SRC|value)=("|&quot;)$filename("|&quot;)#$1="$newfilename"#g;
 2914:                 }
 2915:             } elsif ($$settings{files}[$filecount]{fileaction} eq 'link') {
 2916:                 unless (($$settings{files}[$filecount]{packageparent} ne '') && (grep/^$$settings{files}[$filecount]{packageparent}$/,@{$$settings{files}}) ) {
 2917:                     $linktag .= qq|<a href="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"|;
 2918:                     if ($$settings{newwindow} eq "true") {
 2919:                         $linktag .= qq| target="$res$filecount"|;
 2920:                     }
 2921:                     foreach my $entry (keys %{$$settings{files}[$filecount]{registry}}) {
 2922:                         $linktag .= qq| $entry="$$settings{files}[$filecount]{registry}{$entry}"|;
 2923:                     }
 2924:                       $linktag .= qq|>$$settings{files}[$filecount]{linkname}</a><br/>\n|;
 2925:                 }
 2926:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'PACKAGE') || ($$settings{files}[$filecount]{fileaction} eq 'package') ) {
 2927:                my $open_package = '';
 2928:                if ($$settings{files}[$filecount]{'relfile'} =~ m|\.zip$|i) {
 2929:                    $open_package = &expand_zip("$docroot/$res",$$settings{files}[$filecount]{'relfile'});
 2930:                }
 2931:                if ($open_package eq 'ok') {
 2932:                    opendir(DIR,"$docroot/$res");
 2933:                    my @dircontents = grep(!/^\./,readdir(DIR));
 2934:                    closedir(DIR);
 2935:                    push @{$resrcfiles}, @dircontents;
 2936:                    @{$$hrefs{$res}} = @dircontents;
 2937:                    push @{$packages}, $res;
 2938:                }
 2939:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'BROKEN_IMAGE') && ($cms eq 'bb6') ) {
 2940:                 my $filename=$$settings{files}[$filecount]{'relfile'};
 2941:                 my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 2942:                 $$settings{maindata}{text} =~ s#(src|SRC|value)=("|&quot;)$filename("|&quot;)#$1="$newfilename"#g;
 2943:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'LINK') && ($cms eq 'bb6') ) {
 2944:                 my $filename=$$settings{files}[$filecount]{'relfile'};
 2945:                 my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 2946:                 my $filetitle = $$settings{files}[$filecount]{'linkname'};
 2947:                 $$settings{maindata}{text} = '<a href="'.$newfilename.'">'.$filetitle.'</a><br /><br />'. $$settings{maindata}{text};
 2948:             }
 2949:         }
 2950:     }
 2951:     if (defined($$settings{maindata}{textcolor})) {
 2952:         $fontcol =  qq|<font color="$$settings{maindata}{textcolor}">|;
 2953:     }
 2954:     if (defined($$settings{maindata}{text})) {
 2955:         if ($$settings{maindata}{bodytype} eq "S") {
 2956:             $$settings{maindata}{text} =~ s#\n#<br/>#g;
 2957:         }
 2958:         if ($$settings{maindata}{ishtml} eq "false") {
 2959:             if ($$settings{maindata}{isnewline} eq "true") {
 2960:                 $$settings{maindata}{text} =~ s#\n#<br/>#g;
 2961:             }
 2962:         } else {
 2963: #            $$settings{maindata}{text} = &HTML::Entities::decode($$settings{maindata}{text});
 2964:         }
 2965:     }
 2966: 
 2967:     if (!open(FILE,">$destdir/resfiles/$res.html")) {
 2968:         &Apache::lonnet::logthis("IMS import error: Cannot open file - $destdir/resfiles/$res.html - $!");
 2969:     } else {
 2970:         push @{$resrcfiles}, "$res.html";
 2971:         my $htmldoc = 0;
 2972: #        if ($$settings{maindata}{text} =~ m-&lt;(html|HTML)>.+&lt;\\(html|HTML)-) {
 2973:         if ($$settings{maindata}{text} =~ m-<(html|HTML)>-) {
 2974:             $htmldoc = 1;
 2975:         }
 2976:         unless ($htmldoc) {
 2977:             print FILE qq|<html>
 2978: <head>
 2979: <title>$$settings{title}</title>
 2980: </head>
 2981: <body bgcolor='#ffffff'>
 2982: $fontcol
 2983: |;
 2984:         }
 2985:         unless ($$settings{title} eq '') { 
 2986:             print FILE qq|$$settings{title}<br/><br/>\n|;
 2987:         }
 2988:         print FILE qq|
 2989: $$settings{maindata}{text}
 2990: $linktag|;
 2991:         unless ($htmldoc) {
 2992:             if (defined($$settings{maindata}{textcolor})) {
 2993:                 print FILE qq|</font>|;
 2994:             }
 2995:             print FILE qq|
 2996:   </body>
 2997:  </html>|;
 2998:         }
 2999:         close(FILE);
 3000:     }
 3001: }
 3002: 
 3003: 
 3004: sub process_angelboards {
 3005:     my ($context,$destdir,$boards,$timestamp,$crs,$cdom,$uname,$db_handling,$messages,$items,$resources,$hrefs,$tempdir,$longcrs) = @_;
 3006:     for (my $i=0; $i<@{$boards}; $i++) {
 3007:         my %msgidx = ();
 3008:         my $forumtext = '';
 3009:         my $boardname = 'bulletinpage_'.$$timestamp[$i];
 3010:         my $forumfile = $tempdir.'/_assoc/'.$$boards[$i].'/pg'.$$boards[$i].'.htm';
 3011:         my @state = ();
 3012:         my $p = HTML::Parser->new
 3013:         (
 3014:            xml_mode => 1,
 3015:            start_h =>
 3016:            [sub {
 3017:                 my ($tagname, $attr) = @_;
 3018:                 push @state, $tagname;
 3019:                 },  "tagname, attr"],
 3020:            text_h =>
 3021:            [sub {
 3022:                 my ($text) = @_;
 3023:                 if ("@state" eq "html body div div") {
 3024:                     $forumtext = $text;
 3025:                 }
 3026:               }, "dtext"],
 3027:             end_h =>
 3028:             [sub {
 3029:                   my ($tagname) = @_;
 3030:                   pop @state;
 3031:                }, "tagname"],
 3032:         );
 3033:         $p->parse_file($forumfile);
 3034:         $p->eof;
 3035: 
 3036:         my %boardinfo = (
 3037:                   'aaa_title' => $$items{$$resources{$$boards[$i]}{revitm}}{title},
 3038:                   'bbb_content' => $forumtext,
 3039:                   'ccc_webreferences' => '',
 3040:                   'uploaded.lastmodified' => time,
 3041:                   );
 3042:         my $msgcount = 0; 
 3043:                                                                                                      
 3044:         my $putresult = &Apache::lonnet::put($boardname,\%boardinfo,$cdom,$crs);
 3045:         if ($db_handling eq 'importall') {
 3046:             foreach my $msg_id (@{$$messages{$$boards[$i]}}) {
 3047:                 $msgcount ++;
 3048:                 $msgidx{$msg_id} = $msgcount;
 3049:                 my %contrib = (
 3050:                             'sendername' => 'NoName',
 3051:                             'senderdomain' => $cdom,
 3052:                             'screenname' => '',
 3053:                             'message' => $$items{$$resources{$msg_id}{revitm}}{title}
 3054:                             );
 3055:                 unless ( $$items{$$resources{$msg_id}{revitm}}{parentseq} eq $$resources{$$boards[$i]}{revitm} ) {
 3056:                     unless ( $msgidx{$$items{$$items{$$resources{$msg_id}{revitm}}{parentseq}}{resnum}} eq ''){
 3057:                         $contrib{replyto} = $msgidx{$$items{$$items{$$resources{$msg_id}{revitm}}{parentseq}}{resnum}};
 3058:                     }
 3059:                 }
 3060:                 if ( @{$$hrefs{$msg_id}} > 1 )  {
 3061:                     my $newurl = '';
 3062:                     foreach my $file (@{$$hrefs{$msg_id}}) {
 3063:                         unless ($file eq 'pg'.$msg_id.'.htm') {
 3064:                             $newurl = $msg_id.$file;
 3065:                              unless ($longcrs eq '') {
 3066:                                 if ($context eq 'CSTR') {
 3067:                                     if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles") {
 3068:                                         mkdir("/home/httpd/lonUsers/$cdom/$longcrs/userfiles",0755);
 3069:                                     }
 3070:                                     if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl") {
 3071:                                         rename("$destdir/resfiles/$msg_id/$file","/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl");
 3072:                                     }
 3073:                                 }
 3074:                                 $contrib{attachmenturl} = '/uploaded/'.$cdom.'/'.$crs.'/'.$file;
 3075:                             }
 3076:                         }
 3077:                     }
 3078:                 }
 3079:                 my $xmlfile = $tempdir.'/_assoc/'.$msg_id.'/'.$$resources{$msg_id}{file};
 3080:                 &angel_message($msg_id,\%contrib,$xmlfile);
 3081:                 unless ($$resources{$msg_id}{file} eq '') {
 3082:                     unlink($xmlfile);
 3083:                 }
 3084:                 my $symb = 'bulletin___'.$$timestamp[$i].'___adm/wrapper/adm/'.$cdom.'/'.$uname.'/'.$$timestamp[$i].'/bulletinboard';
 3085:                 my $postresult = &addposting($symb,\%contrib,$cdom,$crs);
 3086:             }
 3087:         }
 3088:     }
 3089: }
 3090: 
 3091: # ---------------------------------------------------------------- Process ANGEL message board messages
 3092: sub angel_message {
 3093:     my ($msg_id,$contrib,$xmlfile) = @_;
 3094:     my @state = ();
 3095:     my $p = HTML::Parser->new
 3096:     (
 3097:        xml_mode => 1,
 3098:        start_h =>
 3099:        [sub {
 3100:              my ($tagname, $attr) = @_;
 3101:              push @state, $tagname;
 3102:              },  "tagname, attr"],
 3103:         text_h =>
 3104:         [sub {
 3105:              my ($text) = @_;
 3106:              if ("@state" eq "html body table tr td div small span") {
 3107:                   $$contrib{'plainname'} = $text;
 3108:              } elsif ("@state" eq "html body div div") {
 3109:                   $$contrib{'message'} .= '<br /><br />'.$text;
 3110:              }
 3111:            }, "dtext"],
 3112:          end_h =>
 3113:          [sub {
 3114:                my ($tagname) = @_;
 3115:                pop @state;
 3116:             }, "tagname"],
 3117:     );
 3118:     $p->parse_file($xmlfile);
 3119:     $p->eof;
 3120: }
 3121: 
 3122: # ---------------------------------------------------------------- ANGEL content
 3123: sub angel_content {
 3124:     my ($res,$docroot,$destdir,$settings,$dom,$user,$type,$title,$resrcfiles) = @_;
 3125:     my $xmlfile = $docroot.'/_assoc/'.$res.'/pg'.$res.'.htm';
 3126:     my $filecount = 0;
 3127:     my $firstline;
 3128:     my $lastline;
 3129:     my @buffer = ();
 3130:     my @state;
 3131:     @{$$settings{links}} = ();
 3132:     my $p = HTML::Parser->new
 3133:     (
 3134:        xml_mode => 1,
 3135:        start_h =>
 3136:        [sub {
 3137:              my ($tagname, $attr) = @_;
 3138:              push @state, $tagname;
 3139:             },  "tagname, attr"],
 3140:        text_h =>
 3141:        [sub {
 3142:              my ($text) = @_;
 3143:              if ("@state" eq "html body table tr td div small span") {
 3144:                  $$settings{'subtitle'} = $text;
 3145:              } elsif ("@state" eq "html body div div") {
 3146:                  $$settings{'text'} = $text;
 3147:              } elsif ("@state" eq "html body div div a") {
 3148:                 push @{$$settings{'links'}}, $text;
 3149:              }
 3150:             }, "dtext"],
 3151:        end_h =>
 3152:        [sub {
 3153:              my ($tagname) = @_;
 3154:              pop @state;
 3155:             }, "tagname"],
 3156:     );
 3157:     $p->parse_file($xmlfile);
 3158:     $p->eof;
 3159:     if ($type eq "PAGE") {
 3160:         open(FILE,"<$xmlfile");
 3161:         @buffer = <FILE>;
 3162:         close(FILE);
 3163:         chomp(@buffer);
 3164:         $firstline = -1;
 3165:         $lastline = 0;
 3166:         for (my $i=0; $i<@buffer; $i++) {
 3167:             if (($firstline == -1) && ($buffer[$i] =~ m/<div\sclass="normalDiv"><div\sclass="normalSpan">/)) {
 3168:                 $firstline = $i;
 3169:                 $buffer[$i] = substr($buffer[$i],index($buffer[$i],'"normalSpan"')+13);
 3170:             }
 3171:             if (($firstline > -1) && ($buffer[$i] =~ m-<p></p></div></div>-)) {
 3172:                 $buffer[$i] = substr($buffer[$i],0,index($buffer[$i],'<p></p></div></div>'));
 3173:                 $lastline = $i;
 3174:             }
 3175:         }
 3176:     }
 3177:     open(FILE,">$destdir/resfiles/$res.html");
 3178:     push @{$resrcfiles}, "$res.html";
 3179:     print FILE qq|<html>
 3180: <head>
 3181: <title>$title</title>
 3182: </head>
 3183: <body bgcolor='#ffffff'>
 3184:     |;
 3185:     unless ($title eq '') {
 3186:         print FILE qq|<b>$title</b><br/>\n|;
 3187:     }
 3188:     unless ($$settings{subtitle} eq '') {
 3189:         print FILE qq|$$settings{subtitle}<br/>\n|;
 3190:     }
 3191:     print FILE "<br/>\n";
 3192:     if ($type eq "LINK") {
 3193:         foreach my $link (@{$$settings{links}}) {
 3194:             print FILE qq|<a href="$link">$link</a><br/>\n|; 
 3195:         }
 3196:     } elsif ($type eq "PAGE") {
 3197:         if ($firstline > -1) {
 3198:             for (my $i=$firstline; $i<=$lastline; $i++) {
 3199:                 print FILE "$buffer[$i]\n";
 3200:             }
 3201:         }
 3202:     }
 3203:     print FILE qq|
 3204:   </body>
 3205:  </html>|;
 3206:     close(FILE);
 3207: }
 3208: 
 3209: # ---------------------------------------------------------------- WebCT content
 3210: sub webct4_content {
 3211:     my ($res,$docroot,$destdir,$settings,$dom,$user,$type,$title,$resrcfiles) = @_;
 3212:     if (!open(FILE,">$destdir/resfiles/$res.html")) {
 3213:         &Apache::lonnet::logthis("IMS import error: Cannot open file - $destdir/resfiles/$res.html - $!");
 3214:     } else {
 3215:         push(@{$resrcfiles}, "$res.html");
 3216:         my $linktag = '';
 3217:         if (defined($$settings{url})) {
 3218:             $linktag = qq|<a href="$$settings{url}"|;
 3219:             if ($title ne '') {
 3220:                 $linktag .= qq|>$title</a>|;
 3221:             } else {
 3222:                 $linktag .= qq|>$$settings{url}|;
 3223:             }
 3224:         }
 3225:         print FILE qq|<html>
 3226: <head>
 3227: <title>$title</title>
 3228: </head>
 3229: <body bgcolor='#ffffff'>
 3230: $linktag
 3231: </body>
 3232: </html>|;
 3233:         close(FILE);
 3234:     }
 3235: }
 3236: 
 3237: # ---------------------------------------------------------------- WebCT assessments
 3238: sub webct4_assessment {
 3239:     my ($res,$docroot,$destdir,$settings,$dom,$user,$type,$title,$resrcfiles) = @_;
 3240:     print STDERR "webct assessment detected - $res, $docroot, $destdir, $type,$title\n";
 3241: }
 3242: 
 3243: 1;
 3244: __END__

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