File:  [LON-CAPA] / loncom / imspackages / imsprocessor.pm
Revision 1.16: download - view: text, annotated - select for diffs
Tue Mar 15 15:25:32 2005 UTC (19 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Various fixes to import of questions from webct4 question database. Notably:
attempt to handle underspecified string response type questions, by assuming order of answers (in resprocessing area)  matches order of answer boxes in a multi-part problem.

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

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