File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.52: download - view: text, annotated - select for diffs
Sat Jul 27 22:04:49 2013 UTC (10 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support import of Blackboard Vista question banks (IMS format) containing
  questions with images.

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

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