File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.43: download - view: text, annotated - select for diffs
Sat Aug 23 21:08:29 2008 UTC (15 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Keep count of files renamed to report as files copied when reporting results of IMS import into CSTR.

    1: # Copyright Michigan State University Board of Trustees
    2: #
    3: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    4: #
    5: # LON-CAPA is free software; you can redistribute it and/or modify
    6: # it under the terms of the GNU General Public License as published by
    7: # the Free Software Foundation; either version 2 of the License, or
    8: # (at your option) any later version.
    9: #
   10: # LON-CAPA is distributed in the hope that it will be useful,
   11: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13: # GNU General Public License for more details.
   14: #
   15: # You should have received a copy of the GNU General Public License
   16: # along with LON-CAPA; if not, write to the Free Software
   17: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18: #
   19: # /home/httpd/html/adm/gpl.txt
   20: #
   21: # http://www.lon-capa.org/
   22: #
   23: 
   24: package Apache::imsprocessor;
   25: 
   26: use Apache::lonnet;
   27: use Apache::loncleanup;
   28: use Apache::lonlocal;
   29: use LWP::UserAgent;
   30: use HTTP::Request::Common;
   31: use LONCAPA::Configuration;
   32: use strict;
   33: 
   34: sub ims_config {
   35:     my ($areas,$cmsmap,$areaname) = @_;
   36:     @{$areas} = ("doc","extlink","announce","staff","board","quiz","survey","pool","users","question");
   37:     %{$$cmsmap{bb5}} = (
   38:                 announce => 'resource/x-bb-announcement',
   39:                 board => 'resource/x-bb-discussionboard',
   40:                 doc => 'resource/x-bb-document',
   41:                 extlink => 'resource/x-bb-externallink',
   42:                 pool => 'assessment/x-bb-pool',
   43:                 quiz => 'assessment/x-bb-quiz',
   44:                 staff => 'resource/x-bb-staffinfo',
   45:                 survey => 'assessment/x-bb-survey',
   46:                 users => 'course/x-bb-user',
   47:                 );
   48:     %{$$cmsmap{bb6}} = (
   49:                 announce => 'resource/x-bb-announcement',
   50:                 board => 'resource/x-bb-discussionboard',
   51:                 doc => 'resource/x-bb-document',
   52:                 extlink => 'resource/x-bb-externallink',
   53:                 pool => 'assessment/x-bb-qti-pool',
   54:                 quiz => 'assessment/x-bb-qti-test',
   55:                 staff => 'resource/x-bb-staffinfo',
   56:                 survey => 'assessment/x-bb-survey',
   57:                 users => 'course/x-bb-user',
   58:                 );
   59:     $$cmsmap{bb6}{conference} = 'resource/x-bb-conference';
   60:     %{$$cmsmap{angel}} =  (
   61:                 board => 'BOARD',
   62:                 extlink => 'LINK',
   63:                 msg => 'MESSAGE',
   64:                 quiz => 'QUIZ',
   65:                 survey => 'FORM',
   66:                 );
   67:     @{$$cmsmap{angel}{doc}} = ('FILE','PAGE');
   68:     %{$$cmsmap{webctce4}} = (
   69:                 quiz => 'webctquiz',
   70:                 survey => 'webctsurvey',
   71:                 doc => 'webcontent'
   72:                 );
   73:     %{$$cmsmap{webctvista4}} = (
   74:                 question => 'webct.question',
   75:                 quiz => 'webct.assessment',
   76:                 survey => 'webctsurvey',
   77:                 doc => 'webcontent'
   78:                 );
   79:     %{$areaname} = &Apache::lonlocal::texthash (
   80:                 announce => 'Announcements',
   81:                 board => 'Discussion Boards',
   82:                 doc => 'Documents, pages, and folders',
   83:                 extlink => 'Links to external sites',
   84:                 pool => 'Question pools',
   85:                 quiz => 'Quizzes',
   86:                 question => 'Assessment Questions',
   87:                 staff => 'Staff information',
   88:                 survey => 'Surveys',
   89:                 users => 'Enrollment',
   90:                 );
   91: }
   92:  
   93: sub create_tempdir {
   94:     my ($context,$pathinfo,$timenow) = @_;   
   95:     my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
   96:     my $tempdir;
   97:     if ($context eq 'DOCS') {
   98:         $tempdir =  $$configvars{'lonDaemons'}.'/tmp/'.$pathinfo;
   99:         if (!-e "$tempdir") {
  100:             mkdir("$tempdir",0770);
  101:         } 
  102:         $tempdir .= '/'.$timenow;
  103:         if (!-e "$tempdir") {
  104:             mkdir("$tempdir",0770);
  105:         } 
  106:     } elsif ($context eq "CSTR") {
  107:         if (!-e "$pathinfo/temp") {
  108:             mkdir("$pathinfo/temp",0770);
  109:         }
  110:         $tempdir =  $pathinfo.'/temp';
  111:     }
  112:     return $tempdir;
  113: }
  114: 
  115: sub uploadzip {
  116:     my ($context,$tempdir,$source) = @_;
  117:     my $fname;
  118:     if ($context eq 'DOCS') {
  119:         $fname=$env{'form.uploadname.filename'};
  120: # Replace Windows backslashes by forward slashes
  121:         $fname=~s/\\/\//g;
  122: # Get rid of everything but the actual filename
  123:         $fname=~s/^.*\/([^\/]+)$/$1/;
  124: # Replace spaces by underscores
  125:         $fname=~s/\s+/\_/g;
  126: # Replace all other weird characters by nothing
  127:         $fname=~s/[^\w\.\-]//g;
  128: # See if there is anything left
  129:         unless ($fname) { return 'error: no uploaded file'; }
  130: # Save the file
  131:         chomp($env{'form.uploadname'});
  132:         open(my $fh,'>'.$tempdir.'/'.$fname);
  133:         print $fh $env{'form.uploadname'};
  134:         close($fh);
  135:     } elsif ($context eq 'CSTR') {
  136:         if ($source =~ m/\/([^\/]+)$/) {
  137:             $fname = $1;
  138:             my $destination = $tempdir.'/'.$fname;
  139:             rename($source,$destination);
  140:         }
  141:     }
  142:     return $fname;   
  143: }
  144: 
  145: sub expand_zip {
  146:     my ($tempdir,$filename) = @_;
  147:     my $zipfile = "$tempdir/$filename";
  148:     if (!-e "$zipfile") {
  149:         return 'no zip';
  150:     }
  151:     if ($filename =~ m|\.zip$|i) {
  152:         open(OUTPUT, "unzip -o $zipfile -d $tempdir  2> /dev/null |");
  153:         close(OUTPUT);
  154:     } else {
  155:         return 'nozip';
  156:     }
  157:     if ($filename =~ m|\.zip$|i) {
  158:         unlink($zipfile);
  159:     }
  160:     return 'ok';
  161: }
  162: 
  163: sub process_manifest {
  164:     my ($cms,$tempdir,$resources,$items,$hrefs,$resinfo,$phase,$includedres,$includeditems) = @_;
  165:     my %toc = (
  166:               bb6 => 'organization',
  167:               bb5 => 'tableofcontents',
  168:               angel => 'organization',
  169:               webctce4 => 'organization',
  170:               webctvista4 => 'organization'
  171:               );
  172:     my @seq = "Top";
  173:     %{$$items{'Top'}} = (
  174:                       contentscount => 0,
  175:                       resnum => 'toplevel',
  176:                       );
  177:     %{$$resources{'toplevel'}} = (
  178:                                   revitm => 'Top'
  179:                                  );
  180:  
  181:     if ($cms eq 'angel') {
  182:         $$resources{'toplevel'}{type} = "FOLDER";
  183:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  184:         $$resources{'toplevel'}{type} = 'resource/x-bb-document';
  185:     } else {
  186:         $$resources{'toplevel'}{type} = 'webcontent';
  187:     }
  188: 
  189:     unless (-e "$tempdir/imsmanifest.xml") {
  190:         return 'nomanifest';
  191:     }
  192: 
  193:     my $xmlfile = $tempdir.'/imsmanifest.xml';
  194:     &parse_manifest($cms,$phase,$tempdir,$xmlfile,\%toc,$includedres,
  195:                     $includeditems,$items,$resources,$resinfo,$hrefs,\@seq);
  196:     return 'ok' ;
  197: }
  198: 
  199: sub parse_manifest {
  200:     my ($cms,$phase,$tempdir,$xmlfile,$toc,$includedres,$includeditems,$items,
  201:         $resources,$resinfo,$hrefs,$seq) = @_;
  202:     my @state = ();
  203:     my $itm = '';
  204:     my %contents = ();
  205:     my $identifier = '';
  206:     my @allidentifiers = ();
  207:     my $lastitem;
  208:     my $p = HTML::Parser->new
  209:     (
  210:        xml_mode => 1,
  211:        start_h =>
  212:            [sub {
  213:                 my ($tagname, $attr) = @_;
  214:                 push @state, $tagname;
  215:                 my $start = @state - 3;
  216:                 if ( ($state[0] eq "manifest") && ($state[1] eq "organizations") && ($state[2] eq $$toc{$cms}) ) {
  217:                     if ($state[-1] eq 'item') {
  218:                         $itm = $attr->{identifier};
  219:                         if ($$includeditems{$itm} || $phase ne 'build') {
  220:                             %{$$items{$itm}} = ();
  221:                             $$items{$itm}{contentscount} = 0;
  222:                             @{$$items{$itm}{contents}} = ();
  223:                             if ($cms eq 'bb5' || $cms eq 'bb6' || $cms eq 'webctce4' || $cms eq 'webctvista4') {
  224:                                 $$items{$itm}{resnum} = $attr->{identifierref};
  225:                                 if ($cms eq 'bb5') {
  226:                                     $$items{$itm}{title} = $attr->{title};
  227:                                 }
  228:                             } elsif ($cms eq 'angel') {
  229:                                 if ($attr->{identifierref} =~ m/^res(.+)$/) {
  230:                                     $$items{$itm}{resnum} = $1;
  231:                                 }
  232:                             }
  233:                             unless (defined(%{$$resources{$$items{$itm}{resnum}}}) ) {
  234:                                 %{$$resources{$$items{$itm}{resnum}}} = ();
  235:                             }
  236:                             $$resources{$$items{$itm}{resnum}}{revitm} = $itm;
  237:                             if ($start > @{$seq}) {
  238:                                 unless ($lastitem eq '') {
  239:                                     push @{$seq}, $lastitem;
  240:                                     unless ( defined($contents{$$seq[-1]}) ) {
  241:                                         @{$contents{$$seq[-1]}} = ();
  242:                                     }
  243:                                     push @{$contents{$$seq[-1]}},$itm;
  244:                                     $$items{$itm}{parentseq} = $$seq[-1];
  245:                                 }
  246:                             } elsif ($start < @{$seq}) {
  247:                                 my $diff = @{$seq} - $start;
  248:                                 while ($diff > 0) {
  249:                                     pop @{$seq};
  250:                                     $diff --;
  251:                                 }
  252:                                 if (@{$seq}) {
  253:                                     push @{$contents{$$seq[-1]}}, $itm;
  254:                                 }
  255:                             } else {
  256:                                 push @{$contents{$$seq[-1]}}, $itm;
  257:                             }
  258:                             my $path;
  259:                             if (@{$seq} > 1) {
  260:                                 $path = join(',',@{$seq});
  261:                             } elsif (@{$seq} > 0) {
  262:                                 $path = $$seq[0];
  263:                             }
  264:                             $$items{$itm}{filepath} = $path;
  265:                             if ($cms eq 'bb5' || $cms eq 'bb6') {
  266:                                 if ($$items{$itm}{filepath} eq 'Top') {
  267:                                     $$items{$itm}{resnum} = $itm;
  268:                                     $$resources{$$items{$itm}{resnum}}{type} = 'resource/x-bb-document';
  269:                                     $$resources{$$items{$itm}{resnum}}{revitm} = $itm;
  270:                                     $$resinfo{$$items{$itm}{resnum}}{'isfolder'} = 'true';
  271:                                 }
  272:                             }
  273:                             $$items{$$seq[-1]}{contentscount} ++;
  274:                             $$resources{$$items{$itm}{resnum}}{seqref} = $seq;
  275:                             $lastitem = $itm;
  276:                         }
  277:                     }
  278:                     if ($cms eq 'webctce4') {
  279:                         if (($state[-1] eq "webct:properties") && (@state > 4)) {
  280:                             $$items{$itm}{properties} = $attr->{identifierref};
  281:                         }
  282:                     }
  283:                 } elsif ("@state" eq "manifest resources resource" ) {
  284:                     $identifier = $attr->{identifier};
  285:                     push(@allidentifiers,$identifier);
  286:                     if ($$includedres{$identifier} || $phase ne 'build') { 
  287:                         if ($cms eq 'bb5' || $cms eq 'bb6') {
  288:                             $$resources{$identifier}{file} = $attr->{file};
  289:                             $$resources{$identifier}{type} = $attr->{type};
  290:                         } elsif ($cms eq 'webctce4') {
  291:                             $$resources{$identifier}{type} = $attr->{type};
  292:                             $$resources{$identifier}{file} = $attr->{href};
  293:                         } elsif ($cms eq 'webctvista4') {
  294:                             $$resources{$identifier}{type} = $attr->{type};
  295:                             $$resources{$identifier}{'webct:coType'} = $attr->{'webct:coType'};
  296:                         } elsif ($cms eq 'angel') {
  297:                             $identifier = substr($identifier,3);
  298:                             if ($attr->{href} =~ m-^_assoc/$identifier/(.+)$-) {
  299:                                 $$resources{$identifier}{file} = $1;
  300:                             }
  301:                         }
  302:                         @{$$hrefs{$identifier}} = ();
  303:                     }
  304:                 } elsif ("@state" eq "manifest resources resource file") {
  305:                     if ($$includedres{$identifier} || $phase ne 'build') {
  306:                         if ($cms eq 'webctvista4') {
  307:                             $$resources{$identifier}{file} = $attr->{href};
  308:                         }
  309:                         if ($cms eq 'bb5' || $cms eq 'bb6' || 
  310:                             $cms eq 'webctce4' || $cms eq 'webctvista4') {
  311:                             push @{$$hrefs{$identifier}},$attr->{href};
  312:                             if ($$resources{$identifier}{type} eq 
  313:                                 'webct.manifest') {
  314:                                 my $manifestfile = $tempdir.'/'.$attr->{href};
  315:                                 my $currseqref = [];
  316:                                 if ($itm) {
  317:                                     $currseqref =   
  318:                                     $$resources{$$items{$itm}{resnum}}{seqref};
  319:                                 }
  320:                                 &parse_manifest($cms,$phase,$tempdir,$manifestfile,
  321:                                                 $toc,$includedres,$includeditems,
  322:                                                 $items,$resources,$resinfo,
  323:                                                 $hrefs,$currseqref);
  324:                             }
  325:                         } elsif ($cms eq 'angel') {
  326:                             if ($attr->{href} =~ m/^_assoc\\$identifier\\(.+)$/) {
  327:                                 push @{$$hrefs{$identifier}},$1;
  328:                             } elsif ($attr->{href} =~ m/^Icons\\icon(\w+)\.gif/) {
  329:                                 $$resources{$identifier}{type} = $1;
  330:                             }
  331:                         } 
  332:                     }
  333:                 } elsif ("@state" eq "manifest webct:ContentObject") {
  334:                     foreach my $ident (@allidentifiers) {
  335:                         if ($$resources{$ident}{type} eq 'ims_qtiasiv1p2') {
  336:                             $$resources{$ident}{type} = $attr->{'webct:coType'};
  337:                         }
  338:                     }
  339:                 }
  340:            }, "tagname, attr"],
  341:         text_h =>
  342:             [sub {
  343:                 my ($text) = @_;
  344:                 if ("@state" eq "manifest metadata lom general title langstring") {
  345:                     $$items{'Top'}{title} = $text;
  346:                 }
  347:                 if ($state[0] eq "manifest" && $state[1] eq "organizations" && $state[2] eq $$toc{$cms} && $state[-1] eq "title") {
  348:                     if ($$includeditems{$itm} || $phase ne 'build') {
  349:                         if ($cms eq 'angel' || $cms eq 'bb6' || $cms eq 'webctvista4') {
  350:                             $$items{$itm}{title} = $text;
  351:                         }
  352:                         if ($cms eq 'webctce4') {
  353:                             $$items{$itm}{title} = $text;
  354:                             $$items{$itm}{title} =~ s/(<[^>]*>)//g;
  355:                         }
  356:                     }
  357:                 }
  358:               }, "dtext"],
  359:         end_h =>
  360:               [sub {
  361:                   my ($tagname) = @_;
  362:                   pop @state;
  363:                }, "tagname"],
  364:     );
  365:     $p->parse_file($xmlfile);
  366:     $p->eof;
  367:     foreach my $itm (keys %contents) {
  368:         @{$$items{$itm}{contents}} = @{$contents{$itm}};
  369:     }
  370: }
  371: 
  372: sub get_imports {
  373:     my ($includeditems,$items,$resources,$importareas,$itm) = @_;
  374:     if (exists($$items{$itm}{resnum})) {
  375:         if ($$importareas{$$resources{$$items{$itm}{resnum}}{type}}) {
  376:             unless (exists($$includeditems{$itm})) {
  377:                 $$includeditems{$itm} = 1;
  378:             }
  379:         }
  380:     }
  381:     if ($$items{$itm}{contentscount} > 0) {
  382:         foreach my $child (@{$$items{$itm}{contents}}) {
  383:             &get_imports($includeditems,$items,$resources,$importareas,$child);
  384:         }
  385:     }
  386: }
  387: 
  388: sub get_parents {
  389:     my ($includeditems,$items,$itm) = @_;
  390:     my @pathitems = ();
  391:     if ($$items{$itm}{filepath} =~ m/,/) {
  392:        @pathitems = split/,/,$$items{$itm}{filepath};
  393:     } else {
  394:        $pathitems[0] = $$items{$itm}{filepath};
  395:     }
  396:     foreach (@pathitems) {
  397:         $$includeditems{$_} = 1;
  398:     }
  399: }
  400: 
  401: sub target_resources {
  402:     my ($resources,$oktypes,$targets) = @_;
  403:     foreach my $key (keys %{$resources}) {
  404:         if ( defined($$oktypes{$$resources{$key}{type}}) ) {
  405:             push @{$targets}, $key;
  406:         }
  407:     }
  408:     return;
  409: }
  410: 
  411: sub copy_resources {
  412:     my ($context,$cms,$hrefs,$tempdir,$targets,$url,$crs,$cdom,$destdir,$timenow,$assessmentfiles,$total) = @_;
  413:     if ($context eq 'DOCS') {
  414:         foreach my $key (sort keys %{$hrefs}) {
  415:             if (grep/^$key$/,@{$targets}) {
  416:                 %{$$url{$key}} = ();
  417:                 foreach my $file (@{$$hrefs{$key}}) {
  418:                     my $source = $tempdir.'/'.$key.'/'.$file;
  419:                     if ($cms eq 'webctce4' || $cms eq 'webctvista4') {
  420:                         $source = $tempdir.'/'.$file;
  421:                     }
  422:                     my $filename = '';
  423:                     my $fpath = $timenow.'/resfiles/'.$key.'/';
  424:                     if ($cms eq 'angel') {
  425:                         if ($file eq 'pg'.$key.'.htm') {
  426:                             next;
  427:                         }
  428:                     }
  429:                     $file =~ s-\\-/-g;
  430:                     my $copyfile = $file;
  431:                     if ($cms eq 'webctce4' || $cms eq 'webctvista4') {
  432:                         if ($file =~ m-/my_files/(.+)$-) {
  433:                             $copyfile = $1;
  434:                         }
  435:                     }
  436:                     unless ((($cms eq 'webctce4') && ($copyfile =~ m/questionDB\.xml$/ || $copyfile =~ m/quiz_QIZ_\d+\.xml$/ || $copyfile =~ m/properties_QIZ_\d+\.xml$/)) || (($cms eq 'webctvista4') && (grep/^$key$/,@{$assessmentfiles}) && $file =~ /\.xml$/))    {
  437:                         $copyfile = $fpath.$copyfile;
  438:                         my $fileresult;
  439:                         if (-e $source) {
  440:                             $fileresult = &Apache::lonnet::process_coursefile('copy',$crs,$cdom,$copyfile,$source);
  441:                         }
  442:                     }
  443:                 }
  444:             }
  445:         }
  446:     } elsif ($context eq 'CSTR') {
  447:         if (!-e "$destdir/resfiles") {
  448:             mkdir("$destdir/resfiles",0770);
  449:         }
  450:         foreach my $key (sort keys %{$hrefs}) {
  451:             if (grep/^$key$/,@{$targets}) {
  452:                 foreach my $file (@{$$hrefs{$key}}) {
  453:                     $file =~ s-\\-/-g;
  454:                     if ( ($cms eq 'angel' && $file ne 'pg'.$key.'.htm') || ($cms eq 'bb5') || ($cms eq 'bb6')) {
  455:                         if (!-e "$destdir/resfiles/$key") {
  456:                             mkdir("$destdir/resfiles/$key",0770);
  457:                         }
  458:                         my $filepath = $file;
  459:                         my $front = '';
  460:                         while ($filepath =~ m-(\w+)/(.+)-) {
  461:                             $front .= $1.'/';
  462:                             $filepath = $2;
  463:                             my $fulldir = "$destdir/resfiles/$key/$front";
  464:                             chop($fulldir);
  465:                             if (!-e "$fulldir") {
  466:                                 mkdir("$fulldir",0770);
  467:                             }
  468:                         }
  469:                         my $renameres;
  470:                         if ($cms eq 'angel') {
  471:                             $renameres = rename("$tempdir/_assoc/$key/$file","$destdir/resfiles/$key/$file");
  472:                         } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  473:                             $renameres = rename("$tempdir/$key/$file","$destdir/resfiles/$key/$file");
  474:                         }
  475:                         if ($renameres) {
  476:                             if (ref($total) eq 'HASH') {
  477:                                 $$total{'file'} ++;
  478:                             }
  479:                         } else {
  480:                             &Apache::lonnet::logthis("IMS import error: $cms - renaming failed for file $file");
  481:                         }
  482:                     } elsif ($cms eq 'webctce4') {
  483:                         if ($file =~ m-/my_files/(.+)$-) {
  484:                             my $copyfile = $1;
  485:                             if ($copyfile =~ m-^[^/]+/[^/]+-) {
  486:                                 my @dirs = split/\//,$copyfile;
  487:                                 my $path = "$destdir/resfiles";
  488:                                 while (@dirs > 1) {
  489:                                     $path .= '/'.$dirs[0];
  490:                                     if (!-e "$path") {
  491:                                         mkdir("$path",0755);
  492:                                     }
  493:                                     shift @dirs;
  494:                                 }
  495:                             }
  496:                             if (-e "$tempdir/$file") {
  497:                                 my $renameres = rename("$tempdir/$file","$destdir/resfiles/$copyfile");
  498:                                 if ($renameres) {
  499:                                     if (ref($total) eq 'HASH') {
  500:                                         $$total{'file'} ++;
  501:                                     }
  502:                                 } else {
  503:                                     &Apache::lonnet::logthis("IMS import error: WebCT4 - renaming failed for file $file");
  504:                                 }
  505:                             }
  506:                         } elsif ($file !~ m-/data/(.+)$-) {
  507:                             &Apache::lonnet::logthis("IMS import error: WebCT4 - file $file is in unexpected location");
  508:                         }
  509:                     }
  510:                 }
  511:             }
  512:         }
  513:     }
  514: }
  515: 
  516: sub process_resinfo {
  517:     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) = @_;
  518:     my $board_id = time;
  519:     my $board_count = 0;
  520:     my $dbparse = 0;
  521:     my $announce_handling = 'include';
  522:     my $longcrs = '';
  523:     my %allassessments = ();
  524:     my %allquestions = ();
  525:     my %qzdbsettings = ();
  526:     my %catinfo = ();
  527:     if ($crs =~ m/^(\d)(\d)(\d)/) {
  528:         $longcrs = $1.'/'.$2.'/'.$3.'/'.$crs;
  529:     }
  530:     if ($context eq 'CSTR') {
  531:         if (!-e "$destdir/resfiles") {
  532:             mkdir("$destdir/resfiles",0770);
  533:         }
  534:     }
  535:     if ($cms eq 'angel') {
  536:         my $currboard = '';
  537:         foreach my $key (sort keys %{$resources}) {
  538:           if (grep/^$key$/,@{$targets}) {
  539:             if ($$resources{$key}{type} eq "BOARD") {
  540:                 push @{$boards}, $key;
  541:                 $$boardnum{$$resources{$key}{revitm}} = $board_count;
  542:                 $currboard = $key;
  543:                 @{$$messages{$key}} = ();
  544:                 $$timestamp[$board_count] = $board_id;
  545:                 $board_id ++;
  546:                 $board_count ++;
  547:             } elsif ($$resources{$key}{type} eq "MESSAGE") {
  548:                 push @{$$messages{$currboard}}, $key;
  549:             } elsif ($$resources{$key}{type} eq "PAGE" || $$resources{$key}{type} eq "LINK") {
  550:                 %{$$resinfo{$key}} = ();
  551:                 &angel_content($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$$resources{$key}{type},$$items{$$resources{$key}{revitm}}{title},$resrcfiles);
  552:             } elsif ($$resources{$key}{type} eq "QUIZ") {
  553:                 %{$$resinfo{$key}} = ();
  554:                 push @{$quizzes}, $key;
  555: #               &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  556:             } elsif ($$resources{$key}{type} eq "FORM") {
  557:                 %{$$resinfo{$key}} = ();
  558:                 push @{$surveys}, $key;
  559: #                &angel_assessment($key,$docroot,$dirname,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  560:             } elsif ($$resources{$key}{type} eq "DROPBOX") {
  561:                 %{$$resinfo{$key}} = ();
  562:             }
  563:           }
  564:         }
  565:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
  566:         foreach my $key (sort keys %{$resources}) {
  567:           if (grep/^$key$/,@{$targets}) {
  568:             if ($$resources{$key}{type} eq "resource/x-bb-document") {
  569:                 unless ($$items{$$resources{$key}{revitm}}{filepath} eq 'Top') {
  570:                     %{$$resinfo{$key}} = ();
  571:                     &process_content($cms,$key,$context,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$resrcfiles,$packages,$hrefs);
  572:                 }
  573:             } elsif ($$resources{$key}{type} eq "resource/x-bb-staffinfo") {
  574:                 %{$$resinfo{$key}} = ();
  575:                 &process_staff($key,$docroot,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  576:             } elsif ($$resources{$key}{type} eq "resource/x-bb-externallink") {
  577:                 %{$$resinfo{$key}} = ();
  578:                 &process_link($key,$docroot,$destdir,\%{$$resinfo{$key}},$resrcfiles);
  579:             } elsif ($$resources{$key}{type} eq "resource/x-bb-discussionboard") {
  580:                 %{$$resinfo{$key}} = ();
  581:                 unless ($db_handling eq 'ignore') {
  582:                     push @{$boards}, $key;
  583:                     $$timestamp[$board_count] = $board_id;
  584:                     &process_db($key,$docroot,$destdir,$board_id,$crs,$cdom,$db_handling,$uname,\%{$$resinfo{$key}},$longcrs);
  585:                     $board_id ++;
  586:                     $board_count ++;
  587:                 }
  588:             } elsif ($$resources{$key}{type} =~/assessment\/x\-bb\-(qti\-)?pool/) {
  589:                 %{$$resinfo{$key}} = ();
  590:                 &process_assessment($cms,$context,$key,$docroot,'pool',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse,$resources,$items,\%catinfo,\%qzdbsettings,$hrefs,\%allquestions);
  591:                 push @{$pools}, $key;
  592:             } elsif ($$resources{$key}{type} =~ /assessment\/x\-bb\-(qti\-)?quiz/) {
  593:                 %{$$resinfo{$key}} = ();
  594:                 &process_assessment($cms,$context,$key,$docroot,'quiz',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse,$resources,$items,\%catinfo,\%qzdbsettings,$hrefs,\%allquestions);
  595:                 push @{$quizzes}, $key;
  596:             } elsif ($$resources{$key}{type} =~ /assessment\/x\-bb\-(qti\-)?survey/) {
  597:                 %{$$resinfo{$key}} = ();
  598:                 &process_assessment($cms,$context,$key,$docroot,'survey',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse,$resources,$items,\%catinfo,\%qzdbsettings,$hrefs,\%allquestions);
  599:                 push @{$surveys}, $key;
  600:             } elsif ($$resources{$key}{type} eq "assessment/x-bb-group") {
  601:                 %{$$resinfo{$key}} = ();
  602:                 push @{$groups}, $key;
  603:                 &process_group($key,$docroot,$destdir,\%{$$resinfo{$key}});
  604:             } elsif ($$resources{$key}{type} eq "resource/x-bb-user") {   
  605:                 %{$$resinfo{$key}} = ();
  606:                 unless ($user_handling eq 'ignore') {
  607:                     &process_user($key,$docroot,$destdir,\%{$$resinfo{$key}},$crs,$cdom,$user_handling);
  608:                 }
  609:             } elsif ($$resources{$key}{type} eq "resource/x-bb-announcement") {
  610:                 unless ($announce_handling eq 'ignore') {
  611:                     push @{$announcements}, $key;
  612:                     %{$$resinfo{$key}} = ();
  613:                     &process_announce($key,$docroot,$destdir,\%{$$resinfo{$key}},$resinfo,$seqstem,$resrcfiles);
  614:                 }
  615:             }
  616:           }
  617:         }
  618:         if (@{$announcements}) {
  619:             $$items{'Top'}{'contentscount'} ++;
  620:         }
  621:         if (@{$boards}) {
  622:             $$items{'Top'}{'contentscount'} ++;
  623:         }
  624:         if (@{$quizzes}) {
  625:             $$items{'Top'}{'contentscount'} ++;
  626:         }
  627:         if (@{$surveys}) {
  628:             $$items{'Top'}{'contentscount'} ++;
  629:         }
  630:         if (@{$pools}) {
  631:             $$items{'Top'}{'contentscount'} ++;
  632:         }
  633:     } elsif ($cms eq 'webctce4') {
  634:         foreach my $key (sort keys %{$resources}) {
  635:             if (grep/^$key$/,@{$targets}) {
  636:                 if ($$resources{$key}{type} eq "webcontent") {
  637:                     %{$$resinfo{$key}} = ();
  638:                     &webct4_content($key,$docroot,$destdir,\%{$$resinfo{$key}},$udom,$uname,$$resources{$key}{type},$$items{$$resources{$key}{revitm}}{title},$resrcfiles);
  639:                 } elsif ($$resources{$key}{type} eq "webctquiz") {
  640:                     &process_assessment($cms,$context,$key,$docroot,'quiz',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse,$resources,$items,\%catinfo,\%qzdbsettings,$hrefs,\%allquestions);
  641:                 }
  642:             }
  643:         }
  644:     } elsif ($cms eq 'webctvista4') {
  645:         foreach my $key (sort keys %{$resources}) {
  646:             if (grep/^$key$/,@{$targets}) {
  647:                 %{$$resinfo{$key}} = ();
  648:                 if ($$resources{$key}{type} eq 'webct.question') {
  649:                     $allquestions{$key} = 1;
  650:                 } elsif ($$resources{$key}{type} eq 'webct.assessment') {
  651:                     $allassessments{$key} = 1;
  652:                 }
  653:             }
  654:         }
  655:         if (keys(%allassessments) > 0) {
  656:             foreach my $key (sort(keys(%allassessments))) {
  657:                 &process_assessment($cms,$context,$key,$docroot,'quiz',$dirname,$destdir,\%{$$resinfo{$key}},$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,\$dbparse,$resources,$items,\%catinfo,\%qzdbsettings,$hrefs,\%allquestions);
  658:             }
  659:         } elsif (keys(%allquestions) > 0) {
  660:             my %catinfo = ();
  661:             my @allids = ();
  662:             my @allquestids = ();
  663:             my %allanswers = ();
  664:             my %allchoices = ();
  665:             my $containerdir;
  666:             my $newdir;
  667:             my $cid;
  668:             my $randompickflag = 0;
  669:             if ($context eq 'DOCS') {
  670:                 $cid = $env{'request.course.id'};
  671:             }
  672:             my $destresdir = $destdir;
  673:             if ($context eq 'CSTR') {
  674:                 $destresdir =~ s|/home/$uname/public_html/|/res/$udom/$uname/|;
  675:             } elsif ($context eq 'DOCS') {
  676:                 $destresdir =~ s|^/home/httpd/html/userfiles|/uploaded|;
  677:             }
  678:             foreach my $res (sort(keys(%allquestions))) {
  679:                 my $parent = $allquestions{$res};
  680:                 &parse_webctvista4_question($res,$docroot,$resources,$hrefs,\%qzdbsettings,\@allquestids,\%allanswers,\%allchoices,$parent,\%catinfo);
  681:             }
  682:             &build_category_sequences($destdir,\%catinfo,$sequencesfiles,$pagesfiles,$destresdir,$newdir,$cms,$total,$randompickflag,$context,$udom,$uname,$dirname,$cid,$cdom,$crs,\%qzdbsettings);
  683:             &write_webct4_questions($cms,\@allquestids,$context,\%qzdbsettings,$dirname,\%allanswers,\%allchoices,$total,$cid,$cdom,$crs,$destdir,\%catinfo);
  684:         }
  685:     }
  686: 
  687:     $$total{'board'} = $board_count;
  688:     $$total{'quiz'} = @{$quizzes};
  689:     $$total{'surv'} = @{$surveys};
  690:     $$total{'pool'} = @{$pools};
  691: }
  692: 
  693: sub build_structure {
  694:     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) = @_;
  695:     my %flag = ();
  696:     my %count = ();
  697:     my %pagecontents = ();
  698:     my %seqtext = ();
  699:     my $topnum = 0;
  700:     my $topspecials = @$announcements + @$boards + @$quizzes + @$surveys + @$pools;
  701: 
  702:     if (!-e "$destdir") {
  703:         mkdir("$destdir",0755);
  704:     }
  705:     if (!-e "$destdir/sequences") {
  706:         mkdir("$destdir/sequences",0770);
  707:     }
  708:     if (!-e "$destdir/resfiles") {
  709:         mkdir("$destdir/resfiles",0770);
  710:     }
  711:     if (!-e "$destdir/pages") {
  712:         mkdir("$destdir/pages",0770);
  713:     }
  714:     if (!-e "$destdir/problems") {
  715:         mkdir("$destdir/problems",0770);
  716:     }
  717: 
  718:     $seqtext{'Top'} = qq|<map>\n|;       
  719:     %{$$resinfo{$$items{'Top'}{resnum}}} = (
  720:                                          isfolder => 'true',
  721:                                         );
  722: 
  723:     my $srcstem = "";
  724:  
  725:     if ($context eq 'DOCS') {
  726:         $srcstem = "/uploaded/$cdom/$crs/$timenow";
  727:     } elsif ($context eq 'CSTR') {
  728:         $srcstem = "/res/$udom/$uname/$newdir";
  729:     }
  730: 
  731:     foreach my $key (sort keys %{$items}) {
  732:       if ($$includeditems{$key}) {
  733:         %{$flag{$key}} = (
  734:                           page => 0,
  735:                           seq => 0,
  736:                           board => 0,
  737:                           file => 0,
  738:                          );
  739: 
  740:         %{$count{$key}} = (
  741:                            page => -1,
  742:                            seq => 0,
  743:                            board => 0,
  744:                            file => 0,
  745:                           );
  746: 
  747:         my $src = "";
  748: 
  749:         my $next_id = 2;
  750:         my $curr_id = 1;
  751:         my $resnum = $$items{$key}{resnum};
  752:         my $type = $$resources{$resnum}{type};
  753:         my $contentscount = $$items{$key}{'contentscount'};
  754:         my $seqtitle = $$items{$key}{'title'};
  755:         $seqtitle =~ s|/+|_|g;
  756:         $seqtitle =~ s/\s+/_/g;
  757:         $seqtitle .= '_'.$key;
  758:         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 'webctce4' &&  $contentscount > 0)) {
  759:             unless (($cms eq 'bb5') && $key eq 'Top') {
  760:                 $seqtext{$key} = "<map>\n";
  761:             }
  762:             if ($contentscount == 0) {
  763: 	        if ($key eq 'Top') {
  764:                     unless ($topspecials) {
  765:                         $seqtext{$key} .= qq|<resource id="$curr_id" src="" type="start"></resource>
  766: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  767: <resource id="$next_id" src="" type="finish"></resource>\n|;
  768:                     }
  769:                 } else {
  770:                     $seqtext{$key} .= qq|<resource id="$curr_id" src="" type="start"></resource>
  771: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
  772: <resource id="$next_id" src="" type="finish"></resource>\n|;
  773:                 }
  774:             } else {
  775:                 my $contcount = 0;
  776:                 if (defined($$items{$key}{contents})) { 
  777:                     $contcount = @{$$items{$key}{contents}};
  778:                 } else {
  779:                     &Apache::lonnet::logthis("IMS Import error for item: $key- contents count = $contentscount, but identity of contents not defined.");
  780:                 }
  781:                 my $contitem = $$items{$key}{contents}[0];
  782:                 my $contitemcount = $$items{$contitem}{contentscount}; 
  783:                 my ($res,$itm,$type,$file);
  784:                 if (exists($$items{$contitem}{resnum})) {
  785:                     $res = $$items{$contitem}{resnum};
  786:                     $itm = $$resources{$res}{revitm};
  787:                     $type = $$resources{$res}{type};
  788:                     $file = $$resources{$res}{file};
  789:                 }
  790:                 my $title = $$items{$contitem}{title};
  791:                 my $packageflag = 0;
  792:                 if (grep/^$res$/,@{$packages}) {
  793:                     $packageflag = 1;
  794:                 }
  795:                 $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$file,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount,$$randompicks{$contitem},$title);
  796:                 unless ($flag{$key}{page} == 1) {
  797:                     if ($$randompicks{$contitem}) {
  798:                         $seqtext{$key} .= qq|
  799: <param to="$curr_id" type="int_pos" name="parameter_randompick" value="$$randompicks{$contitem}"></param>\n|;
  800:                     }
  801:                     $seqtext{$key} .= qq|<resource id="$curr_id" src="$src" title="$title" type="start"|;
  802:                     unless ($flag{$key}{seq} || $flag{$key}{board} || $flag{$key}{file}) {
  803:                         $flag{$key}{page} = 1;
  804:                     }
  805:                     if ($key eq 'Top') {
  806:                         push @{$topurls}, $src;
  807:                         push @{$topnames}, $title;
  808:                     }
  809:                 }
  810:                 if ($contcount == 1) {
  811:                     $seqtext{$key} .= qq|></resource>
  812: <link from="$curr_id" to="$next_id" index="$curr_id"></link>|;
  813:                     if ($key eq 'Top') {
  814:                         unless ($topspecials) {
  815:                             $seqtext{$key} .= qq|
  816: <resource id="$next_id" src="" type="finish"></resource>\n|;
  817:                         }
  818:                     } else {
  819:                         $seqtext{$key} .= qq|
  820: <resource id="$next_id" src="" type="finish"></resource>\n|;
  821:                     }
  822:                 } else {
  823:                     if ($contcount > 2 ) {
  824:                         for (my $i=1; $i<$contcount-1; $i++) {
  825:                             my $contitem = $$items{$key}{contents}[$i];
  826:                             my $contitemcount = $$items{$contitem}{contentscount};
  827:                             my $res = $$items{$contitem}{resnum};
  828:                             my $type = $$resources{$res}{type};
  829:                             my $file = $$resources{$res}{file};
  830:                             my $title = $$items{$contitem}{title};
  831:                             my $packageflag = 0;
  832:                             if (grep/^$res$/,@{$packages}) {
  833:                                 $packageflag = 1;
  834:                             }
  835:                             $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$file,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount,$$randompicks{$contitem},$title);
  836:                             unless ($flag{$key}{page} == 1) {
  837:                                 $seqtext{$key} .= qq|></resource>
  838: <link from="$curr_id" to="$next_id" index="$curr_id"></link>\n|;
  839:                                 if ($$randompicks{$contitem}) {
  840:                                     $seqtext{$key} .= qq|
  841: <param to="$next_id" type="int_pos" name="parameter_randompick" value="$$randompicks{$contitem}"></param>|;
  842:                                 }
  843:                                 $seqtext{$key} .= qq|
  844: <resource id="$next_id" src="$src" title="$title"|;
  845:                                 $curr_id ++;
  846:                                 $next_id ++;
  847:                                 unless ($flag{$key}{seq} || $flag{$key}{board} || $flag{$key}{file}) {
  848:                                     $flag{$key}{page} = 1;
  849:                                 }
  850:                                 if ($key eq 'Top') {
  851:                                     push @{$topurls}, $src;
  852:                                     push @{$topnames}, $title;
  853:                                 }
  854:                             }
  855:                         }
  856:                     }
  857:                     my $contitem = $$items{$key}{contents}[-1];
  858:                     my $contitemcount = $$items{$contitem}{contentscount};
  859:                     my $res = $$items{$contitem}{resnum};
  860:                     my $type = $$resources{$res}{type};
  861:                     my $file = $$resources{$res}{file};
  862:                     my $title = $$items{$contitem}{title};
  863:                     my $packageflag = 0;
  864:                     if (grep/^$res$/,@{$packages}) {
  865:                         $packageflag = 1;
  866:                     }
  867:                     $src = &make_structure($cms,$key,$srcstem,\%flag,\%count,$timestamp,$boardnum,$hrefs,\%pagecontents,$res,$type,$file,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount,$$randompicks{$contitem},$title);
  868: 
  869:                     if ($flag{$key}{page}) {
  870:                         if ($count{$key}{seq} + $count{$key}{page} + $count{$key}{board} + $count{$key}{file} +1 == 1) {
  871:                             $seqtext{$key} .= qq|></resource>
  872: <link from="$curr_id" index="$curr_id" to="$next_id">
  873: <resource id ="$next_id" src="" |;
  874:                         }
  875:                     } else {
  876:                         $seqtext{$key} .= qq|></resource>
  877: <link from="$curr_id" to="$next_id" index="$curr_id"></link>\n|;
  878:                         if ($$randompicks{$contitem}) {
  879:                             $seqtext{$key} .= qq|
  880: <param to="$next_id" type="int_pos" name="parameter_randompick" value="$$randompicks{$contitem}"></param>\n|;
  881:                         }
  882:                         $seqtext{$key} .= qq|
  883: <resource id="$next_id" src="$src" title="$title" |;
  884:                         if ($key eq 'Top') {
  885:                             push @{$topurls}, $src;
  886:                             push @{$topnames}, $title;
  887:                         }
  888:                     }
  889:                     if ($contcount == $$items{$key}{contentscount}) {
  890:                         $seqtext{$key} .= qq|type="finish"></resource>\n|;
  891:                     } else {
  892:                         $curr_id ++;
  893:                         $next_id ++;
  894:                         $seqtext{$key} .= qq|></resource>
  895: <link from="$curr_id" to="$next_id" index="$curr_id"></link>\n|;
  896:                     } 
  897:                 }
  898:             }
  899:             unless (($cms eq 'bb5') && $key eq 'Top') {
  900:                 $seqtext{$key} .= "</map>\n";
  901:                 if ($cms eq 'webctce4' && $key ne 'Top') {
  902:                     push @{$seqfiles}, "$seqtitle.sequence";
  903:                     open(LOCFILE,">$destdir/sequences/$seqtitle.sequence");
  904:                 } else {
  905:                     push @{$seqfiles}, "$key.sequence";
  906:                     open(LOCFILE,">$destdir/sequences/$key.sequence");
  907:                 }
  908:                 print LOCFILE $seqtext{$key};
  909:                 close(LOCFILE);
  910:             }
  911:             $count{$key}{page} ++;
  912:             $$total{page} += $count{$key}{page};
  913:         }
  914:         $$total{seq} += $count{$key}{seq};
  915:       }
  916:     }
  917:     $topnum += ($count{'Top'}{page} + $count{'Top'}{seq});
  918: 
  919:     if ($cms eq 'bb5' || $cms eq 'bb6') {
  920:         if (@{$announcements} > 0) {
  921:             &process_specials($context,'announcements',$announcements,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  922:         }
  923:         if (@{$boards} > 0) {
  924:             &process_specials($context,'boards',$boards,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  925:         }
  926:         if (@{$quizzes} > 0) {
  927:             &process_specials($context,'quizzes',$quizzes,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  928:         }
  929:         if (@{$surveys} > 0)  {
  930:             &process_specials($context,'surveys',$surveys,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  931:         }
  932:         if (@{$pools} > 0)  {
  933:             &process_specials($context,'pools',$pools,\$topnum,$$items{'Top'}{contentscount},$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,\$seqtext{'Top'},$pagesfiles,$seqfiles,$topurls,$topnames);
  934:         }
  935:         $seqtext{'Top'} .= "</map>\n";
  936:         open(TOPFILE,">$destdir/sequences/Top.sequence");
  937:         print TOPFILE $seqtext{'Top'};
  938:         close(TOPFILE);
  939:         push @{$seqfiles}, 'Top.sequence';
  940:     }
  941: 
  942:     my $filestem;
  943:     if ($context eq 'DOCS') {
  944:         $filestem = "/uploaded/$cdom/$crs/$timenow";
  945:     } elsif ($context eq 'CSTR') {
  946:         $filestem = "/res/$udom/$uname/$newdir";
  947:     }
  948: 
  949:     foreach my $key (sort keys %pagecontents) {
  950:         for (my $i=0; $i<@{$pagecontents{$key}}; $i++) {
  951:             my $filename = $destdir.'/pages/'.$key.'_'.$i.'.page';
  952:             my $resource = "$filestem/resfiles/$$items{$pagecontents{$key}[$i][0]}{resnum}.html";
  953:             my $res = $$items{$pagecontents{$key}[$i][0]}{resnum};
  954:             my $resource = $filestem.'/resfiles/'.$res.'.html';
  955:             if (grep/^$res$/,@{$packages}) {
  956:                 $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # should be entry_point
  957:             }
  958:             open(PAGEFILE,">$filename");
  959:             print PAGEFILE qq|<map>
  960: <resource src="$resource" id="1" type="start" title="$$items{$pagecontents{$key}[$i][0]}{title}"></resource>
  961: <link to="2" index="1" from="1">\n|;
  962:             if (@{$pagecontents{$key}[$i]} == 1) {
  963:                 print PAGEFILE qq|<resource src="" id="2" type="finish"></resource>\n|;
  964:             } elsif (@{$pagecontents{$key}[$i]} == 2)  {
  965:                 my $res = $$items{$pagecontents{$key}[$i][1]}{resnum};
  966:                 my $resource = $filestem.'/resfiles/'.$res.'.html';
  967:                 if (grep/^$res$/,@{$packages}) {
  968:                     $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # should be entry_point
  969:                 }
  970:                 print PAGEFILE qq|<resource src="$resource" id="2" type="finish" title="$$items{$pagecontents{$key}[$i][1]}{title}"></resource>\n|;
  971:             } else {
  972:                 for (my $j=1; $j<@{$pagecontents{$key}[$i]}-1; $j++) {
  973:                     my $curr_id = $j+1;
  974:                     my $next_id = $j+2;
  975:                     my $res = $$items{$pagecontents{$key}[$i][$j]}{resnum};
  976:                     my $resource = $filestem.'/resfiles/'.$res.'.html';
  977:                     if (grep/^$res$/,@{$packages}) {
  978:                         $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # entry_point
  979:                     }
  980:                     print PAGEFILE qq|<resource src="$resource" id="$curr_id" title="$$items{$pagecontents{$key}[$i][$j]}{title}"></resource>
  981: <link to="$next_id" index="$curr_id" from="$curr_id">\n|;
  982:                 }
  983:                 my $final_id = @{$pagecontents{$key}[$i]};
  984:                 my $res = $$items{$pagecontents{$key}[$i][-1]}{resnum};
  985:                 my $resource = $filestem.'/resfiles/'.$res.'.html';
  986:                 if (grep/^$res$/,@{$packages}) {
  987:                     $resource =  $filestem.'/resfiles/'.$res.'./index.html'; # entry_point
  988:                 }
  989:                 print PAGEFILE qq|<resource src="$resource" id="$final_id" type="finish" title="$$items{$pagecontents{$key}[$i][-1]}{title}"></resource>\n|;
  990:             }
  991:             print PAGEFILE "</map>";
  992:             close(PAGEFILE);
  993:             push @{$pagesfiles}, $key.'_'.$i.'.page'; 
  994:         }
  995:     }
  996: }
  997: 
  998: sub make_structure {
  999:     my ($cms,$key,$srcstem,$flag,$count,$timestamp,$boardnum,$hrefs,$pagecontents,$res,$type,$file,$resinfo,$contitem,$uname,$cdom,$contcount,$packageflag,$contitemcount,$randompick,$title) = @_;
 1000:     my $src ='';
 1001:     if (($cms eq 'angel' && $type eq 'FOLDER') || (($cms eq 'bb5' || $cms eq 'bb6') && (($$resinfo{$res}{'isfolder'} eq 'true') || $key eq 'Top')) || ($cms eq 'webctce4' && $contitemcount > 0)) {
 1002:         $src = $srcstem.'/sequences/'.$contitem.'.sequence';
 1003:         if ($cms eq 'webctce4') {
 1004:             $title =~ s|/+|_|g;
 1005:             $title =~ s/\s+/_/g;
 1006:             $title .= '_'.$contitem;
 1007:             $src = $srcstem.'/sequences/'.$title.'.sequence';
 1008:         }
 1009:         $$flag{$key}{page} = 0;
 1010:         $$flag{$key}{seq} = 1;
 1011:         $$count{$key}{seq} ++;
 1012:     } elsif ($cms eq 'webctce4' && $randompick) {
 1013:         $src = $srcstem.'/sequences/'.$res.'.sequence';
 1014:         $$flag{$key}{page} = 0;
 1015:         $$flag{$key}{seq} = 1;
 1016:         $$count{$key}{seq} ++;
 1017:     } elsif ($cms eq 'angel' && $type eq 'BOARD') {
 1018:         $src = '/adm/'.$cdom.'/'.$uname.'/'.$$timestamp[$$boardnum{$res}].'/bulletinboard'; 
 1019:         $$flag{$key}{page} = 0;
 1020:         $$flag{$key}{board} = 1;
 1021:         $$count{$key}{board} ++;
 1022:     } elsif ($cms eq 'angel' && $type eq "FILE") {
 1023:         foreach my $file (@{$$hrefs{$res}}) {
 1024:             unless ($file eq 'pg'.$res.'.htm') {
 1025:                 $src = $srcstem.'/resfiles/'.$res.'/'.$file;
 1026:             }
 1027:         }
 1028:         $$flag{$key}{page} = 0;
 1029:         $$flag{$key}{file} = 1;
 1030:     } elsif ($cms eq 'angel' && (($type eq "PAGE") || ($type eq "LINK")) )  {
 1031:         if ($$flag{$key}{page}) {
 1032:             if ($$count{$key}{page} == -1) {
 1033:                 &Apache::lonnet::logthis("IMS Angel import error in array index for page: value = -1, resource is $key, type is $type.");
 1034:             } else { 
 1035:                 push @{$$pagecontents{$key}[$$count{$key}{page}]},$contitem;
 1036:             }
 1037:         } else {
 1038:             $$count{$key}{page} ++;
 1039:             $src = $srcstem.'/pages/'.$key.'_'.$$count{$key}{page}.'.page';
 1040:             @{$$pagecontents{$key}[$$count{$key}{page}]} = ("$contitem");
 1041:             $$flag{$key}{seq} = 0;
 1042:         }
 1043:     } elsif ($cms eq 'bb5' || $cms eq 'bb6') {
 1044:         if ($$flag{$key}{page}) {
 1045:             push @{$$pagecontents{$key}[$$count{$key}{page}]},$contitem;
 1046:         } else {
 1047:             if ($contcount == 1) {
 1048:                 if ($packageflag) {
 1049:                     $src = $srcstem.'/resfiles/'.$res.'/index.html'; # Needs to be entry point
 1050:                 } else {
 1051:                     $src = $srcstem.'/resfiles/'.$res.'.html';
 1052:                 }
 1053:             } else {
 1054:                 $$count{$key}{page} ++;
 1055:                 $src = $srcstem.'/pages/'.$key.'_'.$$count{$key}{page}.'.page';
 1056:                 @{$$pagecontents{$key}[$$count{$key}{page}]} = ("$contitem");
 1057:             }
 1058:             $$flag{$key}{seq} = 0;
 1059:         }
 1060:     } elsif ($cms eq 'webctce4') {
 1061:         if ($type eq 'webctquiz') {
 1062:             $src =  $srcstem.'/pages/'.$res.'.page';
 1063:             $$count{$key}{page} ++;
 1064:             $$flag{$key}{seq} = 0;
 1065:         } else {
 1066:             if (grep/^$file$/,@{$$hrefs{$res}}) {
 1067:                 my $filename;
 1068:                 if ($file =~ m-/([^/]+)$-) {
 1069:                     $filename = $1;
 1070:                 }
 1071:                 $src =  $srcstem.'/resfiles/'.$filename;
 1072:             } else {
 1073:                 foreach my $file (@{$$hrefs{$res}}) {
 1074:                     my $filename;
 1075:                     if ($file =~ m-/my_files/(.+)$-) {
 1076:                         $filename = $1;
 1077:                     } elsif ($file =~ m-/([^/]+)$-) { 
 1078:                         $filename = $1;
 1079:                     }
 1080:                     $src = $srcstem.'/resfiles/'.$filename;
 1081:                 }
 1082:             }
 1083:             $$flag{$key}{page} = 0;
 1084:             $$flag{$key}{file} = 1;
 1085:         }
 1086:     }
 1087:     return $src;
 1088: }
 1089: 
 1090: 
 1091: # ---------------------------------------------------------------- Process Blackboard specials - announcements, bulletin boards, quizzes and surveys
 1092: sub process_specials {
 1093:     my ($context,$type,$specials,$topnum,$contentscount,$destdir,$udom,$uname,$cdom,$crs,$timenow,$newdir,$timestamp,$resinfo,$seqtext,$pagesfiles,$seqfiles,$topurls,$topnames) = @_;
 1094:     my $src = '';
 1095:     my $specialsrc = '';
 1096:     my $nextnum = 0;
 1097:     my $seqstem = '';
 1098:     if ($context eq 'CSTR') {
 1099:         $seqstem = "/res/$udom/$uname/$newdir";
 1100:     } elsif ($context eq 'DOCS') {
 1101:         $seqstem = '/uploaded/'.$cdom.'/'.$crs.'/'.$timenow;
 1102:     }
 1103:     my %seqnames = (
 1104:                   boards => 'bulletinboards',
 1105:                   quizzes => 'quizzes',
 1106:                   surveys => 'surveys',
 1107:                   announcements => 'announcements',
 1108:                   pools => 'pools'
 1109:                   );
 1110:     my %seqtitles = (
 1111:                   boards => 'Course Bulletin Boards',
 1112:                   quizzes => 'Course Quizzes',
 1113:                   surveys => 'Course Surveys',
 1114:                   announcements => 'Course Announcements',
 1115:                   pools => 'Course Question Pools'
 1116:                    );
 1117:     $$topnum ++;
 1118: 
 1119:     if ($type eq 'announcements') {
 1120:         $src = "$seqstem/pages/$seqnames{$type}.page";
 1121:     } else {
 1122:         $src = "$seqstem/sequences/$seqnames{$type}.sequence";
 1123:     }
 1124: 
 1125:     push @{$topurls}, $src;
 1126:     push @{$topnames}, $seqtitles{$type};
 1127: 
 1128:     $$seqtext .= qq|<resource id="$$topnum" src="$src" title="$seqtitles{$type}"|;
 1129:     $nextnum = $$topnum +1;
 1130:     if ($$topnum == 1) {
 1131:         $$seqtext .= qq| type="start"></resource>
 1132: <link from="$$topnum" to="$nextnum" index="$$topnum"></link>\n|;
 1133:         if ($$topnum == $contentscount) {
 1134:             $$seqtext .= qq|<resource id="$nextnum" src="" type="finish"></resource>\n|;
 1135:         }
 1136:     } else {
 1137:         if ($$topnum == $contentscount) {
 1138:             $$seqtext .= qq| type="finish"></resource>\n|;
 1139:         } else {
 1140:             $$seqtext .= qq|></resource>
 1141: <link from="$$topnum" to="$nextnum" index="$$topnum"></link>\n|;
 1142:         }
 1143:     }
 1144: 
 1145:     if ($type eq "announcements") {
 1146:         push @{$pagesfiles}, "$seqnames{$type}.page";
 1147:         open(ITEM,">$destdir/pages/$seqnames{$type}.page");
 1148:     } else {
 1149:         push @{$seqfiles}, "$seqnames{$type}.sequence";
 1150:         open(ITEM,">$destdir/sequences/$seqnames{$type}.sequence");
 1151:     }
 1152: 
 1153:     if ($type eq 'boards') {
 1154:         $specialsrc = "/adm/$udom/$uname/$$timestamp[0]/bulletinboard";
 1155:     } elsif ($type eq 'announcements') {
 1156:         $specialsrc = "$seqstem/resfiles/$$specials[0].html";
 1157:     } elsif ($type eq 'pools') {
 1158:         $specialsrc = "$seqstem/sequences/$$specials[0].sequence";
 1159:     } else {
 1160:         $specialsrc = "$seqstem/pages/$$specials[0].page";
 1161:     }
 1162:     print ITEM qq|<map>
 1163: <resource id="1" src="$specialsrc" title="$$resinfo{$$specials[0]}{title}" type="start"></resource>
 1164: <link from="1" to="2" index="1"></link>|;
 1165:     if (@{$specials} == 1) {
 1166:         print ITEM qq|
 1167: <resource id="2" src="" type="finish"></resource>\n|;
 1168:     } else {
 1169:         for (my $i=1; $i<@{$specials}; $i++) {
 1170:             my $curr = $i+1;
 1171:             my $next = $i+2;
 1172:             if ($type eq 'boards') {
 1173:                 $specialsrc = "/adm/$udom/$uname/$$timestamp[$i]/bulletinboard";
 1174:             } elsif ($type eq 'announcements') {
 1175:                 $specialsrc = "$seqstem/resfiles/$$specials[$i].html";
 1176:             } else {
 1177:                 $specialsrc = "$seqstem/pages/$$specials[$i].page";
 1178:             }
 1179:             print ITEM qq|<resource id="$curr" src="$specialsrc" title="$$resinfo{$$specials[$i]}{title}"|;
 1180:             if (@{$specials} == $i+1) {
 1181:                 print ITEM qq| type="finish"></resource>\n|;
 1182:             } else {
 1183:                 print ITEM qq|></resource>
 1184: <link from="$curr" to="$next" index="$next">\n|;
 1185:             }
 1186:         }
 1187:     }
 1188:     print ITEM qq|</map>|;
 1189:     close(ITEM);
 1190: }
 1191: 
 1192: # ---------------------------------------------------------------- Process Blackboard users
 1193: sub process_user {
 1194:   my ($res,$docroot,$destdir,$settings,$user_crs,$user_cdom,$user_handling) = @_;
 1195:   my $xmlfile = $docroot.'/'.$res.".dat";
 1196:   my $filecount = 0;
 1197:   my @state;
 1198:   my $userid = '';
 1199:   my $linknum = 0;
 1200: 
 1201:   my $p = HTML::Parser->new
 1202:     (
 1203:      xml_mode => 1,
 1204:      start_h =>
 1205:      [sub {
 1206:         my ($tagname, $attr) = @_;
 1207:         push @state, $tagname;
 1208:         if ("@state" eq "USERS USER") {
 1209:             $userid = $attr->{value};
 1210:             %{$$settings{$userid}} = ();
 1211:             @{$$settings{$userid}{links}} = ();
 1212:         } elsif ("@state" eq "USERS USER LOGINID") {  
 1213:             $$settings{$userid}{loginid} = $attr->{value};
 1214:         } elsif ("@state" eq "USERS USER PASSPHRASE") {  
 1215:             $$settings{$userid}{passphrase} = $attr->{value};
 1216:         } elsif ("@state" eq "USERS USER STUDENTID" ) {
 1217:             $$settings{$userid}{studentid} = $attr->{value};
 1218:         } elsif ("@state" eq "USERS USER NAMES FAMILY" ) {
 1219:             $$settings{$userid}{family} = $attr->{value};
 1220:         } elsif ("@state" eq "USERS USER NAMES GIVEN" ) {
 1221:             $$settings{$userid}{given} = $attr->{value};
 1222:         } elsif ("@state" eq "USERS USER ADDRESSES BUSINESS DATA EMAIL") {
 1223:             $$settings{$userid}{email} = $attr->{value};
 1224:         } elsif ("@state" eq "USERS USER USER_ROLE") {
 1225:             $$settings{$userid}{user_role} = $attr->{value};
 1226:         } elsif ("@state" eq "USERS USER FLAGS ISAVAILABLE") {
 1227:             $$settings{$userid}{isavailable} = $attr->{value};
 1228:         } elsif ("@state" eq "USERS USER PERSONALPAGE FILELIST IMAGE") {
 1229:             $$settings{$userid}{image} = $attr->{value};
 1230:         } elsif ( ($state[-2] eq "LINKLIST") && ($state[-1] eq "LINK") ) {
 1231:             %{$$settings{$userid}{links}[$linknum]} = ();
 1232:             $$settings{$userid}{links}[$linknum]{url} = $attr->{value};
 1233:             $linknum ++;
 1234:         }
 1235:      }, "tagname, attr"],
 1236:      text_h =>
 1237:      [sub {
 1238:         my ($text) = @_;
 1239:         if ("@state" eq "USERS USER PERSONALPAGE TITLE") {
 1240:             $$settings{$userid}{title} = $text;
 1241:         } elsif ("@state" eq "USERS USER PERSONALPAGE DESCRIPTION") {
 1242:             $$settings{$userid}{description} = $text;
 1243:         } elsif (($state[-2] eq "LINK") && ($state[-1] eq "TITLE")) {
 1244:             $$settings{$userid}{links}[$linknum]{title} = $text;
 1245:         } elsif (($state[-3] eq "LINK") && ($state[-2] eq  "DESCRIPTION") && ($state[-1] eq "TEXT")) {
 1246:             $$settings{$userid}{links}[$linknum]{text} = $text;
 1247:         }
 1248:       }, "dtext"],
 1249:      end_h =>
 1250:      [sub {
 1251:         my ($tagname) = @_;
 1252:         if ("@state" eq "USERS USER") {
 1253:             $linknum = 0;
 1254:         }
 1255:         pop @state;
 1256:      }, "tagname"],
 1257:     );
 1258:   $p->unbroken_text(1);
 1259:   $p->parse_file($xmlfile);
 1260:   $p->eof;
 1261:   
 1262:   my $configvars = &LONCAPA::Configuration::read_conf('loncapa.conf');
 1263:   my $xmlstem =  $$configvars{'lonDaemons'}."/tmp/".$user_cdom."_".$user_crs."_";
 1264: 
 1265:   foreach my $user_id (keys %{$settings}) {
 1266:       if ($$settings{$user_id}{user_role} eq "s") {
 1267:            
 1268:       } elsif ($user_handling eq 'enrollall') {
 1269: 
 1270:       }
 1271:   }
 1272: }
 1273: 
 1274: # ---------------------------------------------------------------- Process Blackboard groups
 1275: sub process_group {  
 1276:   my ($res,$docroot,$destdir,$settings) = @_;
 1277:   my $xmlfile = $docroot.'/'.$res.".dat";
 1278:   my $filecount = 0;
 1279:   my @state;
 1280:   my $grp;
 1281: 
 1282:   my $p = HTML::Parser->new
 1283:     (
 1284:      xml_mode => 1,
 1285:      start_h =>
 1286:      [sub {
 1287:         my ($tagname, $attr) = @_;
 1288:         push @state, $tagname;
 1289:         if ("@state" eq "GROUPS GROUP") {
 1290:             $grp = $attr->{id};
 1291:         }        
 1292:         if ("@state" eq "GROUPS GROUP TITLE") {
 1293:             $$settings{$grp}{title} = $attr->{value};
 1294:         } elsif ("@state" eq "GROUPS GROUP FLAGS ISAVAILABLE") {  
 1295:             $$settings{$grp}{isavailable} = $attr->{value};
 1296:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASCHATROOM") {  
 1297:             $$settings{$grp}{chat} = $attr->{value};
 1298:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASDISCUSSIONBOARD") {
 1299:             $$settings{$grp}{discussion} = $attr->{value};
 1300:         } elsif ("@state" eq "GROUPS GROUP FLAGS HASTRANSFERAREA") {
 1301:             $$settings{$grp}{transfer} = $attr->{value};
 1302:         } elsif ("@state" eq "GROUPS GROUP FLAGS ISPUBLIC") {
 1303:             $$settings{$grp}{public} = $attr->{value};
 1304:         }
 1305:      }, "tagname, attr"],
 1306:      text_h =>
 1307:      [sub {
 1308:         my ($text) = @_;
 1309:         if ("@state" eq "GROUPS DESCRIPTION") {
 1310:           $$settings{$grp}{description} = $text;
 1311: #          print "Staff text is $text\n";
 1312:         }
 1313:       }, "dtext"],
 1314:      end_h =>
 1315:      [sub {
 1316:         my ($tagname) = @_;
 1317:         pop @state;
 1318:      }, "tagname"],
 1319:     );
 1320:   $p->unbroken_text(1);
 1321:   $p->parse_file($xmlfile);
 1322:   $p->eof;
 1323: }
 1324: 
 1325: # ---------------------------------------------------------------- Process Blackboard Staff
 1326: sub process_staff {
 1327:   my ($res,$docroot,$destdir,$settings,$resrcfiles) = @_;
 1328:   my $xmlfile = $docroot.'/'.$res.".dat";
 1329:   my $filecount = 0;
 1330:   my @state;
 1331:   %{$$settings{name}} = ();
 1332:   %{$$settings{office}} = ();
 1333: 
 1334:   my $p = HTML::Parser->new
 1335:     (
 1336:      xml_mode => 1,
 1337:      start_h =>
 1338:      [sub {
 1339:         my ($tagname, $attr) = @_;
 1340:         push @state, $tagname;
 1341:         if ("@state" eq "STAFFINFO TITLE") {
 1342:             $$settings{title} = $attr->{value};
 1343:         } elsif ("@state" eq "STAFFINFO BIOGRAPHY TEXTCOLOR") {
 1344:             $$settings{textcolor} = $attr->{value};
 1345:         } elsif ("@state" eq "STAFFINFO BIOGRAPHY FLAGS ISHTML") {
 1346:             $$settings{ishtml} = $attr->{value};
 1347:         } elsif ("@state" eq "STAFFINFO FLAGS ISAVAILABLE" ) {
 1348:             $$settings{isavailable} = $attr->{value};
 1349:         } elsif ("@state" eq "STAFFINFO FLAGS ISFOLDER" ) {
 1350:             $$settings{isfolder} = $attr->{value};
 1351:         } elsif ("@state" eq "STAFFINFO POSITION" ) {
 1352:             $$settings{position} = $attr->{value};
 1353:         } elsif ("@state" eq "STAFFINFO HOMEPAGE" ) {
 1354:             $$settings{homepage} = $attr->{value};
 1355:         } elsif ("@state" eq "STAFFINFO IMAGE") {
 1356:             $$settings{image} = $attr->{value};
 1357:         }
 1358:      }, "tagname, attr"],
 1359:      text_h =>
 1360:      [sub {
 1361:         my ($text) = @_;
 1362:         if ("@state" eq "STAFFINFO BIOGRAPHY TEXT") {
 1363:           $$settings{text} = $text;
 1364: #          print "Staff text is $text\n";
 1365:         } elsif ("@state" eq "STAFFINFO CONTACT PHONE") {
 1366:           $$settings{phone} = $text;
 1367:         } elsif ("@state" eq "STAFFINFO CONTACT EMAIL") {
 1368:           $$settings{email} = $text;
 1369:         } elsif ("@state" eq "STAFFINFO CONTACT NAME FORMALTITLE") {
 1370:           $$settings{name}{formaltitle} = $text;
 1371:         } elsif ("@state" eq "STAFFINFO CONTACT NAME FAMILY") {
 1372:           $$settings{name}{family} = $text;
 1373:         } elsif ("@state" eq "STAFFINFO CONTACT NAME GIVEN") {
 1374:           $$settings{name}{given} = $text;
 1375:         } elsif ("@state" eq "STAFFINFO CONTACT OFFICE HOURS") {
 1376:           $$settings{office}{hours} = $text;
 1377:         }  elsif ("@state" eq "STAFFINFO CONTACT OFFICE ADDRESS") {
 1378:           $$settings{office}{address} = $text;
 1379:         }        
 1380:       }, "dtext"],
 1381:      end_h =>
 1382:      [sub {
 1383:         my ($tagname) = @_;
 1384:         pop @state;
 1385:      }, "tagname"],
 1386:     );
 1387:   $p->unbroken_text(1);
 1388:   $p->parse_file($xmlfile);
 1389:   $p->eof;
 1390: 
 1391:     my $fontcol = '';
 1392:     if (defined($$settings{textcolor})) {
 1393:         $fontcol =  qq|color="$$settings{textcolor}"|;
 1394:     }
 1395:     if (defined($$settings{text})) {
 1396:         if ($$settings{ishtml} eq "true") {
 1397:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1398:         }
 1399:     }
 1400:     my $staffentry = qq|
 1401: <table border="0" cellpadding="0" cellspacing="0" width="100%">
 1402:   <tr>
 1403:     <td colspan="2"><hr /><font face="arial,helv" size="3"><b>$$settings{name}{formaltitle} $$settings{name}{given} $$settings{name}{family}</b></font>
 1404:     </td>
 1405:   </tr>
 1406:   <tr>
 1407:     <td valign="top">
 1408:       <table width="100% border="0" cols="2" cellpadding="0" cellspacing="0">|;
 1409:     if ( defined($$settings{email}) && $$settings{email} ne '') {
 1410:         $staffentry .= qq|
 1411:         <tr>
 1412:           <td width="100" valign="top">
 1413:            <font face="arial" size="2"><b>Email:</b></font>
 1414:           </td>
 1415:           <td>
 1416:            <font face="arial" size="2"><a href="mailto:$$settings{email}">$$settings{email}</a></font>
 1417:           </td>
 1418:         </tr>
 1419:         |;
 1420:     }
 1421:     if (defined($$settings{phone}) && $$settings{phone} ne '') {
 1422:         $staffentry .= qq|
 1423:         <tr>
 1424:           <td width="100" valign="top">
 1425:             <font face="arial" size="2"><b>Phone:</b></font>
 1426:           </td>
 1427:           <td>
 1428:             <font face="arial" size="2">$$settings{phone}</font>
 1429:           </td>
 1430:         </tr>
 1431:         |;
 1432:     }
 1433:     if (defined($$settings{office}{address}) && $$settings{office}{address} ne '') {
 1434:         $staffentry .= qq|
 1435:         <tr>
 1436:          <td width="100" valign="top">
 1437:            <font face="arial" size="2"><b>Address:</b></font>
 1438:          </td>
 1439:          <td>
 1440:            <font face="arial" size="2">$$settings{office}{address}</font>
 1441:          </td>
 1442:         </tr>
 1443:         |;
 1444:     }
 1445:     if (defined($$settings{office}{hours}) && $$settings{office}{hours} ne '') {
 1446:         $staffentry .= qq|
 1447:         <tr>
 1448:           <td width="100" valign="top">
 1449:             <font face="arial" size="2"><b>Office Hours:</b></font>
 1450:           </td>
 1451:           <td>
 1452:             <font face=arial size=2>$$settings{office}{hours}</font>
 1453:           </td>
 1454:         </tr>
 1455:         |;
 1456:     }
 1457:     if ( defined($$settings{homepage}) && $$settings{homepage} ne '') {
 1458:         $staffentry .= qq|
 1459:         <tr>
 1460:           <td width="100" valign="top">
 1461:             <font face="arial" size="2"><b>Personal Link:</b></font>
 1462:           </td>
 1463:           <td>
 1464:             <font face="arial" size="2"><a href="$$settings{homepage}">$$settings{homepage}</a></font>
 1465:           </td>
 1466:         </tr>
 1467:         |;
 1468:     }
 1469:     if (defined($$settings{text}) && $$settings{text} ne '') {
 1470:         $staffentry .= qq|
 1471:         <tr>
 1472:           <td colspan="2">
 1473:             <font face="arial" size="2" $fontcol><b>Other Information:</b><br/>$$settings{text}</font>
 1474:           </td>
 1475:         </tr>
 1476:         |;
 1477:      }
 1478:      $staffentry .= qq|
 1479:       </table>
 1480:     </td>
 1481:     <td align="right" valign="top">
 1482:      |;
 1483:      if ( defined($$settings{image}) ) {
 1484:          $staffentry .= qq|
 1485:       <img src="$res/$$settings{image}">
 1486:          |;
 1487:      }
 1488:      $staffentry .= qq|
 1489:     </td>
 1490:   </tr>
 1491: </table>
 1492:     |;
 1493:     open(FILE,">$destdir/resfiles/$res.html");
 1494:     push @{$resrcfiles}, "$res.html";
 1495:     print FILE qq|<html>
 1496: <head>
 1497: <title>$$settings{title}</title>
 1498: </head>
 1499: <body bgcolor='#ffffff'>
 1500: $staffentry
 1501: </body>
 1502: </html>|;
 1503:     close(FILE);
 1504: }
 1505: 
 1506: # ---------------------------------------------------------------- Process Blackboard Links
 1507: sub process_link {
 1508:     my ($res,$docroot,$destdir,$settings,$resrcfiles) = @_;
 1509:     my $xmlfile = $docroot.'/'.$res.".dat";
 1510:     my @state = ();
 1511:     my $p = HTML::Parser->new
 1512:     (
 1513:         xml_mode => 1,
 1514:         start_h =>
 1515:         [sub {
 1516:             my ($tagname, $attr) = @_;
 1517:             push @state, $tagname;
 1518:             if ("@state" eq "EXTERNALLINK TITLE") {
 1519:                 $$settings{title} = $attr->{value};
 1520:             } elsif ("@state" eq "EXTERNALLINK TEXTCOLOR") {  
 1521:                 $$settings{textcolor} = $attr->{value};
 1522:             } elsif ("@state" eq "EXTERNALLINK DESCRIPTION FLAGS ISHTML") {  
 1523:                 $$settings{ishtml} = $attr->{value};
 1524:             } elsif ("@state" eq "EXTERNALLINK FLAGS ISAVAILABLE" ) {
 1525:                 $$settings{isavailable} = $attr->{value};
 1526:             } elsif ("@state" eq "EXTERNALLINK FLAGS LAUNCHINNEWWINDOW" ) {
 1527:                 $$settings{newwindow} = $attr->{value};
 1528:             } elsif ("@state" eq "EXTERNALLINK FLAGS ISFOLDER" ) {
 1529:                 $$settings{isfolder} = $attr->{value};
 1530:             } elsif ("@state" eq "EXTERNALLINK POSITION" ) {
 1531:                 $$settings{position} = $attr->{value};
 1532:             } elsif ("@state" eq "EXTERNALLINK URL" ) {
 1533:                 $$settings{url} = $attr->{value};
 1534:             }
 1535:         }, "tagname, attr"],
 1536:         text_h =>
 1537:         [sub {
 1538:             my ($text) = @_;
 1539:             if ("@state" eq "EXTERNALLINK DESCRIPTION TEXT") {
 1540:                $$settings{text} = $text;
 1541:             }
 1542:         }, "dtext"],
 1543:         end_h =>
 1544:         [sub {
 1545:             my ($tagname) = @_;
 1546:             pop @state;
 1547:         }, "tagname"],
 1548:     );
 1549:     $p->unbroken_text(1);
 1550:     $p->parse_file($xmlfile);
 1551:     $p->eof;
 1552: 
 1553:     my $linktag = '';
 1554:     my $fontcol = '';
 1555:     if (defined($$settings{textcolor})) {
 1556:         $fontcol =  qq|<font color="$$settings{textcolor}">|;
 1557:     }
 1558:     if (defined($$settings{text})) {
 1559:         if ($$settings{ishtml} eq "true") {
 1560:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1561:         }
 1562:     }
 1563: 
 1564:     if (defined($$settings{url}) ) {
 1565:         $linktag = qq|<a href="$$settings{url}"|;
 1566:         if ($$settings{newwindow} eq "true") {
 1567:             $linktag .= qq| target="launch"|;
 1568:         }
 1569:         $linktag .= qq|>$$settings{title}</a>|;
 1570:     }
 1571: 
 1572:     open(FILE,">$destdir/resfiles/$res.html");
 1573:     push @{$resrcfiles}, "$res.html";
 1574:     print FILE qq|<html>
 1575: <head>
 1576: <title>$$settings{title}</title>
 1577: </head>
 1578: <body bgcolor='#ffffff'>
 1579: $fontcol
 1580: $linktag
 1581: $$settings{text}
 1582: |;
 1583:     if (defined($$settings{textcolor})) {
 1584:         print FILE qq|</font>|;
 1585:     }
 1586:     print FILE qq|
 1587:   </body>
 1588:  </html>|;
 1589:     close(FILE);
 1590: }
 1591: 
 1592: # ---------------------------------------------------------------- Process Blackboard Discussion Boards
 1593: sub process_db {
 1594:     my ($res,$docroot,$destdir,$timestamp,$crs,$cdom,$handling,$uname,$settings,$longcrs) = @_;
 1595:     my $xmlfile = $docroot.'/'.$res.".dat";
 1596:     my @state = ();
 1597:     my @allmsgs = ();
 1598:     my %msgidx = ();
 1599:     my %threads; # all threads, keyed by message ID
 1600:     my $msg_id; # the current message ID
 1601:     my %message; # the current message being accumulated for $msg_id
 1602: 
 1603:     my $p = HTML::Parser->new
 1604:     (
 1605:        xml_mode => 1,
 1606:        start_h =>
 1607:        [sub {
 1608:            my ($tagname, $attr) = @_;
 1609:            push @state, $tagname;
 1610:            my $depth = 0;
 1611:            my @seq = ();
 1612:            if ("@state" eq "FORUM TITLE") {
 1613:                $$settings{title} = $attr->{value};
 1614:            } elsif ("@state" eq "FORUM DESCRIPTION TEXTCOLOR") {  
 1615:                $$settings{textcolor} = $attr->{value};
 1616:            } elsif ("@state" eq "FORUM DESCRIPTION FLAGS ISHTML") {  
 1617:                $$settings{ishtml} = $attr->{value};
 1618:            } elsif ("@state" eq "FORUM DESCRIPTION FLAGS ISNEWLINELITERAL") {  
 1619:                $$settings{newline} = $attr->{value};
 1620:            } elsif ("@state" eq "FORUM POSITION" ) {
 1621:                $$settings{position} = $attr->{value};
 1622:            } elsif ("@state" eq "FORUM FLAGS ISREADONLY") {
 1623:                $$settings{isreadonly} = $attr->{value};
 1624:            } elsif ("@state" eq "FORUM FLAGS ISAVAILABLE" ) {
 1625:                $$settings{isavailable} = $attr->{value};
 1626:            } elsif ("@state" eq "FORUM FLAGS ALLOWANONYMOUSPOSTINGS" ) {
 1627:                $$settings{allowanon} = $attr->{value};
 1628:            } elsif ( ($state[0] eq "FORUM") && ($state[1] eq "MESSAGETHREADS") && ($state[2] eq "MSG") ) {
 1629:                if ($state[-1] eq "MSG") {
 1630:                    unless ($msg_id eq '') {
 1631:                        push @{$threads{$msg_id}}, { %message };
 1632:                        $depth = @state - 3;
 1633:                        if ($depth > @seq) {
 1634:                            push @seq, $msg_id; 
 1635:                        }
 1636:                    }
 1637:                    if ($depth < @seq) {
 1638:                        pop @seq;
 1639:                    }                
 1640:                    $msg_id = $attr->{id};
 1641:                    push @allmsgs, $msg_id;
 1642:                    $msgidx{$msg_id} = @allmsgs;
 1643:                    %message = ();
 1644:                    $message{depth} = $depth;
 1645:                    if ($depth > 0) {
 1646:                        $message{parent} = $seq[-1];
 1647:                    } else {
 1648:                        $message{parent} = "None";
 1649:                    }
 1650:                } elsif ($state[-1] eq "TITLE") {
 1651:                    $message{title} = $attr->{value};
 1652:                } elsif ( ( $state[-3] eq "MESSAGETEXT" ) && ( $state[-2] eq "FLAGS" ) && ( $state[-1] eq "ISHTML" ) ) {
 1653:                    $message{ishtml} = $attr->{value};
 1654:                } elsif ( ( $state[-3] eq "MESSAGETEXT" ) && ( $state[-2] eq "FLAGS" ) && ( $state[-1] eq "ISNEWLINELITERAL" ) ) {
 1655:                    $message{newline} = $attr->{value};
 1656:                } elsif ( ( $state[-2] eq "DATES" ) && ( $state[-1] eq "CREATED" ) ) {
 1657:                    $message{created} = $attr->{value};
 1658:                } elsif ( $state[@state-2] eq "FLAGS") {
 1659:                    if ($state[@state-1] eq "ISANONYMOUS") {
 1660:                        $message{isanonymous} =  $attr->{value};
 1661:                    }
 1662:                } elsif ( $state[-2] eq "USER" ) {
 1663:                    if ($state[-1] eq "USERID") {
 1664:                        $message{userid} =  $attr->{value};
 1665:                    } elsif ($state[@state-1] eq "USERNAME") {
 1666:                        $message{username} =  $attr->{value};
 1667:                    } elsif ($state[@state-1] eq "EMAIL") {
 1668:                        $message{email} =  $attr->{value};
 1669:                    }          
 1670:                } elsif ( ($state[-2] eq "FILELIST") && ($state[-1] eq "IMAGE") ) {
 1671:                    $message{attachment} = $attr->{value};
 1672:                }
 1673:            }
 1674:        }, "tagname, attr"],
 1675:        text_h =>
 1676:        [sub {
 1677:            my ($text) = @_;
 1678:            if ("@state" eq "FORUM DESCRIPTION TEXT") {
 1679:                $$settings{text} = $text;
 1680:            } elsif ( ($state[0] eq "FORUM") && ($state[1] eq "MESSAGETHREADS") && ($state[2] eq "MSG") ) {
 1681:                if ( ($state[-2] eq "MESSAGETEXT") && ($state[-1] eq "TEXT") ){
 1682:                    $message{text} = $text;
 1683:                }
 1684:            }
 1685:        }, "dtext"],
 1686:        end_h =>
 1687:        [sub {
 1688:            my ($tagname) = @_;
 1689:            if ( $state[-1] eq "MESSAGETHREADS" ) {
 1690:                push @{$threads{$msg_id}}, { %message };
 1691:            }
 1692:            pop @state;
 1693:        }, "tagname"],
 1694:     );
 1695:     $p->unbroken_text(1);
 1696:     $p->parse_file($xmlfile);
 1697:     $p->eof;
 1698: 
 1699:     if (defined($$settings{text})) {
 1700:         if ($$settings{ishtml} eq "false") {
 1701:             if ($$settings{isnewline} eq "true") {
 1702:                 $$settings{text} =~ s#\n#<br/>#g;
 1703:             }
 1704:         } else {
 1705:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 1706:         }
 1707:         if (defined($$settings{fontcolor}) ) {
 1708:             $$settings{text} = "<font color=\"".$$settings{textcolor}."\">".$$settings{text}."</font>";
 1709:         }
 1710:     }
 1711:     my $boardname = 'bulletinpage_'.$timestamp;
 1712:     my %boardinfo = (
 1713:                   'aaa_title' => $$settings{title},
 1714:                   'bbb_content' => $$settings{text},
 1715:                   'ccc_webreferences' => '',
 1716:                   'uploaded.lastmodified' => time,
 1717:                   );
 1718:   
 1719:     my $putresult = &Apache::lonnet::put($boardname,\%boardinfo,$cdom,$crs);
 1720:     if ($handling eq 'importall') {
 1721:         foreach my $msg_id (@allmsgs) {
 1722:             foreach my $message ( @{$threads{$msg_id}} ) {
 1723:                 my %contrib = (
 1724:                             'sendername' => $$message{userid},
 1725:                             'senderdomain' => $cdom,
 1726:                             'screenname' => '',
 1727:                             'plainname' => $$message{username},
 1728:                             );
 1729:                 unless ($$message{parent} eq 'None') {
 1730:                     $contrib{replyto} = $msgidx{$$message{parent}};
 1731:                 }
 1732:                 if (defined($$message{isanonymous}) ) {
 1733:                     if ($$message{isanonymous} eq 'true') {
 1734:                         $contrib{'anonymous'} = 'true';
 1735:                     }
 1736:                 }
 1737:                 if ( defined($$message{attachment}) )  {
 1738:                     my $url = $$message{attachment};
 1739:                     my $oldurl = $url;
 1740:                     my $newurl = $url;
 1741:                     unless ($url eq '') {
 1742:                         $newurl =~ s/\//_/g;
 1743:                         unless ($longcrs eq '') {
 1744:                             if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles") {
 1745:                                 mkdir("/home/httpd/lonUsers/$cdom/$longcrs/userfiles",0755);
 1746:                             }
 1747:                             if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl") {
 1748:                                 system("cp $destdir/resfiles/$res/$$message{attachment} /home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl");
 1749:                             }
 1750:                             $contrib{attachmenturl} = '/uploaded/'.$cdom.'/'.$crs.'/'.$newurl;
 1751:                         }
 1752:                     }
 1753:                 }
 1754:                 if (defined($$message{title}) ) {
 1755:                     $contrib{'message'} = $$message{title};
 1756:                 }
 1757:                 if (defined($$message{text})) {
 1758:                     if ($$message{ishtml} eq "false") {
 1759:                         if ($$message{isnewline} eq "true") {
 1760:                             $$message{text} =~ s#\n#<br/>#g;
 1761:                         }
 1762:                     } else {
 1763:                         $$message{text} = &HTML::Entities::decode($$message{text});
 1764:                     }
 1765:                     $contrib{'message'} .= '<br /><br />'.$$message{text};
 1766:                     my $symb = 'bulletin___'.$timestamp.'___adm/wrapper/adm/'.$cdom.'/'.$uname.'/'.$timestamp.'/bulletinboard';
 1767:                     my $postresult = &addposting($symb,\%contrib,$cdom,$crs);
 1768:                 }
 1769:             }
 1770:         }
 1771:     }
 1772: }
 1773: 
 1774: # ---------------------------------------------------------------- Add Posting to Bulletin Board
 1775: sub addposting {
 1776:     my ($symb,$contrib,$cdom,$crs)=@_;
 1777:     my $status='';
 1778:     if (($symb) && ($$contrib{message})) {
 1779:          my $crsdom = $cdom.'_'.$crs;
 1780:          &Apache::lonnet::store($contrib,$symb,$crsdom,$cdom,$crs);
 1781:          my %storenewentry=($symb => time);
 1782:          &Apache::lonnet::put('discussiontimes',\%storenewentry,$cdom,$crs);
 1783:     }
 1784:     my %record=&Apache::lonnet::restore('_discussion');
 1785:     my ($temp)=keys %record;
 1786:     unless ($temp=~/^error\:/) {
 1787:         my %newrecord=();
 1788:         $newrecord{'resource'}=$symb;
 1789:         $newrecord{'subnumber'}=$record{'subnumber'}+1;
 1790:         &Apache::lonnet::cstore(\%newrecord,'_discussion');
 1791:         $status = 'ok';
 1792:     } else {
 1793:         $status.='Failed.';
 1794:     }
 1795:     return $status;
 1796: }
 1797: 
 1798: sub parse_bb5_assessment {
 1799:     my ($res,$docroot,$container,$settings,$allanswers,$allchoices,$allids) = @_;
 1800:     my $xmlfile = $docroot.'/'.$res.".dat";
 1801:     my @state = ();
 1802:     my $id; # the current question ID
 1803:     my $answer_id; # the current answer ID
 1804:     my %toptag = ( pool => 'POOL',
 1805:                  quiz => 'ASSESSMENT',
 1806:                  survey => 'ASSESSMENT'
 1807:                );
 1808: 
 1809:     my $p = HTML::Parser->new
 1810:     (
 1811:      xml_mode => 1,
 1812:      start_h =>
 1813:      [sub {
 1814:         my ($tagname, $attr) = @_;
 1815:         push @state, $tagname;
 1816:         my $depth = 0;
 1817:         my @seq = ();
 1818:         my $class;
 1819:         my $state_str = join(" ",@state);
 1820:         if ($container eq "pool") {
 1821:             if ("@state" eq "POOL TITLE") {
 1822:                 $$settings{title} = $attr->{value};
 1823:             }
 1824:         } else {
 1825:             if ("@state" eq "ASSESSMENT TITLE") {  
 1826:                 $$settings{title} = $attr->{value};          
 1827:             } elsif ("@state" eq "ASSESSMENT FLAG" ) {
 1828:                 $$settings{isnewline} = $attr->{value};
 1829:             } elsif ("@state" eq "ASSESSMENT FLAGS ISAVAILABLE") {
 1830:                 $$settings{isavailable} = $attr->{value};
 1831:             } elsif ("@state" eq "ASSESSMENT FLAGS ISANONYMOUS" ) {
 1832:                 $$settings{isanonymous} = $attr->{id};
 1833:             } elsif ("@state" eq "ASSESSMENT FLAGS GIVE FEEDBACK" ) {
 1834:                 $$settings{feedback} = $attr->{id};        
 1835:             } elsif ("@state" eq "ASSESSMENT FLAGS SHOWCORRECT" ) {
 1836:                 $$settings{showcorrect} = $attr->{id};        
 1837:             } elsif ("@state" eq "ASSESSMENT FLAGS SHOWRESULTS" ) {
 1838:                 $$settings{showresults} = $attr->{id};        
 1839:             } elsif ("@state" eq "ASSESSMENT FLAGS ALLOWMULTIPLE" ) {
 1840:                 $$settings{allowmultiple} = $attr->{id};        
 1841:             } elsif ("@state" eq "ASSESSMENT ASSESSMENTTYPE" ) {
 1842:                 $$settings{type} = $attr->{id};        
 1843:             }
 1844:         }    
 1845:         if ("@state" eq "$toptag{$container} QUESTIONLIST QUESTION") {  
 1846:             $id = $attr->{id};
 1847:             push @{$allids}, $id;
 1848:             %{$$settings{$id}} = ();
 1849:             @{$$allanswers{$id}} = ();
 1850:             $$settings{$id}{class} = $attr->{class};
 1851:             unless ($container eq "pool") {
 1852:                 $$settings{$id}{points} = $attr->{points};
 1853:             }
 1854:             @{$$settings{$id}{correctanswer}} = ();                              
 1855:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[-1] =~ m/^QUESTION_(\w+)$/) ) {
 1856:             $id = $attr->{id};
 1857:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "BODY") && ($state[3] eq "FLAGS") ) {
 1858:             if ($state[4] eq "ISHTML") {
 1859:                 $$settings{$id}{ishtml} = $attr->{value};
 1860:             } elsif ($state[4] eq "ISNEWLINELITERAL") {
 1861:                 $$settings{$id}{newline} = $attr->{value};
 1862:             }
 1863:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "IMAGE") ) {
 1864:             $$settings{$id}{image} = $attr->{value};
 1865:             $$settings{$id}{style} = $attr->{style};
 1866:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "URL") ) {
 1867:             $$settings{$id}{url} = $attr->{value};
 1868:             $$settings{$id}{name} = $attr->{name};
 1869:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[-1] eq "ANSWER") ) {
 1870:             $answer_id = $attr->{id};
 1871:             push @{$$allanswers{$id}},$answer_id;
 1872:             %{$$settings{$id}{$answer_id}} = ();
 1873:             $$settings{$id}{$answer_id}{position} = $attr->{position};
 1874:             if ($$settings{$id}{class} eq 'QUESTION_MATCH') {
 1875:                 $$settings{$id}{$answer_id}{placement} = $attr->{placement};
 1876:                 $$settings{$id}{$answer_id}{type} = 'answer';
 1877:             }
 1878:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[-1] eq "CHOICE") ) {
 1879:             $answer_id = $attr->{id};
 1880:             push @{$$allchoices{$id}},$answer_id; 
 1881:             %{$$settings{$id}{$answer_id}} = ();
 1882:             $$settings{$id}{$answer_id}{position} = $attr->{position};
 1883:             $$settings{$id}{$answer_id}{placement} = $attr->{placement};
 1884:             $$settings{$id}{$answer_id}{type} = 'choice';
 1885:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "ANSWER") ) {
 1886:             if ($state[3] eq "IMAGE") {
 1887:                 $$settings{$id}{$answer_id}{image} = $attr->{value};
 1888:                 $$settings{$id}{$answer_id}{style} = $attr->{style};
 1889:             } elsif ($state[3] eq "URL") {
 1890:                 $$settings{$id}{$answer_id}{url} = $attr->{value};
 1891:             }
 1892:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "CHOICE") ) {
 1893:             if ($state[3] eq "IMAGE") {
 1894:                 $$settings{$id}{$answer_id}{image} = $attr->{value};
 1895:                 $$settings{$id}{$answer_id}{style} = $attr->{style};
 1896:             } elsif ($state[3] eq "URL") {
 1897:                 $$settings{$id}{$answer_id}{url} = $attr->{value};
 1898:             }
 1899:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[3] eq "CORRECTANSWER") ) {
 1900:             my $corr_answer = $attr->{answer_id};
 1901:             push @{$$settings{$id}{correctanswer}}, $corr_answer;
 1902:             my $type = $1;
 1903:             if ($type eq 'TRUEFALSE') {
 1904:                 $$settings{$id}{$corr_answer}{answer_position} = $attr->{position};
 1905:             } elsif ($type eq 'ORDER') {
 1906:                 $$settings{$id}{$corr_answer}{order} = $attr->{order};
 1907:             } elsif ($type eq 'MATCH') {
 1908:                 $$settings{$id}{$corr_answer}{choice_id} = $attr->{choice_id};
 1909:             }
 1910:         }
 1911:      }, "tagname, attr"],
 1912:      text_h =>
 1913:      [sub {
 1914:         my ($text) = @_;
 1915:         $text =~ s/^\s+//g;
 1916:         $text =~ s/\s+$//g;
 1917:         unless ($container eq "pool") {        
 1918:             if ("@state" eq "ASSESSMENT DESCRIPTION TEXT") {
 1919:                 $$settings{description} = $text;
 1920:             } elsif ("@state" eq "ASSESSMENT INSTRUCTIONS ") {
 1921:                 $$settings{instructions}{text} = $text;
 1922:             }
 1923:         }
 1924:         if ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "BODY") && ($state[-1] eq "TEXT") ) {
 1925:             unless ($text eq '') { 
 1926:                 $$settings{$id}{text} = $text;
 1927:             }
 1928:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "ANSWER") && ($state[-1] eq "TEXT") ) {
 1929:             unless ($text eq '') {
 1930:                 $$settings{$id}{$answer_id}{text} = $text;
 1931:             }
 1932:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "CHOICE") && ($state[-1] eq "TEXT") ) {
 1933:             unless ($text eq '') {
 1934:                 $$settings{$id}{$answer_id}{text} = $text;
 1935:             }
 1936:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[-1] eq "FEEDBACK_WHEN_CORRECT") ) {
 1937:             unless ($text eq '') {
 1938:                 $$settings{$id}{feedback_corr} = $text;
 1939:             }
 1940:         } elsif ( ($state[0] eq $toptag{$container}) && ($state[1] =~ m/^QUESTION_(\w+)$/) && ($state[2] eq "GRADABLE") && ($state[-1] eq "FEEDBACK_WHEN_INCORRECT") ) {
 1941:             unless ($text eq '') {
 1942:                 $$settings{$id}{feedback_incorr} = $text;
 1943:             }
 1944:         }
 1945:       }, "dtext"],
 1946:      end_h =>
 1947:      [sub {
 1948:         my ($tagname) = @_;
 1949:         pop @state;
 1950:      }, "tagname"],
 1951:     );
 1952:     $p->unbroken_text(1);
 1953:     $p->marked_sections(1);
 1954:     $p->parse_file($xmlfile);
 1955:     $p->eof;
 1956: }
 1957: 
 1958: sub parse_bb6_assessment {
 1959:     my ($res,$docroot,$container,$settings,$allids) = @_;
 1960:     my $xmlfile = $docroot.'/'.$res.".dat";
 1961:     my @state = ();
 1962:     my $id; # the current question ID
 1963:     my $response; # the current response ID
 1964:     my $foil; # the current foil ID
 1965:     my $numchoice; # the current right match choice;
 1966:     my $labelcount; # the current count of choices for a matching item.
 1967:     my $curr_shuffle;
 1968:     my $curr_class; # the current question type
 1969:     my $curr_matchitem;
 1970:     my $curr_block_type; # the current block type
 1971:     my $curr_flow; # the current flow class attribute
 1972:     my $curr_flow_mat; # the current flow_mat class attribute
 1973:     my $curr_feedback_type; # the current feedback type
 1974:     my $numorder; # counter for ordering type questions
 1975: 
 1976:     my $itemfrag = "questestinterop assessment section item";
 1977:     my $presfrag = "$itemfrag presentation flow flow";
 1978:     my $blockflow = 'flow';
 1979:     my $responselid;
 1980:     my $instructionfrag = "questestinterop assessment presentation_material flow_mat material";
 1981:     my $feedbackfrag = "$itemfrag itemfeedback";
 1982:     my $feedback_tag = '';
 1983:     my $responselid;
 1984:     my $p = HTML::Parser->new
 1985:     (
 1986:      xml_mode => 1,
 1987:      start_h =>
 1988:      [sub {
 1989:         my ($tagname, $attr) = @_;
 1990:         push @state, $tagname;
 1991:         if ("@state" eq "questestinterop assessment") {
 1992:             $$settings{title} = $attr->{title};
 1993:         }
 1994:         if ("@state" eq "questestinterop assessment rubric flow_mat material mat_extension mat_formattedtext") {
 1995:             $$settings{description}{texttype} = $attr->{type};
 1996:         }
 1997:         if ("@state" eq $presfrag) {
 1998:             if ($attr->{class} eq 'QUESTION_BLOCK') {
 1999:                 $curr_block_type = 'question';
 2000:             } elsif ($attr->{class} eq 'RESPONSE_BLOCK') {
 2001:                 $curr_block_type = 'response';
 2002:                 if ($curr_class eq 'Matching') {
 2003:                     $responselid = 'flow response_lid';
 2004:                 } else {
 2005:                     $responselid = 'response_lid';
 2006:                 }
 2007:             } elsif (($attr->{class} eq 'RIGHT_MATCH_BLOCK')) {
 2008:                 $numchoice = 0;
 2009:                 $curr_block_type = 'rightmatch';
 2010:             }
 2011:         }
 2012:         if ("@state" eq "$presfrag flow") {
 2013:             if (($curr_block_type =~ /^rightmatch/)  && ($attr->{class} eq 'Block')) {
 2014:                 $curr_block_type = 'rightmatch'.$numchoice;
 2015:                 $numchoice ++;
 2016:             }
 2017:         }
 2018:         if ($state[-1] eq 'flow') {
 2019:             $curr_flow = $attr->{class};
 2020:         }
 2021:         if ($state[-1] eq 'flow_mat') {
 2022:             $curr_flow_mat = $attr->{class};
 2023:         }
 2024:         if ("@state" eq "$presfrag $blockflow material mat_extension mat_formattedtext") {
 2025:             $$settings{$id}{$curr_block_type}{texttype} = $attr->{texttype};
 2026:         }
 2027:         if ("@state" eq "$presfrag $blockflow material matapplication") {
 2028:             $$settings{$id}{$curr_block_type}{image} = $attr->{uri};
 2029:             $$settings{$id}{$curr_block_type}{style} = $attr->{embedded};
 2030:             $$settings{$id}{$curr_block_type}{label} = $attr->{label};
 2031:         }
 2032:         if ("@state" eq "$presfrag $blockflow material mattext") {
 2033:             $$settings{$id}{$curr_block_type}{link} = $attr->{uri};
 2034:         }
 2035:         if ("@state" eq "$presfrag $responselid") {
 2036:             $response = $attr->{ident};
 2037:             $labelcount = 0; 
 2038:             if ($curr_class eq 'Matching') {
 2039:                 push(@{$$settings{$id}{answers}},$response);
 2040:                 %{$$settings{$id}{$response}} = ();
 2041:                 foreach my $key (keys(%{$$settings{$id}{$curr_block_type}})) {
 2042:                     $$settings{$id}{$response}{$key} = $$settings{$id}{$curr_block_type}{$key};
 2043:                 }
 2044:                 %{$$settings{$id}{$curr_block_type}} = ();
 2045:             }
 2046:         }
 2047:         if ("@state" eq "$presfrag $responselid render_choice") {
 2048:             $curr_shuffle = $attr->{shuffle};
 2049:         }
 2050:         if ("@state" eq "$presfrag $responselid render_choice flow_label response_label") {
 2051:             $foil = $attr->{ident};
 2052:             %{$$settings{$id}{$foil}} = ();
 2053:             $$settings{$id}{$foil}{randomize} = $curr_shuffle;
 2054:             unless ($curr_class eq 'Essay'){
 2055:                 if ($curr_class eq 'Matching') {
 2056:                     push(@{$$settings{$id}{$response}{items}},$foil);
 2057:                     $$settings{$id}{$foil}{order} = $labelcount;
 2058:                     $labelcount ++;
 2059:                 } else {
 2060:                     push(@{$$settings{$id}{answers}},$foil);
 2061:                     @{$$settings{$id}{correctanswer}} = ();
 2062:                 }
 2063:             }
 2064:         }
 2065:         if ("@state" eq "$presfrag $responselid render_choice flow_label response_label flow_mat material matapplication") {
 2066:             $$settings{$id}{$foil}{filetype} = $attr->{embedded};
 2067:             $$settings{$id}{$foil}{label} = $attr->{label};
 2068:             $$settings{$id}{$foil}{uri} = $attr->{uri};
 2069:         }
 2070:         if ("@state" eq "$presfrag $responselid render_choice flow_label response_label flow_mat material mattext") {
 2071:             $$settings{$id}{$foil}{link} = $attr->{uri};
 2072:         }
 2073:         if ("@state" eq "questestinterop assessment section item resprocessing") {
 2074:             if ($curr_class eq 'Matching') {
 2075:                 $$settings{$id}{allchoices} = $numchoice;
 2076:             }
 2077:         }
 2078:         if ("@state" eq "questestinterop assessment section item resprocessing respcondition conditionvar varequal") {
 2079:             if ($curr_class eq 'Matching') { 
 2080:                 $curr_matchitem = $attr->{respident};
 2081:             }
 2082:         }
 2083:         if ("@state" eq $feedbackfrag) {
 2084:             $curr_feedback_type = $attr->{ident};
 2085:             $feedback_tag = "";
 2086:         }
 2087:         if ("@state" eq "$feedbackfrag solution") {
 2088:             $curr_feedback_type = 'solution';
 2089:             $feedback_tag = "solution solutionmaterial";
 2090:         }
 2091:         if ("@state" eq "$feedbackfrag $feedback_tag flow_mat flow_mat material matapplication") {
 2092:             $$settings{$id}{$curr_feedback_type.'feedback'}{filetype} = $attr->{'embedded'};
 2093:             $$settings{$id}{$curr_feedback_type.'feedback'}{label} = $attr->{label};
 2094:             $$settings{$id}{$curr_feedback_type.'feedback'}{uri} = $attr->{uri};
 2095:         }
 2096:         if ("@state" eq "$feedbackfrag $feedback_tag flow_mat flow_mat material mattext") {
 2097:             $$settings{$id}{$curr_feedback_type.'feedback'}{link} = $attr->{uri};
 2098:         }
 2099:      }, "tagname, attr"],
 2100:      text_h =>
 2101:      [sub {
 2102:         my ($text) = @_;
 2103:         $text =~ s/^\s+//g;
 2104:         $text =~ s/\s+$//g;
 2105:         if ("@state" eq "questestinterop assessment rubric flow_mat material mat_extension mat_formattedtext") {
 2106:             $$settings{description}{text} = $text;
 2107:         }
 2108:         if ("@state" eq "questestinterop assessment rubric flow_mat material mattext") {
 2109:             $$settings{description}{text} = $text;
 2110:         }
 2111:         if ("@state" eq "$instructionfrag mat_extension mat_formattedtext") {
 2112:             $$settings{instructions}{text} = $text;
 2113:         }
 2114:         if ("@state" eq "$instructionfrag mattext") {
 2115:             $$settings{instructions}{text} = $text;
 2116:         }
 2117:         if ("@state" eq "questestinterop assessment section item itemmetadata bbmd_asi_object_id") {
 2118:             $id = $text;
 2119:             push @{$allids}, $id;
 2120:             %{$$settings{$id}} = ();
 2121:             @{$$settings{$id}{answers}} = ();
 2122:             %{$$settings{$id}{question}} = ();
 2123:             %{$$settings{$id}{correctfeedback}} = ();
 2124:             %{$$settings{$id}{incorrectfeedback}} = ();
 2125:             %{$$settings{$id}{solutionfeedback}} = ();
 2126:         }
 2127:         if ("@state" eq "questestinterop assessment section item itemmetadata bbmd_questiontype") {
 2128:             $$settings{$id}{class} = $text;
 2129:             $curr_class = $text;
 2130:             if ($curr_class eq 'Matching') {
 2131:                 $blockflow = 'flow flow';
 2132:             } else {
 2133:                 $blockflow = 'flow';
 2134:             } 
 2135:         }
 2136:         if ("@state" eq "$presfrag $blockflow material mat_extension mat_formattedtext") {
 2137:             $$settings{$id}{$curr_block_type}{text} = $text;
 2138:         }
 2139:         if ("@state" eq "$presfrag $blockflow material mattext") {
 2140:             if ($curr_flow eq 'LINK_BLOCK') { 
 2141:                 $$settings{$id}{$curr_block_type}{linkname} = $text;
 2142:             } elsif ($curr_flow eq 'FORMATTED_TEXT_BLOCK') {
 2143:                 $$settings{$id}{$curr_block_type}{text} = $text;
 2144:             }
 2145:         }
 2146:         if ("@state" eq "$presfrag $responselid render_choice flow_label response_label flow_mat material mat_extension mat_formattedtext") {
 2147:             $$settings{$id}{$foil}{text} = $text;
 2148:         }
 2149:         if ("@state" eq "$presfrag $responselid render_choice flow_label response_label flow_mat material mattext") {
 2150:             if ($curr_flow_mat eq 'LINK_BLOCK') {
 2151:                 $$settings{$id}{$foil}{linkname} = $text;
 2152:             } else {
 2153:                 $$settings{$id}{$foil}{text} = $text;
 2154:             } 
 2155:         }
 2156:         if ("@state" eq "questestinterop assessment section item resprocessing respcondition conditionvar varequal") {
 2157:             if ($curr_class eq 'Matching') {
 2158:                 $$settings{$id}{$curr_matchitem}{correctanswer} = $text;
 2159:             } else {
 2160:                 push(@{$$settings{$id}{correctanswer}},$text);
 2161:             }
 2162:         }
 2163:         if ("@state" eq "questestinterop assessment section item resprocessing respcondition conditionvar") {
 2164:             $numorder = 0;
 2165:         }
 2166:         if ("@state" eq "questestinterop assessment section item resprocessing respcondition conditionvar and varequal") {
 2167:             push(@{$$settings{$id}{correctanswer}},$text);
 2168:             if ($curr_class eq 'Ordering') {
 2169:                 $numorder ++;
 2170:                 $$settings{$id}{$text}{order} = $numorder;
 2171:             }
 2172:         }
 2173:         if ("@state" eq "$feedbackfrag $feedback_tag flow_mat flow_mat material mat_extension mat_formattedtext") {
 2174:             $$settings{$id}{$curr_feedback_type.'feedback'}{text} = $text;
 2175:         }
 2176:         if ("@state" eq "$feedbackfrag $feedback_tag flow_mat flow_mat material mattext") {
 2177:             $$settings{$id}{$curr_feedback_type.'feedback'}{linkname} = $text;
 2178:         }
 2179:      }, "dtext"],
 2180:      end_h =>
 2181:      [sub {
 2182:         my ($tagname) = @_;
 2183:         pop @state;
 2184:      }, "tagname"],
 2185:     );
 2186:     $p->unbroken_text(1);
 2187:     $p->marked_sections(1);
 2188:     $p->parse_file($xmlfile);
 2189:     $p->eof;
 2190:     return;
 2191: }
 2192: 
 2193: sub parse_webctvista4_assessment {
 2194:     my ($res,$docroot,$href,$allids,$qzparams) = @_;
 2195:     my $xmlfile = $docroot.'/'.$href; #assessment file
 2196:     my @state = ();
 2197:     my $id; # the current question ID
 2198:     my $fieldlabel; # the current qti metadata field label
 2199:     my $outcome_id; # the current question ID for outcomes conditions
 2200:     my $pname; # the current outcomes parameter name
 2201:     my $numids = 0;
 2202:     %{$$qzparams{$res}} = ();
 2203:     %{$$qzparams{$res}{weight}} = ();
 2204: 
 2205:     my $p = HTML::Parser->new
 2206:     (
 2207:      xml_mode => 1,
 2208:      start_h =>
 2209:      [sub {
 2210:         my ($tagname, $attr) = @_;
 2211:         push @state, $tagname;
 2212:         my @seq = ();
 2213:         if ("@state" eq "questestinterop assessment") {
 2214:             $$qzparams{$res}{id} = $attr->{'ident'};
 2215:             $$qzparams{$res}{title} = $attr->{'title'};
 2216:         }
 2217:         if ("@state" eq "questestinterop assessment section itemref") {
 2218:             $id = $attr->{linkrefid};
 2219:             push(@{$allids},$id);
 2220:             $numids ++;
 2221:         }
 2222:         if ("@state" eq "questestinterop assessment section selection_ordering order") {
 2223:            $$qzparams{$res}{order_type} = $attr->{order_type};
 2224:         }
 2225:      }, "tagname, attr"],
 2226:      text_h =>
 2227:      [sub {
 2228:         my ($text) = @_;
 2229:         if ("@state" eq "questestinterop assessment qtimetadata qtimetadatafield fieldlabel") {
 2230:             $fieldlabel = $text;
 2231:         }
 2232:         if ("@state" eq "questestinterop assessment qtimetadata qtimetadatafield fieldentry") {
 2233:             $$qzparams{$res}{$fieldlabel} = $text;
 2234:         }
 2235:         if ("@state" eq "questestinterop assessment section outcomes_processing objects_condition outcomes_metadata") {
 2236:             $outcome_id = $text;
 2237:         }
 2238:         if ("@state" eq "questestinterop assessment section outcomes_processing objects_condition objects_parameter") {
 2239:             if ($pname eq 'qmd_weighting') {
 2240:                 $$qzparams{$res}{weight}{$outcome_id} = $text;
 2241:             }
 2242:         }
 2243:         if ("@state" eq "questestinterop assessment section selection_ordering selection selection_number") {
 2244:             $$qzparams{$res}{numpick} = $text;
 2245:         }
 2246:       }, "dtext"],
 2247:      end_h =>
 2248:      [sub {
 2249:         my ($tagname) = @_;
 2250:         pop @state;
 2251:      }, "tagname"],
 2252:     );
 2253:     $p->unbroken_text(1);
 2254:     $p->parse_file($xmlfile);
 2255:     $p->eof;
 2256:     unless(defined($$qzparams{$res}{numpick})) {
 2257:         $$qzparams{$res}{numpick} = $numids;
 2258:     }
 2259: }
 2260: 
 2261: sub parse_webctvista4_question {
 2262:     my ($res,$docroot,$resources,$hrefs,$settings,$allquestids,$allanswers,$allchoices,$parent,$catinfo) = @_;
 2263:     my $xmlfile = $docroot.'/'.$$resources{$res}{file};
 2264:     my %classtypes = (
 2265:                       WCT_Calculated => 'numerical',
 2266:                       WCT_TrueFalse => 'multiplechoice',
 2267:                       WCT_ShortAnswer => 'shortanswer',
 2268:                       WCT_Paragraph => 'paragraph',
 2269:                       WCT_MultipleChoice => 'multiplechoice',
 2270:                       WCT_Matching => 'match',
 2271:                       WCT_JumbledSentence => 'jumbled',
 2272:                       WCT_FillInTheBlank => 'string',
 2273:                       WCT_Combination => 'combination'
 2274:     );
 2275:     my @state = ();
 2276:     my $fieldlabel;
 2277:     my %questiondata;
 2278:     my $id; # the current question ID
 2279:     my $list; # the current list ID for multiple choice questions 
 2280:     my $numid; # the current answer ID for numerical questions
 2281:     my $grp; # the current group ID for matching questions
 2282:     my $label; # the current reponse label for string questions
 2283:     my $str_id; # the current string ID for string questions
 2284:     my $unitid; # the current unit ID for numerical questions
 2285:     my $answer_id;  # the current answer ID 
 2286:     my $fdbk; # the current feedback ID
 2287:     my $currvar; # the current variable for numerical problems
 2288:     my $fibtype; # the current fill-in-blank type for numerical or string
 2289:     my $prompt;
 2290:     my $rows;
 2291:     my $columns;
 2292:     my $maxchars;
 2293:     my %setvar = (
 2294:                    varname => '',
 2295:                    action => '',
 2296:                  );
 2297:     my $currtexttype;
 2298:     my $jumble_item;
 2299:     my $numbox = 0;
 2300:     my %str_answers = ();
 2301:     my $textlabel;
 2302:     my $currindex;
 2303:     my %varinfo = ();
 2304:     my $formula;
 2305:     my $jumbnum = 0;
 2306:     my $p = HTML::Parser->new
 2307:     (
 2308:      xml_mode => 1,
 2309:      start_h =>
 2310:      [sub {
 2311:         my ($tagname, $attr) = @_;
 2312:         push @state, $tagname;
 2313:         if ("@state" eq "questestinterop item") {
 2314:             $id = $attr->{ident};
 2315:             push(@{$allquestids},$id);
 2316:             %{$$settings{$id}} = ();
 2317:             %{$varinfo{$id}} = ();
 2318:             @{$$allchoices{$id}} = ();
 2319:             @{$$settings{$id}{grps}} = ();
 2320:             @{$$settings{$id}{lists}} = ();
 2321:             @{$$settings{$id}{feedback}} = ();
 2322:             @{$$settings{$id}{str}} = ();
 2323:             %{$$settings{$id}{strings}} = ();
 2324:             @{$$settings{$id}{numids}} = ();
 2325:             %{$$allanswers{$id}} = ();
 2326:             $$settings{$id}{title} = $attr->{title};
 2327:         }
 2328:         if ("@state" eq "questestinterop item presentation flow material mat_extension webct:calculated webct:var") {
 2329:             $currvar = $attr->{'webct:name'};
 2330:             %{$varinfo{$id}{$currvar}} = ();
 2331:             $varinfo{$id}{$currvar}{min} = $attr->{'webct:min'};
 2332:             $varinfo{$id}{$currvar}{max} = $attr->{'webct:max'};
 2333:             $varinfo{$id}{$currvar}{precision} = $attr->{'webct:precision'};
 2334:         }
 2335:         if ("@state" eq "questestinterop item presentation flow response_num") {
 2336:             $numid = $attr->{ident};
 2337:             push(@{$$settings{$id}{numids}},$numid);
 2338:             %{$$settings{$id}{$numid}} = ();
 2339:             %{$$settings{$id}{$numid}{vars}} = ();
 2340:             @{$$settings{$id}{$numid}{units}} = ();
 2341:             $$settings{$id}{$numid}{rcardinality} = $attr->{rcardinality};
 2342:             $$settings{$id}{$numid}{formula} = $formula;
 2343:             foreach my $var (keys(%{$varinfo{$id}})) {
 2344:                 %{$$settings{$id}{$numid}{vars}{$var}} = %{$varinfo{$id}{$var}};
 2345:             }
 2346:         }
 2347:         if ("@state" eq "questestinterop item presentation flow material mat_extension webct:variable") {
 2348:             $$settings{$id}{text} .= '['.$attr->{'webct:name'}.']';
 2349:         }
 2350:         if ("@state" eq "questestinterop item presentation flow material matimage") {
 2351:             $$settings{$id}{image} = $attr->{uri};
 2352:         }
 2353: 
 2354:         if ("@state" eq "questestinterop item presentation flow material mattext")  {
 2355:             $currtexttype = lc($attr->{texttype});
 2356:             $$settings{$id}{texttype} = $currtexttype;
 2357:             if ($$settings{$id}{class} eq 'combination') {
 2358:                 if (exists($attr->{label})) {
 2359:                     $textlabel = $attr->{label};
 2360:                 } else {
 2361:                     $textlabel = '';
 2362:                 }
 2363:             }
 2364:         }
 2365:         if ("@state" eq "questestinterop item presentation flow response_lid") {
 2366:             $list = $attr->{ident};
 2367:             push(@{$$settings{$id}{lists}},$list);
 2368:             %{$$settings{$id}{$list}} = ();
 2369:             @{$$allanswers{$id}{$list}} = ();
 2370:             @{$$settings{$id}{$list}{correctanswer}} = ();
 2371:             @{$$settings{$id}{$list}{jumbledtext}} = ();
 2372:             @{$$settings{$id}{$list}{jumbledtype}} = ();
 2373:             @{$$settings{$id}{$list}{jumbled}} = ();
 2374:             $$settings{$id}{$list}{rcardinality} = $attr->{rcardinality};
 2375:         }
 2376: # Jumbled sentence
 2377:         if ("@state" eq "questestinterop item presentation flow response_lid render_extension ims_render_object")  {
 2378:             $$settings{$id}{$list}{orientation} = $attr->{orientation};
 2379:         }
 2380:         if ("@state" eq "questestinterop item presentation flow response_lid render_extension ims_render_object material mattext")  {
 2381:             $currtexttype = lc($attr->{texttype});
 2382:             $$settings{$id}{$list}{texttype} = $currtexttype;
 2383:         }
 2384:         if ("@state" eq "questestinterop item presentation flow response_lid render_extension ims_render_object response_label")  {
 2385:             $jumble_item = $attr->{ident};
 2386:         }
 2387:         if ("@state" eq "questestinterop item presentation flow response_lid render_extension ims_render_object response_label material mattext")  {
 2388:             $currtexttype = lc($attr->{texttype});
 2389:             $$settings{$id}{$list}{$jumble_item}{texttype} = $currtexttype;
 2390:         }
 2391:         if ("@state" eq "questestinterop item resprocessing respcondition") { # Jumbled
 2392:             if ($$settings{$id}{class} eq 'jumbled') {
 2393:                 $jumbnum ++;
 2394:                 @{$$settings{$id}{$list}{jumbled}[$jumbnum]} = (); 
 2395:             }
 2396:         }
 2397: 
 2398:         if ("@state" eq "questestinterop item resprocessing respcondition conditionvar and varequal") { # Jumbled
 2399:             $currindex = $attr->{index};
 2400:         }
 2401:         if ("@state" eq "questestinterop item presentation flow response_lid render_choice") {
 2402:             $$settings{$id}{$list}{randomize} = $attr->{shuffle};
 2403:         }
 2404: # Multiple Choice, True/False and Combination
 2405:         if ("@state" eq "questestinterop item presentation flow response_lid render_choice flow_label response_label") {
 2406:             $answer_id = $attr->{ident};
 2407:             push(@{$$allanswers{$id}{$list}},$answer_id);
 2408:             %{$$settings{$id}{$list}{$answer_id}} = ();
 2409:         }
 2410: # True/False
 2411:         if ("@state" eq "questestinterop item presentation flow response_lid render_choice flow_label response_label material mat_extension webct:localizable_mattext") {
 2412:             $currtexttype = lc($attr->{texttype});
 2413:             $$settings{$id}{$list}{$answer_id}{texttype} = $currtexttype;
 2414:         }
 2415: 
 2416: # Multiple Choice and Combination
 2417:         if ("@state" eq "questestinterop item presentation flow response_lid render_choice flow_label response_label material mattext") {
 2418:             $currtexttype = lc($attr->{texttype});
 2419:             $$settings{$id}{$list}{$answer_id}{texttype} = $currtexttype;
 2420:         }
 2421: 
 2422: # String, Shortanswer or Paragraph
 2423:         if (($$settings{$id}{class} eq 'string') || 
 2424:             ($$settings{$id}{class} eq 'shortanswer') ||
 2425:             ($$settings{$id}{class} eq 'paragraph')) { 
 2426:             if ("@state" eq "questestinterop item presentation flow response_str") {
 2427:                 $str_id = $attr->{ident};
 2428:                 %{$$settings{$id}{$str_id}} = ();
 2429:                 push(@{$$settings{$id}{str}},$str_id);
 2430:                 $$settings{$id}{$str_id}{rcardinality} = $attr->{rcardinality};
 2431:                 @{$$settings{$id}{$str_id}{labels}} = ();
 2432:                 %{$$settings{$id}{$str_id}{comparison}} = ();
 2433:             }
 2434:         }
 2435:         if ("@state" eq "questestinterop item presentation flow response_str material mattext") { # string
 2436:             $currtexttype = lc($attr->{texttype});
 2437:             $$settings{$id}{$str_id}{texttype} = $currtexttype;
 2438:         }
 2439:         if ("@state" eq "questestinterop item presentation flow response_str render_fib") {
 2440:             $fibtype = $attr->{fibtype};
 2441:             $prompt = $attr->{prompt};
 2442:             $rows = $attr->{rows};
 2443:             $columns = $attr->{columns};
 2444:             $maxchars = $attr->{maxchars};
 2445:         }
 2446:         if ("@state" eq "questestinterop item presentation flow response_str render_fib response_label") {
 2447:             push(@{$$settings{$id}{$str_id}{labels}},$label);
 2448:             @{$$settings{$id}{strings}{$str_id}} = ();
 2449:             %{$$settings{$id}{$str_id}{$label}} = ();
 2450:             $$settings{$id}{$str_id}{$label}{fibtype} = $fibtype;
 2451:             if ($$settings{$id}{class} eq 'string') {
 2452:                 $$settings{$id}{text} .= '________';
 2453:             }
 2454:         }
 2455:         if ("@state" eq "questestinterop item presentation flow response_str render_fib response_label material mattext") { # Paragraph
 2456:             $textlabel = $attr->{label}; 
 2457:         }
 2458: # Matching
 2459:         if ("@state" eq "questestinterop item presentation flow flow response_grp") {
 2460:             $grp = $attr->{ident};
 2461:             push(@{$$settings{$id}{grps}},$grp);
 2462:             %{$$settings{$id}{$grp}} = ();
 2463:             @{$$allanswers{$id}{$grp}} = ();
 2464:             @{$$settings{$id}{$grp}{correctanswer}} = ();
 2465:             $$settings{$id}{$grp}{rcardinality} = $attr->{rcardinality};
 2466:         }
 2467:         if ("@state" eq "questestinterop item presentation flow flow response_grp material mattext") {
 2468:             $currtexttype = lc($attr->{texttype});
 2469:             $$settings{$id}{$grp}{texttype} = $currtexttype;
 2470:         }
 2471:         if ("@state" eq "questestinterop item presentation flow flow response_grp render_choice flow_label response_label") {
 2472:             $answer_id = $attr->{ident};
 2473:             push(@{$$allanswers{$id}{$grp}},$answer_id);
 2474:             %{$$settings{$id}{$grp}{$answer_id}} = ();
 2475:             $currtexttype = lc($attr->{texttype});
 2476:             $$settings{$id}{$grp}{$answer_id}{texttype} =  $currtexttype;
 2477:         }
 2478: # Multiple choice or combination or string or match 
 2479:         if ("@state" eq "questestinterop item resprocessing respcondition conditionvar varequal") {
 2480:             if (($$settings{$id}{class} eq 'multiplechoice') || 
 2481:                 ($$settings{$id}{class} eq 'combination')) {
 2482:                 $list = $attr->{respident};
 2483:             } elsif (($$settings{$id}{class} eq 'string') ||
 2484:                      ($$settings{$id}{class} eq 'shortanswer')) {
 2485:                 $label = $attr->{respident};
 2486:                 $$settings{$id}{$label}{case} = $attr->{'case'};
 2487:             } elsif ($$settings{$id}{class} eq 'match') {
 2488:                 $grp = $attr->{respident};
 2489:             }
 2490:         }
 2491:         if ("@state" eq "questestinterop item resprocessing") {
 2492:             if (($$settings{$id}{class} eq 'string') ||
 2493:                 ($$settings{$id}{class} eq 'shortanswer')) {
 2494:                 foreach my $str_id (@{$$settings{$id}{str}}) {
 2495:                     @{$str_answers{$str_id}} = ();
 2496:                 }
 2497:             }
 2498:         }
 2499:         if ("@state" eq "questestinterop item resprocessing respcondition") {
 2500:             if (($$settings{$id}{class} eq 'string') ||
 2501:                 ($$settings{$id}{class} eq 'shortanswer')) { 
 2502:                 $numbox ++;
 2503:             }
 2504:         }
 2505:         if ("@state" eq "questestinterop item resprocessing respcondition setvar") {
 2506:             foreach my $key (keys(%{$attr})) {
 2507:                 $setvar{$key} = $attr->{$key};
 2508:             }
 2509:         }
 2510:         if (($$settings{$id}{class} eq 'string') ||
 2511:             ($$settings{$id}{class} eq 'shortanswer')) {
 2512:             if (("@state" eq "questestinterop item resprocessing respcondition conditionvar or varsubset") || ("@state" eq "questestinterop item resprocessing respcondition conditionvar varsubset")) {
 2513:                 $str_id = $attr->{respident};
 2514:                 $$settings{$id}{$str_id}{case} = $attr->{case};
 2515:             }
 2516:         }
 2517:         if ("@state" eq "questestinterop item resprocessing respcondition conditionvar and varsubset") {
 2518:             $list = $attr->{respident};
 2519:         }
 2520: # Numerical
 2521:         if ("@state" eq "questestinterop item resprocessing itemproc_extension webct:calculated_answer") {
 2522:             $numid = $attr->{respident};
 2523:             $$settings{$id}{$numid}{toltype} = $attr->{'webct:toleranceType'};
 2524:             $$settings{$id}{$numid}{tolerance} = $attr->{'webct:tolerance'};
 2525:         }
 2526:         if ("@state" eq "questestinterop item resprocessing itemproc_extension unit_eval conditionvar varequal") {
 2527:             $unitid = $attr->{respident};
 2528:             %{$$settings{$id}{$numid}{$unitid}} = ();
 2529:             push(@{$$settings{$id}{$numid}{units}},$unitid);
 2530:             $$settings{$id}{$numid}{$unitid}{case} = $attr->{case};
 2531:         }
 2532: # Feedback
 2533:         if ("@state" eq "questestinterop item respcondition displayfeedback") {
 2534:             $fdbk = $attr->{linkrefid};
 2535:             push(@{$$settings{$id}{feedback}},$fdbk);
 2536:             $$settings{$id}{$fdbk} = ();
 2537:             $$settings{$id}{$fdbk}{feedbacktype} = $attr->{feedbacktype};
 2538:         }
 2539:         if ("@state" eq "questestinterop item itemfeedback") {
 2540:             $fdbk = $attr->{ident};
 2541:             push(@{$$settings{$id}{feedback}},$fdbk);
 2542:             $$settings{$id}{$fdbk}{view} = $attr->{view};
 2543:         }
 2544:         if ("@state" eq "questestinterop item itemfeedback material mattext") {
 2545:             $currtexttype = lc($attr->{texttype});
 2546:             $$settings{$id}{$fdbk}{texttype} = $currtexttype;
 2547:         }
 2548:         if ("@state" eq "questestinterop item itemfeedback solution solutionmaterial material mattext") {
 2549:             $currtexttype = lc($attr->{texttype});
 2550:             $$settings{$id}{$fdbk}{texttype} = $currtexttype;
 2551:         }
 2552:      }, "tagname, attr"],
 2553:      text_h =>
 2554:      [sub {
 2555:         my ($text) = @_;
 2556:         if ($currtexttype eq '/text/html') {
 2557:             $text =~ s#(&lt;img\ssrc=")([^"]+)"&gt;#$1../resfiles/$2#g;
 2558:         }
 2559:         if ("@state" eq "questestinterop item itemmetadata qtimetadata qtimetadatafield fieldlabel") {
 2560:             $fieldlabel = $text;
 2561:         }
 2562:         if ("@state" eq "questestinterop item itemmetadata qtimetadata qtimetadatafield fieldentry") {
 2563:             $questiondata{$fieldlabel} = $text;
 2564:             if ($fieldlabel eq 'wct_questiontype') {
 2565:                 $$settings{$id}{class} = $classtypes{$text};
 2566:             } elsif ($fieldlabel eq 'wct_questioncategory') {
 2567:                 $$settings{$id}{category} = $text;
 2568:                 unless(exists($$catinfo{$text})) {
 2569:                     %{$$catinfo{$text}} = ();
 2570:                     $$catinfo{$text}{title} = $text;
 2571:                 }
 2572:                 push(@{$$catinfo{$text}{contents}},$id);
 2573:             }
 2574:         }
 2575:         if ("@state" eq "questestinterop item presentation flow material mat_extension webct:calculated webct:formula") {
 2576:             $formula = $text;
 2577:         }
 2578:         if ("@state" eq "questestinterop item presentation flow response_str material mattext") {
 2579:             $$settings{$id}{$str_id}{text} = $text;
 2580:         }
 2581:         if ("@state" eq "questestinterop item presentation flow response_str render_fib response_label material mattext") { # Paragraph
 2582:             if ($textlabel eq 'PRE_FILL_ANSWER') {
 2583:                 $$settings{$id}{$str_id}{$label}{$textlabel} = $text;
 2584:             }
 2585:         }
 2586: # Matching
 2587:         if ("@state" eq "questestinterop item presentation flow response_lid render_choice flow_label response_label material mattext") {
 2588:             $$settings{$id}{$list}{$answer_id}{text} .= $text;
 2589:         }
 2590: # Multiple choice, True/False, Combination
 2591:         if ("@state" eq "questestinterop item presentation flow response_lid render_choice flow_label response_label material mat_extension webct:localizable_mattext") {
 2592:             $$settings{$id}{$list}{$answer_id}{text} = $text;
 2593:         }
 2594:         if ("@state" eq "questestinterop item presentation flow response_lid render_extension ims_render_object material mattext")  {
 2595:             push(@{$$settings{$id}{$list}{jumbledtext}},$text);
 2596:             push(@{$$settings{$id}{$list}{jumbledtype}},'No');
 2597:         }
 2598:         if ("@state" eq "questestinterop item presentation flow response_lid render_extension ims_render_object response_label material mattext")  {
 2599:             $$settings{$id}{$list}{$jumble_item}{text} = $text;
 2600:             push(@{$$settings{$id}{$list}{jumbledtext}},$text);
 2601:             push(@{$$settings{$id}{$list}{jumbledtype}},'Yes');
 2602:         }
 2603:         if ("@state" eq "questestinterop item presentation flow material mattext")  {
 2604:             $$settings{$id}{text} .= $text;
 2605:             if ($$settings{$id}{class} eq 'combination') {
 2606:                 if ($textlabel =~ /^wct_question_label_\d+$/) {
 2607:                     $$settings{$id}{text} .= '<br />';
 2608:                 }
 2609:                 if ($textlabel =~ /^wct_cmc_single_answer\d+$/) {
 2610:                     $$settings{$id}{text} .= '<br />';
 2611:                 }
 2612:             }
 2613:         }
 2614: # Matching
 2615:         if ("@state" eq "questestinterop item presentation flow flow response_grp material mattext")  {
 2616:             $$settings{$id}{$grp}{text} = $text;
 2617:             unless ($text eq '') {
 2618:                 push(@{$$allchoices{$id}},$grp);
 2619:             }
 2620:         }
 2621:         if ("@state" eq "questestinterop item presentation flow flow response_grp render_choice flow_label response_label material mattext") {
 2622:             $$settings{$id}{$grp}{$answer_id}{text} = $text;
 2623:         }
 2624: # Numerical
 2625:         if ("@state" eq "questestinterop item resprocessing itemproc_extension unit_eval conditionvar varequal") {
 2626:             $$settings{$id}{$numid}{$unitid}{text} = $text;
 2627:         }
 2628:         if ("@state" eq "questestinterop item resprocessing respcondition conditionvar varequal") {
 2629:             if (($$settings{$id}{class} eq 'string') ||
 2630:                 ($$settings{$id}{class} eq 'shortanswer')) {
 2631:                 unless (grep/^$text$/,@{$str_answers{$str_id}}) {
 2632:                     push(@{$str_answers{$str_id}},$text);
 2633:                     $$settings{$id}{$str_id}{comparison}{$text} = $questiondata{'wct_comparison_type'.$numbox};
 2634:                 }
 2635:             } else {
 2636:                 $answer_id = $text;
 2637:             }
 2638:         }
 2639:         if (("@state" eq "questestinterop item resprocessing respcondition conditionvar or varsubset") || ("@state" eq "questestinterop item resprocessing respcondition conditionvar varsubset")) { # string
 2640:             if (($$settings{$id}{class} eq 'string') ||
 2641:                 ($$settings{$id}{class} eq 'shortanswer')) {
 2642:                 unless (grep/^$text$/,@{$str_answers{$str_id}}) {
 2643:                     push(@{$str_answers{$str_id}},$text);
 2644:                     $$settings{$id}{$str_id}{comparison}{$text} = $questiondata{'wct_comparison_type'.$numbox};
 2645:                 }
 2646:             }
 2647:         }
 2648: 
 2649:         if ("@state" eq "questestinterop item resprocessing respcondition conditionvar and varequal") { # Jumbled
 2650:             $$settings{$id}{$list}{jumbled}[$jumbnum][$currindex] = $text;
 2651:         }
 2652:         if ("@state" eq "questestinterop item resprocessing respcondition setvar") {
 2653:             if ($setvar{varname} eq "SCORE") { # Multiple Choice, String or Match
 2654:                 if ($text =~ m/^[\d\.]+$/) {
 2655:                     if ($text > 0) {
 2656:                         if (($$settings{$id}{class} eq 'multiplechoice') ||
 2657:                             ($$settings{$id}{class} eq 'combination')) {
 2658:                             push(@{$$settings{$id}{$list}{correctanswer}},$answer_id);
 2659:                         } elsif (($$settings{$id}{class} eq 'string') ||
 2660:                                  ($$settings{$id}{class} eq 'shortanswer')) {
 2661:                             foreach my $answer (@{$str_answers{$str_id}}) {
 2662:                                 unless (grep/^$answer$/,@{$$settings{$id}{strings}{$str_id}}) {
 2663:                                     push(@{$$settings{$id}{strings}{$str_id}},$answer);
 2664:                                 }
 2665:                             }
 2666:                         } elsif ($$settings{$id}{class} eq 'match') {
 2667:                             push(@{$$settings{$id}{$grp}{correctanswer}},$answer_id);
 2668:                         }
 2669:                     }
 2670:                 }
 2671:             }
 2672:         }
 2673:         if ("@state" eq "questestinterop item itemfeedback material mattext") {
 2674:             $$settings{$id}{$fdbk}{text} = $text;
 2675:         }
 2676:         if ("@state" eq "questestinterop item itemfeedback solution solutionmaterial material mattext") {
 2677:             $$settings{$id}{$fdbk}{text} = $text;
 2678:         }
 2679:       }, "dtext"],
 2680:      end_h =>
 2681:      [sub {
 2682:         my ($tagname) = @_;
 2683:         pop @state;
 2684:      }, "tagname"],
 2685:     );
 2686:     $p->unbroken_text(1);
 2687:     $p->parse_file($xmlfile);
 2688:     $p->eof;
 2689: }
 2690: 
 2691: sub parse_webct4_assessment {
 2692:     my ($res,$docroot,$href,$container,$allids) = @_;
 2693:     my $xmlfile = $docroot.'/'.$href; #quiz file
 2694:     my @state = ();
 2695:     my $id; # the current question ID
 2696:     my $p = HTML::Parser->new
 2697:     (
 2698:      xml_mode => 1,
 2699:      start_h =>
 2700:      [sub {
 2701:         my ($tagname, $attr) = @_;
 2702:         push @state, $tagname;
 2703:         my $depth = 0;
 2704:         my @seq = ();
 2705:         if ("@state" eq "questestinterop assessment section itemref") {
 2706:             $id = $attr->{linkrefid}; 
 2707:             push(@{$allids},$id);
 2708:         }
 2709:      }, "tagname, attr"],
 2710:      text_h =>
 2711:      [sub {
 2712:         my ($text) = @_;
 2713:       }, "dtext"],
 2714:      end_h =>
 2715:      [sub {
 2716:         my ($tagname) = @_;
 2717:         pop @state;
 2718:      }, "tagname"],
 2719:     );
 2720:     $p->unbroken_text(1);
 2721:     $p->parse_file($xmlfile);
 2722:     $p->eof;
 2723: }
 2724: 
 2725: sub parse_webct4_quizprops {
 2726:     my ($res,$docroot,$href,$container,$qzparams) = @_;
 2727:     my $xmlfile = $docroot.'/'.$href; #properties file
 2728:     my @state = ();
 2729:     %{$$qzparams{$res}} = ();
 2730:     my $p = HTML::Parser->new
 2731:     (
 2732:      xml_mode => 1,
 2733:      start_h =>
 2734:      [sub {
 2735:         my ($tagname, $attr) = @_;
 2736:         push @state, $tagname;
 2737:      }, "tagname, attr"],
 2738:      text_h =>
 2739:      [sub {
 2740:         my ($text) = @_;
 2741:         if ($state[0] eq 'properties' && $state[1] eq 'delivery')  {
 2742:             if ($state[2] eq 'time_available') {
 2743:                 $$qzparams{$res}{opendate} = $text;
 2744:             } elsif ($state[2] eq 'time_due') {
 2745:                 $$qzparams{$res}{duedate} = $text;
 2746:             } elsif ($state[3] eq 'max_attempt') {
 2747:                 $$qzparams{$res}{tries} = $text;
 2748:             } elsif ($state[3] eq 'post_submission') {
 2749:                 $$qzparams{$res}{posts} = $text;
 2750:             } elsif ($state[3] eq 'method') {
 2751:                 $$qzparams{$res}{method} = $text;
 2752:             }
 2753:         } elsif ($state[0] eq 'properties' && $state[1] eq 'processing')  {
 2754:             if ($state[2] eq 'scores' && $state[3] eq 'score') {
 2755:                 $$qzparams{$res}{weight} = $text;
 2756:             } elsif ($state[2] eq 'selection' && $state[3] eq 'select') {
 2757:                 $$qzparams{$res}{numpick} = $text;
 2758:             }
 2759:         } elsif ($state[0] eq 'properties' && $state[1] eq 'result') {
 2760:             if ($state[2] eq 'display_answer') {
 2761:                 $$qzparams{$res}{showanswer} = $text;
 2762:             }
 2763:         } 
 2764:       }, "dtext"],
 2765:      end_h =>
 2766:      [sub {
 2767:         my ($tagname) = @_;
 2768:         pop @state;
 2769:      }, "tagname"],
 2770:     );
 2771:     $p->unbroken_text(1);
 2772:     $p->parse_file($xmlfile);
 2773:     $p->eof;
 2774: }
 2775: 
 2776: sub parse_webct4_questionDB {
 2777:     my ($docroot,$href,$catinfo,$settings,$allanswers,$allchoices,$allids) = @_;
 2778:     $href =~ s#[^/]+$##;
 2779:     my $xmlfile = $docroot.'/'.$href.'questionDB.xml'; #quizDB file
 2780:     my @state = ();
 2781:     my $category; # the current category ID
 2782:     my $id; # the current question ID
 2783:     my $list; # the current list ID for multiple choice questions
 2784:     my $numid; # the current answer ID for numerical questions
 2785:     my $grp; # the current group ID for matching questions
 2786:     my $label; # the current reponse label for string questions 
 2787:     my $str_id; # the current string ID for string questions
 2788:     my $unitid; # the current unit ID for numerical questions
 2789:     my $answer_id; # the current answer ID
 2790:     my $fdbk; # the current feedback ID
 2791:     my $currvar; # the current variable for numerical problems
 2792:     my $fibtype; # the current fill-in-blank type for numerical or string
 2793:     my $prompt;
 2794:     my $boxnum; 
 2795:     my %setvar = (
 2796:                    varname => '',
 2797:                    action => '',
 2798:                  );
 2799:     my $currtexttype;
 2800:     my $currimagtype;  
 2801:     my $p = HTML::Parser->new
 2802:     (
 2803:      xml_mode => 1,
 2804:      start_h =>
 2805:      [sub {
 2806:         my ($tagname, $attr) = @_;
 2807:         push @state, $tagname;
 2808:         if ("@state" eq "questestinterop section") {
 2809:             $category = $attr->{ident};
 2810:             %{$$catinfo{$category}} = ();
 2811:             $$catinfo{$category}{title} = $attr->{title};   
 2812:         }
 2813:         if ("@state" eq "questestinterop section item") {
 2814:             $id = $attr->{ident};
 2815:             push @{$allids}, $id;
 2816:             push(@{$$catinfo{$category}{contents}},$id);
 2817:             %{$$settings{$id}} = ();
 2818:             @{$$allchoices{$id}} = ();
 2819:             @{$$settings{$id}{grps}} = ();
 2820:             @{$$settings{$id}{lists}} = ();
 2821:             @{$$settings{$id}{feedback}} = ();
 2822:             @{$$settings{$id}{str}} = ();
 2823:             %{$$settings{$id}{strings}} = ();
 2824:             @{$$settings{$id}{numids}} = ();
 2825:             @{$$settings{$id}{boxes}} = ();
 2826:             %{$$allanswers{$id}} = ();
 2827:             $$settings{$id}{title} = $attr->{title};
 2828:             $$settings{$id}{category} = $category;
 2829:             $boxnum = 0;
 2830:         }
 2831: 
 2832:         if ("@state" eq "questestinterop section item presentation material mattext") {
 2833:             $$settings{$id}{texttype} = $attr->{texttype};
 2834:             $currtexttype = $attr->{texttype};
 2835:         }
 2836:         if ("@state" eq "questestinterop section item presentation material matimage") {
 2837:             $$settings{$id}{imagtype} = $attr->{imagtype};
 2838:             $currimagtype = $attr->{imagtype};
 2839:             $$settings{$id}{uri} = $attr->{uri};
 2840:         }
 2841: 
 2842: # Matching
 2843:         if ("@state" eq "questestinterop section item presentation response_grp") {
 2844:             $$settings{$id}{class} = 'match';
 2845:             $grp = $attr->{ident};
 2846:             push(@{$$settings{$id}{grps}},$grp);
 2847:             %{$$settings{$id}{$grp}} = ();
 2848:             @{$$settings{$id}{$grp}{correctanswer}} = ();
 2849:             $$settings{$id}{$grp}{rcardinality} = $attr->{rcardinality};
 2850:         }
 2851:         if ("@state" eq "questestinterop section item presentation response_grp material mattext") { 
 2852:             $$settings{$id}{$grp}{texttype} = $attr->{texttype};
 2853:             $currtexttype = $attr->{texttype};
 2854:         }
 2855:         if ("@state" eq "questestinterop section item presentation response_grp render_choice response_label") {
 2856:             $answer_id = $attr->{ident};
 2857:             push(@{$$allanswers{$id}{$grp}},$answer_id);
 2858:             %{$$settings{$id}{$grp}{$answer_id}} = ();
 2859:             $$settings{$id}{$grp}{$answer_id}{texttype} =  $attr->{texttype};
 2860:             $currtexttype = $attr->{texttype};
 2861:         }
 2862: 
 2863: # Multiple choice
 2864: 
 2865:         if ("@state" eq "questestinterop section item presentation flow material mattext") {
 2866:             $$settings{$id}{texttype} = $attr->{texttype};
 2867:             $currtexttype = $attr->{texttype};
 2868:         }
 2869:         if ("@state" eq "questestinterop section item presentation flow material matimage") {
 2870:             $$settings{$id}{imagtype} = $attr->{imagtype};
 2871:             $currimagtype = $attr->{imagtype};
 2872:             $$settings{$id}{uri} = $attr->{uri};
 2873: 
 2874:         }
 2875:         if ("@state" eq "questestinterop section item presentation flow response_lid") {
 2876:             $$settings{$id}{class} = 'multiplechoice';
 2877:             $list = $attr->{ident};
 2878:             push(@{$$settings{$id}{lists}},$list);
 2879:             %{$$settings{$id}{$list}} = ();
 2880:             @{$$allanswers{$id}{$list}} = ();
 2881:             @{$$settings{$id}{$list}{correctanswer}} = ();
 2882:             $$settings{$id}{$list}{rcardinality} = $attr->{rcardinality};
 2883:         }
 2884:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice") {
 2885:             $$settings{$id}{$list}{randomize} = $attr->{shuffle};
 2886:         }
 2887:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice flow_label response_label") {
 2888:             $answer_id = $attr->{ident};
 2889:             push(@{$$allanswers{$id}{$list}},$answer_id);
 2890:             %{$$settings{$id}{$list}{$answer_id}} = ();
 2891:         }
 2892:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice flow_label response_label material mattext") {
 2893:             $$settings{$id}{$list}{$answer_id}{texttype} = $attr->{texttype};
 2894:             $currtexttype = $attr->{texttype};
 2895:         }
 2896: 
 2897: # Numerical
 2898:         if ("@state" eq "questestinterop section item presentation material mat_extension webct:x_webct_v01_dynamicmattext") {
 2899:             $$settings{$id}{texttype} = $attr->{texttype};
 2900:             $currtexttype = $attr->{texttype};
 2901:         }
 2902:         if ("@state" eq "questestinterop section item presentation response_num") {
 2903:             $$settings{$id}{class} = 'numerical';
 2904:             $numid = $attr->{ident};
 2905:             push(@{$$settings{$id}{numids}},$numid);
 2906:             %{$$settings{$id}{$numid}} = ();
 2907:             %{$$settings{$id}{$numid}{vars}} = ();
 2908:             @{$$settings{$id}{$numid}{units}} = ();
 2909:             $$settings{$id}{$numid}{rcardinality} = $attr->{rcardinality};
 2910:         }
 2911:         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") {            
 2912:             $currvar = $attr->{name};
 2913:             %{$$settings{$id}{$numid}{vars}{$currvar}} = ();
 2914:         }
 2915:         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") {
 2916:             $currvar = $attr->{name};
 2917:         }
 2918:         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") {
 2919:             $currvar = $attr->{name};
 2920:         }
 2921:         if ("@state" eq "questestinterop section item presentation response_num render_fib") {
 2922:             $fibtype = $attr->{fibtype};
 2923:             $prompt = $attr->{prompt};
 2924:         }
 2925:         if ("@state" eq "questestinterop section item presentation response_num render_fib response_label") {
 2926:             $$settings{$id}{$numid}{label} = $attr->{ident};
 2927:         }
 2928: 
 2929: # String or Numerical
 2930:         if ("@state" eq "questestinterop section item presentation response_str") {
 2931:             $str_id = $attr->{ident};
 2932:             push(@{$$settings{$id}{str}},$str_id);
 2933:             @{$$settings{$id}{boxes}[$boxnum]} = ();
 2934:             $boxnum ++;
 2935:             %{$$settings{$id}{$str_id}} = ();
 2936:             @{$$settings{$id}{$str_id}{labels}} = ();
 2937:             $$settings{$id}{$str_id}{rcardinality} = $attr->{rcardinality};
 2938:         }
 2939: 
 2940:         if ("@state" eq "questestinterop section item presentation response_str render_fib") {
 2941:             $fibtype = $attr->{fibtype};
 2942:             $prompt = $attr->{prompt};
 2943:         }    
 2944:         if ("@state" eq "questestinterop section item presentation response_str render_fib response_label") {
 2945:             $label = $attr->{ident};
 2946:             push(@{$$settings{$id}{$str_id}{labels}},$label);
 2947:             @{$$settings{$id}{strings}{$label}} = ();
 2948:             %{$$settings{$id}{$str_id}{$label}} = ();
 2949:             $$settings{$id}{$str_id}{$label}{fibtype} = $fibtype;
 2950:         }
 2951: 
 2952: # Numerical
 2953:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_anspresentation") {
 2954:             $$settings{$id}{$numid}{digits} = $attr->{digits};
 2955:             $$settings{$id}{$numid}{format} = $attr->{format};
 2956:         }
 2957:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_anstolerance") {
 2958:             $$settings{$id}{$numid}{toltype} = $attr->{type};
 2959:         }
 2960:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_unit") {
 2961:             $unitid = $attr->{ident};
 2962:             %{$$settings{$id}{$numid}{$unitid}} = ();
 2963:             push(@{$$settings{$id}{$numid}{units}},$unitid);
 2964:             $$settings{$id}{$numid}{$unitid}{value} = $attr->{value}; 
 2965:             $$settings{$id}{$numid}{$unitid}{space} = $attr->{space};
 2966:             $$settings{$id}{$numid}{$unitid}{case} = $attr->{case};
 2967:         }
 2968: 
 2969: # Matching 
 2970:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varequal") {
 2971:             if ($$settings{$id}{class} eq 'match') {
 2972:                 unless ($attr->{respident} eq 'WebCT_Incorrect') {
 2973:                     $grp = $attr->{respident};
 2974:                 }
 2975: # String
 2976:             } else {
 2977:                 $label = $attr->{respident};
 2978:                 $$settings{$id}{$label}{case} = $attr->{case};   
 2979:             } 
 2980:         }
 2981:         if ("@state" eq "questestinterop section item resprocessing respcondition setvar") {
 2982:             $setvar{varname} = $attr->{varname};
 2983:             if ($setvar{varname} eq 'WebCT_Correct') {
 2984:                 push(@{$$settings{$id}{$grp}{correctanswer}},$answer_id);
 2985:             }
 2986:         }
 2987: 
 2988: # String
 2989:         if ("@state" eq "questestinterop section item resprocessing") {
 2990:             $boxnum = -1;
 2991:         }
 2992:         if ("@state" eq "questestinterop section item resprocessing respcondition") {            $boxnum ++;
 2993:         }
 2994:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varsubset") {
 2995:             $$settings{$id}{class} = 'string';
 2996:             $label = $attr->{respident};
 2997:         }
 2998:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar not") {
 2999:             $$settings{$id}{class} = 'paragraph';
 3000:         }
 3001:  
 3002: 
 3003: # Feedback
 3004:  
 3005:         if ("@state" eq "questestinterop section item respcondition displayfeedback") {
 3006:             $fdbk = $attr->{linkrefid};
 3007:             push(@{$$settings{$id}{feedback}},$fdbk);
 3008:             $$settings{$id}{$fdbk} = ();
 3009:             $$settings{$id}{$fdbk}{feedbacktype} = $attr->{feedbacktype};
 3010:         }
 3011:         if ("@state" eq "questestinterop section item itemfeedback") {
 3012:             $fdbk = $attr->{ident};
 3013:             $$settings{$id}{$fdbk}{view} = $attr->{view};
 3014:         }
 3015:         if ("@state" eq "questestinterop section item itemfeedback material mattext") {
 3016:             $$settings{$id}{$fdbk}{texttype} = $attr->{texttype};
 3017:             $currtexttype = $attr->{texttype};
 3018:         }
 3019:      }, "tagname, attr"],
 3020:      text_h =>
 3021:      [sub {
 3022:         my ($text) = @_;
 3023:         if ($currtexttype eq '/text/html') {
 3024:             $text =~ s#(&lt;img\ssrc=")([^"]+)"&gt;#$1../resfiles/$2#g;
 3025:         }
 3026:         if ("@state" eq "questestinterop section item itemmetadata qmd_itemtype") {
 3027:             $$settings{$id}{itemtype} = $text;
 3028:             if ($text eq 'String') {
 3029:                 $$settings{$id}{class} = 'string';
 3030:             }
 3031:         }
 3032: 
 3033:         if ("@state" eq "questestinterop section item presentation material mattext") {
 3034:             $$settings{$id}{text} = $text;
 3035:         }
 3036: # Matching
 3037:         if ("@state" eq "questestinterop section item presentation response_grp material mattext") {
 3038:             $$settings{$id}{$grp}{text} = $text;
 3039:             unless ($text eq '') {
 3040:                 push(@{$$allchoices{$id}},$grp);
 3041:             }
 3042:         }
 3043:         if ("@state" eq "questestinterop section item presentation response_grp render_choice response_label material mattext") {
 3044:             $$settings{$id}{$grp}{$answer_id}{text} = $text;
 3045:         }
 3046: 
 3047: # Multiple choice
 3048: 
 3049:         if ("@state" eq "questestinterop section item presentation flow material mattext") {
 3050:             $$settings{$id}{text} = $text;
 3051:         }
 3052: 
 3053:         if ("@state" eq "questestinterop section item presentation flow response_lid render_choice flow_label response_label material mattext") {
 3054:             $$settings{$id}{$list}{$answer_id}{text} = $text;
 3055:         }
 3056: 
 3057: # Numerical
 3058:         if ("@state" eq "questestinterop section item presentation material mat_extension webct:x_webct_v01_dynamicmattext") {
 3059:             $$settings{$id}{text} = $text;
 3060:         }
 3061:         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") {
 3062:              $$settings{$id}{$numid}{vars}{$currvar}{min} = $text;
 3063:         }
 3064:         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") {
 3065:              $$settings{$id}{$numid}{vars}{$currvar}{max} = $text;
 3066:         }
 3067:         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") {
 3068:              $$settings{$id}{$numid}{vars}{$currvar}{dec} = $text;
 3069:         }
 3070:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_formula") {
 3071:             $$settings{$id}{$numid}{formula} = $text;
 3072:         }
 3073:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varequal") {
 3074:             if ($$settings{$id}{class} eq 'string') {
 3075:                 unless (grep/^$text$/,@{$$settings{$id}{strings}{$label}}) {
 3076:                     push(@{$$settings{$id}{strings}{$label}},$text);
 3077:                 }
 3078:                 unless (grep/^$text$/,@{$$settings{$id}{boxes}[$boxnum]}) {
 3079:                     push(@{$$settings{$id}{boxes}[$boxnum]},$text);
 3080:                 }
 3081:             } else {
 3082:                 $answer_id = $text;
 3083:             }
 3084:         }
 3085:         if ("@state" eq "questestinterop section item resprocessing respcondition conditionvar varsubset") { # String
 3086:             unless (grep/^$text$/,@{$$settings{$id}{strings}{$label}}) {
 3087:                 push(@{$$settings{$id}{strings}{$label}},$text);
 3088:             }
 3089:             unless (grep/^$text$/,@{$$settings{$id}{boxes}[$boxnum]}) {
 3090:                 push(@{$$settings{$id}{boxes}[$boxnum]},$text);
 3091:             }
 3092:         }
 3093:         if ("@state" eq "questestinterop section item resprocessing respcondition setvar") {
 3094:             if ($setvar{varname} eq "answerValue") { # Multiple Choice WebCT4.0
 3095:                 if ($text =~ m/^\d+$/) {
 3096:                     if ($text > 0) {
 3097:                         push(@{$$settings{$id}{$list}{correctanswer}},$answer_id);   
 3098:                     }
 3099:                 }
 3100:             } elsif ($setvar{varname} eq "que_score") { # Multiple Choice WebCT4.1
 3101:                 if ($text =~ m/^\d+$/) {
 3102:                     if ($text > 0) {
 3103:                         push(@{$$settings{$id}{$list}{correctanswer}},$answer_id);
 3104:                     }
 3105:                 }
 3106:             }
 3107:         }
 3108:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_anstolerance") {
 3109:             $$settings{$id}{$numid}{tolerance} = $text;
 3110:         }
 3111:         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_unit") {
 3112:             $$settings{$id}{$numid}{$unitid}{text} = $text;
 3113:         }
 3114: 
 3115:         if ("@state" eq "questestinterop section item itemfeedback material mattext") {
 3116:             $$settings{$id}{$fdbk}{text} = $text;
 3117:         }
 3118:       }, "dtext"],
 3119:      end_h =>
 3120:      [sub {
 3121:         my ($tagname) = @_;
 3122:         pop @state;
 3123:      }, "tagname"],
 3124:     );
 3125:     $p->unbroken_text(1);
 3126:     $p->parse_file($xmlfile);
 3127:     $p->eof;
 3128:     my $boxcount;
 3129:     foreach my $id (keys %{$settings}) {
 3130:         if ($$settings{$id}{class} eq 'string') {
 3131:             $boxcount = 0;
 3132:             if (@{$$settings{$id}{boxes}} > 1) {
 3133:                 foreach my $str_id (@{$$settings{$id}{str}}) {
 3134:                     foreach my $label (@{$$settings{$id}{$str_id}{labels}}) {
 3135:                         @{$$settings{$id}{strings}{$label}} = @{$$settings{$id}{boxes}[$boxcount]};
 3136:                         $boxcount ++;
 3137:                     }
 3138:                 }
 3139:             }
 3140:         }
 3141:     }
 3142: }
 3143: 
 3144: sub process_assessment {
 3145:     my ($cms,$context,$res,$docroot,$container,$dirname,$destdir,$settings,$total,$udom,$uname,$pagesfiles,$sequencesfiles,$randompicks,$dbparse,$resources,$items,$catinfo,$qzdbsettings,$hrefs,$allquestions) = @_;
 3146:     my @allids = ();
 3147:     my @allquestids = ();
 3148:     my %allanswers = ();
 3149:     my %allchoices = ();
 3150:     my %qzparams = ();
 3151:     my %alldbanswers = ();
 3152:     my %alldbchoices = ();
 3153:     my @alldbquestids = ();
 3154:     my $containerdir;
 3155:     my $newdir;
 3156:     my $randompickflag = 0;
 3157:     my ($cid,$cdom,$cnum);
 3158:     if ($context eq 'DOCS') {
 3159:         $cid = $env{'request.course.id'};
 3160:         ($cdom,$cnum) = split/_/,$cid;
 3161:     }
 3162:     my $destresdir = $destdir;
 3163:     if ($context eq 'CSTR') {
 3164:         $destresdir =~ s|/home/$uname/public_html/|/res/$udom/$uname/|;
 3165:     } elsif ($context eq 'DOCS') {
 3166:         $destresdir =~ s|^/home/httpd/html/userfiles|/uploaded|;
 3167:     }
 3168:     if ($cms eq 'bb5') {
 3169:         &parse_bb5_assessment($res,$docroot,$container,$settings,\%allanswers,\%allchoices,\@allids);
 3170:     } elsif ($cms eq 'bb6') {
 3171:         &parse_bb6_assessment($res,$docroot,$container,$settings,\@allids);
 3172:     } elsif ($cms eq 'webctce4') {
 3173:         unless($$dbparse) {
 3174:             &parse_webct4_questionDB($docroot,$$resources{$res}{file},$catinfo,$qzdbsettings,\%alldbanswers,\%alldbchoices,\@alldbquestids);
 3175:             &build_category_sequences($destdir,$catinfo,$sequencesfiles,$pagesfiles,$destresdir,$newdir,$cms,$total,$randompickflag,$context,$udom,$uname,$dirname,$cid,$cdom,$cnum,$qzdbsettings);
 3176:             &write_webct4_questions($cms,\@alldbquestids,$context,$qzdbsettings,$dirname,\%alldbanswers,\%alldbchoices,$total,$cid,$cdom,$cnum,$destdir,$catinfo);
 3177:             $$dbparse = 1;
 3178:         }
 3179:         &parse_webct4_assessment($res,$docroot,$$resources{$res}{file},$container,\@allids);
 3180:         &parse_webct4_quizprops($res,$docroot,$$hrefs{$$items{$$resources{$res}{revitm}}{properties}}[0],$container,\%qzparams);
 3181:         if (exists($qzparams{$res}{numpick})) { 
 3182:             if ($qzparams{$res}{numpick} < @allids) {
 3183:                 $$randompicks{$$resources{$res}{revitm}} = $qzparams{$res}{numpick};
 3184:                 $randompickflag = 1;
 3185:             }
 3186:         }
 3187:     } elsif ($cms eq 'webctvista4') {
 3188:         unless($$dbparse) {
 3189:             foreach my $res (sort keys %{$allquestions}) {
 3190:                 my $parent = $$allquestions{$res};
 3191:                 &parse_webctvista4_question($res,$docroot,$resources,$hrefs,$qzdbsettings,\@allquestids,\%allanswers,\%allchoices,$parent,$catinfo);
 3192:             }
 3193:             &build_category_sequences($destdir,$catinfo,$sequencesfiles,$pagesfiles,$destresdir,$newdir,$cms,$total,$randompickflag,$context,$udom,$uname,$dirname,$cid,$cdom,$cnum,$qzdbsettings);
 3194:             $$dbparse = 1;
 3195:         }
 3196:         &parse_webctvista4_assessment($res,$docroot,$$resources{$res}{file},\@allids,\%qzparams);
 3197:         if ($qzparams{$res}{numpick} < @allids) {
 3198:             $$randompicks{$$resources{$res}{revitm}} = $qzparams{$res}{numpick};
 3199:             $randompickflag = 1;
 3200:         }
 3201:     }
 3202:     my $dirtitle;
 3203:     unless ($cms eq 'webctce4' || $cms eq 'webctvista4') {
 3204:         $dirtitle = $$settings{'title'};
 3205:         $dirtitle =~ s/\s+/_/g;
 3206:         $dirtitle =~ s/:/_/g;
 3207:         $dirtitle .= '_'.$res;
 3208:         if (!-e "$destdir/problems") {
 3209:             mkdir("$destdir/problems",0755);
 3210:         }
 3211:         if (!-e "$destdir/problems/$dirtitle") {
 3212:             mkdir("$destdir/problems/$dirtitle",0755);
 3213:         }
 3214:         $newdir = "$destdir/problems/$dirtitle";
 3215:     }
 3216: 
 3217:     if ($cms eq 'webctce4') {
 3218:         &build_problem_container($cms,$dirtitle,$destdir,$container,$res,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,\@allids,$udom,$uname,$dirname,\$containerdir,$cid,$cdom,$cnum,$catinfo,$qzdbsettings);
 3219:     } else {
 3220:         &build_problem_container($cms,$dirtitle,$destdir,$container,$res,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,\@allids,$udom,$uname,$dirname,\$containerdir,$cid,$cdom,$cnum,$catinfo,$qzdbsettings,\%qzparams);
 3221:     }
 3222:     if ($cms eq 'bb5') {
 3223:         &write_bb5_questions(\@allids,$containerdir,$context,$settings,$dirname,$destdir,$res,\%allanswers,\%allchoices,$total,$newdir,$cid,$cdom,$cnum,$docroot);
 3224:     } elsif ($cms eq 'bb6') {
 3225:         &write_bb6_questions(\@allids,$containerdir,$context,$settings,$dirname,$destdir,$res,$total,$newdir,$cid,$cdom,$cnum,$docroot);
 3226:     } elsif ($cms eq 'webctvista4') {
 3227:         &write_webct4_questions($cms,\@allquestids,$context,$qzdbsettings,$dirname,\%allanswers,\%allchoices,$total,$cid,$cdom,$cnum,$destdir,$catinfo,$dirtitle);
 3228:     }
 3229: }
 3230: 
 3231: sub build_category_sequences {
 3232:     my ($destdir,$catinfo,$sequencesfiles,$pagesfiles,$destresdir,$newdir,$cms,$total,$randompickflag,$context,$udom,$uname,$dirname,$cid,$cdom,$cnum,$qzdbsettings) = @_;
 3233:     if (!-e "$destdir/sequences") {
 3234:         mkdir("$destdir/sequences",0755);
 3235:     }
 3236:     my $numcats = scalar(keys %{$catinfo});
 3237:     my $curr_id = 0;
 3238:     my $next_id = 1;
 3239:     my $fh;
 3240:     open($fh,">$destdir/sequences/question_database.sequence");
 3241:     push @{$sequencesfiles},'question_database.sequence';
 3242:     foreach my $category (sort keys %{$catinfo}) {
 3243:         my $seqname;
 3244:         if ($cms eq 'webctce4') { 
 3245:             $seqname = $$catinfo{$category}{title}.'_'.$category;
 3246:         } else {
 3247:             $seqname = $$catinfo{$category}{title};
 3248:         }
 3249:         $seqname =~ s/\s+/_/g;
 3250:         $seqname =~ s/:/_/g;
 3251:         push(@{$sequencesfiles},$seqname.'.sequence');
 3252:         my $catsrc = "$destresdir/sequences/$seqname.sequence";
 3253:         if ($curr_id == 0) {
 3254:             print $fh qq|<resource id="1" src="$catsrc" type="start" title="$$catinfo{$category}{title}"></resource>|;
 3255:         }
 3256:         if ($numcats == 1) {
 3257:             print $fh qq|
 3258: <link from="1" to="2" index="1"></link>
 3259: <resource id="2" src="" type="finish">\n|;
 3260:         } else {
 3261:             $curr_id = $next_id;
 3262:             $next_id = $curr_id + 1;
 3263:             $catsrc = "$destresdir/sequences/$seqname.sequence";
 3264:             print $fh qq|
 3265: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
 3266: <resource id="$next_id" src="$catsrc" title="$$catinfo{$category}{title}"|;
 3267:             if ($next_id == $numcats) {
 3268:                 print $fh qq| type="finish"></resource>\n|;
 3269:             } else {
 3270:                 print $fh qq|></resource>\n|;
 3271:             }
 3272:         }
 3273:         print $fh qq|</map>|;
 3274:         if (!-e "$destdir/problems") {
 3275:             mkdir("$destdir/problems",0755);
 3276:         }
 3277:         if (!-e "$destdir/problems/$seqname") {
 3278:             mkdir("$destdir/problems/$seqname",0755);
 3279:         }
 3280:         $$newdir = "$destdir/problems/$seqname";
 3281:         my $dbcontainerdir;
 3282:         &build_problem_container($cms,$seqname,$destdir,'database',$seqname,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,\@{$$catinfo{$category}{contents}},$udom,$uname,$dirname,\$dbcontainerdir,$cid,$cdom,$cnum,$catinfo,$qzdbsettings);
 3283:     }
 3284:     close($fh);
 3285: }
 3286: 
 3287: sub build_problem_container {
 3288:     my ($cms,$dirtitle,$destdir,$container,$res,$total,$sequencesfiles,$pagesfiles,$randompickflag,$context,$allids,$udom,$uname,$dirname,$containerdir,$cid,$cdom,$cnum,$catinfo,$settings,$qzparams) = @_;
 3289:     my $seqdir = "$destdir/sequences";
 3290:     my $pagedir = "$destdir/pages";
 3291:     my $curr_id = 0;
 3292:     my $next_id = 1;
 3293:     my $fh;
 3294:     my $mapname = $res;
 3295:     if ($cms eq 'webctvista4' && ref($$qzparams{$res}) eq 'HASH') {
 3296:         if ($$qzparams{$res}{title}) {
 3297:             $mapname = $$qzparams{$res}{title};
 3298:             $mapname =~ s/\s+/_/g;
 3299:         }
 3300:     }
 3301:     if ($container eq 'pool' || $randompickflag || $container eq 'database') {
 3302:         $$containerdir = $seqdir.'/'.$mapname.'.sequence';
 3303:         if (!-e "$seqdir") {
 3304:             mkdir("$seqdir",0770);
 3305:         }
 3306:         open($fh,">$$containerdir");
 3307:         $$total{seq} ++;
 3308:         push @{$sequencesfiles},$mapname.'.sequence';
 3309:     } else {
 3310:         $$containerdir = $pagedir.'/'.$mapname.'.page';
 3311:         if (!-e "$destdir/pages") {
 3312:             mkdir("$destdir/pages",0770);
 3313:         }
 3314:         open($fh,">$$containerdir");
 3315:         $$total{page} ++;
 3316:         push @{$pagesfiles},$mapname.'.page';
 3317:     }
 3318:     print $fh qq|<map>
 3319: |;
 3320:     my %probtitle = ();
 3321:     my $probsrc = "/res/lib/templates/simpleproblem.problem";
 3322:     if ($context eq 'CSTR') {
 3323:         foreach my $id (@{$allids}) {
 3324:             if (($cms eq 'webctce4') || ($cms eq 'webctvista4')) {
 3325:                 $probtitle{$id} = $$settings{$id}{title};
 3326:             } else {
 3327:                 $probtitle{$id} = $$settings{title};
 3328:             }
 3329:             $probtitle{$id} =~ s/\s+/_/g;
 3330:             $probtitle{$id} =~ s/:/_/g;
 3331:             $probtitle{$id} .= '_'.$id;
 3332:         }
 3333:         if (($cms eq 'webctce4' && $container ne 'database') ||
 3334:             ($cms eq 'webctvista4'))   {
 3335:             my $probdir;
 3336:             my $catid = $$settings{$$allids[0]}{category};
 3337:             if ($catid) {
 3338:                 if ($cms eq 'webctce4') { 
 3339:                     $probdir = $$catinfo{$catid}{title}.'_'.$catid;
 3340:                 } else {
 3341:                     $probdir = $$catinfo{$catid}{title};
 3342:                 }
 3343:                 $probdir =~ s/\s+/_/g;
 3344:                 $probdir =~ s/:/_/g;
 3345:                 $probsrc = "$dirname/problems/$probdir/$probtitle{$$allids[0]}.problem";
 3346:             } else {
 3347:                 $probsrc="$dirname/problems/$dirtitle/$probtitle{$$allids[0]}.problem";
 3348:             }
 3349:         } else {
 3350:             $probsrc="$dirname/problems/$dirtitle/$probtitle{$$allids[0]}.problem";
 3351:         }
 3352:     }
 3353:     print $fh qq|<resource id="1" src="$probsrc" type="start" title="question_0001"></resource>|;
 3354:     if (@{$allids} == 1) {
 3355:         print $fh qq|
 3356: <link from="1" to="2" index="1"></link>
 3357: <resource id="2" src="" type="finish">\n|;
 3358:     } else {
 3359:         for (my $j=1; $j<@{$allids}; $j++) {
 3360:             my $qntitle = $j+1;
 3361:             while (length($qntitle) <4) {
 3362:                 $qntitle = '0'.$qntitle;
 3363:             }
 3364:             $curr_id = $j;
 3365:             $next_id = $curr_id + 1;
 3366:             if ($context eq 'CSTR') {
 3367:                 if (($cms eq 'webctce4' && $container ne 'database') ||
 3368:                     ($cms eq 'webctvista4')) {
 3369:                     my $probdir;
 3370:                     my $catid = $$settings{$$allids[$j]}{category};
 3371:                     if ($catid) {
 3372:                         if ($cms eq 'webctce4') {
 3373:                             $probdir = $$catinfo{$catid}{title}.'_'.$catid;
 3374:                         } else {
 3375:                             $probdir = $$catinfo{$catid}{title};
 3376:                         }
 3377:                         $probdir =~ s/\s/_/g;
 3378:                         $probdir =~ s/:/_/g;
 3379:                         $probsrc = "$dirname/problems/$probdir/$probtitle{$$allids[$j]}.problem";
 3380:                     } else {
 3381:                         $probsrc = "$dirname/problems/$dirtitle/$probtitle{$$allids[$j]}.problem";
 3382:                     }
 3383:                 } else {
 3384:                     $probsrc = "$dirname/problems/$dirtitle/$probtitle{$$allids[$j]}.problem";
 3385:                 }
 3386:             }
 3387:             print $fh qq|
 3388: <link from="$curr_id" to="$next_id" index="$curr_id"></link>
 3389: <resource id="$next_id" src="$probsrc" title="question_$qntitle"|;
 3390:             if ($next_id == @{$allids}) {
 3391:                 print $fh qq| type="finish"></resource>\n|;
 3392:             } else {
 3393:                 print $fh qq|></resource>|;
 3394:             }
 3395:         }
 3396:     }
 3397:     print $fh qq|</map>|;
 3398:     close($fh);
 3399: }
 3400: 
 3401: sub write_bb5_questions {
 3402:     my ($allids,$containerdir,$context,$settings,$dirname,$destdir,$res,$allanswers,$allchoices,$total,$newdir,$cid,$cdom,$cnum,$docroot) = @_;
 3403:     my $qnum = 0;
 3404:     my $pathstart;
 3405:     if ($context eq 'CSTR') {
 3406:         $pathstart = '../..';
 3407:     } else {
 3408:         $pathstart = $dirname;
 3409:     }
 3410:     foreach my $id (@{$allids}) {
 3411:         if ($$settings{$id}{ishtml} eq 'true') {
 3412:             $$settings{$id}{text} = &HTML::Entities::decode($$settings{$id}{text});
 3413:         }
 3414:         if ($$settings{$id}{text} =~ m#<img src=['"]?(https?://[^\s]+/)([^/\s\'"]+)['"]?[^>]*>#) {
 3415:             if (&retrieve_image($context,$res,$dirname,$cdom,$cnum,$docroot,$destdir,$1,$2) eq 'ok') {
 3416:                 $$settings{$id}{text} =~ s#(<img src=['"]?)(https?://[^\s]+/)([^/\s'"]+)(['"]?[^>]*>)#$1$pathstart/resfiles/$res/webimages/$3$4#g;
 3417:             }
 3418:         }
 3419:         $$settings{$id}{text} =~ s#(<img src=[^>]+)/*>#$1 />#gi;
 3420:         $$settings{$id}{text} =~ s#<br>#<br />#g;
 3421:         $qnum ++;
 3422:         my $output;
 3423:         my $permcontainer = $containerdir;
 3424:         $permcontainer =~ s#/home/httpd/html/userfiles#uploaded#;
 3425:         my $symb = $cid.'.'.$permcontainer.'___'.$qnum.'___lib/templates/simpleproblem.problem.0.';
 3426:         my %resourcedata = ();
 3427:         for (my $i=0; $i<10; $i++) {
 3428:             my $iter = $i+1;
 3429:             $resourcedata{$symb.'text'.$iter} = "";
 3430:             $resourcedata{$symb.'value'.$iter} = "unused";
 3431:             $resourcedata{$symb.'position'.$iter} = "random";
 3432:         }
 3433:         $resourcedata{$symb.'randomize'} = 'yes';
 3434:         $resourcedata{$symb.'maxfoils'} = 10;
 3435:         if ($context eq 'CSTR') {
 3436:             $output = qq|<problem>
 3437: |;
 3438:         }
 3439:         $$total{prob} ++;
 3440:         if ($$settings{$id}{class} eq "QUESTION_ESSAY") {
 3441:             if ($context eq 'CSTR') {
 3442:                 $output .= qq|<startouttext />$$settings{$id}{text}<endouttext />
 3443:  <essayresponse>
 3444:  <textfield></textfield>
 3445:  </essayresponse>
 3446:  <postanswerdate>
 3447:   $$settings{$id}{feedbackcorr} 
 3448:  </postanswerdate>
 3449: |;
 3450:              } else {
 3451: 		 $resourcedata{$symb.'questiontext'} = $$settings{$id}{text};
 3452:                  $resourcedata{$symb.'hiddenparts'} = '!essay';
 3453:                  $resourcedata{$symb.'questiontype'} = 'essay';
 3454:              }
 3455:         } else {
 3456:             if ($context eq 'CSTR') {
 3457:                 $output .= qq|<startouttext />$$settings{$id}{text}\n|;
 3458:             } else {
 3459:                 $resourcedata{$symb.'questiontext'} = $$settings{$id}{text};
 3460:             }
 3461:             my ($image,$imglink,$url);
 3462:             if ( defined($$settings{$id}{image}) ) {
 3463:                 if ( $$settings{$id}{style} eq 'embed' ) {
 3464:                     $image = qq|<br /><img src="$pathstart/resfiles/$res/$$settings{$id}{image}" /><br />|;
 3465:                 } else {
 3466:                     $imglink = qq|<br /><a href="$pathstart/resfiles/$res/$$settings{$id}{image}">Link to file</a><br />|;
 3467:                 }
 3468:             }
 3469:             if ( defined($$settings{$id}{url}) ) {
 3470:                 $url = qq|<br /><a href="$$settings{$id}{url}">$$settings{$id}{name}</a><br />|;
 3471:             }
 3472:             if ($context eq 'CSTR') {
 3473:                 $output .= $image.$imglink.$url.'
 3474: <endouttext />';
 3475:             } else {
 3476:                 $resourcedata{$symb.'questiontext'} .= $image.$imglink.$url;
 3477:             }
 3478:             if ($$settings{$id}{class} eq 'QUESTION_MULTIPLECHOICE') {
 3479:                 my $numfoils = @{$$allanswers{$id}};
 3480:                 if ($context eq 'CSTR') {
 3481:                     $output .= qq|
 3482:  <radiobuttonresponse max="$numfoils" randomize="yes">
 3483:   <foilgroup>
 3484: |;
 3485:                 } else {
 3486:                     $resourcedata{$symb.'hiddenparts'} = '!radio';
 3487:                     $resourcedata{$symb.'questiontype'} = 'radio';
 3488:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 3489:                 }
 3490:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 3491:                     my $iter = $k+1;
 3492:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 3493:                     if (grep/^$$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 3494:                         $output .= "true\" location=\"";
 3495:                         $resourcedata{$symb.'value'.$iter} = "true";
 3496:                     } else {
 3497:                         $output .= "false\" location=\"";
 3498:                         $resourcedata{$symb.'value'.$iter} = "false";
 3499:                     }
 3500:                     if (lc ($$allanswers{$id}[$k]) =~ m/^\s?([Aa]ll)|([Nn]one)\s(of\s)?the\sabove\.?/) {
 3501:                         $output .= "bottom\"";
 3502:                         $resourcedata{$symb.'position'.$iter} = "bottom";
 3503:                     } else {
 3504:                         $output .= "random\"";
 3505:                     }
 3506:                     $output .= "\><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text};
 3507:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 3508:                     my ($ans_image,$ans_link);
 3509:                     if ( defined($$settings{$id}{$$allanswers{$id}[$k]}{image}) ) {
 3510:                         if ( $$settings{$id}{$$allanswers{$id}[$k]}{style} eq 'embed' ) {
 3511:                             $ans_image .= qq|<br /><img src="$pathstart/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" /><br />|;
 3512:                         } else {
 3513:                             $ans_link .= qq|<br /><a href="$pathstart/resfiles/$res/$$settings{$id}{$$allanswers{$id}[$k]}{image}" />Link to file</a><br/>|;
 3514:                         }
 3515:                     }
 3516:                     $output .= $ans_image.$ans_link.'<endouttext /></foil>'."\n";
 3517:                     $resourcedata{$symb.'text'.$iter} .= $ans_image.$ans_link;
 3518:                 }
 3519:                 if ($context eq 'CSTR') {
 3520:                     chomp($output);
 3521:                     $output .= qq|
 3522:   </foilgroup>
 3523:  </radiobuttonresponse>
 3524: |;
 3525:                 }
 3526:             } elsif ($$settings{$id}{class} eq 'QUESTION_TRUEFALSE') {
 3527:                 my $numfoils = @{$$allanswers{$id}};
 3528:                 if ($context eq 'CSTR') {
 3529:                     $output .= qq|
 3530:    <radiobuttonresponse max="$numfoils" randomize="yes">
 3531:     <foilgroup>
 3532: |;
 3533:                 } else {
 3534:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 3535:                     $resourcedata{$symb.'hiddenparts'} = '!radio';
 3536:                     $resourcedata{$symb.'questiontype'} = 'radio';
 3537:                 }
 3538:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 3539:                     my $iter = $k+1;
 3540:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 3541:                     if (grep/^$$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 3542:                         $output .= "true\" location=\"random\"";
 3543:                         $resourcedata{$symb.'value'.$iter} = "true";
 3544:                     } else {
 3545:                         $output .= "false\" location=\"random\"";
 3546:                         $resourcedata{$symb.'value'.$iter} = "false";
 3547:                     }
 3548:                     $output .= "\><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 3549:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 3550:                 }
 3551:                 if ($context eq 'CSTR') {
 3552:                     chomp($output);
 3553:                     $output .= qq|
 3554:     </foilgroup>
 3555:    </radiobuttonresponse>
 3556: |;
 3557:                 }
 3558:             } elsif ($$settings{$id}{class} eq 'QUESTION_MULTIPLEANSWER') {
 3559:                 my $numfoils = @{$$allanswers{$id}};
 3560:                 if ($context eq 'CSTR') {
 3561:                     $output .= qq|
 3562:    <optionresponse max="$numfoils" randomize="yes">
 3563:     <foilgroup options="('True','False')">
 3564: |;
 3565:                 } else {
 3566:                     $resourcedata{$symb.'newopt'} = '';
 3567:                     $resourcedata{$symb.'delopt'} = '';
 3568:                     $resourcedata{$symb.'options'} = "('True','False')";
 3569:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 3570:                     $resourcedata{$symb.'questiontype'} = 'option';
 3571:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 3572:                 }
 3573:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 3574:                     my $iter = $k+1;
 3575:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 3576:                     if (grep/^$$allanswers{$id}[$k]$/,@{$$settings{$id}{correctanswer}}) {
 3577:                         $output .= "True\"";
 3578:                         $resourcedata{$symb.'value'.$iter} = "True";
 3579:                     } else {
 3580:                         $output .= "False\"";
 3581:                         $resourcedata{$symb.'value'.$iter} = "False";
 3582:                     }
 3583:                     $output .= "\><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 3584:                     $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 3585:                 }
 3586:                 if ($context eq 'CSTR') {  
 3587:                     chomp($output);
 3588:                     $output .= qq|
 3589:     </foilgroup>
 3590:    </optionresponse>
 3591: |;
 3592:                 }
 3593:             } elsif ($$settings{$id}{class} eq 'QUESTION_ORDER') {
 3594:                 my $numfoils = @{$$allanswers{$id}};
 3595:                 my @allorder = ();
 3596:                 if ($context eq 'CSTR') {
 3597:                     $output .= qq|
 3598:    <rankresponse max="$numfoils" randomize="yes">
 3599:     <foilgroup>
 3600: |;
 3601:                 } else {
 3602:                     $resourcedata{$symb.'newopt'} = '';
 3603:                     $resourcedata{$symb.'delopt'} = '';
 3604:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 3605:                     $resourcedata{$symb.'questiontype'} = 'option';
 3606:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 3607:                 }
 3608:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 3609:                     if ($context eq 'CSTR') {
 3610:                         $output .= "   <foil location=\"random\" name=\"foil".$k."\" value=\"".$$settings{$id}{$$allanswers{$id}[$k]}{order}."\"><startouttext />".$$settings{$id}{$$allanswers{$id}[$k]}{text}."<endouttext /></foil>\n";
 3611:                     } else {
 3612:                         my $iter = $k+1;
 3613:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 3614:                         if (!grep/^$$settings{$id}{$$allanswers{$id}[$k]}{order}$/,@allorder) {
 3615:                             push @allorder, $$settings{$id}{$$allanswers{$id}[$k]}{order};
 3616:                         }
 3617:                     }
 3618:                 }
 3619:                 if ($context eq 'CSTR') {
 3620:                     chomp($output);
 3621:                     $output .= qq|
 3622:     </foilgroup>
 3623:    </rankresponse>
 3624: |;
 3625:                 } else {
 3626:                     @allorder = sort {$a <=> $b} @allorder;
 3627:                     $resourcedata{$symb.'options'} = "('".join("','",@allorder)."')";
 3628:                 }
 3629:             } elsif ($$settings{$id}{class} eq 'QUESTION_FILLINBLANK') {
 3630:                 my $numerical = 1;
 3631:                 if ($context eq 'DOCS') {
 3632:                     $numerical = 0;
 3633:                 } else {
 3634:                     for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 3635:                         if ($$settings{$id}{$$allanswers{$id}[$k]}{text} =~ m/([^\d\.]|\.\.)/) {
 3636:                             $numerical = 0;
 3637:                         }
 3638:                     }
 3639:                 }
 3640:                 if ($numerical) {
 3641:                     my $numans;
 3642:                     my $tol;
 3643:                     if (@{$$allanswers{$id}} == 1) {
 3644:                         $tol = 5;
 3645:                         $numans = $$settings{$id}{$$allanswers{$id}[0]}{text};
 3646:                     } else {
 3647:                         my $min = $$settings{$id}{$$allanswers{$id}[0]}{text};
 3648:                         my $max = $$settings{$id}{$$allanswers{$id}[0]}{text};
 3649:                         for (my $k=1; $k<@{$$allanswers{$id}}; $k++) {
 3650:                             if ($$settings{$id}{$$allanswers{$id}[$k]}{text} <= $min) {
 3651:                                 $min = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 3652:                             }
 3653:                             if ($$settings{$id}{$$allanswers{$id}[$k]}{text} >= $max) {
 3654:                                 $max = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 3655:                             }
 3656:                         }
 3657:                         $numans = ($max + $min)/2;
 3658:                         $tol = 100*($max - $min)/($numans*2);
 3659:                     }
 3660:                     if ($context eq 'CSTR') {
 3661:                         $output .= qq|
 3662: <numericalresponse answer="$numans">
 3663:         <responseparam type="tolerance" default="$tol%" name="tol" description="Numerical Tolerance" />
 3664:         <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures"
 3665: />
 3666:         <textline />
 3667: </numericalresponse>
 3668: |;
 3669:                     }
 3670:                 } else {
 3671:                     if ($context eq 'DOCS') {
 3672:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 3673:                         $resourcedata{$symb.'questiontype'} = 'string';
 3674:                         $resourcedata{$symb.'maxfoils'} = @{$$allanswers{$id}};
 3675:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 3676:                         $resourcedata{$symb.'stringtype'} = 'ci';
 3677:                         $resourcedata{$symb.'stringanswer'} = $$settings{$id}{$$allanswers{$id}[0]}{text};
 3678:                     } else {
 3679:                         if (@{$$allanswers{$id}} == 1) {
 3680:                             $output .= qq|
 3681: <stringresponse answer="$$settings{$id}{$$allanswers{$id}[0]}{text}" type="ci">
 3682: <textline>
 3683: </textline>
 3684: </stringresponse>
 3685: |;
 3686:                         } else {
 3687:                             my @answertext = ();
 3688:                             for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 3689:                                 $$settings{$id}{$$allanswers{$id}[$k]}{text} =~ s/\|/\|/g;
 3690:                                 push @answertext, $$settings{$id}{$$allanswers{$id}[$k]}{text};
 3691:                             }
 3692:                             my $regexpans = join('|',@answertext);
 3693:                             $regexpans = '/^('.$regexpans.')\b/';
 3694:                             $output .= qq|
 3695: <stringresponse answer="$regexpans" type="re">
 3696: <textline>
 3697: </textline>
 3698: </stringresponse>
 3699: |;
 3700:                         }
 3701:                     } 
 3702:                 }
 3703:             } elsif ($$settings{$id}{class} eq "QUESTION_MATCH") {
 3704:                 my @allmatchers = ();
 3705:                 my %matchtext = ();
 3706:                 if ($context eq 'CSTR') {
 3707:                     $output .= qq|
 3708: <matchresponse max="10" randomize="yes">
 3709:     <foilgroup>
 3710:         <itemgroup>
 3711: |;
 3712:                 } else {
 3713:                     $resourcedata{$symb.'newopt'} = '';
 3714:                     $resourcedata{$symb.'delopt'} = '';
 3715:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 3716:                     $resourcedata{$symb.'questiontype'} = 'option';
 3717:                     $resourcedata{$symb.'maxfoils'} =  @{$$allanswers{$id}};
 3718:                 }
 3719:                 for (my $k=0; $k<@{$$allchoices{$id}}; $k++) {
 3720:                     if ($context eq 'CSTR') {
 3721:                         $output .= qq|
 3722: <item name="$$allchoices{$id}[$k]">
 3723: <startouttext />$$settings{$id}{$$allchoices{$id}[$k]}{text}<endouttext />
 3724: </item>
 3725:                     |;
 3726:                     } else {
 3727:                         if (!grep/^$$settings{$id}{$$allchoices{$id}[$k]}{text}$/,@allmatchers) {
 3728:                             push @allmatchers, $$settings{$id}{$$allchoices{$id}[$k]}{text};
 3729:                             $matchtext{$$allchoices{$id}[$k]} = $$settings{$id}{$$allchoices{$id}[$k]}{text};
 3730:                         }
 3731:                     }
 3732:                 }
 3733:                 if ($context eq 'CSTR') {
 3734:                     $output .= qq|
 3735:         </itemgroup>
 3736: |;
 3737:                 }
 3738:                 for (my $k=0; $k<@{$$allanswers{$id}}; $k++) {
 3739:                     if ($context eq 'CSTR') {
 3740:                         $output .= qq|
 3741:         <foil location="random" value="$$settings{$id}{$$allanswers{$id}[$k]}{choice_id}" name="$$allanswers{$id}[$k]">
 3742:          <startouttext />$$settings{$id}{$$allanswers{$id}[$k]}{text}<endouttext />
 3743:         </foil>
 3744: |;
 3745:                     } else {
 3746:                         my $iter = $k+1;
 3747:                         $resourcedata{$symb.'value'.$iter} = $matchtext{$$settings{$id}{$$allanswers{$id}[$k]}{choice_id}};
 3748:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allanswers{$id}[$k]}{text};
 3749:                     }
 3750:                 }
 3751:                 if ($context eq 'CSTR') {
 3752:                     $output .= qq|
 3753:     </foilgroup>
 3754: </matchresponse>
 3755: |;
 3756:                 } else {
 3757:                     $resourcedata{$symb.'options'} = "('".join("','",@allmatchers)."')";
 3758:                 }
 3759:             }
 3760:         }
 3761:         if ($context eq 'CSTR') {
 3762:             $output .= qq|</problem>
 3763: |;
 3764:             my $title = $$settings{title};
 3765:             $title =~ s/\s/_/g;
 3766:             $title =~ s/\W//g;
 3767:             $title .= '_'.$id;
 3768:             open(PROB,">$newdir/$title.problem");
 3769:             print PROB $output;
 3770:             close PROB;
 3771:         } else {
 3772: # put %resourcedata;
 3773:             my $reply=&Apache::lonnet::cput
 3774:                 ('resourcedata',\%resourcedata,$cdom,$cnum);
 3775:         }
 3776:     }
 3777: }
 3778: 
 3779: sub write_webct4_questions {
 3780:     my ($cms,$alldbquestids,$context,$settings,$dirname,$allanswers,$allchoices,$total,$cid,$cdom,$cnum,$destdir,$catinfo,$dirtitle) = @_;
 3781:     my $qnum = 0;
 3782:     foreach my $id (@{$alldbquestids}) {
 3783:         $qnum ++;
 3784:         my $output;
 3785:         my $permcontainer = $destdir.'/sequences/'.$id.'.sequence';
 3786:         my $allfeedback;
 3787:         my $questionimage;
 3788:         foreach my $fdbk (@{$$settings{$id}{feedback}}) {
 3789:             my $feedback =  $$settings{$id}{$fdbk}{text};
 3790:             if ($$settings{$id}{$fdbk}{texttype} eq 'text/html') {
 3791:                 $feedback = &HTML::Entities::decode($feedback);
 3792:             }
 3793:             $allfeedback .= $feedback;
 3794:         }
 3795:         if ($$settings{$id}{texttype} eq 'text/html') {
 3796:             if ($$settings{$id}{text}) {
 3797:                 $$settings{$id}{text} = &text_cleanup($$settings{$id}{text});
 3798:             }
 3799:         } 
 3800:         if ($$settings{$id}{class} eq 'numerical') {
 3801:             foreach my $numid (@{$$settings{$id}{numids}}) {
 3802:                 foreach my $var (keys %{$$settings{$id}{$numid}{vars}}) {
 3803:                     if ($cms eq 'webctce4') {
 3804:                         $$settings{$id}{text} =~ s/{($var)}/\$$1 /g;
 3805:                     } elsif ($cms eq 'webctvista4') {
 3806:                         $$settings{$id}{text} =~ s/\[($var)\]/\$$1 /g;
 3807:                     }
 3808:                 }
 3809:             }
 3810:         }
 3811:         $permcontainer =~ s#/home/httpd/html/userfiles#uploaded#;
 3812:         my $symb = $cid.'.'.$permcontainer.'___'.$qnum.'___lib/templates/simpleproblem.problem.0.';
 3813:         my %resourcedata = ();
 3814:         for (my $i=0; $i<10; $i++) {
 3815:             my $iter = $i+1;
 3816:             $resourcedata{$symb.'text'.$iter} = "";
 3817:             $resourcedata{$symb.'value'.$iter} = "unused";
 3818:             $resourcedata{$symb.'position'.$iter} = "random";
 3819:         }
 3820:         $resourcedata{$symb.'randomize'} = 'yes';
 3821:         $resourcedata{$symb.'maxfoils'} = 10;
 3822:         if ($context eq 'CSTR') {
 3823:             unless ($$settings{$id}{class} eq 'numerical') {
 3824:                 $output = qq|<problem>
 3825: |;
 3826:             }
 3827:         }
 3828:         $$total{prob} ++;
 3829: 
 3830:         if (exists($$settings{$id}{uri})) {
 3831:             if ($cms eq 'webctce4') {
 3832:                 if ($$settings{$id}{imagtype} =~ /^image\//) {
 3833:                     $questionimage = '<p><img src="../../resfiles/'.$$settings{$id}{uri}.'" /></p>'."\n";
 3834:                 } else {
 3835:                     $questionimage = '<p><img src="../../resfiles/'.$$settings{$id}{uri}.'" /></p>'."\n";
 3836:                 }
 3837:             } elsif ($cms eq 'webctvista4') {
 3838:                 if ($$settings{$id}{uri} =~ /(gif|jpg|png)$/i) {
 3839:                     $questionimage = '<p><img src="../../resfiles/'.$$settings{$id}{uri}.'" /></p>'."\n";
 3840:                     $questionimage =~ s#(//+)#/#g;
 3841:                 } else {
 3842:                     $questionimage = '<a href="'.$$settings{$id}{uri}.'" target="exturi" >'.$$settings{$id}{uri}.'</a>';
 3843:                 }
 3844:             }
 3845:         }
 3846:         if ($$settings{$id}{class} eq "paragraph") {
 3847:             my $pre_fill_answer = $$settings{$id}{PARA}{PARA}{PRE_FILL_ANSWER};
 3848:             if ($context eq 'CSTR') {
 3849:                 $output .= qq|<startouttext /><p>$$settings{$id}{text}</p>$questionimage<endouttext />
 3850:  <essayresponse>
 3851:  <textfield>$pre_fill_answer</textfield>
 3852:  </essayresponse>
 3853:  <postanswerdate>
 3854:   $allfeedback
 3855:  </postanswerdate>
 3856: |;
 3857:             } else {
 3858:                 $resourcedata{$symb.'questiontext'} = '<p>'.$$settings{$id}{text}.'</p>'.$questionimage;
 3859:                 $resourcedata{$symb.'hiddenparts'} = '!essay';
 3860:                 $resourcedata{$symb.'questiontype'} = 'essay';
 3861:             }
 3862:         } elsif ($$settings{$id}{class} eq 'jumbled') {
 3863:             if ($context eq 'CSTR') {
 3864:                 my %foiloptions = ();
 3865:                 foreach my $list (@{$$settings{$id}{lists}}) {
 3866:                     @{$foiloptions{$list}} = ();
 3867:                     my $numalternates = @{$$settings{$id}{$list}{jumbled}} - 1;
 3868:                     my $loopstop = 2; #Hard coded for now, so only one permutation of answers is correct; <or> functionality is needed to support the case where multiple permutations are correct.  
 3869:                     for (my $i=1; $i<$loopstop; $i++) {  
 3870:                         $foiloptions{$list}[$i]  = '(';
 3871:                         for (my $j=@{$$settings{$id}{$list}{jumbled}[$i]}-1; $j>0; $j--) {
 3872:                             my $jumble_item = $$settings{$id}{$list}{jumbled}[$i][$j];
 3873:                             $foiloptions{$list}[$i] .= "'".$$settings{$id}{$list}{$jumble_item}{text}."',";
 3874:                         }
 3875:                         $foiloptions{$list}[$i] =~ s/,$//;
 3876:                         $foiloptions{$list}[$i] .= ')';
 3877:                         my $jnum = 0; 
 3878:                         for (my $k=0; $k<@{$$settings{$id}{$list}{jumbledtype}}; $k++) {
 3879:                             if ($$settings{$id}{$list}{jumbledtype}[$k] eq 'No') {
 3880:                                 $output .= qq|
 3881: <startouttext />
 3882: $$settings{$id}{$list}{jumbledtext}[$k]
 3883: <endouttext />|;
 3884:                             } elsif ($$settings{$id}{$list}{jumbledtype}[$k] eq 'Yes') {
 3885:                                 $jnum ++;
 3886:                                 my $jumble_item = $$settings{$id}{$list}{jumbled}[$i][$jnum];
 3887:                                 $output .= qq|
 3888: <optionresponse max="1" randomize="yes" TeXlayout="horizontal">
 3889:     <foilgroup options="$foiloptions{$list}[$i]">
 3890:         <foil location="random" value="$$settings{$id}{$list}{$jumble_item}{text}" name="$jumble_item"></foil>
 3891:     </foilgroup>
 3892: </optionresponse>
 3893: |;
 3894:                             }
 3895:                         }
 3896:                     }
 3897:                     if ($numalternates > 0) { # for now alternates are stored in an instructorcomment.  In the future these alternates could be moved into the main response area once <or> functionality is available.
 3898:                         $output .= '<instructorcomment>(Not shown to students) '."\n".'The following alternates were imported from the corresponding WebCT Vista 4 jumbled sentence question, but are not included in the LON-CAPA version, because this style of question does not currently support multiple correct solutions.'."\n";
 3899:                         for (my $i=2; $i<@{$$settings{$id}{$list}{jumbled}}; $i++) {
 3900:                             my $altid = $i-1;
 3901:                             my $jnum = 0;
 3902:                             $output .= $altid.'. '; 
 3903:                             for (my $k=0; $k<@{$$settings{$id}{$list}{jumbledtype}}; $k++) {
 3904:                                 if ($$settings{$id}{$list}{jumbledtype}[$k] eq 'No') {
 3905:                                     $output .= "$$settings{$id}{$list}{jumbledtext}[$k]" ;
 3906:                                 } elsif ($$settings{$id}{$list}{jumbledtype}[$k] eq 'Yes') {
 3907:                                     $jnum ++;
 3908:                                     my $jumble_item = $$settings{$id}{$list}{jumbled}[$i][$jnum];
 3909:                                     $output .= '['.$$settings{$id}{$list}{$jumble_item}{text}.']';
 3910:                                 }
 3911:                             }
 3912:                             $output .= " \n";
 3913:                         }
 3914:                         $output .= '</instructorcomment>';
 3915:                     }  
 3916:                 }
 3917:             }
 3918:         } else {
 3919:             if ($context eq 'CSTR') {
 3920:                 $output .= qq|<startouttext /><p>$$settings{$id}{text}</p>$questionimage<endouttext />\n|;
 3921:             } else {
 3922:                 $resourcedata{$symb.'questiontext'} = '<p>'.$$settings{$id}{text}.'</p>'.$questionimage;
 3923:             }
 3924:             if (($$settings{$id}{class} eq 'multiplechoice') || 
 3925:                 ($$settings{$id}{class} eq 'combination')) {
 3926:                 foreach my $list (@{$$settings{$id}{lists}}) {
 3927:                     my $numfoils = @{$$allanswers{$id}{$list}};
 3928:                     if ($$settings{$id}{$list}{rcardinality} eq 'Single') {
 3929:                         if ($context eq 'CSTR') {
 3930:                             $output .= qq|
 3931:  <radiobuttonresponse max="$numfoils" randomize="$$settings{$id}{$list}{randomize}">
 3932:   <foilgroup>
 3933: |;
 3934:                         } else {
 3935:                             $resourcedata{$symb.'hiddenparts'} = '!radio';
 3936:                             $resourcedata{$symb.'questiontype'} = 'radio';
 3937:                             $resourcedata{$symb.'maxfoils'} = $numfoils;
 3938:                         }
 3939:                         for (my $k=0; $k<@{$$allanswers{$id}{$list}}; $k++) {
 3940:                             my $iter = $k+1;
 3941:                             $output .= "   <foil name=\"foil".$k."\" value=\"";
 3942:                             if (grep/^$$allanswers{$id}{$list}[$k]$/,@{$$settings{$id}{$list}{correctanswer}}) {
 3943:                                 $output .= "true\" location=\"";
 3944:                                 $resourcedata{$symb.'value'.$iter} = "true";
 3945:                             } else {
 3946:                                 $output .= "false\" location=\"";
 3947:                                 $resourcedata{$symb.'value'.$iter} = "false";
 3948:                             }
 3949:                             if (lc ($$allanswers{$id}{$list}[$k]) =~ m/^\s?([Aa]ll)|([Nn]one)\s(of\s)?the\sabove\.?/) {
 3950:                                 $output .= "bottom\"";
 3951:                                 $resourcedata{$symb.'position'.$iter} = "bottom";
 3952:                             } else {
 3953:                                 $output .= "random\"";
 3954:                             }
 3955:                             if ($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{texttype} eq 'text/html') {
 3956:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} = &HTML::Entities::decode($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text});
 3957:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} = &Apache::loncleanup::htmlclean($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text});
 3958:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} =~  s#(<img src=")([^>]+)>#$1../../resfiles/$2 />#gi;
 3959:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} =~  s#</?p>##g;
 3960: 
 3961:                             }
 3962:                             $output .= "\><startouttext />".$$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text};
 3963:                             $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text};
 3964:                             $output .= '<endouttext /></foil>'."\n";
 3965:                         }
 3966:                         if ($context eq 'CSTR') {
 3967:                             chomp($output);
 3968:                             $output .= qq|
 3969:   </foilgroup>
 3970:  </radiobuttonresponse>
 3971: |;
 3972:                         }
 3973:                     } else {
 3974:                         if ($context eq 'CSTR') {
 3975:                             $output .= qq|
 3976:    <optionresponse max="$numfoils" randomize="yes">
 3977:     <foilgroup options="('True','False')">
 3978: |;
 3979:                         } else {
 3980:                             $resourcedata{$symb.'newopt'} = '';
 3981:                             $resourcedata{$symb.'delopt'} = '';
 3982:                             $resourcedata{$symb.'options'} = "('True','False')";
 3983:                             $resourcedata{$symb.'hiddenparts'} = '!option';
 3984:                             $resourcedata{$symb.'questiontype'} = 'option';
 3985:                             $resourcedata{$symb.'maxfoils'} = $numfoils;
 3986:                         }
 3987:                         for (my $k=0; $k<@{$$allanswers{$id}{$list}}; $k++) {
 3988:                             my $iter = $k+1;
 3989:                             $output .= "   <foil name=\"foil".$k."\" value=\"";
 3990:                             if (grep/^$$allanswers{$id}{$list}[$k]$/,@{$$settings{$id}{$list}{correctanswer}}) {
 3991:                                 $output .= "True\"";
 3992:                                 $resourcedata{$symb.'value'.$iter} = "True";
 3993:                             } else {
 3994:                                 $output .= "False\"";
 3995:                                 $resourcedata{$symb.'value'.$iter} = "False";
 3996:                             }
 3997:                             if ($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{texttype} eq 'text/html') {
 3998:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} = &HTML::Entities::decode($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text});
 3999:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} = &Apache::loncleanup::htmlclean($$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text});
 4000:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} =~  s#(<img src=")([^>]+)>#$1../../resfiles/$2 />#gi;
 4001:                                 $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text} =~  s#</?p>##g;
 4002:                             }
 4003:                             $output .= "\><startouttext />".$$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text}."<br /><endouttext /></foil>\n";
 4004:                             $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$list}{$$allanswers{$id}{$list}[$k]}{text};
 4005:                         }
 4006:                         if ($context eq 'CSTR') {
 4007:                             chomp($output);
 4008:                             $output .= qq|
 4009:     </foilgroup>
 4010:    </optionresponse>
 4011: |;
 4012:                         }
 4013:                     }
 4014:                 }
 4015:             } elsif ($$settings{$id}{class} eq 'match') {
 4016:                 my %allmatchers = ();
 4017:                 my @allmatch = ();
 4018:                 my %matchtext = ();
 4019:                 my $anscount = 0;
 4020:                 my %ansnum = ();
 4021:                 my $maxfoils = 0;
 4022:                 my $test_for_html = 0; 
 4023:                 foreach my $grp (@{$$allchoices{$id}}) {
 4024:                     $maxfoils += @{$$settings{$id}{$grp}{correctanswer}};
 4025:                     foreach my $answer_id (@{$$allanswers{$id}{$grp}}) {
 4026:                         if ($$settings{$id}{$grp}{$answer_id}{texttype} eq '/text/html') {
 4027:                              
 4028:                             $$settings{$id}{$grp}{$answer_id}{text} = &HTML::Entities::decode($$settings{$id}{$grp}{$answer_id}{text});
 4029:                             $test_for_html = &test_for_html($$settings{$id}{$grp}{$answer_id}{text});
 4030:                             $$settings{$id}{$grp}{$answer_id}{text} = &Apache::loncleanup::htmlclean($$settings{$id}{$grp}{$answer_id}{text});
 4031:                             $$settings{$id}{$grp}{$answer_id}{text} =~  s#(<img src=")([^>]+)>#$1../../resfiles/$2 />#gi;
 4032:                             $$settings{$id}{$grp}{$answer_id}{text} =~  s#</?p>##g;
 4033:                         }
 4034:                         unless (exists($allmatchers{$$settings{$id}{$grp}{$answer_id}{text}})) {
 4035:                             $allmatchers{$$settings{$id}{$grp}{$answer_id}{text}} = $anscount;
 4036:                             $allmatch[$anscount] = $$settings{$id}{$grp}{$answer_id}{text};
 4037:                             $anscount ++;
 4038:                             
 4039:                         }
 4040:                         if (grep/^$answer_id$/,@{$$settings{$id}{$grp}{correctanswer}}) {
 4041:                             push(@{$ansnum{$grp}},$allmatchers{$$settings{$id}{$grp}{$answer_id}{text}});
 4042:                         }
 4043:                     }
 4044:                     if ($context eq 'DOCS') {
 4045:                         $matchtext{$ansnum{$grp}[0]} = $allmatch[$ansnum{$grp}[0]-1];
 4046:                     }
 4047:                 }
 4048:                 my $allmatchlist = "('".join("','",@allmatch)."')";
 4049:                 if ($context eq 'CSTR') {
 4050:                     if ($test_for_html) {
 4051:                         $output .= qq|
 4052: <matchresponse max="$maxfoils" randomize="yes">
 4053:     <foilgroup>
 4054:         <itemgroup>
 4055: |;
 4056:                     } else {
 4057:                         $output .= qq|
 4058: <optionresponse max="10" randomize="yes">
 4059:     <foilgroup options="$allmatchlist">
 4060: |;
 4061:                     }
 4062:                 } else {
 4063:                     $resourcedata{$symb.'newopt'} = '';
 4064:                     $resourcedata{$symb.'delopt'} = '';
 4065:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 4066:                     $resourcedata{$symb.'questiontype'} = 'option';
 4067:                     $resourcedata{$symb.'maxfoils'} =  $maxfoils;
 4068:                 }
 4069:                 my $iter = 0;
 4070:                 foreach my $match (@allmatch) {  
 4071:                     $iter ++;
 4072:                     if ($context eq 'CSTR') {
 4073:                         if ($test_for_html) {
 4074:                             $output .= qq|
 4075: <item name="ans_$iter">
 4076: <startouttext />$match<endouttext />
 4077: </item>
 4078: |;
 4079:                         }
 4080:                     }
 4081:                 }
 4082:                 if ($context eq 'CSTR') {
 4083:                     if ($test_for_html) {
 4084:                         $output .= qq|
 4085:         </itemgroup>
 4086: |;
 4087:                     }
 4088:                 }
 4089:                 $iter = 0;
 4090:                 for (my $k=0; $k<@{$$allchoices{$id}}; $k++) {
 4091:                     if ($$settings{$id}{$$allchoices{$id}[$k]}{texttype} eq 'text/html') {
 4092:                         $$settings{$id}{$$allchoices{$id}[$k]}{text} = &HTML::Entities::decode($$settings{$id}{$$allchoices{$id}[$k]}{text});
 4093:                         $$settings{$id}{$$allchoices{$id}[$k]}{text} = &Apache::loncleanup::htmlclean($$settings{$id}{$$allchoices{$id}[$k]}{text});
 4094:                         $$settings{$id}{$$allchoices{$id}[$k]}{text} =~  s#(<img src=")([^>]+)>#$1../../resfiles/$2 />#gi;
 4095:                         $$settings{$id}{$$allchoices{$id}[$k]}{text} =~  s#</?p>##g;
 4096:                     }
 4097:                     foreach my $ans (@{$ansnum{$$allchoices{$id}[$k]}}) {
 4098:                         $iter ++;
 4099:                         my $ans_id = $ans + 1;
 4100:                         if ($context eq 'CSTR') {
 4101:                             my $value;
 4102:                             if ($test_for_html) {
 4103:                                 $value = 'ans_'.$ans_id;
 4104:                             } else {
 4105:                                 $value = $allmatch[$ans];
 4106:                             }
 4107:                             $output .= qq|
 4108:         <foil location="random" value="$value" name="foil_$iter">
 4109:          <startouttext />$$settings{$id}{$$allchoices{$id}[$k]}{text}<endouttext />
 4110:         </foil>
 4111:                            
 4112: |;
 4113:                         }
 4114:                     }
 4115:                     if ($context eq 'DOCS') {
 4116:                         $resourcedata{$symb.'value'.$iter} = $matchtext{$ansnum{$$allchoices{$id}[$k]}[0]};
 4117:                         $resourcedata{$symb.'text'.$iter} = $$settings{$id}{$$allchoices{$id}[0]}{text};
 4118:                     }
 4119:                 }
 4120:                 if ($context eq 'CSTR') {
 4121:                     $output .= qq|
 4122:     </foilgroup>
 4123: |;
 4124:                     if ($test_for_html) {
 4125:                         $output .= qq|
 4126: </matchresponse>
 4127: |;
 4128:                     } else {
 4129:                         $output .= qq|
 4130: </optionresponse>
 4131: |;
 4132:                     }
 4133:                 } else {
 4134:                     $resourcedata{$symb.'options'} = "('".join("','",@allmatch)."')";
 4135:                 }
 4136:             } elsif (($$settings{$id}{class} eq 'string') || 
 4137:                      ($$settings{$id}{class} eq 'shortanswer')) {
 4138:                 my $labelnum = 0;
 4139:                 my @str_labels = ();
 4140:                 if ($cms eq 'webctce4') {
 4141:                     foreach my $str_id (@{$$settings{$id}{str}}) {
 4142:                         foreach my $label (@{$$settings{$id}{$str_id}{labels}}) {
 4143:                             push(@str_labels,$label);
 4144:                         }
 4145:                     }
 4146:                 } elsif ($cms eq 'webctvista4') {
 4147:                     @str_labels = @{$$settings{$id}{str}};
 4148:                 }
 4149:                 foreach my $label (@str_labels) {
 4150:                     $labelnum ++;
 4151:                     my $numerical = 1;
 4152:                     if ($context eq 'DOCS') {
 4153:                         $numerical = 0;
 4154:                     } else {
 4155:                         for (my $i=0; $i<@{$$settings{$id}{strings}{$label}}; $i++) {
 4156:                             $$settings{$id}{strings}{$label}[$i] =~ s/^\s+//;
 4157:                             $$settings{$id}{strings}{$label}[$i] =~ s/\s+$//; 
 4158:                             if ($$settings{$id}{strings}{$label}[$i] =~ m/([^\-\d\.]|\.\.)/) {
 4159:                                 $numerical = 0;
 4160:                             }
 4161:                         }
 4162:                     }
 4163:                     if ($numerical) {
 4164:                         my $numans;
 4165:                         my $tol;
 4166:                         if (@{$$settings{$id}{strings}{$label}} == 1) {
 4167:                             $tol = '5%';
 4168:                             $numans = $$settings{$id}{strings}{$label}[0];
 4169:                         } else {
 4170:                             my $min = $$settings{$id}{strings}{$label}[0];
 4171:                             my $max = $$settings{$id}{strings}{$label}[0];
 4172:                             for (my $k=1; $k<@{$$settings{$id}{strings}{$label}}; $k++) {
 4173:                                 if ($$settings{$id}{strings}{$label}[$k] <= $min) {
 4174:                                     $min = $$settings{$id}{strings}{$label}[$k];
 4175:                                 }
 4176:                                 if ($$settings{$id}{strings}{$label}[$k] >= $max) {
 4177:                                     $max = $$settings{$id}{strings}{$label}[$k];
 4178:                                 }
 4179:                             }
 4180:                             $numans = ($max + $min)/2;
 4181:                             if ($numans == 0) {
 4182:                                 my $dev = abs($max - $numans);
 4183:                                 if (abs($numans - $min) > $dev) {
 4184:                                     $dev = abs($numans - $min);
 4185:                                 }
 4186:                                 $tol = $dev;
 4187:                             } else {
 4188:                                 $tol = 100*($max - $min)/($numans*2);
 4189:                                 $tol .= '%';
 4190:                             }
 4191:                         }
 4192:                         if ($context eq 'CSTR') {
 4193:                             if (@{$$settings{$id}{str}} > 1) {
 4194:                                 $output .= qq|
 4195: <startouttext />$labelnum.<endouttext />
 4196: |;
 4197:                             }
 4198:                             $output .= qq|
 4199: <numericalresponse answer="$numans">
 4200:         <responseparam type="tolerance" default="$tol" name="tol" description="Numerical Tolerance" />
 4201:         <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures"
 4202: />
 4203:         <textline />
 4204: </numericalresponse>
 4205: <startouttext /><br /><endouttext />
 4206: |;
 4207:                         }
 4208:                     } else {
 4209:                         if ($context eq 'DOCS') {
 4210:                             $resourcedata{$symb.'hiddenparts'} = '!string';
 4211:                             $resourcedata{$symb.'questiontype'} = 'string';
 4212:                             $resourcedata{$symb.'maxfoils'} = @{$$allanswers{$id}{strings}{$label}};
 4213:                             $resourcedata{$symb.'hiddenparts'} = '!string';
 4214:                             if ($$settings{$id}{$label}{case} eq "No") {
 4215:                                 $resourcedata{$symb.'stringtype'} = 'ci';
 4216:                             } elsif ($$settings{$id}{$label}{case} eq "Yes") {
 4217:                                 $resourcedata{$symb.'stringtype'} = 'cs';
 4218:                             }
 4219:                             $resourcedata{$symb.'stringanswer'} = $$settings{$id}{strings}{$label}[0];
 4220:                         } else {
 4221:                             if (@{$$settings{$id}{str}} > 1) {
 4222:                                 $output .= qq|
 4223: <startouttext />$labelnum.<endouttext />
 4224: |;
 4225:                             }
 4226:                             if (@{$$settings{$id}{strings}{$label}} == 1) {
 4227:                                 my $casetype;
 4228:                                 if ($$settings{$id}{$label}{case} eq "No") {
 4229:                                     $casetype = 'ci';
 4230:                                 } elsif ($$settings{$id}{$label}{case} eq "Yes") {
 4231:                                     $casetype = 'cs';
 4232:                                 }
 4233:                                 $output .= qq|
 4234: <stringresponse answer="$$settings{$id}{strings}{$label}[0]" type="$casetype">
 4235: <textline>
 4236: </textline>
 4237: </stringresponse>
 4238: <startouttext /><br /><endouttext />
 4239: |;
 4240:                             } else {
 4241:                                 my @answertext = ();
 4242:                                 for (my $k=0; $k<@{$$settings{$id}{strings}{$label}}; $k++) {
 4243:                                     $$settings{$id}{strings}{$label}[$k] =~ s/\|/\|/g;
 4244:                                     push @answertext, $$settings{$id}{strings}{$label}[$k];
 4245:                                 }
 4246:                                 my $regexpans = join('|',@answertext);
 4247:                                 $regexpans = '/^('.$regexpans.')\b/';
 4248:                                 $output .= qq|
 4249: <stringresponse answer="$regexpans" type="re">
 4250: <textline>
 4251: </textline>
 4252: </stringresponse>
 4253: <startouttext /><br /><endouttext />
 4254: |;
 4255:                             }
 4256:                         }
 4257:                     }
 4258:                 }
 4259:             } elsif ($$settings{$id}{class} eq 'numerical') {
 4260:                 my %mathfns = (
 4261:                     'abs' => 'abs',
 4262:                     'acos' => 'acos',
 4263:                     'asin' => 'asin',
 4264:                     'atan' => 'atan',
 4265:                     'ceil' => 'ceil',
 4266:                     'cos' => 'cos',
 4267:                     'exp' => 'exp',
 4268:                     'fact' => 'factorial',
 4269:                     'floor' => 'floor',
 4270:                     'int' => 'int',
 4271:                     'ln' => 'log',
 4272:                     'log' => 'log',
 4273:                     'max' => 'max',
 4274:                     'min' => 'min',
 4275:                     'round' => 'roundto',
 4276:                     'sin' => 'sin',
 4277:                     'sqrt' => 'sqrt',
 4278:                     'tan' => 'tan',
 4279:                 );
 4280:                 my $scriptblock = qq|
 4281: <script type="loncapa/perl">
 4282: |;
 4283:                 foreach my $numid (@{$$settings{$id}{numids}}) {
 4284:                     my $formula = $$settings{$id}{$numid}{formula};
 4285:                     my $pattern = join('|',(sort (keys (%mathfns))));
 4286:                     $formula =~ s/($pattern)/\&$mathfns{$1}/g;
 4287:                     foreach my $var (keys %{$$settings{$id}{$numid}{vars}}) {
 4288:                         my $decnum = $$settings{$id}{$numid}{vars}{$var}{dec};
 4289:                         my $increment = '0.';
 4290:                         if ($decnum == 0) {
 4291:                             $increment = 1; 
 4292:                         } else {
 4293:                             my $deccount = $decnum;
 4294:                             while ($deccount > 1) {
 4295:                                 $increment.= '0';
 4296:                                 $deccount --;
 4297:                             }
 4298:                             $increment .= '1';
 4299:                         }
 4300:                         if ($cms eq 'webctce4') { 
 4301:                             $formula =~ s/{($var)}/(\$$1)/g;
 4302:                         } elsif ($cms eq 'webctvista4') {
 4303:                             $formula =~ s/\[($var)\]/(\$$1)/g;
 4304:                         }
 4305:                         $scriptblock .= qq|
 4306: \$$var=&random($$settings{$id}{$numid}{vars}{$var}{min},$$settings{$id}{$numid}{vars}{$var}{max},$increment);
 4307: |;
 4308:                     }
 4309:                     $scriptblock .= qq|
 4310: \$answervar = $formula;
 4311: </script>
 4312: |;
 4313:                     if ($context eq 'CSTR') {
 4314:                         $output = "<problem>\n".$scriptblock.$output;
 4315:                         my $ansformat = '';
 4316:                         my $sigfig = '0,15';
 4317:                         if ($$settings{$id}{$numid}{format} eq 'sig') {
 4318:                             $sigfig = $$settings{$id}{$numid}{digits}.','.$$settings{$id}{$numid}{digits};
 4319:                         } elsif ($$settings{$id}{$numid}{format} eq 'dec') {
 4320:                             $ansformat = $$settings{$id}{$numid}{digits}.'f';
 4321:                         }
 4322:                         if ($ansformat) {
 4323:                             $ansformat = 'format="'.$ansformat.'"';
 4324:                         }
 4325:                         my $tolerance = $$settings{$id}{$numid}{tolerance};
 4326:                         if (lc($$settings{$id}{$numid}{toltype}) eq 'percent') {
 4327:                             $tolerance .= '%';
 4328:                         }
 4329:                         my $unit = '';
 4330:                         foreach my $unitid (@{$$settings{$id}{$numid}{units}}) {
 4331:                             $unit .=  $$settings{$id}{$numid}{$unitid}{text};
 4332:                         }
 4333:                         my $unitentry = '';
 4334:                         if ($unit ne '') {
 4335:                             $unitentry =  'unit="'.$unit.'"';
 4336:                         }
 4337:                         $output .= qq|
 4338: <numericalresponse $unitentry $ansformat  answer="\$answervar">
 4339:         <responseparam type="tolerance" default="$tolerance" name="tol" description="Numerical Tolerance" />
 4340:         <responseparam name="sig" type="int_range" default="$sigfig" description="Significant Figures"
 4341: />
 4342:         <textline />
 4343: </numericalresponse>
 4344: |;
 4345:                     }
 4346:                 }
 4347:             }
 4348:         }
 4349:         if ($context eq 'CSTR') {
 4350:             my $probdir;
 4351:             my $catid = $$settings{$id}{category};
 4352:             if ($catid) {
 4353:                 if ($cms eq 'webctce4') {
 4354:                     $probdir = $$catinfo{$catid}{title}.'_'.$catid;
 4355:                 } else {
 4356:                     $probdir = $$catinfo{$catid}{title};
 4357:                 }
 4358:                 $probdir =~ s/\s/_/g;
 4359:                 $probdir =~ s/://g;
 4360:             } elsif (defined($dirtitle)) {
 4361:                 $probdir = $dirtitle;
 4362:             }
 4363:             if (!-e "$destdir/problems/$probdir") {
 4364:                 mkdir("$destdir/problems/$probdir",0755);
 4365:             }
 4366:             $output .= qq|</problem>
 4367: |;
 4368:             my $title = $$settings{$id}{title};
 4369:             $title =~ s/\s/_/g;
 4370:             $title =~ s/:/_/g;
 4371:             $title .= '_'.$id;
 4372:             open(PROB,">$destdir/problems/$probdir/$title.problem");
 4373:             print PROB $output;
 4374:             close PROB;
 4375:         } else {
 4376: # put %resourcedata;
 4377:             my $reply=&Apache::lonnet::cput
 4378:                 ('resourcedata',\%resourcedata,$cdom,$cnum);
 4379:         }
 4380:     }
 4381: }
 4382: 
 4383: sub text_cleanup {
 4384:     my ($text) = @_;
 4385:     $text =~ s/(\&)(nbsp|gt|lt)(?!;)/$1$2;$3/gi;
 4386:     $text = &Apache::loncleanup::htmlclean($text);
 4387:     $text =~ s#(<img src=["']?)([^>]+?)(/?>)#$1../../resfiles/$2 />#gi;
 4388:     $text =~ s#<([bh])r>#<$1r />#g;
 4389:     $text =~ s#<p>#<br /><br />#g;
 4390:     $text =~ s#</p>##g;
 4391:     return $text;
 4392: }
 4393: 
 4394: sub test_for_html {
 4395:     my ($source) = @_; 
 4396:     my @tags = ();
 4397:     my $p = HTML::Parser->new
 4398:     (
 4399:      xml_mode => 1,
 4400:      start_h =>
 4401:      [sub {
 4402:         my ($tagname) = @_;
 4403:         push @tags, $tagname;
 4404:      }, "tagname"],
 4405:     );
 4406:     $p->parse($source);
 4407:     $p->eof;
 4408:     return length(@tags); 
 4409: } 
 4410: 
 4411: sub write_bb6_questions {
 4412:     my ($allids,$containerdir,$context,$settings,$dirname,$destdir,$res,$total,$newdir,$cid,$cdom,$cnum,$docroot) = @_;
 4413:     my $qnum = 0;
 4414:     foreach my $id (@{$allids}) {
 4415:         my $questiontext = $$settings{$id}{question}{text};
 4416:         my $question_texttype = $$settings{$id}{question}{texttype};
 4417:         &process_html(\$questiontext,'bb6',$question_texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
 4418:         $qnum ++;
 4419:         my $output;
 4420:         my $permcontainer = $containerdir;
 4421:         $permcontainer =~ s#/home/httpd/html/userfiles#uploaded#;
 4422:         my $symb = $cid.'.'.$permcontainer.'___'.$qnum.'___lib/templates/simpleproblem.problem.0.';
 4423:         my %resourcedata = ();
 4424:         for (my $i=0; $i<10; $i++) {
 4425:             my $iter = $i+1;
 4426:             $resourcedata{$symb.'text'.$iter} = "";
 4427:             $resourcedata{$symb.'value'.$iter} = "unused";
 4428:             $resourcedata{$symb.'position'.$iter} = "random";
 4429:         }
 4430:         $resourcedata{$symb.'randomize'} = 'yes';
 4431:         $resourcedata{$symb.'maxfoils'} = 10;
 4432:         if ($context eq 'CSTR') {
 4433:             $output = qq|<problem>
 4434: |;
 4435:         }
 4436:         $$total{prob} ++;
 4437:         $questiontext .= &add_images_links('question',$context,$settings,$id,$dirname,$res);
 4438:         if ($$settings{$id}{class} eq "Essay") {
 4439:             if ($context eq 'CSTR') {
 4440:                 $output .= qq|<startouttext />$questiontext<endouttext />
 4441:  <essayresponse>
 4442:  <textfield></textfield>
 4443:  </essayresponse>
 4444: |;
 4445:              } else {
 4446:                  $resourcedata{$symb.'questiontext'} = $questiontext;
 4447:                  $resourcedata{$symb.'hiddenparts'} = '!essay';
 4448:                  $resourcedata{$symb.'questiontype'} = 'essay';
 4449:              }
 4450:         } else {
 4451:             if ($context eq 'CSTR') {
 4452:                 $output .= qq|<startouttext />$questiontext\n<endouttext />|;
 4453:             } else {
 4454:                 $resourcedata{$symb.'questiontext'} = $questiontext;
 4455:             }
 4456:             my $numfoils = @{$$settings{$id}{answers}};
 4457:             if (($$settings{$id}{class} eq 'Multiple Choice') || 
 4458:                 ($$settings{$id}{class} eq 'True/False')) {
 4459:                 if ($context eq 'CSTR') {
 4460:                     $output .= qq|
 4461:  <radiobuttonresponse max="$numfoils" randomize="yes">
 4462:   <foilgroup>
 4463: |;
 4464:                 } else {
 4465:                     $resourcedata{$symb.'hiddenparts'} = '!radio';
 4466:                     $resourcedata{$symb.'questiontype'} = 'radio';
 4467:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 4468:                 }
 4469:                 for (my $k=0; $k<$numfoils; $k++) {
 4470:                     my $iter = $k+1;
 4471:                     my $answer_id = $$settings{$id}{answers}[$k];
 4472:                     my $answer_text = $$settings{$id}{$answer_id}{text};
 4473:                     my $texttype = $$settings{$id}{$answer_id}{texttype};
 4474:                     &process_html(\$answer_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
 4475:                     $answer_text .= &add_images_links('response',$context,$settings,$id,$dirname,$res); 
 4476:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 4477:                     if (grep/^$answer_id$/,@{$$settings{$id}{correctanswer}}) {
 4478:                         $output .= "true\" location=\"";
 4479:                         $resourcedata{$symb.'value'.$iter} = "true";
 4480:                     } else {
 4481:                         $output .= "false\" location=\"";
 4482:                         $resourcedata{$symb.'value'.$iter} = "false";
 4483:                     }
 4484:                     if (lc ($$settings{$id}{$answer_id}{text}) =~ m/^\s?([Aa]ll)|([Nn]one)\s(of\s)?the\sabove\.?/) {
 4485:                         $output .= "bottom\"";
 4486:                         $resourcedata{$symb.'position'.$iter} = "bottom";
 4487:                     } else {
 4488:                         $output .= "random\"";
 4489:                     }
 4490:                     $output .= '\><startouttext />'.$answer_text.
 4491:                                '<endouttext /></foil>'."\n";
 4492:                     $resourcedata{$symb.'text'.$iter} = $answer_text;
 4493:                 }
 4494:                 if ($context eq 'CSTR') {
 4495:                     chomp($output);
 4496:                     $output .= qq|
 4497:     </foilgroup>
 4498:     <hintgroup showoncorrect="no">
 4499:      <radiobuttonhint>
 4500:      </radiobuttonhint>
 4501:      <hintpart on="default">
 4502:       <startouttext/><endouttext />
 4503:      </hintpart>
 4504:     </hintgroup>
 4505:    </radiobuttonresponse>
 4506: |;
 4507:                 }
 4508:             } elsif ($$settings{$id}{class} eq 'Multiple Answer') {
 4509:                 if ($context eq 'CSTR') {
 4510:                     $output .= qq|
 4511:    <optionresponse max="$numfoils" randomize="yes">
 4512:     <foilgroup options="('True','False')">
 4513: |;
 4514:                 } else {
 4515:                     $resourcedata{$symb.'newopt'} = '';
 4516:                     $resourcedata{$symb.'delopt'} = '';
 4517:                     $resourcedata{$symb.'options'} = "('True','False')";
 4518:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 4519:                     $resourcedata{$symb.'questiontype'} = 'option';
 4520:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 4521:                 }
 4522:                 for (my $k=0; $k<$numfoils; $k++) {
 4523:                     my $iter = $k+1;
 4524:                     my $answer_id = $$settings{$id}{answers}[$k];
 4525:                     my $answer_text = $$settings{$id}{$answer_id}{text};
 4526:                     my $texttype = $$settings{$id}{$answer_id}{texttype};
 4527:                     &process_html(\$answer_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
 4528:                     $answer_text .= &add_images_links('response',$context,$settings,$id,$dirname,$res);
 4529: 
 4530:                     $output .= "   <foil name=\"foil".$k."\" value=\"";
 4531:                     if (grep/^$answer_id$/,@{$$settings{$id}{correctanswer}}) {
 4532:                         $output .= "True\"";
 4533:                         $resourcedata{$symb.'value'.$iter} = "True";
 4534:                     } else {
 4535:                         $output .= "False\"";
 4536:                         $resourcedata{$symb.'value'.$iter} = "False";
 4537:                     }
 4538:                     $output .= "\><startouttext />".$answer_text."<endouttext /></foil>\n";
 4539:                     $resourcedata{$symb.'text'.$iter} = $answer_text;
 4540:                 }
 4541:                 if ($context eq 'CSTR') {
 4542:                     chomp($output);
 4543:                     $output .= qq|
 4544:     </foilgroup>
 4545:     <hintgroup showoncorrect="no">
 4546:      <optionhint>
 4547:      </optionhint>
 4548:      <hintpart on="default">
 4549:       <startouttext/><endouttext />
 4550:      </hintpart>
 4551:     </hintgroup>
 4552:    </optionresponse>
 4553: |;
 4554:                 }
 4555:             } elsif ($$settings{$id}{class} eq 'Ordering') {
 4556:                 my @allorder = ();
 4557:                 if ($context eq 'CSTR') {
 4558:                     $output .= qq|
 4559:    <rankresponse max="$numfoils" randomize="yes">
 4560:     <foilgroup>
 4561: |;
 4562:                 } else {
 4563:                     $resourcedata{$symb.'newopt'} = '';
 4564:                     $resourcedata{$symb.'delopt'} = '';
 4565:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 4566:                     $resourcedata{$symb.'questiontype'} = 'option';
 4567:                     $resourcedata{$symb.'maxfoils'} = $numfoils;
 4568:                 }
 4569:                 for (my $k=0; $k<$numfoils; $k++) {
 4570:                     my $answer_id = $$settings{$id}{answers}[$k];
 4571:                     my $answer_text = $$settings{$id}{$answer_id}{text};
 4572:                     my $texttype = $$settings{$id}{$answer_id}{texttype};
 4573:                     &process_html(\$answer_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
 4574:                     $answer_text .= &add_images_links('response',$context,$settings,$id,$dirname,$res);
 4575:                     my $iter = $k+1;
 4576:                     if ($context eq 'CSTR') {
 4577:                         $output .= "   <foil location=\"random\" name=\"foil".$k."\" value=\"".$$settings{$id}{$answer_id}{order}."\"><startouttext />".$answer_text."<endouttext /></foil>\n";
 4578:                     } else {
 4579:                         $resourcedata{$symb.'text'.$iter} = $answer_text;
 4580:                         $resourcedata{$symb.'value'.$iter} = $$settings{$id}{$answer_id}{order};
 4581:                         if (!grep/^$$settings{$id}{$answer_id}{order}$/,@allorder) {
 4582:                             push(@allorder,$$settings{$id}{$answer_id}{order}); 
 4583:                         }
 4584:                     }
 4585:                 }
 4586:                 if ($context eq 'CSTR') {
 4587:                     chomp($output);
 4588:                     $output .= qq|
 4589:     </foilgroup>
 4590:    </rankresponse>
 4591: |;
 4592:                 } else {
 4593:                     @allorder = sort {$a <=> $b} @allorder;
 4594:                     $resourcedata{$symb.'options'} = "('".join("','",@allorder)."')";
 4595:                 }
 4596:             } elsif ($$settings{$id}{class} eq 'Fill in the Blank') {
 4597:                 my $numerical = 1;
 4598:                 if ($context eq 'DOCS') {
 4599:                     $numerical = 0;
 4600:                 } else {
 4601:                     for (my $k=0; $k<@{$$settings{$id}{correctanswer}}; $k++) {
 4602:                         if ($$settings{$id}{correctanswer}[$k] =~ m/([^\d\.]|\.\.)/) {
 4603:                             $numerical = 0;
 4604:                         }
 4605:                     }
 4606:                 }
 4607:                 if ($numerical) {
 4608:                     my $numans;
 4609:                     my $tol;
 4610:                     if (@{$$settings{$id}{correctanswer}} == 1) {
 4611:                         $tol = 5;
 4612:                         $numans = $$settings{$id}{correctanswer}[0];
 4613:                     } else {
 4614:                         my $min = $$settings{$id}{correctanswer}[0];;
 4615:                         my $max = $min;
 4616:                         for (my $k=1; $k<@{$$settings{$id}{correctanswer}}; $k++) {
 4617:                             if ($$settings{$id}{correctanswer}[$k] <= $min) {
 4618:                                 $min = $$settings{$id}{correctanswer}[$k];
 4619:                             }
 4620:                             if ($$settings{$id}{correctanswer}[$k] >= $max) {
 4621:                                 $max = $$settings{$id}{correctanswer}[$k];
 4622:                             }
 4623:                         }
 4624:                         $numans = ($max + $min)/2;
 4625:                         $tol = 100*($max - $min)/($numans*2);
 4626:                         $tol = 5;
 4627:                     }
 4628:                     if ($context eq 'CSTR') {
 4629:                         $output .= qq|
 4630: <numericalresponse answer="$numans">
 4631:         <responseparam type="tolerance" default="$tol%" name="tol" description="Numerical Tolerance" />
 4632:         <responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures"
 4633: />
 4634:         <textline />
 4635: </numericalresponse>
 4636: <hintgroup showoncorrect="no">
 4637:  <numericalhint>
 4638:  </numericalhint>
 4639:  <hintpart on="default">
 4640:     <startouttext/><endouttext />
 4641:  </hintpart>
 4642: </hintgroup>
 4643: |;
 4644:                     }
 4645:                 } else {
 4646:                     if ($context eq 'DOCS') {
 4647:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 4648:                         $resourcedata{$symb.'questiontype'} = 'string';
 4649:                         $resourcedata{$symb.'maxfoils'} = 1;
 4650:                         $resourcedata{$symb.'hiddenparts'} = '!string';
 4651:                         $resourcedata{$symb.'stringtype'} = 'ci';
 4652:                         $resourcedata{$symb.'stringanswer'} = $$settings{$id}{correctanswer}[0];
 4653:                     } else {
 4654:                         if (@{$$settings{$id}{correctanswer}} == 1) {
 4655:                             $output .= qq|
 4656: <stringresponse answer="$$settings{$id}{correctanswer}[0];" type="ci">
 4657: <textline>
 4658: </textline>
 4659: </stringresponse>
 4660: <hintgroup showoncorrect="no">
 4661: <stringhint type="cs">
 4662: </stringhint>
 4663: <hintpart on="default">
 4664:   <startouttext/><endouttext />
 4665: </hintpart>
 4666: </hintgroup>
 4667: |;
 4668:                         } else {
 4669:                             my @answertext = ();
 4670:                             for (my $k=0; $k<@{$$settings{$id}{correctanswer}}; $k++) {
 4671:                                 my $answer_text = $$settings{$id}{correctanswer}[$k];
 4672:                                 $answer_text =~ s/\|/\|/g;
 4673:                                 push @answertext, $answer_text;
 4674:                             }
 4675:                             my $regexpans = join('|',@answertext);
 4676:                             $regexpans = '/^('.$regexpans.')\b/';
 4677:                             $output .= qq|
 4678: <stringresponse answer="$regexpans" type="re">
 4679: <textline>
 4680: </textline>
 4681: </stringresponse>
 4682: <hintgroup showoncorrect="no">
 4683:  <stringhint type="cs">
 4684:  </stringhint>
 4685:  <hintpart on="default">
 4686:     <startouttext/><endouttext />
 4687:  </hintpart>
 4688: </hintgroup>
 4689: |;
 4690:                         }
 4691:                     }
 4692:                 }
 4693:             } elsif ($$settings{$id}{class} eq "Matching") {
 4694:                 my @allmatchers = ();
 4695:                 my %matchtext = ();
 4696:                 if ($context eq 'CSTR') {
 4697:                     $output .= qq|
 4698: <matchresponse max="10" randomize="yes">
 4699:     <foilgroup>
 4700:         <itemgroup>
 4701: |;
 4702:                 } else {
 4703:                     $resourcedata{$symb.'newopt'} = '';
 4704:                     $resourcedata{$symb.'delopt'} = '';
 4705:                     $resourcedata{$symb.'hiddenparts'} = '!option';
 4706:                     $resourcedata{$symb.'questiontype'} = 'option';
 4707:                     $resourcedata{$symb.'maxfoils'} =  $numfoils;
 4708:                 }
 4709:                 for (my $k=0; $k<$$settings{$id}{allchoices}; $k++) {
 4710:                     my $choice_id = 'rightmatch'.$k;
 4711:                     my $choice_text = $$settings{$id}{$choice_id}{text};
 4712:                     my $texttype = $$settings{$id}{$choice_id}{texttype};
 4713:                     my $choice_plaintext = &remove_html($choice_text);
 4714:                     &process_html(\$choice_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
 4715:                     $choice_text .= &add_images_links($choice_id,$context,$settings,$id,$dirname,$res);
 4716:                     push(@allmatchers,$choice_plaintext);
 4717:                     if ($context eq 'CSTR') {
 4718:                         $output .= qq|
 4719: <item name="$choice_id">
 4720: <startouttext />$choice_text<endouttext />
 4721: </item>
 4722:                     |;
 4723:                     }
 4724:                 }
 4725:                 if ($context eq 'CSTR') {
 4726:                     $output .= qq|
 4727:         </itemgroup>
 4728: |;
 4729:                 }
 4730:                 for (my $k=0; $k<$numfoils; $k++) {
 4731:                     my $answer_id = $$settings{$id}{answers}[$k];
 4732:                     my $answer_text = $$settings{$id}{$answer_id}{text};
 4733:                     my $texttype = $$settings{$id}{$answer_id}{texttype};
 4734:                     &process_html(\$answer_text,'bb6',$texttype,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir);
 4735:                     $answer_text .= &add_images_links($answer_id,$context,$settings,$id,$dirname,$res);
 4736:                     if ($context eq 'CSTR') {
 4737:                         $output .= '
 4738:         <foil location="random" value="rightmatch'.$$settings{$id}{$$settings{$id}{$answer_id}{correctanswer}}{order}.'" name="'.$answer_id.'">
 4739:          <startouttext />'.$answer_text.'<endouttext />
 4740:         </foil>
 4741: ';
 4742:                     } else {
 4743:                         my $iter = $k+1;
 4744:                         $resourcedata{$symb.'value'.$iter} = "$allmatchers[$$settings{$id}{$$settings{$id}{$answer_id}{correctanswer}}{order}]";
 4745:                         $resourcedata{$symb.'text'.$iter} = $answer_text;
 4746:                     }
 4747:                 }
 4748:                 if ($context eq 'CSTR') {
 4749:                     $output .= qq|
 4750:     </foilgroup>
 4751: </matchresponse>
 4752: |;
 4753:                 } else {
 4754:                     $resourcedata{$symb.'options'} = "('".join("','",@allmatchers)."')";
 4755:                 }
 4756:             }
 4757:         }
 4758:         if ($context eq 'CSTR') {
 4759:             
 4760:             $output .= qq|
 4761:  <postanswerdate>
 4762:   $$settings{$id}{solutionfeedback}{text}
 4763:  </postanswerdate>
 4764: </problem>
 4765: |;
 4766:             my $title = $$settings{title};
 4767:             $title =~ s/\s/_/g;
 4768:             $title =~ s/\W//g;
 4769:             $title .= '_'.$id;
 4770:             open(PROB,">$newdir/$title.problem");
 4771:             print PROB $output;
 4772:             close PROB;
 4773:         } else {
 4774: # put %resourcedata;
 4775:             my $reply=&Apache::lonnet::cput
 4776:                 ('resourcedata',\%resourcedata,$cdom,$cnum);
 4777:         }
 4778:     }
 4779: }
 4780: 
 4781: sub retrieve_image {
 4782:     my ($context,$res,$dirname,$cdom,$cname,$docroot,$destdir,$urlpath,$filename) = @_;
 4783:     my $contents;
 4784:     my $url = $urlpath.$filename;
 4785:     my $ua=new LWP::UserAgent;
 4786:     my $request=new HTTP::Request('GET',$url);
 4787:     my $response=$ua->request($request);
 4788:     if ($response->is_success) { 
 4789:         $contents = $response->content;
 4790:         if (!-e "$docroot/$res") {
 4791:             mkdir("$docroot/$res",0755);
 4792:         }
 4793:         if (!-e "$docroot/$res/webimages") {
 4794:             mkdir("$docroot/$res/webimages",0755);
 4795:         }
 4796:         open(my $fh,">$docroot/$res/webimages/$filename");
 4797:         print $fh $contents;
 4798:         close($fh);
 4799:         if ($context eq 'DOCS') {
 4800:             my $copyfile = $dirname.'/'.$filename;
 4801:             my $source = "$docroot/$res/webimages/$filename";
 4802:             my $fileresult;
 4803:             if (-e $source) {
 4804:                 $fileresult = &Apache::lonnet::process_coursefile('copy',$cname,$cdom,$copyfile,$source);
 4805:             }
 4806:             return $fileresult;
 4807:         } elsif ($context eq 'CSTR') {
 4808:             if (!-e "$destdir/resfiles/$res") {
 4809:                 mkdir("$destdir/resfiles/$res",0755);
 4810:             }
 4811:             if (!-e "$destdir/resfiles/$res/webimages") {
 4812:                 mkdir("$destdir/resfiles/$res/webimages",0755);
 4813:             }
 4814:             rename("$docroot/$res/webimages/$filename","$destdir/resfiles/$res/webimages/$filename");
 4815:             return 'ok';
 4816:         }
 4817:     } else {
 4818:         return -1;
 4819:     }
 4820: }
 4821: 
 4822: # ---------------------------------------------------------------- Process Blackboard Announcements
 4823: sub process_announce {
 4824:     my ($res,$docroot,$destdir,$settings,$globalresref,$seqstem,$resrcfiles) = @_;
 4825:     my $xmlfile = $docroot.'/'.$res.".dat";
 4826:     my @state = ();
 4827:     my @assess = ();
 4828:     my $id;
 4829:     my $p = HTML::Parser->new
 4830:     (
 4831:      xml_mode => 1,
 4832:      start_h =>
 4833:      [sub {
 4834:         my ($tagname, $attr) = @_;
 4835:         push @state, $tagname;
 4836:         if ("@state" eq "ANNOUNCEMENT TITLE") {
 4837:             $$settings{title} = $attr->{value};
 4838:             $$settings{startassessment} = ();
 4839:         } elsif ("@state" eq "ANNOUNCEMENT DESCRIPTION FLAGS ISHTML") {  
 4840:             $$settings{ishtml} = $attr->{value};          
 4841:         } elsif ("@state" eq "ANNOUNCEMENT DESCRIPTION FLAGS ISNEWLINELITERAL" ) {
 4842:             $$settings{isnewline} = $attr->{value};
 4843:         } elsif ("@state" eq "ANNOUNCEMENT ISPERMANENT" ) {
 4844:             $$settings{ispermanent} = $attr->{value};
 4845:         } elsif ("@state" eq "ANNOUNCEMENT DATES UPDATED") {
 4846:             $$settings{dates} = $attr->{value}; 
 4847:         } elsif ("@state" eq "ANNOUNCEMENT FILES STARTASSESSMENT" ) {
 4848:             $id = $attr->{id};
 4849:             %{$$settings{startassessment}{$id}} = ();
 4850:             push @assess,$id;
 4851:         } elsif ("@state" eq "ANNOUNCEMENT FILES STARTASSESSMENT ATTRIB" ) {
 4852:             my $key = $attr->{key};
 4853:             $$settings{startassessment}{$id}{$key} = $attr->{value};
 4854:         }
 4855:      }, "tagname, attr"],
 4856:      text_h =>
 4857:      [sub {
 4858:         my ($text) = @_;
 4859:         if ("@state" eq "ANNOUNCEMENT DESCRIPTION TEXT") {
 4860:             $$settings{text} = $text;
 4861:         }
 4862:       }, "dtext"],
 4863:      end_h =>
 4864:      [sub {
 4865:         my ($tagname) = @_;
 4866:         pop @state;
 4867:      }, "tagname"],
 4868:     );
 4869:     $p->unbroken_text(1);
 4870:     $p->parse_file($xmlfile);
 4871:     $p->eof;
 4872: 
 4873:     if (defined($$settings{text})) {
 4874:         if ($$settings{ishtml} eq "false") {
 4875:             if ($$settings{isnewline} eq "true") {
 4876:                 $$settings{text} =~ s#\n#<br/>#g;
 4877:             }
 4878:         } else {
 4879:             $$settings{text} = &HTML::Entities::decode($$settings{text});
 4880:         }
 4881:     }
 4882:   
 4883:     if (@assess > 0) {
 4884:         foreach my $id (@assess) {
 4885:             $$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.";
 4886:         }
 4887:     }
 4888: 
 4889:     open(FILE,">$destdir/resfiles/$res.html");
 4890:     push @{$resrcfiles}, "$res.html";
 4891:     print FILE qq|<html>
 4892: <head>
 4893: <title>$$settings{title}</title>
 4894: </head>
 4895: <body bgcolor='#ffffff'>
 4896: <table>
 4897:  <tr>
 4898:   <td bgcolor='#CCCCFF'>$$settings{title} - announcement date: $$settings{dates}</td>
 4899:  </tr>
 4900: </table>
 4901: <br/>
 4902: $$settings{text}
 4903: |;
 4904:     print FILE qq|
 4905:   </body>
 4906:  </html>|;
 4907:     close(FILE);
 4908: }
 4909: 
 4910: # ---------------------------------------------------------------- Process Blackboard Content
 4911: sub process_content {
 4912:     my ($cms,$res,$context,$docroot,$destdir,$settings,$dom,$user,$resrcfiles,$packages,$hrefs) = @_;
 4913:     my $xmlfile = $docroot.'/'.$res.".dat";
 4914:     my $destresdir = $destdir;
 4915:     if ($context eq 'CSTR') {
 4916:         $destresdir =~ s|/home/$user/public_html/|/res/$dom/$user/|;
 4917:     } elsif ($context eq 'DOCS') {
 4918:         $destresdir =~ s|^/home/httpd/html/userfiles|/uploaded|;
 4919:     }
 4920:     my $filetag = '';
 4921:     if ($cms eq 'bb5') {
 4922:         $filetag = 'FILEREF';
 4923:     } elsif ($cms eq 'bb6') {
 4924:         $filetag = 'FILE';
 4925:     }
 4926:     my $filecount = 0;
 4927:     my @allrelfiles = ();
 4928:     my @state;
 4929:     @{$$settings{files}} = (); 
 4930:     my $p = HTML::Parser->new
 4931:     (
 4932:       xml_mode => 1,
 4933:       start_h =>
 4934:       [sub {
 4935:         my ($tagname, $attr) = @_;
 4936:         push @state, $tagname;
 4937:         if ("@state" eq "CONTENT ") {
 4938:             %{$$settings{maindata}} = ();
 4939:         } elsif ("@state" eq "CONTENT TITLECOLOR") {
 4940:             $$settings{titlecolor} =  $attr->{value};
 4941:         } elsif ("@state" eq "CONTENT MAINDATA TEXTCOLOR") {
 4942:             $$settings{maindata}{color} = $attr->{value};
 4943:         } elsif ("@state" eq "CONTENT MAINDATA FLAGS ISHTML") {  
 4944:             $$settings{maindata}{ishtml} = $attr->{value}; 
 4945:         } elsif ("@state" eq "CONTENT MAINDATA FLAGS ISNEWLINELITERAL") {  
 4946:             $$settings{maindata}{isnewline} = $attr->{value};
 4947:         } elsif ("@state" eq "CONTENT BODY TYPE") {
 4948:             $$settings{maindata}{bodytype} =  $attr->{value};
 4949:         } elsif ("@state" eq "CONTENT FLAGS ISAVAILABLE" ) {
 4950:             $$settings{isavailable} = $attr->{value};
 4951:         } elsif ("@state" eq "CONTENT FLAGS ISFOLDER" ) {
 4952:             $$settings{isfolder} = $attr->{value};
 4953:         } elsif ("@state" eq "CONTENT FLAGS LAUNCHINNEWWINDOW" ) {
 4954:             $$settings{newwindow} = $attr->{value};
 4955:         } elsif ("@state" eq "CONTENT FILES $filetag") {
 4956:             %{$$settings{files}[$filecount]} = ();
 4957:             %{$$settings{files}[$filecount]{registry}} = (); 
 4958:         } elsif ("@state" eq "CONTENT FILES FILEREF RELFILE" ) {
 4959:             $$settings{files}[$filecount]{'relfile'} = $attr->{value};
 4960:             push @allrelfiles, $attr->{value};
 4961:         } elsif ("@state" eq "CONTENT FILES $filetag MIMETYPE") {
 4962:             $$settings{files}[$filecount]{mimetype} = $attr->{value};
 4963:         } elsif ("@state" eq "CONTENT FILES $filetag CONTENTTYPE") {
 4964:             $$settings{files}[$filecount]{contenttype} = $attr->{value};
 4965:         } elsif ("@state" eq "CONTENT FILES $filetag FILEACTION") {
 4966:             $$settings{files}[$filecount]{fileaction} = $attr->{value};
 4967:         } elsif ("@state" eq "CONTENT FILES $filetag PACKAGEPARENT") {
 4968:             $$settings{files}[$filecount]{packageparent} = $attr->{value};
 4969:         } elsif ("@state" eq "CONTENT FILES $filetag LINKNAME") {
 4970:             $$settings{files}[$filecount]{linkname} = $attr->{value};
 4971:         } elsif ("@state" eq "CONTENT FILES $filetag REGISTRY REGISTRYENTRY") {
 4972:             my $key = $attr->{key};
 4973:             $$settings{files}[$filecount]{registry}{$key} = $attr->{value};
 4974:         }
 4975:       }, "tagname, attr"],
 4976:       text_h =>
 4977:       [sub {
 4978:         my ($text) = @_;
 4979:         if ("@state" eq "CONTENT TITLE") {
 4980:             $$settings{title} = $text;
 4981:         } elsif ( ("@state" eq "CONTENT MAINDATA TEXT") || ("@state" eq "CONTENT BODY TEXT") ) {
 4982:             $$settings{maindata}{text} = $text;
 4983:         }  elsif ("@state" eq "CONTENT FILES $filetag REFTEXT") {
 4984:             $$settings{files}[$filecount]{reftext} = $text;
 4985:         } elsif ("@state" eq "CONTENT FILES FILE NAME" ) {
 4986:             $$settings{files}[$filecount]{'relfile'} = $text;
 4987:             push @allrelfiles, $text;
 4988:         }
 4989:        }, "dtext"],
 4990:       end_h =>
 4991:       [sub {
 4992:         my ($tagname) = @_;
 4993:         if ("@state" eq "CONTENT FILES $filetag") {
 4994:             $filecount ++;
 4995:         }
 4996:         pop @state;
 4997:       }, "tagname"],
 4998:      );
 4999:     $p->unbroken_text(1);
 5000:     $p->parse_file($xmlfile);
 5001:     $p->eof;
 5002:     my $linktag = '';
 5003:     my $fontcol = '';
 5004:     if (@{$$settings{files}} > 0) {
 5005:         for (my $filecount=0;  $filecount<@{$$settings{files}}; $filecount++) {
 5006:             if ($$settings{files}[$filecount]{'fileaction'} eq 'embed') {
 5007:                 if ( $$settings{files}[$filecount]{reftext} =~ m#<\!\-\-\s_(\d+)\\_\s\-\-\>#) { 
 5008:                     my $newtag = qq|<img src="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"/>|;
 5009:                     $$settings{maindata}{text} =~ s#<\!\-\-\s_/($1)\\_\s\-\-\>#$newtag#;
 5010:                 } elsif ( $$settings{files}[$filecount]{reftext} =~m#^_/(\d+)\\_$# ) {
 5011:                     my $reftag = $1;
 5012:                     my $newtag;
 5013:                     if ($$settings{files}[$filecount]{mimetype} =~ m/^image/) {
 5014:                         $newtag = qq|<img src="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"|;
 5015:                         if ( defined($$settings{files}[$filecount]{registry}{alttext}) ) {
 5016:                             $newtag .= qq| alt="$$settings{files}[$filecount]{registry}{alttext}"|;
 5017:                         }
 5018:                         if ( defined($$settings{files}[$filecount]{registry}{alignment}) )
 5019: {
 5020:                             $newtag .= qq| align="$$settings{files}[$filecount]{registry}{alignment}"|; 
 5021:                         }
 5022:                         if ( defined($$settings{files}[$filecount]{registry}{border}) ) {
 5023:                             $newtag .= qq| border="$$settings{files}[$filecount]{registry}{border}"|;
 5024:                         }
 5025:                         $newtag .= " />";
 5026:                         my $reftext =  $$settings{files}[$filecount]{reftext};
 5027:                         my $fname = $$settings{files}[$filecount]{'relfile'};
 5028:                         $$settings{maindata}{text} =~ s/<!\-\-\sCOMMENT\sBLOCK\sFOR\sEMBEDDED\sFILE:\s$fname[\s\n]+DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n]+//;
 5029: #                      $$settings{maindata}{text} =~ s/DO\sNOT\sEDIT\sTHIS\sCOMMENT\sBLOCK[\s\n]+//;
 5030:                         $$settings{maindata}{text} =~ s/Move\swhole\scomment\sto\schange\sfile\splacement\swithin\spage\.[\s\n]+//;
 5031:                         $$settings{maindata}{text} =~ s/_\/$reftag\\_/$newtag/;
 5032:                         $$settings{maindata}{text} =~ s/END\sOF\sBLOCK\sON\sNEXT\sLINE[\s\n]+//;
 5033:                         $$settings{maindata}{text} =~ s/\-\->//;
 5034: #                      $$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/;
 5035: #                      print STDERR $$settings{maindata}{text};
 5036:                     }
 5037:                 } else {
 5038:                     my $filename=$$settings{files}[$filecount]{'relfile'};
 5039:                     my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 5040:                     $$settings{maindata}{text} =~ s#(src|SRC|value)=("|&quot;)$filename("|&quot;)#$1="$newfilename"#g;
 5041:                 }
 5042:             } elsif ($$settings{files}[$filecount]{fileaction} eq 'link') {
 5043:                 unless (($$settings{files}[$filecount]{packageparent} ne '') && (grep/^$$settings{files}[$filecount]{packageparent}$/,@{$$settings{files}}) ) {
 5044:                     $linktag .= qq|<a href="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}"|;
 5045:                     if ($$settings{newwindow} eq "true") {
 5046:                         $linktag .= qq| target="$res$filecount"|;
 5047:                     }
 5048:                     foreach my $entry (keys %{$$settings{files}[$filecount]{registry}}) {
 5049:                         $linktag .= qq| $entry="$$settings{files}[$filecount]{registry}{$entry}"|;
 5050:                     }
 5051:                       $linktag .= qq|>$$settings{files}[$filecount]{linkname}</a><br/>\n|;
 5052:                 }
 5053:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'PACKAGE') || ($$settings{files}[$filecount]{fileaction} eq 'package') ) {
 5054:                my $open_package = '';
 5055:                if ($$settings{files}[$filecount]{'relfile'} =~ m|\.zip$|i) {
 5056:                    $open_package = &expand_zip("$docroot/$res",$$settings{files}[$filecount]{'relfile'});
 5057:                }
 5058:                if ($open_package eq 'ok') {
 5059:                    opendir(DIR,"$docroot/$res");
 5060:                    my @dircontents = grep(!/^\./,readdir(DIR));
 5061:                    closedir(DIR);
 5062:                    push @{$resrcfiles}, @dircontents;
 5063:                    @{$$hrefs{$res}} = @dircontents;
 5064:                    push @{$packages}, $res;
 5065:                }
 5066:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'BROKEN_IMAGE') && ($cms eq 'bb6') ) {
 5067:                 my $filename=$$settings{files}[$filecount]{'relfile'};
 5068:                 my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 5069:                 $$settings{maindata}{text} =~ s#(src|SRC|value)=("|&quot;)$filename("|&quot;)#$1="$newfilename"#g;
 5070:             } elsif ( ($$settings{files}[$filecount]{fileaction} eq 'LINK') && ($cms eq 'bb6') ) {
 5071:                 my $filename=$$settings{files}[$filecount]{'relfile'};
 5072:                 my $newfilename="$destresdir/resfiles/$res/$$settings{files}[$filecount]{relfile}";
 5073:                 my $filetitle = $$settings{files}[$filecount]{'linkname'};
 5074:                 $$settings{maindata}{text} = '<a href="'.$newfilename.'">'.$filetitle.'</a><br /><br />'. $$settings{maindata}{text};
 5075:             }
 5076:         }
 5077:     }
 5078:     if (defined($$settings{maindata}{textcolor})) {
 5079:         $fontcol =  qq|<font color="$$settings{maindata}{textcolor}">|;
 5080:     }
 5081:     if (defined($$settings{maindata}{text})) {
 5082:         if ($$settings{maindata}{bodytype} eq "S") {
 5083:             $$settings{maindata}{text} =~ s#\n#<br/>#g;
 5084:         }
 5085:         if ($$settings{maindata}{ishtml} eq "false") {
 5086:             if ($$settings{maindata}{isnewline} eq "true") {
 5087:                 $$settings{maindata}{text} =~ s#\n#<br/>#g;
 5088:             }
 5089:         } else {
 5090: #            $$settings{maindata}{text} = &HTML::Entities::decode($$settings{maindata}{text});
 5091:         }
 5092:     }
 5093: 
 5094:     if (!open(FILE,">$destdir/resfiles/$res.html")) {
 5095:         &Apache::lonnet::logthis("IMS import error: Cannot open file - $destdir/resfiles/$res.html - $!");
 5096:     } else {
 5097:         push @{$resrcfiles}, "$res.html";
 5098:         my $htmldoc = 0;
 5099: #        if ($$settings{maindata}{text} =~ m-&lt;(html|HTML)>.+&lt;\\(html|HTML)-) {
 5100:         if ($$settings{maindata}{text} =~ m-<(html|HTML)>-) {
 5101:             $htmldoc = 1;
 5102:         }
 5103:         unless ($htmldoc) {
 5104:             print FILE qq|<html>
 5105: <head>
 5106: <title>$$settings{title}</title>
 5107: </head>
 5108: <body bgcolor='#ffffff'>
 5109: $fontcol
 5110: |;
 5111:         }
 5112:         unless ($$settings{title} eq '') { 
 5113:             print FILE qq|$$settings{title}<br/><br/>\n|;
 5114:         }
 5115:         print FILE qq|
 5116: $$settings{maindata}{text}
 5117: $linktag|;
 5118:         unless ($htmldoc) {
 5119:             if (defined($$settings{maindata}{textcolor})) {
 5120:                 print FILE qq|</font>|;
 5121:             }
 5122:             print FILE qq|
 5123:   </body>
 5124:  </html>|;
 5125:         }
 5126:         close(FILE);
 5127:     }
 5128: }
 5129: 
 5130: 
 5131: sub process_angelboards {
 5132:     my ($context,$destdir,$boards,$timestamp,$crs,$cdom,$uname,$db_handling,$messages,$items,$resources,$hrefs,$tempdir,$longcrs) = @_;
 5133:     for (my $i=0; $i<@{$boards}; $i++) {
 5134:         my %msgidx = ();
 5135:         my $forumtext = '';
 5136:         my $boardname = 'bulletinpage_'.$$timestamp[$i];
 5137:         my $forumfile = $tempdir.'/_assoc/'.$$boards[$i].'/pg'.$$boards[$i].'.htm';
 5138:         my @state = ();
 5139:         my $p = HTML::Parser->new
 5140:         (
 5141:            xml_mode => 1,
 5142:            start_h =>
 5143:            [sub {
 5144:                 my ($tagname, $attr) = @_;
 5145:                 push @state, $tagname;
 5146:                 },  "tagname, attr"],
 5147:            text_h =>
 5148:            [sub {
 5149:                 my ($text) = @_;
 5150:                 if ("@state" eq "html body div div") {
 5151:                     $forumtext = $text;
 5152:                 }
 5153:               }, "dtext"],
 5154:             end_h =>
 5155:             [sub {
 5156:                   my ($tagname) = @_;
 5157:                   pop @state;
 5158:                }, "tagname"],
 5159:         );
 5160:         $p->parse_file($forumfile);
 5161:         $p->eof;
 5162: 
 5163:         my %boardinfo = (
 5164:                   'aaa_title' => $$items{$$resources{$$boards[$i]}{revitm}}{title},
 5165:                   'bbb_content' => $forumtext,
 5166:                   'ccc_webreferences' => '',
 5167:                   'uploaded.lastmodified' => time,
 5168:                   );
 5169:         my $msgcount = 0; 
 5170:                                                                                                      
 5171:         my $putresult = &Apache::lonnet::put($boardname,\%boardinfo,$cdom,$crs);
 5172:         if ($db_handling eq 'importall') {
 5173:             foreach my $msg_id (@{$$messages{$$boards[$i]}}) {
 5174:                 $msgcount ++;
 5175:                 $msgidx{$msg_id} = $msgcount;
 5176:                 my %contrib = (
 5177:                             'sendername' => 'NoName',
 5178:                             'senderdomain' => $cdom,
 5179:                             'screenname' => '',
 5180:                             'message' => $$items{$$resources{$msg_id}{revitm}}{title}
 5181:                             );
 5182:                 unless ( $$items{$$resources{$msg_id}{revitm}}{parentseq} eq $$resources{$$boards[$i]}{revitm} ) {
 5183:                     unless ( $msgidx{$$items{$$items{$$resources{$msg_id}{revitm}}{parentseq}}{resnum}} eq ''){
 5184:                         $contrib{replyto} = $msgidx{$$items{$$items{$$resources{$msg_id}{revitm}}{parentseq}}{resnum}};
 5185:                     }
 5186:                 }
 5187:                 if ( @{$$hrefs{$msg_id}} > 1 )  {
 5188:                     my $newurl = '';
 5189:                     foreach my $file (@{$$hrefs{$msg_id}}) {
 5190:                         unless ($file eq 'pg'.$msg_id.'.htm') {
 5191:                             $newurl = $msg_id.$file;
 5192:                              unless ($longcrs eq '') {
 5193:                                 if ($context eq 'CSTR') {
 5194:                                     if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles") {
 5195:                                         mkdir("/home/httpd/lonUsers/$cdom/$longcrs/userfiles",0755);
 5196:                                     }
 5197:                                     if (!-e "/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl") {
 5198:                                         rename("$destdir/resfiles/$msg_id/$file","/home/httpd/lonUsers/$cdom/$longcrs/userfiles/$newurl");
 5199:                                     }
 5200:                                 }
 5201:                                 $contrib{attachmenturl} = '/uploaded/'.$cdom.'/'.$crs.'/'.$file;
 5202:                             }
 5203:                         }
 5204:                     }
 5205:                 }
 5206:                 my $xmlfile = $tempdir.'/_assoc/'.$msg_id.'/'.$$resources{$msg_id}{file};
 5207:                 &angel_message($msg_id,\%contrib,$xmlfile);
 5208:                 unless ($$resources{$msg_id}{file} eq '') {
 5209:                     unlink($xmlfile);
 5210:                 }
 5211:                 my $symb = 'bulletin___'.$$timestamp[$i].'___adm/wrapper/adm/'.$cdom.'/'.$uname.'/'.$$timestamp[$i].'/bulletinboard';
 5212:                 my $postresult = &addposting($symb,\%contrib,$cdom,$crs);
 5213:             }
 5214:         }
 5215:     }
 5216: }
 5217: 
 5218: # ---------------------------------------------------------------- Process ANGEL message board messages
 5219: sub angel_message {
 5220:     my ($msg_id,$contrib,$xmlfile) = @_;
 5221:     my @state = ();
 5222:     my $p = HTML::Parser->new
 5223:     (
 5224:        xml_mode => 1,
 5225:        start_h =>
 5226:        [sub {
 5227:              my ($tagname, $attr) = @_;
 5228:              push @state, $tagname;
 5229:              },  "tagname, attr"],
 5230:         text_h =>
 5231:         [sub {
 5232:              my ($text) = @_;
 5233:              if ("@state" eq "html body table tr td div small span") {
 5234:                   $$contrib{'plainname'} = $text;
 5235:              } elsif ("@state" eq "html body div div") {
 5236:                   $$contrib{'message'} .= '<br /><br />'.$text;
 5237:              }
 5238:            }, "dtext"],
 5239:          end_h =>
 5240:          [sub {
 5241:                my ($tagname) = @_;
 5242:                pop @state;
 5243:             }, "tagname"],
 5244:     );
 5245:     $p->parse_file($xmlfile);
 5246:     $p->eof;
 5247: }
 5248: 
 5249: # ---------------------------------------------------------------- ANGEL content
 5250: sub angel_content {
 5251:     my ($res,$docroot,$destdir,$settings,$dom,$user,$type,$title,$resrcfiles) = @_;
 5252:     my $xmlfile = $docroot.'/_assoc/'.$res.'/pg'.$res.'.htm';
 5253:     my $filecount = 0;
 5254:     my $firstline;
 5255:     my $lastline;
 5256:     my @buffer = ();
 5257:     my @state;
 5258:     @{$$settings{links}} = ();
 5259:     my $p = HTML::Parser->new
 5260:     (
 5261:        xml_mode => 1,
 5262:        start_h =>
 5263:        [sub {
 5264:              my ($tagname, $attr) = @_;
 5265:              push @state, $tagname;
 5266:             },  "tagname, attr"],
 5267:        text_h =>
 5268:        [sub {
 5269:              my ($text) = @_;
 5270:              if ("@state" eq "html body table tr td div small span") {
 5271:                  $$settings{'subtitle'} = $text;
 5272:              } elsif ("@state" eq "html body div div") {
 5273:                  $$settings{'text'} = $text;
 5274:              } elsif ("@state" eq "html body div div a") {
 5275:                 push @{$$settings{'links'}}, $text;
 5276:              }
 5277:             }, "dtext"],
 5278:        end_h =>
 5279:        [sub {
 5280:              my ($tagname) = @_;
 5281:              pop @state;
 5282:             }, "tagname"],
 5283:     );
 5284:     $p->parse_file($xmlfile);
 5285:     $p->eof;
 5286:     if ($type eq "PAGE") {
 5287:         open(FILE,"<$xmlfile");
 5288:         @buffer = <FILE>;
 5289:         close(FILE);
 5290:         chomp(@buffer);
 5291:         $firstline = -1;
 5292:         $lastline = 0;
 5293:         for (my $i=0; $i<@buffer; $i++) {
 5294:             if (($firstline == -1) && ($buffer[$i] =~ m/<div\sclass="normalDiv"><div\sclass="normalSpan">/)) {
 5295:                 $firstline = $i;
 5296:                 $buffer[$i] = substr($buffer[$i],index($buffer[$i],'"normalSpan"')+13);
 5297:             }
 5298:             if (($firstline > -1) && ($buffer[$i] =~ m-<p></p></div></div>-)) {
 5299:                 $buffer[$i] = substr($buffer[$i],0,index($buffer[$i],'<p></p></div></div>'));
 5300:                 $lastline = $i;
 5301:             }
 5302:         }
 5303:     }
 5304:     open(FILE,">$destdir/resfiles/$res.html");
 5305:     push @{$resrcfiles}, "$res.html";
 5306:     print FILE qq|<html>
 5307: <head>
 5308: <title>$title</title>
 5309: </head>
 5310: <body bgcolor='#ffffff'>
 5311:     |;
 5312:     unless ($title eq '') {
 5313:         print FILE qq|<b>$title</b><br/>\n|;
 5314:     }
 5315:     unless ($$settings{subtitle} eq '') {
 5316:         print FILE qq|$$settings{subtitle}<br/>\n|;
 5317:     }
 5318:     print FILE "<br/>\n";
 5319:     if ($type eq "LINK") {
 5320:         foreach my $link (@{$$settings{links}}) {
 5321:             print FILE qq|<a href="$link">$link</a><br/>\n|; 
 5322:         }
 5323:     } elsif ($type eq "PAGE") {
 5324:         if ($firstline > -1) {
 5325:             for (my $i=$firstline; $i<=$lastline; $i++) {
 5326:                 print FILE "$buffer[$i]\n";
 5327:             }
 5328:         }
 5329:     }
 5330:     print FILE qq|
 5331:   </body>
 5332:  </html>|;
 5333:     close(FILE);
 5334: }
 5335: 
 5336: # ---------------------------------------------------------------- WebCT content
 5337: sub webct4_content {
 5338:     my ($res,$docroot,$destdir,$settings,$dom,$user,$type,$title,$resrcfiles) = @_;
 5339:     if (defined($$settings{url})) {
 5340:         if (!open(FILE,">$destdir/resfiles/$res.html")) {
 5341:             &Apache::lonnet::logthis("IMS import error: Cannot open file - $destdir/resfiles/$res.html - $!");
 5342:         } else {
 5343:             push(@{$resrcfiles}, "$res.html");
 5344:             my $linktag = qq|<a href="$$settings{url}"|;
 5345:             if ($title ne '') {
 5346:                 $linktag .= qq|>$title</a>|;
 5347:             } else {
 5348:                 $linktag .= qq|>$$settings{url}|;
 5349:             }
 5350:             print FILE qq|<html>
 5351: <head>
 5352: <title>$title</title>
 5353: </head>
 5354: <body bgcolor='#ffffff'>
 5355: $linktag
 5356: </body>
 5357: </html>|;
 5358:             close(FILE);
 5359:         }
 5360:     }
 5361: }
 5362: 
 5363: sub process_html {
 5364:     my ($text,$caller,$html_cond,$context,$res,$dirname,$cdom,$cnum,$docroot,$destdir) = @_;
 5365:     my $pathstart;
 5366:     if ($context eq 'CSTR') {
 5367:         $pathstart = '../..';
 5368:     } else {
 5369:         $pathstart = $dirname;
 5370:     }
 5371:     if ($caller eq 'bb5') {
 5372:         if ($html_cond eq 'true') {
 5373:             $$text = &HTML::Entities::decode($$text);
 5374:         }
 5375:     } elsif ($caller eq 'bb6') {
 5376:         if ($html_cond eq 'HTML') {
 5377:             $$text = &HTML::Entities::decode($$text);
 5378:         }
 5379:     }
 5380:     if ($$text =~ m#<img src=['"]?(https?://[^\s]+/)([^/\s\'"]+)['"]?[^>]*>#) {
 5381:         if (&retrieve_image($context,$res,$dirname,$cdom,$cnum,$docroot,$destdir,$1,$2) eq 'ok') {
 5382:             $$text =~ s#(<img src=['"]?)(https?://[^\s]+/)([^/\s'"]+)(['"]?[^>]*>)#$1$pathstart/resfiles/$res/webimages/$3$4#g;
 5383:         }
 5384:     }
 5385:     $$text =~ s#(<img src=[^>]+)/*>#$1 />#gi;
 5386:     $$text =~ s#<br>#<br />#g;
 5387:     return;
 5388: }
 5389: 
 5390: sub add_images_links {
 5391:     my ($type,$context,$settings,$id,$dirname,$res) = @_;
 5392:     my ($image,$imglink,$url,$pathstart);
 5393:     if ($context eq 'CSTR') {
 5394:         $pathstart = '../..';
 5395:     } else {
 5396:         $pathstart = $dirname;
 5397:     }
 5398:     if ((defined($$settings{$id}{$type}{image})) && ($$settings{$id}{$type}{image} ne '')) {
 5399:         if ( $$settings{$id}{$type}{style} eq 'Inline' ) {
 5400:             $image = qq|<br /><img src="$pathstart/resfiles/$res/$$settings{$id}{$type}{image}" alt="$$settings{$id}{$type}{label}"/><br />|;
 5401:         } else {
 5402:             $imglink = qq|<br /><a href="$pathstart/resfiles/$res/$$settings{$id}{$type}{image}">$$settings{$id}{$type}{label}</a><br />|;
 5403:         }
 5404:     }
 5405:     if ((defined($$settings{$id}{$type}{link})) && ($$settings{$id}{$type}{link} ne '' )) {
 5406:         $url = qq|<br /><a href="$$settings{$id}{$type}{link}">$$settings{$id}{$type}{linkname}</a><br />|;
 5407:     }
 5408:     return $image.$imglink.$url; 
 5409: }
 5410: 
 5411: sub remove_html {
 5412:     my ($choice_text) = @_;
 5413:     return $choice_text;
 5414: }
 5415: 
 5416: 
 5417: 1;
 5418: __END__

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