File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.44: download - view: text, annotated - select for diffs
Mon Aug 25 13:43:39 2008 UTC (15 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Changes to support import of standalone question library from WebCT (d2lquestionlibrary type, with filename of questiondb.xml; items tagged as "objectbank" in XML file).
- rcardinality set to "Single" if only one answer is correct (even if questiondb.xml has attribute set to "Multiple").
- correctness of response based on numerical test of text defined by setvar tag.
- itemfeedback accumulated and displayed in <postanswerdate> block for all response types.

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

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