File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.36: download - view: text, annotated - select for diffs
Tue Apr 4 17:42:28 2006 UTC (18 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Import of WebCT Vista 4 "Combination" question type supported (maps to LC problem with radioresponse).

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

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