File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.34: download - view: text, annotated - select for diffs
Fri Mar 24 17:36:27 2006 UTC (18 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Existing webct4 code identified as appropriate for WebCT 4 Campus Edition. Vista to follow.

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

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