File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.41: download - view: text, annotated - select for diffs
Sun Sep 30 13:20:08 2007 UTC (16 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, HEAD
WebCT Campus Edition 4.1 differences
- varname attribute in <setvar> is set to que_score instead of answerValue
- <matimage> inside <presentation><flow><material>

Fix typo:
Replace webct4ce with webctce4

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

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